f8153059f7d9f9a59628ece83cf4cf188ec78670
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "intl.h"
33 #include "pointer-set.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "toplev.h"
42 #include "timevar.h"
43 #include "tree-iterator.h"
44
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
48
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
55 };
56
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
59
60 int processing_template_parmlist;
61 static int template_header_count;
62
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
65
66 static GTY(()) struct tinst_level *current_tinst_level;
67
68 static GTY(()) tree saved_access_scope;
69
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
74
75 /* A map from local variable declarations in the body of the template
76 presently being instantiated to the corresponding instantiated
77 local variables. */
78 static struct pointer_map_t *local_specializations;
79
80 /* True if we've recursed into fn_type_unification too many times. */
81 static bool excessive_deduction_depth;
82
83 typedef struct GTY(()) spec_entry
84 {
85 tree tmpl;
86 tree args;
87 tree spec;
88 } spec_entry;
89
90 static GTY ((param_is (spec_entry)))
91 htab_t decl_specializations;
92
93 static GTY ((param_is (spec_entry)))
94 htab_t type_specializations;
95
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
101
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
110
111 enum template_base_result {
112 tbr_incomplete_type,
113 tbr_ambiguous_baseclass,
114 tbr_success
115 };
116
117 static void push_access_scope (tree);
118 static void pop_access_scope (tree);
119 static bool resolve_overloaded_unification (tree, tree, tree, tree,
120 unification_kind_t, int,
121 bool);
122 static int try_one_overload (tree, tree, tree, tree, tree,
123 unification_kind_t, int, bool, bool);
124 static int unify (tree, tree, tree, tree, int, bool);
125 static void add_pending_template (tree);
126 static tree reopen_tinst_level (struct tinst_level *);
127 static tree tsubst_initializer_list (tree, tree);
128 static tree get_class_bindings (tree, tree, tree, tree);
129 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
130 bool, bool);
131 static tree coerce_innermost_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 tree current_template_args (void);
205 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
206 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
207
208 /* Make the current scope suitable for access checking when we are
209 processing T. T can be FUNCTION_DECL for instantiated function
210 template, VAR_DECL for static member variable, or TYPE_DECL for
211 alias template (needed by instantiate_decl). */
212
213 static void
214 push_access_scope (tree t)
215 {
216 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
217 || TREE_CODE (t) == TYPE_DECL
218 || TREE_CODE (t) == VAR_DECL);
219
220 if (DECL_FRIEND_CONTEXT (t))
221 push_nested_class (DECL_FRIEND_CONTEXT (t));
222 else if (DECL_CLASS_SCOPE_P (t))
223 push_nested_class (DECL_CONTEXT (t));
224 else
225 push_to_top_level ();
226
227 if (TREE_CODE (t) == FUNCTION_DECL)
228 {
229 saved_access_scope = tree_cons
230 (NULL_TREE, current_function_decl, saved_access_scope);
231 current_function_decl = t;
232 }
233 }
234
235 /* Restore the scope set up by push_access_scope. T is the node we
236 are processing. */
237
238 static void
239 pop_access_scope (tree t)
240 {
241 if (TREE_CODE (t) == FUNCTION_DECL)
242 {
243 current_function_decl = TREE_VALUE (saved_access_scope);
244 saved_access_scope = TREE_CHAIN (saved_access_scope);
245 }
246
247 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
248 pop_nested_class ();
249 else
250 pop_from_top_level ();
251 }
252
253 /* Do any processing required when DECL (a member template
254 declaration) is finished. Returns the TEMPLATE_DECL corresponding
255 to DECL, unless it is a specialization, in which case the DECL
256 itself is returned. */
257
258 tree
259 finish_member_template_decl (tree decl)
260 {
261 if (decl == error_mark_node)
262 return error_mark_node;
263
264 gcc_assert (DECL_P (decl));
265
266 if (TREE_CODE (decl) == TYPE_DECL)
267 {
268 tree type;
269
270 type = TREE_TYPE (decl);
271 if (type == error_mark_node)
272 return error_mark_node;
273 if (MAYBE_CLASS_TYPE_P (type)
274 && CLASSTYPE_TEMPLATE_INFO (type)
275 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
276 {
277 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
278 check_member_template (tmpl);
279 return tmpl;
280 }
281 return NULL_TREE;
282 }
283 else if (TREE_CODE (decl) == FIELD_DECL)
284 error ("data member %qD cannot be a member template", decl);
285 else if (DECL_TEMPLATE_INFO (decl))
286 {
287 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
288 {
289 check_member_template (DECL_TI_TEMPLATE (decl));
290 return DECL_TI_TEMPLATE (decl);
291 }
292 else
293 return decl;
294 }
295 else
296 error ("invalid member template declaration %qD", decl);
297
298 return error_mark_node;
299 }
300
301 /* Create a template info node. */
302
303 tree
304 build_template_info (tree template_decl, tree template_args)
305 {
306 tree result = make_node (TEMPLATE_INFO);
307 TI_TEMPLATE (result) = template_decl;
308 TI_ARGS (result) = template_args;
309 return result;
310 }
311
312 /* Return the template info node corresponding to T, whatever T is. */
313
314 tree
315 get_template_info (const_tree t)
316 {
317 tree tinfo = NULL_TREE;
318
319 if (!t || t == error_mark_node)
320 return NULL;
321
322 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
323 tinfo = DECL_TEMPLATE_INFO (t);
324
325 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
326 t = TREE_TYPE (t);
327
328 if (TAGGED_TYPE_P (t))
329 tinfo = TYPE_TEMPLATE_INFO (t);
330 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
331 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
332
333 return tinfo;
334 }
335
336 /* Returns the template nesting level of the indicated class TYPE.
337
338 For example, in:
339 template <class T>
340 struct A
341 {
342 template <class U>
343 struct B {};
344 };
345
346 A<T>::B<U> has depth two, while A<T> has depth one.
347 Both A<T>::B<int> and A<int>::B<U> have depth one, if
348 they are instantiations, not specializations.
349
350 This function is guaranteed to return 0 if passed NULL_TREE so
351 that, for example, `template_class_depth (current_class_type)' is
352 always safe. */
353
354 int
355 template_class_depth (tree type)
356 {
357 int depth;
358
359 for (depth = 0;
360 type && TREE_CODE (type) != NAMESPACE_DECL;
361 type = (TREE_CODE (type) == FUNCTION_DECL)
362 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
363 {
364 tree tinfo = get_template_info (type);
365
366 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
367 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
368 ++depth;
369 }
370
371 return depth;
372 }
373
374 /* Subroutine of maybe_begin_member_template_processing.
375 Returns true if processing DECL needs us to push template parms. */
376
377 static bool
378 inline_needs_template_parms (tree decl)
379 {
380 if (! DECL_TEMPLATE_INFO (decl))
381 return false;
382
383 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
384 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
385 }
386
387 /* Subroutine of maybe_begin_member_template_processing.
388 Push the template parms in PARMS, starting from LEVELS steps into the
389 chain, and ending at the beginning, since template parms are listed
390 innermost first. */
391
392 static void
393 push_inline_template_parms_recursive (tree parmlist, int levels)
394 {
395 tree parms = TREE_VALUE (parmlist);
396 int i;
397
398 if (levels > 1)
399 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
400
401 ++processing_template_decl;
402 current_template_parms
403 = tree_cons (size_int (processing_template_decl),
404 parms, current_template_parms);
405 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
406
407 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
408 NULL);
409 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
410 {
411 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
412
413 if (parm == error_mark_node)
414 continue;
415
416 gcc_assert (DECL_P (parm));
417
418 switch (TREE_CODE (parm))
419 {
420 case TYPE_DECL:
421 case TEMPLATE_DECL:
422 pushdecl (parm);
423 break;
424
425 case PARM_DECL:
426 {
427 /* Make a CONST_DECL as is done in process_template_parm.
428 It is ugly that we recreate this here; the original
429 version built in process_template_parm is no longer
430 available. */
431 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
432 CONST_DECL, DECL_NAME (parm),
433 TREE_TYPE (parm));
434 DECL_ARTIFICIAL (decl) = 1;
435 TREE_CONSTANT (decl) = 1;
436 TREE_READONLY (decl) = 1;
437 DECL_INITIAL (decl) = DECL_INITIAL (parm);
438 SET_DECL_TEMPLATE_PARM_P (decl);
439 pushdecl (decl);
440 }
441 break;
442
443 default:
444 gcc_unreachable ();
445 }
446 }
447 }
448
449 /* Restore the template parameter context for a member template or
450 a friend template defined in a class definition. */
451
452 void
453 maybe_begin_member_template_processing (tree decl)
454 {
455 tree parms;
456 int levels = 0;
457
458 if (inline_needs_template_parms (decl))
459 {
460 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
461 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
462
463 if (DECL_TEMPLATE_SPECIALIZATION (decl))
464 {
465 --levels;
466 parms = TREE_CHAIN (parms);
467 }
468
469 push_inline_template_parms_recursive (parms, levels);
470 }
471
472 /* Remember how many levels of template parameters we pushed so that
473 we can pop them later. */
474 inline_parm_levels.safe_push (levels);
475 }
476
477 /* Undo the effects of maybe_begin_member_template_processing. */
478
479 void
480 maybe_end_member_template_processing (void)
481 {
482 int i;
483 int last;
484
485 if (inline_parm_levels.length () == 0)
486 return;
487
488 last = inline_parm_levels.pop ();
489 for (i = 0; i < last; ++i)
490 {
491 --processing_template_decl;
492 current_template_parms = TREE_CHAIN (current_template_parms);
493 poplevel (0, 0, 0);
494 }
495 }
496
497 /* Return a new template argument vector which contains all of ARGS,
498 but has as its innermost set of arguments the EXTRA_ARGS. */
499
500 static tree
501 add_to_template_args (tree args, tree extra_args)
502 {
503 tree new_args;
504 int extra_depth;
505 int i;
506 int j;
507
508 if (args == NULL_TREE || extra_args == error_mark_node)
509 return extra_args;
510
511 extra_depth = TMPL_ARGS_DEPTH (extra_args);
512 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
513
514 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
515 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
516
517 for (j = 1; j <= extra_depth; ++j, ++i)
518 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
519
520 return new_args;
521 }
522
523 /* Like add_to_template_args, but only the outermost ARGS are added to
524 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
525 (EXTRA_ARGS) levels are added. This function is used to combine
526 the template arguments from a partial instantiation with the
527 template arguments used to attain the full instantiation from the
528 partial instantiation. */
529
530 static tree
531 add_outermost_template_args (tree args, tree extra_args)
532 {
533 tree new_args;
534
535 /* If there are more levels of EXTRA_ARGS than there are ARGS,
536 something very fishy is going on. */
537 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
538
539 /* If *all* the new arguments will be the EXTRA_ARGS, just return
540 them. */
541 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
542 return extra_args;
543
544 /* For the moment, we make ARGS look like it contains fewer levels. */
545 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
546
547 new_args = add_to_template_args (args, extra_args);
548
549 /* Now, we restore ARGS to its full dimensions. */
550 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
551
552 return new_args;
553 }
554
555 /* Return the N levels of innermost template arguments from the ARGS. */
556
557 tree
558 get_innermost_template_args (tree args, int n)
559 {
560 tree new_args;
561 int extra_levels;
562 int i;
563
564 gcc_assert (n >= 0);
565
566 /* If N is 1, just return the innermost set of template arguments. */
567 if (n == 1)
568 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
569
570 /* If we're not removing anything, just return the arguments we were
571 given. */
572 extra_levels = TMPL_ARGS_DEPTH (args) - n;
573 gcc_assert (extra_levels >= 0);
574 if (extra_levels == 0)
575 return args;
576
577 /* Make a new set of arguments, not containing the outer arguments. */
578 new_args = make_tree_vec (n);
579 for (i = 1; i <= n; ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i,
581 TMPL_ARGS_LEVEL (args, i + extra_levels));
582
583 return new_args;
584 }
585
586 /* The inverse of get_innermost_template_args: Return all but the innermost
587 EXTRA_LEVELS levels of template arguments from the ARGS. */
588
589 static tree
590 strip_innermost_template_args (tree args, int extra_levels)
591 {
592 tree new_args;
593 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
594 int i;
595
596 gcc_assert (n >= 0);
597
598 /* If N is 1, just return the outermost set of template arguments. */
599 if (n == 1)
600 return TMPL_ARGS_LEVEL (args, 1);
601
602 /* If we're not removing anything, just return the arguments we were
603 given. */
604 gcc_assert (extra_levels >= 0);
605 if (extra_levels == 0)
606 return args;
607
608 /* Make a new set of arguments, not containing the inner arguments. */
609 new_args = make_tree_vec (n);
610 for (i = 1; i <= n; ++i)
611 SET_TMPL_ARGS_LEVEL (new_args, i,
612 TMPL_ARGS_LEVEL (args, i));
613
614 return new_args;
615 }
616
617 /* We've got a template header coming up; push to a new level for storing
618 the parms. */
619
620 void
621 begin_template_parm_list (void)
622 {
623 /* We use a non-tag-transparent scope here, which causes pushtag to
624 put tags in this scope, rather than in the enclosing class or
625 namespace scope. This is the right thing, since we want
626 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
627 global template class, push_template_decl handles putting the
628 TEMPLATE_DECL into top-level scope. For a nested template class,
629 e.g.:
630
631 template <class T> struct S1 {
632 template <class T> struct S2 {};
633 };
634
635 pushtag contains special code to call pushdecl_with_scope on the
636 TEMPLATE_DECL for S2. */
637 begin_scope (sk_template_parms, NULL);
638 ++processing_template_decl;
639 ++processing_template_parmlist;
640 note_template_header (0);
641 }
642
643 /* This routine is called when a specialization is declared. If it is
644 invalid to declare a specialization here, an error is reported and
645 false is returned, otherwise this routine will return true. */
646
647 static bool
648 check_specialization_scope (void)
649 {
650 tree scope = current_scope ();
651
652 /* [temp.expl.spec]
653
654 An explicit specialization shall be declared in the namespace of
655 which the template is a member, or, for member templates, in the
656 namespace of which the enclosing class or enclosing class
657 template is a member. An explicit specialization of a member
658 function, member class or static data member of a class template
659 shall be declared in the namespace of which the class template
660 is a member. */
661 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
662 {
663 error ("explicit specialization in non-namespace scope %qD", scope);
664 return false;
665 }
666
667 /* [temp.expl.spec]
668
669 In an explicit specialization declaration for a member of a class
670 template or a member template that appears in namespace scope,
671 the member template and some of its enclosing class templates may
672 remain unspecialized, except that the declaration shall not
673 explicitly specialize a class member template if its enclosing
674 class templates are not explicitly specialized as well. */
675 if (current_template_parms)
676 {
677 error ("enclosing class templates are not explicitly specialized");
678 return false;
679 }
680
681 return true;
682 }
683
684 /* We've just seen template <>. */
685
686 bool
687 begin_specialization (void)
688 {
689 begin_scope (sk_template_spec, NULL);
690 note_template_header (1);
691 return check_specialization_scope ();
692 }
693
694 /* Called at then end of processing a declaration preceded by
695 template<>. */
696
697 void
698 end_specialization (void)
699 {
700 finish_scope ();
701 reset_specialization ();
702 }
703
704 /* Any template <>'s that we have seen thus far are not referring to a
705 function specialization. */
706
707 void
708 reset_specialization (void)
709 {
710 processing_specialization = 0;
711 template_header_count = 0;
712 }
713
714 /* We've just seen a template header. If SPECIALIZATION is nonzero,
715 it was of the form template <>. */
716
717 static void
718 note_template_header (int specialization)
719 {
720 processing_specialization = specialization;
721 template_header_count++;
722 }
723
724 /* We're beginning an explicit instantiation. */
725
726 void
727 begin_explicit_instantiation (void)
728 {
729 gcc_assert (!processing_explicit_instantiation);
730 processing_explicit_instantiation = true;
731 }
732
733
734 void
735 end_explicit_instantiation (void)
736 {
737 gcc_assert (processing_explicit_instantiation);
738 processing_explicit_instantiation = false;
739 }
740
741 /* An explicit specialization or partial specialization of TMPL is being
742 declared. Check that the namespace in which the specialization is
743 occurring is permissible. Returns false iff it is invalid to
744 specialize TMPL in the current namespace. */
745
746 static bool
747 check_specialization_namespace (tree tmpl)
748 {
749 tree tpl_ns = decl_namespace_context (tmpl);
750
751 /* [tmpl.expl.spec]
752
753 An explicit specialization shall be declared in the namespace of
754 which the template is a member, or, for member templates, in the
755 namespace of which the enclosing class or enclosing class
756 template is a member. An explicit specialization of a member
757 function, member class or static data member of a class template
758 shall be declared in the namespace of which the class template is
759 a member. */
760 if (current_scope() != DECL_CONTEXT (tmpl)
761 && !at_namespace_scope_p ())
762 {
763 error ("specialization of %qD must appear at namespace scope", tmpl);
764 return false;
765 }
766 if (is_associated_namespace (current_namespace, tpl_ns))
767 /* Same or super-using namespace. */
768 return true;
769 else
770 {
771 permerror (input_location, "specialization of %qD in different namespace", tmpl);
772 permerror (input_location, " from definition of %q+#D", tmpl);
773 return false;
774 }
775 }
776
777 /* SPEC is an explicit instantiation. Check that it is valid to
778 perform this explicit instantiation in the current namespace. */
779
780 static void
781 check_explicit_instantiation_namespace (tree spec)
782 {
783 tree ns;
784
785 /* DR 275: An explicit instantiation shall appear in an enclosing
786 namespace of its template. */
787 ns = decl_namespace_context (spec);
788 if (!is_ancestor (current_namespace, ns))
789 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
790 "(which does not enclose namespace %qD)",
791 spec, current_namespace, ns);
792 }
793
794 /* The TYPE is being declared. If it is a template type, that means it
795 is a partial specialization. Do appropriate error-checking. */
796
797 tree
798 maybe_process_partial_specialization (tree type)
799 {
800 tree context;
801
802 if (type == error_mark_node)
803 return error_mark_node;
804
805 /* A lambda that appears in specialization context is not itself a
806 specialization. */
807 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
808 return type;
809
810 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
811 {
812 error ("name of class shadows template template parameter %qD",
813 TYPE_NAME (type));
814 return error_mark_node;
815 }
816
817 context = TYPE_CONTEXT (type);
818
819 if (TYPE_ALIAS_P (type))
820 {
821 if (TYPE_TEMPLATE_INFO (type)
822 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
823 error ("specialization of alias template %qD",
824 TYPE_TI_TEMPLATE (type));
825 else
826 error ("explicit specialization of non-template %qT", type);
827 return error_mark_node;
828 }
829 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
830 {
831 /* This is for ordinary explicit specialization and partial
832 specialization of a template class such as:
833
834 template <> class C<int>;
835
836 or:
837
838 template <class T> class C<T*>;
839
840 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
841
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
843 && !COMPLETE_TYPE_P (type))
844 {
845 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
846 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
847 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
848 if (processing_template_decl)
849 {
850 if (push_template_decl (TYPE_MAIN_DECL (type))
851 == error_mark_node)
852 return error_mark_node;
853 }
854 }
855 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
856 error ("specialization of %qT after instantiation", type);
857 else if (errorcount && !processing_specialization
858 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
859 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
860 /* Trying to define a specialization either without a template<> header
861 or in an inappropriate place. We've already given an error, so just
862 bail now so we don't actually define the specialization. */
863 return error_mark_node;
864 }
865 else if (CLASS_TYPE_P (type)
866 && !CLASSTYPE_USE_TEMPLATE (type)
867 && CLASSTYPE_TEMPLATE_INFO (type)
868 && context && CLASS_TYPE_P (context)
869 && CLASSTYPE_TEMPLATE_INFO (context))
870 {
871 /* This is for an explicit specialization of member class
872 template according to [temp.expl.spec/18]:
873
874 template <> template <class U> class C<int>::D;
875
876 The context `C<int>' must be an implicit instantiation.
877 Otherwise this is just a member class template declared
878 earlier like:
879
880 template <> class C<int> { template <class U> class D; };
881 template <> template <class U> class C<int>::D;
882
883 In the first case, `C<int>::D' is a specialization of `C<T>::D'
884 while in the second case, `C<int>::D' is a primary template
885 and `C<T>::D' may not exist. */
886
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
888 && !COMPLETE_TYPE_P (type))
889 {
890 tree t;
891 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
892
893 if (current_namespace
894 != decl_namespace_context (tmpl))
895 {
896 permerror (input_location, "specializing %q#T in different namespace", type);
897 permerror (input_location, " from definition of %q+#D", tmpl);
898 }
899
900 /* Check for invalid specialization after instantiation:
901
902 template <> template <> class C<int>::D<int>;
903 template <> template <class U> class C<int>::D; */
904
905 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
906 t; t = TREE_CHAIN (t))
907 {
908 tree inst = TREE_VALUE (t);
909 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
910 {
911 /* We already have a full specialization of this partial
912 instantiation. Reassign it to the new member
913 specialization template. */
914 spec_entry elt;
915 spec_entry *entry;
916 void **slot;
917
918 elt.tmpl = most_general_template (tmpl);
919 elt.args = CLASSTYPE_TI_ARGS (inst);
920 elt.spec = inst;
921
922 htab_remove_elt (type_specializations, &elt);
923
924 elt.tmpl = tmpl;
925 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
926
927 slot = htab_find_slot (type_specializations, &elt, INSERT);
928 entry = ggc_alloc_spec_entry ();
929 *entry = elt;
930 *slot = entry;
931 }
932 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
933 /* But if we've had an implicit instantiation, that's a
934 problem ([temp.expl.spec]/6). */
935 error ("specialization %qT after instantiation %qT",
936 type, inst);
937 }
938
939 /* Mark TYPE as a specialization. And as a result, we only
940 have one level of template argument for the innermost
941 class template. */
942 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
943 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
944 CLASSTYPE_TI_ARGS (type)
945 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
946 }
947 }
948 else if (processing_specialization)
949 {
950 /* Someday C++0x may allow for enum template specialization. */
951 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
952 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
953 pedwarn (input_location, OPT_Wpedantic, "template specialization "
954 "of %qD not allowed by ISO C++", type);
955 else
956 {
957 error ("explicit specialization of non-template %qT", type);
958 return error_mark_node;
959 }
960 }
961
962 return type;
963 }
964
965 /* Returns nonzero if we can optimize the retrieval of specializations
966 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
967 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
968
969 static inline bool
970 optimize_specialization_lookup_p (tree tmpl)
971 {
972 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
973 && DECL_CLASS_SCOPE_P (tmpl)
974 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
975 parameter. */
976 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
977 /* The optimized lookup depends on the fact that the
978 template arguments for the member function template apply
979 purely to the containing class, which is not true if the
980 containing class is an explicit or partial
981 specialization. */
982 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
983 && !DECL_MEMBER_TEMPLATE_P (tmpl)
984 && !DECL_CONV_FN_P (tmpl)
985 /* It is possible to have a template that is not a member
986 template and is not a member of a template class:
987
988 template <typename T>
989 struct S { friend A::f(); };
990
991 Here, the friend function is a template, but the context does
992 not have template information. The optimized lookup relies
993 on having ARGS be the template arguments for both the class
994 and the function template. */
995 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
996 }
997
998 /* Retrieve the specialization (in the sense of [temp.spec] - a
999 specialization is either an instantiation or an explicit
1000 specialization) of TMPL for the given template ARGS. If there is
1001 no such specialization, return NULL_TREE. The ARGS are a vector of
1002 arguments, or a vector of vectors of arguments, in the case of
1003 templates with more than one level of parameters.
1004
1005 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1006 then we search for a partial specialization matching ARGS. This
1007 parameter is ignored if TMPL is not a class template. */
1008
1009 static tree
1010 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1011 {
1012 if (tmpl == NULL_TREE)
1013 return NULL_TREE;
1014
1015 if (args == error_mark_node)
1016 return NULL_TREE;
1017
1018 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1019
1020 /* There should be as many levels of arguments as there are
1021 levels of parameters. */
1022 gcc_assert (TMPL_ARGS_DEPTH (args)
1023 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1024
1025 if (optimize_specialization_lookup_p (tmpl))
1026 {
1027 tree class_template;
1028 tree class_specialization;
1029 vec<tree, va_gc> *methods;
1030 tree fns;
1031 int idx;
1032
1033 /* The template arguments actually apply to the containing
1034 class. Find the class specialization with those
1035 arguments. */
1036 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1037 class_specialization
1038 = retrieve_specialization (class_template, args, 0);
1039 if (!class_specialization)
1040 return NULL_TREE;
1041 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1042 for the specialization. */
1043 idx = class_method_index_for_fn (class_specialization, tmpl);
1044 if (idx == -1)
1045 return NULL_TREE;
1046 /* Iterate through the methods with the indicated name, looking
1047 for the one that has an instance of TMPL. */
1048 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1049 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1050 {
1051 tree fn = OVL_CURRENT (fns);
1052 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1053 /* using-declarations can add base methods to the method vec,
1054 and we don't want those here. */
1055 && DECL_CONTEXT (fn) == class_specialization)
1056 return fn;
1057 }
1058 return NULL_TREE;
1059 }
1060 else
1061 {
1062 spec_entry *found;
1063 spec_entry elt;
1064 htab_t specializations;
1065
1066 elt.tmpl = tmpl;
1067 elt.args = args;
1068 elt.spec = NULL_TREE;
1069
1070 if (DECL_CLASS_TEMPLATE_P (tmpl))
1071 specializations = type_specializations;
1072 else
1073 specializations = decl_specializations;
1074
1075 if (hash == 0)
1076 hash = hash_specialization (&elt);
1077 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1078 if (found)
1079 return found->spec;
1080 }
1081
1082 return NULL_TREE;
1083 }
1084
1085 /* Like retrieve_specialization, but for local declarations. */
1086
1087 static tree
1088 retrieve_local_specialization (tree tmpl)
1089 {
1090 void **slot;
1091
1092 if (local_specializations == NULL)
1093 return NULL_TREE;
1094
1095 slot = pointer_map_contains (local_specializations, tmpl);
1096 return slot ? (tree) *slot : NULL_TREE;
1097 }
1098
1099 /* Returns nonzero iff DECL is a specialization of TMPL. */
1100
1101 int
1102 is_specialization_of (tree decl, tree tmpl)
1103 {
1104 tree t;
1105
1106 if (TREE_CODE (decl) == FUNCTION_DECL)
1107 {
1108 for (t = decl;
1109 t != NULL_TREE;
1110 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1111 if (t == tmpl)
1112 return 1;
1113 }
1114 else
1115 {
1116 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1117
1118 for (t = TREE_TYPE (decl);
1119 t != NULL_TREE;
1120 t = CLASSTYPE_USE_TEMPLATE (t)
1121 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1122 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1123 return 1;
1124 }
1125
1126 return 0;
1127 }
1128
1129 /* Returns nonzero iff DECL is a specialization of friend declaration
1130 FRIEND_DECL according to [temp.friend]. */
1131
1132 bool
1133 is_specialization_of_friend (tree decl, tree friend_decl)
1134 {
1135 bool need_template = true;
1136 int template_depth;
1137
1138 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1139 || TREE_CODE (decl) == TYPE_DECL);
1140
1141 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1142 of a template class, we want to check if DECL is a specialization
1143 if this. */
1144 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1145 && DECL_TEMPLATE_INFO (friend_decl)
1146 && !DECL_USE_TEMPLATE (friend_decl))
1147 {
1148 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1149 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1150 need_template = false;
1151 }
1152 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1153 && !PRIMARY_TEMPLATE_P (friend_decl))
1154 need_template = false;
1155
1156 /* There is nothing to do if this is not a template friend. */
1157 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1158 return false;
1159
1160 if (is_specialization_of (decl, friend_decl))
1161 return true;
1162
1163 /* [temp.friend/6]
1164 A member of a class template may be declared to be a friend of a
1165 non-template class. In this case, the corresponding member of
1166 every specialization of the class template is a friend of the
1167 class granting friendship.
1168
1169 For example, given a template friend declaration
1170
1171 template <class T> friend void A<T>::f();
1172
1173 the member function below is considered a friend
1174
1175 template <> struct A<int> {
1176 void f();
1177 };
1178
1179 For this type of template friend, TEMPLATE_DEPTH below will be
1180 nonzero. To determine if DECL is a friend of FRIEND, we first
1181 check if the enclosing class is a specialization of another. */
1182
1183 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1184 if (template_depth
1185 && DECL_CLASS_SCOPE_P (decl)
1186 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1187 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1188 {
1189 /* Next, we check the members themselves. In order to handle
1190 a few tricky cases, such as when FRIEND_DECL's are
1191
1192 template <class T> friend void A<T>::g(T t);
1193 template <class T> template <T t> friend void A<T>::h();
1194
1195 and DECL's are
1196
1197 void A<int>::g(int);
1198 template <int> void A<int>::h();
1199
1200 we need to figure out ARGS, the template arguments from
1201 the context of DECL. This is required for template substitution
1202 of `T' in the function parameter of `g' and template parameter
1203 of `h' in the above examples. Here ARGS corresponds to `int'. */
1204
1205 tree context = DECL_CONTEXT (decl);
1206 tree args = NULL_TREE;
1207 int current_depth = 0;
1208
1209 while (current_depth < template_depth)
1210 {
1211 if (CLASSTYPE_TEMPLATE_INFO (context))
1212 {
1213 if (current_depth == 0)
1214 args = TYPE_TI_ARGS (context);
1215 else
1216 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1217 current_depth++;
1218 }
1219 context = TYPE_CONTEXT (context);
1220 }
1221
1222 if (TREE_CODE (decl) == FUNCTION_DECL)
1223 {
1224 bool is_template;
1225 tree friend_type;
1226 tree decl_type;
1227 tree friend_args_type;
1228 tree decl_args_type;
1229
1230 /* Make sure that both DECL and FRIEND_DECL are templates or
1231 non-templates. */
1232 is_template = DECL_TEMPLATE_INFO (decl)
1233 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1234 if (need_template ^ is_template)
1235 return false;
1236 else if (is_template)
1237 {
1238 /* If both are templates, check template parameter list. */
1239 tree friend_parms
1240 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1241 args, tf_none);
1242 if (!comp_template_parms
1243 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1244 friend_parms))
1245 return false;
1246
1247 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1248 }
1249 else
1250 decl_type = TREE_TYPE (decl);
1251
1252 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1253 tf_none, NULL_TREE);
1254 if (friend_type == error_mark_node)
1255 return false;
1256
1257 /* Check if return types match. */
1258 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1259 return false;
1260
1261 /* Check if function parameter types match, ignoring the
1262 `this' parameter. */
1263 friend_args_type = TYPE_ARG_TYPES (friend_type);
1264 decl_args_type = TYPE_ARG_TYPES (decl_type);
1265 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1266 friend_args_type = TREE_CHAIN (friend_args_type);
1267 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1268 decl_args_type = TREE_CHAIN (decl_args_type);
1269
1270 return compparms (decl_args_type, friend_args_type);
1271 }
1272 else
1273 {
1274 /* DECL is a TYPE_DECL */
1275 bool is_template;
1276 tree decl_type = TREE_TYPE (decl);
1277
1278 /* Make sure that both DECL and FRIEND_DECL are templates or
1279 non-templates. */
1280 is_template
1281 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1282 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1283
1284 if (need_template ^ is_template)
1285 return false;
1286 else if (is_template)
1287 {
1288 tree friend_parms;
1289 /* If both are templates, check the name of the two
1290 TEMPLATE_DECL's first because is_friend didn't. */
1291 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1292 != DECL_NAME (friend_decl))
1293 return false;
1294
1295 /* Now check template parameter list. */
1296 friend_parms
1297 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1298 args, tf_none);
1299 return comp_template_parms
1300 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1301 friend_parms);
1302 }
1303 else
1304 return (DECL_NAME (decl)
1305 == DECL_NAME (friend_decl));
1306 }
1307 }
1308 return false;
1309 }
1310
1311 /* Register the specialization SPEC as a specialization of TMPL with
1312 the indicated ARGS. IS_FRIEND indicates whether the specialization
1313 is actually just a friend declaration. Returns SPEC, or an
1314 equivalent prior declaration, if available. */
1315
1316 static tree
1317 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1318 hashval_t hash)
1319 {
1320 tree fn;
1321 void **slot = NULL;
1322 spec_entry elt;
1323
1324 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1325
1326 if (TREE_CODE (spec) == FUNCTION_DECL
1327 && uses_template_parms (DECL_TI_ARGS (spec)))
1328 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1329 register it; we want the corresponding TEMPLATE_DECL instead.
1330 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1331 the more obvious `uses_template_parms (spec)' to avoid problems
1332 with default function arguments. In particular, given
1333 something like this:
1334
1335 template <class T> void f(T t1, T t = T())
1336
1337 the default argument expression is not substituted for in an
1338 instantiation unless and until it is actually needed. */
1339 return spec;
1340
1341 if (optimize_specialization_lookup_p (tmpl))
1342 /* We don't put these specializations in the hash table, but we might
1343 want to give an error about a mismatch. */
1344 fn = retrieve_specialization (tmpl, args, 0);
1345 else
1346 {
1347 elt.tmpl = tmpl;
1348 elt.args = args;
1349 elt.spec = spec;
1350
1351 if (hash == 0)
1352 hash = hash_specialization (&elt);
1353
1354 slot =
1355 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1356 if (*slot)
1357 fn = ((spec_entry *) *slot)->spec;
1358 else
1359 fn = NULL_TREE;
1360 }
1361
1362 /* We can sometimes try to re-register a specialization that we've
1363 already got. In particular, regenerate_decl_from_template calls
1364 duplicate_decls which will update the specialization list. But,
1365 we'll still get called again here anyhow. It's more convenient
1366 to simply allow this than to try to prevent it. */
1367 if (fn == spec)
1368 return spec;
1369 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1370 {
1371 if (DECL_TEMPLATE_INSTANTIATION (fn))
1372 {
1373 if (DECL_ODR_USED (fn)
1374 || DECL_EXPLICIT_INSTANTIATION (fn))
1375 {
1376 error ("specialization of %qD after instantiation",
1377 fn);
1378 return error_mark_node;
1379 }
1380 else
1381 {
1382 tree clone;
1383 /* This situation should occur only if the first
1384 specialization is an implicit instantiation, the
1385 second is an explicit specialization, and the
1386 implicit instantiation has not yet been used. That
1387 situation can occur if we have implicitly
1388 instantiated a member function and then specialized
1389 it later.
1390
1391 We can also wind up here if a friend declaration that
1392 looked like an instantiation turns out to be a
1393 specialization:
1394
1395 template <class T> void foo(T);
1396 class S { friend void foo<>(int) };
1397 template <> void foo(int);
1398
1399 We transform the existing DECL in place so that any
1400 pointers to it become pointers to the updated
1401 declaration.
1402
1403 If there was a definition for the template, but not
1404 for the specialization, we want this to look as if
1405 there were no definition, and vice versa. */
1406 DECL_INITIAL (fn) = NULL_TREE;
1407 duplicate_decls (spec, fn, is_friend);
1408 /* The call to duplicate_decls will have applied
1409 [temp.expl.spec]:
1410
1411 An explicit specialization of a function template
1412 is inline only if it is explicitly declared to be,
1413 and independently of whether its function template
1414 is.
1415
1416 to the primary function; now copy the inline bits to
1417 the various clones. */
1418 FOR_EACH_CLONE (clone, fn)
1419 {
1420 DECL_DECLARED_INLINE_P (clone)
1421 = DECL_DECLARED_INLINE_P (fn);
1422 DECL_SOURCE_LOCATION (clone)
1423 = DECL_SOURCE_LOCATION (fn);
1424 }
1425 check_specialization_namespace (tmpl);
1426
1427 return fn;
1428 }
1429 }
1430 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1431 {
1432 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1433 /* Dup decl failed, but this is a new definition. Set the
1434 line number so any errors match this new
1435 definition. */
1436 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1437
1438 return fn;
1439 }
1440 }
1441 else if (fn)
1442 return duplicate_decls (spec, fn, is_friend);
1443
1444 /* A specialization must be declared in the same namespace as the
1445 template it is specializing. */
1446 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1447 && !check_specialization_namespace (tmpl))
1448 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1449
1450 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1451 {
1452 spec_entry *entry = ggc_alloc_spec_entry ();
1453 gcc_assert (tmpl && args && spec);
1454 *entry = elt;
1455 *slot = entry;
1456 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1457 && PRIMARY_TEMPLATE_P (tmpl)
1458 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1459 /* TMPL is a forward declaration of a template function; keep a list
1460 of all specializations in case we need to reassign them to a friend
1461 template later in tsubst_friend_function. */
1462 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1463 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1464 }
1465
1466 return spec;
1467 }
1468
1469 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1470 TMPL and ARGS members, ignores SPEC. */
1471
1472 int comparing_specializations;
1473
1474 static int
1475 eq_specializations (const void *p1, const void *p2)
1476 {
1477 const spec_entry *e1 = (const spec_entry *)p1;
1478 const spec_entry *e2 = (const spec_entry *)p2;
1479 int equal;
1480
1481 ++comparing_specializations;
1482 equal = (e1->tmpl == e2->tmpl
1483 && comp_template_args (e1->args, e2->args));
1484 --comparing_specializations;
1485
1486 return equal;
1487 }
1488
1489 /* Returns a hash for a template TMPL and template arguments ARGS. */
1490
1491 static hashval_t
1492 hash_tmpl_and_args (tree tmpl, tree args)
1493 {
1494 hashval_t val = DECL_UID (tmpl);
1495 return iterative_hash_template_arg (args, val);
1496 }
1497
1498 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1499 ignoring SPEC. */
1500
1501 static hashval_t
1502 hash_specialization (const void *p)
1503 {
1504 const spec_entry *e = (const spec_entry *)p;
1505 return hash_tmpl_and_args (e->tmpl, e->args);
1506 }
1507
1508 /* Recursively calculate a hash value for a template argument ARG, for use
1509 in the hash tables of template specializations. */
1510
1511 hashval_t
1512 iterative_hash_template_arg (tree arg, hashval_t val)
1513 {
1514 unsigned HOST_WIDE_INT i;
1515 enum tree_code code;
1516 char tclass;
1517
1518 if (arg == NULL_TREE)
1519 return iterative_hash_object (arg, val);
1520
1521 if (!TYPE_P (arg))
1522 STRIP_NOPS (arg);
1523
1524 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1525 /* We can get one of these when re-hashing a previous entry in the middle
1526 of substituting into a pack expansion. Just look through it. */
1527 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1528
1529 code = TREE_CODE (arg);
1530 tclass = TREE_CODE_CLASS (code);
1531
1532 val = iterative_hash_object (code, val);
1533
1534 switch (code)
1535 {
1536 case ERROR_MARK:
1537 return val;
1538
1539 case IDENTIFIER_NODE:
1540 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1541
1542 case TREE_VEC:
1543 {
1544 int i, len = TREE_VEC_LENGTH (arg);
1545 for (i = 0; i < len; ++i)
1546 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1547 return val;
1548 }
1549
1550 case TYPE_PACK_EXPANSION:
1551 case EXPR_PACK_EXPANSION:
1552 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1553 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1554
1555 case TYPE_ARGUMENT_PACK:
1556 case NONTYPE_ARGUMENT_PACK:
1557 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1558
1559 case TREE_LIST:
1560 for (; arg; arg = TREE_CHAIN (arg))
1561 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1562 return val;
1563
1564 case OVERLOAD:
1565 for (; arg; arg = OVL_NEXT (arg))
1566 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1567 return val;
1568
1569 case CONSTRUCTOR:
1570 {
1571 tree field, value;
1572 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1573 {
1574 val = iterative_hash_template_arg (field, val);
1575 val = iterative_hash_template_arg (value, val);
1576 }
1577 return val;
1578 }
1579
1580 case PARM_DECL:
1581 if (!DECL_ARTIFICIAL (arg))
1582 {
1583 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1584 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1585 }
1586 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1587
1588 case TARGET_EXPR:
1589 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1590
1591 case PTRMEM_CST:
1592 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1593 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1594
1595 case TEMPLATE_PARM_INDEX:
1596 val = iterative_hash_template_arg
1597 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1598 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1599 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1600
1601 case TRAIT_EXPR:
1602 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1603 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1604 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1605
1606 case BASELINK:
1607 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1608 val);
1609 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1610 val);
1611
1612 case MODOP_EXPR:
1613 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1614 code = TREE_CODE (TREE_OPERAND (arg, 1));
1615 val = iterative_hash_object (code, val);
1616 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1617
1618 case LAMBDA_EXPR:
1619 /* A lambda can't appear in a template arg, but don't crash on
1620 erroneous input. */
1621 gcc_assert (seen_error ());
1622 return val;
1623
1624 case CAST_EXPR:
1625 case IMPLICIT_CONV_EXPR:
1626 case STATIC_CAST_EXPR:
1627 case REINTERPRET_CAST_EXPR:
1628 case CONST_CAST_EXPR:
1629 case DYNAMIC_CAST_EXPR:
1630 case NEW_EXPR:
1631 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1632 /* Now hash operands as usual. */
1633 break;
1634
1635 default:
1636 break;
1637 }
1638
1639 switch (tclass)
1640 {
1641 case tcc_type:
1642 if (TYPE_CANONICAL (arg))
1643 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1644 val);
1645 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1646 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1647 /* Otherwise just compare the types during lookup. */
1648 return val;
1649
1650 case tcc_declaration:
1651 case tcc_constant:
1652 return iterative_hash_expr (arg, val);
1653
1654 default:
1655 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1656 {
1657 unsigned n = cp_tree_operand_length (arg);
1658 for (i = 0; i < n; ++i)
1659 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1660 return val;
1661 }
1662 }
1663 gcc_unreachable ();
1664 return 0;
1665 }
1666
1667 /* Unregister the specialization SPEC as a specialization of TMPL.
1668 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1669 if the SPEC was listed as a specialization of TMPL.
1670
1671 Note that SPEC has been ggc_freed, so we can't look inside it. */
1672
1673 bool
1674 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1675 {
1676 spec_entry *entry;
1677 spec_entry elt;
1678
1679 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1680 elt.args = TI_ARGS (tinfo);
1681 elt.spec = NULL_TREE;
1682
1683 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1684 if (entry != NULL)
1685 {
1686 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1687 gcc_assert (new_spec != NULL_TREE);
1688 entry->spec = new_spec;
1689 return 1;
1690 }
1691
1692 return 0;
1693 }
1694
1695 /* Like register_specialization, but for local declarations. We are
1696 registering SPEC, an instantiation of TMPL. */
1697
1698 static void
1699 register_local_specialization (tree spec, tree tmpl)
1700 {
1701 void **slot;
1702
1703 slot = pointer_map_insert (local_specializations, tmpl);
1704 *slot = spec;
1705 }
1706
1707 /* TYPE is a class type. Returns true if TYPE is an explicitly
1708 specialized class. */
1709
1710 bool
1711 explicit_class_specialization_p (tree type)
1712 {
1713 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1714 return false;
1715 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1716 }
1717
1718 /* Print the list of functions at FNS, going through all the overloads
1719 for each element of the list. Alternatively, FNS can not be a
1720 TREE_LIST, in which case it will be printed together with all the
1721 overloads.
1722
1723 MORE and *STR should respectively be FALSE and NULL when the function
1724 is called from the outside. They are used internally on recursive
1725 calls. print_candidates manages the two parameters and leaves NULL
1726 in *STR when it ends. */
1727
1728 static void
1729 print_candidates_1 (tree fns, bool more, const char **str)
1730 {
1731 tree fn, fn2;
1732 char *spaces = NULL;
1733
1734 for (fn = fns; fn; fn = OVL_NEXT (fn))
1735 if (TREE_CODE (fn) == TREE_LIST)
1736 {
1737 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1738 print_candidates_1 (TREE_VALUE (fn2),
1739 TREE_CHAIN (fn2) || more, str);
1740 }
1741 else
1742 {
1743 tree cand = OVL_CURRENT (fn);
1744 if (!*str)
1745 {
1746 /* Pick the prefix string. */
1747 if (!more && !OVL_NEXT (fns))
1748 {
1749 inform (DECL_SOURCE_LOCATION (cand),
1750 "candidate is: %#D", cand);
1751 continue;
1752 }
1753
1754 *str = _("candidates are:");
1755 spaces = get_spaces (*str);
1756 }
1757 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1758 *str = spaces ? spaces : *str;
1759 }
1760
1761 if (!more)
1762 {
1763 free (spaces);
1764 *str = NULL;
1765 }
1766 }
1767
1768 /* Print the list of candidate FNS in an error message. FNS can also
1769 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1770
1771 void
1772 print_candidates (tree fns)
1773 {
1774 const char *str = NULL;
1775 print_candidates_1 (fns, false, &str);
1776 gcc_assert (str == NULL);
1777 }
1778
1779 /* Returns the template (one of the functions given by TEMPLATE_ID)
1780 which can be specialized to match the indicated DECL with the
1781 explicit template args given in TEMPLATE_ID. The DECL may be
1782 NULL_TREE if none is available. In that case, the functions in
1783 TEMPLATE_ID are non-members.
1784
1785 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1786 specialization of a member template.
1787
1788 The TEMPLATE_COUNT is the number of references to qualifying
1789 template classes that appeared in the name of the function. See
1790 check_explicit_specialization for a more accurate description.
1791
1792 TSK indicates what kind of template declaration (if any) is being
1793 declared. TSK_TEMPLATE indicates that the declaration given by
1794 DECL, though a FUNCTION_DECL, has template parameters, and is
1795 therefore a template function.
1796
1797 The template args (those explicitly specified and those deduced)
1798 are output in a newly created vector *TARGS_OUT.
1799
1800 If it is impossible to determine the result, an error message is
1801 issued. The error_mark_node is returned to indicate failure. */
1802
1803 static tree
1804 determine_specialization (tree template_id,
1805 tree decl,
1806 tree* targs_out,
1807 int need_member_template,
1808 int template_count,
1809 tmpl_spec_kind tsk)
1810 {
1811 tree fns;
1812 tree targs;
1813 tree explicit_targs;
1814 tree candidates = NULL_TREE;
1815 /* A TREE_LIST of templates of which DECL may be a specialization.
1816 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1817 corresponding TREE_PURPOSE is the set of template arguments that,
1818 when used to instantiate the template, would produce a function
1819 with the signature of DECL. */
1820 tree templates = NULL_TREE;
1821 int header_count;
1822 cp_binding_level *b;
1823
1824 *targs_out = NULL_TREE;
1825
1826 if (template_id == error_mark_node || decl == error_mark_node)
1827 return error_mark_node;
1828
1829 /* We shouldn't be specializing a member template of an
1830 unspecialized class template; we already gave an error in
1831 check_specialization_scope, now avoid crashing. */
1832 if (template_count && DECL_CLASS_SCOPE_P (decl)
1833 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1834 {
1835 gcc_assert (errorcount);
1836 return error_mark_node;
1837 }
1838
1839 fns = TREE_OPERAND (template_id, 0);
1840 explicit_targs = TREE_OPERAND (template_id, 1);
1841
1842 if (fns == error_mark_node)
1843 return error_mark_node;
1844
1845 /* Check for baselinks. */
1846 if (BASELINK_P (fns))
1847 fns = BASELINK_FUNCTIONS (fns);
1848
1849 if (!is_overloaded_fn (fns))
1850 {
1851 error ("%qD is not a function template", fns);
1852 return error_mark_node;
1853 }
1854
1855 /* Count the number of template headers specified for this
1856 specialization. */
1857 header_count = 0;
1858 for (b = current_binding_level;
1859 b->kind == sk_template_parms;
1860 b = b->level_chain)
1861 ++header_count;
1862
1863 for (; fns; fns = OVL_NEXT (fns))
1864 {
1865 tree fn = OVL_CURRENT (fns);
1866
1867 if (TREE_CODE (fn) == TEMPLATE_DECL)
1868 {
1869 tree decl_arg_types;
1870 tree fn_arg_types;
1871 tree insttype;
1872
1873 /* In case of explicit specialization, we need to check if
1874 the number of template headers appearing in the specialization
1875 is correct. This is usually done in check_explicit_specialization,
1876 but the check done there cannot be exhaustive when specializing
1877 member functions. Consider the following code:
1878
1879 template <> void A<int>::f(int);
1880 template <> template <> void A<int>::f(int);
1881
1882 Assuming that A<int> is not itself an explicit specialization
1883 already, the first line specializes "f" which is a non-template
1884 member function, whilst the second line specializes "f" which
1885 is a template member function. So both lines are syntactically
1886 correct, and check_explicit_specialization does not reject
1887 them.
1888
1889 Here, we can do better, as we are matching the specialization
1890 against the declarations. We count the number of template
1891 headers, and we check if they match TEMPLATE_COUNT + 1
1892 (TEMPLATE_COUNT is the number of qualifying template classes,
1893 plus there must be another header for the member template
1894 itself).
1895
1896 Notice that if header_count is zero, this is not a
1897 specialization but rather a template instantiation, so there
1898 is no check we can perform here. */
1899 if (header_count && header_count != template_count + 1)
1900 continue;
1901
1902 /* Check that the number of template arguments at the
1903 innermost level for DECL is the same as for FN. */
1904 if (current_binding_level->kind == sk_template_parms
1905 && !current_binding_level->explicit_spec_p
1906 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1907 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1908 (current_template_parms))))
1909 continue;
1910
1911 /* DECL might be a specialization of FN. */
1912 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1913 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1914
1915 /* For a non-static member function, we need to make sure
1916 that the const qualification is the same. Since
1917 get_bindings does not try to merge the "this" parameter,
1918 we must do the comparison explicitly. */
1919 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1920 && !same_type_p (TREE_VALUE (fn_arg_types),
1921 TREE_VALUE (decl_arg_types)))
1922 continue;
1923
1924 /* Skip the "this" parameter and, for constructors of
1925 classes with virtual bases, the VTT parameter. A
1926 full specialization of a constructor will have a VTT
1927 parameter, but a template never will. */
1928 decl_arg_types
1929 = skip_artificial_parms_for (decl, decl_arg_types);
1930 fn_arg_types
1931 = skip_artificial_parms_for (fn, fn_arg_types);
1932
1933 /* Function templates cannot be specializations; there are
1934 no partial specializations of functions. Therefore, if
1935 the type of DECL does not match FN, there is no
1936 match. */
1937 if (tsk == tsk_template)
1938 {
1939 if (compparms (fn_arg_types, decl_arg_types))
1940 candidates = tree_cons (NULL_TREE, fn, candidates);
1941 continue;
1942 }
1943
1944 /* See whether this function might be a specialization of this
1945 template. Suppress access control because we might be trying
1946 to make this specialization a friend, and we have already done
1947 access control for the declaration of the specialization. */
1948 push_deferring_access_checks (dk_no_check);
1949 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1950 pop_deferring_access_checks ();
1951
1952 if (!targs)
1953 /* We cannot deduce template arguments that when used to
1954 specialize TMPL will produce DECL. */
1955 continue;
1956
1957 /* Make sure that the deduced arguments actually work. */
1958 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1959 if (insttype == error_mark_node)
1960 continue;
1961 fn_arg_types
1962 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1963 if (!compparms (fn_arg_types, decl_arg_types))
1964 continue;
1965
1966 /* Save this template, and the arguments deduced. */
1967 templates = tree_cons (targs, fn, templates);
1968 }
1969 else if (need_member_template)
1970 /* FN is an ordinary member function, and we need a
1971 specialization of a member template. */
1972 ;
1973 else if (TREE_CODE (fn) != FUNCTION_DECL)
1974 /* We can get IDENTIFIER_NODEs here in certain erroneous
1975 cases. */
1976 ;
1977 else if (!DECL_FUNCTION_MEMBER_P (fn))
1978 /* This is just an ordinary non-member function. Nothing can
1979 be a specialization of that. */
1980 ;
1981 else if (DECL_ARTIFICIAL (fn))
1982 /* Cannot specialize functions that are created implicitly. */
1983 ;
1984 else
1985 {
1986 tree decl_arg_types;
1987
1988 /* This is an ordinary member function. However, since
1989 we're here, we can assume it's enclosing class is a
1990 template class. For example,
1991
1992 template <typename T> struct S { void f(); };
1993 template <> void S<int>::f() {}
1994
1995 Here, S<int>::f is a non-template, but S<int> is a
1996 template class. If FN has the same type as DECL, we
1997 might be in business. */
1998
1999 if (!DECL_TEMPLATE_INFO (fn))
2000 /* Its enclosing class is an explicit specialization
2001 of a template class. This is not a candidate. */
2002 continue;
2003
2004 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2005 TREE_TYPE (TREE_TYPE (fn))))
2006 /* The return types differ. */
2007 continue;
2008
2009 /* Adjust the type of DECL in case FN is a static member. */
2010 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2011 if (DECL_STATIC_FUNCTION_P (fn)
2012 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2013 decl_arg_types = TREE_CHAIN (decl_arg_types);
2014
2015 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2016 decl_arg_types))
2017 /* They match! */
2018 candidates = tree_cons (NULL_TREE, fn, candidates);
2019 }
2020 }
2021
2022 if (templates && TREE_CHAIN (templates))
2023 {
2024 /* We have:
2025
2026 [temp.expl.spec]
2027
2028 It is possible for a specialization with a given function
2029 signature to be instantiated from more than one function
2030 template. In such cases, explicit specification of the
2031 template arguments must be used to uniquely identify the
2032 function template specialization being specialized.
2033
2034 Note that here, there's no suggestion that we're supposed to
2035 determine which of the candidate templates is most
2036 specialized. However, we, also have:
2037
2038 [temp.func.order]
2039
2040 Partial ordering of overloaded function template
2041 declarations is used in the following contexts to select
2042 the function template to which a function template
2043 specialization refers:
2044
2045 -- when an explicit specialization refers to a function
2046 template.
2047
2048 So, we do use the partial ordering rules, at least for now.
2049 This extension can only serve to make invalid programs valid,
2050 so it's safe. And, there is strong anecdotal evidence that
2051 the committee intended the partial ordering rules to apply;
2052 the EDG front end has that behavior, and John Spicer claims
2053 that the committee simply forgot to delete the wording in
2054 [temp.expl.spec]. */
2055 tree tmpl = most_specialized_instantiation (templates);
2056 if (tmpl != error_mark_node)
2057 {
2058 templates = tmpl;
2059 TREE_CHAIN (templates) = NULL_TREE;
2060 }
2061 }
2062
2063 if (templates == NULL_TREE && candidates == NULL_TREE)
2064 {
2065 error ("template-id %qD for %q+D does not match any template "
2066 "declaration", template_id, decl);
2067 if (header_count && header_count != template_count + 1)
2068 inform (input_location, "saw %d %<template<>%>, need %d for "
2069 "specializing a member function template",
2070 header_count, template_count + 1);
2071 return error_mark_node;
2072 }
2073 else if ((templates && TREE_CHAIN (templates))
2074 || (candidates && TREE_CHAIN (candidates))
2075 || (templates && candidates))
2076 {
2077 error ("ambiguous template specialization %qD for %q+D",
2078 template_id, decl);
2079 candidates = chainon (candidates, templates);
2080 print_candidates (candidates);
2081 return error_mark_node;
2082 }
2083
2084 /* We have one, and exactly one, match. */
2085 if (candidates)
2086 {
2087 tree fn = TREE_VALUE (candidates);
2088 *targs_out = copy_node (DECL_TI_ARGS (fn));
2089 /* DECL is a re-declaration or partial instantiation of a template
2090 function. */
2091 if (TREE_CODE (fn) == TEMPLATE_DECL)
2092 return fn;
2093 /* It was a specialization of an ordinary member function in a
2094 template class. */
2095 return DECL_TI_TEMPLATE (fn);
2096 }
2097
2098 /* It was a specialization of a template. */
2099 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2100 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2101 {
2102 *targs_out = copy_node (targs);
2103 SET_TMPL_ARGS_LEVEL (*targs_out,
2104 TMPL_ARGS_DEPTH (*targs_out),
2105 TREE_PURPOSE (templates));
2106 }
2107 else
2108 *targs_out = TREE_PURPOSE (templates);
2109 return TREE_VALUE (templates);
2110 }
2111
2112 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2113 but with the default argument values filled in from those in the
2114 TMPL_TYPES. */
2115
2116 static tree
2117 copy_default_args_to_explicit_spec_1 (tree spec_types,
2118 tree tmpl_types)
2119 {
2120 tree new_spec_types;
2121
2122 if (!spec_types)
2123 return NULL_TREE;
2124
2125 if (spec_types == void_list_node)
2126 return void_list_node;
2127
2128 /* Substitute into the rest of the list. */
2129 new_spec_types =
2130 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2131 TREE_CHAIN (tmpl_types));
2132
2133 /* Add the default argument for this parameter. */
2134 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2135 TREE_VALUE (spec_types),
2136 new_spec_types);
2137 }
2138
2139 /* DECL is an explicit specialization. Replicate default arguments
2140 from the template it specializes. (That way, code like:
2141
2142 template <class T> void f(T = 3);
2143 template <> void f(double);
2144 void g () { f (); }
2145
2146 works, as required.) An alternative approach would be to look up
2147 the correct default arguments at the call-site, but this approach
2148 is consistent with how implicit instantiations are handled. */
2149
2150 static void
2151 copy_default_args_to_explicit_spec (tree decl)
2152 {
2153 tree tmpl;
2154 tree spec_types;
2155 tree tmpl_types;
2156 tree new_spec_types;
2157 tree old_type;
2158 tree new_type;
2159 tree t;
2160 tree object_type = NULL_TREE;
2161 tree in_charge = NULL_TREE;
2162 tree vtt = NULL_TREE;
2163
2164 /* See if there's anything we need to do. */
2165 tmpl = DECL_TI_TEMPLATE (decl);
2166 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2167 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2168 if (TREE_PURPOSE (t))
2169 break;
2170 if (!t)
2171 return;
2172
2173 old_type = TREE_TYPE (decl);
2174 spec_types = TYPE_ARG_TYPES (old_type);
2175
2176 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2177 {
2178 /* Remove the this pointer, but remember the object's type for
2179 CV quals. */
2180 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2181 spec_types = TREE_CHAIN (spec_types);
2182 tmpl_types = TREE_CHAIN (tmpl_types);
2183
2184 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2185 {
2186 /* DECL may contain more parameters than TMPL due to the extra
2187 in-charge parameter in constructors and destructors. */
2188 in_charge = spec_types;
2189 spec_types = TREE_CHAIN (spec_types);
2190 }
2191 if (DECL_HAS_VTT_PARM_P (decl))
2192 {
2193 vtt = spec_types;
2194 spec_types = TREE_CHAIN (spec_types);
2195 }
2196 }
2197
2198 /* Compute the merged default arguments. */
2199 new_spec_types =
2200 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2201
2202 /* Compute the new FUNCTION_TYPE. */
2203 if (object_type)
2204 {
2205 if (vtt)
2206 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2207 TREE_VALUE (vtt),
2208 new_spec_types);
2209
2210 if (in_charge)
2211 /* Put the in-charge parameter back. */
2212 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2213 TREE_VALUE (in_charge),
2214 new_spec_types);
2215
2216 new_type = build_method_type_directly (object_type,
2217 TREE_TYPE (old_type),
2218 new_spec_types);
2219 }
2220 else
2221 new_type = build_function_type (TREE_TYPE (old_type),
2222 new_spec_types);
2223 new_type = cp_build_type_attribute_variant (new_type,
2224 TYPE_ATTRIBUTES (old_type));
2225 new_type = build_exception_variant (new_type,
2226 TYPE_RAISES_EXCEPTIONS (old_type));
2227 TREE_TYPE (decl) = new_type;
2228 }
2229
2230 /* Return the number of template headers we expect to see for a definition
2231 or specialization of CTYPE or one of its non-template members. */
2232
2233 int
2234 num_template_headers_for_class (tree ctype)
2235 {
2236 int num_templates = 0;
2237
2238 while (ctype && CLASS_TYPE_P (ctype))
2239 {
2240 /* You're supposed to have one `template <...>' for every
2241 template class, but you don't need one for a full
2242 specialization. For example:
2243
2244 template <class T> struct S{};
2245 template <> struct S<int> { void f(); };
2246 void S<int>::f () {}
2247
2248 is correct; there shouldn't be a `template <>' for the
2249 definition of `S<int>::f'. */
2250 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2251 /* If CTYPE does not have template information of any
2252 kind, then it is not a template, nor is it nested
2253 within a template. */
2254 break;
2255 if (explicit_class_specialization_p (ctype))
2256 break;
2257 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2258 ++num_templates;
2259
2260 ctype = TYPE_CONTEXT (ctype);
2261 }
2262
2263 return num_templates;
2264 }
2265
2266 /* Do a simple sanity check on the template headers that precede the
2267 variable declaration DECL. */
2268
2269 void
2270 check_template_variable (tree decl)
2271 {
2272 tree ctx = CP_DECL_CONTEXT (decl);
2273 int wanted = num_template_headers_for_class (ctx);
2274 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2275 permerror (DECL_SOURCE_LOCATION (decl),
2276 "%qD is not a static data member of a class template", decl);
2277 else if (template_header_count > wanted)
2278 {
2279 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2280 "too many template headers for %D (should be %d)",
2281 decl, wanted);
2282 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2283 inform (DECL_SOURCE_LOCATION (decl),
2284 "members of an explicitly specialized class are defined "
2285 "without a template header");
2286 }
2287 }
2288
2289 /* Check to see if the function just declared, as indicated in
2290 DECLARATOR, and in DECL, is a specialization of a function
2291 template. We may also discover that the declaration is an explicit
2292 instantiation at this point.
2293
2294 Returns DECL, or an equivalent declaration that should be used
2295 instead if all goes well. Issues an error message if something is
2296 amiss. Returns error_mark_node if the error is not easily
2297 recoverable.
2298
2299 FLAGS is a bitmask consisting of the following flags:
2300
2301 2: The function has a definition.
2302 4: The function is a friend.
2303
2304 The TEMPLATE_COUNT is the number of references to qualifying
2305 template classes that appeared in the name of the function. For
2306 example, in
2307
2308 template <class T> struct S { void f(); };
2309 void S<int>::f();
2310
2311 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2312 classes are not counted in the TEMPLATE_COUNT, so that in
2313
2314 template <class T> struct S {};
2315 template <> struct S<int> { void f(); }
2316 template <> void S<int>::f();
2317
2318 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2319 invalid; there should be no template <>.)
2320
2321 If the function is a specialization, it is marked as such via
2322 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2323 is set up correctly, and it is added to the list of specializations
2324 for that template. */
2325
2326 tree
2327 check_explicit_specialization (tree declarator,
2328 tree decl,
2329 int template_count,
2330 int flags)
2331 {
2332 int have_def = flags & 2;
2333 int is_friend = flags & 4;
2334 int specialization = 0;
2335 int explicit_instantiation = 0;
2336 int member_specialization = 0;
2337 tree ctype = DECL_CLASS_CONTEXT (decl);
2338 tree dname = DECL_NAME (decl);
2339 tmpl_spec_kind tsk;
2340
2341 if (is_friend)
2342 {
2343 if (!processing_specialization)
2344 tsk = tsk_none;
2345 else
2346 tsk = tsk_excessive_parms;
2347 }
2348 else
2349 tsk = current_tmpl_spec_kind (template_count);
2350
2351 switch (tsk)
2352 {
2353 case tsk_none:
2354 if (processing_specialization)
2355 {
2356 specialization = 1;
2357 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2358 }
2359 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2360 {
2361 if (is_friend)
2362 /* This could be something like:
2363
2364 template <class T> void f(T);
2365 class S { friend void f<>(int); } */
2366 specialization = 1;
2367 else
2368 {
2369 /* This case handles bogus declarations like template <>
2370 template <class T> void f<int>(); */
2371
2372 error ("template-id %qD in declaration of primary template",
2373 declarator);
2374 return decl;
2375 }
2376 }
2377 break;
2378
2379 case tsk_invalid_member_spec:
2380 /* The error has already been reported in
2381 check_specialization_scope. */
2382 return error_mark_node;
2383
2384 case tsk_invalid_expl_inst:
2385 error ("template parameter list used in explicit instantiation");
2386
2387 /* Fall through. */
2388
2389 case tsk_expl_inst:
2390 if (have_def)
2391 error ("definition provided for explicit instantiation");
2392
2393 explicit_instantiation = 1;
2394 break;
2395
2396 case tsk_excessive_parms:
2397 case tsk_insufficient_parms:
2398 if (tsk == tsk_excessive_parms)
2399 error ("too many template parameter lists in declaration of %qD",
2400 decl);
2401 else if (template_header_count)
2402 error("too few template parameter lists in declaration of %qD", decl);
2403 else
2404 error("explicit specialization of %qD must be introduced by "
2405 "%<template <>%>", decl);
2406
2407 /* Fall through. */
2408 case tsk_expl_spec:
2409 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2410 if (ctype)
2411 member_specialization = 1;
2412 else
2413 specialization = 1;
2414 break;
2415
2416 case tsk_template:
2417 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2418 {
2419 /* This case handles bogus declarations like template <>
2420 template <class T> void f<int>(); */
2421
2422 if (uses_template_parms (declarator))
2423 error ("function template partial specialization %qD "
2424 "is not allowed", declarator);
2425 else
2426 error ("template-id %qD in declaration of primary template",
2427 declarator);
2428 return decl;
2429 }
2430
2431 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2432 /* This is a specialization of a member template, without
2433 specialization the containing class. Something like:
2434
2435 template <class T> struct S {
2436 template <class U> void f (U);
2437 };
2438 template <> template <class U> void S<int>::f(U) {}
2439
2440 That's a specialization -- but of the entire template. */
2441 specialization = 1;
2442 break;
2443
2444 default:
2445 gcc_unreachable ();
2446 }
2447
2448 if (specialization || member_specialization)
2449 {
2450 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2451 for (; t; t = TREE_CHAIN (t))
2452 if (TREE_PURPOSE (t))
2453 {
2454 permerror (input_location,
2455 "default argument specified in explicit specialization");
2456 break;
2457 }
2458 }
2459
2460 if (specialization || member_specialization || explicit_instantiation)
2461 {
2462 tree tmpl = NULL_TREE;
2463 tree targs = NULL_TREE;
2464
2465 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2466 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2467 {
2468 tree fns;
2469
2470 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2471 if (ctype)
2472 fns = dname;
2473 else
2474 {
2475 /* If there is no class context, the explicit instantiation
2476 must be at namespace scope. */
2477 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2478
2479 /* Find the namespace binding, using the declaration
2480 context. */
2481 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2482 false, true);
2483 if (fns == error_mark_node || !is_overloaded_fn (fns))
2484 {
2485 error ("%qD is not a template function", dname);
2486 fns = error_mark_node;
2487 }
2488 else
2489 {
2490 tree fn = OVL_CURRENT (fns);
2491 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2492 CP_DECL_CONTEXT (fn)))
2493 error ("%qD is not declared in %qD",
2494 decl, current_namespace);
2495 }
2496 }
2497
2498 declarator = lookup_template_function (fns, NULL_TREE);
2499 }
2500
2501 if (declarator == error_mark_node)
2502 return error_mark_node;
2503
2504 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2505 {
2506 if (!explicit_instantiation)
2507 /* A specialization in class scope. This is invalid,
2508 but the error will already have been flagged by
2509 check_specialization_scope. */
2510 return error_mark_node;
2511 else
2512 {
2513 /* It's not valid to write an explicit instantiation in
2514 class scope, e.g.:
2515
2516 class C { template void f(); }
2517
2518 This case is caught by the parser. However, on
2519 something like:
2520
2521 template class C { void f(); };
2522
2523 (which is invalid) we can get here. The error will be
2524 issued later. */
2525 ;
2526 }
2527
2528 return decl;
2529 }
2530 else if (ctype != NULL_TREE
2531 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2532 IDENTIFIER_NODE))
2533 {
2534 /* Find the list of functions in ctype that have the same
2535 name as the declared function. */
2536 tree name = TREE_OPERAND (declarator, 0);
2537 tree fns = NULL_TREE;
2538 int idx;
2539
2540 if (constructor_name_p (name, ctype))
2541 {
2542 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2543
2544 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2545 : !CLASSTYPE_DESTRUCTORS (ctype))
2546 {
2547 /* From [temp.expl.spec]:
2548
2549 If such an explicit specialization for the member
2550 of a class template names an implicitly-declared
2551 special member function (clause _special_), the
2552 program is ill-formed.
2553
2554 Similar language is found in [temp.explicit]. */
2555 error ("specialization of implicitly-declared special member function");
2556 return error_mark_node;
2557 }
2558
2559 name = is_constructor ? ctor_identifier : dtor_identifier;
2560 }
2561
2562 if (!DECL_CONV_FN_P (decl))
2563 {
2564 idx = lookup_fnfields_1 (ctype, name);
2565 if (idx >= 0)
2566 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2567 }
2568 else
2569 {
2570 vec<tree, va_gc> *methods;
2571 tree ovl;
2572
2573 /* For a type-conversion operator, we cannot do a
2574 name-based lookup. We might be looking for `operator
2575 int' which will be a specialization of `operator T'.
2576 So, we find *all* the conversion operators, and then
2577 select from them. */
2578 fns = NULL_TREE;
2579
2580 methods = CLASSTYPE_METHOD_VEC (ctype);
2581 if (methods)
2582 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2583 methods->iterate (idx, &ovl);
2584 ++idx)
2585 {
2586 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2587 /* There are no more conversion functions. */
2588 break;
2589
2590 /* Glue all these conversion functions together
2591 with those we already have. */
2592 for (; ovl; ovl = OVL_NEXT (ovl))
2593 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2594 }
2595 }
2596
2597 if (fns == NULL_TREE)
2598 {
2599 error ("no member function %qD declared in %qT", name, ctype);
2600 return error_mark_node;
2601 }
2602 else
2603 TREE_OPERAND (declarator, 0) = fns;
2604 }
2605
2606 /* Figure out what exactly is being specialized at this point.
2607 Note that for an explicit instantiation, even one for a
2608 member function, we cannot tell apriori whether the
2609 instantiation is for a member template, or just a member
2610 function of a template class. Even if a member template is
2611 being instantiated, the member template arguments may be
2612 elided if they can be deduced from the rest of the
2613 declaration. */
2614 tmpl = determine_specialization (declarator, decl,
2615 &targs,
2616 member_specialization,
2617 template_count,
2618 tsk);
2619
2620 if (!tmpl || tmpl == error_mark_node)
2621 /* We couldn't figure out what this declaration was
2622 specializing. */
2623 return error_mark_node;
2624 else
2625 {
2626 tree gen_tmpl = most_general_template (tmpl);
2627
2628 if (explicit_instantiation)
2629 {
2630 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2631 is done by do_decl_instantiation later. */
2632
2633 int arg_depth = TMPL_ARGS_DEPTH (targs);
2634 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2635
2636 if (arg_depth > parm_depth)
2637 {
2638 /* If TMPL is not the most general template (for
2639 example, if TMPL is a friend template that is
2640 injected into namespace scope), then there will
2641 be too many levels of TARGS. Remove some of them
2642 here. */
2643 int i;
2644 tree new_targs;
2645
2646 new_targs = make_tree_vec (parm_depth);
2647 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2648 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2649 = TREE_VEC_ELT (targs, i);
2650 targs = new_targs;
2651 }
2652
2653 return instantiate_template (tmpl, targs, tf_error);
2654 }
2655
2656 /* If we thought that the DECL was a member function, but it
2657 turns out to be specializing a static member function,
2658 make DECL a static member function as well. */
2659 if (DECL_STATIC_FUNCTION_P (tmpl)
2660 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2661 revert_static_member_fn (decl);
2662
2663 /* If this is a specialization of a member template of a
2664 template class, we want to return the TEMPLATE_DECL, not
2665 the specialization of it. */
2666 if (tsk == tsk_template)
2667 {
2668 tree result = DECL_TEMPLATE_RESULT (tmpl);
2669 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2670 DECL_INITIAL (result) = NULL_TREE;
2671 if (have_def)
2672 {
2673 tree parm;
2674 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2675 DECL_SOURCE_LOCATION (result)
2676 = DECL_SOURCE_LOCATION (decl);
2677 /* We want to use the argument list specified in the
2678 definition, not in the original declaration. */
2679 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2680 for (parm = DECL_ARGUMENTS (result); parm;
2681 parm = DECL_CHAIN (parm))
2682 DECL_CONTEXT (parm) = result;
2683 }
2684 return register_specialization (tmpl, gen_tmpl, targs,
2685 is_friend, 0);
2686 }
2687
2688 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2689 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2690
2691 /* Inherit default function arguments from the template
2692 DECL is specializing. */
2693 copy_default_args_to_explicit_spec (decl);
2694
2695 /* This specialization has the same protection as the
2696 template it specializes. */
2697 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2698 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2699
2700 /* 7.1.1-1 [dcl.stc]
2701
2702 A storage-class-specifier shall not be specified in an
2703 explicit specialization...
2704
2705 The parser rejects these, so unless action is taken here,
2706 explicit function specializations will always appear with
2707 global linkage.
2708
2709 The action recommended by the C++ CWG in response to C++
2710 defect report 605 is to make the storage class and linkage
2711 of the explicit specialization match the templated function:
2712
2713 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2714 */
2715 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2716 {
2717 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2718 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2719
2720 /* This specialization has the same linkage and visibility as
2721 the function template it specializes. */
2722 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2723 if (! TREE_PUBLIC (decl))
2724 {
2725 DECL_INTERFACE_KNOWN (decl) = 1;
2726 DECL_NOT_REALLY_EXTERN (decl) = 1;
2727 }
2728 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2729 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2730 {
2731 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2732 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2733 }
2734 }
2735
2736 /* If DECL is a friend declaration, declared using an
2737 unqualified name, the namespace associated with DECL may
2738 have been set incorrectly. For example, in:
2739
2740 template <typename T> void f(T);
2741 namespace N {
2742 struct S { friend void f<int>(int); }
2743 }
2744
2745 we will have set the DECL_CONTEXT for the friend
2746 declaration to N, rather than to the global namespace. */
2747 if (DECL_NAMESPACE_SCOPE_P (decl))
2748 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2749
2750 if (is_friend && !have_def)
2751 /* This is not really a declaration of a specialization.
2752 It's just the name of an instantiation. But, it's not
2753 a request for an instantiation, either. */
2754 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2755 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2756 /* This is indeed a specialization. In case of constructors
2757 and destructors, we need in-charge and not-in-charge
2758 versions in V3 ABI. */
2759 clone_function_decl (decl, /*update_method_vec_p=*/0);
2760
2761 /* Register this specialization so that we can find it
2762 again. */
2763 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2764 }
2765 }
2766
2767 return decl;
2768 }
2769
2770 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2771 parameters. These are represented in the same format used for
2772 DECL_TEMPLATE_PARMS. */
2773
2774 int
2775 comp_template_parms (const_tree parms1, const_tree parms2)
2776 {
2777 const_tree p1;
2778 const_tree p2;
2779
2780 if (parms1 == parms2)
2781 return 1;
2782
2783 for (p1 = parms1, p2 = parms2;
2784 p1 != NULL_TREE && p2 != NULL_TREE;
2785 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2786 {
2787 tree t1 = TREE_VALUE (p1);
2788 tree t2 = TREE_VALUE (p2);
2789 int i;
2790
2791 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2792 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2793
2794 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2795 return 0;
2796
2797 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2798 {
2799 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2800 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2801
2802 /* If either of the template parameters are invalid, assume
2803 they match for the sake of error recovery. */
2804 if (parm1 == error_mark_node || parm2 == error_mark_node)
2805 return 1;
2806
2807 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2808 return 0;
2809
2810 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2811 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2812 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2813 continue;
2814 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2815 return 0;
2816 }
2817 }
2818
2819 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2820 /* One set of parameters has more parameters lists than the
2821 other. */
2822 return 0;
2823
2824 return 1;
2825 }
2826
2827 /* Determine whether PARM is a parameter pack. */
2828
2829 bool
2830 template_parameter_pack_p (const_tree parm)
2831 {
2832 /* Determine if we have a non-type template parameter pack. */
2833 if (TREE_CODE (parm) == PARM_DECL)
2834 return (DECL_TEMPLATE_PARM_P (parm)
2835 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2836 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2837 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2838
2839 /* If this is a list of template parameters, we could get a
2840 TYPE_DECL or a TEMPLATE_DECL. */
2841 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2842 parm = TREE_TYPE (parm);
2843
2844 /* Otherwise it must be a type template parameter. */
2845 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2846 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2847 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2848 }
2849
2850 /* Determine if T is a function parameter pack. */
2851
2852 bool
2853 function_parameter_pack_p (const_tree t)
2854 {
2855 if (t && TREE_CODE (t) == PARM_DECL)
2856 return FUNCTION_PARAMETER_PACK_P (t);
2857 return false;
2858 }
2859
2860 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2861 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2862
2863 tree
2864 get_function_template_decl (const_tree primary_func_tmpl_inst)
2865 {
2866 if (! primary_func_tmpl_inst
2867 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2868 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2869 return NULL;
2870
2871 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2872 }
2873
2874 /* Return true iff the function parameter PARAM_DECL was expanded
2875 from the function parameter pack PACK. */
2876
2877 bool
2878 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2879 {
2880 if (DECL_ARTIFICIAL (param_decl)
2881 || !function_parameter_pack_p (pack))
2882 return false;
2883
2884 /* The parameter pack and its pack arguments have the same
2885 DECL_PARM_INDEX. */
2886 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2887 }
2888
2889 /* Determine whether ARGS describes a variadic template args list,
2890 i.e., one that is terminated by a template argument pack. */
2891
2892 static bool
2893 template_args_variadic_p (tree args)
2894 {
2895 int nargs;
2896 tree last_parm;
2897
2898 if (args == NULL_TREE)
2899 return false;
2900
2901 args = INNERMOST_TEMPLATE_ARGS (args);
2902 nargs = TREE_VEC_LENGTH (args);
2903
2904 if (nargs == 0)
2905 return false;
2906
2907 last_parm = TREE_VEC_ELT (args, nargs - 1);
2908
2909 return ARGUMENT_PACK_P (last_parm);
2910 }
2911
2912 /* Generate a new name for the parameter pack name NAME (an
2913 IDENTIFIER_NODE) that incorporates its */
2914
2915 static tree
2916 make_ith_pack_parameter_name (tree name, int i)
2917 {
2918 /* Munge the name to include the parameter index. */
2919 #define NUMBUF_LEN 128
2920 char numbuf[NUMBUF_LEN];
2921 char* newname;
2922 int newname_len;
2923
2924 if (name == NULL_TREE)
2925 return name;
2926 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2927 newname_len = IDENTIFIER_LENGTH (name)
2928 + strlen (numbuf) + 2;
2929 newname = (char*)alloca (newname_len);
2930 snprintf (newname, newname_len,
2931 "%s#%i", IDENTIFIER_POINTER (name), i);
2932 return get_identifier (newname);
2933 }
2934
2935 /* Return true if T is a primary function, class or alias template
2936 instantiation. */
2937
2938 bool
2939 primary_template_instantiation_p (const_tree t)
2940 {
2941 if (!t)
2942 return false;
2943
2944 if (TREE_CODE (t) == FUNCTION_DECL)
2945 return DECL_LANG_SPECIFIC (t)
2946 && DECL_TEMPLATE_INSTANTIATION (t)
2947 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2948 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2949 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2950 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2951 else if (alias_template_specialization_p (t))
2952 return true;
2953 return false;
2954 }
2955
2956 /* Return true if PARM is a template template parameter. */
2957
2958 bool
2959 template_template_parameter_p (const_tree parm)
2960 {
2961 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2962 }
2963
2964 /* Return true iff PARM is a DECL representing a type template
2965 parameter. */
2966
2967 bool
2968 template_type_parameter_p (const_tree parm)
2969 {
2970 return (parm
2971 && (TREE_CODE (parm) == TYPE_DECL
2972 || TREE_CODE (parm) == TEMPLATE_DECL)
2973 && DECL_TEMPLATE_PARM_P (parm));
2974 }
2975
2976 /* Return the template parameters of T if T is a
2977 primary template instantiation, NULL otherwise. */
2978
2979 tree
2980 get_primary_template_innermost_parameters (const_tree t)
2981 {
2982 tree parms = NULL, template_info = NULL;
2983
2984 if ((template_info = get_template_info (t))
2985 && primary_template_instantiation_p (t))
2986 parms = INNERMOST_TEMPLATE_PARMS
2987 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2988
2989 return parms;
2990 }
2991
2992 /* Return the template parameters of the LEVELth level from the full list
2993 of template parameters PARMS. */
2994
2995 tree
2996 get_template_parms_at_level (tree parms, int level)
2997 {
2998 tree p;
2999 if (!parms
3000 || TREE_CODE (parms) != TREE_LIST
3001 || level > TMPL_PARMS_DEPTH (parms))
3002 return NULL_TREE;
3003
3004 for (p = parms; p; p = TREE_CHAIN (p))
3005 if (TMPL_PARMS_DEPTH (p) == level)
3006 return p;
3007
3008 return NULL_TREE;
3009 }
3010
3011 /* Returns the template arguments of T if T is a template instantiation,
3012 NULL otherwise. */
3013
3014 tree
3015 get_template_innermost_arguments (const_tree t)
3016 {
3017 tree args = NULL, template_info = NULL;
3018
3019 if ((template_info = get_template_info (t))
3020 && TI_ARGS (template_info))
3021 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3022
3023 return args;
3024 }
3025
3026 /* Return the argument pack elements of T if T is a template argument pack,
3027 NULL otherwise. */
3028
3029 tree
3030 get_template_argument_pack_elems (const_tree t)
3031 {
3032 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3033 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3034 return NULL;
3035
3036 return ARGUMENT_PACK_ARGS (t);
3037 }
3038
3039 /* Structure used to track the progress of find_parameter_packs_r. */
3040 struct find_parameter_pack_data
3041 {
3042 /* TREE_LIST that will contain all of the parameter packs found by
3043 the traversal. */
3044 tree* parameter_packs;
3045
3046 /* Set of AST nodes that have been visited by the traversal. */
3047 struct pointer_set_t *visited;
3048 };
3049
3050 /* Identifies all of the argument packs that occur in a template
3051 argument and appends them to the TREE_LIST inside DATA, which is a
3052 find_parameter_pack_data structure. This is a subroutine of
3053 make_pack_expansion and uses_parameter_packs. */
3054 static tree
3055 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3056 {
3057 tree t = *tp;
3058 struct find_parameter_pack_data* ppd =
3059 (struct find_parameter_pack_data*)data;
3060 bool parameter_pack_p = false;
3061
3062 /* Handle type aliases/typedefs. */
3063 if (TYPE_P (t)
3064 && TYPE_NAME (t)
3065 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
3066 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3067 {
3068 if (TYPE_TEMPLATE_INFO (t))
3069 cp_walk_tree (&TYPE_TI_ARGS (t),
3070 &find_parameter_packs_r,
3071 ppd, ppd->visited);
3072 *walk_subtrees = 0;
3073 return NULL_TREE;
3074 }
3075
3076 /* Identify whether this is a parameter pack or not. */
3077 switch (TREE_CODE (t))
3078 {
3079 case TEMPLATE_PARM_INDEX:
3080 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3081 parameter_pack_p = true;
3082 break;
3083
3084 case TEMPLATE_TYPE_PARM:
3085 t = TYPE_MAIN_VARIANT (t);
3086 case TEMPLATE_TEMPLATE_PARM:
3087 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3088 parameter_pack_p = true;
3089 break;
3090
3091 case PARM_DECL:
3092 if (FUNCTION_PARAMETER_PACK_P (t))
3093 {
3094 /* We don't want to walk into the type of a PARM_DECL,
3095 because we don't want to see the type parameter pack. */
3096 *walk_subtrees = 0;
3097 parameter_pack_p = true;
3098 }
3099 break;
3100
3101 case BASES:
3102 parameter_pack_p = true;
3103 break;
3104 default:
3105 /* Not a parameter pack. */
3106 break;
3107 }
3108
3109 if (parameter_pack_p)
3110 {
3111 /* Add this parameter pack to the list. */
3112 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3113 }
3114
3115 if (TYPE_P (t))
3116 cp_walk_tree (&TYPE_CONTEXT (t),
3117 &find_parameter_packs_r, ppd, ppd->visited);
3118
3119 /* This switch statement will return immediately if we don't find a
3120 parameter pack. */
3121 switch (TREE_CODE (t))
3122 {
3123 case TEMPLATE_PARM_INDEX:
3124 return NULL_TREE;
3125
3126 case BOUND_TEMPLATE_TEMPLATE_PARM:
3127 /* Check the template itself. */
3128 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3129 &find_parameter_packs_r, ppd, ppd->visited);
3130 /* Check the template arguments. */
3131 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3132 ppd->visited);
3133 *walk_subtrees = 0;
3134 return NULL_TREE;
3135
3136 case TEMPLATE_TYPE_PARM:
3137 case TEMPLATE_TEMPLATE_PARM:
3138 return NULL_TREE;
3139
3140 case PARM_DECL:
3141 return NULL_TREE;
3142
3143 case RECORD_TYPE:
3144 if (TYPE_PTRMEMFUNC_P (t))
3145 return NULL_TREE;
3146 /* Fall through. */
3147
3148 case UNION_TYPE:
3149 case ENUMERAL_TYPE:
3150 if (TYPE_TEMPLATE_INFO (t))
3151 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3152 &find_parameter_packs_r, ppd, ppd->visited);
3153
3154 *walk_subtrees = 0;
3155 return NULL_TREE;
3156
3157 case CONSTRUCTOR:
3158 case TEMPLATE_DECL:
3159 cp_walk_tree (&TREE_TYPE (t),
3160 &find_parameter_packs_r, ppd, ppd->visited);
3161 return NULL_TREE;
3162
3163 case TYPENAME_TYPE:
3164 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3165 ppd, ppd->visited);
3166 *walk_subtrees = 0;
3167 return NULL_TREE;
3168
3169 case TYPE_PACK_EXPANSION:
3170 case EXPR_PACK_EXPANSION:
3171 *walk_subtrees = 0;
3172 return NULL_TREE;
3173
3174 case INTEGER_TYPE:
3175 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3176 ppd, ppd->visited);
3177 *walk_subtrees = 0;
3178 return NULL_TREE;
3179
3180 case IDENTIFIER_NODE:
3181 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3182 ppd->visited);
3183 *walk_subtrees = 0;
3184 return NULL_TREE;
3185
3186 default:
3187 return NULL_TREE;
3188 }
3189
3190 return NULL_TREE;
3191 }
3192
3193 /* Determines if the expression or type T uses any parameter packs. */
3194 bool
3195 uses_parameter_packs (tree t)
3196 {
3197 tree parameter_packs = NULL_TREE;
3198 struct find_parameter_pack_data ppd;
3199 ppd.parameter_packs = &parameter_packs;
3200 ppd.visited = pointer_set_create ();
3201 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3202 pointer_set_destroy (ppd.visited);
3203 return parameter_packs != NULL_TREE;
3204 }
3205
3206 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3207 representation a base-class initializer into a parameter pack
3208 expansion. If all goes well, the resulting node will be an
3209 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3210 respectively. */
3211 tree
3212 make_pack_expansion (tree arg)
3213 {
3214 tree result;
3215 tree parameter_packs = NULL_TREE;
3216 bool for_types = false;
3217 struct find_parameter_pack_data ppd;
3218
3219 if (!arg || arg == error_mark_node)
3220 return arg;
3221
3222 if (TREE_CODE (arg) == TREE_LIST)
3223 {
3224 /* The only time we will see a TREE_LIST here is for a base
3225 class initializer. In this case, the TREE_PURPOSE will be a
3226 _TYPE node (representing the base class expansion we're
3227 initializing) and the TREE_VALUE will be a TREE_LIST
3228 containing the initialization arguments.
3229
3230 The resulting expansion looks somewhat different from most
3231 expansions. Rather than returning just one _EXPANSION, we
3232 return a TREE_LIST whose TREE_PURPOSE is a
3233 TYPE_PACK_EXPANSION containing the bases that will be
3234 initialized. The TREE_VALUE will be identical to the
3235 original TREE_VALUE, which is a list of arguments that will
3236 be passed to each base. We do not introduce any new pack
3237 expansion nodes into the TREE_VALUE (although it is possible
3238 that some already exist), because the TREE_PURPOSE and
3239 TREE_VALUE all need to be expanded together with the same
3240 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3241 resulting TREE_PURPOSE will mention the parameter packs in
3242 both the bases and the arguments to the bases. */
3243 tree purpose;
3244 tree value;
3245 tree parameter_packs = NULL_TREE;
3246
3247 /* Determine which parameter packs will be used by the base
3248 class expansion. */
3249 ppd.visited = pointer_set_create ();
3250 ppd.parameter_packs = &parameter_packs;
3251 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3252 &ppd, ppd.visited);
3253
3254 if (parameter_packs == NULL_TREE)
3255 {
3256 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3257 pointer_set_destroy (ppd.visited);
3258 return error_mark_node;
3259 }
3260
3261 if (TREE_VALUE (arg) != void_type_node)
3262 {
3263 /* Collect the sets of parameter packs used in each of the
3264 initialization arguments. */
3265 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3266 {
3267 /* Determine which parameter packs will be expanded in this
3268 argument. */
3269 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3270 &ppd, ppd.visited);
3271 }
3272 }
3273
3274 pointer_set_destroy (ppd.visited);
3275
3276 /* Create the pack expansion type for the base type. */
3277 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3278 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3279 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3280
3281 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3282 they will rarely be compared to anything. */
3283 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3284
3285 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3286 }
3287
3288 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3289 for_types = true;
3290
3291 /* Build the PACK_EXPANSION_* node. */
3292 result = for_types
3293 ? cxx_make_type (TYPE_PACK_EXPANSION)
3294 : make_node (EXPR_PACK_EXPANSION);
3295 SET_PACK_EXPANSION_PATTERN (result, arg);
3296 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3297 {
3298 /* Propagate type and const-expression information. */
3299 TREE_TYPE (result) = TREE_TYPE (arg);
3300 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3301 }
3302 else
3303 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3304 they will rarely be compared to anything. */
3305 SET_TYPE_STRUCTURAL_EQUALITY (result);
3306
3307 /* Determine which parameter packs will be expanded. */
3308 ppd.parameter_packs = &parameter_packs;
3309 ppd.visited = pointer_set_create ();
3310 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3311 pointer_set_destroy (ppd.visited);
3312
3313 /* Make sure we found some parameter packs. */
3314 if (parameter_packs == NULL_TREE)
3315 {
3316 if (TYPE_P (arg))
3317 error ("expansion pattern %<%T%> contains no argument packs", arg);
3318 else
3319 error ("expansion pattern %<%E%> contains no argument packs", arg);
3320 return error_mark_node;
3321 }
3322 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3323
3324 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3325
3326 return result;
3327 }
3328
3329 /* Checks T for any "bare" parameter packs, which have not yet been
3330 expanded, and issues an error if any are found. This operation can
3331 only be done on full expressions or types (e.g., an expression
3332 statement, "if" condition, etc.), because we could have expressions like:
3333
3334 foo(f(g(h(args)))...)
3335
3336 where "args" is a parameter pack. check_for_bare_parameter_packs
3337 should not be called for the subexpressions args, h(args),
3338 g(h(args)), or f(g(h(args))), because we would produce erroneous
3339 error messages.
3340
3341 Returns TRUE and emits an error if there were bare parameter packs,
3342 returns FALSE otherwise. */
3343 bool
3344 check_for_bare_parameter_packs (tree t)
3345 {
3346 tree parameter_packs = NULL_TREE;
3347 struct find_parameter_pack_data ppd;
3348
3349 if (!processing_template_decl || !t || t == error_mark_node)
3350 return false;
3351
3352 if (TREE_CODE (t) == TYPE_DECL)
3353 t = TREE_TYPE (t);
3354
3355 ppd.parameter_packs = &parameter_packs;
3356 ppd.visited = pointer_set_create ();
3357 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3358 pointer_set_destroy (ppd.visited);
3359
3360 if (parameter_packs)
3361 {
3362 error ("parameter packs not expanded with %<...%>:");
3363 while (parameter_packs)
3364 {
3365 tree pack = TREE_VALUE (parameter_packs);
3366 tree name = NULL_TREE;
3367
3368 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3369 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3370 name = TYPE_NAME (pack);
3371 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3372 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3373 else
3374 name = DECL_NAME (pack);
3375
3376 if (name)
3377 inform (input_location, " %qD", name);
3378 else
3379 inform (input_location, " <anonymous>");
3380
3381 parameter_packs = TREE_CHAIN (parameter_packs);
3382 }
3383
3384 return true;
3385 }
3386
3387 return false;
3388 }
3389
3390 /* Expand any parameter packs that occur in the template arguments in
3391 ARGS. */
3392 tree
3393 expand_template_argument_pack (tree args)
3394 {
3395 tree result_args = NULL_TREE;
3396 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3397 int num_result_args = -1;
3398 int non_default_args_count = -1;
3399
3400 /* First, determine if we need to expand anything, and the number of
3401 slots we'll need. */
3402 for (in_arg = 0; in_arg < nargs; ++in_arg)
3403 {
3404 tree arg = TREE_VEC_ELT (args, in_arg);
3405 if (arg == NULL_TREE)
3406 return args;
3407 if (ARGUMENT_PACK_P (arg))
3408 {
3409 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3410 if (num_result_args < 0)
3411 num_result_args = in_arg + num_packed;
3412 else
3413 num_result_args += num_packed;
3414 }
3415 else
3416 {
3417 if (num_result_args >= 0)
3418 num_result_args++;
3419 }
3420 }
3421
3422 /* If no expansion is necessary, we're done. */
3423 if (num_result_args < 0)
3424 return args;
3425
3426 /* Expand arguments. */
3427 result_args = make_tree_vec (num_result_args);
3428 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3429 non_default_args_count =
3430 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3431 for (in_arg = 0; in_arg < nargs; ++in_arg)
3432 {
3433 tree arg = TREE_VEC_ELT (args, in_arg);
3434 if (ARGUMENT_PACK_P (arg))
3435 {
3436 tree packed = ARGUMENT_PACK_ARGS (arg);
3437 int i, num_packed = TREE_VEC_LENGTH (packed);
3438 for (i = 0; i < num_packed; ++i, ++out_arg)
3439 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3440 if (non_default_args_count > 0)
3441 non_default_args_count += num_packed;
3442 }
3443 else
3444 {
3445 TREE_VEC_ELT (result_args, out_arg) = arg;
3446 ++out_arg;
3447 }
3448 }
3449 if (non_default_args_count >= 0)
3450 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3451 return result_args;
3452 }
3453
3454 /* Checks if DECL shadows a template parameter.
3455
3456 [temp.local]: A template-parameter shall not be redeclared within its
3457 scope (including nested scopes).
3458
3459 Emits an error and returns TRUE if the DECL shadows a parameter,
3460 returns FALSE otherwise. */
3461
3462 bool
3463 check_template_shadow (tree decl)
3464 {
3465 tree olddecl;
3466
3467 /* If we're not in a template, we can't possibly shadow a template
3468 parameter. */
3469 if (!current_template_parms)
3470 return true;
3471
3472 /* Figure out what we're shadowing. */
3473 if (TREE_CODE (decl) == OVERLOAD)
3474 decl = OVL_CURRENT (decl);
3475 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3476
3477 /* If there's no previous binding for this name, we're not shadowing
3478 anything, let alone a template parameter. */
3479 if (!olddecl)
3480 return true;
3481
3482 /* If we're not shadowing a template parameter, we're done. Note
3483 that OLDDECL might be an OVERLOAD (or perhaps even an
3484 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3485 node. */
3486 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3487 return true;
3488
3489 /* We check for decl != olddecl to avoid bogus errors for using a
3490 name inside a class. We check TPFI to avoid duplicate errors for
3491 inline member templates. */
3492 if (decl == olddecl
3493 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3494 return true;
3495
3496 error ("declaration of %q+#D", decl);
3497 error (" shadows template parm %q+#D", olddecl);
3498 return false;
3499 }
3500
3501 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3502 ORIG_LEVEL, DECL, and TYPE. */
3503
3504 static tree
3505 build_template_parm_index (int index,
3506 int level,
3507 int orig_level,
3508 tree decl,
3509 tree type)
3510 {
3511 tree t = make_node (TEMPLATE_PARM_INDEX);
3512 TEMPLATE_PARM_IDX (t) = index;
3513 TEMPLATE_PARM_LEVEL (t) = level;
3514 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3515 TEMPLATE_PARM_DECL (t) = decl;
3516 TREE_TYPE (t) = type;
3517 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3518 TREE_READONLY (t) = TREE_READONLY (decl);
3519
3520 return t;
3521 }
3522
3523 /* Find the canonical type parameter for the given template type
3524 parameter. Returns the canonical type parameter, which may be TYPE
3525 if no such parameter existed. */
3526
3527 static tree
3528 canonical_type_parameter (tree type)
3529 {
3530 tree list;
3531 int idx = TEMPLATE_TYPE_IDX (type);
3532 if (!canonical_template_parms)
3533 vec_alloc (canonical_template_parms, idx+1);
3534
3535 while (canonical_template_parms->length () <= (unsigned)idx)
3536 vec_safe_push (canonical_template_parms, NULL_TREE);
3537
3538 list = (*canonical_template_parms)[idx];
3539 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3540 list = TREE_CHAIN (list);
3541
3542 if (list)
3543 return TREE_VALUE (list);
3544 else
3545 {
3546 (*canonical_template_parms)[idx]
3547 = tree_cons (NULL_TREE, type,
3548 (*canonical_template_parms)[idx]);
3549 return type;
3550 }
3551 }
3552
3553 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3554 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3555 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3556 new one is created. */
3557
3558 static tree
3559 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3560 tsubst_flags_t complain)
3561 {
3562 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3563 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3564 != TEMPLATE_PARM_LEVEL (index) - levels)
3565 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3566 {
3567 tree orig_decl = TEMPLATE_PARM_DECL (index);
3568 tree decl, t;
3569
3570 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3571 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3572 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3573 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3574 DECL_ARTIFICIAL (decl) = 1;
3575 SET_DECL_TEMPLATE_PARM_P (decl);
3576
3577 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3578 TEMPLATE_PARM_LEVEL (index) - levels,
3579 TEMPLATE_PARM_ORIG_LEVEL (index),
3580 decl, type);
3581 TEMPLATE_PARM_DESCENDANTS (index) = t;
3582 TEMPLATE_PARM_PARAMETER_PACK (t)
3583 = TEMPLATE_PARM_PARAMETER_PACK (index);
3584
3585 /* Template template parameters need this. */
3586 if (TREE_CODE (decl) == TEMPLATE_DECL)
3587 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3588 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3589 args, complain);
3590 }
3591
3592 return TEMPLATE_PARM_DESCENDANTS (index);
3593 }
3594
3595 /* Process information from new template parameter PARM and append it
3596 to the LIST being built. This new parameter is a non-type
3597 parameter iff IS_NON_TYPE is true. This new parameter is a
3598 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3599 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3600 parameter list PARM belongs to. This is used used to create a
3601 proper canonical type for the type of PARM that is to be created,
3602 iff PARM is a type. If the size is not known, this parameter shall
3603 be set to 0. */
3604
3605 tree
3606 process_template_parm (tree list, location_t parm_loc, tree parm,
3607 bool is_non_type, bool is_parameter_pack)
3608 {
3609 tree decl = 0;
3610 tree defval;
3611 tree err_parm_list;
3612 int idx = 0;
3613
3614 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3615 defval = TREE_PURPOSE (parm);
3616
3617 if (list)
3618 {
3619 tree p = tree_last (list);
3620
3621 if (p && TREE_VALUE (p) != error_mark_node)
3622 {
3623 p = TREE_VALUE (p);
3624 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3625 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3626 else
3627 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3628 }
3629
3630 ++idx;
3631 }
3632 else
3633 idx = 0;
3634
3635 if (is_non_type)
3636 {
3637 parm = TREE_VALUE (parm);
3638
3639 SET_DECL_TEMPLATE_PARM_P (parm);
3640
3641 if (TREE_TYPE (parm) == error_mark_node)
3642 {
3643 err_parm_list = build_tree_list (defval, parm);
3644 TREE_VALUE (err_parm_list) = error_mark_node;
3645 return chainon (list, err_parm_list);
3646 }
3647 else
3648 {
3649 /* [temp.param]
3650
3651 The top-level cv-qualifiers on the template-parameter are
3652 ignored when determining its type. */
3653 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3654 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3655 {
3656 err_parm_list = build_tree_list (defval, parm);
3657 TREE_VALUE (err_parm_list) = error_mark_node;
3658 return chainon (list, err_parm_list);
3659 }
3660
3661 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3662 {
3663 /* This template parameter is not a parameter pack, but it
3664 should be. Complain about "bare" parameter packs. */
3665 check_for_bare_parameter_packs (TREE_TYPE (parm));
3666
3667 /* Recover by calling this a parameter pack. */
3668 is_parameter_pack = true;
3669 }
3670 }
3671
3672 /* A template parameter is not modifiable. */
3673 TREE_CONSTANT (parm) = 1;
3674 TREE_READONLY (parm) = 1;
3675 decl = build_decl (parm_loc,
3676 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3677 TREE_CONSTANT (decl) = 1;
3678 TREE_READONLY (decl) = 1;
3679 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3680 = build_template_parm_index (idx, processing_template_decl,
3681 processing_template_decl,
3682 decl, TREE_TYPE (parm));
3683
3684 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3685 = is_parameter_pack;
3686 }
3687 else
3688 {
3689 tree t;
3690 parm = TREE_VALUE (TREE_VALUE (parm));
3691
3692 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3693 {
3694 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3695 /* This is for distinguishing between real templates and template
3696 template parameters */
3697 TREE_TYPE (parm) = t;
3698 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3699 decl = parm;
3700 }
3701 else
3702 {
3703 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3704 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3705 decl = build_decl (parm_loc,
3706 TYPE_DECL, parm, t);
3707 }
3708
3709 TYPE_NAME (t) = decl;
3710 TYPE_STUB_DECL (t) = decl;
3711 parm = decl;
3712 TEMPLATE_TYPE_PARM_INDEX (t)
3713 = build_template_parm_index (idx, processing_template_decl,
3714 processing_template_decl,
3715 decl, TREE_TYPE (parm));
3716 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3717 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3718 }
3719 DECL_ARTIFICIAL (decl) = 1;
3720 SET_DECL_TEMPLATE_PARM_P (decl);
3721 pushdecl (decl);
3722 parm = build_tree_list (defval, parm);
3723 return chainon (list, parm);
3724 }
3725
3726 /* The end of a template parameter list has been reached. Process the
3727 tree list into a parameter vector, converting each parameter into a more
3728 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3729 as PARM_DECLs. */
3730
3731 tree
3732 end_template_parm_list (tree parms)
3733 {
3734 int nparms;
3735 tree parm, next;
3736 tree saved_parmlist = make_tree_vec (list_length (parms));
3737
3738 current_template_parms
3739 = tree_cons (size_int (processing_template_decl),
3740 saved_parmlist, current_template_parms);
3741
3742 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3743 {
3744 next = TREE_CHAIN (parm);
3745 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3746 TREE_CHAIN (parm) = NULL_TREE;
3747 }
3748
3749 --processing_template_parmlist;
3750
3751 return saved_parmlist;
3752 }
3753
3754 /* end_template_decl is called after a template declaration is seen. */
3755
3756 void
3757 end_template_decl (void)
3758 {
3759 reset_specialization ();
3760
3761 if (! processing_template_decl)
3762 return;
3763
3764 /* This matches the pushlevel in begin_template_parm_list. */
3765 finish_scope ();
3766
3767 --processing_template_decl;
3768 current_template_parms = TREE_CHAIN (current_template_parms);
3769 }
3770
3771 /* Takes a TREE_LIST representing a template parameter and convert it
3772 into an argument suitable to be passed to the type substitution
3773 functions. Note that If the TREE_LIST contains an error_mark
3774 node, the returned argument is error_mark_node. */
3775
3776 static tree
3777 template_parm_to_arg (tree t)
3778 {
3779
3780 if (t == NULL_TREE
3781 || TREE_CODE (t) != TREE_LIST)
3782 return t;
3783
3784 if (error_operand_p (TREE_VALUE (t)))
3785 return error_mark_node;
3786
3787 t = TREE_VALUE (t);
3788
3789 if (TREE_CODE (t) == TYPE_DECL
3790 || TREE_CODE (t) == TEMPLATE_DECL)
3791 {
3792 t = TREE_TYPE (t);
3793
3794 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3795 {
3796 /* Turn this argument into a TYPE_ARGUMENT_PACK
3797 with a single element, which expands T. */
3798 tree vec = make_tree_vec (1);
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 = cxx_make_type (TYPE_ARGUMENT_PACK);
3806 SET_ARGUMENT_PACK_ARGS (t, vec);
3807 }
3808 }
3809 else
3810 {
3811 t = DECL_INITIAL (t);
3812
3813 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3814 {
3815 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3816 with a single element, which expands T. */
3817 tree vec = make_tree_vec (1);
3818 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3819 #ifdef ENABLE_CHECKING
3820 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3821 (vec, TREE_VEC_LENGTH (vec));
3822 #endif
3823 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3824
3825 t = make_node (NONTYPE_ARGUMENT_PACK);
3826 SET_ARGUMENT_PACK_ARGS (t, vec);
3827 TREE_TYPE (t) = type;
3828 }
3829 }
3830 return t;
3831 }
3832
3833 /* Given a set of template parameters, return them as a set of template
3834 arguments. The template parameters are represented as a TREE_VEC, in
3835 the form documented in cp-tree.h for template arguments. */
3836
3837 static tree
3838 template_parms_to_args (tree parms)
3839 {
3840 tree header;
3841 tree args = NULL_TREE;
3842 int length = TMPL_PARMS_DEPTH (parms);
3843 int l = length;
3844
3845 /* If there is only one level of template parameters, we do not
3846 create a TREE_VEC of TREE_VECs. Instead, we return a single
3847 TREE_VEC containing the arguments. */
3848 if (length > 1)
3849 args = make_tree_vec (length);
3850
3851 for (header = parms; header; header = TREE_CHAIN (header))
3852 {
3853 tree a = copy_node (TREE_VALUE (header));
3854 int i;
3855
3856 TREE_TYPE (a) = NULL_TREE;
3857 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3858 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3859
3860 #ifdef ENABLE_CHECKING
3861 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3862 #endif
3863
3864 if (length > 1)
3865 TREE_VEC_ELT (args, --l) = a;
3866 else
3867 args = a;
3868 }
3869
3870 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3871 /* This can happen for template parms of a template template
3872 parameter, e.g:
3873
3874 template<template<class T, class U> class TT> struct S;
3875
3876 Consider the level of the parms of TT; T and U both have
3877 level 2; TT has no template parm of level 1. So in this case
3878 the first element of full_template_args is NULL_TREE. If we
3879 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3880 of 2. This will make tsubst wrongly consider that T and U
3881 have level 1. Instead, let's create a dummy vector as the
3882 first element of full_template_args so that TMPL_ARG_DEPTH
3883 returns the correct depth for args. */
3884 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3885 return args;
3886 }
3887
3888 /* Within the declaration of a template, return the currently active
3889 template parameters as an argument TREE_VEC. */
3890
3891 static tree
3892 current_template_args (void)
3893 {
3894 return template_parms_to_args (current_template_parms);
3895 }
3896
3897 /* Update the declared TYPE by doing any lookups which were thought to be
3898 dependent, but are not now that we know the SCOPE of the declarator. */
3899
3900 tree
3901 maybe_update_decl_type (tree orig_type, tree scope)
3902 {
3903 tree type = orig_type;
3904
3905 if (type == NULL_TREE)
3906 return type;
3907
3908 if (TREE_CODE (orig_type) == TYPE_DECL)
3909 type = TREE_TYPE (type);
3910
3911 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3912 && dependent_type_p (type)
3913 /* Don't bother building up the args in this case. */
3914 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3915 {
3916 /* tsubst in the args corresponding to the template parameters,
3917 including auto if present. Most things will be unchanged, but
3918 make_typename_type and tsubst_qualified_id will resolve
3919 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3920 tree args = current_template_args ();
3921 tree auto_node = type_uses_auto (type);
3922 tree pushed;
3923 if (auto_node)
3924 {
3925 tree auto_vec = make_tree_vec (1);
3926 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3927 args = add_to_template_args (args, auto_vec);
3928 }
3929 pushed = push_scope (scope);
3930 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3931 if (pushed)
3932 pop_scope (scope);
3933 }
3934
3935 if (type == error_mark_node)
3936 return orig_type;
3937
3938 if (TREE_CODE (orig_type) == TYPE_DECL)
3939 {
3940 if (same_type_p (type, TREE_TYPE (orig_type)))
3941 type = orig_type;
3942 else
3943 type = TYPE_NAME (type);
3944 }
3945 return type;
3946 }
3947
3948 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3949 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3950 a member template. Used by push_template_decl below. */
3951
3952 static tree
3953 build_template_decl (tree decl, tree parms, bool member_template_p)
3954 {
3955 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3956 DECL_TEMPLATE_PARMS (tmpl) = parms;
3957 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3958 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3959 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3960
3961 return tmpl;
3962 }
3963
3964 struct template_parm_data
3965 {
3966 /* The level of the template parameters we are currently
3967 processing. */
3968 int level;
3969
3970 /* The index of the specialization argument we are currently
3971 processing. */
3972 int current_arg;
3973
3974 /* An array whose size is the number of template parameters. The
3975 elements are nonzero if the parameter has been used in any one
3976 of the arguments processed so far. */
3977 int* parms;
3978
3979 /* An array whose size is the number of template arguments. The
3980 elements are nonzero if the argument makes use of template
3981 parameters of this level. */
3982 int* arg_uses_template_parms;
3983 };
3984
3985 /* Subroutine of push_template_decl used to see if each template
3986 parameter in a partial specialization is used in the explicit
3987 argument list. If T is of the LEVEL given in DATA (which is
3988 treated as a template_parm_data*), then DATA->PARMS is marked
3989 appropriately. */
3990
3991 static int
3992 mark_template_parm (tree t, void* data)
3993 {
3994 int level;
3995 int idx;
3996 struct template_parm_data* tpd = (struct template_parm_data*) data;
3997
3998 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3999 {
4000 level = TEMPLATE_PARM_LEVEL (t);
4001 idx = TEMPLATE_PARM_IDX (t);
4002 }
4003 else
4004 {
4005 level = TEMPLATE_TYPE_LEVEL (t);
4006 idx = TEMPLATE_TYPE_IDX (t);
4007 }
4008
4009 if (level == tpd->level)
4010 {
4011 tpd->parms[idx] = 1;
4012 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4013 }
4014
4015 /* Return zero so that for_each_template_parm will continue the
4016 traversal of the tree; we want to mark *every* template parm. */
4017 return 0;
4018 }
4019
4020 /* Process the partial specialization DECL. */
4021
4022 static tree
4023 process_partial_specialization (tree decl)
4024 {
4025 tree type = TREE_TYPE (decl);
4026 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4027 tree specargs = CLASSTYPE_TI_ARGS (type);
4028 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4029 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4030 tree inner_parms;
4031 tree inst;
4032 int nargs = TREE_VEC_LENGTH (inner_args);
4033 int ntparms;
4034 int i;
4035 bool did_error_intro = false;
4036 struct template_parm_data tpd;
4037 struct template_parm_data tpd2;
4038
4039 gcc_assert (current_template_parms);
4040
4041 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4042 ntparms = TREE_VEC_LENGTH (inner_parms);
4043
4044 /* We check that each of the template parameters given in the
4045 partial specialization is used in the argument list to the
4046 specialization. For example:
4047
4048 template <class T> struct S;
4049 template <class T> struct S<T*>;
4050
4051 The second declaration is OK because `T*' uses the template
4052 parameter T, whereas
4053
4054 template <class T> struct S<int>;
4055
4056 is no good. Even trickier is:
4057
4058 template <class T>
4059 struct S1
4060 {
4061 template <class U>
4062 struct S2;
4063 template <class U>
4064 struct S2<T>;
4065 };
4066
4067 The S2<T> declaration is actually invalid; it is a
4068 full-specialization. Of course,
4069
4070 template <class U>
4071 struct S2<T (*)(U)>;
4072
4073 or some such would have been OK. */
4074 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4075 tpd.parms = XALLOCAVEC (int, ntparms);
4076 memset (tpd.parms, 0, sizeof (int) * ntparms);
4077
4078 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4079 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4080 for (i = 0; i < nargs; ++i)
4081 {
4082 tpd.current_arg = i;
4083 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4084 &mark_template_parm,
4085 &tpd,
4086 NULL,
4087 /*include_nondeduced_p=*/false);
4088 }
4089 for (i = 0; i < ntparms; ++i)
4090 if (tpd.parms[i] == 0)
4091 {
4092 /* One of the template parms was not used in the
4093 specialization. */
4094 if (!did_error_intro)
4095 {
4096 error ("template parameters not used in partial specialization:");
4097 did_error_intro = true;
4098 }
4099
4100 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4101 }
4102
4103 if (did_error_intro)
4104 return error_mark_node;
4105
4106 /* [temp.class.spec]
4107
4108 The argument list of the specialization shall not be identical to
4109 the implicit argument list of the primary template. */
4110 if (comp_template_args
4111 (inner_args,
4112 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4113 (maintmpl)))))
4114 error ("partial specialization %qT does not specialize any template arguments", type);
4115
4116 /* A partial specialization that replaces multiple parameters of the
4117 primary template with a pack expansion is less specialized for those
4118 parameters. */
4119 if (nargs < DECL_NTPARMS (maintmpl))
4120 {
4121 error ("partial specialization is not more specialized than the "
4122 "primary template because it replaces multiple parameters "
4123 "with a pack expansion");
4124 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4125 return decl;
4126 }
4127
4128 /* [temp.class.spec]
4129
4130 A partially specialized non-type argument expression shall not
4131 involve template parameters of the partial specialization except
4132 when the argument expression is a simple identifier.
4133
4134 The type of a template parameter corresponding to a specialized
4135 non-type argument shall not be dependent on a parameter of the
4136 specialization.
4137
4138 Also, we verify that pack expansions only occur at the
4139 end of the argument list. */
4140 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4141 tpd2.parms = 0;
4142 for (i = 0; i < nargs; ++i)
4143 {
4144 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4145 tree arg = TREE_VEC_ELT (inner_args, i);
4146 tree packed_args = NULL_TREE;
4147 int j, len = 1;
4148
4149 if (ARGUMENT_PACK_P (arg))
4150 {
4151 /* Extract the arguments from the argument pack. We'll be
4152 iterating over these in the following loop. */
4153 packed_args = ARGUMENT_PACK_ARGS (arg);
4154 len = TREE_VEC_LENGTH (packed_args);
4155 }
4156
4157 for (j = 0; j < len; j++)
4158 {
4159 if (packed_args)
4160 /* Get the Jth argument in the parameter pack. */
4161 arg = TREE_VEC_ELT (packed_args, j);
4162
4163 if (PACK_EXPANSION_P (arg))
4164 {
4165 /* Pack expansions must come at the end of the
4166 argument list. */
4167 if ((packed_args && j < len - 1)
4168 || (!packed_args && i < nargs - 1))
4169 {
4170 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4171 error ("parameter pack argument %qE must be at the "
4172 "end of the template argument list", arg);
4173 else
4174 error ("parameter pack argument %qT must be at the "
4175 "end of the template argument list", arg);
4176 }
4177 }
4178
4179 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4180 /* We only care about the pattern. */
4181 arg = PACK_EXPANSION_PATTERN (arg);
4182
4183 if (/* These first two lines are the `non-type' bit. */
4184 !TYPE_P (arg)
4185 && TREE_CODE (arg) != TEMPLATE_DECL
4186 /* This next line is the `argument expression is not just a
4187 simple identifier' condition and also the `specialized
4188 non-type argument' bit. */
4189 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4190 {
4191 if ((!packed_args && tpd.arg_uses_template_parms[i])
4192 || (packed_args && uses_template_parms (arg)))
4193 error ("template argument %qE involves template parameter(s)",
4194 arg);
4195 else
4196 {
4197 /* Look at the corresponding template parameter,
4198 marking which template parameters its type depends
4199 upon. */
4200 tree type = TREE_TYPE (parm);
4201
4202 if (!tpd2.parms)
4203 {
4204 /* We haven't yet initialized TPD2. Do so now. */
4205 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4206 /* The number of parameters here is the number in the
4207 main template, which, as checked in the assertion
4208 above, is NARGS. */
4209 tpd2.parms = XALLOCAVEC (int, nargs);
4210 tpd2.level =
4211 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4212 }
4213
4214 /* Mark the template parameters. But this time, we're
4215 looking for the template parameters of the main
4216 template, not in the specialization. */
4217 tpd2.current_arg = i;
4218 tpd2.arg_uses_template_parms[i] = 0;
4219 memset (tpd2.parms, 0, sizeof (int) * nargs);
4220 for_each_template_parm (type,
4221 &mark_template_parm,
4222 &tpd2,
4223 NULL,
4224 /*include_nondeduced_p=*/false);
4225
4226 if (tpd2.arg_uses_template_parms [i])
4227 {
4228 /* The type depended on some template parameters.
4229 If they are fully specialized in the
4230 specialization, that's OK. */
4231 int j;
4232 int count = 0;
4233 for (j = 0; j < nargs; ++j)
4234 if (tpd2.parms[j] != 0
4235 && tpd.arg_uses_template_parms [j])
4236 ++count;
4237 if (count != 0)
4238 error_n (input_location, count,
4239 "type %qT of template argument %qE depends "
4240 "on a template parameter",
4241 "type %qT of template argument %qE depends "
4242 "on template parameters",
4243 type,
4244 arg);
4245 }
4246 }
4247 }
4248 }
4249 }
4250
4251 /* We should only get here once. */
4252 gcc_assert (!COMPLETE_TYPE_P (type));
4253
4254 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4255 = tree_cons (specargs, inner_parms,
4256 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4257 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4258
4259 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4260 inst = TREE_CHAIN (inst))
4261 {
4262 tree inst_type = TREE_VALUE (inst);
4263 if (COMPLETE_TYPE_P (inst_type)
4264 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4265 {
4266 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4267 if (spec && TREE_TYPE (spec) == type)
4268 permerror (input_location,
4269 "partial specialization of %qT after instantiation "
4270 "of %qT", type, inst_type);
4271 }
4272 }
4273
4274 return decl;
4275 }
4276
4277 /* Check that a template declaration's use of default arguments and
4278 parameter packs is not invalid. Here, PARMS are the template
4279 parameters. IS_PRIMARY is true if DECL is the thing declared by
4280 a primary template. IS_PARTIAL is true if DECL is a partial
4281 specialization.
4282
4283 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4284 declaration (but not a definition); 1 indicates a declaration, 2
4285 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4286 emitted for extraneous default arguments.
4287
4288 Returns TRUE if there were no errors found, FALSE otherwise. */
4289
4290 bool
4291 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4292 bool is_partial, int is_friend_decl)
4293 {
4294 const char *msg;
4295 int last_level_to_check;
4296 tree parm_level;
4297 bool no_errors = true;
4298
4299 /* [temp.param]
4300
4301 A default template-argument shall not be specified in a
4302 function template declaration or a function template definition, nor
4303 in the template-parameter-list of the definition of a member of a
4304 class template. */
4305
4306 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4307 /* You can't have a function template declaration in a local
4308 scope, nor you can you define a member of a class template in a
4309 local scope. */
4310 return true;
4311
4312 if (TREE_CODE (decl) == TYPE_DECL
4313 && TREE_TYPE (decl)
4314 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4315 /* A lambda doesn't have an explicit declaration; don't complain
4316 about the parms of the enclosing class. */
4317 return true;
4318
4319 if (current_class_type
4320 && !TYPE_BEING_DEFINED (current_class_type)
4321 && DECL_LANG_SPECIFIC (decl)
4322 && DECL_DECLARES_FUNCTION_P (decl)
4323 /* If this is either a friend defined in the scope of the class
4324 or a member function. */
4325 && (DECL_FUNCTION_MEMBER_P (decl)
4326 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4327 : DECL_FRIEND_CONTEXT (decl)
4328 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4329 : false)
4330 /* And, if it was a member function, it really was defined in
4331 the scope of the class. */
4332 && (!DECL_FUNCTION_MEMBER_P (decl)
4333 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4334 /* We already checked these parameters when the template was
4335 declared, so there's no need to do it again now. This function
4336 was defined in class scope, but we're processing it's body now
4337 that the class is complete. */
4338 return true;
4339
4340 /* Core issue 226 (C++0x only): the following only applies to class
4341 templates. */
4342 if (is_primary
4343 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4344 {
4345 /* [temp.param]
4346
4347 If a template-parameter has a default template-argument, all
4348 subsequent template-parameters shall have a default
4349 template-argument supplied. */
4350 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4351 {
4352 tree inner_parms = TREE_VALUE (parm_level);
4353 int ntparms = TREE_VEC_LENGTH (inner_parms);
4354 int seen_def_arg_p = 0;
4355 int i;
4356
4357 for (i = 0; i < ntparms; ++i)
4358 {
4359 tree parm = TREE_VEC_ELT (inner_parms, i);
4360
4361 if (parm == error_mark_node)
4362 continue;
4363
4364 if (TREE_PURPOSE (parm))
4365 seen_def_arg_p = 1;
4366 else if (seen_def_arg_p
4367 && !template_parameter_pack_p (TREE_VALUE (parm)))
4368 {
4369 error ("no default argument for %qD", TREE_VALUE (parm));
4370 /* For better subsequent error-recovery, we indicate that
4371 there should have been a default argument. */
4372 TREE_PURPOSE (parm) = error_mark_node;
4373 no_errors = false;
4374 }
4375 else if (!is_partial
4376 && !is_friend_decl
4377 /* Don't complain about an enclosing partial
4378 specialization. */
4379 && parm_level == parms
4380 && TREE_CODE (decl) == TYPE_DECL
4381 && i < ntparms - 1
4382 && template_parameter_pack_p (TREE_VALUE (parm)))
4383 {
4384 /* A primary class template can only have one
4385 parameter pack, at the end of the template
4386 parameter list. */
4387
4388 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4389 error ("parameter pack %qE must be at the end of the"
4390 " template parameter list", TREE_VALUE (parm));
4391 else
4392 error ("parameter pack %qT must be at the end of the"
4393 " template parameter list",
4394 TREE_TYPE (TREE_VALUE (parm)));
4395
4396 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4397 = error_mark_node;
4398 no_errors = false;
4399 }
4400 }
4401 }
4402 }
4403
4404 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4405 || is_partial
4406 || !is_primary
4407 || is_friend_decl)
4408 /* For an ordinary class template, default template arguments are
4409 allowed at the innermost level, e.g.:
4410 template <class T = int>
4411 struct S {};
4412 but, in a partial specialization, they're not allowed even
4413 there, as we have in [temp.class.spec]:
4414
4415 The template parameter list of a specialization shall not
4416 contain default template argument values.
4417
4418 So, for a partial specialization, or for a function template
4419 (in C++98/C++03), we look at all of them. */
4420 ;
4421 else
4422 /* But, for a primary class template that is not a partial
4423 specialization we look at all template parameters except the
4424 innermost ones. */
4425 parms = TREE_CHAIN (parms);
4426
4427 /* Figure out what error message to issue. */
4428 if (is_friend_decl == 2)
4429 msg = G_("default template arguments may not be used in function template "
4430 "friend re-declaration");
4431 else if (is_friend_decl)
4432 msg = G_("default template arguments may not be used in function template "
4433 "friend declarations");
4434 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4435 msg = G_("default template arguments may not be used in function templates "
4436 "without -std=c++11 or -std=gnu++11");
4437 else if (is_partial)
4438 msg = G_("default template arguments may not be used in "
4439 "partial specializations");
4440 else
4441 msg = G_("default argument for template parameter for class enclosing %qD");
4442
4443 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4444 /* If we're inside a class definition, there's no need to
4445 examine the parameters to the class itself. On the one
4446 hand, they will be checked when the class is defined, and,
4447 on the other, default arguments are valid in things like:
4448 template <class T = double>
4449 struct S { template <class U> void f(U); };
4450 Here the default argument for `S' has no bearing on the
4451 declaration of `f'. */
4452 last_level_to_check = template_class_depth (current_class_type) + 1;
4453 else
4454 /* Check everything. */
4455 last_level_to_check = 0;
4456
4457 for (parm_level = parms;
4458 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4459 parm_level = TREE_CHAIN (parm_level))
4460 {
4461 tree inner_parms = TREE_VALUE (parm_level);
4462 int i;
4463 int ntparms;
4464
4465 ntparms = TREE_VEC_LENGTH (inner_parms);
4466 for (i = 0; i < ntparms; ++i)
4467 {
4468 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4469 continue;
4470
4471 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4472 {
4473 if (msg)
4474 {
4475 no_errors = false;
4476 if (is_friend_decl == 2)
4477 return no_errors;
4478
4479 error (msg, decl);
4480 msg = 0;
4481 }
4482
4483 /* Clear out the default argument so that we are not
4484 confused later. */
4485 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4486 }
4487 }
4488
4489 /* At this point, if we're still interested in issuing messages,
4490 they must apply to classes surrounding the object declared. */
4491 if (msg)
4492 msg = G_("default argument for template parameter for class "
4493 "enclosing %qD");
4494 }
4495
4496 return no_errors;
4497 }
4498
4499 /* Worker for push_template_decl_real, called via
4500 for_each_template_parm. DATA is really an int, indicating the
4501 level of the parameters we are interested in. If T is a template
4502 parameter of that level, return nonzero. */
4503
4504 static int
4505 template_parm_this_level_p (tree t, void* data)
4506 {
4507 int this_level = *(int *)data;
4508 int level;
4509
4510 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4511 level = TEMPLATE_PARM_LEVEL (t);
4512 else
4513 level = TEMPLATE_TYPE_LEVEL (t);
4514 return level == this_level;
4515 }
4516
4517 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4518 parameters given by current_template_args, or reuses a
4519 previously existing one, if appropriate. Returns the DECL, or an
4520 equivalent one, if it is replaced via a call to duplicate_decls.
4521
4522 If IS_FRIEND is true, DECL is a friend declaration. */
4523
4524 tree
4525 push_template_decl_real (tree decl, bool is_friend)
4526 {
4527 tree tmpl;
4528 tree args;
4529 tree info;
4530 tree ctx;
4531 bool is_primary;
4532 bool is_partial;
4533 int new_template_p = 0;
4534 /* True if the template is a member template, in the sense of
4535 [temp.mem]. */
4536 bool member_template_p = false;
4537
4538 if (decl == error_mark_node || !current_template_parms)
4539 return error_mark_node;
4540
4541 /* See if this is a partial specialization. */
4542 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4543 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4544 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4545
4546 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4547 is_friend = true;
4548
4549 if (is_friend)
4550 /* For a friend, we want the context of the friend function, not
4551 the type of which it is a friend. */
4552 ctx = CP_DECL_CONTEXT (decl);
4553 else if (CP_DECL_CONTEXT (decl)
4554 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4555 /* In the case of a virtual function, we want the class in which
4556 it is defined. */
4557 ctx = CP_DECL_CONTEXT (decl);
4558 else
4559 /* Otherwise, if we're currently defining some class, the DECL
4560 is assumed to be a member of the class. */
4561 ctx = current_scope ();
4562
4563 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4564 ctx = NULL_TREE;
4565
4566 if (!DECL_CONTEXT (decl))
4567 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4568
4569 /* See if this is a primary template. */
4570 if (is_friend && ctx)
4571 /* A friend template that specifies a class context, i.e.
4572 template <typename T> friend void A<T>::f();
4573 is not primary. */
4574 is_primary = false;
4575 else
4576 is_primary = template_parm_scope_p ();
4577
4578 if (is_primary)
4579 {
4580 if (DECL_CLASS_SCOPE_P (decl))
4581 member_template_p = true;
4582 if (TREE_CODE (decl) == TYPE_DECL
4583 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4584 {
4585 error ("template class without a name");
4586 return error_mark_node;
4587 }
4588 else if (TREE_CODE (decl) == FUNCTION_DECL)
4589 {
4590 if (DECL_DESTRUCTOR_P (decl))
4591 {
4592 /* [temp.mem]
4593
4594 A destructor shall not be a member template. */
4595 error ("destructor %qD declared as member template", decl);
4596 return error_mark_node;
4597 }
4598 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4599 && (!prototype_p (TREE_TYPE (decl))
4600 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4601 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4602 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4603 == void_list_node)))
4604 {
4605 /* [basic.stc.dynamic.allocation]
4606
4607 An allocation function can be a function
4608 template. ... Template allocation functions shall
4609 have two or more parameters. */
4610 error ("invalid template declaration of %qD", decl);
4611 return error_mark_node;
4612 }
4613 }
4614 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4615 && CLASS_TYPE_P (TREE_TYPE (decl)))
4616 /* OK */;
4617 else if (TREE_CODE (decl) == TYPE_DECL
4618 && TYPE_DECL_ALIAS_P (decl))
4619 /* alias-declaration */
4620 gcc_assert (!DECL_ARTIFICIAL (decl));
4621 else
4622 {
4623 error ("template declaration of %q#D", decl);
4624 return error_mark_node;
4625 }
4626 }
4627
4628 /* Check to see that the rules regarding the use of default
4629 arguments are not being violated. */
4630 check_default_tmpl_args (decl, current_template_parms,
4631 is_primary, is_partial, /*is_friend_decl=*/0);
4632
4633 /* Ensure that there are no parameter packs in the type of this
4634 declaration that have not been expanded. */
4635 if (TREE_CODE (decl) == FUNCTION_DECL)
4636 {
4637 /* Check each of the arguments individually to see if there are
4638 any bare parameter packs. */
4639 tree type = TREE_TYPE (decl);
4640 tree arg = DECL_ARGUMENTS (decl);
4641 tree argtype = TYPE_ARG_TYPES (type);
4642
4643 while (arg && argtype)
4644 {
4645 if (!FUNCTION_PARAMETER_PACK_P (arg)
4646 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4647 {
4648 /* This is a PARM_DECL that contains unexpanded parameter
4649 packs. We have already complained about this in the
4650 check_for_bare_parameter_packs call, so just replace
4651 these types with ERROR_MARK_NODE. */
4652 TREE_TYPE (arg) = error_mark_node;
4653 TREE_VALUE (argtype) = error_mark_node;
4654 }
4655
4656 arg = DECL_CHAIN (arg);
4657 argtype = TREE_CHAIN (argtype);
4658 }
4659
4660 /* Check for bare parameter packs in the return type and the
4661 exception specifiers. */
4662 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4663 /* Errors were already issued, set return type to int
4664 as the frontend doesn't expect error_mark_node as
4665 the return type. */
4666 TREE_TYPE (type) = integer_type_node;
4667 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4668 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4669 }
4670 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4671 && TYPE_DECL_ALIAS_P (decl))
4672 ? DECL_ORIGINAL_TYPE (decl)
4673 : TREE_TYPE (decl)))
4674 {
4675 TREE_TYPE (decl) = error_mark_node;
4676 return error_mark_node;
4677 }
4678
4679 if (is_partial)
4680 return process_partial_specialization (decl);
4681
4682 args = current_template_args ();
4683
4684 if (!ctx
4685 || TREE_CODE (ctx) == FUNCTION_DECL
4686 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4687 || (TREE_CODE (decl) == TYPE_DECL
4688 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4689 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4690 {
4691 if (DECL_LANG_SPECIFIC (decl)
4692 && DECL_TEMPLATE_INFO (decl)
4693 && DECL_TI_TEMPLATE (decl))
4694 tmpl = DECL_TI_TEMPLATE (decl);
4695 /* If DECL is a TYPE_DECL for a class-template, then there won't
4696 be DECL_LANG_SPECIFIC. The information equivalent to
4697 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4698 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4699 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4700 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4701 {
4702 /* Since a template declaration already existed for this
4703 class-type, we must be redeclaring it here. Make sure
4704 that the redeclaration is valid. */
4705 redeclare_class_template (TREE_TYPE (decl),
4706 current_template_parms);
4707 /* We don't need to create a new TEMPLATE_DECL; just use the
4708 one we already had. */
4709 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4710 }
4711 else
4712 {
4713 tmpl = build_template_decl (decl, current_template_parms,
4714 member_template_p);
4715 new_template_p = 1;
4716
4717 if (DECL_LANG_SPECIFIC (decl)
4718 && DECL_TEMPLATE_SPECIALIZATION (decl))
4719 {
4720 /* A specialization of a member template of a template
4721 class. */
4722 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4723 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4724 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4725 }
4726 }
4727 }
4728 else
4729 {
4730 tree a, t, current, parms;
4731 int i;
4732 tree tinfo = get_template_info (decl);
4733
4734 if (!tinfo)
4735 {
4736 error ("template definition of non-template %q#D", decl);
4737 return error_mark_node;
4738 }
4739
4740 tmpl = TI_TEMPLATE (tinfo);
4741
4742 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4743 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4744 && DECL_TEMPLATE_SPECIALIZATION (decl)
4745 && DECL_MEMBER_TEMPLATE_P (tmpl))
4746 {
4747 tree new_tmpl;
4748
4749 /* The declaration is a specialization of a member
4750 template, declared outside the class. Therefore, the
4751 innermost template arguments will be NULL, so we
4752 replace them with the arguments determined by the
4753 earlier call to check_explicit_specialization. */
4754 args = DECL_TI_ARGS (decl);
4755
4756 new_tmpl
4757 = build_template_decl (decl, current_template_parms,
4758 member_template_p);
4759 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4760 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4761 DECL_TI_TEMPLATE (decl) = new_tmpl;
4762 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4763 DECL_TEMPLATE_INFO (new_tmpl)
4764 = build_template_info (tmpl, args);
4765
4766 register_specialization (new_tmpl,
4767 most_general_template (tmpl),
4768 args,
4769 is_friend, 0);
4770 return decl;
4771 }
4772
4773 /* Make sure the template headers we got make sense. */
4774
4775 parms = DECL_TEMPLATE_PARMS (tmpl);
4776 i = TMPL_PARMS_DEPTH (parms);
4777 if (TMPL_ARGS_DEPTH (args) != i)
4778 {
4779 error ("expected %d levels of template parms for %q#D, got %d",
4780 i, decl, TMPL_ARGS_DEPTH (args));
4781 }
4782 else
4783 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4784 {
4785 a = TMPL_ARGS_LEVEL (args, i);
4786 t = INNERMOST_TEMPLATE_PARMS (parms);
4787
4788 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4789 {
4790 if (current == decl)
4791 error ("got %d template parameters for %q#D",
4792 TREE_VEC_LENGTH (a), decl);
4793 else
4794 error ("got %d template parameters for %q#T",
4795 TREE_VEC_LENGTH (a), current);
4796 error (" but %d required", TREE_VEC_LENGTH (t));
4797 /* Avoid crash in import_export_decl. */
4798 DECL_INTERFACE_KNOWN (decl) = 1;
4799 return error_mark_node;
4800 }
4801
4802 if (current == decl)
4803 current = ctx;
4804 else if (current == NULL_TREE)
4805 /* Can happen in erroneous input. */
4806 break;
4807 else
4808 current = (TYPE_P (current)
4809 ? TYPE_CONTEXT (current)
4810 : DECL_CONTEXT (current));
4811 }
4812
4813 /* Check that the parms are used in the appropriate qualifying scopes
4814 in the declarator. */
4815 if (!comp_template_args
4816 (TI_ARGS (tinfo),
4817 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4818 {
4819 error ("\
4820 template arguments to %qD do not match original template %qD",
4821 decl, DECL_TEMPLATE_RESULT (tmpl));
4822 if (!uses_template_parms (TI_ARGS (tinfo)))
4823 inform (input_location, "use template<> for an explicit specialization");
4824 /* Avoid crash in import_export_decl. */
4825 DECL_INTERFACE_KNOWN (decl) = 1;
4826 return error_mark_node;
4827 }
4828 }
4829
4830 DECL_TEMPLATE_RESULT (tmpl) = decl;
4831 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4832
4833 /* Push template declarations for global functions and types. Note
4834 that we do not try to push a global template friend declared in a
4835 template class; such a thing may well depend on the template
4836 parameters of the class. */
4837 if (new_template_p && !ctx
4838 && !(is_friend && template_class_depth (current_class_type) > 0))
4839 {
4840 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4841 if (tmpl == error_mark_node)
4842 return error_mark_node;
4843
4844 /* Hide template friend classes that haven't been declared yet. */
4845 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4846 {
4847 DECL_ANTICIPATED (tmpl) = 1;
4848 DECL_FRIEND_P (tmpl) = 1;
4849 }
4850 }
4851
4852 if (is_primary)
4853 {
4854 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4855 int i;
4856
4857 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4858 if (DECL_CONV_FN_P (tmpl))
4859 {
4860 int depth = TMPL_PARMS_DEPTH (parms);
4861
4862 /* It is a conversion operator. See if the type converted to
4863 depends on innermost template operands. */
4864
4865 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4866 depth))
4867 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4868 }
4869
4870 /* Give template template parms a DECL_CONTEXT of the template
4871 for which they are a parameter. */
4872 parms = INNERMOST_TEMPLATE_PARMS (parms);
4873 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4874 {
4875 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4876 if (TREE_CODE (parm) == TEMPLATE_DECL)
4877 DECL_CONTEXT (parm) = tmpl;
4878 }
4879 }
4880
4881 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4882 back to its most general template. If TMPL is a specialization,
4883 ARGS may only have the innermost set of arguments. Add the missing
4884 argument levels if necessary. */
4885 if (DECL_TEMPLATE_INFO (tmpl))
4886 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4887
4888 info = build_template_info (tmpl, args);
4889
4890 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4891 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4892 else
4893 {
4894 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4895 retrofit_lang_decl (decl);
4896 if (DECL_LANG_SPECIFIC (decl))
4897 DECL_TEMPLATE_INFO (decl) = info;
4898 }
4899
4900 return DECL_TEMPLATE_RESULT (tmpl);
4901 }
4902
4903 tree
4904 push_template_decl (tree decl)
4905 {
4906 return push_template_decl_real (decl, false);
4907 }
4908
4909 /* FN is an inheriting constructor that inherits from the constructor
4910 template INHERITED; turn FN into a constructor template with a matching
4911 template header. */
4912
4913 tree
4914 add_inherited_template_parms (tree fn, tree inherited)
4915 {
4916 tree inner_parms
4917 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4918 inner_parms = copy_node (inner_parms);
4919 tree parms
4920 = tree_cons (size_int (processing_template_decl + 1),
4921 inner_parms, current_template_parms);
4922 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4923 tree args = template_parms_to_args (parms);
4924 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4925 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4926 DECL_TEMPLATE_RESULT (tmpl) = fn;
4927 DECL_ARTIFICIAL (tmpl) = true;
4928 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4929 return tmpl;
4930 }
4931
4932 /* Called when a class template TYPE is redeclared with the indicated
4933 template PARMS, e.g.:
4934
4935 template <class T> struct S;
4936 template <class T> struct S {}; */
4937
4938 bool
4939 redeclare_class_template (tree type, tree parms)
4940 {
4941 tree tmpl;
4942 tree tmpl_parms;
4943 int i;
4944
4945 if (!TYPE_TEMPLATE_INFO (type))
4946 {
4947 error ("%qT is not a template type", type);
4948 return false;
4949 }
4950
4951 tmpl = TYPE_TI_TEMPLATE (type);
4952 if (!PRIMARY_TEMPLATE_P (tmpl))
4953 /* The type is nested in some template class. Nothing to worry
4954 about here; there are no new template parameters for the nested
4955 type. */
4956 return true;
4957
4958 if (!parms)
4959 {
4960 error ("template specifiers not specified in declaration of %qD",
4961 tmpl);
4962 return false;
4963 }
4964
4965 parms = INNERMOST_TEMPLATE_PARMS (parms);
4966 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4967
4968 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4969 {
4970 error_n (input_location, TREE_VEC_LENGTH (parms),
4971 "redeclared with %d template parameter",
4972 "redeclared with %d template parameters",
4973 TREE_VEC_LENGTH (parms));
4974 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4975 "previous declaration %q+D used %d template parameter",
4976 "previous declaration %q+D used %d template parameters",
4977 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4978 return false;
4979 }
4980
4981 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4982 {
4983 tree tmpl_parm;
4984 tree parm;
4985 tree tmpl_default;
4986 tree parm_default;
4987
4988 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4989 || TREE_VEC_ELT (parms, i) == error_mark_node)
4990 continue;
4991
4992 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4993 if (tmpl_parm == error_mark_node)
4994 return false;
4995
4996 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4997 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4998 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4999
5000 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5001 TEMPLATE_DECL. */
5002 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5003 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5004 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5005 || (TREE_CODE (tmpl_parm) != PARM_DECL
5006 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5007 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5008 || (TREE_CODE (tmpl_parm) == PARM_DECL
5009 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5010 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5011 {
5012 error ("template parameter %q+#D", tmpl_parm);
5013 error ("redeclared here as %q#D", parm);
5014 return false;
5015 }
5016
5017 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5018 {
5019 /* We have in [temp.param]:
5020
5021 A template-parameter may not be given default arguments
5022 by two different declarations in the same scope. */
5023 error_at (input_location, "redefinition of default argument for %q#D", parm);
5024 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5025 "original definition appeared here");
5026 return false;
5027 }
5028
5029 if (parm_default != NULL_TREE)
5030 /* Update the previous template parameters (which are the ones
5031 that will really count) with the new default value. */
5032 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5033 else if (tmpl_default != NULL_TREE)
5034 /* Update the new parameters, too; they'll be used as the
5035 parameters for any members. */
5036 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5037 }
5038
5039 return true;
5040 }
5041
5042 /* Simplify EXPR if it is a non-dependent expression. Returns the
5043 (possibly simplified) expression. */
5044
5045 tree
5046 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5047 {
5048 if (expr == NULL_TREE)
5049 return NULL_TREE;
5050
5051 /* If we're in a template, but EXPR isn't value dependent, simplify
5052 it. We're supposed to treat:
5053
5054 template <typename T> void f(T[1 + 1]);
5055 template <typename T> void f(T[2]);
5056
5057 as two declarations of the same function, for example. */
5058 if (processing_template_decl
5059 && !instantiation_dependent_expression_p (expr)
5060 && potential_constant_expression (expr))
5061 {
5062 HOST_WIDE_INT saved_processing_template_decl;
5063
5064 saved_processing_template_decl = processing_template_decl;
5065 processing_template_decl = 0;
5066 expr = tsubst_copy_and_build (expr,
5067 /*args=*/NULL_TREE,
5068 complain,
5069 /*in_decl=*/NULL_TREE,
5070 /*function_p=*/false,
5071 /*integral_constant_expression_p=*/true);
5072 processing_template_decl = saved_processing_template_decl;
5073 }
5074 return expr;
5075 }
5076
5077 tree
5078 fold_non_dependent_expr (tree expr)
5079 {
5080 return fold_non_dependent_expr_sfinae (expr, tf_error);
5081 }
5082
5083 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5084 template declaration, or a TYPE_DECL for an alias declaration. */
5085
5086 bool
5087 alias_type_or_template_p (tree t)
5088 {
5089 if (t == NULL_TREE)
5090 return false;
5091 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5092 || (TYPE_P (t)
5093 && TYPE_NAME (t)
5094 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5095 || DECL_ALIAS_TEMPLATE_P (t));
5096 }
5097
5098 /* Return TRUE iff is a specialization of an alias template. */
5099
5100 bool
5101 alias_template_specialization_p (const_tree t)
5102 {
5103 if (t == NULL_TREE)
5104 return false;
5105
5106 return (TYPE_P (t)
5107 && TYPE_TEMPLATE_INFO (t)
5108 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5109 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5110 }
5111
5112 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5113 must be a function or a pointer-to-function type, as specified
5114 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5115 and check that the resulting function has external linkage. */
5116
5117 static tree
5118 convert_nontype_argument_function (tree type, tree expr)
5119 {
5120 tree fns = expr;
5121 tree fn, fn_no_ptr;
5122 linkage_kind linkage;
5123
5124 fn = instantiate_type (type, fns, tf_none);
5125 if (fn == error_mark_node)
5126 return error_mark_node;
5127
5128 fn_no_ptr = fn;
5129 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5130 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5131 if (BASELINK_P (fn_no_ptr))
5132 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5133
5134 /* [temp.arg.nontype]/1
5135
5136 A template-argument for a non-type, non-template template-parameter
5137 shall be one of:
5138 [...]
5139 -- the address of an object or function with external [C++11: or
5140 internal] linkage. */
5141
5142 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5143 {
5144 error ("%qE is not a valid template argument for type %qT", expr, type);
5145 if (TREE_CODE (type) == POINTER_TYPE)
5146 error ("it must be the address of a function with external linkage");
5147 else
5148 error ("it must be the name of a function with external linkage");
5149 return NULL_TREE;
5150 }
5151
5152 linkage = decl_linkage (fn_no_ptr);
5153 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5154 {
5155 if (cxx_dialect >= cxx0x)
5156 error ("%qE is not a valid template argument for type %qT "
5157 "because %qD has no linkage",
5158 expr, type, fn_no_ptr);
5159 else
5160 error ("%qE is not a valid template argument for type %qT "
5161 "because %qD does not have external linkage",
5162 expr, type, fn_no_ptr);
5163 return NULL_TREE;
5164 }
5165
5166 return fn;
5167 }
5168
5169 /* Subroutine of convert_nontype_argument.
5170 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5171 Emit an error otherwise. */
5172
5173 static bool
5174 check_valid_ptrmem_cst_expr (tree type, tree expr,
5175 tsubst_flags_t complain)
5176 {
5177 STRIP_NOPS (expr);
5178 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5179 return true;
5180 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5181 return true;
5182 if (complain & tf_error)
5183 {
5184 error ("%qE is not a valid template argument for type %qT",
5185 expr, type);
5186 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5187 }
5188 return false;
5189 }
5190
5191 /* Returns TRUE iff the address of OP is value-dependent.
5192
5193 14.6.2.4 [temp.dep.temp]:
5194 A non-integral non-type template-argument is dependent if its type is
5195 dependent or it has either of the following forms
5196 qualified-id
5197 & qualified-id
5198 and contains a nested-name-specifier which specifies a class-name that
5199 names a dependent type.
5200
5201 We generalize this to just say that the address of a member of a
5202 dependent class is value-dependent; the above doesn't cover the
5203 address of a static data member named with an unqualified-id. */
5204
5205 static bool
5206 has_value_dependent_address (tree op)
5207 {
5208 /* We could use get_inner_reference here, but there's no need;
5209 this is only relevant for template non-type arguments, which
5210 can only be expressed as &id-expression. */
5211 if (DECL_P (op))
5212 {
5213 tree ctx = CP_DECL_CONTEXT (op);
5214 if (TYPE_P (ctx) && dependent_type_p (ctx))
5215 return true;
5216 }
5217
5218 return false;
5219 }
5220
5221 /* The next set of functions are used for providing helpful explanatory
5222 diagnostics for failed overload resolution. Their messages should be
5223 indented by two spaces for consistency with the messages in
5224 call.c */
5225
5226 static int
5227 unify_success (bool /*explain_p*/)
5228 {
5229 return 0;
5230 }
5231
5232 static int
5233 unify_parameter_deduction_failure (bool explain_p, tree parm)
5234 {
5235 if (explain_p)
5236 inform (input_location,
5237 " couldn't deduce template parameter %qD", parm);
5238 return 1;
5239 }
5240
5241 static int
5242 unify_invalid (bool /*explain_p*/)
5243 {
5244 return 1;
5245 }
5246
5247 static int
5248 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5249 {
5250 if (explain_p)
5251 inform (input_location,
5252 " types %qT and %qT have incompatible cv-qualifiers",
5253 parm, arg);
5254 return 1;
5255 }
5256
5257 static int
5258 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5259 {
5260 if (explain_p)
5261 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5262 return 1;
5263 }
5264
5265 static int
5266 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5267 {
5268 if (explain_p)
5269 inform (input_location,
5270 " template parameter %qD is not a parameter pack, but "
5271 "argument %qD is",
5272 parm, arg);
5273 return 1;
5274 }
5275
5276 static int
5277 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5278 {
5279 if (explain_p)
5280 inform (input_location,
5281 " template argument %qE does not match "
5282 "pointer-to-member constant %qE",
5283 arg, parm);
5284 return 1;
5285 }
5286
5287 static int
5288 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5289 {
5290 if (explain_p)
5291 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5292 return 1;
5293 }
5294
5295 static int
5296 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5297 {
5298 if (explain_p)
5299 inform (input_location,
5300 " inconsistent parameter pack deduction with %qT and %qT",
5301 old_arg, new_arg);
5302 return 1;
5303 }
5304
5305 static int
5306 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5307 {
5308 if (explain_p)
5309 {
5310 if (TYPE_P (parm))
5311 inform (input_location,
5312 " deduced conflicting types for parameter %qT (%qT and %qT)",
5313 parm, first, second);
5314 else
5315 inform (input_location,
5316 " deduced conflicting values for non-type parameter "
5317 "%qE (%qE and %qE)", parm, first, second);
5318 }
5319 return 1;
5320 }
5321
5322 static int
5323 unify_vla_arg (bool explain_p, tree arg)
5324 {
5325 if (explain_p)
5326 inform (input_location,
5327 " variable-sized array type %qT is not "
5328 "a valid template argument",
5329 arg);
5330 return 1;
5331 }
5332
5333 static int
5334 unify_method_type_error (bool explain_p, tree arg)
5335 {
5336 if (explain_p)
5337 inform (input_location,
5338 " member function type %qT is not a valid template argument",
5339 arg);
5340 return 1;
5341 }
5342
5343 static int
5344 unify_arity (bool explain_p, int have, int wanted)
5345 {
5346 if (explain_p)
5347 inform_n (input_location, wanted,
5348 " candidate expects %d argument, %d provided",
5349 " candidate expects %d arguments, %d provided",
5350 wanted, have);
5351 return 1;
5352 }
5353
5354 static int
5355 unify_too_many_arguments (bool explain_p, int have, int wanted)
5356 {
5357 return unify_arity (explain_p, have, wanted);
5358 }
5359
5360 static int
5361 unify_too_few_arguments (bool explain_p, int have, int wanted)
5362 {
5363 return unify_arity (explain_p, have, wanted);
5364 }
5365
5366 static int
5367 unify_arg_conversion (bool explain_p, tree to_type,
5368 tree from_type, tree arg)
5369 {
5370 if (explain_p)
5371 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5372 arg, from_type, to_type);
5373 return 1;
5374 }
5375
5376 static int
5377 unify_no_common_base (bool explain_p, enum template_base_result r,
5378 tree parm, tree arg)
5379 {
5380 if (explain_p)
5381 switch (r)
5382 {
5383 case tbr_ambiguous_baseclass:
5384 inform (input_location, " %qT is an ambiguous base class of %qT",
5385 arg, parm);
5386 break;
5387 default:
5388 inform (input_location, " %qT is not derived from %qT", arg, parm);
5389 break;
5390 }
5391 return 1;
5392 }
5393
5394 static int
5395 unify_inconsistent_template_template_parameters (bool explain_p)
5396 {
5397 if (explain_p)
5398 inform (input_location,
5399 " template parameters of a template template argument are "
5400 "inconsistent with other deduced template arguments");
5401 return 1;
5402 }
5403
5404 static int
5405 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5406 {
5407 if (explain_p)
5408 inform (input_location,
5409 " can't deduce a template for %qT from non-template type %qT",
5410 parm, arg);
5411 return 1;
5412 }
5413
5414 static int
5415 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5416 {
5417 if (explain_p)
5418 inform (input_location,
5419 " template argument %qE does not match %qD", arg, parm);
5420 return 1;
5421 }
5422
5423 static int
5424 unify_overload_resolution_failure (bool explain_p, tree arg)
5425 {
5426 if (explain_p)
5427 inform (input_location,
5428 " could not resolve address from overloaded function %qE",
5429 arg);
5430 return 1;
5431 }
5432
5433 /* Attempt to convert the non-type template parameter EXPR to the
5434 indicated TYPE. If the conversion is successful, return the
5435 converted value. If the conversion is unsuccessful, return
5436 NULL_TREE if we issued an error message, or error_mark_node if we
5437 did not. We issue error messages for out-and-out bad template
5438 parameters, but not simply because the conversion failed, since we
5439 might be just trying to do argument deduction. Both TYPE and EXPR
5440 must be non-dependent.
5441
5442 The conversion follows the special rules described in
5443 [temp.arg.nontype], and it is much more strict than an implicit
5444 conversion.
5445
5446 This function is called twice for each template argument (see
5447 lookup_template_class for a more accurate description of this
5448 problem). This means that we need to handle expressions which
5449 are not valid in a C++ source, but can be created from the
5450 first call (for instance, casts to perform conversions). These
5451 hacks can go away after we fix the double coercion problem. */
5452
5453 static tree
5454 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5455 {
5456 tree expr_type;
5457
5458 /* Detect immediately string literals as invalid non-type argument.
5459 This special-case is not needed for correctness (we would easily
5460 catch this later), but only to provide better diagnostic for this
5461 common user mistake. As suggested by DR 100, we do not mention
5462 linkage issues in the diagnostic as this is not the point. */
5463 /* FIXME we're making this OK. */
5464 if (TREE_CODE (expr) == STRING_CST)
5465 {
5466 if (complain & tf_error)
5467 error ("%qE is not a valid template argument for type %qT "
5468 "because string literals can never be used in this context",
5469 expr, type);
5470 return NULL_TREE;
5471 }
5472
5473 /* Add the ADDR_EXPR now for the benefit of
5474 value_dependent_expression_p. */
5475 if (TYPE_PTROBV_P (type)
5476 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5477 {
5478 expr = decay_conversion (expr, complain);
5479 if (expr == error_mark_node)
5480 return error_mark_node;
5481 }
5482
5483 /* If we are in a template, EXPR may be non-dependent, but still
5484 have a syntactic, rather than semantic, form. For example, EXPR
5485 might be a SCOPE_REF, rather than the VAR_DECL to which the
5486 SCOPE_REF refers. Preserving the qualifying scope is necessary
5487 so that access checking can be performed when the template is
5488 instantiated -- but here we need the resolved form so that we can
5489 convert the argument. */
5490 if (TYPE_REF_OBJ_P (type)
5491 && has_value_dependent_address (expr))
5492 /* If we want the address and it's value-dependent, don't fold. */;
5493 else if (!type_unknown_p (expr))
5494 expr = fold_non_dependent_expr_sfinae (expr, complain);
5495 if (error_operand_p (expr))
5496 return error_mark_node;
5497 expr_type = TREE_TYPE (expr);
5498 if (TREE_CODE (type) == REFERENCE_TYPE)
5499 expr = mark_lvalue_use (expr);
5500 else
5501 expr = mark_rvalue_use (expr);
5502
5503 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5504 to a non-type argument of "nullptr". */
5505 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5506 expr = convert (type, expr);
5507
5508 /* In C++11, integral or enumeration non-type template arguments can be
5509 arbitrary constant expressions. Pointer and pointer to
5510 member arguments can be general constant expressions that evaluate
5511 to a null value, but otherwise still need to be of a specific form. */
5512 if (cxx_dialect >= cxx0x)
5513 {
5514 if (TREE_CODE (expr) == PTRMEM_CST)
5515 /* A PTRMEM_CST is already constant, and a valid template
5516 argument for a parameter of pointer to member type, we just want
5517 to leave it in that form rather than lower it to a
5518 CONSTRUCTOR. */;
5519 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5520 expr = maybe_constant_value (expr);
5521 else if (TYPE_PTR_OR_PTRMEM_P (type))
5522 {
5523 tree folded = maybe_constant_value (expr);
5524 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5525 : null_member_pointer_value_p (folded))
5526 expr = folded;
5527 }
5528 }
5529
5530 /* HACK: Due to double coercion, we can get a
5531 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5532 which is the tree that we built on the first call (see
5533 below when coercing to reference to object or to reference to
5534 function). We just strip everything and get to the arg.
5535 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5536 for examples. */
5537 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5538 {
5539 tree probe_type, probe = expr;
5540 if (REFERENCE_REF_P (probe))
5541 probe = TREE_OPERAND (probe, 0);
5542 probe_type = TREE_TYPE (probe);
5543 if (TREE_CODE (probe) == NOP_EXPR)
5544 {
5545 /* ??? Maybe we could use convert_from_reference here, but we
5546 would need to relax its constraints because the NOP_EXPR
5547 could actually change the type to something more cv-qualified,
5548 and this is not folded by convert_from_reference. */
5549 tree addr = TREE_OPERAND (probe, 0);
5550 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5551 && TREE_CODE (addr) == ADDR_EXPR
5552 && TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE
5553 && (same_type_ignoring_top_level_qualifiers_p
5554 (TREE_TYPE (probe_type),
5555 TREE_TYPE (TREE_TYPE (addr)))))
5556 {
5557 expr = TREE_OPERAND (addr, 0);
5558 expr_type = TREE_TYPE (expr);
5559 }
5560 }
5561 }
5562
5563 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5564 parameter is a pointer to object, through decay and
5565 qualification conversion. Let's strip everything. */
5566 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5567 {
5568 tree probe = expr;
5569 STRIP_NOPS (probe);
5570 if (TREE_CODE (probe) == ADDR_EXPR
5571 && TREE_CODE (TREE_TYPE (probe)) == POINTER_TYPE)
5572 {
5573 /* Skip the ADDR_EXPR only if it is part of the decay for
5574 an array. Otherwise, it is part of the original argument
5575 in the source code. */
5576 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5577 probe = TREE_OPERAND (probe, 0);
5578 expr = probe;
5579 expr_type = TREE_TYPE (expr);
5580 }
5581 }
5582
5583 /* [temp.arg.nontype]/5, bullet 1
5584
5585 For a non-type template-parameter of integral or enumeration type,
5586 integral promotions (_conv.prom_) and integral conversions
5587 (_conv.integral_) are applied. */
5588 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5589 {
5590 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5591 t = maybe_constant_value (t);
5592 if (t != error_mark_node)
5593 expr = t;
5594
5595 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5596 return error_mark_node;
5597
5598 /* Notice that there are constant expressions like '4 % 0' which
5599 do not fold into integer constants. */
5600 if (TREE_CODE (expr) != INTEGER_CST)
5601 {
5602 if (complain & tf_error)
5603 {
5604 int errs = errorcount, warns = warningcount + werrorcount;
5605 if (processing_template_decl
5606 && !require_potential_constant_expression (expr))
5607 return NULL_TREE;
5608 expr = cxx_constant_value (expr);
5609 if (errorcount > errs || warningcount + werrorcount > warns)
5610 inform (EXPR_LOC_OR_HERE (expr),
5611 "in template argument for type %qT ", type);
5612 if (expr == error_mark_node)
5613 return NULL_TREE;
5614 /* else cxx_constant_value complained but gave us
5615 a real constant, so go ahead. */
5616 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5617 }
5618 else
5619 return NULL_TREE;
5620 }
5621 }
5622 /* [temp.arg.nontype]/5, bullet 2
5623
5624 For a non-type template-parameter of type pointer to object,
5625 qualification conversions (_conv.qual_) and the array-to-pointer
5626 conversion (_conv.array_) are applied. */
5627 else if (TYPE_PTROBV_P (type))
5628 {
5629 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5630
5631 A template-argument for a non-type, non-template template-parameter
5632 shall be one of: [...]
5633
5634 -- the name of a non-type template-parameter;
5635 -- the address of an object or function with external linkage, [...]
5636 expressed as "& id-expression" where the & is optional if the name
5637 refers to a function or array, or if the corresponding
5638 template-parameter is a reference.
5639
5640 Here, we do not care about functions, as they are invalid anyway
5641 for a parameter of type pointer-to-object. */
5642
5643 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5644 /* Non-type template parameters are OK. */
5645 ;
5646 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5647 /* Null pointer values are OK in C++11. */;
5648 else if (TREE_CODE (expr) != ADDR_EXPR
5649 && TREE_CODE (expr_type) != ARRAY_TYPE)
5650 {
5651 if (TREE_CODE (expr) == VAR_DECL)
5652 {
5653 error ("%qD is not a valid template argument "
5654 "because %qD is a variable, not the address of "
5655 "a variable",
5656 expr, expr);
5657 return NULL_TREE;
5658 }
5659 if (POINTER_TYPE_P (expr_type))
5660 {
5661 error ("%qE is not a valid template argument for %qT"
5662 "because it is not the address of a variable",
5663 expr, type);
5664 return NULL_TREE;
5665 }
5666 /* Other values, like integer constants, might be valid
5667 non-type arguments of some other type. */
5668 return error_mark_node;
5669 }
5670 else
5671 {
5672 tree decl;
5673
5674 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5675 ? TREE_OPERAND (expr, 0) : expr);
5676 if (TREE_CODE (decl) != VAR_DECL)
5677 {
5678 error ("%qE is not a valid template argument of type %qT "
5679 "because %qE is not a variable",
5680 expr, type, decl);
5681 return NULL_TREE;
5682 }
5683 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5684 {
5685 error ("%qE is not a valid template argument of type %qT "
5686 "because %qD does not have external linkage",
5687 expr, type, decl);
5688 return NULL_TREE;
5689 }
5690 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5691 {
5692 error ("%qE is not a valid template argument of type %qT "
5693 "because %qD has no linkage",
5694 expr, type, decl);
5695 return NULL_TREE;
5696 }
5697 }
5698
5699 expr = decay_conversion (expr, complain);
5700 if (expr == error_mark_node)
5701 return error_mark_node;
5702
5703 expr = perform_qualification_conversions (type, expr);
5704 if (expr == error_mark_node)
5705 return error_mark_node;
5706 }
5707 /* [temp.arg.nontype]/5, bullet 3
5708
5709 For a non-type template-parameter of type reference to object, no
5710 conversions apply. The type referred to by the reference may be more
5711 cv-qualified than the (otherwise identical) type of the
5712 template-argument. The template-parameter is bound directly to the
5713 template-argument, which must be an lvalue. */
5714 else if (TYPE_REF_OBJ_P (type))
5715 {
5716 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5717 expr_type))
5718 return error_mark_node;
5719
5720 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5721 {
5722 error ("%qE is not a valid template argument for type %qT "
5723 "because of conflicts in cv-qualification", expr, type);
5724 return NULL_TREE;
5725 }
5726
5727 if (!real_lvalue_p (expr))
5728 {
5729 error ("%qE is not a valid template argument for type %qT "
5730 "because it is not an lvalue", expr, type);
5731 return NULL_TREE;
5732 }
5733
5734 /* [temp.arg.nontype]/1
5735
5736 A template-argument for a non-type, non-template template-parameter
5737 shall be one of: [...]
5738
5739 -- the address of an object or function with external linkage. */
5740 if (TREE_CODE (expr) == INDIRECT_REF
5741 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5742 {
5743 expr = TREE_OPERAND (expr, 0);
5744 if (DECL_P (expr))
5745 {
5746 error ("%q#D is not a valid template argument for type %qT "
5747 "because a reference variable does not have a constant "
5748 "address", expr, type);
5749 return NULL_TREE;
5750 }
5751 }
5752
5753 if (!DECL_P (expr))
5754 {
5755 error ("%qE is not a valid template argument for type %qT "
5756 "because it is not an object with external linkage",
5757 expr, type);
5758 return NULL_TREE;
5759 }
5760
5761 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5762 {
5763 error ("%qE is not a valid template argument for type %qT "
5764 "because object %qD has not external linkage",
5765 expr, type, expr);
5766 return NULL_TREE;
5767 }
5768
5769 expr = build_nop (type, build_address (expr));
5770 }
5771 /* [temp.arg.nontype]/5, bullet 4
5772
5773 For a non-type template-parameter of type pointer to function, only
5774 the function-to-pointer conversion (_conv.func_) is applied. If the
5775 template-argument represents a set of overloaded functions (or a
5776 pointer to such), the matching function is selected from the set
5777 (_over.over_). */
5778 else if (TYPE_PTRFN_P (type))
5779 {
5780 /* If the argument is a template-id, we might not have enough
5781 context information to decay the pointer. */
5782 if (!type_unknown_p (expr_type))
5783 {
5784 expr = decay_conversion (expr, complain);
5785 if (expr == error_mark_node)
5786 return error_mark_node;
5787 }
5788
5789 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5790 /* Null pointer values are OK in C++11. */
5791 return perform_qualification_conversions (type, expr);
5792
5793 expr = convert_nontype_argument_function (type, expr);
5794 if (!expr || expr == error_mark_node)
5795 return expr;
5796 }
5797 /* [temp.arg.nontype]/5, bullet 5
5798
5799 For a non-type template-parameter of type reference to function, no
5800 conversions apply. If the template-argument represents a set of
5801 overloaded functions, the matching function is selected from the set
5802 (_over.over_). */
5803 else if (TYPE_REFFN_P (type))
5804 {
5805 if (TREE_CODE (expr) == ADDR_EXPR)
5806 {
5807 error ("%qE is not a valid template argument for type %qT "
5808 "because it is a pointer", expr, type);
5809 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5810 return NULL_TREE;
5811 }
5812
5813 expr = convert_nontype_argument_function (type, expr);
5814 if (!expr || expr == error_mark_node)
5815 return expr;
5816
5817 expr = build_nop (type, build_address (expr));
5818 }
5819 /* [temp.arg.nontype]/5, bullet 6
5820
5821 For a non-type template-parameter of type pointer to member function,
5822 no conversions apply. If the template-argument represents a set of
5823 overloaded member functions, the matching member function is selected
5824 from the set (_over.over_). */
5825 else if (TYPE_PTRMEMFUNC_P (type))
5826 {
5827 expr = instantiate_type (type, expr, tf_none);
5828 if (expr == error_mark_node)
5829 return error_mark_node;
5830
5831 /* [temp.arg.nontype] bullet 1 says the pointer to member
5832 expression must be a pointer-to-member constant. */
5833 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5834 return error_mark_node;
5835
5836 /* There is no way to disable standard conversions in
5837 resolve_address_of_overloaded_function (called by
5838 instantiate_type). It is possible that the call succeeded by
5839 converting &B::I to &D::I (where B is a base of D), so we need
5840 to reject this conversion here.
5841
5842 Actually, even if there was a way to disable standard conversions,
5843 it would still be better to reject them here so that we can
5844 provide a superior diagnostic. */
5845 if (!same_type_p (TREE_TYPE (expr), type))
5846 {
5847 error ("%qE is not a valid template argument for type %qT "
5848 "because it is of type %qT", expr, type,
5849 TREE_TYPE (expr));
5850 /* If we are just one standard conversion off, explain. */
5851 if (can_convert (type, TREE_TYPE (expr), complain))
5852 inform (input_location,
5853 "standard conversions are not allowed in this context");
5854 return NULL_TREE;
5855 }
5856 }
5857 /* [temp.arg.nontype]/5, bullet 7
5858
5859 For a non-type template-parameter of type pointer to data member,
5860 qualification conversions (_conv.qual_) are applied. */
5861 else if (TYPE_PTRDATAMEM_P (type))
5862 {
5863 /* [temp.arg.nontype] bullet 1 says the pointer to member
5864 expression must be a pointer-to-member constant. */
5865 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5866 return error_mark_node;
5867
5868 expr = perform_qualification_conversions (type, expr);
5869 if (expr == error_mark_node)
5870 return expr;
5871 }
5872 else if (NULLPTR_TYPE_P (type))
5873 {
5874 if (expr != nullptr_node)
5875 {
5876 error ("%qE is not a valid template argument for type %qT "
5877 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5878 return NULL_TREE;
5879 }
5880 return expr;
5881 }
5882 /* A template non-type parameter must be one of the above. */
5883 else
5884 gcc_unreachable ();
5885
5886 /* Sanity check: did we actually convert the argument to the
5887 right type? */
5888 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5889 (type, TREE_TYPE (expr)));
5890 return expr;
5891 }
5892
5893 /* Subroutine of coerce_template_template_parms, which returns 1 if
5894 PARM_PARM and ARG_PARM match using the rule for the template
5895 parameters of template template parameters. Both PARM and ARG are
5896 template parameters; the rest of the arguments are the same as for
5897 coerce_template_template_parms.
5898 */
5899 static int
5900 coerce_template_template_parm (tree parm,
5901 tree arg,
5902 tsubst_flags_t complain,
5903 tree in_decl,
5904 tree outer_args)
5905 {
5906 if (arg == NULL_TREE || arg == error_mark_node
5907 || parm == NULL_TREE || parm == error_mark_node)
5908 return 0;
5909
5910 if (TREE_CODE (arg) != TREE_CODE (parm))
5911 return 0;
5912
5913 switch (TREE_CODE (parm))
5914 {
5915 case TEMPLATE_DECL:
5916 /* We encounter instantiations of templates like
5917 template <template <template <class> class> class TT>
5918 class C; */
5919 {
5920 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5921 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5922
5923 if (!coerce_template_template_parms
5924 (parmparm, argparm, complain, in_decl, outer_args))
5925 return 0;
5926 }
5927 /* Fall through. */
5928
5929 case TYPE_DECL:
5930 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5931 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5932 /* Argument is a parameter pack but parameter is not. */
5933 return 0;
5934 break;
5935
5936 case PARM_DECL:
5937 /* The tsubst call is used to handle cases such as
5938
5939 template <int> class C {};
5940 template <class T, template <T> class TT> class D {};
5941 D<int, C> d;
5942
5943 i.e. the parameter list of TT depends on earlier parameters. */
5944 if (!uses_template_parms (TREE_TYPE (arg))
5945 && !same_type_p
5946 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5947 TREE_TYPE (arg)))
5948 return 0;
5949
5950 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5951 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5952 /* Argument is a parameter pack but parameter is not. */
5953 return 0;
5954
5955 break;
5956
5957 default:
5958 gcc_unreachable ();
5959 }
5960
5961 return 1;
5962 }
5963
5964
5965 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5966 template template parameters. Both PARM_PARMS and ARG_PARMS are
5967 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5968 or PARM_DECL.
5969
5970 Consider the example:
5971 template <class T> class A;
5972 template<template <class U> class TT> class B;
5973
5974 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5975 the parameters to A, and OUTER_ARGS contains A. */
5976
5977 static int
5978 coerce_template_template_parms (tree parm_parms,
5979 tree arg_parms,
5980 tsubst_flags_t complain,
5981 tree in_decl,
5982 tree outer_args)
5983 {
5984 int nparms, nargs, i;
5985 tree parm, arg;
5986 int variadic_p = 0;
5987
5988 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5989 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5990
5991 nparms = TREE_VEC_LENGTH (parm_parms);
5992 nargs = TREE_VEC_LENGTH (arg_parms);
5993
5994 /* Determine whether we have a parameter pack at the end of the
5995 template template parameter's template parameter list. */
5996 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5997 {
5998 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5999
6000 if (parm == error_mark_node)
6001 return 0;
6002
6003 switch (TREE_CODE (parm))
6004 {
6005 case TEMPLATE_DECL:
6006 case TYPE_DECL:
6007 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6008 variadic_p = 1;
6009 break;
6010
6011 case PARM_DECL:
6012 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6013 variadic_p = 1;
6014 break;
6015
6016 default:
6017 gcc_unreachable ();
6018 }
6019 }
6020
6021 if (nargs != nparms
6022 && !(variadic_p && nargs >= nparms - 1))
6023 return 0;
6024
6025 /* Check all of the template parameters except the parameter pack at
6026 the end (if any). */
6027 for (i = 0; i < nparms - variadic_p; ++i)
6028 {
6029 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6030 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6031 continue;
6032
6033 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6034 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6035
6036 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6037 outer_args))
6038 return 0;
6039
6040 }
6041
6042 if (variadic_p)
6043 {
6044 /* Check each of the template parameters in the template
6045 argument against the template parameter pack at the end of
6046 the template template parameter. */
6047 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6048 return 0;
6049
6050 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6051
6052 for (; i < nargs; ++i)
6053 {
6054 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6055 continue;
6056
6057 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6058
6059 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6060 outer_args))
6061 return 0;
6062 }
6063 }
6064
6065 return 1;
6066 }
6067
6068 /* Verifies that the deduced template arguments (in TARGS) for the
6069 template template parameters (in TPARMS) represent valid bindings,
6070 by comparing the template parameter list of each template argument
6071 to the template parameter list of its corresponding template
6072 template parameter, in accordance with DR150. This
6073 routine can only be called after all template arguments have been
6074 deduced. It will return TRUE if all of the template template
6075 parameter bindings are okay, FALSE otherwise. */
6076 bool
6077 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6078 {
6079 int i, ntparms = TREE_VEC_LENGTH (tparms);
6080 bool ret = true;
6081
6082 /* We're dealing with template parms in this process. */
6083 ++processing_template_decl;
6084
6085 targs = INNERMOST_TEMPLATE_ARGS (targs);
6086
6087 for (i = 0; i < ntparms; ++i)
6088 {
6089 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6090 tree targ = TREE_VEC_ELT (targs, i);
6091
6092 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6093 {
6094 tree packed_args = NULL_TREE;
6095 int idx, len = 1;
6096
6097 if (ARGUMENT_PACK_P (targ))
6098 {
6099 /* Look inside the argument pack. */
6100 packed_args = ARGUMENT_PACK_ARGS (targ);
6101 len = TREE_VEC_LENGTH (packed_args);
6102 }
6103
6104 for (idx = 0; idx < len; ++idx)
6105 {
6106 tree targ_parms = NULL_TREE;
6107
6108 if (packed_args)
6109 /* Extract the next argument from the argument
6110 pack. */
6111 targ = TREE_VEC_ELT (packed_args, idx);
6112
6113 if (PACK_EXPANSION_P (targ))
6114 /* Look at the pattern of the pack expansion. */
6115 targ = PACK_EXPANSION_PATTERN (targ);
6116
6117 /* Extract the template parameters from the template
6118 argument. */
6119 if (TREE_CODE (targ) == TEMPLATE_DECL)
6120 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6121 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6122 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6123
6124 /* Verify that we can coerce the template template
6125 parameters from the template argument to the template
6126 parameter. This requires an exact match. */
6127 if (targ_parms
6128 && !coerce_template_template_parms
6129 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6130 targ_parms,
6131 tf_none,
6132 tparm,
6133 targs))
6134 {
6135 ret = false;
6136 goto out;
6137 }
6138 }
6139 }
6140 }
6141
6142 out:
6143
6144 --processing_template_decl;
6145 return ret;
6146 }
6147
6148 /* Since type attributes aren't mangled, we need to strip them from
6149 template type arguments. */
6150
6151 static tree
6152 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6153 {
6154 tree mv;
6155 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6156 return arg;
6157 mv = TYPE_MAIN_VARIANT (arg);
6158 arg = strip_typedefs (arg);
6159 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6160 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6161 {
6162 if (complain & tf_warning)
6163 warning (0, "ignoring attributes on template argument %qT", arg);
6164 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6165 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6166 }
6167 return arg;
6168 }
6169
6170 /* Convert the indicated template ARG as necessary to match the
6171 indicated template PARM. Returns the converted ARG, or
6172 error_mark_node if the conversion was unsuccessful. Error and
6173 warning messages are issued under control of COMPLAIN. This
6174 conversion is for the Ith parameter in the parameter list. ARGS is
6175 the full set of template arguments deduced so far. */
6176
6177 static tree
6178 convert_template_argument (tree parm,
6179 tree arg,
6180 tree args,
6181 tsubst_flags_t complain,
6182 int i,
6183 tree in_decl)
6184 {
6185 tree orig_arg;
6186 tree val;
6187 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6188
6189 if (TREE_CODE (arg) == TREE_LIST
6190 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6191 {
6192 /* The template argument was the name of some
6193 member function. That's usually
6194 invalid, but static members are OK. In any
6195 case, grab the underlying fields/functions
6196 and issue an error later if required. */
6197 orig_arg = TREE_VALUE (arg);
6198 TREE_TYPE (arg) = unknown_type_node;
6199 }
6200
6201 orig_arg = arg;
6202
6203 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6204 requires_type = (TREE_CODE (parm) == TYPE_DECL
6205 || requires_tmpl_type);
6206
6207 /* When determining whether an argument pack expansion is a template,
6208 look at the pattern. */
6209 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6210 arg = PACK_EXPANSION_PATTERN (arg);
6211
6212 /* Deal with an injected-class-name used as a template template arg. */
6213 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6214 {
6215 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6216 if (TREE_CODE (t) == TEMPLATE_DECL)
6217 {
6218 if (cxx_dialect >= cxx0x)
6219 /* OK under DR 1004. */;
6220 else if (complain & tf_warning_or_error)
6221 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6222 " used as template template argument", TYPE_NAME (arg));
6223 else if (flag_pedantic_errors)
6224 t = arg;
6225
6226 arg = t;
6227 }
6228 }
6229
6230 is_tmpl_type =
6231 ((TREE_CODE (arg) == TEMPLATE_DECL
6232 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6233 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6234 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6235 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6236
6237 if (is_tmpl_type
6238 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6239 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6240 arg = TYPE_STUB_DECL (arg);
6241
6242 is_type = TYPE_P (arg) || is_tmpl_type;
6243
6244 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6245 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6246 {
6247 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6248 {
6249 if (complain & tf_error)
6250 error ("invalid use of destructor %qE as a type", orig_arg);
6251 return error_mark_node;
6252 }
6253
6254 permerror (input_location,
6255 "to refer to a type member of a template parameter, "
6256 "use %<typename %E%>", orig_arg);
6257
6258 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6259 TREE_OPERAND (arg, 1),
6260 typename_type,
6261 complain);
6262 arg = orig_arg;
6263 is_type = 1;
6264 }
6265 if (is_type != requires_type)
6266 {
6267 if (in_decl)
6268 {
6269 if (complain & tf_error)
6270 {
6271 error ("type/value mismatch at argument %d in template "
6272 "parameter list for %qD",
6273 i + 1, in_decl);
6274 if (is_type)
6275 error (" expected a constant of type %qT, got %qT",
6276 TREE_TYPE (parm),
6277 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6278 else if (requires_tmpl_type)
6279 error (" expected a class template, got %qE", orig_arg);
6280 else
6281 error (" expected a type, got %qE", orig_arg);
6282 }
6283 }
6284 return error_mark_node;
6285 }
6286 if (is_tmpl_type ^ requires_tmpl_type)
6287 {
6288 if (in_decl && (complain & tf_error))
6289 {
6290 error ("type/value mismatch at argument %d in template "
6291 "parameter list for %qD",
6292 i + 1, in_decl);
6293 if (is_tmpl_type)
6294 error (" expected a type, got %qT", DECL_NAME (arg));
6295 else
6296 error (" expected a class template, got %qT", orig_arg);
6297 }
6298 return error_mark_node;
6299 }
6300
6301 if (is_type)
6302 {
6303 if (requires_tmpl_type)
6304 {
6305 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6306 val = orig_arg;
6307 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6308 /* The number of argument required is not known yet.
6309 Just accept it for now. */
6310 val = TREE_TYPE (arg);
6311 else
6312 {
6313 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6314 tree argparm;
6315
6316 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6317
6318 if (coerce_template_template_parms (parmparm, argparm,
6319 complain, in_decl,
6320 args))
6321 {
6322 val = arg;
6323
6324 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6325 TEMPLATE_DECL. */
6326 if (val != error_mark_node)
6327 {
6328 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6329 val = TREE_TYPE (val);
6330 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6331 val = make_pack_expansion (val);
6332 }
6333 }
6334 else
6335 {
6336 if (in_decl && (complain & tf_error))
6337 {
6338 error ("type/value mismatch at argument %d in "
6339 "template parameter list for %qD",
6340 i + 1, in_decl);
6341 error (" expected a template of type %qD, got %qT",
6342 parm, orig_arg);
6343 }
6344
6345 val = error_mark_node;
6346 }
6347 }
6348 }
6349 else
6350 val = orig_arg;
6351 /* We only form one instance of each template specialization.
6352 Therefore, if we use a non-canonical variant (i.e., a
6353 typedef), any future messages referring to the type will use
6354 the typedef, which is confusing if those future uses do not
6355 themselves also use the typedef. */
6356 if (TYPE_P (val))
6357 val = canonicalize_type_argument (val, complain);
6358 }
6359 else
6360 {
6361 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6362
6363 if (invalid_nontype_parm_type_p (t, complain))
6364 return error_mark_node;
6365
6366 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6367 {
6368 if (same_type_p (t, TREE_TYPE (orig_arg)))
6369 val = orig_arg;
6370 else
6371 {
6372 /* Not sure if this is reachable, but it doesn't hurt
6373 to be robust. */
6374 error ("type mismatch in nontype parameter pack");
6375 val = error_mark_node;
6376 }
6377 }
6378 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6379 /* We used to call digest_init here. However, digest_init
6380 will report errors, which we don't want when complain
6381 is zero. More importantly, digest_init will try too
6382 hard to convert things: for example, `0' should not be
6383 converted to pointer type at this point according to
6384 the standard. Accepting this is not merely an
6385 extension, since deciding whether or not these
6386 conversions can occur is part of determining which
6387 function template to call, or whether a given explicit
6388 argument specification is valid. */
6389 val = convert_nontype_argument (t, orig_arg, complain);
6390 else
6391 val = strip_typedefs_expr (orig_arg);
6392
6393 if (val == NULL_TREE)
6394 val = error_mark_node;
6395 else if (val == error_mark_node && (complain & tf_error))
6396 error ("could not convert template argument %qE to %qT", orig_arg, t);
6397
6398 if (TREE_CODE (val) == SCOPE_REF)
6399 {
6400 /* Strip typedefs from the SCOPE_REF. */
6401 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6402 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6403 complain);
6404 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6405 QUALIFIED_NAME_IS_TEMPLATE (val));
6406 }
6407 }
6408
6409 return val;
6410 }
6411
6412 /* Coerces the remaining template arguments in INNER_ARGS (from
6413 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6414 Returns the coerced argument pack. PARM_IDX is the position of this
6415 parameter in the template parameter list. ARGS is the original
6416 template argument list. */
6417 static tree
6418 coerce_template_parameter_pack (tree parms,
6419 int parm_idx,
6420 tree args,
6421 tree inner_args,
6422 int arg_idx,
6423 tree new_args,
6424 int* lost,
6425 tree in_decl,
6426 tsubst_flags_t complain)
6427 {
6428 tree parm = TREE_VEC_ELT (parms, parm_idx);
6429 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6430 tree packed_args;
6431 tree argument_pack;
6432 tree packed_types = NULL_TREE;
6433
6434 if (arg_idx > nargs)
6435 arg_idx = nargs;
6436
6437 packed_args = make_tree_vec (nargs - arg_idx);
6438
6439 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6440 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6441 {
6442 /* When the template parameter is a non-type template
6443 parameter pack whose type uses parameter packs, we need
6444 to look at each of the template arguments
6445 separately. Build a vector of the types for these
6446 non-type template parameters in PACKED_TYPES. */
6447 tree expansion
6448 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6449 packed_types = tsubst_pack_expansion (expansion, args,
6450 complain, in_decl);
6451
6452 if (packed_types == error_mark_node)
6453 return error_mark_node;
6454
6455 /* Check that we have the right number of arguments. */
6456 if (arg_idx < nargs
6457 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6458 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6459 {
6460 int needed_parms
6461 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6462 error ("wrong number of template arguments (%d, should be %d)",
6463 nargs, needed_parms);
6464 return error_mark_node;
6465 }
6466
6467 /* If we aren't able to check the actual arguments now
6468 (because they haven't been expanded yet), we can at least
6469 verify that all of the types used for the non-type
6470 template parameter pack are, in fact, valid for non-type
6471 template parameters. */
6472 if (arg_idx < nargs
6473 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6474 {
6475 int j, len = TREE_VEC_LENGTH (packed_types);
6476 for (j = 0; j < len; ++j)
6477 {
6478 tree t = TREE_VEC_ELT (packed_types, j);
6479 if (invalid_nontype_parm_type_p (t, complain))
6480 return error_mark_node;
6481 }
6482 }
6483 }
6484
6485 /* Convert the remaining arguments, which will be a part of the
6486 parameter pack "parm". */
6487 for (; arg_idx < nargs; ++arg_idx)
6488 {
6489 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6490 tree actual_parm = TREE_VALUE (parm);
6491
6492 if (packed_types && !PACK_EXPANSION_P (arg))
6493 {
6494 /* When we have a vector of types (corresponding to the
6495 non-type template parameter pack that uses parameter
6496 packs in its type, as mention above), and the
6497 argument is not an expansion (which expands to a
6498 currently unknown number of arguments), clone the
6499 parm and give it the next type in PACKED_TYPES. */
6500 actual_parm = copy_node (actual_parm);
6501 TREE_TYPE (actual_parm) =
6502 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6503 }
6504
6505 if (arg == error_mark_node)
6506 {
6507 if (complain & tf_error)
6508 error ("template argument %d is invalid", arg_idx + 1);
6509 }
6510 else
6511 arg = convert_template_argument (actual_parm,
6512 arg, new_args, complain, parm_idx,
6513 in_decl);
6514 if (arg == error_mark_node)
6515 (*lost)++;
6516 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6517 }
6518
6519 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6520 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6521 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6522 else
6523 {
6524 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6525 TREE_TYPE (argument_pack)
6526 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6527 TREE_CONSTANT (argument_pack) = 1;
6528 }
6529
6530 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6531 #ifdef ENABLE_CHECKING
6532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6533 TREE_VEC_LENGTH (packed_args));
6534 #endif
6535 return argument_pack;
6536 }
6537
6538 /* Returns true if the template argument vector ARGS contains
6539 any pack expansions, false otherwise. */
6540
6541 static bool
6542 any_pack_expanson_args_p (tree args)
6543 {
6544 int i;
6545 if (args)
6546 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6547 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6548 return true;
6549 return false;
6550 }
6551
6552 /* Convert all template arguments to their appropriate types, and
6553 return a vector containing the innermost resulting template
6554 arguments. If any error occurs, return error_mark_node. Error and
6555 warning messages are issued under control of COMPLAIN.
6556
6557 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6558 for arguments not specified in ARGS. Otherwise, if
6559 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6560 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6561 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6562 ARGS. */
6563
6564 static tree
6565 coerce_template_parms (tree parms,
6566 tree args,
6567 tree in_decl,
6568 tsubst_flags_t complain,
6569 bool require_all_args,
6570 bool use_default_args)
6571 {
6572 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6573 tree inner_args;
6574 tree new_args;
6575 tree new_inner_args;
6576 int saved_unevaluated_operand;
6577 int saved_inhibit_evaluation_warnings;
6578
6579 /* When used as a boolean value, indicates whether this is a
6580 variadic template parameter list. Since it's an int, we can also
6581 subtract it from nparms to get the number of non-variadic
6582 parameters. */
6583 int variadic_p = 0;
6584 int post_variadic_parms = 0;
6585
6586 if (args == error_mark_node)
6587 return error_mark_node;
6588
6589 nparms = TREE_VEC_LENGTH (parms);
6590
6591 /* Determine if there are any parameter packs. */
6592 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6593 {
6594 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6595 if (variadic_p)
6596 ++post_variadic_parms;
6597 if (template_parameter_pack_p (tparm))
6598 ++variadic_p;
6599 }
6600
6601 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6602 /* If there are no parameters that follow a parameter pack, we need to
6603 expand any argument packs so that we can deduce a parameter pack from
6604 some non-packed args followed by an argument pack, as in variadic85.C.
6605 If there are such parameters, we need to leave argument packs intact
6606 so the arguments are assigned properly. This can happen when dealing
6607 with a nested class inside a partial specialization of a class
6608 template, as in variadic92.C, or when deducing a template parameter pack
6609 from a sub-declarator, as in variadic114.C. */
6610 if (!post_variadic_parms)
6611 inner_args = expand_template_argument_pack (inner_args);
6612
6613 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6614 if ((nargs > nparms && !variadic_p)
6615 || (nargs < nparms - variadic_p
6616 && require_all_args
6617 && !any_pack_expanson_args_p (inner_args)
6618 && (!use_default_args
6619 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6620 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6621 {
6622 if (complain & tf_error)
6623 {
6624 if (variadic_p)
6625 {
6626 nparms -= variadic_p;
6627 error ("wrong number of template arguments "
6628 "(%d, should be %d or more)", nargs, nparms);
6629 }
6630 else
6631 error ("wrong number of template arguments "
6632 "(%d, should be %d)", nargs, nparms);
6633
6634 if (in_decl)
6635 error ("provided for %q+D", in_decl);
6636 }
6637
6638 return error_mark_node;
6639 }
6640
6641 /* We need to evaluate the template arguments, even though this
6642 template-id may be nested within a "sizeof". */
6643 saved_unevaluated_operand = cp_unevaluated_operand;
6644 cp_unevaluated_operand = 0;
6645 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6646 c_inhibit_evaluation_warnings = 0;
6647 new_inner_args = make_tree_vec (nparms);
6648 new_args = add_outermost_template_args (args, new_inner_args);
6649 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6650 {
6651 tree arg;
6652 tree parm;
6653
6654 /* Get the Ith template parameter. */
6655 parm = TREE_VEC_ELT (parms, parm_idx);
6656
6657 if (parm == error_mark_node)
6658 {
6659 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6660 continue;
6661 }
6662
6663 /* Calculate the next argument. */
6664 if (arg_idx < nargs)
6665 arg = TREE_VEC_ELT (inner_args, arg_idx);
6666 else
6667 arg = NULL_TREE;
6668
6669 if (template_parameter_pack_p (TREE_VALUE (parm))
6670 && !(arg && ARGUMENT_PACK_P (arg)))
6671 {
6672 /* All remaining arguments will be placed in the
6673 template parameter pack PARM. */
6674 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6675 inner_args, arg_idx,
6676 new_args, &lost,
6677 in_decl, complain);
6678
6679 /* Store this argument. */
6680 if (arg == error_mark_node)
6681 lost++;
6682 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6683
6684 /* We are done with all of the arguments. */
6685 arg_idx = nargs;
6686
6687 continue;
6688 }
6689 else if (arg)
6690 {
6691 if (PACK_EXPANSION_P (arg))
6692 {
6693 /* We don't know how many args we have yet, just
6694 use the unconverted ones for now. */
6695 new_inner_args = inner_args;
6696 break;
6697 }
6698 }
6699 else if (require_all_args)
6700 {
6701 /* There must be a default arg in this case. */
6702 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6703 complain, in_decl);
6704 /* The position of the first default template argument,
6705 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6706 Record that. */
6707 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6709 }
6710 else
6711 break;
6712
6713 if (arg == error_mark_node)
6714 {
6715 if (complain & tf_error)
6716 error ("template argument %d is invalid", arg_idx + 1);
6717 }
6718 else if (!arg)
6719 /* This only occurs if there was an error in the template
6720 parameter list itself (which we would already have
6721 reported) that we are trying to recover from, e.g., a class
6722 template with a parameter list such as
6723 template<typename..., typename>. */
6724 ++lost;
6725 else
6726 arg = convert_template_argument (TREE_VALUE (parm),
6727 arg, new_args, complain,
6728 parm_idx, in_decl);
6729
6730 if (arg == error_mark_node)
6731 lost++;
6732 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6733 }
6734 cp_unevaluated_operand = saved_unevaluated_operand;
6735 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6736
6737 if (lost)
6738 return error_mark_node;
6739
6740 #ifdef ENABLE_CHECKING
6741 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6742 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6743 TREE_VEC_LENGTH (new_inner_args));
6744 #endif
6745
6746 return new_inner_args;
6747 }
6748
6749 /* Like coerce_template_parms. If PARMS represents all template
6750 parameters levels, this function returns a vector of vectors
6751 representing all the resulting argument levels. Note that in this
6752 case, only the innermost arguments are coerced because the
6753 outermost ones are supposed to have been coerced already.
6754
6755 Otherwise, if PARMS represents only (the innermost) vector of
6756 parameters, this function returns a vector containing just the
6757 innermost resulting arguments. */
6758
6759 static tree
6760 coerce_innermost_template_parms (tree parms,
6761 tree args,
6762 tree in_decl,
6763 tsubst_flags_t complain,
6764 bool require_all_args,
6765 bool use_default_args)
6766 {
6767 int parms_depth = TMPL_PARMS_DEPTH (parms);
6768 int args_depth = TMPL_ARGS_DEPTH (args);
6769 tree coerced_args;
6770
6771 if (parms_depth > 1)
6772 {
6773 coerced_args = make_tree_vec (parms_depth);
6774 tree level;
6775 int cur_depth;
6776
6777 for (level = parms, cur_depth = parms_depth;
6778 parms_depth > 0 && level != NULL_TREE;
6779 level = TREE_CHAIN (level), --cur_depth)
6780 {
6781 tree l;
6782 if (cur_depth == args_depth)
6783 l = coerce_template_parms (TREE_VALUE (level),
6784 args, in_decl, complain,
6785 require_all_args,
6786 use_default_args);
6787 else
6788 l = TMPL_ARGS_LEVEL (args, cur_depth);
6789
6790 if (l == error_mark_node)
6791 return error_mark_node;
6792
6793 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6794 }
6795 }
6796 else
6797 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6798 args, in_decl, complain,
6799 require_all_args,
6800 use_default_args);
6801 return coerced_args;
6802 }
6803
6804 /* Returns 1 if template args OT and NT are equivalent. */
6805
6806 static int
6807 template_args_equal (tree ot, tree nt)
6808 {
6809 if (nt == ot)
6810 return 1;
6811 if (nt == NULL_TREE || ot == NULL_TREE)
6812 return false;
6813
6814 if (TREE_CODE (nt) == TREE_VEC)
6815 /* For member templates */
6816 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6817 else if (PACK_EXPANSION_P (ot))
6818 return (PACK_EXPANSION_P (nt)
6819 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6820 PACK_EXPANSION_PATTERN (nt))
6821 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6822 PACK_EXPANSION_EXTRA_ARGS (nt)));
6823 else if (ARGUMENT_PACK_P (ot))
6824 {
6825 int i, len;
6826 tree opack, npack;
6827
6828 if (!ARGUMENT_PACK_P (nt))
6829 return 0;
6830
6831 opack = ARGUMENT_PACK_ARGS (ot);
6832 npack = ARGUMENT_PACK_ARGS (nt);
6833 len = TREE_VEC_LENGTH (opack);
6834 if (TREE_VEC_LENGTH (npack) != len)
6835 return 0;
6836 for (i = 0; i < len; ++i)
6837 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6838 TREE_VEC_ELT (npack, i)))
6839 return 0;
6840 return 1;
6841 }
6842 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6843 {
6844 /* We get here probably because we are in the middle of substituting
6845 into the pattern of a pack expansion. In that case the
6846 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6847 interested in. So we want to use the initial pack argument for
6848 the comparison. */
6849 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6850 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6851 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6852 return template_args_equal (ot, nt);
6853 }
6854 else if (TYPE_P (nt))
6855 return TYPE_P (ot) && same_type_p (ot, nt);
6856 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6857 return 0;
6858 else
6859 return cp_tree_equal (ot, nt);
6860 }
6861
6862 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6863 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6864 NEWARG_PTR with the offending arguments if they are non-NULL. */
6865
6866 static int
6867 comp_template_args_with_info (tree oldargs, tree newargs,
6868 tree *oldarg_ptr, tree *newarg_ptr)
6869 {
6870 int i;
6871
6872 if (oldargs == newargs)
6873 return 1;
6874
6875 if (!oldargs || !newargs)
6876 return 0;
6877
6878 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6879 return 0;
6880
6881 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6882 {
6883 tree nt = TREE_VEC_ELT (newargs, i);
6884 tree ot = TREE_VEC_ELT (oldargs, i);
6885
6886 if (! template_args_equal (ot, nt))
6887 {
6888 if (oldarg_ptr != NULL)
6889 *oldarg_ptr = ot;
6890 if (newarg_ptr != NULL)
6891 *newarg_ptr = nt;
6892 return 0;
6893 }
6894 }
6895 return 1;
6896 }
6897
6898 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6899 of template arguments. Returns 0 otherwise. */
6900
6901 int
6902 comp_template_args (tree oldargs, tree newargs)
6903 {
6904 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6905 }
6906
6907 static void
6908 add_pending_template (tree d)
6909 {
6910 tree ti = (TYPE_P (d)
6911 ? CLASSTYPE_TEMPLATE_INFO (d)
6912 : DECL_TEMPLATE_INFO (d));
6913 struct pending_template *pt;
6914 int level;
6915
6916 if (TI_PENDING_TEMPLATE_FLAG (ti))
6917 return;
6918
6919 /* We are called both from instantiate_decl, where we've already had a
6920 tinst_level pushed, and instantiate_template, where we haven't.
6921 Compensate. */
6922 level = !current_tinst_level || current_tinst_level->decl != d;
6923
6924 if (level)
6925 push_tinst_level (d);
6926
6927 pt = ggc_alloc_pending_template ();
6928 pt->next = NULL;
6929 pt->tinst = current_tinst_level;
6930 if (last_pending_template)
6931 last_pending_template->next = pt;
6932 else
6933 pending_templates = pt;
6934
6935 last_pending_template = pt;
6936
6937 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6938
6939 if (level)
6940 pop_tinst_level ();
6941 }
6942
6943
6944 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6945 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6946 documentation for TEMPLATE_ID_EXPR. */
6947
6948 tree
6949 lookup_template_function (tree fns, tree arglist)
6950 {
6951 tree type;
6952
6953 if (fns == error_mark_node || arglist == error_mark_node)
6954 return error_mark_node;
6955
6956 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6957
6958 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6959 {
6960 error ("%q#D is not a function template", fns);
6961 return error_mark_node;
6962 }
6963
6964 if (BASELINK_P (fns))
6965 {
6966 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6967 unknown_type_node,
6968 BASELINK_FUNCTIONS (fns),
6969 arglist);
6970 return fns;
6971 }
6972
6973 type = TREE_TYPE (fns);
6974 if (TREE_CODE (fns) == OVERLOAD || !type)
6975 type = unknown_type_node;
6976
6977 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6978 }
6979
6980 /* Within the scope of a template class S<T>, the name S gets bound
6981 (in build_self_reference) to a TYPE_DECL for the class, not a
6982 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6983 or one of its enclosing classes, and that type is a template,
6984 return the associated TEMPLATE_DECL. Otherwise, the original
6985 DECL is returned.
6986
6987 Also handle the case when DECL is a TREE_LIST of ambiguous
6988 injected-class-names from different bases. */
6989
6990 tree
6991 maybe_get_template_decl_from_type_decl (tree decl)
6992 {
6993 if (decl == NULL_TREE)
6994 return decl;
6995
6996 /* DR 176: A lookup that finds an injected-class-name (10.2
6997 [class.member.lookup]) can result in an ambiguity in certain cases
6998 (for example, if it is found in more than one base class). If all of
6999 the injected-class-names that are found refer to specializations of
7000 the same class template, and if the name is followed by a
7001 template-argument-list, the reference refers to the class template
7002 itself and not a specialization thereof, and is not ambiguous. */
7003 if (TREE_CODE (decl) == TREE_LIST)
7004 {
7005 tree t, tmpl = NULL_TREE;
7006 for (t = decl; t; t = TREE_CHAIN (t))
7007 {
7008 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7009 if (!tmpl)
7010 tmpl = elt;
7011 else if (tmpl != elt)
7012 break;
7013 }
7014 if (tmpl && t == NULL_TREE)
7015 return tmpl;
7016 else
7017 return decl;
7018 }
7019
7020 return (decl != NULL_TREE
7021 && DECL_SELF_REFERENCE_P (decl)
7022 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7023 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7024 }
7025
7026 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7027 parameters, find the desired type.
7028
7029 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7030
7031 IN_DECL, if non-NULL, is the template declaration we are trying to
7032 instantiate.
7033
7034 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7035 the class we are looking up.
7036
7037 Issue error and warning messages under control of COMPLAIN.
7038
7039 If the template class is really a local class in a template
7040 function, then the FUNCTION_CONTEXT is the function in which it is
7041 being instantiated.
7042
7043 ??? Note that this function is currently called *twice* for each
7044 template-id: the first time from the parser, while creating the
7045 incomplete type (finish_template_type), and the second type during the
7046 real instantiation (instantiate_template_class). This is surely something
7047 that we want to avoid. It also causes some problems with argument
7048 coercion (see convert_nontype_argument for more information on this). */
7049
7050 static tree
7051 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7052 int entering_scope, tsubst_flags_t complain)
7053 {
7054 tree templ = NULL_TREE, parmlist;
7055 tree t;
7056 void **slot;
7057 spec_entry *entry;
7058 spec_entry elt;
7059 hashval_t hash;
7060
7061 if (TREE_CODE (d1) == IDENTIFIER_NODE)
7062 {
7063 tree value = innermost_non_namespace_value (d1);
7064 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7065 templ = value;
7066 else
7067 {
7068 if (context)
7069 push_decl_namespace (context);
7070 templ = lookup_name (d1);
7071 templ = maybe_get_template_decl_from_type_decl (templ);
7072 if (context)
7073 pop_decl_namespace ();
7074 }
7075 if (templ)
7076 context = DECL_CONTEXT (templ);
7077 }
7078 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7079 {
7080 tree type = TREE_TYPE (d1);
7081
7082 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7083 an implicit typename for the second A. Deal with it. */
7084 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7085 type = TREE_TYPE (type);
7086
7087 if (CLASSTYPE_TEMPLATE_INFO (type))
7088 {
7089 templ = CLASSTYPE_TI_TEMPLATE (type);
7090 d1 = DECL_NAME (templ);
7091 }
7092 }
7093 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7094 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7095 {
7096 templ = TYPE_TI_TEMPLATE (d1);
7097 d1 = DECL_NAME (templ);
7098 }
7099 else if (TREE_CODE (d1) == TEMPLATE_DECL
7100 && DECL_TEMPLATE_RESULT (d1)
7101 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7102 {
7103 templ = d1;
7104 d1 = DECL_NAME (templ);
7105 context = DECL_CONTEXT (templ);
7106 }
7107
7108 /* Issue an error message if we didn't find a template. */
7109 if (! templ)
7110 {
7111 if (complain & tf_error)
7112 error ("%qT is not a template", d1);
7113 return error_mark_node;
7114 }
7115
7116 if (TREE_CODE (templ) != TEMPLATE_DECL
7117 /* Make sure it's a user visible template, if it was named by
7118 the user. */
7119 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7120 && !PRIMARY_TEMPLATE_P (templ)))
7121 {
7122 if (complain & tf_error)
7123 {
7124 error ("non-template type %qT used as a template", d1);
7125 if (in_decl)
7126 error ("for template declaration %q+D", in_decl);
7127 }
7128 return error_mark_node;
7129 }
7130
7131 complain &= ~tf_user;
7132
7133 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7134 {
7135 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7136 template arguments */
7137
7138 tree parm;
7139 tree arglist2;
7140 tree outer;
7141
7142 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7143
7144 /* Consider an example where a template template parameter declared as
7145
7146 template <class T, class U = std::allocator<T> > class TT
7147
7148 The template parameter level of T and U are one level larger than
7149 of TT. To proper process the default argument of U, say when an
7150 instantiation `TT<int>' is seen, we need to build the full
7151 arguments containing {int} as the innermost level. Outer levels,
7152 available when not appearing as default template argument, can be
7153 obtained from the arguments of the enclosing template.
7154
7155 Suppose that TT is later substituted with std::vector. The above
7156 instantiation is `TT<int, std::allocator<T> >' with TT at
7157 level 1, and T at level 2, while the template arguments at level 1
7158 becomes {std::vector} and the inner level 2 is {int}. */
7159
7160 outer = DECL_CONTEXT (templ);
7161 if (outer)
7162 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7163 else if (current_template_parms)
7164 /* This is an argument of the current template, so we haven't set
7165 DECL_CONTEXT yet. */
7166 outer = current_template_args ();
7167
7168 if (outer)
7169 arglist = add_to_template_args (outer, arglist);
7170
7171 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7172 complain,
7173 /*require_all_args=*/true,
7174 /*use_default_args=*/true);
7175 if (arglist2 == error_mark_node
7176 || (!uses_template_parms (arglist2)
7177 && check_instantiated_args (templ, arglist2, complain)))
7178 return error_mark_node;
7179
7180 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7181 return parm;
7182 }
7183 else
7184 {
7185 tree template_type = TREE_TYPE (templ);
7186 tree gen_tmpl;
7187 tree type_decl;
7188 tree found = NULL_TREE;
7189 int arg_depth;
7190 int parm_depth;
7191 int is_dependent_type;
7192 int use_partial_inst_tmpl = false;
7193
7194 if (template_type == error_mark_node)
7195 /* An error occured while building the template TEMPL, and a
7196 diagnostic has most certainly been emitted for that
7197 already. Let's propagate that error. */
7198 return error_mark_node;
7199
7200 gen_tmpl = most_general_template (templ);
7201 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7202 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7203 arg_depth = TMPL_ARGS_DEPTH (arglist);
7204
7205 if (arg_depth == 1 && parm_depth > 1)
7206 {
7207 /* We've been given an incomplete set of template arguments.
7208 For example, given:
7209
7210 template <class T> struct S1 {
7211 template <class U> struct S2 {};
7212 template <class U> struct S2<U*> {};
7213 };
7214
7215 we will be called with an ARGLIST of `U*', but the
7216 TEMPLATE will be `template <class T> template
7217 <class U> struct S1<T>::S2'. We must fill in the missing
7218 arguments. */
7219 arglist
7220 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7221 arglist);
7222 arg_depth = TMPL_ARGS_DEPTH (arglist);
7223 }
7224
7225 /* Now we should have enough arguments. */
7226 gcc_assert (parm_depth == arg_depth);
7227
7228 /* From here on, we're only interested in the most general
7229 template. */
7230
7231 /* Calculate the BOUND_ARGS. These will be the args that are
7232 actually tsubst'd into the definition to create the
7233 instantiation. */
7234 if (parm_depth > 1)
7235 {
7236 /* We have multiple levels of arguments to coerce, at once. */
7237 int i;
7238 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7239
7240 tree bound_args = make_tree_vec (parm_depth);
7241
7242 for (i = saved_depth,
7243 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7244 i > 0 && t != NULL_TREE;
7245 --i, t = TREE_CHAIN (t))
7246 {
7247 tree a;
7248 if (i == saved_depth)
7249 a = coerce_template_parms (TREE_VALUE (t),
7250 arglist, gen_tmpl,
7251 complain,
7252 /*require_all_args=*/true,
7253 /*use_default_args=*/true);
7254 else
7255 /* Outer levels should have already been coerced. */
7256 a = TMPL_ARGS_LEVEL (arglist, i);
7257
7258 /* Don't process further if one of the levels fails. */
7259 if (a == error_mark_node)
7260 {
7261 /* Restore the ARGLIST to its full size. */
7262 TREE_VEC_LENGTH (arglist) = saved_depth;
7263 return error_mark_node;
7264 }
7265
7266 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7267
7268 /* We temporarily reduce the length of the ARGLIST so
7269 that coerce_template_parms will see only the arguments
7270 corresponding to the template parameters it is
7271 examining. */
7272 TREE_VEC_LENGTH (arglist)--;
7273 }
7274
7275 /* Restore the ARGLIST to its full size. */
7276 TREE_VEC_LENGTH (arglist) = saved_depth;
7277
7278 arglist = bound_args;
7279 }
7280 else
7281 arglist
7282 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7283 INNERMOST_TEMPLATE_ARGS (arglist),
7284 gen_tmpl,
7285 complain,
7286 /*require_all_args=*/true,
7287 /*use_default_args=*/true);
7288
7289 if (arglist == error_mark_node)
7290 /* We were unable to bind the arguments. */
7291 return error_mark_node;
7292
7293 /* In the scope of a template class, explicit references to the
7294 template class refer to the type of the template, not any
7295 instantiation of it. For example, in:
7296
7297 template <class T> class C { void f(C<T>); }
7298
7299 the `C<T>' is just the same as `C'. Outside of the
7300 class, however, such a reference is an instantiation. */
7301 if ((entering_scope
7302 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7303 || currently_open_class (template_type))
7304 /* comp_template_args is expensive, check it last. */
7305 && comp_template_args (TYPE_TI_ARGS (template_type),
7306 arglist))
7307 return template_type;
7308
7309 /* If we already have this specialization, return it. */
7310 elt.tmpl = gen_tmpl;
7311 elt.args = arglist;
7312 hash = hash_specialization (&elt);
7313 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7314 &elt, hash);
7315
7316 if (entry)
7317 return entry->spec;
7318
7319 is_dependent_type = uses_template_parms (arglist);
7320
7321 /* If the deduced arguments are invalid, then the binding
7322 failed. */
7323 if (!is_dependent_type
7324 && check_instantiated_args (gen_tmpl,
7325 INNERMOST_TEMPLATE_ARGS (arglist),
7326 complain))
7327 return error_mark_node;
7328
7329 if (!is_dependent_type
7330 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7331 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7332 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7333 {
7334 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7335 DECL_NAME (gen_tmpl),
7336 /*tag_scope=*/ts_global);
7337 return found;
7338 }
7339
7340 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7341 complain, in_decl);
7342 if (context == error_mark_node)
7343 return error_mark_node;
7344
7345 if (!context)
7346 context = global_namespace;
7347
7348 /* Create the type. */
7349 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7350 {
7351 if (!is_dependent_type)
7352 {
7353 set_current_access_from_decl (TYPE_NAME (template_type));
7354 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7355 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7356 arglist, complain, in_decl),
7357 SCOPED_ENUM_P (template_type), NULL);
7358 }
7359 else
7360 {
7361 /* We don't want to call start_enum for this type, since
7362 the values for the enumeration constants may involve
7363 template parameters. And, no one should be interested
7364 in the enumeration constants for such a type. */
7365 t = cxx_make_type (ENUMERAL_TYPE);
7366 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7367 }
7368 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7369 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7370 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7371 }
7372 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7373 {
7374 /* The user referred to a specialization of an alias
7375 template represented by GEN_TMPL.
7376
7377 [temp.alias]/2 says:
7378
7379 When a template-id refers to the specialization of an
7380 alias template, it is equivalent to the associated
7381 type obtained by substitution of its
7382 template-arguments for the template-parameters in the
7383 type-id of the alias template. */
7384
7385 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7386 /* Note that the call above (by indirectly calling
7387 register_specialization in tsubst_decl) registers the
7388 TYPE_DECL representing the specialization of the alias
7389 template. So next time someone substitutes ARGLIST for
7390 the template parms into the alias template (GEN_TMPL),
7391 she'll get that TYPE_DECL back. */
7392
7393 if (t == error_mark_node)
7394 return t;
7395 }
7396 else if (CLASS_TYPE_P (template_type))
7397 {
7398 t = make_class_type (TREE_CODE (template_type));
7399 CLASSTYPE_DECLARED_CLASS (t)
7400 = CLASSTYPE_DECLARED_CLASS (template_type);
7401 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7402 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7403
7404 /* A local class. Make sure the decl gets registered properly. */
7405 if (context == current_function_decl)
7406 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7407
7408 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7409 /* This instantiation is another name for the primary
7410 template type. Set the TYPE_CANONICAL field
7411 appropriately. */
7412 TYPE_CANONICAL (t) = template_type;
7413 else if (any_template_arguments_need_structural_equality_p (arglist))
7414 /* Some of the template arguments require structural
7415 equality testing, so this template class requires
7416 structural equality testing. */
7417 SET_TYPE_STRUCTURAL_EQUALITY (t);
7418 }
7419 else
7420 gcc_unreachable ();
7421
7422 /* If we called start_enum or pushtag above, this information
7423 will already be set up. */
7424 if (!TYPE_NAME (t))
7425 {
7426 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7427
7428 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7429 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7430 DECL_SOURCE_LOCATION (type_decl)
7431 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7432 }
7433 else
7434 type_decl = TYPE_NAME (t);
7435
7436 if (CLASS_TYPE_P (template_type))
7437 {
7438 TREE_PRIVATE (type_decl)
7439 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7440 TREE_PROTECTED (type_decl)
7441 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7442 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7443 {
7444 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7445 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7446 }
7447 }
7448
7449 /* Let's consider the explicit specialization of a member
7450 of a class template specialization that is implicitely instantiated,
7451 e.g.:
7452 template<class T>
7453 struct S
7454 {
7455 template<class U> struct M {}; //#0
7456 };
7457
7458 template<>
7459 template<>
7460 struct S<int>::M<char> //#1
7461 {
7462 int i;
7463 };
7464 [temp.expl.spec]/4 says this is valid.
7465
7466 In this case, when we write:
7467 S<int>::M<char> m;
7468
7469 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7470 the one of #0.
7471
7472 When we encounter #1, we want to store the partial instantiation
7473 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7474
7475 For all cases other than this "explicit specialization of member of a
7476 class template", we just want to store the most general template into
7477 the CLASSTYPE_TI_TEMPLATE of M.
7478
7479 This case of "explicit specialization of member of a class template"
7480 only happens when:
7481 1/ the enclosing class is an instantiation of, and therefore not
7482 the same as, the context of the most general template, and
7483 2/ we aren't looking at the partial instantiation itself, i.e.
7484 the innermost arguments are not the same as the innermost parms of
7485 the most general template.
7486
7487 So it's only when 1/ and 2/ happens that we want to use the partial
7488 instantiation of the member template in lieu of its most general
7489 template. */
7490
7491 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7492 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7493 /* the enclosing class must be an instantiation... */
7494 && CLASS_TYPE_P (context)
7495 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7496 {
7497 tree partial_inst_args;
7498 TREE_VEC_LENGTH (arglist)--;
7499 ++processing_template_decl;
7500 partial_inst_args =
7501 tsubst (INNERMOST_TEMPLATE_ARGS
7502 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7503 arglist, complain, NULL_TREE);
7504 --processing_template_decl;
7505 TREE_VEC_LENGTH (arglist)++;
7506 use_partial_inst_tmpl =
7507 /*...and we must not be looking at the partial instantiation
7508 itself. */
7509 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7510 partial_inst_args);
7511 }
7512
7513 if (!use_partial_inst_tmpl)
7514 /* This case is easy; there are no member templates involved. */
7515 found = gen_tmpl;
7516 else
7517 {
7518 /* This is a full instantiation of a member template. Find
7519 the partial instantiation of which this is an instance. */
7520
7521 /* Temporarily reduce by one the number of levels in the ARGLIST
7522 so as to avoid comparing the last set of arguments. */
7523 TREE_VEC_LENGTH (arglist)--;
7524 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7525 TREE_VEC_LENGTH (arglist)++;
7526 /* FOUND is either a proper class type, or an alias
7527 template specialization. In the later case, it's a
7528 TYPE_DECL, resulting from the substituting of arguments
7529 for parameters in the TYPE_DECL of the alias template
7530 done earlier. So be careful while getting the template
7531 of FOUND. */
7532 found = TREE_CODE (found) == TYPE_DECL
7533 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7534 : CLASSTYPE_TI_TEMPLATE (found);
7535 }
7536
7537 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7538
7539 elt.spec = t;
7540 slot = htab_find_slot_with_hash (type_specializations,
7541 &elt, hash, INSERT);
7542 entry = ggc_alloc_spec_entry ();
7543 *entry = elt;
7544 *slot = entry;
7545
7546 /* Note this use of the partial instantiation so we can check it
7547 later in maybe_process_partial_specialization. */
7548 DECL_TEMPLATE_INSTANTIATIONS (templ)
7549 = tree_cons (arglist, t,
7550 DECL_TEMPLATE_INSTANTIATIONS (templ));
7551
7552 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7553 /* Now that the type has been registered on the instantiations
7554 list, we set up the enumerators. Because the enumeration
7555 constants may involve the enumeration type itself, we make
7556 sure to register the type first, and then create the
7557 constants. That way, doing tsubst_expr for the enumeration
7558 constants won't result in recursive calls here; we'll find
7559 the instantiation and exit above. */
7560 tsubst_enum (template_type, t, arglist);
7561
7562 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7563 /* If the type makes use of template parameters, the
7564 code that generates debugging information will crash. */
7565 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7566
7567 /* Possibly limit visibility based on template args. */
7568 TREE_PUBLIC (type_decl) = 1;
7569 determine_visibility (type_decl);
7570
7571 return t;
7572 }
7573 }
7574
7575 /* Wrapper for lookup_template_class_1. */
7576
7577 tree
7578 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7579 int entering_scope, tsubst_flags_t complain)
7580 {
7581 tree ret;
7582 timevar_push (TV_TEMPLATE_INST);
7583 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7584 entering_scope, complain);
7585 timevar_pop (TV_TEMPLATE_INST);
7586 return ret;
7587 }
7588 \f
7589 struct pair_fn_data
7590 {
7591 tree_fn_t fn;
7592 void *data;
7593 /* True when we should also visit template parameters that occur in
7594 non-deduced contexts. */
7595 bool include_nondeduced_p;
7596 struct pointer_set_t *visited;
7597 };
7598
7599 /* Called from for_each_template_parm via walk_tree. */
7600
7601 static tree
7602 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7603 {
7604 tree t = *tp;
7605 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7606 tree_fn_t fn = pfd->fn;
7607 void *data = pfd->data;
7608
7609 if (TYPE_P (t)
7610 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7611 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7612 pfd->include_nondeduced_p))
7613 return error_mark_node;
7614
7615 switch (TREE_CODE (t))
7616 {
7617 case RECORD_TYPE:
7618 if (TYPE_PTRMEMFUNC_P (t))
7619 break;
7620 /* Fall through. */
7621
7622 case UNION_TYPE:
7623 case ENUMERAL_TYPE:
7624 if (!TYPE_TEMPLATE_INFO (t))
7625 *walk_subtrees = 0;
7626 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7627 fn, data, pfd->visited,
7628 pfd->include_nondeduced_p))
7629 return error_mark_node;
7630 break;
7631
7632 case INTEGER_TYPE:
7633 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7634 fn, data, pfd->visited,
7635 pfd->include_nondeduced_p)
7636 || for_each_template_parm (TYPE_MAX_VALUE (t),
7637 fn, data, pfd->visited,
7638 pfd->include_nondeduced_p))
7639 return error_mark_node;
7640 break;
7641
7642 case METHOD_TYPE:
7643 /* Since we're not going to walk subtrees, we have to do this
7644 explicitly here. */
7645 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7646 pfd->visited, pfd->include_nondeduced_p))
7647 return error_mark_node;
7648 /* Fall through. */
7649
7650 case FUNCTION_TYPE:
7651 /* Check the return type. */
7652 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7653 pfd->include_nondeduced_p))
7654 return error_mark_node;
7655
7656 /* Check the parameter types. Since default arguments are not
7657 instantiated until they are needed, the TYPE_ARG_TYPES may
7658 contain expressions that involve template parameters. But,
7659 no-one should be looking at them yet. And, once they're
7660 instantiated, they don't contain template parameters, so
7661 there's no point in looking at them then, either. */
7662 {
7663 tree parm;
7664
7665 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7666 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7667 pfd->visited, pfd->include_nondeduced_p))
7668 return error_mark_node;
7669
7670 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7671 want walk_tree walking into them itself. */
7672 *walk_subtrees = 0;
7673 }
7674 break;
7675
7676 case TYPEOF_TYPE:
7677 case UNDERLYING_TYPE:
7678 if (pfd->include_nondeduced_p
7679 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7680 pfd->visited,
7681 pfd->include_nondeduced_p))
7682 return error_mark_node;
7683 break;
7684
7685 case FUNCTION_DECL:
7686 case VAR_DECL:
7687 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7688 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7689 pfd->visited, pfd->include_nondeduced_p))
7690 return error_mark_node;
7691 /* Fall through. */
7692
7693 case PARM_DECL:
7694 case CONST_DECL:
7695 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7696 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7697 pfd->visited, pfd->include_nondeduced_p))
7698 return error_mark_node;
7699 if (DECL_CONTEXT (t)
7700 && pfd->include_nondeduced_p
7701 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7702 pfd->visited, pfd->include_nondeduced_p))
7703 return error_mark_node;
7704 break;
7705
7706 case BOUND_TEMPLATE_TEMPLATE_PARM:
7707 /* Record template parameters such as `T' inside `TT<T>'. */
7708 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7709 pfd->include_nondeduced_p))
7710 return error_mark_node;
7711 /* Fall through. */
7712
7713 case TEMPLATE_TEMPLATE_PARM:
7714 case TEMPLATE_TYPE_PARM:
7715 case TEMPLATE_PARM_INDEX:
7716 if (fn && (*fn)(t, data))
7717 return error_mark_node;
7718 else if (!fn)
7719 return error_mark_node;
7720 break;
7721
7722 case TEMPLATE_DECL:
7723 /* A template template parameter is encountered. */
7724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7725 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7726 pfd->include_nondeduced_p))
7727 return error_mark_node;
7728
7729 /* Already substituted template template parameter */
7730 *walk_subtrees = 0;
7731 break;
7732
7733 case TYPENAME_TYPE:
7734 if (!fn
7735 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7736 data, pfd->visited,
7737 pfd->include_nondeduced_p))
7738 return error_mark_node;
7739 break;
7740
7741 case CONSTRUCTOR:
7742 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7743 && pfd->include_nondeduced_p
7744 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7745 (TREE_TYPE (t)), fn, data,
7746 pfd->visited, pfd->include_nondeduced_p))
7747 return error_mark_node;
7748 break;
7749
7750 case INDIRECT_REF:
7751 case COMPONENT_REF:
7752 /* If there's no type, then this thing must be some expression
7753 involving template parameters. */
7754 if (!fn && !TREE_TYPE (t))
7755 return error_mark_node;
7756 break;
7757
7758 case MODOP_EXPR:
7759 case CAST_EXPR:
7760 case IMPLICIT_CONV_EXPR:
7761 case REINTERPRET_CAST_EXPR:
7762 case CONST_CAST_EXPR:
7763 case STATIC_CAST_EXPR:
7764 case DYNAMIC_CAST_EXPR:
7765 case ARROW_EXPR:
7766 case DOTSTAR_EXPR:
7767 case TYPEID_EXPR:
7768 case PSEUDO_DTOR_EXPR:
7769 if (!fn)
7770 return error_mark_node;
7771 break;
7772
7773 default:
7774 break;
7775 }
7776
7777 /* We didn't find any template parameters we liked. */
7778 return NULL_TREE;
7779 }
7780
7781 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7782 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7783 call FN with the parameter and the DATA.
7784 If FN returns nonzero, the iteration is terminated, and
7785 for_each_template_parm returns 1. Otherwise, the iteration
7786 continues. If FN never returns a nonzero value, the value
7787 returned by for_each_template_parm is 0. If FN is NULL, it is
7788 considered to be the function which always returns 1.
7789
7790 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7791 parameters that occur in non-deduced contexts. When false, only
7792 visits those template parameters that can be deduced. */
7793
7794 static int
7795 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7796 struct pointer_set_t *visited,
7797 bool include_nondeduced_p)
7798 {
7799 struct pair_fn_data pfd;
7800 int result;
7801
7802 /* Set up. */
7803 pfd.fn = fn;
7804 pfd.data = data;
7805 pfd.include_nondeduced_p = include_nondeduced_p;
7806
7807 /* Walk the tree. (Conceptually, we would like to walk without
7808 duplicates, but for_each_template_parm_r recursively calls
7809 for_each_template_parm, so we would need to reorganize a fair
7810 bit to use walk_tree_without_duplicates, so we keep our own
7811 visited list.) */
7812 if (visited)
7813 pfd.visited = visited;
7814 else
7815 pfd.visited = pointer_set_create ();
7816 result = cp_walk_tree (&t,
7817 for_each_template_parm_r,
7818 &pfd,
7819 pfd.visited) != NULL_TREE;
7820
7821 /* Clean up. */
7822 if (!visited)
7823 {
7824 pointer_set_destroy (pfd.visited);
7825 pfd.visited = 0;
7826 }
7827
7828 return result;
7829 }
7830
7831 /* Returns true if T depends on any template parameter. */
7832
7833 int
7834 uses_template_parms (tree t)
7835 {
7836 bool dependent_p;
7837 int saved_processing_template_decl;
7838
7839 saved_processing_template_decl = processing_template_decl;
7840 if (!saved_processing_template_decl)
7841 processing_template_decl = 1;
7842 if (TYPE_P (t))
7843 dependent_p = dependent_type_p (t);
7844 else if (TREE_CODE (t) == TREE_VEC)
7845 dependent_p = any_dependent_template_arguments_p (t);
7846 else if (TREE_CODE (t) == TREE_LIST)
7847 dependent_p = (uses_template_parms (TREE_VALUE (t))
7848 || uses_template_parms (TREE_CHAIN (t)));
7849 else if (TREE_CODE (t) == TYPE_DECL)
7850 dependent_p = dependent_type_p (TREE_TYPE (t));
7851 else if (DECL_P (t)
7852 || EXPR_P (t)
7853 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7854 || TREE_CODE (t) == OVERLOAD
7855 || BASELINK_P (t)
7856 || TREE_CODE (t) == IDENTIFIER_NODE
7857 || TREE_CODE (t) == TRAIT_EXPR
7858 || TREE_CODE (t) == CONSTRUCTOR
7859 || CONSTANT_CLASS_P (t))
7860 dependent_p = (type_dependent_expression_p (t)
7861 || value_dependent_expression_p (t));
7862 else
7863 {
7864 gcc_assert (t == error_mark_node);
7865 dependent_p = false;
7866 }
7867
7868 processing_template_decl = saved_processing_template_decl;
7869
7870 return dependent_p;
7871 }
7872
7873 /* Returns true iff current_function_decl is an incompletely instantiated
7874 template. Useful instead of processing_template_decl because the latter
7875 is set to 0 during fold_non_dependent_expr. */
7876
7877 bool
7878 in_template_function (void)
7879 {
7880 tree fn = current_function_decl;
7881 bool ret;
7882 ++processing_template_decl;
7883 ret = (fn && DECL_LANG_SPECIFIC (fn)
7884 && DECL_TEMPLATE_INFO (fn)
7885 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7886 --processing_template_decl;
7887 return ret;
7888 }
7889
7890 /* Returns true if T depends on any template parameter with level LEVEL. */
7891
7892 int
7893 uses_template_parms_level (tree t, int level)
7894 {
7895 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7896 /*include_nondeduced_p=*/true);
7897 }
7898
7899 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7900 ill-formed translation unit, i.e. a variable or function that isn't
7901 usable in a constant expression. */
7902
7903 static inline bool
7904 neglectable_inst_p (tree d)
7905 {
7906 return (DECL_P (d)
7907 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7908 : decl_maybe_constant_var_p (d)));
7909 }
7910
7911 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7912 neglectable and instantiated from within an erroneous instantiation. */
7913
7914 static bool
7915 limit_bad_template_recursion (tree decl)
7916 {
7917 struct tinst_level *lev = current_tinst_level;
7918 int errs = errorcount + sorrycount;
7919 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7920 return false;
7921
7922 for (; lev; lev = lev->next)
7923 if (neglectable_inst_p (lev->decl))
7924 break;
7925
7926 return (lev && errs > lev->errors);
7927 }
7928
7929 static int tinst_depth;
7930 extern int max_tinst_depth;
7931 int depth_reached;
7932
7933 static GTY(()) struct tinst_level *last_error_tinst_level;
7934
7935 /* We're starting to instantiate D; record the template instantiation context
7936 for diagnostics and to restore it later. */
7937
7938 int
7939 push_tinst_level (tree d)
7940 {
7941 struct tinst_level *new_level;
7942
7943 if (tinst_depth >= max_tinst_depth)
7944 {
7945 last_error_tinst_level = current_tinst_level;
7946 if (TREE_CODE (d) == TREE_LIST)
7947 error ("template instantiation depth exceeds maximum of %d (use "
7948 "-ftemplate-depth= to increase the maximum) substituting %qS",
7949 max_tinst_depth, d);
7950 else
7951 error ("template instantiation depth exceeds maximum of %d (use "
7952 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7953 max_tinst_depth, d);
7954
7955 print_instantiation_context ();
7956
7957 return 0;
7958 }
7959
7960 /* If the current instantiation caused problems, don't let it instantiate
7961 anything else. Do allow deduction substitution and decls usable in
7962 constant expressions. */
7963 if (limit_bad_template_recursion (d))
7964 return 0;
7965
7966 new_level = ggc_alloc_tinst_level ();
7967 new_level->decl = d;
7968 new_level->locus = input_location;
7969 new_level->errors = errorcount+sorrycount;
7970 new_level->in_system_header_p = in_system_header;
7971 new_level->next = current_tinst_level;
7972 current_tinst_level = new_level;
7973
7974 ++tinst_depth;
7975 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
7976 depth_reached = tinst_depth;
7977
7978 return 1;
7979 }
7980
7981 /* We're done instantiating this template; return to the instantiation
7982 context. */
7983
7984 void
7985 pop_tinst_level (void)
7986 {
7987 /* Restore the filename and line number stashed away when we started
7988 this instantiation. */
7989 input_location = current_tinst_level->locus;
7990 current_tinst_level = current_tinst_level->next;
7991 --tinst_depth;
7992 }
7993
7994 /* We're instantiating a deferred template; restore the template
7995 instantiation context in which the instantiation was requested, which
7996 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7997
7998 static tree
7999 reopen_tinst_level (struct tinst_level *level)
8000 {
8001 struct tinst_level *t;
8002
8003 tinst_depth = 0;
8004 for (t = level; t; t = t->next)
8005 ++tinst_depth;
8006
8007 current_tinst_level = level;
8008 pop_tinst_level ();
8009 if (current_tinst_level)
8010 current_tinst_level->errors = errorcount+sorrycount;
8011 return level->decl;
8012 }
8013
8014 /* Returns the TINST_LEVEL which gives the original instantiation
8015 context. */
8016
8017 struct tinst_level *
8018 outermost_tinst_level (void)
8019 {
8020 struct tinst_level *level = current_tinst_level;
8021 if (level)
8022 while (level->next)
8023 level = level->next;
8024 return level;
8025 }
8026
8027 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8028 vector of template arguments, as for tsubst.
8029
8030 Returns an appropriate tsubst'd friend declaration. */
8031
8032 static tree
8033 tsubst_friend_function (tree decl, tree args)
8034 {
8035 tree new_friend;
8036
8037 if (TREE_CODE (decl) == FUNCTION_DECL
8038 && DECL_TEMPLATE_INSTANTIATION (decl)
8039 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8040 /* This was a friend declared with an explicit template
8041 argument list, e.g.:
8042
8043 friend void f<>(T);
8044
8045 to indicate that f was a template instantiation, not a new
8046 function declaration. Now, we have to figure out what
8047 instantiation of what template. */
8048 {
8049 tree template_id, arglist, fns;
8050 tree new_args;
8051 tree tmpl;
8052 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8053
8054 /* Friend functions are looked up in the containing namespace scope.
8055 We must enter that scope, to avoid finding member functions of the
8056 current class with same name. */
8057 push_nested_namespace (ns);
8058 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8059 tf_warning_or_error, NULL_TREE,
8060 /*integral_constant_expression_p=*/false);
8061 pop_nested_namespace (ns);
8062 arglist = tsubst (DECL_TI_ARGS (decl), args,
8063 tf_warning_or_error, NULL_TREE);
8064 template_id = lookup_template_function (fns, arglist);
8065
8066 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8067 tmpl = determine_specialization (template_id, new_friend,
8068 &new_args,
8069 /*need_member_template=*/0,
8070 TREE_VEC_LENGTH (args),
8071 tsk_none);
8072 return instantiate_template (tmpl, new_args, tf_error);
8073 }
8074
8075 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8076
8077 /* The NEW_FRIEND will look like an instantiation, to the
8078 compiler, but is not an instantiation from the point of view of
8079 the language. For example, we might have had:
8080
8081 template <class T> struct S {
8082 template <class U> friend void f(T, U);
8083 };
8084
8085 Then, in S<int>, template <class U> void f(int, U) is not an
8086 instantiation of anything. */
8087 if (new_friend == error_mark_node)
8088 return error_mark_node;
8089
8090 DECL_USE_TEMPLATE (new_friend) = 0;
8091 if (TREE_CODE (decl) == TEMPLATE_DECL)
8092 {
8093 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8094 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8095 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8096 }
8097
8098 /* The mangled name for the NEW_FRIEND is incorrect. The function
8099 is not a template instantiation and should not be mangled like
8100 one. Therefore, we forget the mangling here; we'll recompute it
8101 later if we need it. */
8102 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8103 {
8104 SET_DECL_RTL (new_friend, NULL);
8105 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8106 }
8107
8108 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8109 {
8110 tree old_decl;
8111 tree new_friend_template_info;
8112 tree new_friend_result_template_info;
8113 tree ns;
8114 int new_friend_is_defn;
8115
8116 /* We must save some information from NEW_FRIEND before calling
8117 duplicate decls since that function will free NEW_FRIEND if
8118 possible. */
8119 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8120 new_friend_is_defn =
8121 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8122 (template_for_substitution (new_friend)))
8123 != NULL_TREE);
8124 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8125 {
8126 /* This declaration is a `primary' template. */
8127 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8128
8129 new_friend_result_template_info
8130 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8131 }
8132 else
8133 new_friend_result_template_info = NULL_TREE;
8134
8135 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8136 if (new_friend_is_defn)
8137 DECL_INITIAL (new_friend) = error_mark_node;
8138
8139 /* Inside pushdecl_namespace_level, we will push into the
8140 current namespace. However, the friend function should go
8141 into the namespace of the template. */
8142 ns = decl_namespace_context (new_friend);
8143 push_nested_namespace (ns);
8144 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8145 pop_nested_namespace (ns);
8146
8147 if (old_decl == error_mark_node)
8148 return error_mark_node;
8149
8150 if (old_decl != new_friend)
8151 {
8152 /* This new friend declaration matched an existing
8153 declaration. For example, given:
8154
8155 template <class T> void f(T);
8156 template <class U> class C {
8157 template <class T> friend void f(T) {}
8158 };
8159
8160 the friend declaration actually provides the definition
8161 of `f', once C has been instantiated for some type. So,
8162 old_decl will be the out-of-class template declaration,
8163 while new_friend is the in-class definition.
8164
8165 But, if `f' was called before this point, the
8166 instantiation of `f' will have DECL_TI_ARGS corresponding
8167 to `T' but not to `U', references to which might appear
8168 in the definition of `f'. Previously, the most general
8169 template for an instantiation of `f' was the out-of-class
8170 version; now it is the in-class version. Therefore, we
8171 run through all specialization of `f', adding to their
8172 DECL_TI_ARGS appropriately. In particular, they need a
8173 new set of outer arguments, corresponding to the
8174 arguments for this class instantiation.
8175
8176 The same situation can arise with something like this:
8177
8178 friend void f(int);
8179 template <class T> class C {
8180 friend void f(T) {}
8181 };
8182
8183 when `C<int>' is instantiated. Now, `f(int)' is defined
8184 in the class. */
8185
8186 if (!new_friend_is_defn)
8187 /* On the other hand, if the in-class declaration does
8188 *not* provide a definition, then we don't want to alter
8189 existing definitions. We can just leave everything
8190 alone. */
8191 ;
8192 else
8193 {
8194 tree new_template = TI_TEMPLATE (new_friend_template_info);
8195 tree new_args = TI_ARGS (new_friend_template_info);
8196
8197 /* Overwrite whatever template info was there before, if
8198 any, with the new template information pertaining to
8199 the declaration. */
8200 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8201
8202 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8203 {
8204 /* We should have called reregister_specialization in
8205 duplicate_decls. */
8206 gcc_assert (retrieve_specialization (new_template,
8207 new_args, 0)
8208 == old_decl);
8209
8210 /* Instantiate it if the global has already been used. */
8211 if (DECL_ODR_USED (old_decl))
8212 instantiate_decl (old_decl, /*defer_ok=*/true,
8213 /*expl_inst_class_mem_p=*/false);
8214 }
8215 else
8216 {
8217 tree t;
8218
8219 /* Indicate that the old function template is a partial
8220 instantiation. */
8221 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8222 = new_friend_result_template_info;
8223
8224 gcc_assert (new_template
8225 == most_general_template (new_template));
8226 gcc_assert (new_template != old_decl);
8227
8228 /* Reassign any specializations already in the hash table
8229 to the new more general template, and add the
8230 additional template args. */
8231 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8232 t != NULL_TREE;
8233 t = TREE_CHAIN (t))
8234 {
8235 tree spec = TREE_VALUE (t);
8236 spec_entry elt;
8237
8238 elt.tmpl = old_decl;
8239 elt.args = DECL_TI_ARGS (spec);
8240 elt.spec = NULL_TREE;
8241
8242 htab_remove_elt (decl_specializations, &elt);
8243
8244 DECL_TI_ARGS (spec)
8245 = add_outermost_template_args (new_args,
8246 DECL_TI_ARGS (spec));
8247
8248 register_specialization
8249 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8250
8251 }
8252 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8253 }
8254 }
8255
8256 /* The information from NEW_FRIEND has been merged into OLD_DECL
8257 by duplicate_decls. */
8258 new_friend = old_decl;
8259 }
8260 }
8261 else
8262 {
8263 tree context = DECL_CONTEXT (new_friend);
8264 bool dependent_p;
8265
8266 /* In the code
8267 template <class T> class C {
8268 template <class U> friend void C1<U>::f (); // case 1
8269 friend void C2<T>::f (); // case 2
8270 };
8271 we only need to make sure CONTEXT is a complete type for
8272 case 2. To distinguish between the two cases, we note that
8273 CONTEXT of case 1 remains dependent type after tsubst while
8274 this isn't true for case 2. */
8275 ++processing_template_decl;
8276 dependent_p = dependent_type_p (context);
8277 --processing_template_decl;
8278
8279 if (!dependent_p
8280 && !complete_type_or_else (context, NULL_TREE))
8281 return error_mark_node;
8282
8283 if (COMPLETE_TYPE_P (context))
8284 {
8285 /* Check to see that the declaration is really present, and,
8286 possibly obtain an improved declaration. */
8287 tree fn = check_classfn (context,
8288 new_friend, NULL_TREE);
8289
8290 if (fn)
8291 new_friend = fn;
8292 }
8293 }
8294
8295 return new_friend;
8296 }
8297
8298 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8299 template arguments, as for tsubst.
8300
8301 Returns an appropriate tsubst'd friend type or error_mark_node on
8302 failure. */
8303
8304 static tree
8305 tsubst_friend_class (tree friend_tmpl, tree args)
8306 {
8307 tree friend_type;
8308 tree tmpl;
8309 tree context;
8310
8311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8312 {
8313 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8314 return TREE_TYPE (t);
8315 }
8316
8317 context = CP_DECL_CONTEXT (friend_tmpl);
8318
8319 if (context != global_namespace)
8320 {
8321 if (TREE_CODE (context) == NAMESPACE_DECL)
8322 push_nested_namespace (context);
8323 else
8324 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8325 }
8326
8327 /* Look for a class template declaration. We look for hidden names
8328 because two friend declarations of the same template are the
8329 same. For example, in:
8330
8331 struct A {
8332 template <typename> friend class F;
8333 };
8334 template <typename> struct B {
8335 template <typename> friend class F;
8336 };
8337
8338 both F templates are the same. */
8339 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8340 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8341
8342 /* But, if we don't find one, it might be because we're in a
8343 situation like this:
8344
8345 template <class T>
8346 struct S {
8347 template <class U>
8348 friend struct S;
8349 };
8350
8351 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8352 for `S<int>', not the TEMPLATE_DECL. */
8353 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8354 {
8355 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8356 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8357 }
8358
8359 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8360 {
8361 /* The friend template has already been declared. Just
8362 check to see that the declarations match, and install any new
8363 default parameters. We must tsubst the default parameters,
8364 of course. We only need the innermost template parameters
8365 because that is all that redeclare_class_template will look
8366 at. */
8367 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8368 > TMPL_ARGS_DEPTH (args))
8369 {
8370 tree parms;
8371 location_t saved_input_location;
8372 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8373 args, tf_warning_or_error);
8374
8375 saved_input_location = input_location;
8376 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8377 redeclare_class_template (TREE_TYPE (tmpl), parms);
8378 input_location = saved_input_location;
8379
8380 }
8381
8382 friend_type = TREE_TYPE (tmpl);
8383 }
8384 else
8385 {
8386 /* The friend template has not already been declared. In this
8387 case, the instantiation of the template class will cause the
8388 injection of this template into the global scope. */
8389 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8390 if (tmpl == error_mark_node)
8391 return error_mark_node;
8392
8393 /* The new TMPL is not an instantiation of anything, so we
8394 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8395 the new type because that is supposed to be the corresponding
8396 template decl, i.e., TMPL. */
8397 DECL_USE_TEMPLATE (tmpl) = 0;
8398 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8399 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8400 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8401 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8402
8403 /* Inject this template into the global scope. */
8404 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8405 }
8406
8407 if (context != global_namespace)
8408 {
8409 if (TREE_CODE (context) == NAMESPACE_DECL)
8410 pop_nested_namespace (context);
8411 else
8412 pop_nested_class ();
8413 }
8414
8415 return friend_type;
8416 }
8417
8418 /* Returns zero if TYPE cannot be completed later due to circularity.
8419 Otherwise returns one. */
8420
8421 static int
8422 can_complete_type_without_circularity (tree type)
8423 {
8424 if (type == NULL_TREE || type == error_mark_node)
8425 return 0;
8426 else if (COMPLETE_TYPE_P (type))
8427 return 1;
8428 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8429 return can_complete_type_without_circularity (TREE_TYPE (type));
8430 else if (CLASS_TYPE_P (type)
8431 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8432 return 0;
8433 else
8434 return 1;
8435 }
8436
8437 /* Apply any attributes which had to be deferred until instantiation
8438 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8439 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8440
8441 static void
8442 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8443 tree args, tsubst_flags_t complain, tree in_decl)
8444 {
8445 tree last_dep = NULL_TREE;
8446 tree t;
8447 tree *p;
8448
8449 for (t = attributes; t; t = TREE_CHAIN (t))
8450 if (ATTR_IS_DEPENDENT (t))
8451 {
8452 last_dep = t;
8453 attributes = copy_list (attributes);
8454 break;
8455 }
8456
8457 if (DECL_P (*decl_p))
8458 {
8459 if (TREE_TYPE (*decl_p) == error_mark_node)
8460 return;
8461 p = &DECL_ATTRIBUTES (*decl_p);
8462 }
8463 else
8464 p = &TYPE_ATTRIBUTES (*decl_p);
8465
8466 if (last_dep)
8467 {
8468 tree late_attrs = NULL_TREE;
8469 tree *q = &late_attrs;
8470
8471 for (*p = attributes; *p; )
8472 {
8473 t = *p;
8474 if (ATTR_IS_DEPENDENT (t))
8475 {
8476 *p = TREE_CHAIN (t);
8477 TREE_CHAIN (t) = NULL_TREE;
8478 /* If the first attribute argument is an identifier, don't
8479 pass it through tsubst. Attributes like mode, format,
8480 cleanup and several target specific attributes expect it
8481 unmodified. */
8482 if (TREE_VALUE (t)
8483 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8484 && TREE_VALUE (TREE_VALUE (t))
8485 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8486 == IDENTIFIER_NODE))
8487 {
8488 tree chain
8489 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8490 in_decl,
8491 /*integral_constant_expression_p=*/false);
8492 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8493 TREE_VALUE (t)
8494 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8495 chain);
8496 }
8497 else
8498 TREE_VALUE (t)
8499 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8500 /*integral_constant_expression_p=*/false);
8501 *q = t;
8502 q = &TREE_CHAIN (t);
8503 }
8504 else
8505 p = &TREE_CHAIN (t);
8506 }
8507
8508 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8509 }
8510 }
8511
8512 /* Perform (or defer) access check for typedefs that were referenced
8513 from within the template TMPL code.
8514 This is a subroutine of instantiate_decl and instantiate_class_template.
8515 TMPL is the template to consider and TARGS is the list of arguments of
8516 that template. */
8517
8518 static void
8519 perform_typedefs_access_check (tree tmpl, tree targs)
8520 {
8521 location_t saved_location;
8522 unsigned i;
8523 qualified_typedef_usage_t *iter;
8524
8525 if (!tmpl
8526 || (!CLASS_TYPE_P (tmpl)
8527 && TREE_CODE (tmpl) != FUNCTION_DECL))
8528 return;
8529
8530 saved_location = input_location;
8531 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8532 {
8533 tree type_decl = iter->typedef_decl;
8534 tree type_scope = iter->context;
8535
8536 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8537 continue;
8538
8539 if (uses_template_parms (type_decl))
8540 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8541 if (uses_template_parms (type_scope))
8542 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8543
8544 /* Make access check error messages point to the location
8545 of the use of the typedef. */
8546 input_location = iter->locus;
8547 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8548 type_decl, type_decl,
8549 tf_warning_or_error);
8550 }
8551 input_location = saved_location;
8552 }
8553
8554 static tree
8555 instantiate_class_template_1 (tree type)
8556 {
8557 tree templ, args, pattern, t, member;
8558 tree typedecl;
8559 tree pbinfo;
8560 tree base_list;
8561 unsigned int saved_maximum_field_alignment;
8562 tree fn_context;
8563
8564 if (type == error_mark_node)
8565 return error_mark_node;
8566
8567 if (COMPLETE_OR_OPEN_TYPE_P (type)
8568 || uses_template_parms (type))
8569 return type;
8570
8571 /* Figure out which template is being instantiated. */
8572 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8573 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8574
8575 /* Determine what specialization of the original template to
8576 instantiate. */
8577 t = most_specialized_class (type, templ, tf_warning_or_error);
8578 if (t == error_mark_node)
8579 {
8580 TYPE_BEING_DEFINED (type) = 1;
8581 return error_mark_node;
8582 }
8583 else if (t)
8584 {
8585 /* This TYPE is actually an instantiation of a partial
8586 specialization. We replace the innermost set of ARGS with
8587 the arguments appropriate for substitution. For example,
8588 given:
8589
8590 template <class T> struct S {};
8591 template <class T> struct S<T*> {};
8592
8593 and supposing that we are instantiating S<int*>, ARGS will
8594 presently be {int*} -- but we need {int}. */
8595 pattern = TREE_TYPE (t);
8596 args = TREE_PURPOSE (t);
8597 }
8598 else
8599 {
8600 pattern = TREE_TYPE (templ);
8601 args = CLASSTYPE_TI_ARGS (type);
8602 }
8603
8604 /* If the template we're instantiating is incomplete, then clearly
8605 there's nothing we can do. */
8606 if (!COMPLETE_TYPE_P (pattern))
8607 return type;
8608
8609 /* If we've recursively instantiated too many templates, stop. */
8610 if (! push_tinst_level (type))
8611 return type;
8612
8613 /* Now we're really doing the instantiation. Mark the type as in
8614 the process of being defined. */
8615 TYPE_BEING_DEFINED (type) = 1;
8616
8617 /* We may be in the middle of deferred access check. Disable
8618 it now. */
8619 push_deferring_access_checks (dk_no_deferred);
8620
8621 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8622 if (!fn_context)
8623 push_to_top_level ();
8624 /* Use #pragma pack from the template context. */
8625 saved_maximum_field_alignment = maximum_field_alignment;
8626 maximum_field_alignment = TYPE_PRECISION (pattern);
8627
8628 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8629
8630 /* Set the input location to the most specialized template definition.
8631 This is needed if tsubsting causes an error. */
8632 typedecl = TYPE_MAIN_DECL (pattern);
8633 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8634 DECL_SOURCE_LOCATION (typedecl);
8635
8636 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8637 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8638 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8639 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8640 if (ANON_AGGR_TYPE_P (pattern))
8641 SET_ANON_AGGR_TYPE_P (type);
8642 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8643 {
8644 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8645 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8646 /* Adjust visibility for template arguments. */
8647 determine_visibility (TYPE_MAIN_DECL (type));
8648 }
8649 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8650
8651 pbinfo = TYPE_BINFO (pattern);
8652
8653 /* We should never instantiate a nested class before its enclosing
8654 class; we need to look up the nested class by name before we can
8655 instantiate it, and that lookup should instantiate the enclosing
8656 class. */
8657 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8658 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8659
8660 base_list = NULL_TREE;
8661 if (BINFO_N_BASE_BINFOS (pbinfo))
8662 {
8663 tree pbase_binfo;
8664 tree pushed_scope;
8665 int i;
8666
8667 /* We must enter the scope containing the type, as that is where
8668 the accessibility of types named in dependent bases are
8669 looked up from. */
8670 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8671
8672 /* Substitute into each of the bases to determine the actual
8673 basetypes. */
8674 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8675 {
8676 tree base;
8677 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8678 tree expanded_bases = NULL_TREE;
8679 int idx, len = 1;
8680
8681 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8682 {
8683 expanded_bases =
8684 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8685 args, tf_error, NULL_TREE);
8686 if (expanded_bases == error_mark_node)
8687 continue;
8688
8689 len = TREE_VEC_LENGTH (expanded_bases);
8690 }
8691
8692 for (idx = 0; idx < len; idx++)
8693 {
8694 if (expanded_bases)
8695 /* Extract the already-expanded base class. */
8696 base = TREE_VEC_ELT (expanded_bases, idx);
8697 else
8698 /* Substitute to figure out the base class. */
8699 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8700 NULL_TREE);
8701
8702 if (base == error_mark_node)
8703 continue;
8704
8705 base_list = tree_cons (access, base, base_list);
8706 if (BINFO_VIRTUAL_P (pbase_binfo))
8707 TREE_TYPE (base_list) = integer_type_node;
8708 }
8709 }
8710
8711 /* The list is now in reverse order; correct that. */
8712 base_list = nreverse (base_list);
8713
8714 if (pushed_scope)
8715 pop_scope (pushed_scope);
8716 }
8717 /* Now call xref_basetypes to set up all the base-class
8718 information. */
8719 xref_basetypes (type, base_list);
8720
8721 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8722 (int) ATTR_FLAG_TYPE_IN_PLACE,
8723 args, tf_error, NULL_TREE);
8724 fixup_attribute_variants (type);
8725
8726 /* Now that our base classes are set up, enter the scope of the
8727 class, so that name lookups into base classes, etc. will work
8728 correctly. This is precisely analogous to what we do in
8729 begin_class_definition when defining an ordinary non-template
8730 class, except we also need to push the enclosing classes. */
8731 push_nested_class (type);
8732
8733 /* Now members are processed in the order of declaration. */
8734 for (member = CLASSTYPE_DECL_LIST (pattern);
8735 member; member = TREE_CHAIN (member))
8736 {
8737 tree t = TREE_VALUE (member);
8738
8739 if (TREE_PURPOSE (member))
8740 {
8741 if (TYPE_P (t))
8742 {
8743 /* Build new CLASSTYPE_NESTED_UTDS. */
8744
8745 tree newtag;
8746 bool class_template_p;
8747
8748 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8749 && TYPE_LANG_SPECIFIC (t)
8750 && CLASSTYPE_IS_TEMPLATE (t));
8751 /* If the member is a class template, then -- even after
8752 substitution -- there may be dependent types in the
8753 template argument list for the class. We increment
8754 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8755 that function will assume that no types are dependent
8756 when outside of a template. */
8757 if (class_template_p)
8758 ++processing_template_decl;
8759 newtag = tsubst (t, args, tf_error, NULL_TREE);
8760 if (class_template_p)
8761 --processing_template_decl;
8762 if (newtag == error_mark_node)
8763 continue;
8764
8765 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8766 {
8767 tree name = TYPE_IDENTIFIER (t);
8768
8769 if (class_template_p)
8770 /* Unfortunately, lookup_template_class sets
8771 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8772 instantiation (i.e., for the type of a member
8773 template class nested within a template class.)
8774 This behavior is required for
8775 maybe_process_partial_specialization to work
8776 correctly, but is not accurate in this case;
8777 the TAG is not an instantiation of anything.
8778 (The corresponding TEMPLATE_DECL is an
8779 instantiation, but the TYPE is not.) */
8780 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8781
8782 /* Now, we call pushtag to put this NEWTAG into the scope of
8783 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8784 pushtag calling push_template_decl. We don't have to do
8785 this for enums because it will already have been done in
8786 tsubst_enum. */
8787 if (name)
8788 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8789 pushtag (name, newtag, /*tag_scope=*/ts_current);
8790 }
8791 }
8792 else if (TREE_CODE (t) == FUNCTION_DECL
8793 || DECL_FUNCTION_TEMPLATE_P (t))
8794 {
8795 /* Build new TYPE_METHODS. */
8796 tree r;
8797
8798 if (TREE_CODE (t) == TEMPLATE_DECL)
8799 ++processing_template_decl;
8800 r = tsubst (t, args, tf_error, NULL_TREE);
8801 if (TREE_CODE (t) == TEMPLATE_DECL)
8802 --processing_template_decl;
8803 set_current_access_from_decl (r);
8804 finish_member_declaration (r);
8805 /* Instantiate members marked with attribute used. */
8806 if (r != error_mark_node && DECL_PRESERVE_P (r))
8807 mark_used (r);
8808 }
8809 else
8810 {
8811 /* Build new TYPE_FIELDS. */
8812 if (TREE_CODE (t) == STATIC_ASSERT)
8813 {
8814 tree condition;
8815
8816 ++c_inhibit_evaluation_warnings;
8817 condition =
8818 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8819 tf_warning_or_error, NULL_TREE,
8820 /*integral_constant_expression_p=*/true);
8821 --c_inhibit_evaluation_warnings;
8822
8823 finish_static_assert (condition,
8824 STATIC_ASSERT_MESSAGE (t),
8825 STATIC_ASSERT_SOURCE_LOCATION (t),
8826 /*member_p=*/true);
8827 }
8828 else if (TREE_CODE (t) != CONST_DECL)
8829 {
8830 tree r;
8831
8832 /* The file and line for this declaration, to
8833 assist in error message reporting. Since we
8834 called push_tinst_level above, we don't need to
8835 restore these. */
8836 input_location = DECL_SOURCE_LOCATION (t);
8837
8838 if (TREE_CODE (t) == TEMPLATE_DECL)
8839 ++processing_template_decl;
8840 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8841 if (TREE_CODE (t) == TEMPLATE_DECL)
8842 --processing_template_decl;
8843 if (TREE_CODE (r) == VAR_DECL)
8844 {
8845 /* In [temp.inst]:
8846
8847 [t]he initialization (and any associated
8848 side-effects) of a static data member does
8849 not occur unless the static data member is
8850 itself used in a way that requires the
8851 definition of the static data member to
8852 exist.
8853
8854 Therefore, we do not substitute into the
8855 initialized for the static data member here. */
8856 finish_static_data_member_decl
8857 (r,
8858 /*init=*/NULL_TREE,
8859 /*init_const_expr_p=*/false,
8860 /*asmspec_tree=*/NULL_TREE,
8861 /*flags=*/0);
8862 /* Instantiate members marked with attribute used. */
8863 if (r != error_mark_node && DECL_PRESERVE_P (r))
8864 mark_used (r);
8865 }
8866 else if (TREE_CODE (r) == FIELD_DECL)
8867 {
8868 /* Determine whether R has a valid type and can be
8869 completed later. If R is invalid, then it is
8870 replaced by error_mark_node so that it will not be
8871 added to TYPE_FIELDS. */
8872 tree rtype = TREE_TYPE (r);
8873 if (can_complete_type_without_circularity (rtype))
8874 complete_type (rtype);
8875
8876 if (!COMPLETE_TYPE_P (rtype))
8877 {
8878 cxx_incomplete_type_error (r, rtype);
8879 r = error_mark_node;
8880 }
8881 }
8882
8883 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8884 such a thing will already have been added to the field
8885 list by tsubst_enum in finish_member_declaration in the
8886 CLASSTYPE_NESTED_UTDS case above. */
8887 if (!(TREE_CODE (r) == TYPE_DECL
8888 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8889 && DECL_ARTIFICIAL (r)))
8890 {
8891 set_current_access_from_decl (r);
8892 finish_member_declaration (r);
8893 }
8894 }
8895 }
8896 }
8897 else
8898 {
8899 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
8900 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8901 {
8902 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8903
8904 tree friend_type = t;
8905 bool adjust_processing_template_decl = false;
8906
8907 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8908 {
8909 /* template <class T> friend class C; */
8910 friend_type = tsubst_friend_class (friend_type, args);
8911 adjust_processing_template_decl = true;
8912 }
8913 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8914 {
8915 /* template <class T> friend class C::D; */
8916 friend_type = tsubst (friend_type, args,
8917 tf_warning_or_error, NULL_TREE);
8918 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8919 friend_type = TREE_TYPE (friend_type);
8920 adjust_processing_template_decl = true;
8921 }
8922 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8923 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8924 {
8925 /* This could be either
8926
8927 friend class T::C;
8928
8929 when dependent_type_p is false or
8930
8931 template <class U> friend class T::C;
8932
8933 otherwise. */
8934 friend_type = tsubst (friend_type, args,
8935 tf_warning_or_error, NULL_TREE);
8936 /* Bump processing_template_decl for correct
8937 dependent_type_p calculation. */
8938 ++processing_template_decl;
8939 if (dependent_type_p (friend_type))
8940 adjust_processing_template_decl = true;
8941 --processing_template_decl;
8942 }
8943 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8944 && hidden_name_p (TYPE_NAME (friend_type)))
8945 {
8946 /* friend class C;
8947
8948 where C hasn't been declared yet. Let's lookup name
8949 from namespace scope directly, bypassing any name that
8950 come from dependent base class. */
8951 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8952
8953 /* The call to xref_tag_from_type does injection for friend
8954 classes. */
8955 push_nested_namespace (ns);
8956 friend_type =
8957 xref_tag_from_type (friend_type, NULL_TREE,
8958 /*tag_scope=*/ts_current);
8959 pop_nested_namespace (ns);
8960 }
8961 else if (uses_template_parms (friend_type))
8962 /* friend class C<T>; */
8963 friend_type = tsubst (friend_type, args,
8964 tf_warning_or_error, NULL_TREE);
8965 /* Otherwise it's
8966
8967 friend class C;
8968
8969 where C is already declared or
8970
8971 friend class C<int>;
8972
8973 We don't have to do anything in these cases. */
8974
8975 if (adjust_processing_template_decl)
8976 /* Trick make_friend_class into realizing that the friend
8977 we're adding is a template, not an ordinary class. It's
8978 important that we use make_friend_class since it will
8979 perform some error-checking and output cross-reference
8980 information. */
8981 ++processing_template_decl;
8982
8983 if (friend_type != error_mark_node)
8984 make_friend_class (type, friend_type, /*complain=*/false);
8985
8986 if (adjust_processing_template_decl)
8987 --processing_template_decl;
8988 }
8989 else
8990 {
8991 /* Build new DECL_FRIENDLIST. */
8992 tree r;
8993
8994 /* The file and line for this declaration, to
8995 assist in error message reporting. Since we
8996 called push_tinst_level above, we don't need to
8997 restore these. */
8998 input_location = DECL_SOURCE_LOCATION (t);
8999
9000 if (TREE_CODE (t) == TEMPLATE_DECL)
9001 {
9002 ++processing_template_decl;
9003 push_deferring_access_checks (dk_no_check);
9004 }
9005
9006 r = tsubst_friend_function (t, args);
9007 add_friend (type, r, /*complain=*/false);
9008 if (TREE_CODE (t) == TEMPLATE_DECL)
9009 {
9010 pop_deferring_access_checks ();
9011 --processing_template_decl;
9012 }
9013 }
9014 }
9015 }
9016
9017 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9018 {
9019 tree decl = lambda_function (type);
9020 if (decl)
9021 {
9022 instantiate_decl (decl, false, false);
9023
9024 /* We need to instantiate the capture list from the template
9025 after we've instantiated the closure members, but before we
9026 consider adding the conversion op. Also keep any captures
9027 that may have been added during instantiation of the op(). */
9028 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9029 tree tmpl_cap
9030 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9031 args, tf_warning_or_error, NULL_TREE,
9032 false, false);
9033
9034 LAMBDA_EXPR_CAPTURE_LIST (expr)
9035 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9036
9037 maybe_add_lambda_conv_op (type);
9038 }
9039 else
9040 gcc_assert (errorcount);
9041 }
9042
9043 /* Set the file and line number information to whatever is given for
9044 the class itself. This puts error messages involving generated
9045 implicit functions at a predictable point, and the same point
9046 that would be used for non-template classes. */
9047 input_location = DECL_SOURCE_LOCATION (typedecl);
9048
9049 unreverse_member_declarations (type);
9050 finish_struct_1 (type);
9051 TYPE_BEING_DEFINED (type) = 0;
9052
9053 /* We don't instantiate default arguments for member functions. 14.7.1:
9054
9055 The implicit instantiation of a class template specialization causes
9056 the implicit instantiation of the declarations, but not of the
9057 definitions or default arguments, of the class member functions,
9058 member classes, static data members and member templates.... */
9059
9060 /* Some typedefs referenced from within the template code need to be access
9061 checked at template instantiation time, i.e now. These types were
9062 added to the template at parsing time. Let's get those and perform
9063 the access checks then. */
9064 perform_typedefs_access_check (pattern, args);
9065 perform_deferred_access_checks (tf_warning_or_error);
9066 pop_nested_class ();
9067 maximum_field_alignment = saved_maximum_field_alignment;
9068 if (!fn_context)
9069 pop_from_top_level ();
9070 pop_deferring_access_checks ();
9071 pop_tinst_level ();
9072
9073 /* The vtable for a template class can be emitted in any translation
9074 unit in which the class is instantiated. When there is no key
9075 method, however, finish_struct_1 will already have added TYPE to
9076 the keyed_classes list. */
9077 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9078 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9079
9080 return type;
9081 }
9082
9083 /* Wrapper for instantiate_class_template_1. */
9084
9085 tree
9086 instantiate_class_template (tree type)
9087 {
9088 tree ret;
9089 timevar_push (TV_TEMPLATE_INST);
9090 ret = instantiate_class_template_1 (type);
9091 timevar_pop (TV_TEMPLATE_INST);
9092 return ret;
9093 }
9094
9095 static tree
9096 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9097 {
9098 tree r;
9099
9100 if (!t)
9101 r = t;
9102 else if (TYPE_P (t))
9103 r = tsubst (t, args, complain, in_decl);
9104 else
9105 {
9106 if (!(complain & tf_warning))
9107 ++c_inhibit_evaluation_warnings;
9108 r = tsubst_expr (t, args, complain, in_decl,
9109 /*integral_constant_expression_p=*/true);
9110 if (!(complain & tf_warning))
9111 --c_inhibit_evaluation_warnings;
9112 /* Preserve the raw-reference nature of T. */
9113 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9114 && REFERENCE_REF_P (r))
9115 r = TREE_OPERAND (r, 0);
9116 }
9117 return r;
9118 }
9119
9120 /* Given a function parameter pack TMPL_PARM and some function parameters
9121 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9122 and set *SPEC_P to point at the next point in the list. */
9123
9124 static tree
9125 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9126 {
9127 /* Collect all of the extra "packed" parameters into an
9128 argument pack. */
9129 tree parmvec;
9130 tree parmtypevec;
9131 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9132 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9133 tree spec_parm = *spec_p;
9134 int i, len;
9135
9136 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9137 if (tmpl_parm
9138 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9139 break;
9140
9141 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9142 parmvec = make_tree_vec (len);
9143 parmtypevec = make_tree_vec (len);
9144 spec_parm = *spec_p;
9145 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9146 {
9147 TREE_VEC_ELT (parmvec, i) = spec_parm;
9148 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9149 }
9150
9151 /* Build the argument packs. */
9152 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9153 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9154 TREE_TYPE (argpack) = argtypepack;
9155 *spec_p = spec_parm;
9156
9157 return argpack;
9158 }
9159
9160 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9161 NONTYPE_ARGUMENT_PACK. */
9162
9163 static tree
9164 make_fnparm_pack (tree spec_parm)
9165 {
9166 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9167 }
9168
9169 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9170 pack expansion. */
9171
9172 static bool
9173 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9174 {
9175 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9176 if (i >= TREE_VEC_LENGTH (vec))
9177 return false;
9178 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9179 }
9180
9181
9182 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9183
9184 static tree
9185 make_argument_pack_select (tree arg_pack, unsigned index)
9186 {
9187 tree aps = make_node (ARGUMENT_PACK_SELECT);
9188
9189 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9190 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9191
9192 return aps;
9193 }
9194
9195 /* This is a subroutine of tsubst_pack_expansion.
9196
9197 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9198 mechanism to store the (non complete list of) arguments of the
9199 substitution and return a non substituted pack expansion, in order
9200 to wait for when we have enough arguments to really perform the
9201 substitution. */
9202
9203 static bool
9204 use_pack_expansion_extra_args_p (tree parm_packs,
9205 int arg_pack_len,
9206 bool has_empty_arg)
9207 {
9208 if (parm_packs == NULL_TREE)
9209 return false;
9210
9211 bool has_expansion_arg = false;
9212 for (int i = 0 ; i < arg_pack_len; ++i)
9213 {
9214 bool has_non_expansion_arg = false;
9215 for (tree parm_pack = parm_packs;
9216 parm_pack;
9217 parm_pack = TREE_CHAIN (parm_pack))
9218 {
9219 tree arg = TREE_VALUE (parm_pack);
9220
9221 if (argument_pack_element_is_expansion_p (arg, i))
9222 has_expansion_arg = true;
9223 else
9224 has_non_expansion_arg = true;
9225 }
9226
9227 /* If one pack has an expansion and another pack has a normal
9228 argument or if one pack has an empty argument another one
9229 hasn't then tsubst_pack_expansion cannot perform the
9230 substitution and need to fall back on the
9231 PACK_EXPANSION_EXTRA mechanism. */
9232 if ((has_expansion_arg && has_non_expansion_arg)
9233 || (has_empty_arg && (has_expansion_arg || has_non_expansion_arg)))
9234 return true;
9235 }
9236 return false;
9237 }
9238
9239 /* [temp.variadic]/6 says that:
9240
9241 The instantiation of a pack expansion [...]
9242 produces a list E1,E2, ..., En, where N is the number of elements
9243 in the pack expansion parameters.
9244
9245 This subroutine of tsubst_pack_expansion produces one of these Ei.
9246
9247 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9248 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9249 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9250 INDEX is the index 'i' of the element Ei to produce. ARGS,
9251 COMPLAIN, and IN_DECL are the same parameters as for the
9252 tsubst_pack_expansion function.
9253
9254 The function returns the resulting Ei upon successful completion,
9255 or error_mark_node.
9256
9257 Note that this function possibly modifies the ARGS parameter, so
9258 it's the responsibility of the caller to restore it. */
9259
9260 static tree
9261 gen_elem_of_pack_expansion_instantiation (tree pattern,
9262 tree parm_packs,
9263 unsigned index,
9264 tree args /* This parm gets
9265 modified. */,
9266 tsubst_flags_t complain,
9267 tree in_decl)
9268 {
9269 tree t;
9270 bool ith_elem_is_expansion = false;
9271
9272 /* For each parameter pack, change the substitution of the parameter
9273 pack to the ith argument in its argument pack, then expand the
9274 pattern. */
9275 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9276 {
9277 tree parm = TREE_PURPOSE (pack);
9278 tree arg_pack = TREE_VALUE (pack);
9279 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9280
9281 ith_elem_is_expansion |=
9282 argument_pack_element_is_expansion_p (arg_pack, index);
9283
9284 /* Select the Ith argument from the pack. */
9285 if (TREE_CODE (parm) == PARM_DECL)
9286 {
9287 if (index == 0)
9288 {
9289 aps = make_argument_pack_select (arg_pack, index);
9290 mark_used (parm);
9291 register_local_specialization (aps, parm);
9292 }
9293 else
9294 aps = retrieve_local_specialization (parm);
9295 }
9296 else
9297 {
9298 int idx, level;
9299 template_parm_level_and_index (parm, &level, &idx);
9300
9301 if (index == 0)
9302 {
9303 aps = make_argument_pack_select (arg_pack, index);
9304 /* Update the corresponding argument. */
9305 TMPL_ARG (args, level, idx) = aps;
9306 }
9307 else
9308 /* Re-use the ARGUMENT_PACK_SELECT. */
9309 aps = TMPL_ARG (args, level, idx);
9310 }
9311 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9312 }
9313
9314 /* Substitute into the PATTERN with the (possibly altered)
9315 arguments. */
9316 if (!TYPE_P (pattern))
9317 t = tsubst_expr (pattern, args, complain, in_decl,
9318 /*integral_constant_expression_p=*/false);
9319 else
9320 t = tsubst (pattern, args, complain, in_decl);
9321
9322 /* If the Ith argument pack element is a pack expansion, then
9323 the Ith element resulting from the substituting is going to
9324 be a pack expansion as well. */
9325 if (ith_elem_is_expansion)
9326 t = make_pack_expansion (t);
9327
9328 return t;
9329 }
9330
9331 /* Substitute ARGS into T, which is an pack expansion
9332 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9333 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9334 (if only a partial substitution could be performed) or
9335 ERROR_MARK_NODE if there was an error. */
9336 tree
9337 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9338 tree in_decl)
9339 {
9340 tree pattern;
9341 tree pack, packs = NULL_TREE;
9342 bool unsubstituted_packs = false;
9343 int i, len = -1;
9344 tree result;
9345 struct pointer_map_t *saved_local_specializations = NULL;
9346 bool need_local_specializations = false;
9347 int levels;
9348
9349 gcc_assert (PACK_EXPANSION_P (t));
9350 pattern = PACK_EXPANSION_PATTERN (t);
9351
9352 /* Add in any args remembered from an earlier partial instantiation. */
9353 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9354
9355 levels = TMPL_ARGS_DEPTH (args);
9356
9357 /* Determine the argument packs that will instantiate the parameter
9358 packs used in the expansion expression. While we're at it,
9359 compute the number of arguments to be expanded and make sure it
9360 is consistent. */
9361 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9362 pack = TREE_CHAIN (pack))
9363 {
9364 tree parm_pack = TREE_VALUE (pack);
9365 tree arg_pack = NULL_TREE;
9366 tree orig_arg = NULL_TREE;
9367 int level = 0;
9368
9369 if (TREE_CODE (parm_pack) == BASES)
9370 {
9371 if (BASES_DIRECT (parm_pack))
9372 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9373 args, complain, in_decl, false));
9374 else
9375 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9376 args, complain, in_decl, false));
9377 }
9378 if (TREE_CODE (parm_pack) == PARM_DECL)
9379 {
9380 if (PACK_EXPANSION_LOCAL_P (t))
9381 arg_pack = retrieve_local_specialization (parm_pack);
9382 else
9383 {
9384 /* We can't rely on local_specializations for a parameter
9385 name used later in a function declaration (such as in a
9386 late-specified return type). Even if it exists, it might
9387 have the wrong value for a recursive call. Just make a
9388 dummy decl, since it's only used for its type. */
9389 arg_pack = tsubst_decl (parm_pack, args, complain);
9390 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9391 /* Partial instantiation of the parm_pack, we can't build
9392 up an argument pack yet. */
9393 arg_pack = NULL_TREE;
9394 else
9395 arg_pack = make_fnparm_pack (arg_pack);
9396 need_local_specializations = true;
9397 }
9398 }
9399 else
9400 {
9401 int idx;
9402 template_parm_level_and_index (parm_pack, &level, &idx);
9403
9404 if (level <= levels)
9405 arg_pack = TMPL_ARG (args, level, idx);
9406 }
9407
9408 orig_arg = arg_pack;
9409 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9410 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9411
9412 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9413 /* This can only happen if we forget to expand an argument
9414 pack somewhere else. Just return an error, silently. */
9415 {
9416 result = make_tree_vec (1);
9417 TREE_VEC_ELT (result, 0) = error_mark_node;
9418 return result;
9419 }
9420
9421 if (arg_pack)
9422 {
9423 int my_len =
9424 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9425
9426 /* Don't bother trying to do a partial substitution with
9427 incomplete packs; we'll try again after deduction. */
9428 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9429 return t;
9430
9431 if (len < 0)
9432 len = my_len;
9433 else if (len != my_len)
9434 {
9435 if (!(complain & tf_error))
9436 /* Fail quietly. */;
9437 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9438 error ("mismatched argument pack lengths while expanding "
9439 "%<%T%>",
9440 pattern);
9441 else
9442 error ("mismatched argument pack lengths while expanding "
9443 "%<%E%>",
9444 pattern);
9445 return error_mark_node;
9446 }
9447
9448 /* Keep track of the parameter packs and their corresponding
9449 argument packs. */
9450 packs = tree_cons (parm_pack, arg_pack, packs);
9451 TREE_TYPE (packs) = orig_arg;
9452 }
9453 else
9454 {
9455 /* We can't substitute for this parameter pack. We use a flag as
9456 well as the missing_level counter because function parameter
9457 packs don't have a level. */
9458 unsubstituted_packs = true;
9459 }
9460 }
9461
9462 /* We cannot expand this expansion expression, because we don't have
9463 all of the argument packs we need. */
9464 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9465 {
9466 /* We got some full packs, but we can't substitute them in until we
9467 have values for all the packs. So remember these until then. */
9468
9469 t = make_pack_expansion (pattern);
9470 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9471 return t;
9472 }
9473 else if (unsubstituted_packs)
9474 {
9475 /* There were no real arguments, we're just replacing a parameter
9476 pack with another version of itself. Substitute into the
9477 pattern and return a PACK_EXPANSION_*. The caller will need to
9478 deal with that. */
9479 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9480 t = tsubst_expr (pattern, args, complain, in_decl,
9481 /*integral_constant_expression_p=*/false);
9482 else
9483 t = tsubst (pattern, args, complain, in_decl);
9484 t = make_pack_expansion (t);
9485 return t;
9486 }
9487
9488 gcc_assert (len >= 0);
9489
9490 if (need_local_specializations)
9491 {
9492 /* We're in a late-specified return type, so create our own local
9493 specializations map; the current map is either NULL or (in the
9494 case of recursive unification) might have bindings that we don't
9495 want to use or alter. */
9496 saved_local_specializations = local_specializations;
9497 local_specializations = pointer_map_create ();
9498 }
9499
9500 /* For each argument in each argument pack, substitute into the
9501 pattern. */
9502 result = make_tree_vec (len);
9503 for (i = 0; i < len; ++i)
9504 {
9505 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9506 i,
9507 args, complain,
9508 in_decl);
9509 TREE_VEC_ELT (result, i) = t;
9510 if (t == error_mark_node)
9511 {
9512 result = error_mark_node;
9513 break;
9514 }
9515 }
9516
9517 /* Update ARGS to restore the substitution from parameter packs to
9518 their argument packs. */
9519 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9520 {
9521 tree parm = TREE_PURPOSE (pack);
9522
9523 if (TREE_CODE (parm) == PARM_DECL)
9524 register_local_specialization (TREE_TYPE (pack), parm);
9525 else
9526 {
9527 int idx, level;
9528
9529 if (TREE_VALUE (pack) == NULL_TREE)
9530 continue;
9531
9532 template_parm_level_and_index (parm, &level, &idx);
9533
9534 /* Update the corresponding argument. */
9535 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9536 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9537 TREE_TYPE (pack);
9538 else
9539 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9540 }
9541 }
9542
9543 if (need_local_specializations)
9544 {
9545 pointer_map_destroy (local_specializations);
9546 local_specializations = saved_local_specializations;
9547 }
9548
9549 return result;
9550 }
9551
9552 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9553 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9554 parameter packs; all parms generated from a function parameter pack will
9555 have the same DECL_PARM_INDEX. */
9556
9557 tree
9558 get_pattern_parm (tree parm, tree tmpl)
9559 {
9560 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9561 tree patparm;
9562
9563 if (DECL_ARTIFICIAL (parm))
9564 {
9565 for (patparm = DECL_ARGUMENTS (pattern);
9566 patparm; patparm = DECL_CHAIN (patparm))
9567 if (DECL_ARTIFICIAL (patparm)
9568 && DECL_NAME (parm) == DECL_NAME (patparm))
9569 break;
9570 }
9571 else
9572 {
9573 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9574 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9575 gcc_assert (DECL_PARM_INDEX (patparm)
9576 == DECL_PARM_INDEX (parm));
9577 }
9578
9579 return patparm;
9580 }
9581
9582 /* Substitute ARGS into the vector or list of template arguments T. */
9583
9584 static tree
9585 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9586 {
9587 tree orig_t = t;
9588 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9589 tree *elts;
9590
9591 if (t == error_mark_node)
9592 return error_mark_node;
9593
9594 len = TREE_VEC_LENGTH (t);
9595 elts = XALLOCAVEC (tree, len);
9596
9597 for (i = 0; i < len; i++)
9598 {
9599 tree orig_arg = TREE_VEC_ELT (t, i);
9600 tree new_arg;
9601
9602 if (TREE_CODE (orig_arg) == TREE_VEC)
9603 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9604 else if (PACK_EXPANSION_P (orig_arg))
9605 {
9606 /* Substitute into an expansion expression. */
9607 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9608
9609 if (TREE_CODE (new_arg) == TREE_VEC)
9610 /* Add to the expanded length adjustment the number of
9611 expanded arguments. We subtract one from this
9612 measurement, because the argument pack expression
9613 itself is already counted as 1 in
9614 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9615 the argument pack is empty. */
9616 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9617 }
9618 else if (ARGUMENT_PACK_P (orig_arg))
9619 {
9620 /* Substitute into each of the arguments. */
9621 new_arg = TYPE_P (orig_arg)
9622 ? cxx_make_type (TREE_CODE (orig_arg))
9623 : make_node (TREE_CODE (orig_arg));
9624
9625 SET_ARGUMENT_PACK_ARGS (
9626 new_arg,
9627 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9628 args, complain, in_decl));
9629
9630 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9631 new_arg = error_mark_node;
9632
9633 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9634 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9635 complain, in_decl);
9636 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9637
9638 if (TREE_TYPE (new_arg) == error_mark_node)
9639 new_arg = error_mark_node;
9640 }
9641 }
9642 else
9643 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9644
9645 if (new_arg == error_mark_node)
9646 return error_mark_node;
9647
9648 elts[i] = new_arg;
9649 if (new_arg != orig_arg)
9650 need_new = 1;
9651 }
9652
9653 if (!need_new)
9654 return t;
9655
9656 /* Make space for the expanded arguments coming from template
9657 argument packs. */
9658 t = make_tree_vec (len + expanded_len_adjust);
9659 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9660 arguments for a member template.
9661 In that case each TREE_VEC in ORIG_T represents a level of template
9662 arguments, and ORIG_T won't carry any non defaulted argument count.
9663 It will rather be the nested TREE_VECs that will carry one.
9664 In other words, ORIG_T carries a non defaulted argument count only
9665 if it doesn't contain any nested TREE_VEC. */
9666 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9667 {
9668 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9669 count += expanded_len_adjust;
9670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9671 }
9672 for (i = 0, out = 0; i < len; i++)
9673 {
9674 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9675 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9676 && TREE_CODE (elts[i]) == TREE_VEC)
9677 {
9678 int idx;
9679
9680 /* Now expand the template argument pack "in place". */
9681 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9682 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9683 }
9684 else
9685 {
9686 TREE_VEC_ELT (t, out) = elts[i];
9687 out++;
9688 }
9689 }
9690
9691 return t;
9692 }
9693
9694 /* Return the result of substituting ARGS into the template parameters
9695 given by PARMS. If there are m levels of ARGS and m + n levels of
9696 PARMS, then the result will contain n levels of PARMS. For
9697 example, if PARMS is `template <class T> template <class U>
9698 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9699 result will be `template <int*, double, class V>'. */
9700
9701 static tree
9702 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9703 {
9704 tree r = NULL_TREE;
9705 tree* new_parms;
9706
9707 /* When substituting into a template, we must set
9708 PROCESSING_TEMPLATE_DECL as the template parameters may be
9709 dependent if they are based on one-another, and the dependency
9710 predicates are short-circuit outside of templates. */
9711 ++processing_template_decl;
9712
9713 for (new_parms = &r;
9714 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9715 new_parms = &(TREE_CHAIN (*new_parms)),
9716 parms = TREE_CHAIN (parms))
9717 {
9718 tree new_vec =
9719 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9720 int i;
9721
9722 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9723 {
9724 tree tuple;
9725
9726 if (parms == error_mark_node)
9727 continue;
9728
9729 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9730
9731 if (tuple == error_mark_node)
9732 continue;
9733
9734 TREE_VEC_ELT (new_vec, i) =
9735 tsubst_template_parm (tuple, args, complain);
9736 }
9737
9738 *new_parms =
9739 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9740 - TMPL_ARGS_DEPTH (args)),
9741 new_vec, NULL_TREE);
9742 }
9743
9744 --processing_template_decl;
9745
9746 return r;
9747 }
9748
9749 /* Return the result of substituting ARGS into one template parameter
9750 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9751 parameter and which TREE_PURPOSE is the default argument of the
9752 template parameter. */
9753
9754 static tree
9755 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9756 {
9757 tree default_value, parm_decl;
9758
9759 if (args == NULL_TREE
9760 || t == NULL_TREE
9761 || t == error_mark_node)
9762 return t;
9763
9764 gcc_assert (TREE_CODE (t) == TREE_LIST);
9765
9766 default_value = TREE_PURPOSE (t);
9767 parm_decl = TREE_VALUE (t);
9768
9769 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9770 if (TREE_CODE (parm_decl) == PARM_DECL
9771 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9772 parm_decl = error_mark_node;
9773 default_value = tsubst_template_arg (default_value, args,
9774 complain, NULL_TREE);
9775
9776 return build_tree_list (default_value, parm_decl);
9777 }
9778
9779 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9780 type T. If T is not an aggregate or enumeration type, it is
9781 handled as if by tsubst. IN_DECL is as for tsubst. If
9782 ENTERING_SCOPE is nonzero, T is the context for a template which
9783 we are presently tsubst'ing. Return the substituted value. */
9784
9785 static tree
9786 tsubst_aggr_type (tree t,
9787 tree args,
9788 tsubst_flags_t complain,
9789 tree in_decl,
9790 int entering_scope)
9791 {
9792 if (t == NULL_TREE)
9793 return NULL_TREE;
9794
9795 switch (TREE_CODE (t))
9796 {
9797 case RECORD_TYPE:
9798 if (TYPE_PTRMEMFUNC_P (t))
9799 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9800
9801 /* Else fall through. */
9802 case ENUMERAL_TYPE:
9803 case UNION_TYPE:
9804 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9805 {
9806 tree argvec;
9807 tree context;
9808 tree r;
9809 int saved_unevaluated_operand;
9810 int saved_inhibit_evaluation_warnings;
9811
9812 /* In "sizeof(X<I>)" we need to evaluate "I". */
9813 saved_unevaluated_operand = cp_unevaluated_operand;
9814 cp_unevaluated_operand = 0;
9815 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9816 c_inhibit_evaluation_warnings = 0;
9817
9818 /* First, determine the context for the type we are looking
9819 up. */
9820 context = TYPE_CONTEXT (t);
9821 if (context && TYPE_P (context))
9822 {
9823 context = tsubst_aggr_type (context, args, complain,
9824 in_decl, /*entering_scope=*/1);
9825 /* If context is a nested class inside a class template,
9826 it may still need to be instantiated (c++/33959). */
9827 context = complete_type (context);
9828 }
9829
9830 /* Then, figure out what arguments are appropriate for the
9831 type we are trying to find. For example, given:
9832
9833 template <class T> struct S;
9834 template <class T, class U> void f(T, U) { S<U> su; }
9835
9836 and supposing that we are instantiating f<int, double>,
9837 then our ARGS will be {int, double}, but, when looking up
9838 S we only want {double}. */
9839 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9840 complain, in_decl);
9841 if (argvec == error_mark_node)
9842 r = error_mark_node;
9843 else
9844 {
9845 r = lookup_template_class (t, argvec, in_decl, context,
9846 entering_scope, complain);
9847 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9848 }
9849
9850 cp_unevaluated_operand = saved_unevaluated_operand;
9851 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9852
9853 return r;
9854 }
9855 else
9856 /* This is not a template type, so there's nothing to do. */
9857 return t;
9858
9859 default:
9860 return tsubst (t, args, complain, in_decl);
9861 }
9862 }
9863
9864 /* Substitute into the default argument ARG (a default argument for
9865 FN), which has the indicated TYPE. */
9866
9867 tree
9868 tsubst_default_argument (tree fn, tree type, tree arg)
9869 {
9870 tree saved_class_ptr = NULL_TREE;
9871 tree saved_class_ref = NULL_TREE;
9872 int errs = errorcount + sorrycount;
9873
9874 /* This can happen in invalid code. */
9875 if (TREE_CODE (arg) == DEFAULT_ARG)
9876 return arg;
9877
9878 /* This default argument came from a template. Instantiate the
9879 default argument here, not in tsubst. In the case of
9880 something like:
9881
9882 template <class T>
9883 struct S {
9884 static T t();
9885 void f(T = t());
9886 };
9887
9888 we must be careful to do name lookup in the scope of S<T>,
9889 rather than in the current class. */
9890 push_access_scope (fn);
9891 /* The "this" pointer is not valid in a default argument. */
9892 if (cfun)
9893 {
9894 saved_class_ptr = current_class_ptr;
9895 cp_function_chain->x_current_class_ptr = NULL_TREE;
9896 saved_class_ref = current_class_ref;
9897 cp_function_chain->x_current_class_ref = NULL_TREE;
9898 }
9899
9900 push_deferring_access_checks(dk_no_deferred);
9901 /* The default argument expression may cause implicitly defined
9902 member functions to be synthesized, which will result in garbage
9903 collection. We must treat this situation as if we were within
9904 the body of function so as to avoid collecting live data on the
9905 stack. */
9906 ++function_depth;
9907 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9908 tf_warning_or_error, NULL_TREE,
9909 /*integral_constant_expression_p=*/false);
9910 --function_depth;
9911 pop_deferring_access_checks();
9912
9913 /* Restore the "this" pointer. */
9914 if (cfun)
9915 {
9916 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9917 cp_function_chain->x_current_class_ref = saved_class_ref;
9918 }
9919
9920 if (errorcount+sorrycount > errs)
9921 inform (input_location,
9922 " when instantiating default argument for call to %D", fn);
9923
9924 /* Make sure the default argument is reasonable. */
9925 arg = check_default_argument (type, arg);
9926
9927 pop_access_scope (fn);
9928
9929 return arg;
9930 }
9931
9932 /* Substitute into all the default arguments for FN. */
9933
9934 static void
9935 tsubst_default_arguments (tree fn)
9936 {
9937 tree arg;
9938 tree tmpl_args;
9939
9940 tmpl_args = DECL_TI_ARGS (fn);
9941
9942 /* If this function is not yet instantiated, we certainly don't need
9943 its default arguments. */
9944 if (uses_template_parms (tmpl_args))
9945 return;
9946 /* Don't do this again for clones. */
9947 if (DECL_CLONED_FUNCTION_P (fn))
9948 return;
9949
9950 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9951 arg;
9952 arg = TREE_CHAIN (arg))
9953 if (TREE_PURPOSE (arg))
9954 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9955 TREE_VALUE (arg),
9956 TREE_PURPOSE (arg));
9957 }
9958
9959 /* Substitute the ARGS into the T, which is a _DECL. Return the
9960 result of the substitution. Issue error and warning messages under
9961 control of COMPLAIN. */
9962
9963 static tree
9964 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9965 {
9966 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9967 location_t saved_loc;
9968 tree r = NULL_TREE;
9969 tree in_decl = t;
9970 hashval_t hash = 0;
9971
9972 /* Set the filename and linenumber to improve error-reporting. */
9973 saved_loc = input_location;
9974 input_location = DECL_SOURCE_LOCATION (t);
9975
9976 switch (TREE_CODE (t))
9977 {
9978 case TEMPLATE_DECL:
9979 {
9980 /* We can get here when processing a member function template,
9981 member class template, or template template parameter. */
9982 tree decl = DECL_TEMPLATE_RESULT (t);
9983 tree spec;
9984 tree tmpl_args;
9985 tree full_args;
9986
9987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9988 {
9989 /* Template template parameter is treated here. */
9990 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9991 if (new_type == error_mark_node)
9992 RETURN (error_mark_node);
9993 /* If we get a real template back, return it. This can happen in
9994 the context of most_specialized_class. */
9995 if (TREE_CODE (new_type) == TEMPLATE_DECL)
9996 return new_type;
9997
9998 r = copy_decl (t);
9999 DECL_CHAIN (r) = NULL_TREE;
10000 TREE_TYPE (r) = new_type;
10001 DECL_TEMPLATE_RESULT (r)
10002 = build_decl (DECL_SOURCE_LOCATION (decl),
10003 TYPE_DECL, DECL_NAME (decl), new_type);
10004 DECL_TEMPLATE_PARMS (r)
10005 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10006 complain);
10007 TYPE_NAME (new_type) = r;
10008 break;
10009 }
10010
10011 /* We might already have an instance of this template.
10012 The ARGS are for the surrounding class type, so the
10013 full args contain the tsubst'd args for the context,
10014 plus the innermost args from the template decl. */
10015 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10016 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10017 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10018 /* Because this is a template, the arguments will still be
10019 dependent, even after substitution. If
10020 PROCESSING_TEMPLATE_DECL is not set, the dependency
10021 predicates will short-circuit. */
10022 ++processing_template_decl;
10023 full_args = tsubst_template_args (tmpl_args, args,
10024 complain, in_decl);
10025 --processing_template_decl;
10026 if (full_args == error_mark_node)
10027 RETURN (error_mark_node);
10028
10029 /* If this is a default template template argument,
10030 tsubst might not have changed anything. */
10031 if (full_args == tmpl_args)
10032 RETURN (t);
10033
10034 hash = hash_tmpl_and_args (t, full_args);
10035 spec = retrieve_specialization (t, full_args, hash);
10036 if (spec != NULL_TREE)
10037 {
10038 r = spec;
10039 break;
10040 }
10041
10042 /* Make a new template decl. It will be similar to the
10043 original, but will record the current template arguments.
10044 We also create a new function declaration, which is just
10045 like the old one, but points to this new template, rather
10046 than the old one. */
10047 r = copy_decl (t);
10048 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10049 DECL_CHAIN (r) = NULL_TREE;
10050
10051 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10052
10053 if (TREE_CODE (decl) == TYPE_DECL
10054 && !TYPE_DECL_ALIAS_P (decl))
10055 {
10056 tree new_type;
10057 ++processing_template_decl;
10058 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10059 --processing_template_decl;
10060 if (new_type == error_mark_node)
10061 RETURN (error_mark_node);
10062
10063 TREE_TYPE (r) = new_type;
10064 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10065 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10066 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10067 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10068 }
10069 else
10070 {
10071 tree new_decl;
10072 ++processing_template_decl;
10073 new_decl = tsubst (decl, args, complain, in_decl);
10074 --processing_template_decl;
10075 if (new_decl == error_mark_node)
10076 RETURN (error_mark_node);
10077
10078 DECL_TEMPLATE_RESULT (r) = new_decl;
10079 DECL_TI_TEMPLATE (new_decl) = r;
10080 TREE_TYPE (r) = TREE_TYPE (new_decl);
10081 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10082 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10083 }
10084
10085 SET_DECL_IMPLICIT_INSTANTIATION (r);
10086 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10087 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10088
10089 /* The template parameters for this new template are all the
10090 template parameters for the old template, except the
10091 outermost level of parameters. */
10092 DECL_TEMPLATE_PARMS (r)
10093 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10094 complain);
10095
10096 if (PRIMARY_TEMPLATE_P (t))
10097 DECL_PRIMARY_TEMPLATE (r) = r;
10098
10099 if (TREE_CODE (decl) != TYPE_DECL)
10100 /* Record this non-type partial instantiation. */
10101 register_specialization (r, t,
10102 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10103 false, hash);
10104 }
10105 break;
10106
10107 case FUNCTION_DECL:
10108 {
10109 tree ctx;
10110 tree argvec = NULL_TREE;
10111 tree *friends;
10112 tree gen_tmpl;
10113 tree type;
10114 int member;
10115 int args_depth;
10116 int parms_depth;
10117
10118 /* Nobody should be tsubst'ing into non-template functions. */
10119 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10120
10121 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10122 {
10123 tree spec;
10124 bool dependent_p;
10125
10126 /* If T is not dependent, just return it. We have to
10127 increment PROCESSING_TEMPLATE_DECL because
10128 value_dependent_expression_p assumes that nothing is
10129 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10130 ++processing_template_decl;
10131 dependent_p = value_dependent_expression_p (t);
10132 --processing_template_decl;
10133 if (!dependent_p)
10134 RETURN (t);
10135
10136 /* Calculate the most general template of which R is a
10137 specialization, and the complete set of arguments used to
10138 specialize R. */
10139 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10140 argvec = tsubst_template_args (DECL_TI_ARGS
10141 (DECL_TEMPLATE_RESULT
10142 (DECL_TI_TEMPLATE (t))),
10143 args, complain, in_decl);
10144 if (argvec == error_mark_node)
10145 RETURN (error_mark_node);
10146
10147 /* Check to see if we already have this specialization. */
10148 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10149 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10150
10151 if (spec)
10152 {
10153 r = spec;
10154 break;
10155 }
10156
10157 /* We can see more levels of arguments than parameters if
10158 there was a specialization of a member template, like
10159 this:
10160
10161 template <class T> struct S { template <class U> void f(); }
10162 template <> template <class U> void S<int>::f(U);
10163
10164 Here, we'll be substituting into the specialization,
10165 because that's where we can find the code we actually
10166 want to generate, but we'll have enough arguments for
10167 the most general template.
10168
10169 We also deal with the peculiar case:
10170
10171 template <class T> struct S {
10172 template <class U> friend void f();
10173 };
10174 template <class U> void f() {}
10175 template S<int>;
10176 template void f<double>();
10177
10178 Here, the ARGS for the instantiation of will be {int,
10179 double}. But, we only need as many ARGS as there are
10180 levels of template parameters in CODE_PATTERN. We are
10181 careful not to get fooled into reducing the ARGS in
10182 situations like:
10183
10184 template <class T> struct S { template <class U> void f(U); }
10185 template <class T> template <> void S<T>::f(int) {}
10186
10187 which we can spot because the pattern will be a
10188 specialization in this case. */
10189 args_depth = TMPL_ARGS_DEPTH (args);
10190 parms_depth =
10191 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10192 if (args_depth > parms_depth
10193 && !DECL_TEMPLATE_SPECIALIZATION (t))
10194 args = get_innermost_template_args (args, parms_depth);
10195 }
10196 else
10197 {
10198 /* This special case arises when we have something like this:
10199
10200 template <class T> struct S {
10201 friend void f<int>(int, double);
10202 };
10203
10204 Here, the DECL_TI_TEMPLATE for the friend declaration
10205 will be an IDENTIFIER_NODE. We are being called from
10206 tsubst_friend_function, and we want only to create a
10207 new decl (R) with appropriate types so that we can call
10208 determine_specialization. */
10209 gen_tmpl = NULL_TREE;
10210 }
10211
10212 if (DECL_CLASS_SCOPE_P (t))
10213 {
10214 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10215 member = 2;
10216 else
10217 member = 1;
10218 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10219 complain, t, /*entering_scope=*/1);
10220 }
10221 else
10222 {
10223 member = 0;
10224 ctx = DECL_CONTEXT (t);
10225 }
10226 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10227 if (type == error_mark_node)
10228 RETURN (error_mark_node);
10229
10230 /* If we hit excessive deduction depth, the type is bogus even if
10231 it isn't error_mark_node, so don't build a decl. */
10232 if (excessive_deduction_depth)
10233 RETURN (error_mark_node);
10234
10235 /* We do NOT check for matching decls pushed separately at this
10236 point, as they may not represent instantiations of this
10237 template, and in any case are considered separate under the
10238 discrete model. */
10239 r = copy_decl (t);
10240 DECL_USE_TEMPLATE (r) = 0;
10241 TREE_TYPE (r) = type;
10242 /* Clear out the mangled name and RTL for the instantiation. */
10243 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10244 SET_DECL_RTL (r, NULL);
10245 /* Leave DECL_INITIAL set on deleted instantiations. */
10246 if (!DECL_DELETED_FN (r))
10247 DECL_INITIAL (r) = NULL_TREE;
10248 DECL_CONTEXT (r) = ctx;
10249
10250 if (member && DECL_CONV_FN_P (r))
10251 /* Type-conversion operator. Reconstruct the name, in
10252 case it's the name of one of the template's parameters. */
10253 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10254
10255 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10256 complain, t);
10257 DECL_RESULT (r) = NULL_TREE;
10258
10259 TREE_STATIC (r) = 0;
10260 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10261 DECL_EXTERNAL (r) = 1;
10262 /* If this is an instantiation of a function with internal
10263 linkage, we already know what object file linkage will be
10264 assigned to the instantiation. */
10265 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10266 DECL_DEFER_OUTPUT (r) = 0;
10267 DECL_CHAIN (r) = NULL_TREE;
10268 DECL_PENDING_INLINE_INFO (r) = 0;
10269 DECL_PENDING_INLINE_P (r) = 0;
10270 DECL_SAVED_TREE (r) = NULL_TREE;
10271 DECL_STRUCT_FUNCTION (r) = NULL;
10272 TREE_USED (r) = 0;
10273 /* We'll re-clone as appropriate in instantiate_template. */
10274 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10275
10276 /* If we aren't complaining now, return on error before we register
10277 the specialization so that we'll complain eventually. */
10278 if ((complain & tf_error) == 0
10279 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10280 && !grok_op_properties (r, /*complain=*/false))
10281 RETURN (error_mark_node);
10282
10283 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10284 this in the special friend case mentioned above where
10285 GEN_TMPL is NULL. */
10286 if (gen_tmpl)
10287 {
10288 DECL_TEMPLATE_INFO (r)
10289 = build_template_info (gen_tmpl, argvec);
10290 SET_DECL_IMPLICIT_INSTANTIATION (r);
10291
10292 tree new_r
10293 = register_specialization (r, gen_tmpl, argvec, false, hash);
10294 if (new_r != r)
10295 /* We instantiated this while substituting into
10296 the type earlier (template/friend54.C). */
10297 RETURN (new_r);
10298
10299 /* We're not supposed to instantiate default arguments
10300 until they are called, for a template. But, for a
10301 declaration like:
10302
10303 template <class T> void f ()
10304 { extern void g(int i = T()); }
10305
10306 we should do the substitution when the template is
10307 instantiated. We handle the member function case in
10308 instantiate_class_template since the default arguments
10309 might refer to other members of the class. */
10310 if (!member
10311 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10312 && !uses_template_parms (argvec))
10313 tsubst_default_arguments (r);
10314 }
10315 else
10316 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10317
10318 /* Copy the list of befriending classes. */
10319 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10320 *friends;
10321 friends = &TREE_CHAIN (*friends))
10322 {
10323 *friends = copy_node (*friends);
10324 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10325 args, complain,
10326 in_decl);
10327 }
10328
10329 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10330 {
10331 maybe_retrofit_in_chrg (r);
10332 if (DECL_CONSTRUCTOR_P (r))
10333 grok_ctor_properties (ctx, r);
10334 if (DECL_INHERITED_CTOR_BASE (r))
10335 deduce_inheriting_ctor (r);
10336 /* If this is an instantiation of a member template, clone it.
10337 If it isn't, that'll be handled by
10338 clone_constructors_and_destructors. */
10339 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10340 clone_function_decl (r, /*update_method_vec_p=*/0);
10341 }
10342 else if ((complain & tf_error) != 0
10343 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10344 && !grok_op_properties (r, /*complain=*/true))
10345 RETURN (error_mark_node);
10346
10347 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10348 SET_DECL_FRIEND_CONTEXT (r,
10349 tsubst (DECL_FRIEND_CONTEXT (t),
10350 args, complain, in_decl));
10351
10352 /* Possibly limit visibility based on template args. */
10353 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10354 if (DECL_VISIBILITY_SPECIFIED (t))
10355 {
10356 DECL_VISIBILITY_SPECIFIED (r) = 0;
10357 DECL_ATTRIBUTES (r)
10358 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10359 }
10360 determine_visibility (r);
10361 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10362 && !processing_template_decl)
10363 defaulted_late_check (r);
10364
10365 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10366 args, complain, in_decl);
10367 }
10368 break;
10369
10370 case PARM_DECL:
10371 {
10372 tree type = NULL_TREE;
10373 int i, len = 1;
10374 tree expanded_types = NULL_TREE;
10375 tree prev_r = NULL_TREE;
10376 tree first_r = NULL_TREE;
10377
10378 if (FUNCTION_PARAMETER_PACK_P (t))
10379 {
10380 /* If there is a local specialization that isn't a
10381 parameter pack, it means that we're doing a "simple"
10382 substitution from inside tsubst_pack_expansion. Just
10383 return the local specialization (which will be a single
10384 parm). */
10385 tree spec = retrieve_local_specialization (t);
10386 if (spec
10387 && TREE_CODE (spec) == PARM_DECL
10388 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10389 RETURN (spec);
10390
10391 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10392 the parameters in this function parameter pack. */
10393 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10394 complain, in_decl);
10395 if (TREE_CODE (expanded_types) == TREE_VEC)
10396 {
10397 len = TREE_VEC_LENGTH (expanded_types);
10398
10399 /* Zero-length parameter packs are boring. Just substitute
10400 into the chain. */
10401 if (len == 0)
10402 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10403 TREE_CHAIN (t)));
10404 }
10405 else
10406 {
10407 /* All we did was update the type. Make a note of that. */
10408 type = expanded_types;
10409 expanded_types = NULL_TREE;
10410 }
10411 }
10412
10413 /* Loop through all of the parameter's we'll build. When T is
10414 a function parameter pack, LEN is the number of expanded
10415 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10416 r = NULL_TREE;
10417 for (i = 0; i < len; ++i)
10418 {
10419 prev_r = r;
10420 r = copy_node (t);
10421 if (DECL_TEMPLATE_PARM_P (t))
10422 SET_DECL_TEMPLATE_PARM_P (r);
10423
10424 if (expanded_types)
10425 /* We're on the Ith parameter of the function parameter
10426 pack. */
10427 {
10428 /* An argument of a function parameter pack is not a parameter
10429 pack. */
10430 FUNCTION_PARAMETER_PACK_P (r) = false;
10431
10432 /* Get the Ith type. */
10433 type = TREE_VEC_ELT (expanded_types, i);
10434
10435 /* Rename the parameter to include the index. */
10436 DECL_NAME (r)
10437 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10438 }
10439 else if (!type)
10440 /* We're dealing with a normal parameter. */
10441 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10442
10443 type = type_decays_to (type);
10444 TREE_TYPE (r) = type;
10445 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10446
10447 if (DECL_INITIAL (r))
10448 {
10449 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10450 DECL_INITIAL (r) = TREE_TYPE (r);
10451 else
10452 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10453 complain, in_decl);
10454 }
10455
10456 DECL_CONTEXT (r) = NULL_TREE;
10457
10458 if (!DECL_TEMPLATE_PARM_P (r))
10459 DECL_ARG_TYPE (r) = type_passed_as (type);
10460
10461 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10462 args, complain, in_decl);
10463
10464 /* Keep track of the first new parameter we
10465 generate. That's what will be returned to the
10466 caller. */
10467 if (!first_r)
10468 first_r = r;
10469
10470 /* Build a proper chain of parameters when substituting
10471 into a function parameter pack. */
10472 if (prev_r)
10473 DECL_CHAIN (prev_r) = r;
10474 }
10475
10476 /* If cp_unevaluated_operand is set, we're just looking for a
10477 single dummy parameter, so don't keep going. */
10478 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10479 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10480 complain, DECL_CHAIN (t));
10481
10482 /* FIRST_R contains the start of the chain we've built. */
10483 r = first_r;
10484 }
10485 break;
10486
10487 case FIELD_DECL:
10488 {
10489 tree type;
10490
10491 r = copy_decl (t);
10492 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10493 if (type == error_mark_node)
10494 RETURN (error_mark_node);
10495 TREE_TYPE (r) = type;
10496 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10497
10498 if (DECL_C_BIT_FIELD (r))
10499 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10500 non-bit-fields DECL_INITIAL is a non-static data member
10501 initializer, which gets deferred instantiation. */
10502 DECL_INITIAL (r)
10503 = tsubst_expr (DECL_INITIAL (t), args,
10504 complain, in_decl,
10505 /*integral_constant_expression_p=*/true);
10506 else if (DECL_INITIAL (t))
10507 {
10508 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10509 NSDMI in perform_member_init. Still set DECL_INITIAL
10510 so that we know there is one. */
10511 DECL_INITIAL (r) = void_zero_node;
10512 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10513 retrofit_lang_decl (r);
10514 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10515 }
10516 /* We don't have to set DECL_CONTEXT here; it is set by
10517 finish_member_declaration. */
10518 DECL_CHAIN (r) = NULL_TREE;
10519 if (VOID_TYPE_P (type))
10520 error ("instantiation of %q+D as type %qT", r, type);
10521
10522 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10523 args, complain, in_decl);
10524 }
10525 break;
10526
10527 case USING_DECL:
10528 /* We reach here only for member using decls. We also need to check
10529 uses_template_parms because DECL_DEPENDENT_P is not set for a
10530 using-declaration that designates a member of the current
10531 instantiation (c++/53549). */
10532 if (DECL_DEPENDENT_P (t)
10533 || uses_template_parms (USING_DECL_SCOPE (t)))
10534 {
10535 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10536 complain, in_decl);
10537 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10538 r = do_class_using_decl (inst_scope, name);
10539 if (!r)
10540 r = error_mark_node;
10541 else
10542 {
10543 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10544 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10545 }
10546 }
10547 else
10548 {
10549 r = copy_node (t);
10550 DECL_CHAIN (r) = NULL_TREE;
10551 }
10552 break;
10553
10554 case TYPE_DECL:
10555 case VAR_DECL:
10556 {
10557 tree argvec = NULL_TREE;
10558 tree gen_tmpl = NULL_TREE;
10559 tree spec;
10560 tree tmpl = NULL_TREE;
10561 tree ctx;
10562 tree type = NULL_TREE;
10563 bool local_p;
10564
10565 if (TREE_CODE (t) == TYPE_DECL
10566 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10567 {
10568 /* If this is the canonical decl, we don't have to
10569 mess with instantiations, and often we can't (for
10570 typename, template type parms and such). Note that
10571 TYPE_NAME is not correct for the above test if
10572 we've copied the type for a typedef. */
10573 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10574 if (type == error_mark_node)
10575 RETURN (error_mark_node);
10576 r = TYPE_NAME (type);
10577 break;
10578 }
10579
10580 /* Check to see if we already have the specialization we
10581 need. */
10582 spec = NULL_TREE;
10583 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10584 {
10585 /* T is a static data member or namespace-scope entity.
10586 We have to substitute into namespace-scope variables
10587 (even though such entities are never templates) because
10588 of cases like:
10589
10590 template <class T> void f() { extern T t; }
10591
10592 where the entity referenced is not known until
10593 instantiation time. */
10594 local_p = false;
10595 ctx = DECL_CONTEXT (t);
10596 if (DECL_CLASS_SCOPE_P (t))
10597 {
10598 ctx = tsubst_aggr_type (ctx, args,
10599 complain,
10600 in_decl, /*entering_scope=*/1);
10601 /* If CTX is unchanged, then T is in fact the
10602 specialization we want. That situation occurs when
10603 referencing a static data member within in its own
10604 class. We can use pointer equality, rather than
10605 same_type_p, because DECL_CONTEXT is always
10606 canonical... */
10607 if (ctx == DECL_CONTEXT (t)
10608 && (TREE_CODE (t) != TYPE_DECL
10609 /* ... unless T is a member template; in which
10610 case our caller can be willing to create a
10611 specialization of that template represented
10612 by T. */
10613 || !(DECL_TI_TEMPLATE (t)
10614 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10615 spec = t;
10616 }
10617
10618 if (!spec)
10619 {
10620 tmpl = DECL_TI_TEMPLATE (t);
10621 gen_tmpl = most_general_template (tmpl);
10622 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10623 if (argvec == error_mark_node)
10624 RETURN (error_mark_node);
10625 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10626 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10627 }
10628 }
10629 else
10630 {
10631 /* A local variable. */
10632 local_p = true;
10633 /* Subsequent calls to pushdecl will fill this in. */
10634 ctx = NULL_TREE;
10635 spec = retrieve_local_specialization (t);
10636 }
10637 /* If we already have the specialization we need, there is
10638 nothing more to do. */
10639 if (spec)
10640 {
10641 r = spec;
10642 break;
10643 }
10644
10645 if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t))
10646 {
10647 /* Just use name lookup to find a member alias for an anonymous
10648 union, but then add it to the hash table. */
10649 r = lookup_name (DECL_NAME (t));
10650 gcc_assert (DECL_ANON_UNION_VAR_P (r));
10651 register_local_specialization (r, t);
10652 break;
10653 }
10654
10655 /* Create a new node for the specialization we need. */
10656 r = copy_decl (t);
10657 if (type == NULL_TREE)
10658 {
10659 if (is_typedef_decl (t))
10660 type = DECL_ORIGINAL_TYPE (t);
10661 else
10662 type = TREE_TYPE (t);
10663 if (TREE_CODE (t) == VAR_DECL
10664 && VAR_HAD_UNKNOWN_BOUND (t)
10665 && type != error_mark_node)
10666 type = strip_array_domain (type);
10667 type = tsubst (type, args, complain, in_decl);
10668 }
10669 if (TREE_CODE (r) == VAR_DECL)
10670 {
10671 /* Even if the original location is out of scope, the
10672 newly substituted one is not. */
10673 DECL_DEAD_FOR_LOCAL (r) = 0;
10674 DECL_INITIALIZED_P (r) = 0;
10675 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10676 if (type == error_mark_node)
10677 RETURN (error_mark_node);
10678 if (TREE_CODE (type) == FUNCTION_TYPE)
10679 {
10680 /* It may seem that this case cannot occur, since:
10681
10682 typedef void f();
10683 void g() { f x; }
10684
10685 declares a function, not a variable. However:
10686
10687 typedef void f();
10688 template <typename T> void g() { T t; }
10689 template void g<f>();
10690
10691 is an attempt to declare a variable with function
10692 type. */
10693 error ("variable %qD has function type",
10694 /* R is not yet sufficiently initialized, so we
10695 just use its name. */
10696 DECL_NAME (r));
10697 RETURN (error_mark_node);
10698 }
10699 type = complete_type (type);
10700 /* Wait until cp_finish_decl to set this again, to handle
10701 circular dependency (template/instantiate6.C). */
10702 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10703 type = check_var_type (DECL_NAME (r), type);
10704
10705 if (DECL_HAS_VALUE_EXPR_P (t))
10706 {
10707 tree ve = DECL_VALUE_EXPR (t);
10708 ve = tsubst_expr (ve, args, complain, in_decl,
10709 /*constant_expression_p=*/false);
10710 if (REFERENCE_REF_P (ve))
10711 {
10712 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10713 ve = TREE_OPERAND (ve, 0);
10714 }
10715 SET_DECL_VALUE_EXPR (r, ve);
10716 }
10717 }
10718 else if (DECL_SELF_REFERENCE_P (t))
10719 SET_DECL_SELF_REFERENCE_P (r);
10720 TREE_TYPE (r) = type;
10721 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10722 DECL_CONTEXT (r) = ctx;
10723 /* Clear out the mangled name and RTL for the instantiation. */
10724 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10725 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10726 SET_DECL_RTL (r, NULL);
10727 /* The initializer must not be expanded until it is required;
10728 see [temp.inst]. */
10729 DECL_INITIAL (r) = NULL_TREE;
10730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10731 SET_DECL_RTL (r, NULL);
10732 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10733 if (TREE_CODE (r) == VAR_DECL)
10734 {
10735 /* Possibly limit visibility based on template args. */
10736 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10737 if (DECL_VISIBILITY_SPECIFIED (t))
10738 {
10739 DECL_VISIBILITY_SPECIFIED (r) = 0;
10740 DECL_ATTRIBUTES (r)
10741 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10742 }
10743 determine_visibility (r);
10744 }
10745
10746 if (!local_p)
10747 {
10748 /* A static data member declaration is always marked
10749 external when it is declared in-class, even if an
10750 initializer is present. We mimic the non-template
10751 processing here. */
10752 DECL_EXTERNAL (r) = 1;
10753
10754 register_specialization (r, gen_tmpl, argvec, false, hash);
10755 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10756 SET_DECL_IMPLICIT_INSTANTIATION (r);
10757 }
10758 else if (cp_unevaluated_operand)
10759 {
10760 /* We're substituting this var in a decltype outside of its
10761 scope, such as for a lambda return type. Don't add it to
10762 local_specializations, do perform auto deduction. */
10763 tree auto_node = type_uses_auto (type);
10764 if (auto_node)
10765 {
10766 tree init
10767 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10768 /*constant_expression_p=*/false);
10769 init = resolve_nondeduced_context (init);
10770 TREE_TYPE (r) = type
10771 = do_auto_deduction (type, init, auto_node);
10772 }
10773 }
10774 else
10775 register_local_specialization (r, t);
10776
10777 DECL_CHAIN (r) = NULL_TREE;
10778
10779 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10780 /*flags=*/0,
10781 args, complain, in_decl);
10782
10783 /* Preserve a typedef that names a type. */
10784 if (is_typedef_decl (r))
10785 {
10786 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10787 set_underlying_type (r);
10788 }
10789
10790 layout_decl (r, 0);
10791 }
10792 break;
10793
10794 default:
10795 gcc_unreachable ();
10796 }
10797 #undef RETURN
10798
10799 out:
10800 /* Restore the file and line information. */
10801 input_location = saved_loc;
10802
10803 return r;
10804 }
10805
10806 /* Substitute into the ARG_TYPES of a function type.
10807 If END is a TREE_CHAIN, leave it and any following types
10808 un-substituted. */
10809
10810 static tree
10811 tsubst_arg_types (tree arg_types,
10812 tree args,
10813 tree end,
10814 tsubst_flags_t complain,
10815 tree in_decl)
10816 {
10817 tree remaining_arg_types;
10818 tree type = NULL_TREE;
10819 int i = 1;
10820 tree expanded_args = NULL_TREE;
10821 tree default_arg;
10822
10823 if (!arg_types || arg_types == void_list_node || arg_types == end)
10824 return arg_types;
10825
10826 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10827 args, end, complain, in_decl);
10828 if (remaining_arg_types == error_mark_node)
10829 return error_mark_node;
10830
10831 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10832 {
10833 /* For a pack expansion, perform substitution on the
10834 entire expression. Later on, we'll handle the arguments
10835 one-by-one. */
10836 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10837 args, complain, in_decl);
10838
10839 if (TREE_CODE (expanded_args) == TREE_VEC)
10840 /* So that we'll spin through the parameters, one by one. */
10841 i = TREE_VEC_LENGTH (expanded_args);
10842 else
10843 {
10844 /* We only partially substituted into the parameter
10845 pack. Our type is TYPE_PACK_EXPANSION. */
10846 type = expanded_args;
10847 expanded_args = NULL_TREE;
10848 }
10849 }
10850
10851 while (i > 0) {
10852 --i;
10853
10854 if (expanded_args)
10855 type = TREE_VEC_ELT (expanded_args, i);
10856 else if (!type)
10857 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10858
10859 if (type == error_mark_node)
10860 return error_mark_node;
10861 if (VOID_TYPE_P (type))
10862 {
10863 if (complain & tf_error)
10864 {
10865 error ("invalid parameter type %qT", type);
10866 if (in_decl)
10867 error ("in declaration %q+D", in_decl);
10868 }
10869 return error_mark_node;
10870 }
10871 /* DR 657. */
10872 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
10873 return error_mark_node;
10874
10875 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10876 top-level qualifiers as required. */
10877 type = cv_unqualified (type_decays_to (type));
10878
10879 /* We do not substitute into default arguments here. The standard
10880 mandates that they be instantiated only when needed, which is
10881 done in build_over_call. */
10882 default_arg = TREE_PURPOSE (arg_types);
10883
10884 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10885 {
10886 /* We've instantiated a template before its default arguments
10887 have been parsed. This can happen for a nested template
10888 class, and is not an error unless we require the default
10889 argument in a call of this function. */
10890 remaining_arg_types =
10891 tree_cons (default_arg, type, remaining_arg_types);
10892 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
10893 }
10894 else
10895 remaining_arg_types =
10896 hash_tree_cons (default_arg, type, remaining_arg_types);
10897 }
10898
10899 return remaining_arg_types;
10900 }
10901
10902 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10903 *not* handle the exception-specification for FNTYPE, because the
10904 initial substitution of explicitly provided template parameters
10905 during argument deduction forbids substitution into the
10906 exception-specification:
10907
10908 [temp.deduct]
10909
10910 All references in the function type of the function template to the
10911 corresponding template parameters are replaced by the specified tem-
10912 plate argument values. If a substitution in a template parameter or
10913 in the function type of the function template results in an invalid
10914 type, type deduction fails. [Note: The equivalent substitution in
10915 exception specifications is done only when the function is instanti-
10916 ated, at which point a program is ill-formed if the substitution
10917 results in an invalid type.] */
10918
10919 static tree
10920 tsubst_function_type (tree t,
10921 tree args,
10922 tsubst_flags_t complain,
10923 tree in_decl)
10924 {
10925 tree return_type;
10926 tree arg_types;
10927 tree fntype;
10928
10929 /* The TYPE_CONTEXT is not used for function/method types. */
10930 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10931
10932 /* Substitute the return type. */
10933 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10934 if (return_type == error_mark_node)
10935 return error_mark_node;
10936 /* DR 486 clarifies that creation of a function type with an
10937 invalid return type is a deduction failure. */
10938 if (TREE_CODE (return_type) == ARRAY_TYPE
10939 || TREE_CODE (return_type) == FUNCTION_TYPE)
10940 {
10941 if (complain & tf_error)
10942 {
10943 if (TREE_CODE (return_type) == ARRAY_TYPE)
10944 error ("function returning an array");
10945 else
10946 error ("function returning a function");
10947 }
10948 return error_mark_node;
10949 }
10950 /* And DR 657. */
10951 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
10952 return error_mark_node;
10953
10954 /* Substitute the argument types. */
10955 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10956 complain, in_decl);
10957 if (arg_types == error_mark_node)
10958 return error_mark_node;
10959
10960 /* Construct a new type node and return it. */
10961 if (TREE_CODE (t) == FUNCTION_TYPE)
10962 {
10963 fntype = build_function_type (return_type, arg_types);
10964 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10965 }
10966 else
10967 {
10968 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10969 if (! MAYBE_CLASS_TYPE_P (r))
10970 {
10971 /* [temp.deduct]
10972
10973 Type deduction may fail for any of the following
10974 reasons:
10975
10976 -- Attempting to create "pointer to member of T" when T
10977 is not a class type. */
10978 if (complain & tf_error)
10979 error ("creating pointer to member function of non-class type %qT",
10980 r);
10981 return error_mark_node;
10982 }
10983
10984 fntype = build_method_type_directly (r, return_type,
10985 TREE_CHAIN (arg_types));
10986 }
10987 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10988
10989 return fntype;
10990 }
10991
10992 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10993 ARGS into that specification, and return the substituted
10994 specification. If there is no specification, return NULL_TREE. */
10995
10996 static tree
10997 tsubst_exception_specification (tree fntype,
10998 tree args,
10999 tsubst_flags_t complain,
11000 tree in_decl,
11001 bool defer_ok)
11002 {
11003 tree specs;
11004 tree new_specs;
11005
11006 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11007 new_specs = NULL_TREE;
11008 if (specs && TREE_PURPOSE (specs))
11009 {
11010 /* A noexcept-specifier. */
11011 tree expr = TREE_PURPOSE (specs);
11012 if (TREE_CODE (expr) == INTEGER_CST)
11013 new_specs = expr;
11014 else if (defer_ok)
11015 {
11016 /* Defer instantiation of noexcept-specifiers to avoid
11017 excessive instantiations (c++/49107). */
11018 new_specs = make_node (DEFERRED_NOEXCEPT);
11019 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11020 {
11021 /* We already partially instantiated this member template,
11022 so combine the new args with the old. */
11023 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11024 = DEFERRED_NOEXCEPT_PATTERN (expr);
11025 DEFERRED_NOEXCEPT_ARGS (new_specs)
11026 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11027 }
11028 else
11029 {
11030 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11031 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11032 }
11033 }
11034 else
11035 new_specs = tsubst_copy_and_build
11036 (expr, args, complain, in_decl, /*function_p=*/false,
11037 /*integral_constant_expression_p=*/true);
11038 new_specs = build_noexcept_spec (new_specs, complain);
11039 }
11040 else if (specs)
11041 {
11042 if (! TREE_VALUE (specs))
11043 new_specs = specs;
11044 else
11045 while (specs)
11046 {
11047 tree spec;
11048 int i, len = 1;
11049 tree expanded_specs = NULL_TREE;
11050
11051 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11052 {
11053 /* Expand the pack expansion type. */
11054 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11055 args, complain,
11056 in_decl);
11057
11058 if (expanded_specs == error_mark_node)
11059 return error_mark_node;
11060 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11061 len = TREE_VEC_LENGTH (expanded_specs);
11062 else
11063 {
11064 /* We're substituting into a member template, so
11065 we got a TYPE_PACK_EXPANSION back. Add that
11066 expansion and move on. */
11067 gcc_assert (TREE_CODE (expanded_specs)
11068 == TYPE_PACK_EXPANSION);
11069 new_specs = add_exception_specifier (new_specs,
11070 expanded_specs,
11071 complain);
11072 specs = TREE_CHAIN (specs);
11073 continue;
11074 }
11075 }
11076
11077 for (i = 0; i < len; ++i)
11078 {
11079 if (expanded_specs)
11080 spec = TREE_VEC_ELT (expanded_specs, i);
11081 else
11082 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11083 if (spec == error_mark_node)
11084 return spec;
11085 new_specs = add_exception_specifier (new_specs, spec,
11086 complain);
11087 }
11088
11089 specs = TREE_CHAIN (specs);
11090 }
11091 }
11092 return new_specs;
11093 }
11094
11095 /* Take the tree structure T and replace template parameters used
11096 therein with the argument vector ARGS. IN_DECL is an associated
11097 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11098 Issue error and warning messages under control of COMPLAIN. Note
11099 that we must be relatively non-tolerant of extensions here, in
11100 order to preserve conformance; if we allow substitutions that
11101 should not be allowed, we may allow argument deductions that should
11102 not succeed, and therefore report ambiguous overload situations
11103 where there are none. In theory, we could allow the substitution,
11104 but indicate that it should have failed, and allow our caller to
11105 make sure that the right thing happens, but we don't try to do this
11106 yet.
11107
11108 This function is used for dealing with types, decls and the like;
11109 for expressions, use tsubst_expr or tsubst_copy. */
11110
11111 tree
11112 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11113 {
11114 enum tree_code code;
11115 tree type, r = NULL_TREE;
11116
11117 if (t == NULL_TREE || t == error_mark_node
11118 || t == integer_type_node
11119 || t == void_type_node
11120 || t == char_type_node
11121 || t == unknown_type_node
11122 || TREE_CODE (t) == NAMESPACE_DECL
11123 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11124 return t;
11125
11126 if (DECL_P (t))
11127 return tsubst_decl (t, args, complain);
11128
11129 if (args == NULL_TREE)
11130 return t;
11131
11132 code = TREE_CODE (t);
11133
11134 if (code == IDENTIFIER_NODE)
11135 type = IDENTIFIER_TYPE_VALUE (t);
11136 else
11137 type = TREE_TYPE (t);
11138
11139 gcc_assert (type != unknown_type_node);
11140
11141 /* Reuse typedefs. We need to do this to handle dependent attributes,
11142 such as attribute aligned. */
11143 if (TYPE_P (t)
11144 && typedef_variant_p (t))
11145 {
11146 tree decl = TYPE_NAME (t);
11147
11148 if (alias_template_specialization_p (t))
11149 {
11150 /* DECL represents an alias template and we want to
11151 instantiate it. */
11152 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11153 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11154 r = instantiate_alias_template (tmpl, gen_args, complain);
11155 }
11156 else if (DECL_CLASS_SCOPE_P (decl)
11157 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11158 && uses_template_parms (DECL_CONTEXT (decl)))
11159 {
11160 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11161 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11162 r = retrieve_specialization (tmpl, gen_args, 0);
11163 }
11164 else if (DECL_FUNCTION_SCOPE_P (decl)
11165 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11166 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11167 r = retrieve_local_specialization (decl);
11168 else
11169 /* The typedef is from a non-template context. */
11170 return t;
11171
11172 if (r)
11173 {
11174 r = TREE_TYPE (r);
11175 r = cp_build_qualified_type_real
11176 (r, cp_type_quals (t) | cp_type_quals (r),
11177 complain | tf_ignore_bad_quals);
11178 return r;
11179 }
11180 else
11181 {
11182 /* We don't have an instantiation yet, so drop the typedef. */
11183 int quals = cp_type_quals (t);
11184 t = DECL_ORIGINAL_TYPE (decl);
11185 t = cp_build_qualified_type_real (t, quals,
11186 complain | tf_ignore_bad_quals);
11187 }
11188 }
11189
11190 if (type
11191 && code != TYPENAME_TYPE
11192 && code != TEMPLATE_TYPE_PARM
11193 && code != IDENTIFIER_NODE
11194 && code != FUNCTION_TYPE
11195 && code != METHOD_TYPE)
11196 type = tsubst (type, args, complain, in_decl);
11197 if (type == error_mark_node)
11198 return error_mark_node;
11199
11200 switch (code)
11201 {
11202 case RECORD_TYPE:
11203 case UNION_TYPE:
11204 case ENUMERAL_TYPE:
11205 return tsubst_aggr_type (t, args, complain, in_decl,
11206 /*entering_scope=*/0);
11207
11208 case ERROR_MARK:
11209 case IDENTIFIER_NODE:
11210 case VOID_TYPE:
11211 case REAL_TYPE:
11212 case COMPLEX_TYPE:
11213 case VECTOR_TYPE:
11214 case BOOLEAN_TYPE:
11215 case NULLPTR_TYPE:
11216 case LANG_TYPE:
11217 return t;
11218
11219 case INTEGER_TYPE:
11220 if (t == integer_type_node)
11221 return t;
11222
11223 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11224 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11225 return t;
11226
11227 {
11228 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11229
11230 max = tsubst_expr (omax, args, complain, in_decl,
11231 /*integral_constant_expression_p=*/false);
11232
11233 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11234 needed. */
11235 if (TREE_CODE (max) == NOP_EXPR
11236 && TREE_SIDE_EFFECTS (omax)
11237 && !TREE_TYPE (max))
11238 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11239
11240 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11241 with TREE_SIDE_EFFECTS that indicates this is not an integral
11242 constant expression. */
11243 if (processing_template_decl
11244 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11245 {
11246 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11247 TREE_SIDE_EFFECTS (max) = 1;
11248 }
11249
11250 return compute_array_index_type (NULL_TREE, max, complain);
11251 }
11252
11253 case TEMPLATE_TYPE_PARM:
11254 case TEMPLATE_TEMPLATE_PARM:
11255 case BOUND_TEMPLATE_TEMPLATE_PARM:
11256 case TEMPLATE_PARM_INDEX:
11257 {
11258 int idx;
11259 int level;
11260 int levels;
11261 tree arg = NULL_TREE;
11262
11263 r = NULL_TREE;
11264
11265 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11266 template_parm_level_and_index (t, &level, &idx);
11267
11268 levels = TMPL_ARGS_DEPTH (args);
11269 if (level <= levels)
11270 {
11271 arg = TMPL_ARG (args, level, idx);
11272
11273 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11274 {
11275 /* See through ARGUMENT_PACK_SELECT arguments. */
11276 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11277 /* If the selected argument is an expansion E, that most
11278 likely means we were called from
11279 gen_elem_of_pack_expansion_instantiation during the
11280 substituting of pack an argument pack (which Ith
11281 element is a pack expansion, where I is
11282 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11283 In this case, the Ith element resulting from this
11284 substituting is going to be a pack expansion, which
11285 pattern is the pattern of E. Let's return the
11286 pattern of E, and
11287 gen_elem_of_pack_expansion_instantiation will
11288 build the resulting pack expansion from it. */
11289 if (PACK_EXPANSION_P (arg))
11290 arg = PACK_EXPANSION_PATTERN (arg);
11291 }
11292 }
11293
11294 if (arg == error_mark_node)
11295 return error_mark_node;
11296 else if (arg != NULL_TREE)
11297 {
11298 if (ARGUMENT_PACK_P (arg))
11299 /* If ARG is an argument pack, we don't actually want to
11300 perform a substitution here, because substitutions
11301 for argument packs are only done
11302 element-by-element. We can get to this point when
11303 substituting the type of a non-type template
11304 parameter pack, when that type actually contains
11305 template parameter packs from an outer template, e.g.,
11306
11307 template<typename... Types> struct A {
11308 template<Types... Values> struct B { };
11309 }; */
11310 return t;
11311
11312 if (code == TEMPLATE_TYPE_PARM)
11313 {
11314 int quals;
11315 gcc_assert (TYPE_P (arg));
11316
11317 quals = cp_type_quals (arg) | cp_type_quals (t);
11318
11319 return cp_build_qualified_type_real
11320 (arg, quals, complain | tf_ignore_bad_quals);
11321 }
11322 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11323 {
11324 /* We are processing a type constructed from a
11325 template template parameter. */
11326 tree argvec = tsubst (TYPE_TI_ARGS (t),
11327 args, complain, in_decl);
11328 if (argvec == error_mark_node)
11329 return error_mark_node;
11330
11331 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11332 || TREE_CODE (arg) == TEMPLATE_DECL
11333 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11334
11335 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11336 /* Consider this code:
11337
11338 template <template <class> class Template>
11339 struct Internal {
11340 template <class Arg> using Bind = Template<Arg>;
11341 };
11342
11343 template <template <class> class Template, class Arg>
11344 using Instantiate = Template<Arg>; //#0
11345
11346 template <template <class> class Template,
11347 class Argument>
11348 using Bind =
11349 Instantiate<Internal<Template>::template Bind,
11350 Argument>; //#1
11351
11352 When #1 is parsed, the
11353 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11354 parameter `Template' in #0 matches the
11355 UNBOUND_CLASS_TEMPLATE representing the argument
11356 `Internal<Template>::template Bind'; We then want
11357 to assemble the type `Bind<Argument>' that can't
11358 be fully created right now, because
11359 `Internal<Template>' not being complete, the Bind
11360 template cannot be looked up in that context. So
11361 we need to "store" `Bind<Argument>' for later
11362 when the context of Bind becomes complete. Let's
11363 store that in a TYPENAME_TYPE. */
11364 return make_typename_type (TYPE_CONTEXT (arg),
11365 build_nt (TEMPLATE_ID_EXPR,
11366 TYPE_IDENTIFIER (arg),
11367 argvec),
11368 typename_type,
11369 complain);
11370
11371 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11372 are resolving nested-types in the signature of a
11373 member function templates. Otherwise ARG is a
11374 TEMPLATE_DECL and is the real template to be
11375 instantiated. */
11376 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11377 arg = TYPE_NAME (arg);
11378
11379 r = lookup_template_class (arg,
11380 argvec, in_decl,
11381 DECL_CONTEXT (arg),
11382 /*entering_scope=*/0,
11383 complain);
11384 return cp_build_qualified_type_real
11385 (r, cp_type_quals (t), complain);
11386 }
11387 else
11388 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11389 return convert_from_reference (unshare_expr (arg));
11390 }
11391
11392 if (level == 1)
11393 /* This can happen during the attempted tsubst'ing in
11394 unify. This means that we don't yet have any information
11395 about the template parameter in question. */
11396 return t;
11397
11398 /* Early in template argument deduction substitution, we don't
11399 want to reduce the level of 'auto', or it will be confused
11400 with a normal template parm in subsequent deduction. */
11401 if (is_auto (t) && (complain & tf_partial))
11402 return t;
11403
11404 /* If we get here, we must have been looking at a parm for a
11405 more deeply nested template. Make a new version of this
11406 template parameter, but with a lower level. */
11407 switch (code)
11408 {
11409 case TEMPLATE_TYPE_PARM:
11410 case TEMPLATE_TEMPLATE_PARM:
11411 case BOUND_TEMPLATE_TEMPLATE_PARM:
11412 if (cp_type_quals (t))
11413 {
11414 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11415 r = cp_build_qualified_type_real
11416 (r, cp_type_quals (t),
11417 complain | (code == TEMPLATE_TYPE_PARM
11418 ? tf_ignore_bad_quals : 0));
11419 }
11420 else
11421 {
11422 r = copy_type (t);
11423 TEMPLATE_TYPE_PARM_INDEX (r)
11424 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11425 r, levels, args, complain);
11426 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11427 TYPE_MAIN_VARIANT (r) = r;
11428 TYPE_POINTER_TO (r) = NULL_TREE;
11429 TYPE_REFERENCE_TO (r) = NULL_TREE;
11430
11431 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11432 /* We have reduced the level of the template
11433 template parameter, but not the levels of its
11434 template parameters, so canonical_type_parameter
11435 will not be able to find the canonical template
11436 template parameter for this level. Thus, we
11437 require structural equality checking to compare
11438 TEMPLATE_TEMPLATE_PARMs. */
11439 SET_TYPE_STRUCTURAL_EQUALITY (r);
11440 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11441 SET_TYPE_STRUCTURAL_EQUALITY (r);
11442 else
11443 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11444
11445 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11446 {
11447 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11448 complain, in_decl);
11449 if (argvec == error_mark_node)
11450 return error_mark_node;
11451
11452 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11453 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11454 }
11455 }
11456 break;
11457
11458 case TEMPLATE_PARM_INDEX:
11459 r = reduce_template_parm_level (t, type, levels, args, complain);
11460 break;
11461
11462 default:
11463 gcc_unreachable ();
11464 }
11465
11466 return r;
11467 }
11468
11469 case TREE_LIST:
11470 {
11471 tree purpose, value, chain;
11472
11473 if (t == void_list_node)
11474 return t;
11475
11476 purpose = TREE_PURPOSE (t);
11477 if (purpose)
11478 {
11479 purpose = tsubst (purpose, args, complain, in_decl);
11480 if (purpose == error_mark_node)
11481 return error_mark_node;
11482 }
11483 value = TREE_VALUE (t);
11484 if (value)
11485 {
11486 value = tsubst (value, args, complain, in_decl);
11487 if (value == error_mark_node)
11488 return error_mark_node;
11489 }
11490 chain = TREE_CHAIN (t);
11491 if (chain && chain != void_type_node)
11492 {
11493 chain = tsubst (chain, args, complain, in_decl);
11494 if (chain == error_mark_node)
11495 return error_mark_node;
11496 }
11497 if (purpose == TREE_PURPOSE (t)
11498 && value == TREE_VALUE (t)
11499 && chain == TREE_CHAIN (t))
11500 return t;
11501 return hash_tree_cons (purpose, value, chain);
11502 }
11503
11504 case TREE_BINFO:
11505 /* We should never be tsubsting a binfo. */
11506 gcc_unreachable ();
11507
11508 case TREE_VEC:
11509 /* A vector of template arguments. */
11510 gcc_assert (!type);
11511 return tsubst_template_args (t, args, complain, in_decl);
11512
11513 case POINTER_TYPE:
11514 case REFERENCE_TYPE:
11515 {
11516 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11517 return t;
11518
11519 /* [temp.deduct]
11520
11521 Type deduction may fail for any of the following
11522 reasons:
11523
11524 -- Attempting to create a pointer to reference type.
11525 -- Attempting to create a reference to a reference type or
11526 a reference to void.
11527
11528 Core issue 106 says that creating a reference to a reference
11529 during instantiation is no longer a cause for failure. We
11530 only enforce this check in strict C++98 mode. */
11531 if ((TREE_CODE (type) == REFERENCE_TYPE
11532 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11533 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11534 {
11535 static location_t last_loc;
11536
11537 /* We keep track of the last time we issued this error
11538 message to avoid spewing a ton of messages during a
11539 single bad template instantiation. */
11540 if (complain & tf_error
11541 && last_loc != input_location)
11542 {
11543 if (TREE_CODE (type) == VOID_TYPE)
11544 error ("forming reference to void");
11545 else if (code == POINTER_TYPE)
11546 error ("forming pointer to reference type %qT", type);
11547 else
11548 error ("forming reference to reference type %qT", type);
11549 last_loc = input_location;
11550 }
11551
11552 return error_mark_node;
11553 }
11554 else if (code == POINTER_TYPE)
11555 {
11556 r = build_pointer_type (type);
11557 if (TREE_CODE (type) == METHOD_TYPE)
11558 r = build_ptrmemfunc_type (r);
11559 }
11560 else if (TREE_CODE (type) == REFERENCE_TYPE)
11561 /* In C++0x, during template argument substitution, when there is an
11562 attempt to create a reference to a reference type, reference
11563 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11564
11565 "If a template-argument for a template-parameter T names a type
11566 that is a reference to a type A, an attempt to create the type
11567 'lvalue reference to cv T' creates the type 'lvalue reference to
11568 A,' while an attempt to create the type type rvalue reference to
11569 cv T' creates the type T"
11570 */
11571 r = cp_build_reference_type
11572 (TREE_TYPE (type),
11573 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11574 else
11575 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11576 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11577
11578 if (r != error_mark_node)
11579 /* Will this ever be needed for TYPE_..._TO values? */
11580 layout_type (r);
11581
11582 return r;
11583 }
11584 case OFFSET_TYPE:
11585 {
11586 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11587 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11588 {
11589 /* [temp.deduct]
11590
11591 Type deduction may fail for any of the following
11592 reasons:
11593
11594 -- Attempting to create "pointer to member of T" when T
11595 is not a class type. */
11596 if (complain & tf_error)
11597 error ("creating pointer to member of non-class type %qT", r);
11598 return error_mark_node;
11599 }
11600 if (TREE_CODE (type) == REFERENCE_TYPE)
11601 {
11602 if (complain & tf_error)
11603 error ("creating pointer to member reference type %qT", type);
11604 return error_mark_node;
11605 }
11606 if (TREE_CODE (type) == VOID_TYPE)
11607 {
11608 if (complain & tf_error)
11609 error ("creating pointer to member of type void");
11610 return error_mark_node;
11611 }
11612 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11613 if (TREE_CODE (type) == FUNCTION_TYPE)
11614 {
11615 /* The type of the implicit object parameter gets its
11616 cv-qualifiers from the FUNCTION_TYPE. */
11617 tree memptr;
11618 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11619 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11620 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11621 complain);
11622 }
11623 else
11624 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11625 cp_type_quals (t),
11626 complain);
11627 }
11628 case FUNCTION_TYPE:
11629 case METHOD_TYPE:
11630 {
11631 tree fntype;
11632 tree specs;
11633 fntype = tsubst_function_type (t, args, complain, in_decl);
11634 if (fntype == error_mark_node)
11635 return error_mark_node;
11636
11637 /* Substitute the exception specification. */
11638 specs = tsubst_exception_specification (t, args, complain,
11639 in_decl, /*defer_ok*/true);
11640 if (specs == error_mark_node)
11641 return error_mark_node;
11642 if (specs)
11643 fntype = build_exception_variant (fntype, specs);
11644 return fntype;
11645 }
11646 case ARRAY_TYPE:
11647 {
11648 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11649 if (domain == error_mark_node)
11650 return error_mark_node;
11651
11652 /* As an optimization, we avoid regenerating the array type if
11653 it will obviously be the same as T. */
11654 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11655 return t;
11656
11657 /* These checks should match the ones in grokdeclarator.
11658
11659 [temp.deduct]
11660
11661 The deduction may fail for any of the following reasons:
11662
11663 -- Attempting to create an array with an element type that
11664 is void, a function type, or a reference type, or [DR337]
11665 an abstract class type. */
11666 if (TREE_CODE (type) == VOID_TYPE
11667 || TREE_CODE (type) == FUNCTION_TYPE
11668 || TREE_CODE (type) == REFERENCE_TYPE)
11669 {
11670 if (complain & tf_error)
11671 error ("creating array of %qT", type);
11672 return error_mark_node;
11673 }
11674
11675 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11676 return error_mark_node;
11677
11678 r = build_cplus_array_type (type, domain);
11679
11680 if (TYPE_USER_ALIGN (t))
11681 {
11682 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11683 TYPE_USER_ALIGN (r) = 1;
11684 }
11685
11686 return r;
11687 }
11688
11689 case TYPENAME_TYPE:
11690 {
11691 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11692 in_decl, /*entering_scope=*/1);
11693 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11694 complain, in_decl);
11695
11696 if (ctx == error_mark_node || f == error_mark_node)
11697 return error_mark_node;
11698
11699 if (!MAYBE_CLASS_TYPE_P (ctx))
11700 {
11701 if (complain & tf_error)
11702 error ("%qT is not a class, struct, or union type", ctx);
11703 return error_mark_node;
11704 }
11705 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11706 {
11707 /* Normally, make_typename_type does not require that the CTX
11708 have complete type in order to allow things like:
11709
11710 template <class T> struct S { typename S<T>::X Y; };
11711
11712 But, such constructs have already been resolved by this
11713 point, so here CTX really should have complete type, unless
11714 it's a partial instantiation. */
11715 ctx = complete_type (ctx);
11716 if (!COMPLETE_TYPE_P (ctx))
11717 {
11718 if (complain & tf_error)
11719 cxx_incomplete_type_error (NULL_TREE, ctx);
11720 return error_mark_node;
11721 }
11722 }
11723
11724 f = make_typename_type (ctx, f, typename_type,
11725 complain | tf_keep_type_decl);
11726 if (f == error_mark_node)
11727 return f;
11728 if (TREE_CODE (f) == TYPE_DECL)
11729 {
11730 complain |= tf_ignore_bad_quals;
11731 f = TREE_TYPE (f);
11732 }
11733
11734 if (TREE_CODE (f) != TYPENAME_TYPE)
11735 {
11736 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11737 {
11738 if (complain & tf_error)
11739 error ("%qT resolves to %qT, which is not an enumeration type",
11740 t, f);
11741 else
11742 return error_mark_node;
11743 }
11744 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11745 {
11746 if (complain & tf_error)
11747 error ("%qT resolves to %qT, which is is not a class type",
11748 t, f);
11749 else
11750 return error_mark_node;
11751 }
11752 }
11753
11754 return cp_build_qualified_type_real
11755 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11756 }
11757
11758 case UNBOUND_CLASS_TEMPLATE:
11759 {
11760 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11761 in_decl, /*entering_scope=*/1);
11762 tree name = TYPE_IDENTIFIER (t);
11763 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11764
11765 if (ctx == error_mark_node || name == error_mark_node)
11766 return error_mark_node;
11767
11768 if (parm_list)
11769 parm_list = tsubst_template_parms (parm_list, args, complain);
11770 return make_unbound_class_template (ctx, name, parm_list, complain);
11771 }
11772
11773 case TYPEOF_TYPE:
11774 {
11775 tree type;
11776
11777 ++cp_unevaluated_operand;
11778 ++c_inhibit_evaluation_warnings;
11779
11780 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11781 complain, in_decl,
11782 /*integral_constant_expression_p=*/false);
11783
11784 --cp_unevaluated_operand;
11785 --c_inhibit_evaluation_warnings;
11786
11787 type = finish_typeof (type);
11788 return cp_build_qualified_type_real (type,
11789 cp_type_quals (t)
11790 | cp_type_quals (type),
11791 complain);
11792 }
11793
11794 case DECLTYPE_TYPE:
11795 {
11796 tree type;
11797
11798 ++cp_unevaluated_operand;
11799 ++c_inhibit_evaluation_warnings;
11800
11801 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11802 complain|tf_decltype, in_decl,
11803 /*integral_constant_expression_p=*/false);
11804
11805 --cp_unevaluated_operand;
11806 --c_inhibit_evaluation_warnings;
11807
11808 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11809 type = lambda_capture_field_type (type);
11810 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11811 type = lambda_proxy_type (type);
11812 else
11813 type = finish_decltype_type
11814 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11815 return cp_build_qualified_type_real (type,
11816 cp_type_quals (t)
11817 | cp_type_quals (type),
11818 complain);
11819 }
11820
11821 case UNDERLYING_TYPE:
11822 {
11823 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11824 complain, in_decl);
11825 return finish_underlying_type (type);
11826 }
11827
11828 case TYPE_ARGUMENT_PACK:
11829 case NONTYPE_ARGUMENT_PACK:
11830 {
11831 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11832 tree packed_out =
11833 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11834 args,
11835 complain,
11836 in_decl);
11837 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11838
11839 /* For template nontype argument packs, also substitute into
11840 the type. */
11841 if (code == NONTYPE_ARGUMENT_PACK)
11842 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11843
11844 return r;
11845 }
11846 break;
11847
11848 case INTEGER_CST:
11849 case REAL_CST:
11850 case STRING_CST:
11851 case PLUS_EXPR:
11852 case MINUS_EXPR:
11853 case NEGATE_EXPR:
11854 case NOP_EXPR:
11855 case INDIRECT_REF:
11856 case ADDR_EXPR:
11857 case CALL_EXPR:
11858 case ARRAY_REF:
11859 case SCOPE_REF:
11860 /* We should use one of the expression tsubsts for these codes. */
11861 gcc_unreachable ();
11862
11863 default:
11864 sorry ("use of %qs in template", tree_code_name [(int) code]);
11865 return error_mark_node;
11866 }
11867 }
11868
11869 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11870 type of the expression on the left-hand side of the "." or "->"
11871 operator. */
11872
11873 static tree
11874 tsubst_baselink (tree baselink, tree object_type,
11875 tree args, tsubst_flags_t complain, tree in_decl)
11876 {
11877 tree name;
11878 tree qualifying_scope;
11879 tree fns;
11880 tree optype;
11881 tree template_args = 0;
11882 bool template_id_p = false;
11883 bool qualified = BASELINK_QUALIFIED_P (baselink);
11884
11885 /* A baselink indicates a function from a base class. Both the
11886 BASELINK_ACCESS_BINFO and the base class referenced may
11887 indicate bases of the template class, rather than the
11888 instantiated class. In addition, lookups that were not
11889 ambiguous before may be ambiguous now. Therefore, we perform
11890 the lookup again. */
11891 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11892 qualifying_scope = tsubst (qualifying_scope, args,
11893 complain, in_decl);
11894 fns = BASELINK_FUNCTIONS (baselink);
11895 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11896 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11897 {
11898 template_id_p = true;
11899 template_args = TREE_OPERAND (fns, 1);
11900 fns = TREE_OPERAND (fns, 0);
11901 if (template_args)
11902 template_args = tsubst_template_args (template_args, args,
11903 complain, in_decl);
11904 }
11905 name = DECL_NAME (get_first_fn (fns));
11906 if (IDENTIFIER_TYPENAME_P (name))
11907 name = mangle_conv_op_name_for_type (optype);
11908 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11909 if (!baselink)
11910 return error_mark_node;
11911
11912 /* If lookup found a single function, mark it as used at this
11913 point. (If it lookup found multiple functions the one selected
11914 later by overload resolution will be marked as used at that
11915 point.) */
11916 if (BASELINK_P (baselink))
11917 fns = BASELINK_FUNCTIONS (baselink);
11918 if (!template_id_p && !really_overloaded_fn (fns))
11919 mark_used (OVL_CURRENT (fns));
11920
11921 /* Add back the template arguments, if present. */
11922 if (BASELINK_P (baselink) && template_id_p)
11923 BASELINK_FUNCTIONS (baselink)
11924 = build_nt (TEMPLATE_ID_EXPR,
11925 BASELINK_FUNCTIONS (baselink),
11926 template_args);
11927 /* Update the conversion operator type. */
11928 BASELINK_OPTYPE (baselink) = optype;
11929
11930 if (!object_type)
11931 object_type = current_class_type;
11932
11933 if (qualified)
11934 baselink = adjust_result_of_qualified_name_lookup (baselink,
11935 qualifying_scope,
11936 object_type);
11937 return baselink;
11938 }
11939
11940 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11941 true if the qualified-id will be a postfix-expression in-and-of
11942 itself; false if more of the postfix-expression follows the
11943 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11944 of "&". */
11945
11946 static tree
11947 tsubst_qualified_id (tree qualified_id, tree args,
11948 tsubst_flags_t complain, tree in_decl,
11949 bool done, bool address_p)
11950 {
11951 tree expr;
11952 tree scope;
11953 tree name;
11954 bool is_template;
11955 tree template_args;
11956 location_t loc = UNKNOWN_LOCATION;
11957
11958 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11959
11960 /* Figure out what name to look up. */
11961 name = TREE_OPERAND (qualified_id, 1);
11962 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11963 {
11964 is_template = true;
11965 loc = EXPR_LOCATION (name);
11966 template_args = TREE_OPERAND (name, 1);
11967 if (template_args)
11968 template_args = tsubst_template_args (template_args, args,
11969 complain, in_decl);
11970 name = TREE_OPERAND (name, 0);
11971 }
11972 else
11973 {
11974 is_template = false;
11975 template_args = NULL_TREE;
11976 }
11977
11978 /* Substitute into the qualifying scope. When there are no ARGS, we
11979 are just trying to simplify a non-dependent expression. In that
11980 case the qualifying scope may be dependent, and, in any case,
11981 substituting will not help. */
11982 scope = TREE_OPERAND (qualified_id, 0);
11983 if (args)
11984 {
11985 scope = tsubst (scope, args, complain, in_decl);
11986 expr = tsubst_copy (name, args, complain, in_decl);
11987 }
11988 else
11989 expr = name;
11990
11991 if (dependent_scope_p (scope))
11992 {
11993 if (is_template)
11994 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11995 return build_qualified_name (NULL_TREE, scope, expr,
11996 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11997 }
11998
11999 if (!BASELINK_P (name) && !DECL_P (expr))
12000 {
12001 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12002 {
12003 /* A BIT_NOT_EXPR is used to represent a destructor. */
12004 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12005 {
12006 error ("qualifying type %qT does not match destructor name ~%qT",
12007 scope, TREE_OPERAND (expr, 0));
12008 expr = error_mark_node;
12009 }
12010 else
12011 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12012 /*is_type_p=*/0, false);
12013 }
12014 else
12015 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12016 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12017 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12018 {
12019 if (complain & tf_error)
12020 {
12021 error ("dependent-name %qE is parsed as a non-type, but "
12022 "instantiation yields a type", qualified_id);
12023 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12024 }
12025 return error_mark_node;
12026 }
12027 }
12028
12029 if (DECL_P (expr))
12030 {
12031 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12032 scope);
12033 /* Remember that there was a reference to this entity. */
12034 mark_used (expr);
12035 }
12036
12037 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12038 {
12039 if (complain & tf_error)
12040 qualified_name_lookup_error (scope,
12041 TREE_OPERAND (qualified_id, 1),
12042 expr, input_location);
12043 return error_mark_node;
12044 }
12045
12046 if (is_template)
12047 expr = lookup_template_function (expr, template_args);
12048
12049 if (expr == error_mark_node && complain & tf_error)
12050 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12051 expr, input_location);
12052 else if (TYPE_P (scope))
12053 {
12054 expr = (adjust_result_of_qualified_name_lookup
12055 (expr, scope, current_nonlambda_class_type ()));
12056 expr = (finish_qualified_id_expr
12057 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12058 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12059 /*template_arg_p=*/false));
12060 }
12061
12062 /* Expressions do not generally have reference type. */
12063 if (TREE_CODE (expr) != SCOPE_REF
12064 /* However, if we're about to form a pointer-to-member, we just
12065 want the referenced member referenced. */
12066 && TREE_CODE (expr) != OFFSET_REF)
12067 expr = convert_from_reference (expr);
12068
12069 return expr;
12070 }
12071
12072 /* Like tsubst, but deals with expressions. This function just replaces
12073 template parms; to finish processing the resultant expression, use
12074 tsubst_copy_and_build or tsubst_expr. */
12075
12076 static tree
12077 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12078 {
12079 enum tree_code code;
12080 tree r;
12081
12082 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12083 return t;
12084
12085 code = TREE_CODE (t);
12086
12087 switch (code)
12088 {
12089 case PARM_DECL:
12090 r = retrieve_local_specialization (t);
12091
12092 if (r == NULL_TREE)
12093 {
12094 /* We get here for a use of 'this' in an NSDMI. */
12095 if (DECL_NAME (t) == this_identifier
12096 && at_function_scope_p ()
12097 && DECL_CONSTRUCTOR_P (current_function_decl))
12098 return current_class_ptr;
12099
12100 /* This can happen for a parameter name used later in a function
12101 declaration (such as in a late-specified return type). Just
12102 make a dummy decl, since it's only used for its type. */
12103 gcc_assert (cp_unevaluated_operand != 0);
12104 r = tsubst_decl (t, args, complain);
12105 /* Give it the template pattern as its context; its true context
12106 hasn't been instantiated yet and this is good enough for
12107 mangling. */
12108 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12109 }
12110
12111 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12112 r = ARGUMENT_PACK_SELECT_ARG (r);
12113 mark_used (r);
12114 return r;
12115
12116 case CONST_DECL:
12117 {
12118 tree enum_type;
12119 tree v;
12120
12121 if (DECL_TEMPLATE_PARM_P (t))
12122 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12123 /* There is no need to substitute into namespace-scope
12124 enumerators. */
12125 if (DECL_NAMESPACE_SCOPE_P (t))
12126 return t;
12127 /* If ARGS is NULL, then T is known to be non-dependent. */
12128 if (args == NULL_TREE)
12129 return integral_constant_value (t);
12130
12131 /* Unfortunately, we cannot just call lookup_name here.
12132 Consider:
12133
12134 template <int I> int f() {
12135 enum E { a = I };
12136 struct S { void g() { E e = a; } };
12137 };
12138
12139 When we instantiate f<7>::S::g(), say, lookup_name is not
12140 clever enough to find f<7>::a. */
12141 enum_type
12142 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12143 /*entering_scope=*/0);
12144
12145 for (v = TYPE_VALUES (enum_type);
12146 v != NULL_TREE;
12147 v = TREE_CHAIN (v))
12148 if (TREE_PURPOSE (v) == DECL_NAME (t))
12149 return TREE_VALUE (v);
12150
12151 /* We didn't find the name. That should never happen; if
12152 name-lookup found it during preliminary parsing, we
12153 should find it again here during instantiation. */
12154 gcc_unreachable ();
12155 }
12156 return t;
12157
12158 case FIELD_DECL:
12159 if (DECL_CONTEXT (t))
12160 {
12161 tree ctx;
12162
12163 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12164 /*entering_scope=*/1);
12165 if (ctx != DECL_CONTEXT (t))
12166 {
12167 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12168 if (!r)
12169 {
12170 if (complain & tf_error)
12171 error ("using invalid field %qD", t);
12172 return error_mark_node;
12173 }
12174 return r;
12175 }
12176 }
12177
12178 return t;
12179
12180 case VAR_DECL:
12181 case FUNCTION_DECL:
12182 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12183 || local_variable_p (t))
12184 t = tsubst (t, args, complain, in_decl);
12185 mark_used (t);
12186 return t;
12187
12188 case NAMESPACE_DECL:
12189 return t;
12190
12191 case OVERLOAD:
12192 /* An OVERLOAD will always be a non-dependent overload set; an
12193 overload set from function scope will just be represented with an
12194 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12195 gcc_assert (!uses_template_parms (t));
12196 return t;
12197
12198 case BASELINK:
12199 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
12200
12201 case TEMPLATE_DECL:
12202 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12203 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12204 args, complain, in_decl);
12205 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12206 return tsubst (t, args, complain, in_decl);
12207 else if (DECL_CLASS_SCOPE_P (t)
12208 && uses_template_parms (DECL_CONTEXT (t)))
12209 {
12210 /* Template template argument like the following example need
12211 special treatment:
12212
12213 template <template <class> class TT> struct C {};
12214 template <class T> struct D {
12215 template <class U> struct E {};
12216 C<E> c; // #1
12217 };
12218 D<int> d; // #2
12219
12220 We are processing the template argument `E' in #1 for
12221 the template instantiation #2. Originally, `E' is a
12222 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12223 have to substitute this with one having context `D<int>'. */
12224
12225 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12226 return lookup_field (context, DECL_NAME(t), 0, false);
12227 }
12228 else
12229 /* Ordinary template template argument. */
12230 return t;
12231
12232 case CAST_EXPR:
12233 case REINTERPRET_CAST_EXPR:
12234 case CONST_CAST_EXPR:
12235 case STATIC_CAST_EXPR:
12236 case DYNAMIC_CAST_EXPR:
12237 case IMPLICIT_CONV_EXPR:
12238 case CONVERT_EXPR:
12239 case NOP_EXPR:
12240 return build1
12241 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12242 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12243
12244 case SIZEOF_EXPR:
12245 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12246 {
12247
12248 tree expanded, op = TREE_OPERAND (t, 0);
12249 int len = 0;
12250
12251 if (SIZEOF_EXPR_TYPE_P (t))
12252 op = TREE_TYPE (op);
12253
12254 ++cp_unevaluated_operand;
12255 ++c_inhibit_evaluation_warnings;
12256 /* We only want to compute the number of arguments. */
12257 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12258 --cp_unevaluated_operand;
12259 --c_inhibit_evaluation_warnings;
12260
12261 if (TREE_CODE (expanded) == TREE_VEC)
12262 len = TREE_VEC_LENGTH (expanded);
12263
12264 if (expanded == error_mark_node)
12265 return error_mark_node;
12266 else if (PACK_EXPANSION_P (expanded)
12267 || (TREE_CODE (expanded) == TREE_VEC
12268 && len > 0
12269 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12270 {
12271 if (TREE_CODE (expanded) == TREE_VEC)
12272 expanded = TREE_VEC_ELT (expanded, len - 1);
12273
12274 if (TYPE_P (expanded))
12275 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12276 complain & tf_error);
12277 else
12278 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12279 complain & tf_error);
12280 }
12281 else
12282 return build_int_cst (size_type_node, len);
12283 }
12284 if (SIZEOF_EXPR_TYPE_P (t))
12285 {
12286 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12287 args, complain, in_decl);
12288 r = build1 (NOP_EXPR, r, error_mark_node);
12289 r = build1 (SIZEOF_EXPR,
12290 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12291 SIZEOF_EXPR_TYPE_P (r) = 1;
12292 return r;
12293 }
12294 /* Fall through */
12295
12296 case INDIRECT_REF:
12297 case NEGATE_EXPR:
12298 case TRUTH_NOT_EXPR:
12299 case BIT_NOT_EXPR:
12300 case ADDR_EXPR:
12301 case UNARY_PLUS_EXPR: /* Unary + */
12302 case ALIGNOF_EXPR:
12303 case AT_ENCODE_EXPR:
12304 case ARROW_EXPR:
12305 case THROW_EXPR:
12306 case TYPEID_EXPR:
12307 case REALPART_EXPR:
12308 case IMAGPART_EXPR:
12309 return build1
12310 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12311 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12312
12313 case COMPONENT_REF:
12314 {
12315 tree object;
12316 tree name;
12317
12318 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12319 name = TREE_OPERAND (t, 1);
12320 if (TREE_CODE (name) == BIT_NOT_EXPR)
12321 {
12322 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12323 complain, in_decl);
12324 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12325 }
12326 else if (TREE_CODE (name) == SCOPE_REF
12327 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12328 {
12329 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12330 complain, in_decl);
12331 name = TREE_OPERAND (name, 1);
12332 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12333 complain, in_decl);
12334 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12335 name = build_qualified_name (/*type=*/NULL_TREE,
12336 base, name,
12337 /*template_p=*/false);
12338 }
12339 else if (BASELINK_P (name))
12340 name = tsubst_baselink (name,
12341 non_reference (TREE_TYPE (object)),
12342 args, complain,
12343 in_decl);
12344 else
12345 name = tsubst_copy (name, args, complain, in_decl);
12346 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12347 }
12348
12349 case PLUS_EXPR:
12350 case MINUS_EXPR:
12351 case MULT_EXPR:
12352 case TRUNC_DIV_EXPR:
12353 case CEIL_DIV_EXPR:
12354 case FLOOR_DIV_EXPR:
12355 case ROUND_DIV_EXPR:
12356 case EXACT_DIV_EXPR:
12357 case BIT_AND_EXPR:
12358 case BIT_IOR_EXPR:
12359 case BIT_XOR_EXPR:
12360 case TRUNC_MOD_EXPR:
12361 case FLOOR_MOD_EXPR:
12362 case TRUTH_ANDIF_EXPR:
12363 case TRUTH_ORIF_EXPR:
12364 case TRUTH_AND_EXPR:
12365 case TRUTH_OR_EXPR:
12366 case RSHIFT_EXPR:
12367 case LSHIFT_EXPR:
12368 case RROTATE_EXPR:
12369 case LROTATE_EXPR:
12370 case EQ_EXPR:
12371 case NE_EXPR:
12372 case MAX_EXPR:
12373 case MIN_EXPR:
12374 case LE_EXPR:
12375 case GE_EXPR:
12376 case LT_EXPR:
12377 case GT_EXPR:
12378 case COMPOUND_EXPR:
12379 case DOTSTAR_EXPR:
12380 case MEMBER_REF:
12381 case PREDECREMENT_EXPR:
12382 case PREINCREMENT_EXPR:
12383 case POSTDECREMENT_EXPR:
12384 case POSTINCREMENT_EXPR:
12385 return build_nt
12386 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12387 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12388
12389 case SCOPE_REF:
12390 return build_qualified_name (/*type=*/NULL_TREE,
12391 tsubst_copy (TREE_OPERAND (t, 0),
12392 args, complain, in_decl),
12393 tsubst_copy (TREE_OPERAND (t, 1),
12394 args, complain, in_decl),
12395 QUALIFIED_NAME_IS_TEMPLATE (t));
12396
12397 case ARRAY_REF:
12398 return build_nt
12399 (ARRAY_REF,
12400 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12401 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12402 NULL_TREE, NULL_TREE);
12403
12404 case CALL_EXPR:
12405 {
12406 int n = VL_EXP_OPERAND_LENGTH (t);
12407 tree result = build_vl_exp (CALL_EXPR, n);
12408 int i;
12409 for (i = 0; i < n; i++)
12410 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12411 complain, in_decl);
12412 return result;
12413 }
12414
12415 case COND_EXPR:
12416 case MODOP_EXPR:
12417 case PSEUDO_DTOR_EXPR:
12418 {
12419 r = build_nt
12420 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12421 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12422 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12423 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12424 return r;
12425 }
12426
12427 case NEW_EXPR:
12428 {
12429 r = build_nt
12430 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12431 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12432 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12433 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12434 return r;
12435 }
12436
12437 case DELETE_EXPR:
12438 {
12439 r = build_nt
12440 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12441 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12442 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12443 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12444 return r;
12445 }
12446
12447 case TEMPLATE_ID_EXPR:
12448 {
12449 /* Substituted template arguments */
12450 tree fn = TREE_OPERAND (t, 0);
12451 tree targs = TREE_OPERAND (t, 1);
12452
12453 fn = tsubst_copy (fn, args, complain, in_decl);
12454 if (targs)
12455 targs = tsubst_template_args (targs, args, complain, in_decl);
12456
12457 return lookup_template_function (fn, targs);
12458 }
12459
12460 case TREE_LIST:
12461 {
12462 tree purpose, value, chain;
12463
12464 if (t == void_list_node)
12465 return t;
12466
12467 purpose = TREE_PURPOSE (t);
12468 if (purpose)
12469 purpose = tsubst_copy (purpose, args, complain, in_decl);
12470 value = TREE_VALUE (t);
12471 if (value)
12472 value = tsubst_copy (value, args, complain, in_decl);
12473 chain = TREE_CHAIN (t);
12474 if (chain && chain != void_type_node)
12475 chain = tsubst_copy (chain, args, complain, in_decl);
12476 if (purpose == TREE_PURPOSE (t)
12477 && value == TREE_VALUE (t)
12478 && chain == TREE_CHAIN (t))
12479 return t;
12480 return tree_cons (purpose, value, chain);
12481 }
12482
12483 case RECORD_TYPE:
12484 case UNION_TYPE:
12485 case ENUMERAL_TYPE:
12486 case INTEGER_TYPE:
12487 case TEMPLATE_TYPE_PARM:
12488 case TEMPLATE_TEMPLATE_PARM:
12489 case BOUND_TEMPLATE_TEMPLATE_PARM:
12490 case TEMPLATE_PARM_INDEX:
12491 case POINTER_TYPE:
12492 case REFERENCE_TYPE:
12493 case OFFSET_TYPE:
12494 case FUNCTION_TYPE:
12495 case METHOD_TYPE:
12496 case ARRAY_TYPE:
12497 case TYPENAME_TYPE:
12498 case UNBOUND_CLASS_TEMPLATE:
12499 case TYPEOF_TYPE:
12500 case DECLTYPE_TYPE:
12501 case TYPE_DECL:
12502 return tsubst (t, args, complain, in_decl);
12503
12504 case IDENTIFIER_NODE:
12505 if (IDENTIFIER_TYPENAME_P (t))
12506 {
12507 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12508 return mangle_conv_op_name_for_type (new_type);
12509 }
12510 else
12511 return t;
12512
12513 case CONSTRUCTOR:
12514 /* This is handled by tsubst_copy_and_build. */
12515 gcc_unreachable ();
12516
12517 case VA_ARG_EXPR:
12518 return build_x_va_arg (EXPR_LOCATION (t),
12519 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12520 in_decl),
12521 tsubst (TREE_TYPE (t), args, complain, in_decl));
12522
12523 case CLEANUP_POINT_EXPR:
12524 /* We shouldn't have built any of these during initial template
12525 generation. Instead, they should be built during instantiation
12526 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12527 gcc_unreachable ();
12528
12529 case OFFSET_REF:
12530 r = build2
12531 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12532 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12533 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12534 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12535 mark_used (TREE_OPERAND (r, 1));
12536 return r;
12537
12538 case EXPR_PACK_EXPANSION:
12539 error ("invalid use of pack expansion expression");
12540 return error_mark_node;
12541
12542 case NONTYPE_ARGUMENT_PACK:
12543 error ("use %<...%> to expand argument pack");
12544 return error_mark_node;
12545
12546 case INTEGER_CST:
12547 case REAL_CST:
12548 case STRING_CST:
12549 case COMPLEX_CST:
12550 {
12551 /* Instantiate any typedefs in the type. */
12552 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12553 r = fold_convert (type, t);
12554 gcc_assert (TREE_CODE (r) == code);
12555 return r;
12556 }
12557
12558 case PTRMEM_CST:
12559 /* These can sometimes show up in a partial instantiation, but never
12560 involve template parms. */
12561 gcc_assert (!uses_template_parms (t));
12562 return t;
12563
12564 default:
12565 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12566 gcc_checking_assert (false);
12567 return t;
12568 }
12569 }
12570
12571 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12572
12573 static tree
12574 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12575 tree in_decl)
12576 {
12577 tree new_clauses = NULL, nc, oc;
12578
12579 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12580 {
12581 nc = copy_node (oc);
12582 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12583 new_clauses = nc;
12584
12585 switch (OMP_CLAUSE_CODE (nc))
12586 {
12587 case OMP_CLAUSE_LASTPRIVATE:
12588 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12589 {
12590 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12591 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12592 in_decl, /*integral_constant_expression_p=*/false);
12593 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12594 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12595 }
12596 /* FALLTHRU */
12597 case OMP_CLAUSE_PRIVATE:
12598 case OMP_CLAUSE_SHARED:
12599 case OMP_CLAUSE_FIRSTPRIVATE:
12600 case OMP_CLAUSE_REDUCTION:
12601 case OMP_CLAUSE_COPYIN:
12602 case OMP_CLAUSE_COPYPRIVATE:
12603 case OMP_CLAUSE_IF:
12604 case OMP_CLAUSE_NUM_THREADS:
12605 case OMP_CLAUSE_SCHEDULE:
12606 case OMP_CLAUSE_COLLAPSE:
12607 case OMP_CLAUSE_FINAL:
12608 OMP_CLAUSE_OPERAND (nc, 0)
12609 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12610 in_decl, /*integral_constant_expression_p=*/false);
12611 break;
12612 case OMP_CLAUSE_NOWAIT:
12613 case OMP_CLAUSE_ORDERED:
12614 case OMP_CLAUSE_DEFAULT:
12615 case OMP_CLAUSE_UNTIED:
12616 case OMP_CLAUSE_MERGEABLE:
12617 break;
12618 default:
12619 gcc_unreachable ();
12620 }
12621 }
12622
12623 return finish_omp_clauses (nreverse (new_clauses));
12624 }
12625
12626 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12627
12628 static tree
12629 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12630 tree in_decl)
12631 {
12632 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12633
12634 tree purpose, value, chain;
12635
12636 if (t == NULL)
12637 return t;
12638
12639 if (TREE_CODE (t) != TREE_LIST)
12640 return tsubst_copy_and_build (t, args, complain, in_decl,
12641 /*function_p=*/false,
12642 /*integral_constant_expression_p=*/false);
12643
12644 if (t == void_list_node)
12645 return t;
12646
12647 purpose = TREE_PURPOSE (t);
12648 if (purpose)
12649 purpose = RECUR (purpose);
12650 value = TREE_VALUE (t);
12651 if (value)
12652 {
12653 if (TREE_CODE (value) != LABEL_DECL)
12654 value = RECUR (value);
12655 else
12656 {
12657 value = lookup_label (DECL_NAME (value));
12658 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12659 TREE_USED (value) = 1;
12660 }
12661 }
12662 chain = TREE_CHAIN (t);
12663 if (chain && chain != void_type_node)
12664 chain = RECUR (chain);
12665 return tree_cons (purpose, value, chain);
12666 #undef RECUR
12667 }
12668
12669 /* Substitute one OMP_FOR iterator. */
12670
12671 static void
12672 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12673 tree condv, tree incrv, tree *clauses,
12674 tree args, tsubst_flags_t complain, tree in_decl,
12675 bool integral_constant_expression_p)
12676 {
12677 #define RECUR(NODE) \
12678 tsubst_expr ((NODE), args, complain, in_decl, \
12679 integral_constant_expression_p)
12680 tree decl, init, cond, incr;
12681 bool init_decl;
12682
12683 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12684 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12685 decl = TREE_OPERAND (init, 0);
12686 init = TREE_OPERAND (init, 1);
12687 /* Do this before substituting into decl to handle 'auto'. */
12688 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12689 init = RECUR (init);
12690 decl = RECUR (decl);
12691 if (init_decl)
12692 {
12693 gcc_assert (!processing_template_decl);
12694 init = DECL_INITIAL (decl);
12695 DECL_INITIAL (decl) = NULL_TREE;
12696 }
12697
12698 gcc_assert (!type_dependent_expression_p (decl));
12699
12700 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12701 {
12702 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12703 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12704 if (TREE_CODE (incr) == MODIFY_EXPR)
12705 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12706 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12707 RECUR (TREE_OPERAND (incr, 1)),
12708 complain);
12709 else
12710 incr = RECUR (incr);
12711 TREE_VEC_ELT (declv, i) = decl;
12712 TREE_VEC_ELT (initv, i) = init;
12713 TREE_VEC_ELT (condv, i) = cond;
12714 TREE_VEC_ELT (incrv, i) = incr;
12715 return;
12716 }
12717
12718 if (init && !init_decl)
12719 {
12720 tree c;
12721 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12722 {
12723 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12724 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12725 && OMP_CLAUSE_DECL (c) == decl)
12726 break;
12727 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12728 && OMP_CLAUSE_DECL (c) == decl)
12729 error ("iteration variable %qD should not be firstprivate", decl);
12730 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12731 && OMP_CLAUSE_DECL (c) == decl)
12732 error ("iteration variable %qD should not be reduction", decl);
12733 }
12734 if (c == NULL)
12735 {
12736 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12737 OMP_CLAUSE_DECL (c) = decl;
12738 c = finish_omp_clauses (c);
12739 if (c)
12740 {
12741 OMP_CLAUSE_CHAIN (c) = *clauses;
12742 *clauses = c;
12743 }
12744 }
12745 }
12746 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12747 if (COMPARISON_CLASS_P (cond))
12748 cond = build2 (TREE_CODE (cond), boolean_type_node,
12749 RECUR (TREE_OPERAND (cond, 0)),
12750 RECUR (TREE_OPERAND (cond, 1)));
12751 else
12752 cond = RECUR (cond);
12753 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12754 switch (TREE_CODE (incr))
12755 {
12756 case PREINCREMENT_EXPR:
12757 case PREDECREMENT_EXPR:
12758 case POSTINCREMENT_EXPR:
12759 case POSTDECREMENT_EXPR:
12760 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12761 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12762 break;
12763 case MODIFY_EXPR:
12764 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12765 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12766 {
12767 tree rhs = TREE_OPERAND (incr, 1);
12768 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12769 RECUR (TREE_OPERAND (incr, 0)),
12770 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12771 RECUR (TREE_OPERAND (rhs, 0)),
12772 RECUR (TREE_OPERAND (rhs, 1))));
12773 }
12774 else
12775 incr = RECUR (incr);
12776 break;
12777 case MODOP_EXPR:
12778 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12779 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12780 {
12781 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12782 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12783 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12784 TREE_TYPE (decl), lhs,
12785 RECUR (TREE_OPERAND (incr, 2))));
12786 }
12787 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12788 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12789 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12790 {
12791 tree rhs = TREE_OPERAND (incr, 2);
12792 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12793 RECUR (TREE_OPERAND (incr, 0)),
12794 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12795 RECUR (TREE_OPERAND (rhs, 0)),
12796 RECUR (TREE_OPERAND (rhs, 1))));
12797 }
12798 else
12799 incr = RECUR (incr);
12800 break;
12801 default:
12802 incr = RECUR (incr);
12803 break;
12804 }
12805
12806 TREE_VEC_ELT (declv, i) = decl;
12807 TREE_VEC_ELT (initv, i) = init;
12808 TREE_VEC_ELT (condv, i) = cond;
12809 TREE_VEC_ELT (incrv, i) = incr;
12810 #undef RECUR
12811 }
12812
12813 /* Like tsubst_copy for expressions, etc. but also does semantic
12814 processing. */
12815
12816 static tree
12817 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12818 bool integral_constant_expression_p)
12819 {
12820 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12821 #define RECUR(NODE) \
12822 tsubst_expr ((NODE), args, complain, in_decl, \
12823 integral_constant_expression_p)
12824
12825 tree stmt, tmp;
12826 tree r;
12827 location_t loc;
12828
12829 if (t == NULL_TREE || t == error_mark_node)
12830 return t;
12831
12832 loc = input_location;
12833 if (EXPR_HAS_LOCATION (t))
12834 input_location = EXPR_LOCATION (t);
12835 if (STATEMENT_CODE_P (TREE_CODE (t)))
12836 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12837
12838 switch (TREE_CODE (t))
12839 {
12840 case STATEMENT_LIST:
12841 {
12842 tree_stmt_iterator i;
12843 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12844 RECUR (tsi_stmt (i));
12845 break;
12846 }
12847
12848 case CTOR_INITIALIZER:
12849 finish_mem_initializers (tsubst_initializer_list
12850 (TREE_OPERAND (t, 0), args));
12851 break;
12852
12853 case RETURN_EXPR:
12854 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12855 break;
12856
12857 case EXPR_STMT:
12858 tmp = RECUR (EXPR_STMT_EXPR (t));
12859 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12860 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12861 else
12862 finish_expr_stmt (tmp);
12863 break;
12864
12865 case USING_STMT:
12866 do_using_directive (USING_STMT_NAMESPACE (t));
12867 break;
12868
12869 case DECL_EXPR:
12870 {
12871 tree decl, pattern_decl;
12872 tree init;
12873
12874 pattern_decl = decl = DECL_EXPR_DECL (t);
12875 if (TREE_CODE (decl) == LABEL_DECL)
12876 finish_label_decl (DECL_NAME (decl));
12877 else if (TREE_CODE (decl) == USING_DECL)
12878 {
12879 tree scope = USING_DECL_SCOPE (decl);
12880 tree name = DECL_NAME (decl);
12881 tree decl;
12882
12883 scope = tsubst (scope, args, complain, in_decl);
12884 decl = lookup_qualified_name (scope, name,
12885 /*is_type_p=*/false,
12886 /*complain=*/false);
12887 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12888 qualified_name_lookup_error (scope, name, decl, input_location);
12889 else
12890 do_local_using_decl (decl, scope, name);
12891 }
12892 else
12893 {
12894 init = DECL_INITIAL (decl);
12895 decl = tsubst (decl, args, complain, in_decl);
12896 if (decl != error_mark_node)
12897 {
12898 /* By marking the declaration as instantiated, we avoid
12899 trying to instantiate it. Since instantiate_decl can't
12900 handle local variables, and since we've already done
12901 all that needs to be done, that's the right thing to
12902 do. */
12903 if (TREE_CODE (decl) == VAR_DECL)
12904 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12905 if (TREE_CODE (decl) == VAR_DECL
12906 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12907 /* Anonymous aggregates are a special case. */
12908 finish_anon_union (decl);
12909 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12910 {
12911 DECL_CONTEXT (decl) = current_function_decl;
12912 if (DECL_NAME (decl) == this_identifier)
12913 {
12914 tree lam = DECL_CONTEXT (current_function_decl);
12915 lam = CLASSTYPE_LAMBDA_EXPR (lam);
12916 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
12917 }
12918 insert_capture_proxy (decl);
12919 }
12920 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12921 /* We already did a pushtag. */;
12922 else
12923 {
12924 int const_init = false;
12925 maybe_push_decl (decl);
12926 if (TREE_CODE (decl) == VAR_DECL
12927 && DECL_PRETTY_FUNCTION_P (decl))
12928 {
12929 /* For __PRETTY_FUNCTION__ we have to adjust the
12930 initializer. */
12931 const char *const name
12932 = cxx_printable_name (current_function_decl, 2);
12933 init = cp_fname_init (name, &TREE_TYPE (decl));
12934 }
12935 else
12936 {
12937 tree t = RECUR (init);
12938
12939 if (init && !t)
12940 {
12941 /* If we had an initializer but it
12942 instantiated to nothing,
12943 value-initialize the object. This will
12944 only occur when the initializer was a
12945 pack expansion where the parameter packs
12946 used in that expansion were of length
12947 zero. */
12948 init = build_value_init (TREE_TYPE (decl),
12949 complain);
12950 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12951 init = get_target_expr_sfinae (init, complain);
12952 }
12953 else
12954 init = t;
12955 }
12956
12957 if (TREE_CODE (decl) == VAR_DECL)
12958 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12959 (pattern_decl));
12960 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12961 }
12962 }
12963 }
12964
12965 break;
12966 }
12967
12968 case FOR_STMT:
12969 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12970 RECUR (FOR_INIT_STMT (t));
12971 finish_for_init_stmt (stmt);
12972 tmp = RECUR (FOR_COND (t));
12973 finish_for_cond (tmp, stmt);
12974 tmp = RECUR (FOR_EXPR (t));
12975 finish_for_expr (tmp, stmt);
12976 RECUR (FOR_BODY (t));
12977 finish_for_stmt (stmt);
12978 break;
12979
12980 case RANGE_FOR_STMT:
12981 {
12982 tree decl, expr;
12983 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12984 decl = RANGE_FOR_DECL (t);
12985 decl = tsubst (decl, args, complain, in_decl);
12986 maybe_push_decl (decl);
12987 expr = RECUR (RANGE_FOR_EXPR (t));
12988 stmt = cp_convert_range_for (stmt, decl, expr);
12989 RECUR (RANGE_FOR_BODY (t));
12990 finish_for_stmt (stmt);
12991 }
12992 break;
12993
12994 case WHILE_STMT:
12995 stmt = begin_while_stmt ();
12996 tmp = RECUR (WHILE_COND (t));
12997 finish_while_stmt_cond (tmp, stmt);
12998 RECUR (WHILE_BODY (t));
12999 finish_while_stmt (stmt);
13000 break;
13001
13002 case DO_STMT:
13003 stmt = begin_do_stmt ();
13004 RECUR (DO_BODY (t));
13005 finish_do_body (stmt);
13006 tmp = RECUR (DO_COND (t));
13007 finish_do_stmt (tmp, stmt);
13008 break;
13009
13010 case IF_STMT:
13011 stmt = begin_if_stmt ();
13012 tmp = RECUR (IF_COND (t));
13013 finish_if_stmt_cond (tmp, stmt);
13014 RECUR (THEN_CLAUSE (t));
13015 finish_then_clause (stmt);
13016
13017 if (ELSE_CLAUSE (t))
13018 {
13019 begin_else_clause (stmt);
13020 RECUR (ELSE_CLAUSE (t));
13021 finish_else_clause (stmt);
13022 }
13023
13024 finish_if_stmt (stmt);
13025 break;
13026
13027 case BIND_EXPR:
13028 if (BIND_EXPR_BODY_BLOCK (t))
13029 stmt = begin_function_body ();
13030 else
13031 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13032 ? BCS_TRY_BLOCK : 0);
13033
13034 RECUR (BIND_EXPR_BODY (t));
13035
13036 if (BIND_EXPR_BODY_BLOCK (t))
13037 finish_function_body (stmt);
13038 else
13039 finish_compound_stmt (stmt);
13040 break;
13041
13042 case BREAK_STMT:
13043 finish_break_stmt ();
13044 break;
13045
13046 case CONTINUE_STMT:
13047 finish_continue_stmt ();
13048 break;
13049
13050 case SWITCH_STMT:
13051 stmt = begin_switch_stmt ();
13052 tmp = RECUR (SWITCH_STMT_COND (t));
13053 finish_switch_cond (tmp, stmt);
13054 RECUR (SWITCH_STMT_BODY (t));
13055 finish_switch_stmt (stmt);
13056 break;
13057
13058 case CASE_LABEL_EXPR:
13059 finish_case_label (EXPR_LOCATION (t),
13060 RECUR (CASE_LOW (t)),
13061 RECUR (CASE_HIGH (t)));
13062 break;
13063
13064 case LABEL_EXPR:
13065 {
13066 tree decl = LABEL_EXPR_LABEL (t);
13067 tree label;
13068
13069 label = finish_label_stmt (DECL_NAME (decl));
13070 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13071 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13072 }
13073 break;
13074
13075 case GOTO_EXPR:
13076 tmp = GOTO_DESTINATION (t);
13077 if (TREE_CODE (tmp) != LABEL_DECL)
13078 /* Computed goto's must be tsubst'd into. On the other hand,
13079 non-computed gotos must not be; the identifier in question
13080 will have no binding. */
13081 tmp = RECUR (tmp);
13082 else
13083 tmp = DECL_NAME (tmp);
13084 finish_goto_stmt (tmp);
13085 break;
13086
13087 case ASM_EXPR:
13088 tmp = finish_asm_stmt
13089 (ASM_VOLATILE_P (t),
13090 RECUR (ASM_STRING (t)),
13091 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13092 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13093 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13094 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13095 {
13096 tree asm_expr = tmp;
13097 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13098 asm_expr = TREE_OPERAND (asm_expr, 0);
13099 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13100 }
13101 break;
13102
13103 case TRY_BLOCK:
13104 if (CLEANUP_P (t))
13105 {
13106 stmt = begin_try_block ();
13107 RECUR (TRY_STMTS (t));
13108 finish_cleanup_try_block (stmt);
13109 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13110 }
13111 else
13112 {
13113 tree compound_stmt = NULL_TREE;
13114
13115 if (FN_TRY_BLOCK_P (t))
13116 stmt = begin_function_try_block (&compound_stmt);
13117 else
13118 stmt = begin_try_block ();
13119
13120 RECUR (TRY_STMTS (t));
13121
13122 if (FN_TRY_BLOCK_P (t))
13123 finish_function_try_block (stmt);
13124 else
13125 finish_try_block (stmt);
13126
13127 RECUR (TRY_HANDLERS (t));
13128 if (FN_TRY_BLOCK_P (t))
13129 finish_function_handler_sequence (stmt, compound_stmt);
13130 else
13131 finish_handler_sequence (stmt);
13132 }
13133 break;
13134
13135 case HANDLER:
13136 {
13137 tree decl = HANDLER_PARMS (t);
13138
13139 if (decl)
13140 {
13141 decl = tsubst (decl, args, complain, in_decl);
13142 /* Prevent instantiate_decl from trying to instantiate
13143 this variable. We've already done all that needs to be
13144 done. */
13145 if (decl != error_mark_node)
13146 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13147 }
13148 stmt = begin_handler ();
13149 finish_handler_parms (decl, stmt);
13150 RECUR (HANDLER_BODY (t));
13151 finish_handler (stmt);
13152 }
13153 break;
13154
13155 case TAG_DEFN:
13156 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13157 if (CLASS_TYPE_P (tmp))
13158 {
13159 /* Local classes are not independent templates; they are
13160 instantiated along with their containing function. And this
13161 way we don't have to deal with pushing out of one local class
13162 to instantiate a member of another local class. */
13163 tree fn;
13164 /* Closures are handled by the LAMBDA_EXPR. */
13165 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13166 complete_type (tmp);
13167 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13168 if (!DECL_ARTIFICIAL (fn))
13169 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13170 }
13171 break;
13172
13173 case STATIC_ASSERT:
13174 {
13175 tree condition;
13176
13177 ++c_inhibit_evaluation_warnings;
13178 condition =
13179 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13180 args,
13181 complain, in_decl,
13182 /*integral_constant_expression_p=*/true);
13183 --c_inhibit_evaluation_warnings;
13184
13185 finish_static_assert (condition,
13186 STATIC_ASSERT_MESSAGE (t),
13187 STATIC_ASSERT_SOURCE_LOCATION (t),
13188 /*member_p=*/false);
13189 }
13190 break;
13191
13192 case OMP_PARALLEL:
13193 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
13194 args, complain, in_decl);
13195 stmt = begin_omp_parallel ();
13196 RECUR (OMP_PARALLEL_BODY (t));
13197 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13198 = OMP_PARALLEL_COMBINED (t);
13199 break;
13200
13201 case OMP_TASK:
13202 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
13203 args, complain, in_decl);
13204 stmt = begin_omp_task ();
13205 RECUR (OMP_TASK_BODY (t));
13206 finish_omp_task (tmp, stmt);
13207 break;
13208
13209 case OMP_FOR:
13210 {
13211 tree clauses, body, pre_body;
13212 tree declv, initv, condv, incrv;
13213 int i;
13214
13215 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
13216 args, complain, in_decl);
13217 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13218 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13219 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13220 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13221
13222 stmt = begin_omp_structured_block ();
13223
13224 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13225 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13226 &clauses, args, complain, in_decl,
13227 integral_constant_expression_p);
13228
13229 pre_body = push_stmt_list ();
13230 RECUR (OMP_FOR_PRE_BODY (t));
13231 pre_body = pop_stmt_list (pre_body);
13232
13233 body = push_stmt_list ();
13234 RECUR (OMP_FOR_BODY (t));
13235 body = pop_stmt_list (body);
13236
13237 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
13238 body, pre_body, clauses);
13239
13240 add_stmt (finish_omp_structured_block (stmt));
13241 }
13242 break;
13243
13244 case OMP_SECTIONS:
13245 case OMP_SINGLE:
13246 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
13247 stmt = push_stmt_list ();
13248 RECUR (OMP_BODY (t));
13249 stmt = pop_stmt_list (stmt);
13250
13251 t = copy_node (t);
13252 OMP_BODY (t) = stmt;
13253 OMP_CLAUSES (t) = tmp;
13254 add_stmt (t);
13255 break;
13256
13257 case OMP_SECTION:
13258 case OMP_CRITICAL:
13259 case OMP_MASTER:
13260 case OMP_ORDERED:
13261 stmt = push_stmt_list ();
13262 RECUR (OMP_BODY (t));
13263 stmt = pop_stmt_list (stmt);
13264
13265 t = copy_node (t);
13266 OMP_BODY (t) = stmt;
13267 add_stmt (t);
13268 break;
13269
13270 case OMP_ATOMIC:
13271 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13272 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13273 {
13274 tree op1 = TREE_OPERAND (t, 1);
13275 tree rhs1 = NULL_TREE;
13276 tree lhs, rhs;
13277 if (TREE_CODE (op1) == COMPOUND_EXPR)
13278 {
13279 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13280 op1 = TREE_OPERAND (op1, 1);
13281 }
13282 lhs = RECUR (TREE_OPERAND (op1, 0));
13283 rhs = RECUR (TREE_OPERAND (op1, 1));
13284 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13285 NULL_TREE, NULL_TREE, rhs1);
13286 }
13287 else
13288 {
13289 tree op1 = TREE_OPERAND (t, 1);
13290 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13291 tree rhs1 = NULL_TREE;
13292 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13293 enum tree_code opcode = NOP_EXPR;
13294 if (code == OMP_ATOMIC_READ)
13295 {
13296 v = RECUR (TREE_OPERAND (op1, 0));
13297 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13298 }
13299 else if (code == OMP_ATOMIC_CAPTURE_OLD
13300 || code == OMP_ATOMIC_CAPTURE_NEW)
13301 {
13302 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13303 v = RECUR (TREE_OPERAND (op1, 0));
13304 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13305 if (TREE_CODE (op11) == COMPOUND_EXPR)
13306 {
13307 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13308 op11 = TREE_OPERAND (op11, 1);
13309 }
13310 lhs = RECUR (TREE_OPERAND (op11, 0));
13311 rhs = RECUR (TREE_OPERAND (op11, 1));
13312 opcode = TREE_CODE (op11);
13313 }
13314 else
13315 {
13316 code = OMP_ATOMIC;
13317 lhs = RECUR (TREE_OPERAND (op1, 0));
13318 rhs = RECUR (TREE_OPERAND (op1, 1));
13319 }
13320 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13321 }
13322 break;
13323
13324 case TRANSACTION_EXPR:
13325 {
13326 int flags = 0;
13327 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13328 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13329
13330 if (TRANSACTION_EXPR_IS_STMT (t))
13331 {
13332 tree body = TRANSACTION_EXPR_BODY (t);
13333 tree noex = NULL_TREE;
13334 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13335 {
13336 noex = MUST_NOT_THROW_COND (body);
13337 if (noex == NULL_TREE)
13338 noex = boolean_true_node;
13339 body = TREE_OPERAND (body, 0);
13340 }
13341 stmt = begin_transaction_stmt (input_location, NULL, flags);
13342 RECUR (body);
13343 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13344 }
13345 else
13346 {
13347 stmt = build_transaction_expr (EXPR_LOCATION (t),
13348 RECUR (TRANSACTION_EXPR_BODY (t)),
13349 flags, NULL_TREE);
13350 RETURN (stmt);
13351 }
13352 }
13353 break;
13354
13355 case MUST_NOT_THROW_EXPR:
13356 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13357 RECUR (MUST_NOT_THROW_COND (t))));
13358
13359 case EXPR_PACK_EXPANSION:
13360 error ("invalid use of pack expansion expression");
13361 RETURN (error_mark_node);
13362
13363 case NONTYPE_ARGUMENT_PACK:
13364 error ("use %<...%> to expand argument pack");
13365 RETURN (error_mark_node);
13366
13367 case COMPOUND_EXPR:
13368 tmp = RECUR (TREE_OPERAND (t, 0));
13369 if (tmp == NULL_TREE)
13370 /* If the first operand was a statement, we're done with it. */
13371 RETURN (RECUR (TREE_OPERAND (t, 1)));
13372 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13373 RECUR (TREE_OPERAND (t, 1)),
13374 complain));
13375
13376 default:
13377 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13378
13379 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13380 /*function_p=*/false,
13381 integral_constant_expression_p));
13382 }
13383
13384 RETURN (NULL_TREE);
13385 out:
13386 input_location = loc;
13387 return r;
13388 #undef RECUR
13389 #undef RETURN
13390 }
13391
13392 /* T is a postfix-expression that is not being used in a function
13393 call. Return the substituted version of T. */
13394
13395 static tree
13396 tsubst_non_call_postfix_expression (tree t, tree args,
13397 tsubst_flags_t complain,
13398 tree in_decl)
13399 {
13400 if (TREE_CODE (t) == SCOPE_REF)
13401 t = tsubst_qualified_id (t, args, complain, in_decl,
13402 /*done=*/false, /*address_p=*/false);
13403 else
13404 t = tsubst_copy_and_build (t, args, complain, in_decl,
13405 /*function_p=*/false,
13406 /*integral_constant_expression_p=*/false);
13407
13408 return t;
13409 }
13410
13411 /* Like tsubst but deals with expressions and performs semantic
13412 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13413
13414 tree
13415 tsubst_copy_and_build (tree t,
13416 tree args,
13417 tsubst_flags_t complain,
13418 tree in_decl,
13419 bool function_p,
13420 bool integral_constant_expression_p)
13421 {
13422 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13423 #define RECUR(NODE) \
13424 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13425 /*function_p=*/false, \
13426 integral_constant_expression_p)
13427
13428 tree retval, op1;
13429 location_t loc;
13430
13431 if (t == NULL_TREE || t == error_mark_node)
13432 return t;
13433
13434 loc = input_location;
13435 if (EXPR_HAS_LOCATION (t))
13436 input_location = EXPR_LOCATION (t);
13437
13438 /* N3276 decltype magic only applies to calls at the top level or on the
13439 right side of a comma. */
13440 if (TREE_CODE (t) != CALL_EXPR
13441 && TREE_CODE (t) != COMPOUND_EXPR)
13442 complain &= ~tf_decltype;
13443
13444 switch (TREE_CODE (t))
13445 {
13446 case USING_DECL:
13447 t = DECL_NAME (t);
13448 /* Fall through. */
13449 case IDENTIFIER_NODE:
13450 {
13451 tree decl;
13452 cp_id_kind idk;
13453 bool non_integral_constant_expression_p;
13454 const char *error_msg;
13455
13456 if (IDENTIFIER_TYPENAME_P (t))
13457 {
13458 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13459 t = mangle_conv_op_name_for_type (new_type);
13460 }
13461
13462 /* Look up the name. */
13463 decl = lookup_name (t);
13464
13465 /* By convention, expressions use ERROR_MARK_NODE to indicate
13466 failure, not NULL_TREE. */
13467 if (decl == NULL_TREE)
13468 decl = error_mark_node;
13469
13470 decl = finish_id_expression (t, decl, NULL_TREE,
13471 &idk,
13472 integral_constant_expression_p,
13473 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13474 &non_integral_constant_expression_p,
13475 /*template_p=*/false,
13476 /*done=*/true,
13477 /*address_p=*/false,
13478 /*template_arg_p=*/false,
13479 &error_msg,
13480 input_location);
13481 if (error_msg)
13482 error (error_msg);
13483 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13484 {
13485 if (complain & tf_error)
13486 unqualified_name_lookup_error (decl);
13487 decl = error_mark_node;
13488 }
13489 RETURN (decl);
13490 }
13491
13492 case TEMPLATE_ID_EXPR:
13493 {
13494 tree object;
13495 tree templ = RECUR (TREE_OPERAND (t, 0));
13496 tree targs = TREE_OPERAND (t, 1);
13497
13498 if (targs)
13499 targs = tsubst_template_args (targs, args, complain, in_decl);
13500
13501 if (TREE_CODE (templ) == COMPONENT_REF)
13502 {
13503 object = TREE_OPERAND (templ, 0);
13504 templ = TREE_OPERAND (templ, 1);
13505 }
13506 else
13507 object = NULL_TREE;
13508 templ = lookup_template_function (templ, targs);
13509
13510 if (object)
13511 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13512 object, templ, NULL_TREE));
13513 else
13514 RETURN (baselink_for_fns (templ));
13515 }
13516
13517 case INDIRECT_REF:
13518 {
13519 tree r = RECUR (TREE_OPERAND (t, 0));
13520
13521 if (REFERENCE_REF_P (t))
13522 {
13523 /* A type conversion to reference type will be enclosed in
13524 such an indirect ref, but the substitution of the cast
13525 will have also added such an indirect ref. */
13526 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13527 r = convert_from_reference (r);
13528 }
13529 else
13530 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13531 RETURN (r);
13532 }
13533
13534 case NOP_EXPR:
13535 RETURN (build_nop
13536 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13537 RECUR (TREE_OPERAND (t, 0))));
13538
13539 case IMPLICIT_CONV_EXPR:
13540 {
13541 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13542 tree expr = RECUR (TREE_OPERAND (t, 0));
13543 int flags = LOOKUP_IMPLICIT;
13544 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13545 flags = LOOKUP_NORMAL;
13546 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13547 flags));
13548 }
13549
13550 case CONVERT_EXPR:
13551 RETURN (build1
13552 (CONVERT_EXPR,
13553 tsubst (TREE_TYPE (t), args, complain, in_decl),
13554 RECUR (TREE_OPERAND (t, 0))));
13555
13556 case CAST_EXPR:
13557 case REINTERPRET_CAST_EXPR:
13558 case CONST_CAST_EXPR:
13559 case DYNAMIC_CAST_EXPR:
13560 case STATIC_CAST_EXPR:
13561 {
13562 tree type;
13563 tree op, r = NULL_TREE;
13564
13565 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13566 if (integral_constant_expression_p
13567 && !cast_valid_in_integral_constant_expression_p (type))
13568 {
13569 if (complain & tf_error)
13570 error ("a cast to a type other than an integral or "
13571 "enumeration type cannot appear in a constant-expression");
13572 RETURN (error_mark_node);
13573 }
13574
13575 op = RECUR (TREE_OPERAND (t, 0));
13576
13577 ++c_inhibit_evaluation_warnings;
13578 switch (TREE_CODE (t))
13579 {
13580 case CAST_EXPR:
13581 r = build_functional_cast (type, op, complain);
13582 break;
13583 case REINTERPRET_CAST_EXPR:
13584 r = build_reinterpret_cast (type, op, complain);
13585 break;
13586 case CONST_CAST_EXPR:
13587 r = build_const_cast (type, op, complain);
13588 break;
13589 case DYNAMIC_CAST_EXPR:
13590 r = build_dynamic_cast (type, op, complain);
13591 break;
13592 case STATIC_CAST_EXPR:
13593 r = build_static_cast (type, op, complain);
13594 break;
13595 default:
13596 gcc_unreachable ();
13597 }
13598 --c_inhibit_evaluation_warnings;
13599
13600 RETURN (r);
13601 }
13602
13603 case POSTDECREMENT_EXPR:
13604 case POSTINCREMENT_EXPR:
13605 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13606 args, complain, in_decl);
13607 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
13608
13609 case PREDECREMENT_EXPR:
13610 case PREINCREMENT_EXPR:
13611 case NEGATE_EXPR:
13612 case BIT_NOT_EXPR:
13613 case ABS_EXPR:
13614 case TRUTH_NOT_EXPR:
13615 case UNARY_PLUS_EXPR: /* Unary + */
13616 case REALPART_EXPR:
13617 case IMAGPART_EXPR:
13618 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
13619 RECUR (TREE_OPERAND (t, 0)), complain));
13620
13621 case FIX_TRUNC_EXPR:
13622 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13623 0, complain));
13624
13625 case ADDR_EXPR:
13626 op1 = TREE_OPERAND (t, 0);
13627 if (TREE_CODE (op1) == LABEL_DECL)
13628 RETURN (finish_label_address_expr (DECL_NAME (op1),
13629 EXPR_LOCATION (op1)));
13630 if (TREE_CODE (op1) == SCOPE_REF)
13631 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13632 /*done=*/true, /*address_p=*/true);
13633 else
13634 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13635 in_decl);
13636 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
13637
13638 case PLUS_EXPR:
13639 case MINUS_EXPR:
13640 case MULT_EXPR:
13641 case TRUNC_DIV_EXPR:
13642 case CEIL_DIV_EXPR:
13643 case FLOOR_DIV_EXPR:
13644 case ROUND_DIV_EXPR:
13645 case EXACT_DIV_EXPR:
13646 case BIT_AND_EXPR:
13647 case BIT_IOR_EXPR:
13648 case BIT_XOR_EXPR:
13649 case TRUNC_MOD_EXPR:
13650 case FLOOR_MOD_EXPR:
13651 case TRUTH_ANDIF_EXPR:
13652 case TRUTH_ORIF_EXPR:
13653 case TRUTH_AND_EXPR:
13654 case TRUTH_OR_EXPR:
13655 case RSHIFT_EXPR:
13656 case LSHIFT_EXPR:
13657 case RROTATE_EXPR:
13658 case LROTATE_EXPR:
13659 case EQ_EXPR:
13660 case NE_EXPR:
13661 case MAX_EXPR:
13662 case MIN_EXPR:
13663 case LE_EXPR:
13664 case GE_EXPR:
13665 case LT_EXPR:
13666 case GT_EXPR:
13667 case MEMBER_REF:
13668 case DOTSTAR_EXPR:
13669 {
13670 tree r;
13671
13672 ++c_inhibit_evaluation_warnings;
13673
13674 r = build_x_binary_op
13675 (input_location, TREE_CODE (t),
13676 RECUR (TREE_OPERAND (t, 0)),
13677 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13678 ? ERROR_MARK
13679 : TREE_CODE (TREE_OPERAND (t, 0))),
13680 RECUR (TREE_OPERAND (t, 1)),
13681 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13682 ? ERROR_MARK
13683 : TREE_CODE (TREE_OPERAND (t, 1))),
13684 /*overload=*/NULL,
13685 complain);
13686 if (EXPR_P (r) && TREE_NO_WARNING (t))
13687 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13688
13689 --c_inhibit_evaluation_warnings;
13690
13691 RETURN (r);
13692 }
13693
13694 case SCOPE_REF:
13695 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13696 /*address_p=*/false));
13697 case ARRAY_REF:
13698 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13699 args, complain, in_decl);
13700 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
13701 RECUR (TREE_OPERAND (t, 1)), complain));
13702
13703 case SIZEOF_EXPR:
13704 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13705 RETURN (tsubst_copy (t, args, complain, in_decl));
13706 /* Fall through */
13707
13708 case ALIGNOF_EXPR:
13709 {
13710 tree r;
13711
13712 op1 = TREE_OPERAND (t, 0);
13713 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
13714 op1 = TREE_TYPE (op1);
13715 if (!args)
13716 {
13717 /* When there are no ARGS, we are trying to evaluate a
13718 non-dependent expression from the parser. Trying to do
13719 the substitutions may not work. */
13720 if (!TYPE_P (op1))
13721 op1 = TREE_TYPE (op1);
13722 }
13723 else
13724 {
13725 ++cp_unevaluated_operand;
13726 ++c_inhibit_evaluation_warnings;
13727 if (TYPE_P (op1))
13728 op1 = tsubst (op1, args, complain, in_decl);
13729 else
13730 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13731 /*function_p=*/false,
13732 /*integral_constant_expression_p=*/
13733 false);
13734 --cp_unevaluated_operand;
13735 --c_inhibit_evaluation_warnings;
13736 }
13737 if (TYPE_P (op1))
13738 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13739 complain & tf_error);
13740 else
13741 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13742 complain & tf_error);
13743 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
13744 {
13745 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
13746 {
13747 if (!processing_template_decl && TYPE_P (op1))
13748 {
13749 r = build_min (SIZEOF_EXPR, size_type_node,
13750 build1 (NOP_EXPR, op1, error_mark_node));
13751 SIZEOF_EXPR_TYPE_P (r) = 1;
13752 }
13753 else
13754 r = build_min (SIZEOF_EXPR, size_type_node, op1);
13755 TREE_SIDE_EFFECTS (r) = 0;
13756 TREE_READONLY (r) = 1;
13757 }
13758 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
13759 }
13760 RETURN (r);
13761 }
13762
13763 case AT_ENCODE_EXPR:
13764 {
13765 op1 = TREE_OPERAND (t, 0);
13766 ++cp_unevaluated_operand;
13767 ++c_inhibit_evaluation_warnings;
13768 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13769 /*function_p=*/false,
13770 /*integral_constant_expression_p=*/false);
13771 --cp_unevaluated_operand;
13772 --c_inhibit_evaluation_warnings;
13773 RETURN (objc_build_encode_expr (op1));
13774 }
13775
13776 case NOEXCEPT_EXPR:
13777 op1 = TREE_OPERAND (t, 0);
13778 ++cp_unevaluated_operand;
13779 ++c_inhibit_evaluation_warnings;
13780 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13781 /*function_p=*/false,
13782 /*integral_constant_expression_p=*/false);
13783 --cp_unevaluated_operand;
13784 --c_inhibit_evaluation_warnings;
13785 RETURN (finish_noexcept_expr (op1, complain));
13786
13787 case MODOP_EXPR:
13788 {
13789 tree r = build_x_modify_expr
13790 (EXPR_LOCATION (t),
13791 RECUR (TREE_OPERAND (t, 0)),
13792 TREE_CODE (TREE_OPERAND (t, 1)),
13793 RECUR (TREE_OPERAND (t, 2)),
13794 complain);
13795 /* TREE_NO_WARNING must be set if either the expression was
13796 parenthesized or it uses an operator such as >>= rather
13797 than plain assignment. In the former case, it was already
13798 set and must be copied. In the latter case,
13799 build_x_modify_expr sets it and it must not be reset
13800 here. */
13801 if (TREE_NO_WARNING (t))
13802 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13803 RETURN (r);
13804 }
13805
13806 case ARROW_EXPR:
13807 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13808 args, complain, in_decl);
13809 /* Remember that there was a reference to this entity. */
13810 if (DECL_P (op1))
13811 mark_used (op1);
13812 RETURN (build_x_arrow (input_location, op1, complain));
13813
13814 case NEW_EXPR:
13815 {
13816 tree placement = RECUR (TREE_OPERAND (t, 0));
13817 tree init = RECUR (TREE_OPERAND (t, 3));
13818 vec<tree, va_gc> *placement_vec;
13819 vec<tree, va_gc> *init_vec;
13820 tree ret;
13821
13822 if (placement == NULL_TREE)
13823 placement_vec = NULL;
13824 else
13825 {
13826 placement_vec = make_tree_vector ();
13827 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13828 vec_safe_push (placement_vec, TREE_VALUE (placement));
13829 }
13830
13831 /* If there was an initializer in the original tree, but it
13832 instantiated to an empty list, then we should pass a
13833 non-NULL empty vector to tell build_new that it was an
13834 empty initializer() rather than no initializer. This can
13835 only happen when the initializer is a pack expansion whose
13836 parameter packs are of length zero. */
13837 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13838 init_vec = NULL;
13839 else
13840 {
13841 init_vec = make_tree_vector ();
13842 if (init == void_zero_node)
13843 gcc_assert (init_vec != NULL);
13844 else
13845 {
13846 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13847 vec_safe_push (init_vec, TREE_VALUE (init));
13848 }
13849 }
13850
13851 ret = build_new (&placement_vec,
13852 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13853 RECUR (TREE_OPERAND (t, 2)),
13854 &init_vec,
13855 NEW_EXPR_USE_GLOBAL (t),
13856 complain);
13857
13858 if (placement_vec != NULL)
13859 release_tree_vector (placement_vec);
13860 if (init_vec != NULL)
13861 release_tree_vector (init_vec);
13862
13863 RETURN (ret);
13864 }
13865
13866 case DELETE_EXPR:
13867 RETURN (delete_sanity
13868 (RECUR (TREE_OPERAND (t, 0)),
13869 RECUR (TREE_OPERAND (t, 1)),
13870 DELETE_EXPR_USE_VEC (t),
13871 DELETE_EXPR_USE_GLOBAL (t),
13872 complain));
13873
13874 case COMPOUND_EXPR:
13875 {
13876 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
13877 complain & ~tf_decltype, in_decl,
13878 /*function_p=*/false,
13879 integral_constant_expression_p);
13880 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
13881 op0,
13882 RECUR (TREE_OPERAND (t, 1)),
13883 complain));
13884 }
13885
13886 case CALL_EXPR:
13887 {
13888 tree function;
13889 vec<tree, va_gc> *call_args;
13890 unsigned int nargs, i;
13891 bool qualified_p;
13892 bool koenig_p;
13893 tree ret;
13894
13895 /* Don't pass tf_decltype down to subexpressions. */
13896 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13897 complain &= ~tf_decltype;
13898
13899 function = CALL_EXPR_FN (t);
13900 /* When we parsed the expression, we determined whether or
13901 not Koenig lookup should be performed. */
13902 koenig_p = KOENIG_LOOKUP_P (t);
13903 if (TREE_CODE (function) == SCOPE_REF)
13904 {
13905 qualified_p = true;
13906 function = tsubst_qualified_id (function, args, complain, in_decl,
13907 /*done=*/false,
13908 /*address_p=*/false);
13909 }
13910 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13911 {
13912 /* Do nothing; calling tsubst_copy_and_build on an identifier
13913 would incorrectly perform unqualified lookup again.
13914
13915 Note that we can also have an IDENTIFIER_NODE if the earlier
13916 unqualified lookup found a member function; in that case
13917 koenig_p will be false and we do want to do the lookup
13918 again to find the instantiated member function.
13919
13920 FIXME but doing that causes c++/15272, so we need to stop
13921 using IDENTIFIER_NODE in that situation. */
13922 qualified_p = false;
13923 }
13924 else
13925 {
13926 if (TREE_CODE (function) == COMPONENT_REF)
13927 {
13928 tree op = TREE_OPERAND (function, 1);
13929
13930 qualified_p = (TREE_CODE (op) == SCOPE_REF
13931 || (BASELINK_P (op)
13932 && BASELINK_QUALIFIED_P (op)));
13933 }
13934 else
13935 qualified_p = false;
13936
13937 if (TREE_CODE (function) == ADDR_EXPR
13938 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
13939 /* Avoid error about taking the address of a constructor. */
13940 function = TREE_OPERAND (function, 0);
13941
13942 function = tsubst_copy_and_build (function, args, complain,
13943 in_decl,
13944 !qualified_p,
13945 integral_constant_expression_p);
13946
13947 if (BASELINK_P (function))
13948 qualified_p = true;
13949 }
13950
13951 nargs = call_expr_nargs (t);
13952 call_args = make_tree_vector ();
13953 for (i = 0; i < nargs; ++i)
13954 {
13955 tree arg = CALL_EXPR_ARG (t, i);
13956
13957 if (!PACK_EXPANSION_P (arg))
13958 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
13959 else
13960 {
13961 /* Expand the pack expansion and push each entry onto
13962 CALL_ARGS. */
13963 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13964 if (TREE_CODE (arg) == TREE_VEC)
13965 {
13966 unsigned int len, j;
13967
13968 len = TREE_VEC_LENGTH (arg);
13969 for (j = 0; j < len; ++j)
13970 {
13971 tree value = TREE_VEC_ELT (arg, j);
13972 if (value != NULL_TREE)
13973 value = convert_from_reference (value);
13974 vec_safe_push (call_args, value);
13975 }
13976 }
13977 else
13978 {
13979 /* A partial substitution. Add one entry. */
13980 vec_safe_push (call_args, arg);
13981 }
13982 }
13983 }
13984
13985 /* We do not perform argument-dependent lookup if normal
13986 lookup finds a non-function, in accordance with the
13987 expected resolution of DR 218. */
13988 if (koenig_p
13989 && ((is_overloaded_fn (function)
13990 /* If lookup found a member function, the Koenig lookup is
13991 not appropriate, even if an unqualified-name was used
13992 to denote the function. */
13993 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13994 || TREE_CODE (function) == IDENTIFIER_NODE)
13995 /* Only do this when substitution turns a dependent call
13996 into a non-dependent call. */
13997 && type_dependent_expression_p_push (t)
13998 && !any_type_dependent_arguments_p (call_args))
13999 function = perform_koenig_lookup (function, call_args, false,
14000 tf_none);
14001
14002 if (TREE_CODE (function) == IDENTIFIER_NODE
14003 && !any_type_dependent_arguments_p (call_args))
14004 {
14005 if (koenig_p && (complain & tf_warning_or_error))
14006 {
14007 /* For backwards compatibility and good diagnostics, try
14008 the unqualified lookup again if we aren't in SFINAE
14009 context. */
14010 tree unq = (tsubst_copy_and_build
14011 (function, args, complain, in_decl, true,
14012 integral_constant_expression_p));
14013 if (unq == error_mark_node)
14014 RETURN (error_mark_node);
14015
14016 if (unq != function)
14017 {
14018 tree fn = unq;
14019 if (TREE_CODE (fn) == INDIRECT_REF)
14020 fn = TREE_OPERAND (fn, 0);
14021 if (TREE_CODE (fn) == COMPONENT_REF)
14022 fn = TREE_OPERAND (fn, 1);
14023 if (is_overloaded_fn (fn))
14024 fn = get_first_fn (fn);
14025 permerror (EXPR_LOC_OR_HERE (t),
14026 "%qD was not declared in this scope, "
14027 "and no declarations were found by "
14028 "argument-dependent lookup at the point "
14029 "of instantiation", function);
14030 if (!DECL_P (fn))
14031 /* Can't say anything more. */;
14032 else if (DECL_CLASS_SCOPE_P (fn))
14033 {
14034 inform (EXPR_LOC_OR_HERE (t),
14035 "declarations in dependent base %qT are "
14036 "not found by unqualified lookup",
14037 DECL_CLASS_CONTEXT (fn));
14038 if (current_class_ptr)
14039 inform (EXPR_LOC_OR_HERE (t),
14040 "use %<this->%D%> instead", function);
14041 else
14042 inform (EXPR_LOC_OR_HERE (t),
14043 "use %<%T::%D%> instead",
14044 current_class_name, function);
14045 }
14046 else
14047 inform (0, "%q+D declared here, later in the "
14048 "translation unit", fn);
14049 function = unq;
14050 }
14051 }
14052 if (TREE_CODE (function) == IDENTIFIER_NODE)
14053 {
14054 if (complain & tf_error)
14055 unqualified_name_lookup_error (function);
14056 release_tree_vector (call_args);
14057 RETURN (error_mark_node);
14058 }
14059 }
14060
14061 /* Remember that there was a reference to this entity. */
14062 if (DECL_P (function))
14063 mark_used (function);
14064
14065 /* Put back tf_decltype for the actual call. */
14066 complain |= decltype_flag;
14067
14068 if (TREE_CODE (function) == OFFSET_REF)
14069 ret = build_offset_ref_call_from_tree (function, &call_args,
14070 complain);
14071 else if (TREE_CODE (function) == COMPONENT_REF)
14072 {
14073 tree instance = TREE_OPERAND (function, 0);
14074 tree fn = TREE_OPERAND (function, 1);
14075
14076 if (processing_template_decl
14077 && (type_dependent_expression_p (instance)
14078 || (!BASELINK_P (fn)
14079 && TREE_CODE (fn) != FIELD_DECL)
14080 || type_dependent_expression_p (fn)
14081 || any_type_dependent_arguments_p (call_args)))
14082 ret = build_nt_call_vec (function, call_args);
14083 else if (!BASELINK_P (fn))
14084 ret = finish_call_expr (function, &call_args,
14085 /*disallow_virtual=*/false,
14086 /*koenig_p=*/false,
14087 complain);
14088 else
14089 ret = (build_new_method_call
14090 (instance, fn,
14091 &call_args, NULL_TREE,
14092 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14093 /*fn_p=*/NULL,
14094 complain));
14095 }
14096 else
14097 ret = finish_call_expr (function, &call_args,
14098 /*disallow_virtual=*/qualified_p,
14099 koenig_p,
14100 complain);
14101
14102 release_tree_vector (call_args);
14103
14104 RETURN (ret);
14105 }
14106
14107 case COND_EXPR:
14108 {
14109 tree cond = RECUR (TREE_OPERAND (t, 0));
14110 tree exp1, exp2;
14111
14112 if (TREE_CODE (cond) == INTEGER_CST)
14113 {
14114 if (integer_zerop (cond))
14115 {
14116 ++c_inhibit_evaluation_warnings;
14117 exp1 = RECUR (TREE_OPERAND (t, 1));
14118 --c_inhibit_evaluation_warnings;
14119 exp2 = RECUR (TREE_OPERAND (t, 2));
14120 }
14121 else
14122 {
14123 exp1 = RECUR (TREE_OPERAND (t, 1));
14124 ++c_inhibit_evaluation_warnings;
14125 exp2 = RECUR (TREE_OPERAND (t, 2));
14126 --c_inhibit_evaluation_warnings;
14127 }
14128 }
14129 else
14130 {
14131 exp1 = RECUR (TREE_OPERAND (t, 1));
14132 exp2 = RECUR (TREE_OPERAND (t, 2));
14133 }
14134
14135 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14136 cond, exp1, exp2, complain));
14137 }
14138
14139 case PSEUDO_DTOR_EXPR:
14140 RETURN (finish_pseudo_destructor_expr
14141 (RECUR (TREE_OPERAND (t, 0)),
14142 RECUR (TREE_OPERAND (t, 1)),
14143 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)));
14144
14145 case TREE_LIST:
14146 {
14147 tree purpose, value, chain;
14148
14149 if (t == void_list_node)
14150 RETURN (t);
14151
14152 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14153 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14154 {
14155 /* We have pack expansions, so expand those and
14156 create a new list out of it. */
14157 tree purposevec = NULL_TREE;
14158 tree valuevec = NULL_TREE;
14159 tree chain;
14160 int i, len = -1;
14161
14162 /* Expand the argument expressions. */
14163 if (TREE_PURPOSE (t))
14164 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14165 complain, in_decl);
14166 if (TREE_VALUE (t))
14167 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14168 complain, in_decl);
14169
14170 /* Build the rest of the list. */
14171 chain = TREE_CHAIN (t);
14172 if (chain && chain != void_type_node)
14173 chain = RECUR (chain);
14174
14175 /* Determine the number of arguments. */
14176 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14177 {
14178 len = TREE_VEC_LENGTH (purposevec);
14179 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14180 }
14181 else if (TREE_CODE (valuevec) == TREE_VEC)
14182 len = TREE_VEC_LENGTH (valuevec);
14183 else
14184 {
14185 /* Since we only performed a partial substitution into
14186 the argument pack, we only RETURN (a single list
14187 node. */
14188 if (purposevec == TREE_PURPOSE (t)
14189 && valuevec == TREE_VALUE (t)
14190 && chain == TREE_CHAIN (t))
14191 RETURN (t);
14192
14193 RETURN (tree_cons (purposevec, valuevec, chain));
14194 }
14195
14196 /* Convert the argument vectors into a TREE_LIST */
14197 i = len;
14198 while (i > 0)
14199 {
14200 /* Grab the Ith values. */
14201 i--;
14202 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14203 : NULL_TREE;
14204 value
14205 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14206 : NULL_TREE;
14207
14208 /* Build the list (backwards). */
14209 chain = tree_cons (purpose, value, chain);
14210 }
14211
14212 RETURN (chain);
14213 }
14214
14215 purpose = TREE_PURPOSE (t);
14216 if (purpose)
14217 purpose = RECUR (purpose);
14218 value = TREE_VALUE (t);
14219 if (value)
14220 value = RECUR (value);
14221 chain = TREE_CHAIN (t);
14222 if (chain && chain != void_type_node)
14223 chain = RECUR (chain);
14224 if (purpose == TREE_PURPOSE (t)
14225 && value == TREE_VALUE (t)
14226 && chain == TREE_CHAIN (t))
14227 RETURN (t);
14228 RETURN (tree_cons (purpose, value, chain));
14229 }
14230
14231 case COMPONENT_REF:
14232 {
14233 tree object;
14234 tree object_type;
14235 tree member;
14236
14237 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14238 args, complain, in_decl);
14239 /* Remember that there was a reference to this entity. */
14240 if (DECL_P (object))
14241 mark_used (object);
14242 object_type = TREE_TYPE (object);
14243
14244 member = TREE_OPERAND (t, 1);
14245 if (BASELINK_P (member))
14246 member = tsubst_baselink (member,
14247 non_reference (TREE_TYPE (object)),
14248 args, complain, in_decl);
14249 else
14250 member = tsubst_copy (member, args, complain, in_decl);
14251 if (member == error_mark_node)
14252 RETURN (error_mark_node);
14253
14254 if (type_dependent_expression_p (object))
14255 /* We can't do much here. */;
14256 else if (!CLASS_TYPE_P (object_type))
14257 {
14258 if (scalarish_type_p (object_type))
14259 {
14260 tree s = NULL_TREE;
14261 tree dtor = member;
14262
14263 if (TREE_CODE (dtor) == SCOPE_REF)
14264 {
14265 s = TREE_OPERAND (dtor, 0);
14266 dtor = TREE_OPERAND (dtor, 1);
14267 }
14268 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14269 {
14270 dtor = TREE_OPERAND (dtor, 0);
14271 if (TYPE_P (dtor))
14272 RETURN (finish_pseudo_destructor_expr (object, s, dtor));
14273 }
14274 }
14275 }
14276 else if (TREE_CODE (member) == SCOPE_REF
14277 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14278 {
14279 /* Lookup the template functions now that we know what the
14280 scope is. */
14281 tree scope = TREE_OPERAND (member, 0);
14282 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14283 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14284 member = lookup_qualified_name (scope, tmpl,
14285 /*is_type_p=*/false,
14286 /*complain=*/false);
14287 if (BASELINK_P (member))
14288 {
14289 BASELINK_FUNCTIONS (member)
14290 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14291 args);
14292 member = (adjust_result_of_qualified_name_lookup
14293 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14294 object_type));
14295 }
14296 else
14297 {
14298 qualified_name_lookup_error (scope, tmpl, member,
14299 input_location);
14300 RETURN (error_mark_node);
14301 }
14302 }
14303 else if (TREE_CODE (member) == SCOPE_REF
14304 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14305 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14306 {
14307 if (complain & tf_error)
14308 {
14309 if (TYPE_P (TREE_OPERAND (member, 0)))
14310 error ("%qT is not a class or namespace",
14311 TREE_OPERAND (member, 0));
14312 else
14313 error ("%qD is not a class or namespace",
14314 TREE_OPERAND (member, 0));
14315 }
14316 RETURN (error_mark_node);
14317 }
14318 else if (TREE_CODE (member) == FIELD_DECL)
14319 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14320
14321 RETURN (finish_class_member_access_expr (object, member,
14322 /*template_p=*/false,
14323 complain));
14324 }
14325
14326 case THROW_EXPR:
14327 RETURN (build_throw
14328 (RECUR (TREE_OPERAND (t, 0))));
14329
14330 case CONSTRUCTOR:
14331 {
14332 vec<constructor_elt, va_gc> *n;
14333 constructor_elt *ce;
14334 unsigned HOST_WIDE_INT idx;
14335 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14336 bool process_index_p;
14337 int newlen;
14338 bool need_copy_p = false;
14339 tree r;
14340
14341 if (type == error_mark_node)
14342 RETURN (error_mark_node);
14343
14344 /* digest_init will do the wrong thing if we let it. */
14345 if (type && TYPE_PTRMEMFUNC_P (type))
14346 RETURN (t);
14347
14348 /* We do not want to process the index of aggregate
14349 initializers as they are identifier nodes which will be
14350 looked up by digest_init. */
14351 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14352
14353 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14354 newlen = vec_safe_length (n);
14355 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14356 {
14357 if (ce->index && process_index_p)
14358 ce->index = RECUR (ce->index);
14359
14360 if (PACK_EXPANSION_P (ce->value))
14361 {
14362 /* Substitute into the pack expansion. */
14363 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14364 in_decl);
14365
14366 if (ce->value == error_mark_node
14367 || PACK_EXPANSION_P (ce->value))
14368 ;
14369 else if (TREE_VEC_LENGTH (ce->value) == 1)
14370 /* Just move the argument into place. */
14371 ce->value = TREE_VEC_ELT (ce->value, 0);
14372 else
14373 {
14374 /* Update the length of the final CONSTRUCTOR
14375 arguments vector, and note that we will need to
14376 copy.*/
14377 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14378 need_copy_p = true;
14379 }
14380 }
14381 else
14382 ce->value = RECUR (ce->value);
14383 }
14384
14385 if (need_copy_p)
14386 {
14387 vec<constructor_elt, va_gc> *old_n = n;
14388
14389 vec_alloc (n, newlen);
14390 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14391 {
14392 if (TREE_CODE (ce->value) == TREE_VEC)
14393 {
14394 int i, len = TREE_VEC_LENGTH (ce->value);
14395 for (i = 0; i < len; ++i)
14396 CONSTRUCTOR_APPEND_ELT (n, 0,
14397 TREE_VEC_ELT (ce->value, i));
14398 }
14399 else
14400 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14401 }
14402 }
14403
14404 r = build_constructor (init_list_type_node, n);
14405 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14406
14407 if (TREE_HAS_CONSTRUCTOR (t))
14408 RETURN (finish_compound_literal (type, r, complain));
14409
14410 TREE_TYPE (r) = type;
14411 RETURN (r);
14412 }
14413
14414 case TYPEID_EXPR:
14415 {
14416 tree operand_0 = TREE_OPERAND (t, 0);
14417 if (TYPE_P (operand_0))
14418 {
14419 operand_0 = tsubst (operand_0, args, complain, in_decl);
14420 RETURN (get_typeid (operand_0, complain));
14421 }
14422 else
14423 {
14424 operand_0 = RECUR (operand_0);
14425 RETURN (build_typeid (operand_0, complain));
14426 }
14427 }
14428
14429 case VAR_DECL:
14430 if (!args)
14431 RETURN (t);
14432 /* Fall through */
14433
14434 case PARM_DECL:
14435 {
14436 tree r = tsubst_copy (t, args, complain, in_decl);
14437
14438 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14439 /* If the original type was a reference, we'll be wrapped in
14440 the appropriate INDIRECT_REF. */
14441 r = convert_from_reference (r);
14442 RETURN (r);
14443 }
14444
14445 case VA_ARG_EXPR:
14446 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14447 RECUR (TREE_OPERAND (t, 0)),
14448 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14449
14450 case OFFSETOF_EXPR:
14451 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14452
14453 case TRAIT_EXPR:
14454 {
14455 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14456 complain, in_decl);
14457
14458 tree type2 = TRAIT_EXPR_TYPE2 (t);
14459 if (type2)
14460 type2 = tsubst_copy (type2, args, complain, in_decl);
14461
14462 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14463 }
14464
14465 case STMT_EXPR:
14466 {
14467 tree old_stmt_expr = cur_stmt_expr;
14468 tree stmt_expr = begin_stmt_expr ();
14469
14470 cur_stmt_expr = stmt_expr;
14471 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14472 integral_constant_expression_p);
14473 stmt_expr = finish_stmt_expr (stmt_expr, false);
14474 cur_stmt_expr = old_stmt_expr;
14475
14476 /* If the resulting list of expression statement is empty,
14477 fold it further into void_zero_node. */
14478 if (empty_expr_stmt_p (stmt_expr))
14479 stmt_expr = void_zero_node;
14480
14481 RETURN (stmt_expr);
14482 }
14483
14484 case LAMBDA_EXPR:
14485 {
14486 tree r = build_lambda_expr ();
14487
14488 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14489 LAMBDA_EXPR_CLOSURE (r) = type;
14490 CLASSTYPE_LAMBDA_EXPR (type) = r;
14491
14492 LAMBDA_EXPR_LOCATION (r)
14493 = LAMBDA_EXPR_LOCATION (t);
14494 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14495 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14496 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14497 LAMBDA_EXPR_DISCRIMINATOR (r)
14498 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14499 /* For a function scope, we want to use tsubst so that we don't
14500 complain about referring to an auto function before its return
14501 type has been deduced. Otherwise, we want to use tsubst_copy so
14502 that we look up the existing field/parameter/variable rather
14503 than build a new one. */
14504 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14505 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14506 scope = tsubst (scope, args, complain, in_decl);
14507 else if (scope && TREE_CODE (scope) == PARM_DECL)
14508 {
14509 /* Look up the parameter we want directly, as tsubst_copy
14510 doesn't do what we need. */
14511 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14512 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14513 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14514 parm = DECL_CHAIN (parm);
14515 scope = parm;
14516 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14517 if (DECL_CONTEXT (scope) == NULL_TREE)
14518 DECL_CONTEXT (scope) = fn;
14519 }
14520 else
14521 scope = RECUR (scope);
14522 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
14523 LAMBDA_EXPR_RETURN_TYPE (r)
14524 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14525
14526 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14527 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14528
14529 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14530 determine_visibility (TYPE_NAME (type));
14531 /* Now that we know visibility, instantiate the type so we have a
14532 declaration of the op() for later calls to lambda_function. */
14533 complete_type (type);
14534
14535 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
14536
14537 RETURN (build_lambda_object (r));
14538 }
14539
14540 case TARGET_EXPR:
14541 /* We can get here for a constant initializer of non-dependent type.
14542 FIXME stop folding in cp_parser_initializer_clause. */
14543 {
14544 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
14545 complain);
14546 RETURN (r);
14547 }
14548
14549 case TRANSACTION_EXPR:
14550 RETURN (tsubst_expr(t, args, complain, in_decl,
14551 integral_constant_expression_p));
14552
14553 default:
14554 /* Handle Objective-C++ constructs, if appropriate. */
14555 {
14556 tree subst
14557 = objcp_tsubst_copy_and_build (t, args, complain,
14558 in_decl, /*function_p=*/false);
14559 if (subst)
14560 RETURN (subst);
14561 }
14562 RETURN (tsubst_copy (t, args, complain, in_decl));
14563 }
14564
14565 #undef RECUR
14566 #undef RETURN
14567 out:
14568 input_location = loc;
14569 return retval;
14570 }
14571
14572 /* Verify that the instantiated ARGS are valid. For type arguments,
14573 make sure that the type's linkage is ok. For non-type arguments,
14574 make sure they are constants if they are integral or enumerations.
14575 Emit an error under control of COMPLAIN, and return TRUE on error. */
14576
14577 static bool
14578 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14579 {
14580 if (dependent_template_arg_p (t))
14581 return false;
14582 if (ARGUMENT_PACK_P (t))
14583 {
14584 tree vec = ARGUMENT_PACK_ARGS (t);
14585 int len = TREE_VEC_LENGTH (vec);
14586 bool result = false;
14587 int i;
14588
14589 for (i = 0; i < len; ++i)
14590 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14591 result = true;
14592 return result;
14593 }
14594 else if (TYPE_P (t))
14595 {
14596 /* [basic.link]: A name with no linkage (notably, the name
14597 of a class or enumeration declared in a local scope)
14598 shall not be used to declare an entity with linkage.
14599 This implies that names with no linkage cannot be used as
14600 template arguments
14601
14602 DR 757 relaxes this restriction for C++0x. */
14603 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14604 : no_linkage_check (t, /*relaxed_p=*/false));
14605
14606 if (nt)
14607 {
14608 /* DR 488 makes use of a type with no linkage cause
14609 type deduction to fail. */
14610 if (complain & tf_error)
14611 {
14612 if (TYPE_ANONYMOUS_P (nt))
14613 error ("%qT is/uses anonymous type", t);
14614 else
14615 error ("template argument for %qD uses local type %qT",
14616 tmpl, t);
14617 }
14618 return true;
14619 }
14620 /* In order to avoid all sorts of complications, we do not
14621 allow variably-modified types as template arguments. */
14622 else if (variably_modified_type_p (t, NULL_TREE))
14623 {
14624 if (complain & tf_error)
14625 error ("%qT is a variably modified type", t);
14626 return true;
14627 }
14628 }
14629 /* Class template and alias template arguments should be OK. */
14630 else if (DECL_TYPE_TEMPLATE_P (t))
14631 ;
14632 /* A non-type argument of integral or enumerated type must be a
14633 constant. */
14634 else if (TREE_TYPE (t)
14635 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14636 && !TREE_CONSTANT (t))
14637 {
14638 if (complain & tf_error)
14639 error ("integral expression %qE is not constant", t);
14640 return true;
14641 }
14642 return false;
14643 }
14644
14645 static bool
14646 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14647 {
14648 int ix, len = DECL_NTPARMS (tmpl);
14649 bool result = false;
14650
14651 for (ix = 0; ix != len; ix++)
14652 {
14653 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14654 result = true;
14655 }
14656 if (result && (complain & tf_error))
14657 error (" trying to instantiate %qD", tmpl);
14658 return result;
14659 }
14660
14661 /* We're out of SFINAE context now, so generate diagnostics for the access
14662 errors we saw earlier when instantiating D from TMPL and ARGS. */
14663
14664 static void
14665 recheck_decl_substitution (tree d, tree tmpl, tree args)
14666 {
14667 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
14668 tree type = TREE_TYPE (pattern);
14669 location_t loc = input_location;
14670
14671 push_access_scope (d);
14672 push_deferring_access_checks (dk_no_deferred);
14673 input_location = DECL_SOURCE_LOCATION (pattern);
14674 tsubst (type, args, tf_warning_or_error, d);
14675 input_location = loc;
14676 pop_deferring_access_checks ();
14677 pop_access_scope (d);
14678 }
14679
14680 /* Instantiate the indicated variable, function, or alias template TMPL with
14681 the template arguments in TARG_PTR. */
14682
14683 static tree
14684 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14685 {
14686 tree targ_ptr = orig_args;
14687 tree fndecl;
14688 tree gen_tmpl;
14689 tree spec;
14690 bool access_ok = true;
14691
14692 if (tmpl == error_mark_node)
14693 return error_mark_node;
14694
14695 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14696
14697 /* If this function is a clone, handle it specially. */
14698 if (DECL_CLONED_FUNCTION_P (tmpl))
14699 {
14700 tree spec;
14701 tree clone;
14702
14703 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14704 DECL_CLONED_FUNCTION. */
14705 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14706 targ_ptr, complain);
14707 if (spec == error_mark_node)
14708 return error_mark_node;
14709
14710 /* Look for the clone. */
14711 FOR_EACH_CLONE (clone, spec)
14712 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14713 return clone;
14714 /* We should always have found the clone by now. */
14715 gcc_unreachable ();
14716 return NULL_TREE;
14717 }
14718
14719 /* Check to see if we already have this specialization. */
14720 gen_tmpl = most_general_template (tmpl);
14721 if (tmpl != gen_tmpl)
14722 /* The TMPL is a partial instantiation. To get a full set of
14723 arguments we must add the arguments used to perform the
14724 partial instantiation. */
14725 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14726 targ_ptr);
14727
14728 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14729 but it doesn't seem to be on the hot path. */
14730 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14731
14732 gcc_assert (tmpl == gen_tmpl
14733 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14734 == spec)
14735 || fndecl == NULL_TREE);
14736
14737 if (spec != NULL_TREE)
14738 {
14739 if (FNDECL_HAS_ACCESS_ERRORS (spec))
14740 {
14741 if (complain & tf_error)
14742 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
14743 return error_mark_node;
14744 }
14745 return spec;
14746 }
14747
14748 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14749 complain))
14750 return error_mark_node;
14751
14752 /* We are building a FUNCTION_DECL, during which the access of its
14753 parameters and return types have to be checked. However this
14754 FUNCTION_DECL which is the desired context for access checking
14755 is not built yet. We solve this chicken-and-egg problem by
14756 deferring all checks until we have the FUNCTION_DECL. */
14757 push_deferring_access_checks (dk_deferred);
14758
14759 /* Instantiation of the function happens in the context of the function
14760 template, not the context of the overload resolution we're doing. */
14761 push_to_top_level ();
14762 /* If there are dependent arguments, e.g. because we're doing partial
14763 ordering, make sure processing_template_decl stays set. */
14764 if (uses_template_parms (targ_ptr))
14765 ++processing_template_decl;
14766 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14767 {
14768 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14769 complain, gen_tmpl);
14770 push_nested_class (ctx);
14771 }
14772 /* Substitute template parameters to obtain the specialization. */
14773 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14774 targ_ptr, complain, gen_tmpl);
14775 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14776 pop_nested_class ();
14777 pop_from_top_level ();
14778
14779 if (fndecl == error_mark_node)
14780 {
14781 pop_deferring_access_checks ();
14782 return error_mark_node;
14783 }
14784
14785 /* The DECL_TI_TEMPLATE should always be the immediate parent
14786 template, not the most general template. */
14787 DECL_TI_TEMPLATE (fndecl) = tmpl;
14788
14789 /* Now we know the specialization, compute access previously
14790 deferred. */
14791 push_access_scope (fndecl);
14792 if (!perform_deferred_access_checks (complain))
14793 access_ok = false;
14794 pop_access_scope (fndecl);
14795 pop_deferring_access_checks ();
14796
14797 /* If we've just instantiated the main entry point for a function,
14798 instantiate all the alternate entry points as well. We do this
14799 by cloning the instantiation of the main entry point, not by
14800 instantiating the template clones. */
14801 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14802 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14803
14804 if (!access_ok)
14805 {
14806 if (!(complain & tf_error))
14807 {
14808 /* Remember to reinstantiate when we're out of SFINAE so the user
14809 can see the errors. */
14810 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
14811 }
14812 return error_mark_node;
14813 }
14814 return fndecl;
14815 }
14816
14817 /* Wrapper for instantiate_template_1. */
14818
14819 tree
14820 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14821 {
14822 tree ret;
14823 timevar_push (TV_TEMPLATE_INST);
14824 ret = instantiate_template_1 (tmpl, orig_args, complain);
14825 timevar_pop (TV_TEMPLATE_INST);
14826 return ret;
14827 }
14828
14829 /* Instantiate the alias template TMPL with ARGS. Also push a template
14830 instantiation level, which instantiate_template doesn't do because
14831 functions and variables have sufficient context established by the
14832 callers. */
14833
14834 static tree
14835 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
14836 {
14837 struct pending_template *old_last_pend = last_pending_template;
14838 struct tinst_level *old_error_tinst = last_error_tinst_level;
14839 if (tmpl == error_mark_node || args == error_mark_node)
14840 return error_mark_node;
14841 tree tinst = build_tree_list (tmpl, args);
14842 if (!push_tinst_level (tinst))
14843 {
14844 ggc_free (tinst);
14845 return error_mark_node;
14846 }
14847
14848 args =
14849 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
14850 args, tmpl, complain,
14851 /*require_all_args=*/true,
14852 /*use_default_args=*/true);
14853
14854 tree r = instantiate_template (tmpl, args, complain);
14855 pop_tinst_level ();
14856 /* We can't free this if a pending_template entry or last_error_tinst_level
14857 is pointing at it. */
14858 if (last_pending_template == old_last_pend
14859 && last_error_tinst_level == old_error_tinst)
14860 ggc_free (tinst);
14861
14862 return r;
14863 }
14864
14865 /* PARM is a template parameter pack for FN. Returns true iff
14866 PARM is used in a deducible way in the argument list of FN. */
14867
14868 static bool
14869 pack_deducible_p (tree parm, tree fn)
14870 {
14871 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14872 for (; t; t = TREE_CHAIN (t))
14873 {
14874 tree type = TREE_VALUE (t);
14875 tree packs;
14876 if (!PACK_EXPANSION_P (type))
14877 continue;
14878 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14879 packs; packs = TREE_CHAIN (packs))
14880 if (TREE_VALUE (packs) == parm)
14881 {
14882 /* The template parameter pack is used in a function parameter
14883 pack. If this is the end of the parameter list, the
14884 template parameter pack is deducible. */
14885 if (TREE_CHAIN (t) == void_list_node)
14886 return true;
14887 else
14888 /* Otherwise, not. Well, it could be deduced from
14889 a non-pack parameter, but doing so would end up with
14890 a deduction mismatch, so don't bother. */
14891 return false;
14892 }
14893 }
14894 /* The template parameter pack isn't used in any function parameter
14895 packs, but it might be used deeper, e.g. tuple<Args...>. */
14896 return true;
14897 }
14898
14899 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14900 NARGS elements of the arguments that are being used when calling
14901 it. TARGS is a vector into which the deduced template arguments
14902 are placed.
14903
14904 Return zero for success, 2 for an incomplete match that doesn't resolve
14905 all the types, and 1 for complete failure. An error message will be
14906 printed only for an incomplete match.
14907
14908 If FN is a conversion operator, or we are trying to produce a specific
14909 specialization, RETURN_TYPE is the return type desired.
14910
14911 The EXPLICIT_TARGS are explicit template arguments provided via a
14912 template-id.
14913
14914 The parameter STRICT is one of:
14915
14916 DEDUCE_CALL:
14917 We are deducing arguments for a function call, as in
14918 [temp.deduct.call].
14919
14920 DEDUCE_CONV:
14921 We are deducing arguments for a conversion function, as in
14922 [temp.deduct.conv].
14923
14924 DEDUCE_EXACT:
14925 We are deducing arguments when doing an explicit instantiation
14926 as in [temp.explicit], when determining an explicit specialization
14927 as in [temp.expl.spec], or when taking the address of a function
14928 template, as in [temp.deduct.funcaddr]. */
14929
14930 tree
14931 fn_type_unification (tree fn,
14932 tree explicit_targs,
14933 tree targs,
14934 const tree *args,
14935 unsigned int nargs,
14936 tree return_type,
14937 unification_kind_t strict,
14938 int flags,
14939 bool explain_p)
14940 {
14941 tree parms;
14942 tree fntype;
14943 tree decl = NULL_TREE;
14944 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
14945 bool ok;
14946 static int deduction_depth;
14947 struct pending_template *old_last_pend = last_pending_template;
14948 struct tinst_level *old_error_tinst = last_error_tinst_level;
14949 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14950 tree tinst;
14951 tree r = error_mark_node;
14952
14953 /* In C++0x, it's possible to have a function template whose type depends
14954 on itself recursively. This is most obvious with decltype, but can also
14955 occur with enumeration scope (c++/48969). So we need to catch infinite
14956 recursion and reject the substitution at deduction time; this function
14957 will return error_mark_node for any repeated substitution.
14958
14959 This also catches excessive recursion such as when f<N> depends on
14960 f<N-1> across all integers, and returns error_mark_node for all the
14961 substitutions back up to the initial one.
14962
14963 This is, of course, not reentrant. */
14964 if (excessive_deduction_depth)
14965 return error_mark_node;
14966 tinst = build_tree_list (fn, NULL_TREE);
14967 ++deduction_depth;
14968 push_deferring_access_checks (dk_deferred);
14969
14970 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14971
14972 fntype = TREE_TYPE (fn);
14973 if (explicit_targs)
14974 {
14975 /* [temp.deduct]
14976
14977 The specified template arguments must match the template
14978 parameters in kind (i.e., type, nontype, template), and there
14979 must not be more arguments than there are parameters;
14980 otherwise type deduction fails.
14981
14982 Nontype arguments must match the types of the corresponding
14983 nontype template parameters, or must be convertible to the
14984 types of the corresponding nontype parameters as specified in
14985 _temp.arg.nontype_, otherwise type deduction fails.
14986
14987 All references in the function type of the function template
14988 to the corresponding template parameters are replaced by the
14989 specified template argument values. If a substitution in a
14990 template parameter or in the function type of the function
14991 template results in an invalid type, type deduction fails. */
14992 int i, len = TREE_VEC_LENGTH (tparms);
14993 location_t loc = input_location;
14994 bool incomplete = false;
14995
14996 /* Adjust any explicit template arguments before entering the
14997 substitution context. */
14998 explicit_targs
14999 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15000 complain,
15001 /*require_all_args=*/false,
15002 /*use_default_args=*/false));
15003 if (explicit_targs == error_mark_node)
15004 goto fail;
15005
15006 /* Substitute the explicit args into the function type. This is
15007 necessary so that, for instance, explicitly declared function
15008 arguments can match null pointed constants. If we were given
15009 an incomplete set of explicit args, we must not do semantic
15010 processing during substitution as we could create partial
15011 instantiations. */
15012 for (i = 0; i < len; i++)
15013 {
15014 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15015 bool parameter_pack = false;
15016 tree targ = TREE_VEC_ELT (explicit_targs, i);
15017
15018 /* Dig out the actual parm. */
15019 if (TREE_CODE (parm) == TYPE_DECL
15020 || TREE_CODE (parm) == TEMPLATE_DECL)
15021 {
15022 parm = TREE_TYPE (parm);
15023 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15024 }
15025 else if (TREE_CODE (parm) == PARM_DECL)
15026 {
15027 parm = DECL_INITIAL (parm);
15028 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15029 }
15030
15031 if (!parameter_pack && targ == NULL_TREE)
15032 /* No explicit argument for this template parameter. */
15033 incomplete = true;
15034
15035 if (parameter_pack && pack_deducible_p (parm, fn))
15036 {
15037 /* Mark the argument pack as "incomplete". We could
15038 still deduce more arguments during unification.
15039 We remove this mark in type_unification_real. */
15040 if (targ)
15041 {
15042 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15043 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15044 = ARGUMENT_PACK_ARGS (targ);
15045 }
15046
15047 /* We have some incomplete argument packs. */
15048 incomplete = true;
15049 }
15050 }
15051
15052 TREE_VALUE (tinst) = explicit_targs;
15053 if (!push_tinst_level (tinst))
15054 {
15055 excessive_deduction_depth = true;
15056 goto fail;
15057 }
15058 processing_template_decl += incomplete;
15059 input_location = DECL_SOURCE_LOCATION (fn);
15060 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15061 complain | tf_partial, NULL_TREE);
15062 input_location = loc;
15063 processing_template_decl -= incomplete;
15064 pop_tinst_level ();
15065
15066 if (fntype == error_mark_node)
15067 goto fail;
15068
15069 /* Throw away these access checks; we'll see them again in
15070 instantiate_template and they might have the wrong
15071 access path at this point. */
15072 pop_deferring_access_checks ();
15073 push_deferring_access_checks (dk_deferred);
15074
15075 /* Place the explicitly specified arguments in TARGS. */
15076 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15077 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15078 }
15079
15080 /* Never do unification on the 'this' parameter. */
15081 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15082
15083 if (return_type)
15084 {
15085 tree *new_args;
15086
15087 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15088 new_args = XALLOCAVEC (tree, nargs + 1);
15089 new_args[0] = return_type;
15090 memcpy (new_args + 1, args, nargs * sizeof (tree));
15091 args = new_args;
15092 ++nargs;
15093 }
15094
15095 /* We allow incomplete unification without an error message here
15096 because the standard doesn't seem to explicitly prohibit it. Our
15097 callers must be ready to deal with unification failures in any
15098 event. */
15099
15100 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15101 targs, parms, args, nargs, /*subr=*/0,
15102 strict, flags, explain_p);
15103 if (!ok)
15104 goto fail;
15105
15106 /* Now that we have bindings for all of the template arguments,
15107 ensure that the arguments deduced for the template template
15108 parameters have compatible template parameter lists. We cannot
15109 check this property before we have deduced all template
15110 arguments, because the template parameter types of a template
15111 template parameter might depend on prior template parameters
15112 deduced after the template template parameter. The following
15113 ill-formed example illustrates this issue:
15114
15115 template<typename T, template<T> class C> void f(C<5>, T);
15116
15117 template<int N> struct X {};
15118
15119 void g() {
15120 f(X<5>(), 5l); // error: template argument deduction fails
15121 }
15122
15123 The template parameter list of 'C' depends on the template type
15124 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15125 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15126 time that we deduce 'C'. */
15127 if (!template_template_parm_bindings_ok_p
15128 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15129 {
15130 unify_inconsistent_template_template_parameters (explain_p);
15131 goto fail;
15132 }
15133
15134 /* All is well so far. Now, check:
15135
15136 [temp.deduct]
15137
15138 When all template arguments have been deduced, all uses of
15139 template parameters in nondeduced contexts are replaced with
15140 the corresponding deduced argument values. If the
15141 substitution results in an invalid type, as described above,
15142 type deduction fails. */
15143 TREE_VALUE (tinst) = targs;
15144 if (!push_tinst_level (tinst))
15145 {
15146 excessive_deduction_depth = true;
15147 goto fail;
15148 }
15149 decl = instantiate_template (fn, targs, complain);
15150 pop_tinst_level ();
15151
15152 if (decl == error_mark_node)
15153 goto fail;
15154
15155 /* Now perform any access checks encountered during deduction, such as
15156 for default template arguments. */
15157 push_access_scope (decl);
15158 ok = perform_deferred_access_checks (complain);
15159 pop_access_scope (decl);
15160 if (!ok)
15161 goto fail;
15162
15163 /* If we're looking for an exact match, check that what we got
15164 is indeed an exact match. It might not be if some template
15165 parameters are used in non-deduced contexts. */
15166 if (strict == DEDUCE_EXACT)
15167 {
15168 tree substed = TREE_TYPE (decl);
15169 unsigned int i;
15170
15171 tree sarg
15172 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15173 if (return_type)
15174 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15175 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15176 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15177 {
15178 unify_type_mismatch (explain_p, args[i],
15179 TREE_VALUE (sarg));
15180 goto fail;
15181 }
15182 }
15183
15184 r = decl;
15185
15186 fail:
15187 pop_deferring_access_checks ();
15188 --deduction_depth;
15189 if (excessive_deduction_depth)
15190 {
15191 if (deduction_depth == 0)
15192 /* Reset once we're all the way out. */
15193 excessive_deduction_depth = false;
15194 }
15195
15196 /* We can't free this if a pending_template entry or last_error_tinst_level
15197 is pointing at it. */
15198 if (last_pending_template == old_last_pend
15199 && last_error_tinst_level == old_error_tinst)
15200 ggc_free (tinst);
15201
15202 return r;
15203 }
15204
15205 /* Adjust types before performing type deduction, as described in
15206 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15207 sections are symmetric. PARM is the type of a function parameter
15208 or the return type of the conversion function. ARG is the type of
15209 the argument passed to the call, or the type of the value
15210 initialized with the result of the conversion function.
15211 ARG_EXPR is the original argument expression, which may be null. */
15212
15213 static int
15214 maybe_adjust_types_for_deduction (unification_kind_t strict,
15215 tree* parm,
15216 tree* arg,
15217 tree arg_expr)
15218 {
15219 int result = 0;
15220
15221 switch (strict)
15222 {
15223 case DEDUCE_CALL:
15224 break;
15225
15226 case DEDUCE_CONV:
15227 {
15228 /* Swap PARM and ARG throughout the remainder of this
15229 function; the handling is precisely symmetric since PARM
15230 will initialize ARG rather than vice versa. */
15231 tree* temp = parm;
15232 parm = arg;
15233 arg = temp;
15234 break;
15235 }
15236
15237 case DEDUCE_EXACT:
15238 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15239 too, but here handle it by stripping the reference from PARM
15240 rather than by adding it to ARG. */
15241 if (TREE_CODE (*parm) == REFERENCE_TYPE
15242 && TYPE_REF_IS_RVALUE (*parm)
15243 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15244 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15245 && TREE_CODE (*arg) == REFERENCE_TYPE
15246 && !TYPE_REF_IS_RVALUE (*arg))
15247 *parm = TREE_TYPE (*parm);
15248 /* Nothing else to do in this case. */
15249 return 0;
15250
15251 default:
15252 gcc_unreachable ();
15253 }
15254
15255 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15256 {
15257 /* [temp.deduct.call]
15258
15259 If P is not a reference type:
15260
15261 --If A is an array type, the pointer type produced by the
15262 array-to-pointer standard conversion (_conv.array_) is
15263 used in place of A for type deduction; otherwise,
15264
15265 --If A is a function type, the pointer type produced by
15266 the function-to-pointer standard conversion
15267 (_conv.func_) is used in place of A for type deduction;
15268 otherwise,
15269
15270 --If A is a cv-qualified type, the top level
15271 cv-qualifiers of A's type are ignored for type
15272 deduction. */
15273 if (TREE_CODE (*arg) == ARRAY_TYPE)
15274 *arg = build_pointer_type (TREE_TYPE (*arg));
15275 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15276 *arg = build_pointer_type (*arg);
15277 else
15278 *arg = TYPE_MAIN_VARIANT (*arg);
15279 }
15280
15281 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15282 of the form T&&, where T is a template parameter, and the argument
15283 is an lvalue, T is deduced as A& */
15284 if (TREE_CODE (*parm) == REFERENCE_TYPE
15285 && TYPE_REF_IS_RVALUE (*parm)
15286 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15287 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15288 && (arg_expr ? real_lvalue_p (arg_expr)
15289 /* try_one_overload doesn't provide an arg_expr, but
15290 functions are always lvalues. */
15291 : TREE_CODE (*arg) == FUNCTION_TYPE))
15292 *arg = build_reference_type (*arg);
15293
15294 /* [temp.deduct.call]
15295
15296 If P is a cv-qualified type, the top level cv-qualifiers
15297 of P's type are ignored for type deduction. If P is a
15298 reference type, the type referred to by P is used for
15299 type deduction. */
15300 *parm = TYPE_MAIN_VARIANT (*parm);
15301 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15302 {
15303 *parm = TREE_TYPE (*parm);
15304 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15305 }
15306
15307 /* DR 322. For conversion deduction, remove a reference type on parm
15308 too (which has been swapped into ARG). */
15309 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15310 *arg = TREE_TYPE (*arg);
15311
15312 return result;
15313 }
15314
15315 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15316 template which does contain any deducible template parameters; check if
15317 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15318 unify_one_argument. */
15319
15320 static int
15321 check_non_deducible_conversion (tree parm, tree arg, int strict,
15322 int flags, bool explain_p)
15323 {
15324 tree type;
15325
15326 if (!TYPE_P (arg))
15327 type = TREE_TYPE (arg);
15328 else
15329 type = arg;
15330
15331 if (same_type_p (parm, type))
15332 return unify_success (explain_p);
15333
15334 if (strict == DEDUCE_CONV)
15335 {
15336 if (can_convert_arg (type, parm, NULL_TREE, flags,
15337 explain_p ? tf_warning_or_error : tf_none))
15338 return unify_success (explain_p);
15339 }
15340 else if (strict != DEDUCE_EXACT)
15341 {
15342 if (can_convert_arg (parm, type,
15343 TYPE_P (arg) ? NULL_TREE : arg,
15344 flags, explain_p ? tf_warning_or_error : tf_none))
15345 return unify_success (explain_p);
15346 }
15347
15348 if (strict == DEDUCE_EXACT)
15349 return unify_type_mismatch (explain_p, parm, arg);
15350 else
15351 return unify_arg_conversion (explain_p, parm, type, arg);
15352 }
15353
15354 /* Subroutine of type_unification_real and unify_pack_expansion to
15355 handle unification of a single P/A pair. Parameters are as
15356 for those functions. */
15357
15358 static int
15359 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
15360 int subr, unification_kind_t strict, int flags,
15361 bool explain_p)
15362 {
15363 tree arg_expr = NULL_TREE;
15364 int arg_strict;
15365
15366 if (arg == error_mark_node || parm == error_mark_node)
15367 return unify_invalid (explain_p);
15368 if (arg == unknown_type_node)
15369 /* We can't deduce anything from this, but we might get all the
15370 template args from other function args. */
15371 return unify_success (explain_p);
15372
15373 /* FIXME uses_deducible_template_parms */
15374 if (TYPE_P (parm) && !uses_template_parms (parm))
15375 return check_non_deducible_conversion (parm, arg, strict, flags,
15376 explain_p);
15377
15378 switch (strict)
15379 {
15380 case DEDUCE_CALL:
15381 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
15382 | UNIFY_ALLOW_MORE_CV_QUAL
15383 | UNIFY_ALLOW_DERIVED);
15384 break;
15385
15386 case DEDUCE_CONV:
15387 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15388 break;
15389
15390 case DEDUCE_EXACT:
15391 arg_strict = UNIFY_ALLOW_NONE;
15392 break;
15393
15394 default:
15395 gcc_unreachable ();
15396 }
15397
15398 /* We only do these transformations if this is the top-level
15399 parameter_type_list in a call or declaration matching; in other
15400 situations (nested function declarators, template argument lists) we
15401 won't be comparing a type to an expression, and we don't do any type
15402 adjustments. */
15403 if (!subr)
15404 {
15405 if (!TYPE_P (arg))
15406 {
15407 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15408 if (type_unknown_p (arg))
15409 {
15410 /* [temp.deduct.type] A template-argument can be
15411 deduced from a pointer to function or pointer
15412 to member function argument if the set of
15413 overloaded functions does not contain function
15414 templates and at most one of a set of
15415 overloaded functions provides a unique
15416 match. */
15417
15418 if (resolve_overloaded_unification
15419 (tparms, targs, parm, arg, strict,
15420 arg_strict, explain_p))
15421 return unify_success (explain_p);
15422 return unify_overload_resolution_failure (explain_p, arg);
15423 }
15424
15425 arg_expr = arg;
15426 arg = unlowered_expr_type (arg);
15427 if (arg == error_mark_node)
15428 return unify_invalid (explain_p);
15429 }
15430
15431 arg_strict |=
15432 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
15433 }
15434 else
15435 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
15436 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
15437
15438 /* For deduction from an init-list we need the actual list. */
15439 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15440 arg = arg_expr;
15441 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
15442 }
15443
15444 /* Most parms like fn_type_unification.
15445
15446 If SUBR is 1, we're being called recursively (to unify the
15447 arguments of a function or method parameter of a function
15448 template). */
15449
15450 static int
15451 type_unification_real (tree tparms,
15452 tree targs,
15453 tree xparms,
15454 const tree *xargs,
15455 unsigned int xnargs,
15456 int subr,
15457 unification_kind_t strict,
15458 int flags,
15459 bool explain_p)
15460 {
15461 tree parm, arg;
15462 int i;
15463 int ntparms = TREE_VEC_LENGTH (tparms);
15464 int saw_undeduced = 0;
15465 tree parms;
15466 const tree *args;
15467 unsigned int nargs;
15468 unsigned int ia;
15469
15470 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15471 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15472 gcc_assert (ntparms > 0);
15473
15474 /* Reset the number of non-defaulted template arguments contained
15475 in TARGS. */
15476 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15477
15478 again:
15479 parms = xparms;
15480 args = xargs;
15481 nargs = xnargs;
15482
15483 ia = 0;
15484 while (parms && parms != void_list_node
15485 && ia < nargs)
15486 {
15487 parm = TREE_VALUE (parms);
15488
15489 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15490 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15491 /* For a function parameter pack that occurs at the end of the
15492 parameter-declaration-list, the type A of each remaining
15493 argument of the call is compared with the type P of the
15494 declarator-id of the function parameter pack. */
15495 break;
15496
15497 parms = TREE_CHAIN (parms);
15498
15499 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15500 /* For a function parameter pack that does not occur at the
15501 end of the parameter-declaration-list, the type of the
15502 parameter pack is a non-deduced context. */
15503 continue;
15504
15505 arg = args[ia];
15506 ++ia;
15507
15508 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15509 flags, explain_p))
15510 return 1;
15511 }
15512
15513 if (parms
15514 && parms != void_list_node
15515 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15516 {
15517 /* Unify the remaining arguments with the pack expansion type. */
15518 tree argvec;
15519 tree parmvec = make_tree_vec (1);
15520
15521 /* Allocate a TREE_VEC and copy in all of the arguments */
15522 argvec = make_tree_vec (nargs - ia);
15523 for (i = 0; ia < nargs; ++ia, ++i)
15524 TREE_VEC_ELT (argvec, i) = args[ia];
15525
15526 /* Copy the parameter into parmvec. */
15527 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15528 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15529 /*subr=*/subr, explain_p))
15530 return 1;
15531
15532 /* Advance to the end of the list of parameters. */
15533 parms = TREE_CHAIN (parms);
15534 }
15535
15536 /* Fail if we've reached the end of the parm list, and more args
15537 are present, and the parm list isn't variadic. */
15538 if (ia < nargs && parms == void_list_node)
15539 return unify_too_many_arguments (explain_p, nargs, ia);
15540 /* Fail if parms are left and they don't have default values. */
15541 if (parms && parms != void_list_node
15542 && TREE_PURPOSE (parms) == NULL_TREE)
15543 {
15544 unsigned int count = nargs;
15545 tree p = parms;
15546 while (p && p != void_list_node)
15547 {
15548 count++;
15549 p = TREE_CHAIN (p);
15550 }
15551 return unify_too_few_arguments (explain_p, ia, count);
15552 }
15553
15554 if (!subr)
15555 {
15556 tsubst_flags_t complain = (explain_p
15557 ? tf_warning_or_error
15558 : tf_none);
15559
15560 for (i = 0; i < ntparms; i++)
15561 {
15562 tree targ = TREE_VEC_ELT (targs, i);
15563 tree tparm = TREE_VEC_ELT (tparms, i);
15564
15565 /* Clear the "incomplete" flags on all argument packs now so that
15566 substituting them into later default arguments works. */
15567 if (targ && ARGUMENT_PACK_P (targ))
15568 {
15569 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15570 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15571 }
15572
15573 if (targ || tparm == error_mark_node)
15574 continue;
15575 tparm = TREE_VALUE (tparm);
15576
15577 /* If this is an undeduced nontype parameter that depends on
15578 a type parameter, try another pass; its type may have been
15579 deduced from a later argument than the one from which
15580 this parameter can be deduced. */
15581 if (TREE_CODE (tparm) == PARM_DECL
15582 && uses_template_parms (TREE_TYPE (tparm))
15583 && !saw_undeduced++)
15584 goto again;
15585
15586 /* Core issue #226 (C++0x) [temp.deduct]:
15587
15588 If a template argument has not been deduced, its
15589 default template argument, if any, is used.
15590
15591 When we are in C++98 mode, TREE_PURPOSE will either
15592 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15593 to explicitly check cxx_dialect here. */
15594 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15595 {
15596 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15597 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15598 location_t save_loc = input_location;
15599 if (DECL_P (parm))
15600 input_location = DECL_SOURCE_LOCATION (parm);
15601 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15602 arg = convert_template_argument (parm, arg, targs, complain,
15603 i, NULL_TREE);
15604 input_location = save_loc;
15605 if (arg == error_mark_node)
15606 return 1;
15607 else
15608 {
15609 TREE_VEC_ELT (targs, i) = arg;
15610 /* The position of the first default template argument,
15611 is also the number of non-defaulted arguments in TARGS.
15612 Record that. */
15613 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15614 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15615 continue;
15616 }
15617 }
15618
15619 /* If the type parameter is a parameter pack, then it will
15620 be deduced to an empty parameter pack. */
15621 if (template_parameter_pack_p (tparm))
15622 {
15623 tree arg;
15624
15625 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15626 {
15627 arg = make_node (NONTYPE_ARGUMENT_PACK);
15628 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15629 TREE_CONSTANT (arg) = 1;
15630 }
15631 else
15632 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15633
15634 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15635
15636 TREE_VEC_ELT (targs, i) = arg;
15637 continue;
15638 }
15639
15640 return unify_parameter_deduction_failure (explain_p, tparm);
15641 }
15642 }
15643 #ifdef ENABLE_CHECKING
15644 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15645 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15646 #endif
15647
15648 return unify_success (explain_p);
15649 }
15650
15651 /* Subroutine of type_unification_real. Args are like the variables
15652 at the call site. ARG is an overloaded function (or template-id);
15653 we try deducing template args from each of the overloads, and if
15654 only one succeeds, we go with that. Modifies TARGS and returns
15655 true on success. */
15656
15657 static bool
15658 resolve_overloaded_unification (tree tparms,
15659 tree targs,
15660 tree parm,
15661 tree arg,
15662 unification_kind_t strict,
15663 int sub_strict,
15664 bool explain_p)
15665 {
15666 tree tempargs = copy_node (targs);
15667 int good = 0;
15668 tree goodfn = NULL_TREE;
15669 bool addr_p;
15670
15671 if (TREE_CODE (arg) == ADDR_EXPR)
15672 {
15673 arg = TREE_OPERAND (arg, 0);
15674 addr_p = true;
15675 }
15676 else
15677 addr_p = false;
15678
15679 if (TREE_CODE (arg) == COMPONENT_REF)
15680 /* Handle `&x' where `x' is some static or non-static member
15681 function name. */
15682 arg = TREE_OPERAND (arg, 1);
15683
15684 if (TREE_CODE (arg) == OFFSET_REF)
15685 arg = TREE_OPERAND (arg, 1);
15686
15687 /* Strip baselink information. */
15688 if (BASELINK_P (arg))
15689 arg = BASELINK_FUNCTIONS (arg);
15690
15691 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15692 {
15693 /* If we got some explicit template args, we need to plug them into
15694 the affected templates before we try to unify, in case the
15695 explicit args will completely resolve the templates in question. */
15696
15697 int ok = 0;
15698 tree expl_subargs = TREE_OPERAND (arg, 1);
15699 arg = TREE_OPERAND (arg, 0);
15700
15701 for (; arg; arg = OVL_NEXT (arg))
15702 {
15703 tree fn = OVL_CURRENT (arg);
15704 tree subargs, elem;
15705
15706 if (TREE_CODE (fn) != TEMPLATE_DECL)
15707 continue;
15708
15709 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15710 expl_subargs, NULL_TREE, tf_none,
15711 /*require_all_args=*/true,
15712 /*use_default_args=*/true);
15713 if (subargs != error_mark_node
15714 && !any_dependent_template_arguments_p (subargs))
15715 {
15716 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15717 if (try_one_overload (tparms, targs, tempargs, parm,
15718 elem, strict, sub_strict, addr_p, explain_p)
15719 && (!goodfn || !same_type_p (goodfn, elem)))
15720 {
15721 goodfn = elem;
15722 ++good;
15723 }
15724 }
15725 else if (subargs)
15726 ++ok;
15727 }
15728 /* If no templates (or more than one) are fully resolved by the
15729 explicit arguments, this template-id is a non-deduced context; it
15730 could still be OK if we deduce all template arguments for the
15731 enclosing call through other arguments. */
15732 if (good != 1)
15733 good = ok;
15734 }
15735 else if (TREE_CODE (arg) != OVERLOAD
15736 && TREE_CODE (arg) != FUNCTION_DECL)
15737 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15738 -- but the deduction does not succeed because the expression is
15739 not just the function on its own. */
15740 return false;
15741 else
15742 for (; arg; arg = OVL_NEXT (arg))
15743 if (try_one_overload (tparms, targs, tempargs, parm,
15744 TREE_TYPE (OVL_CURRENT (arg)),
15745 strict, sub_strict, addr_p, explain_p)
15746 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15747 {
15748 goodfn = OVL_CURRENT (arg);
15749 ++good;
15750 }
15751
15752 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15753 to function or pointer to member function argument if the set of
15754 overloaded functions does not contain function templates and at most
15755 one of a set of overloaded functions provides a unique match.
15756
15757 So if we found multiple possibilities, we return success but don't
15758 deduce anything. */
15759
15760 if (good == 1)
15761 {
15762 int i = TREE_VEC_LENGTH (targs);
15763 for (; i--; )
15764 if (TREE_VEC_ELT (tempargs, i))
15765 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15766 }
15767 if (good)
15768 return true;
15769
15770 return false;
15771 }
15772
15773 /* Core DR 115: In contexts where deduction is done and fails, or in
15774 contexts where deduction is not done, if a template argument list is
15775 specified and it, along with any default template arguments, identifies
15776 a single function template specialization, then the template-id is an
15777 lvalue for the function template specialization. */
15778
15779 tree
15780 resolve_nondeduced_context (tree orig_expr)
15781 {
15782 tree expr, offset, baselink;
15783 bool addr;
15784
15785 if (!type_unknown_p (orig_expr))
15786 return orig_expr;
15787
15788 expr = orig_expr;
15789 addr = false;
15790 offset = NULL_TREE;
15791 baselink = NULL_TREE;
15792
15793 if (TREE_CODE (expr) == ADDR_EXPR)
15794 {
15795 expr = TREE_OPERAND (expr, 0);
15796 addr = true;
15797 }
15798 if (TREE_CODE (expr) == OFFSET_REF)
15799 {
15800 offset = expr;
15801 expr = TREE_OPERAND (expr, 1);
15802 }
15803 if (BASELINK_P (expr))
15804 {
15805 baselink = expr;
15806 expr = BASELINK_FUNCTIONS (expr);
15807 }
15808
15809 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15810 {
15811 int good = 0;
15812 tree goodfn = NULL_TREE;
15813
15814 /* If we got some explicit template args, we need to plug them into
15815 the affected templates before we try to unify, in case the
15816 explicit args will completely resolve the templates in question. */
15817
15818 tree expl_subargs = TREE_OPERAND (expr, 1);
15819 tree arg = TREE_OPERAND (expr, 0);
15820 tree badfn = NULL_TREE;
15821 tree badargs = NULL_TREE;
15822
15823 for (; arg; arg = OVL_NEXT (arg))
15824 {
15825 tree fn = OVL_CURRENT (arg);
15826 tree subargs, elem;
15827
15828 if (TREE_CODE (fn) != TEMPLATE_DECL)
15829 continue;
15830
15831 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15832 expl_subargs, NULL_TREE, tf_none,
15833 /*require_all_args=*/true,
15834 /*use_default_args=*/true);
15835 if (subargs != error_mark_node
15836 && !any_dependent_template_arguments_p (subargs))
15837 {
15838 elem = instantiate_template (fn, subargs, tf_none);
15839 if (elem == error_mark_node)
15840 {
15841 badfn = fn;
15842 badargs = subargs;
15843 }
15844 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15845 {
15846 goodfn = elem;
15847 ++good;
15848 }
15849 }
15850 }
15851 if (good == 1)
15852 {
15853 mark_used (goodfn);
15854 expr = goodfn;
15855 if (baselink)
15856 expr = build_baselink (BASELINK_BINFO (baselink),
15857 BASELINK_ACCESS_BINFO (baselink),
15858 expr, BASELINK_OPTYPE (baselink));
15859 if (offset)
15860 {
15861 tree base
15862 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15863 expr = build_offset_ref (base, expr, addr);
15864 }
15865 if (addr)
15866 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15867 return expr;
15868 }
15869 else if (good == 0 && badargs)
15870 /* There were no good options and at least one bad one, so let the
15871 user know what the problem is. */
15872 instantiate_template (badfn, badargs, tf_warning_or_error);
15873 }
15874 return orig_expr;
15875 }
15876
15877 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15878 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15879 different overloads deduce different arguments for a given parm.
15880 ADDR_P is true if the expression for which deduction is being
15881 performed was of the form "& fn" rather than simply "fn".
15882
15883 Returns 1 on success. */
15884
15885 static int
15886 try_one_overload (tree tparms,
15887 tree orig_targs,
15888 tree targs,
15889 tree parm,
15890 tree arg,
15891 unification_kind_t strict,
15892 int sub_strict,
15893 bool addr_p,
15894 bool explain_p)
15895 {
15896 int nargs;
15897 tree tempargs;
15898 int i;
15899
15900 if (arg == error_mark_node)
15901 return 0;
15902
15903 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15904 to function or pointer to member function argument if the set of
15905 overloaded functions does not contain function templates and at most
15906 one of a set of overloaded functions provides a unique match.
15907
15908 So if this is a template, just return success. */
15909
15910 if (uses_template_parms (arg))
15911 return 1;
15912
15913 if (TREE_CODE (arg) == METHOD_TYPE)
15914 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15915 else if (addr_p)
15916 arg = build_pointer_type (arg);
15917
15918 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15919
15920 /* We don't copy orig_targs for this because if we have already deduced
15921 some template args from previous args, unify would complain when we
15922 try to deduce a template parameter for the same argument, even though
15923 there isn't really a conflict. */
15924 nargs = TREE_VEC_LENGTH (targs);
15925 tempargs = make_tree_vec (nargs);
15926
15927 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15928 return 0;
15929
15930 /* First make sure we didn't deduce anything that conflicts with
15931 explicitly specified args. */
15932 for (i = nargs; i--; )
15933 {
15934 tree elt = TREE_VEC_ELT (tempargs, i);
15935 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15936
15937 if (!elt)
15938 /*NOP*/;
15939 else if (uses_template_parms (elt))
15940 /* Since we're unifying against ourselves, we will fill in
15941 template args used in the function parm list with our own
15942 template parms. Discard them. */
15943 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15944 else if (oldelt && !template_args_equal (oldelt, elt))
15945 return 0;
15946 }
15947
15948 for (i = nargs; i--; )
15949 {
15950 tree elt = TREE_VEC_ELT (tempargs, i);
15951
15952 if (elt)
15953 TREE_VEC_ELT (targs, i) = elt;
15954 }
15955
15956 return 1;
15957 }
15958
15959 /* PARM is a template class (perhaps with unbound template
15960 parameters). ARG is a fully instantiated type. If ARG can be
15961 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15962 TARGS are as for unify. */
15963
15964 static tree
15965 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15966 bool explain_p)
15967 {
15968 tree copy_of_targs;
15969
15970 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15971 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15972 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15973 return NULL_TREE;
15974
15975 /* We need to make a new template argument vector for the call to
15976 unify. If we used TARGS, we'd clutter it up with the result of
15977 the attempted unification, even if this class didn't work out.
15978 We also don't want to commit ourselves to all the unifications
15979 we've already done, since unification is supposed to be done on
15980 an argument-by-argument basis. In other words, consider the
15981 following pathological case:
15982
15983 template <int I, int J, int K>
15984 struct S {};
15985
15986 template <int I, int J>
15987 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15988
15989 template <int I, int J, int K>
15990 void f(S<I, J, K>, S<I, I, I>);
15991
15992 void g() {
15993 S<0, 0, 0> s0;
15994 S<0, 1, 2> s2;
15995
15996 f(s0, s2);
15997 }
15998
15999 Now, by the time we consider the unification involving `s2', we
16000 already know that we must have `f<0, 0, 0>'. But, even though
16001 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16002 because there are two ways to unify base classes of S<0, 1, 2>
16003 with S<I, I, I>. If we kept the already deduced knowledge, we
16004 would reject the possibility I=1. */
16005 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16006
16007 /* If unification failed, we're done. */
16008 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16009 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16010 return NULL_TREE;
16011
16012 return arg;
16013 }
16014
16015 /* Given a template type PARM and a class type ARG, find the unique
16016 base type in ARG that is an instance of PARM. We do not examine
16017 ARG itself; only its base-classes. If there is not exactly one
16018 appropriate base class, return NULL_TREE. PARM may be the type of
16019 a partial specialization, as well as a plain template type. Used
16020 by unify. */
16021
16022 static enum template_base_result
16023 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16024 bool explain_p, tree *result)
16025 {
16026 tree rval = NULL_TREE;
16027 tree binfo;
16028
16029 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16030
16031 binfo = TYPE_BINFO (complete_type (arg));
16032 if (!binfo)
16033 {
16034 /* The type could not be completed. */
16035 *result = NULL_TREE;
16036 return tbr_incomplete_type;
16037 }
16038
16039 /* Walk in inheritance graph order. The search order is not
16040 important, and this avoids multiple walks of virtual bases. */
16041 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16042 {
16043 tree r = try_class_unification (tparms, targs, parm,
16044 BINFO_TYPE (binfo), explain_p);
16045
16046 if (r)
16047 {
16048 /* If there is more than one satisfactory baseclass, then:
16049
16050 [temp.deduct.call]
16051
16052 If they yield more than one possible deduced A, the type
16053 deduction fails.
16054
16055 applies. */
16056 if (rval && !same_type_p (r, rval))
16057 {
16058 *result = NULL_TREE;
16059 return tbr_ambiguous_baseclass;
16060 }
16061
16062 rval = r;
16063 }
16064 }
16065
16066 *result = rval;
16067 return tbr_success;
16068 }
16069
16070 /* Returns the level of DECL, which declares a template parameter. */
16071
16072 static int
16073 template_decl_level (tree decl)
16074 {
16075 switch (TREE_CODE (decl))
16076 {
16077 case TYPE_DECL:
16078 case TEMPLATE_DECL:
16079 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16080
16081 case PARM_DECL:
16082 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16083
16084 default:
16085 gcc_unreachable ();
16086 }
16087 return 0;
16088 }
16089
16090 /* Decide whether ARG can be unified with PARM, considering only the
16091 cv-qualifiers of each type, given STRICT as documented for unify.
16092 Returns nonzero iff the unification is OK on that basis. */
16093
16094 static int
16095 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16096 {
16097 int arg_quals = cp_type_quals (arg);
16098 int parm_quals = cp_type_quals (parm);
16099
16100 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16101 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16102 {
16103 /* Although a CVR qualifier is ignored when being applied to a
16104 substituted template parameter ([8.3.2]/1 for example), that
16105 does not allow us to unify "const T" with "int&" because both
16106 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16107 It is ok when we're allowing additional CV qualifiers
16108 at the outer level [14.8.2.1]/3,1st bullet. */
16109 if ((TREE_CODE (arg) == REFERENCE_TYPE
16110 || TREE_CODE (arg) == FUNCTION_TYPE
16111 || TREE_CODE (arg) == METHOD_TYPE)
16112 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16113 return 0;
16114
16115 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16116 && (parm_quals & TYPE_QUAL_RESTRICT))
16117 return 0;
16118 }
16119
16120 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16121 && (arg_quals & parm_quals) != parm_quals)
16122 return 0;
16123
16124 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16125 && (parm_quals & arg_quals) != arg_quals)
16126 return 0;
16127
16128 return 1;
16129 }
16130
16131 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16132 void
16133 template_parm_level_and_index (tree parm, int* level, int* index)
16134 {
16135 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16136 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16137 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16138 {
16139 *index = TEMPLATE_TYPE_IDX (parm);
16140 *level = TEMPLATE_TYPE_LEVEL (parm);
16141 }
16142 else
16143 {
16144 *index = TEMPLATE_PARM_IDX (parm);
16145 *level = TEMPLATE_PARM_LEVEL (parm);
16146 }
16147 }
16148
16149 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16150 do { \
16151 if (unify (TP, TA, P, A, S, EP)) \
16152 return 1; \
16153 } while (0);
16154
16155 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16156 expansion at the end of PACKED_PARMS. Returns 0 if the type
16157 deduction succeeds, 1 otherwise. STRICT is the same as in
16158 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16159 call argument list. We'll need to adjust the arguments to make them
16160 types. SUBR tells us if this is from a recursive call to
16161 type_unification_real, or for comparing two template argument
16162 lists. */
16163
16164 static int
16165 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16166 tree packed_args, unification_kind_t strict,
16167 bool subr, bool explain_p)
16168 {
16169 tree parm
16170 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16171 tree pattern = PACK_EXPANSION_PATTERN (parm);
16172 tree pack, packs = NULL_TREE;
16173 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16174 int len = TREE_VEC_LENGTH (packed_args);
16175
16176 /* Determine the parameter packs we will be deducing from the
16177 pattern, and record their current deductions. */
16178 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16179 pack; pack = TREE_CHAIN (pack))
16180 {
16181 tree parm_pack = TREE_VALUE (pack);
16182 int idx, level;
16183
16184 /* Determine the index and level of this parameter pack. */
16185 template_parm_level_and_index (parm_pack, &level, &idx);
16186
16187 /* Keep track of the parameter packs and their corresponding
16188 argument packs. */
16189 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16190 TREE_TYPE (packs) = make_tree_vec (len - start);
16191 }
16192
16193 /* Loop through all of the arguments that have not yet been
16194 unified and unify each with the pattern. */
16195 for (i = start; i < len; i++)
16196 {
16197 tree parm;
16198 bool any_explicit = false;
16199 tree arg = TREE_VEC_ELT (packed_args, i);
16200
16201 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16202 or the element of its argument pack at the current index if
16203 this argument was explicitly specified. */
16204 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16205 {
16206 int idx, level;
16207 tree arg, pargs;
16208 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16209
16210 arg = NULL_TREE;
16211 if (TREE_VALUE (pack)
16212 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16213 && (i < TREE_VEC_LENGTH (pargs)))
16214 {
16215 any_explicit = true;
16216 arg = TREE_VEC_ELT (pargs, i);
16217 }
16218 TMPL_ARG (targs, level, idx) = arg;
16219 }
16220
16221 /* If we had explicit template arguments, substitute them into the
16222 pattern before deduction. */
16223 if (any_explicit)
16224 {
16225 /* Some arguments might still be unspecified or dependent. */
16226 bool dependent;
16227 ++processing_template_decl;
16228 dependent = any_dependent_template_arguments_p (targs);
16229 if (!dependent)
16230 --processing_template_decl;
16231 parm = tsubst (pattern, targs,
16232 explain_p ? tf_warning_or_error : tf_none,
16233 NULL_TREE);
16234 if (dependent)
16235 --processing_template_decl;
16236 if (parm == error_mark_node)
16237 return 1;
16238 }
16239 else
16240 parm = pattern;
16241
16242 /* Unify the pattern with the current argument. */
16243 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16244 LOOKUP_IMPLICIT, explain_p))
16245 return 1;
16246
16247 /* For each parameter pack, collect the deduced value. */
16248 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16249 {
16250 int idx, level;
16251 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16252
16253 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16254 TMPL_ARG (targs, level, idx);
16255 }
16256 }
16257
16258 /* Verify that the results of unification with the parameter packs
16259 produce results consistent with what we've seen before, and make
16260 the deduced argument packs available. */
16261 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16262 {
16263 tree old_pack = TREE_VALUE (pack);
16264 tree new_args = TREE_TYPE (pack);
16265 int i, len = TREE_VEC_LENGTH (new_args);
16266 int idx, level;
16267 bool nondeduced_p = false;
16268
16269 /* By default keep the original deduced argument pack.
16270 If necessary, more specific code is going to update the
16271 resulting deduced argument later down in this function. */
16272 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16273 TMPL_ARG (targs, level, idx) = old_pack;
16274
16275 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16276 actually deduce anything. */
16277 for (i = 0; i < len && !nondeduced_p; ++i)
16278 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16279 nondeduced_p = true;
16280 if (nondeduced_p)
16281 continue;
16282
16283 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16284 {
16285 /* If we had fewer function args than explicit template args,
16286 just use the explicits. */
16287 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16288 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16289 if (len < explicit_len)
16290 new_args = explicit_args;
16291 }
16292
16293 if (!old_pack)
16294 {
16295 tree result;
16296 /* Build the deduced *_ARGUMENT_PACK. */
16297 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16298 {
16299 result = make_node (NONTYPE_ARGUMENT_PACK);
16300 TREE_TYPE (result) =
16301 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16302 TREE_CONSTANT (result) = 1;
16303 }
16304 else
16305 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16306
16307 SET_ARGUMENT_PACK_ARGS (result, new_args);
16308
16309 /* Note the deduced argument packs for this parameter
16310 pack. */
16311 TMPL_ARG (targs, level, idx) = result;
16312 }
16313 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16314 && (ARGUMENT_PACK_ARGS (old_pack)
16315 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16316 {
16317 /* We only had the explicitly-provided arguments before, but
16318 now we have a complete set of arguments. */
16319 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16320
16321 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16322 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16323 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16324 }
16325 else
16326 {
16327 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16328 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16329
16330 if (!comp_template_args_with_info (old_args, new_args,
16331 &bad_old_arg, &bad_new_arg))
16332 /* Inconsistent unification of this parameter pack. */
16333 return unify_parameter_pack_inconsistent (explain_p,
16334 bad_old_arg,
16335 bad_new_arg);
16336 }
16337 }
16338
16339 return unify_success (explain_p);
16340 }
16341
16342 /* Deduce the value of template parameters. TPARMS is the (innermost)
16343 set of template parameters to a template. TARGS is the bindings
16344 for those template parameters, as determined thus far; TARGS may
16345 include template arguments for outer levels of template parameters
16346 as well. PARM is a parameter to a template function, or a
16347 subcomponent of that parameter; ARG is the corresponding argument.
16348 This function attempts to match PARM with ARG in a manner
16349 consistent with the existing assignments in TARGS. If more values
16350 are deduced, then TARGS is updated.
16351
16352 Returns 0 if the type deduction succeeds, 1 otherwise. The
16353 parameter STRICT is a bitwise or of the following flags:
16354
16355 UNIFY_ALLOW_NONE:
16356 Require an exact match between PARM and ARG.
16357 UNIFY_ALLOW_MORE_CV_QUAL:
16358 Allow the deduced ARG to be more cv-qualified (by qualification
16359 conversion) than ARG.
16360 UNIFY_ALLOW_LESS_CV_QUAL:
16361 Allow the deduced ARG to be less cv-qualified than ARG.
16362 UNIFY_ALLOW_DERIVED:
16363 Allow the deduced ARG to be a template base class of ARG,
16364 or a pointer to a template base class of the type pointed to by
16365 ARG.
16366 UNIFY_ALLOW_INTEGER:
16367 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16368 case for more information.
16369 UNIFY_ALLOW_OUTER_LEVEL:
16370 This is the outermost level of a deduction. Used to determine validity
16371 of qualification conversions. A valid qualification conversion must
16372 have const qualified pointers leading up to the inner type which
16373 requires additional CV quals, except at the outer level, where const
16374 is not required [conv.qual]. It would be normal to set this flag in
16375 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16376 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16377 This is the outermost level of a deduction, and PARM can be more CV
16378 qualified at this point.
16379 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16380 This is the outermost level of a deduction, and PARM can be less CV
16381 qualified at this point. */
16382
16383 static int
16384 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
16385 bool explain_p)
16386 {
16387 int idx;
16388 tree targ;
16389 tree tparm;
16390 int strict_in = strict;
16391
16392 /* I don't think this will do the right thing with respect to types.
16393 But the only case I've seen it in so far has been array bounds, where
16394 signedness is the only information lost, and I think that will be
16395 okay. */
16396 while (TREE_CODE (parm) == NOP_EXPR)
16397 parm = TREE_OPERAND (parm, 0);
16398
16399 if (arg == error_mark_node)
16400 return unify_invalid (explain_p);
16401 if (arg == unknown_type_node
16402 || arg == init_list_type_node)
16403 /* We can't deduce anything from this, but we might get all the
16404 template args from other function args. */
16405 return unify_success (explain_p);
16406
16407 /* If PARM uses template parameters, then we can't bail out here,
16408 even if ARG == PARM, since we won't record unifications for the
16409 template parameters. We might need them if we're trying to
16410 figure out which of two things is more specialized. */
16411 if (arg == parm && !uses_template_parms (parm))
16412 return unify_success (explain_p);
16413
16414 /* Handle init lists early, so the rest of the function can assume
16415 we're dealing with a type. */
16416 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
16417 {
16418 tree elt, elttype;
16419 unsigned i;
16420 tree orig_parm = parm;
16421
16422 /* Replace T with std::initializer_list<T> for deduction. */
16423 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16424 && flag_deduce_init_list)
16425 parm = listify (parm);
16426
16427 if (!is_std_init_list (parm))
16428 /* We can only deduce from an initializer list argument if the
16429 parameter is std::initializer_list; otherwise this is a
16430 non-deduced context. */
16431 return unify_success (explain_p);
16432
16433 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
16434
16435 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
16436 {
16437 int elt_strict = strict;
16438
16439 if (elt == error_mark_node)
16440 return unify_invalid (explain_p);
16441
16442 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
16443 {
16444 tree type = TREE_TYPE (elt);
16445 /* It should only be possible to get here for a call. */
16446 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
16447 elt_strict |= maybe_adjust_types_for_deduction
16448 (DEDUCE_CALL, &elttype, &type, elt);
16449 elt = type;
16450 }
16451
16452 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
16453 explain_p);
16454 }
16455
16456 /* If the std::initializer_list<T> deduction worked, replace the
16457 deduced A with std::initializer_list<A>. */
16458 if (orig_parm != parm)
16459 {
16460 idx = TEMPLATE_TYPE_IDX (orig_parm);
16461 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16462 targ = listify (targ);
16463 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
16464 }
16465 return unify_success (explain_p);
16466 }
16467
16468 /* Immediately reject some pairs that won't unify because of
16469 cv-qualification mismatches. */
16470 if (TREE_CODE (arg) == TREE_CODE (parm)
16471 && TYPE_P (arg)
16472 /* It is the elements of the array which hold the cv quals of an array
16473 type, and the elements might be template type parms. We'll check
16474 when we recurse. */
16475 && TREE_CODE (arg) != ARRAY_TYPE
16476 /* We check the cv-qualifiers when unifying with template type
16477 parameters below. We want to allow ARG `const T' to unify with
16478 PARM `T' for example, when computing which of two templates
16479 is more specialized, for example. */
16480 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16481 && !check_cv_quals_for_unify (strict_in, arg, parm))
16482 return unify_cv_qual_mismatch (explain_p, parm, arg);
16483
16484 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16485 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16486 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16487 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16488 strict &= ~UNIFY_ALLOW_DERIVED;
16489 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16490 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16491
16492 switch (TREE_CODE (parm))
16493 {
16494 case TYPENAME_TYPE:
16495 case SCOPE_REF:
16496 case UNBOUND_CLASS_TEMPLATE:
16497 /* In a type which contains a nested-name-specifier, template
16498 argument values cannot be deduced for template parameters used
16499 within the nested-name-specifier. */
16500 return unify_success (explain_p);
16501
16502 case TEMPLATE_TYPE_PARM:
16503 case TEMPLATE_TEMPLATE_PARM:
16504 case BOUND_TEMPLATE_TEMPLATE_PARM:
16505 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16506 if (tparm == error_mark_node)
16507 return unify_invalid (explain_p);
16508
16509 if (TEMPLATE_TYPE_LEVEL (parm)
16510 != template_decl_level (tparm))
16511 /* The PARM is not one we're trying to unify. Just check
16512 to see if it matches ARG. */
16513 {
16514 if (TREE_CODE (arg) == TREE_CODE (parm)
16515 && (is_auto (parm) ? is_auto (arg)
16516 : same_type_p (parm, arg)))
16517 return unify_success (explain_p);
16518 else
16519 return unify_type_mismatch (explain_p, parm, arg);
16520 }
16521 idx = TEMPLATE_TYPE_IDX (parm);
16522 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16523 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16524 if (tparm == error_mark_node)
16525 return unify_invalid (explain_p);
16526
16527 /* Check for mixed types and values. */
16528 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16529 && TREE_CODE (tparm) != TYPE_DECL)
16530 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16531 && TREE_CODE (tparm) != TEMPLATE_DECL))
16532 gcc_unreachable ();
16533
16534 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16535 {
16536 /* ARG must be constructed from a template class or a template
16537 template parameter. */
16538 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16539 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16540 return unify_template_deduction_failure (explain_p, parm, arg);
16541
16542 {
16543 tree parmvec = TYPE_TI_ARGS (parm);
16544 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16545 tree full_argvec = add_to_template_args (targs, argvec);
16546 tree parm_parms
16547 = DECL_INNERMOST_TEMPLATE_PARMS
16548 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16549 int i, len;
16550 int parm_variadic_p = 0;
16551
16552 /* The resolution to DR150 makes clear that default
16553 arguments for an N-argument may not be used to bind T
16554 to a template template parameter with fewer than N
16555 parameters. It is not safe to permit the binding of
16556 default arguments as an extension, as that may change
16557 the meaning of a conforming program. Consider:
16558
16559 struct Dense { static const unsigned int dim = 1; };
16560
16561 template <template <typename> class View,
16562 typename Block>
16563 void operator+(float, View<Block> const&);
16564
16565 template <typename Block,
16566 unsigned int Dim = Block::dim>
16567 struct Lvalue_proxy { operator float() const; };
16568
16569 void
16570 test_1d (void) {
16571 Lvalue_proxy<Dense> p;
16572 float b;
16573 b + p;
16574 }
16575
16576 Here, if Lvalue_proxy is permitted to bind to View, then
16577 the global operator+ will be used; if they are not, the
16578 Lvalue_proxy will be converted to float. */
16579 if (coerce_template_parms (parm_parms,
16580 full_argvec,
16581 TYPE_TI_TEMPLATE (parm),
16582 (explain_p
16583 ? tf_warning_or_error
16584 : tf_none),
16585 /*require_all_args=*/true,
16586 /*use_default_args=*/false)
16587 == error_mark_node)
16588 return 1;
16589
16590 /* Deduce arguments T, i from TT<T> or TT<i>.
16591 We check each element of PARMVEC and ARGVEC individually
16592 rather than the whole TREE_VEC since they can have
16593 different number of elements. */
16594
16595 parmvec = expand_template_argument_pack (parmvec);
16596 argvec = expand_template_argument_pack (argvec);
16597
16598 len = TREE_VEC_LENGTH (parmvec);
16599
16600 /* Check if the parameters end in a pack, making them
16601 variadic. */
16602 if (len > 0
16603 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16604 parm_variadic_p = 1;
16605
16606 for (i = 0; i < len - parm_variadic_p; ++i)
16607 /* If the template argument list of P contains a pack
16608 expansion that is not the last template argument, the
16609 entire template argument list is a non-deduced
16610 context. */
16611 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
16612 return unify_success (explain_p);
16613
16614 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16615 return unify_too_few_arguments (explain_p,
16616 TREE_VEC_LENGTH (argvec), len);
16617
16618 for (i = 0; i < len - parm_variadic_p; ++i)
16619 {
16620 RECUR_AND_CHECK_FAILURE (tparms, targs,
16621 TREE_VEC_ELT (parmvec, i),
16622 TREE_VEC_ELT (argvec, i),
16623 UNIFY_ALLOW_NONE, explain_p);
16624 }
16625
16626 if (parm_variadic_p
16627 && unify_pack_expansion (tparms, targs,
16628 parmvec, argvec,
16629 DEDUCE_EXACT,
16630 /*subr=*/true, explain_p))
16631 return 1;
16632 }
16633 arg = TYPE_TI_TEMPLATE (arg);
16634
16635 /* Fall through to deduce template name. */
16636 }
16637
16638 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16639 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16640 {
16641 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16642
16643 /* Simple cases: Value already set, does match or doesn't. */
16644 if (targ != NULL_TREE && template_args_equal (targ, arg))
16645 return unify_success (explain_p);
16646 else if (targ)
16647 return unify_inconsistency (explain_p, parm, targ, arg);
16648 }
16649 else
16650 {
16651 /* If PARM is `const T' and ARG is only `int', we don't have
16652 a match unless we are allowing additional qualification.
16653 If ARG is `const int' and PARM is just `T' that's OK;
16654 that binds `const int' to `T'. */
16655 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16656 arg, parm))
16657 return unify_cv_qual_mismatch (explain_p, parm, arg);
16658
16659 /* Consider the case where ARG is `const volatile int' and
16660 PARM is `const T'. Then, T should be `volatile int'. */
16661 arg = cp_build_qualified_type_real
16662 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16663 if (arg == error_mark_node)
16664 return unify_invalid (explain_p);
16665
16666 /* Simple cases: Value already set, does match or doesn't. */
16667 if (targ != NULL_TREE && same_type_p (targ, arg))
16668 return unify_success (explain_p);
16669 else if (targ)
16670 return unify_inconsistency (explain_p, parm, targ, arg);
16671
16672 /* Make sure that ARG is not a variable-sized array. (Note
16673 that were talking about variable-sized arrays (like
16674 `int[n]'), rather than arrays of unknown size (like
16675 `int[]').) We'll get very confused by such a type since
16676 the bound of the array is not constant, and therefore
16677 not mangleable. Besides, such types are not allowed in
16678 ISO C++, so we can do as we please here. We do allow
16679 them for 'auto' deduction, since that isn't ABI-exposed. */
16680 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16681 return unify_vla_arg (explain_p, arg);
16682
16683 /* Strip typedefs as in convert_template_argument. */
16684 arg = canonicalize_type_argument (arg, tf_none);
16685 }
16686
16687 /* If ARG is a parameter pack or an expansion, we cannot unify
16688 against it unless PARM is also a parameter pack. */
16689 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16690 && !template_parameter_pack_p (parm))
16691 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16692
16693 /* If the argument deduction results is a METHOD_TYPE,
16694 then there is a problem.
16695 METHOD_TYPE doesn't map to any real C++ type the result of
16696 the deduction can not be of that type. */
16697 if (TREE_CODE (arg) == METHOD_TYPE)
16698 return unify_method_type_error (explain_p, arg);
16699
16700 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16701 return unify_success (explain_p);
16702
16703 case TEMPLATE_PARM_INDEX:
16704 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16705 if (tparm == error_mark_node)
16706 return unify_invalid (explain_p);
16707
16708 if (TEMPLATE_PARM_LEVEL (parm)
16709 != template_decl_level (tparm))
16710 {
16711 /* The PARM is not one we're trying to unify. Just check
16712 to see if it matches ARG. */
16713 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16714 && cp_tree_equal (parm, arg));
16715 if (result)
16716 unify_expression_unequal (explain_p, parm, arg);
16717 return result;
16718 }
16719
16720 idx = TEMPLATE_PARM_IDX (parm);
16721 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16722
16723 if (targ)
16724 {
16725 int x = !cp_tree_equal (targ, arg);
16726 if (x)
16727 unify_inconsistency (explain_p, parm, targ, arg);
16728 return x;
16729 }
16730
16731 /* [temp.deduct.type] If, in the declaration of a function template
16732 with a non-type template-parameter, the non-type
16733 template-parameter is used in an expression in the function
16734 parameter-list and, if the corresponding template-argument is
16735 deduced, the template-argument type shall match the type of the
16736 template-parameter exactly, except that a template-argument
16737 deduced from an array bound may be of any integral type.
16738 The non-type parameter might use already deduced type parameters. */
16739 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16740 if (!TREE_TYPE (arg))
16741 /* Template-parameter dependent expression. Just accept it for now.
16742 It will later be processed in convert_template_argument. */
16743 ;
16744 else if (same_type_p (TREE_TYPE (arg), tparm))
16745 /* OK */;
16746 else if ((strict & UNIFY_ALLOW_INTEGER)
16747 && (TREE_CODE (tparm) == INTEGER_TYPE
16748 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16749 /* Convert the ARG to the type of PARM; the deduced non-type
16750 template argument must exactly match the types of the
16751 corresponding parameter. */
16752 arg = fold (build_nop (tparm, arg));
16753 else if (uses_template_parms (tparm))
16754 /* We haven't deduced the type of this parameter yet. Try again
16755 later. */
16756 return unify_success (explain_p);
16757 else
16758 return unify_type_mismatch (explain_p, tparm, arg);
16759
16760 /* If ARG is a parameter pack or an expansion, we cannot unify
16761 against it unless PARM is also a parameter pack. */
16762 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16763 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16764 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16765
16766 arg = strip_typedefs_expr (arg);
16767 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16768 return unify_success (explain_p);
16769
16770 case PTRMEM_CST:
16771 {
16772 /* A pointer-to-member constant can be unified only with
16773 another constant. */
16774 if (TREE_CODE (arg) != PTRMEM_CST)
16775 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16776
16777 /* Just unify the class member. It would be useless (and possibly
16778 wrong, depending on the strict flags) to unify also
16779 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16780 arg refer to the same variable, even if through different
16781 classes. For instance:
16782
16783 struct A { int x; };
16784 struct B : A { };
16785
16786 Unification of &A::x and &B::x must succeed. */
16787 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16788 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16789 }
16790
16791 case POINTER_TYPE:
16792 {
16793 if (TREE_CODE (arg) != POINTER_TYPE)
16794 return unify_type_mismatch (explain_p, parm, arg);
16795
16796 /* [temp.deduct.call]
16797
16798 A can be another pointer or pointer to member type that can
16799 be converted to the deduced A via a qualification
16800 conversion (_conv.qual_).
16801
16802 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16803 This will allow for additional cv-qualification of the
16804 pointed-to types if appropriate. */
16805
16806 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16807 /* The derived-to-base conversion only persists through one
16808 level of pointers. */
16809 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16810
16811 return unify (tparms, targs, TREE_TYPE (parm),
16812 TREE_TYPE (arg), strict, explain_p);
16813 }
16814
16815 case REFERENCE_TYPE:
16816 if (TREE_CODE (arg) != REFERENCE_TYPE)
16817 return unify_type_mismatch (explain_p, parm, arg);
16818 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16819 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16820
16821 case ARRAY_TYPE:
16822 if (TREE_CODE (arg) != ARRAY_TYPE)
16823 return unify_type_mismatch (explain_p, parm, arg);
16824 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16825 != (TYPE_DOMAIN (arg) == NULL_TREE))
16826 return unify_type_mismatch (explain_p, parm, arg);
16827 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16828 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16829 if (TYPE_DOMAIN (parm) != NULL_TREE)
16830 {
16831 tree parm_max;
16832 tree arg_max;
16833 bool parm_cst;
16834 bool arg_cst;
16835
16836 /* Our representation of array types uses "N - 1" as the
16837 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16838 not an integer constant. We cannot unify arbitrarily
16839 complex expressions, so we eliminate the MINUS_EXPRs
16840 here. */
16841 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16842 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16843 if (!parm_cst)
16844 {
16845 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16846 parm_max = TREE_OPERAND (parm_max, 0);
16847 }
16848 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16849 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16850 if (!arg_cst)
16851 {
16852 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16853 trying to unify the type of a variable with the type
16854 of a template parameter. For example:
16855
16856 template <unsigned int N>
16857 void f (char (&) [N]);
16858 int g();
16859 void h(int i) {
16860 char a[g(i)];
16861 f(a);
16862 }
16863
16864 Here, the type of the ARG will be "int [g(i)]", and
16865 may be a SAVE_EXPR, etc. */
16866 if (TREE_CODE (arg_max) != MINUS_EXPR)
16867 return unify_vla_arg (explain_p, arg);
16868 arg_max = TREE_OPERAND (arg_max, 0);
16869 }
16870
16871 /* If only one of the bounds used a MINUS_EXPR, compensate
16872 by adding one to the other bound. */
16873 if (parm_cst && !arg_cst)
16874 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16875 integer_type_node,
16876 parm_max,
16877 integer_one_node);
16878 else if (arg_cst && !parm_cst)
16879 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16880 integer_type_node,
16881 arg_max,
16882 integer_one_node);
16883
16884 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16885 UNIFY_ALLOW_INTEGER, explain_p);
16886 }
16887 return unify_success (explain_p);
16888
16889 case REAL_TYPE:
16890 case COMPLEX_TYPE:
16891 case VECTOR_TYPE:
16892 case INTEGER_TYPE:
16893 case BOOLEAN_TYPE:
16894 case ENUMERAL_TYPE:
16895 case VOID_TYPE:
16896 case NULLPTR_TYPE:
16897 if (TREE_CODE (arg) != TREE_CODE (parm))
16898 return unify_type_mismatch (explain_p, parm, arg);
16899
16900 /* We have already checked cv-qualification at the top of the
16901 function. */
16902 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16903 return unify_type_mismatch (explain_p, parm, arg);
16904
16905 /* As far as unification is concerned, this wins. Later checks
16906 will invalidate it if necessary. */
16907 return unify_success (explain_p);
16908
16909 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16910 /* Type INTEGER_CST can come from ordinary constant template args. */
16911 case INTEGER_CST:
16912 while (TREE_CODE (arg) == NOP_EXPR)
16913 arg = TREE_OPERAND (arg, 0);
16914
16915 if (TREE_CODE (arg) != INTEGER_CST)
16916 return unify_template_argument_mismatch (explain_p, parm, arg);
16917 return (tree_int_cst_equal (parm, arg)
16918 ? unify_success (explain_p)
16919 : unify_template_argument_mismatch (explain_p, parm, arg));
16920
16921 case TREE_VEC:
16922 {
16923 int i, len, argslen;
16924 int parm_variadic_p = 0;
16925
16926 if (TREE_CODE (arg) != TREE_VEC)
16927 return unify_template_argument_mismatch (explain_p, parm, arg);
16928
16929 len = TREE_VEC_LENGTH (parm);
16930 argslen = TREE_VEC_LENGTH (arg);
16931
16932 /* Check for pack expansions in the parameters. */
16933 for (i = 0; i < len; ++i)
16934 {
16935 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16936 {
16937 if (i == len - 1)
16938 /* We can unify against something with a trailing
16939 parameter pack. */
16940 parm_variadic_p = 1;
16941 else
16942 /* [temp.deduct.type]/9: If the template argument list of
16943 P contains a pack expansion that is not the last
16944 template argument, the entire template argument list
16945 is a non-deduced context. */
16946 return unify_success (explain_p);
16947 }
16948 }
16949
16950 /* If we don't have enough arguments to satisfy the parameters
16951 (not counting the pack expression at the end), or we have
16952 too many arguments for a parameter list that doesn't end in
16953 a pack expression, we can't unify. */
16954 if (parm_variadic_p
16955 ? argslen < len - parm_variadic_p
16956 : argslen != len)
16957 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16958
16959 /* Unify all of the parameters that precede the (optional)
16960 pack expression. */
16961 for (i = 0; i < len - parm_variadic_p; ++i)
16962 {
16963 RECUR_AND_CHECK_FAILURE (tparms, targs,
16964 TREE_VEC_ELT (parm, i),
16965 TREE_VEC_ELT (arg, i),
16966 UNIFY_ALLOW_NONE, explain_p);
16967 }
16968 if (parm_variadic_p)
16969 return unify_pack_expansion (tparms, targs, parm, arg,
16970 DEDUCE_EXACT,
16971 /*subr=*/true, explain_p);
16972 return unify_success (explain_p);
16973 }
16974
16975 case RECORD_TYPE:
16976 case UNION_TYPE:
16977 if (TREE_CODE (arg) != TREE_CODE (parm))
16978 return unify_type_mismatch (explain_p, parm, arg);
16979
16980 if (TYPE_PTRMEMFUNC_P (parm))
16981 {
16982 if (!TYPE_PTRMEMFUNC_P (arg))
16983 return unify_type_mismatch (explain_p, parm, arg);
16984
16985 return unify (tparms, targs,
16986 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16987 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16988 strict, explain_p);
16989 }
16990
16991 if (CLASSTYPE_TEMPLATE_INFO (parm))
16992 {
16993 tree t = NULL_TREE;
16994
16995 if (strict_in & UNIFY_ALLOW_DERIVED)
16996 {
16997 /* First, we try to unify the PARM and ARG directly. */
16998 t = try_class_unification (tparms, targs,
16999 parm, arg, explain_p);
17000
17001 if (!t)
17002 {
17003 /* Fallback to the special case allowed in
17004 [temp.deduct.call]:
17005
17006 If P is a class, and P has the form
17007 template-id, then A can be a derived class of
17008 the deduced A. Likewise, if P is a pointer to
17009 a class of the form template-id, A can be a
17010 pointer to a derived class pointed to by the
17011 deduced A. */
17012 enum template_base_result r;
17013 r = get_template_base (tparms, targs, parm, arg,
17014 explain_p, &t);
17015
17016 if (!t)
17017 return unify_no_common_base (explain_p, r, parm, arg);
17018 }
17019 }
17020 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17021 && (CLASSTYPE_TI_TEMPLATE (parm)
17022 == CLASSTYPE_TI_TEMPLATE (arg)))
17023 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17024 Then, we should unify `int' and `U'. */
17025 t = arg;
17026 else
17027 /* There's no chance of unification succeeding. */
17028 return unify_type_mismatch (explain_p, parm, arg);
17029
17030 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17031 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17032 }
17033 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17034 return unify_type_mismatch (explain_p, parm, arg);
17035 return unify_success (explain_p);
17036
17037 case METHOD_TYPE:
17038 case FUNCTION_TYPE:
17039 {
17040 unsigned int nargs;
17041 tree *args;
17042 tree a;
17043 unsigned int i;
17044
17045 if (TREE_CODE (arg) != TREE_CODE (parm))
17046 return unify_type_mismatch (explain_p, parm, arg);
17047
17048 /* CV qualifications for methods can never be deduced, they must
17049 match exactly. We need to check them explicitly here,
17050 because type_unification_real treats them as any other
17051 cv-qualified parameter. */
17052 if (TREE_CODE (parm) == METHOD_TYPE
17053 && (!check_cv_quals_for_unify
17054 (UNIFY_ALLOW_NONE,
17055 class_of_this_parm (arg),
17056 class_of_this_parm (parm))))
17057 return unify_cv_qual_mismatch (explain_p, parm, arg);
17058
17059 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17060 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17061
17062 nargs = list_length (TYPE_ARG_TYPES (arg));
17063 args = XALLOCAVEC (tree, nargs);
17064 for (a = TYPE_ARG_TYPES (arg), i = 0;
17065 a != NULL_TREE && a != void_list_node;
17066 a = TREE_CHAIN (a), ++i)
17067 args[i] = TREE_VALUE (a);
17068 nargs = i;
17069
17070 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17071 args, nargs, 1, DEDUCE_EXACT,
17072 LOOKUP_NORMAL, explain_p);
17073 }
17074
17075 case OFFSET_TYPE:
17076 /* Unify a pointer to member with a pointer to member function, which
17077 deduces the type of the member as a function type. */
17078 if (TYPE_PTRMEMFUNC_P (arg))
17079 {
17080 tree method_type;
17081 tree fntype;
17082
17083 /* Check top-level cv qualifiers */
17084 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17085 return unify_cv_qual_mismatch (explain_p, parm, arg);
17086
17087 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17088 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17089 UNIFY_ALLOW_NONE, explain_p);
17090
17091 /* Determine the type of the function we are unifying against. */
17092 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
17093 fntype =
17094 build_function_type (TREE_TYPE (method_type),
17095 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
17096
17097 /* Extract the cv-qualifiers of the member function from the
17098 implicit object parameter and place them on the function
17099 type to be restored later. */
17100 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
17101 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17102 }
17103
17104 if (TREE_CODE (arg) != OFFSET_TYPE)
17105 return unify_type_mismatch (explain_p, parm, arg);
17106 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17107 TYPE_OFFSET_BASETYPE (arg),
17108 UNIFY_ALLOW_NONE, explain_p);
17109 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17110 strict, explain_p);
17111
17112 case CONST_DECL:
17113 if (DECL_TEMPLATE_PARM_P (parm))
17114 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17115 if (arg != integral_constant_value (parm))
17116 return unify_template_argument_mismatch (explain_p, parm, arg);
17117 return unify_success (explain_p);
17118
17119 case FIELD_DECL:
17120 case TEMPLATE_DECL:
17121 /* Matched cases are handled by the ARG == PARM test above. */
17122 return unify_template_argument_mismatch (explain_p, parm, arg);
17123
17124 case VAR_DECL:
17125 /* A non-type template parameter that is a variable should be a
17126 an integral constant, in which case, it whould have been
17127 folded into its (constant) value. So we should not be getting
17128 a variable here. */
17129 gcc_unreachable ();
17130
17131 case TYPE_ARGUMENT_PACK:
17132 case NONTYPE_ARGUMENT_PACK:
17133 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17134 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17135
17136 case TYPEOF_TYPE:
17137 case DECLTYPE_TYPE:
17138 case UNDERLYING_TYPE:
17139 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17140 or UNDERLYING_TYPE nodes. */
17141 return unify_success (explain_p);
17142
17143 case ERROR_MARK:
17144 /* Unification fails if we hit an error node. */
17145 return unify_invalid (explain_p);
17146
17147 default:
17148 /* An unresolved overload is a nondeduced context. */
17149 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17150 return unify_success (explain_p);
17151 gcc_assert (EXPR_P (parm));
17152
17153 /* We must be looking at an expression. This can happen with
17154 something like:
17155
17156 template <int I>
17157 void foo(S<I>, S<I + 2>);
17158
17159 This is a "nondeduced context":
17160
17161 [deduct.type]
17162
17163 The nondeduced contexts are:
17164
17165 --A type that is a template-id in which one or more of
17166 the template-arguments is an expression that references
17167 a template-parameter.
17168
17169 In these cases, we assume deduction succeeded, but don't
17170 actually infer any unifications. */
17171
17172 if (!uses_template_parms (parm)
17173 && !template_args_equal (parm, arg))
17174 return unify_expression_unequal (explain_p, parm, arg);
17175 else
17176 return unify_success (explain_p);
17177 }
17178 }
17179 #undef RECUR_AND_CHECK_FAILURE
17180 \f
17181 /* Note that DECL can be defined in this translation unit, if
17182 required. */
17183
17184 static void
17185 mark_definable (tree decl)
17186 {
17187 tree clone;
17188 DECL_NOT_REALLY_EXTERN (decl) = 1;
17189 FOR_EACH_CLONE (clone, decl)
17190 DECL_NOT_REALLY_EXTERN (clone) = 1;
17191 }
17192
17193 /* Called if RESULT is explicitly instantiated, or is a member of an
17194 explicitly instantiated class. */
17195
17196 void
17197 mark_decl_instantiated (tree result, int extern_p)
17198 {
17199 SET_DECL_EXPLICIT_INSTANTIATION (result);
17200
17201 /* If this entity has already been written out, it's too late to
17202 make any modifications. */
17203 if (TREE_ASM_WRITTEN (result))
17204 return;
17205
17206 if (TREE_CODE (result) != FUNCTION_DECL)
17207 /* The TREE_PUBLIC flag for function declarations will have been
17208 set correctly by tsubst. */
17209 TREE_PUBLIC (result) = 1;
17210
17211 /* This might have been set by an earlier implicit instantiation. */
17212 DECL_COMDAT (result) = 0;
17213
17214 if (extern_p)
17215 DECL_NOT_REALLY_EXTERN (result) = 0;
17216 else
17217 {
17218 mark_definable (result);
17219 /* Always make artificials weak. */
17220 if (DECL_ARTIFICIAL (result) && flag_weak)
17221 comdat_linkage (result);
17222 /* For WIN32 we also want to put explicit instantiations in
17223 linkonce sections. */
17224 else if (TREE_PUBLIC (result))
17225 maybe_make_one_only (result);
17226 }
17227
17228 /* If EXTERN_P, then this function will not be emitted -- unless
17229 followed by an explicit instantiation, at which point its linkage
17230 will be adjusted. If !EXTERN_P, then this function will be
17231 emitted here. In neither circumstance do we want
17232 import_export_decl to adjust the linkage. */
17233 DECL_INTERFACE_KNOWN (result) = 1;
17234 }
17235
17236 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17237 important template arguments. If any are missing, we check whether
17238 they're important by using error_mark_node for substituting into any
17239 args that were used for partial ordering (the ones between ARGS and END)
17240 and seeing if it bubbles up. */
17241
17242 static bool
17243 check_undeduced_parms (tree targs, tree args, tree end)
17244 {
17245 bool found = false;
17246 int i;
17247 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17248 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17249 {
17250 found = true;
17251 TREE_VEC_ELT (targs, i) = error_mark_node;
17252 }
17253 if (found)
17254 {
17255 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17256 if (substed == error_mark_node)
17257 return true;
17258 }
17259 return false;
17260 }
17261
17262 /* Given two function templates PAT1 and PAT2, return:
17263
17264 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17265 -1 if PAT2 is more specialized than PAT1.
17266 0 if neither is more specialized.
17267
17268 LEN indicates the number of parameters we should consider
17269 (defaulted parameters should not be considered).
17270
17271 The 1998 std underspecified function template partial ordering, and
17272 DR214 addresses the issue. We take pairs of arguments, one from
17273 each of the templates, and deduce them against each other. One of
17274 the templates will be more specialized if all the *other*
17275 template's arguments deduce against its arguments and at least one
17276 of its arguments *does* *not* deduce against the other template's
17277 corresponding argument. Deduction is done as for class templates.
17278 The arguments used in deduction have reference and top level cv
17279 qualifiers removed. Iff both arguments were originally reference
17280 types *and* deduction succeeds in both directions, the template
17281 with the more cv-qualified argument wins for that pairing (if
17282 neither is more cv-qualified, they both are equal). Unlike regular
17283 deduction, after all the arguments have been deduced in this way,
17284 we do *not* verify the deduced template argument values can be
17285 substituted into non-deduced contexts.
17286
17287 The logic can be a bit confusing here, because we look at deduce1 and
17288 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17289 can find template arguments for pat1 to make arg1 look like arg2, that
17290 means that arg2 is at least as specialized as arg1. */
17291
17292 int
17293 more_specialized_fn (tree pat1, tree pat2, int len)
17294 {
17295 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17296 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17297 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17298 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17299 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17300 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17301 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17302 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17303 tree origs1, origs2;
17304 bool lose1 = false;
17305 bool lose2 = false;
17306
17307 /* Remove the this parameter from non-static member functions. If
17308 one is a non-static member function and the other is not a static
17309 member function, remove the first parameter from that function
17310 also. This situation occurs for operator functions where we
17311 locate both a member function (with this pointer) and non-member
17312 operator (with explicit first operand). */
17313 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
17314 {
17315 len--; /* LEN is the number of significant arguments for DECL1 */
17316 args1 = TREE_CHAIN (args1);
17317 if (!DECL_STATIC_FUNCTION_P (decl2))
17318 args2 = TREE_CHAIN (args2);
17319 }
17320 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
17321 {
17322 args2 = TREE_CHAIN (args2);
17323 if (!DECL_STATIC_FUNCTION_P (decl1))
17324 {
17325 len--;
17326 args1 = TREE_CHAIN (args1);
17327 }
17328 }
17329
17330 /* If only one is a conversion operator, they are unordered. */
17331 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
17332 return 0;
17333
17334 /* Consider the return type for a conversion function */
17335 if (DECL_CONV_FN_P (decl1))
17336 {
17337 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
17338 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
17339 len++;
17340 }
17341
17342 processing_template_decl++;
17343
17344 origs1 = args1;
17345 origs2 = args2;
17346
17347 while (len--
17348 /* Stop when an ellipsis is seen. */
17349 && args1 != NULL_TREE && args2 != NULL_TREE)
17350 {
17351 tree arg1 = TREE_VALUE (args1);
17352 tree arg2 = TREE_VALUE (args2);
17353 int deduce1, deduce2;
17354 int quals1 = -1;
17355 int quals2 = -1;
17356
17357 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17358 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17359 {
17360 /* When both arguments are pack expansions, we need only
17361 unify the patterns themselves. */
17362 arg1 = PACK_EXPANSION_PATTERN (arg1);
17363 arg2 = PACK_EXPANSION_PATTERN (arg2);
17364
17365 /* This is the last comparison we need to do. */
17366 len = 0;
17367 }
17368
17369 if (TREE_CODE (arg1) == REFERENCE_TYPE)
17370 {
17371 arg1 = TREE_TYPE (arg1);
17372 quals1 = cp_type_quals (arg1);
17373 }
17374
17375 if (TREE_CODE (arg2) == REFERENCE_TYPE)
17376 {
17377 arg2 = TREE_TYPE (arg2);
17378 quals2 = cp_type_quals (arg2);
17379 }
17380
17381 arg1 = TYPE_MAIN_VARIANT (arg1);
17382 arg2 = TYPE_MAIN_VARIANT (arg2);
17383
17384 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
17385 {
17386 int i, len2 = list_length (args2);
17387 tree parmvec = make_tree_vec (1);
17388 tree argvec = make_tree_vec (len2);
17389 tree ta = args2;
17390
17391 /* Setup the parameter vector, which contains only ARG1. */
17392 TREE_VEC_ELT (parmvec, 0) = arg1;
17393
17394 /* Setup the argument vector, which contains the remaining
17395 arguments. */
17396 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
17397 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17398
17399 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
17400 argvec, DEDUCE_EXACT,
17401 /*subr=*/true, /*explain_p=*/false)
17402 == 0);
17403
17404 /* We cannot deduce in the other direction, because ARG1 is
17405 a pack expansion but ARG2 is not. */
17406 deduce2 = 0;
17407 }
17408 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17409 {
17410 int i, len1 = list_length (args1);
17411 tree parmvec = make_tree_vec (1);
17412 tree argvec = make_tree_vec (len1);
17413 tree ta = args1;
17414
17415 /* Setup the parameter vector, which contains only ARG1. */
17416 TREE_VEC_ELT (parmvec, 0) = arg2;
17417
17418 /* Setup the argument vector, which contains the remaining
17419 arguments. */
17420 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
17421 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17422
17423 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
17424 argvec, DEDUCE_EXACT,
17425 /*subr=*/true, /*explain_p=*/false)
17426 == 0);
17427
17428 /* We cannot deduce in the other direction, because ARG2 is
17429 a pack expansion but ARG1 is not.*/
17430 deduce1 = 0;
17431 }
17432
17433 else
17434 {
17435 /* The normal case, where neither argument is a pack
17436 expansion. */
17437 deduce1 = (unify (tparms1, targs1, arg1, arg2,
17438 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17439 == 0);
17440 deduce2 = (unify (tparms2, targs2, arg2, arg1,
17441 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17442 == 0);
17443 }
17444
17445 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17446 arg2, then arg2 is not as specialized as arg1. */
17447 if (!deduce1)
17448 lose2 = true;
17449 if (!deduce2)
17450 lose1 = true;
17451
17452 /* "If, for a given type, deduction succeeds in both directions
17453 (i.e., the types are identical after the transformations above)
17454 and if the type from the argument template is more cv-qualified
17455 than the type from the parameter template (as described above)
17456 that type is considered to be more specialized than the other. If
17457 neither type is more cv-qualified than the other then neither type
17458 is more specialized than the other." */
17459
17460 if (deduce1 && deduce2
17461 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17462 {
17463 if ((quals1 & quals2) == quals2)
17464 lose2 = true;
17465 if ((quals1 & quals2) == quals1)
17466 lose1 = true;
17467 }
17468
17469 if (lose1 && lose2)
17470 /* We've failed to deduce something in either direction.
17471 These must be unordered. */
17472 break;
17473
17474 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17475 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17476 /* We have already processed all of the arguments in our
17477 handing of the pack expansion type. */
17478 len = 0;
17479
17480 args1 = TREE_CHAIN (args1);
17481 args2 = TREE_CHAIN (args2);
17482 }
17483
17484 /* "In most cases, all template parameters must have values in order for
17485 deduction to succeed, but for partial ordering purposes a template
17486 parameter may remain without a value provided it is not used in the
17487 types being used for partial ordering."
17488
17489 Thus, if we are missing any of the targs1 we need to substitute into
17490 origs1, then pat2 is not as specialized as pat1. This can happen when
17491 there is a nondeduced context. */
17492 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17493 lose2 = true;
17494 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17495 lose1 = true;
17496
17497 processing_template_decl--;
17498
17499 /* All things being equal, if the next argument is a pack expansion
17500 for one function but not for the other, prefer the
17501 non-variadic function. FIXME this is bogus; see c++/41958. */
17502 if (lose1 == lose2
17503 && args1 && TREE_VALUE (args1)
17504 && args2 && TREE_VALUE (args2))
17505 {
17506 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17507 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17508 }
17509
17510 if (lose1 == lose2)
17511 return 0;
17512 else if (!lose1)
17513 return 1;
17514 else
17515 return -1;
17516 }
17517
17518 /* Determine which of two partial specializations of MAIN_TMPL is more
17519 specialized.
17520
17521 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17522 to the first partial specialization. The TREE_VALUE is the
17523 innermost set of template parameters for the partial
17524 specialization. PAT2 is similar, but for the second template.
17525
17526 Return 1 if the first partial specialization is more specialized;
17527 -1 if the second is more specialized; 0 if neither is more
17528 specialized.
17529
17530 See [temp.class.order] for information about determining which of
17531 two templates is more specialized. */
17532
17533 static int
17534 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
17535 {
17536 tree targs;
17537 tree tmpl1, tmpl2;
17538 int winner = 0;
17539 bool any_deductions = false;
17540
17541 tmpl1 = TREE_TYPE (pat1);
17542 tmpl2 = TREE_TYPE (pat2);
17543
17544 /* Just like what happens for functions, if we are ordering between
17545 different class template specializations, we may encounter dependent
17546 types in the arguments, and we need our dependency check functions
17547 to behave correctly. */
17548 ++processing_template_decl;
17549 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
17550 CLASSTYPE_TI_ARGS (tmpl1),
17551 CLASSTYPE_TI_ARGS (tmpl2));
17552 if (targs)
17553 {
17554 --winner;
17555 any_deductions = true;
17556 }
17557
17558 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
17559 CLASSTYPE_TI_ARGS (tmpl2),
17560 CLASSTYPE_TI_ARGS (tmpl1));
17561 if (targs)
17562 {
17563 ++winner;
17564 any_deductions = true;
17565 }
17566 --processing_template_decl;
17567
17568 /* In the case of a tie where at least one of the class templates
17569 has a parameter pack at the end, the template with the most
17570 non-packed parameters wins. */
17571 if (winner == 0
17572 && any_deductions
17573 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17574 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17575 {
17576 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17577 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17578 int len1 = TREE_VEC_LENGTH (args1);
17579 int len2 = TREE_VEC_LENGTH (args2);
17580
17581 /* We don't count the pack expansion at the end. */
17582 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17583 --len1;
17584 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17585 --len2;
17586
17587 if (len1 > len2)
17588 return 1;
17589 else if (len1 < len2)
17590 return -1;
17591 }
17592
17593 return winner;
17594 }
17595
17596 /* Return the template arguments that will produce the function signature
17597 DECL from the function template FN, with the explicit template
17598 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17599 also match. Return NULL_TREE if no satisfactory arguments could be
17600 found. */
17601
17602 static tree
17603 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17604 {
17605 int ntparms = DECL_NTPARMS (fn);
17606 tree targs = make_tree_vec (ntparms);
17607 tree decl_type = TREE_TYPE (decl);
17608 tree decl_arg_types;
17609 tree *args;
17610 unsigned int nargs, ix;
17611 tree arg;
17612
17613 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
17614
17615 /* Never do unification on the 'this' parameter. */
17616 decl_arg_types = skip_artificial_parms_for (decl,
17617 TYPE_ARG_TYPES (decl_type));
17618
17619 nargs = list_length (decl_arg_types);
17620 args = XALLOCAVEC (tree, nargs);
17621 for (arg = decl_arg_types, ix = 0;
17622 arg != NULL_TREE && arg != void_list_node;
17623 arg = TREE_CHAIN (arg), ++ix)
17624 args[ix] = TREE_VALUE (arg);
17625
17626 if (fn_type_unification (fn, explicit_args, targs,
17627 args, ix,
17628 (check_rettype || DECL_CONV_FN_P (fn)
17629 ? TREE_TYPE (decl_type) : NULL_TREE),
17630 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)
17631 == error_mark_node)
17632 return NULL_TREE;
17633
17634 return targs;
17635 }
17636
17637 /* Return the innermost template arguments that, when applied to a partial
17638 specialization of MAIN_TMPL whose innermost template parameters are
17639 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17640 ARGS.
17641
17642 For example, suppose we have:
17643
17644 template <class T, class U> struct S {};
17645 template <class T> struct S<T*, int> {};
17646
17647 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17648 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17649 int}. The resulting vector will be {double}, indicating that `T'
17650 is bound to `double'. */
17651
17652 static tree
17653 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
17654 {
17655 int i, ntparms = TREE_VEC_LENGTH (tparms);
17656 tree deduced_args;
17657 tree innermost_deduced_args;
17658
17659 innermost_deduced_args = make_tree_vec (ntparms);
17660 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17661 {
17662 deduced_args = copy_node (args);
17663 SET_TMPL_ARGS_LEVEL (deduced_args,
17664 TMPL_ARGS_DEPTH (deduced_args),
17665 innermost_deduced_args);
17666 }
17667 else
17668 deduced_args = innermost_deduced_args;
17669
17670 if (unify (tparms, deduced_args,
17671 INNERMOST_TEMPLATE_ARGS (spec_args),
17672 INNERMOST_TEMPLATE_ARGS (args),
17673 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17674 return NULL_TREE;
17675
17676 for (i = 0; i < ntparms; ++i)
17677 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17678 return NULL_TREE;
17679
17680 /* Verify that nondeduced template arguments agree with the type
17681 obtained from argument deduction.
17682
17683 For example:
17684
17685 struct A { typedef int X; };
17686 template <class T, class U> struct C {};
17687 template <class T> struct C<T, typename T::X> {};
17688
17689 Then with the instantiation `C<A, int>', we can deduce that
17690 `T' is `A' but unify () does not check whether `typename T::X'
17691 is `int'. */
17692 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17693 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
17694 spec_args, main_tmpl,
17695 tf_none, false, false);
17696 if (spec_args == error_mark_node
17697 /* We only need to check the innermost arguments; the other
17698 arguments will always agree. */
17699 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17700 INNERMOST_TEMPLATE_ARGS (args)))
17701 return NULL_TREE;
17702
17703 /* Now that we have bindings for all of the template arguments,
17704 ensure that the arguments deduced for the template template
17705 parameters have compatible template parameter lists. See the use
17706 of template_template_parm_bindings_ok_p in fn_type_unification
17707 for more information. */
17708 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17709 return NULL_TREE;
17710
17711 return deduced_args;
17712 }
17713
17714 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17715 Return the TREE_LIST node with the most specialized template, if
17716 any. If there is no most specialized template, the error_mark_node
17717 is returned.
17718
17719 Note that this function does not look at, or modify, the
17720 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17721 returned is one of the elements of INSTANTIATIONS, callers may
17722 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17723 and retrieve it from the value returned. */
17724
17725 tree
17726 most_specialized_instantiation (tree templates)
17727 {
17728 tree fn, champ;
17729
17730 ++processing_template_decl;
17731
17732 champ = templates;
17733 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17734 {
17735 int fate = 0;
17736
17737 if (get_bindings (TREE_VALUE (champ),
17738 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17739 NULL_TREE, /*check_ret=*/true))
17740 fate--;
17741
17742 if (get_bindings (TREE_VALUE (fn),
17743 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17744 NULL_TREE, /*check_ret=*/true))
17745 fate++;
17746
17747 if (fate == -1)
17748 champ = fn;
17749 else if (!fate)
17750 {
17751 /* Equally specialized, move to next function. If there
17752 is no next function, nothing's most specialized. */
17753 fn = TREE_CHAIN (fn);
17754 champ = fn;
17755 if (!fn)
17756 break;
17757 }
17758 }
17759
17760 if (champ)
17761 /* Now verify that champ is better than everything earlier in the
17762 instantiation list. */
17763 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17764 if (get_bindings (TREE_VALUE (champ),
17765 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17766 NULL_TREE, /*check_ret=*/true)
17767 || !get_bindings (TREE_VALUE (fn),
17768 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17769 NULL_TREE, /*check_ret=*/true))
17770 {
17771 champ = NULL_TREE;
17772 break;
17773 }
17774
17775 processing_template_decl--;
17776
17777 if (!champ)
17778 return error_mark_node;
17779
17780 return champ;
17781 }
17782
17783 /* If DECL is a specialization of some template, return the most
17784 general such template. Otherwise, returns NULL_TREE.
17785
17786 For example, given:
17787
17788 template <class T> struct S { template <class U> void f(U); };
17789
17790 if TMPL is `template <class U> void S<int>::f(U)' this will return
17791 the full template. This function will not trace past partial
17792 specializations, however. For example, given in addition:
17793
17794 template <class T> struct S<T*> { template <class U> void f(U); };
17795
17796 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17797 `template <class T> template <class U> S<T*>::f(U)'. */
17798
17799 tree
17800 most_general_template (tree decl)
17801 {
17802 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17803 an immediate specialization. */
17804 if (TREE_CODE (decl) == FUNCTION_DECL)
17805 {
17806 if (DECL_TEMPLATE_INFO (decl)) {
17807 decl = DECL_TI_TEMPLATE (decl);
17808
17809 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17810 template friend. */
17811 if (TREE_CODE (decl) != TEMPLATE_DECL)
17812 return NULL_TREE;
17813 } else
17814 return NULL_TREE;
17815 }
17816
17817 /* Look for more and more general templates. */
17818 while (DECL_TEMPLATE_INFO (decl))
17819 {
17820 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17821 (See cp-tree.h for details.) */
17822 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17823 break;
17824
17825 if (CLASS_TYPE_P (TREE_TYPE (decl))
17826 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17827 break;
17828
17829 /* Stop if we run into an explicitly specialized class template. */
17830 if (!DECL_NAMESPACE_SCOPE_P (decl)
17831 && DECL_CONTEXT (decl)
17832 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17833 break;
17834
17835 decl = DECL_TI_TEMPLATE (decl);
17836 }
17837
17838 return decl;
17839 }
17840
17841 /* Return the most specialized of the class template partial
17842 specializations of TMPL which can produce TYPE, a specialization of
17843 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17844 a _TYPE node corresponding to the partial specialization, while the
17845 TREE_PURPOSE is the set of template arguments that must be
17846 substituted into the TREE_TYPE in order to generate TYPE.
17847
17848 If the choice of partial specialization is ambiguous, a diagnostic
17849 is issued, and the error_mark_node is returned. If there are no
17850 partial specializations of TMPL matching TYPE, then NULL_TREE is
17851 returned. */
17852
17853 static tree
17854 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17855 {
17856 tree list = NULL_TREE;
17857 tree t;
17858 tree champ;
17859 int fate;
17860 bool ambiguous_p;
17861 tree args;
17862 tree outer_args = NULL_TREE;
17863
17864 tmpl = most_general_template (tmpl);
17865 args = CLASSTYPE_TI_ARGS (type);
17866
17867 /* For determining which partial specialization to use, only the
17868 innermost args are interesting. */
17869 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17870 {
17871 outer_args = strip_innermost_template_args (args, 1);
17872 args = INNERMOST_TEMPLATE_ARGS (args);
17873 }
17874
17875 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17876 {
17877 tree partial_spec_args;
17878 tree spec_args;
17879 tree parms = TREE_VALUE (t);
17880
17881 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17882
17883 ++processing_template_decl;
17884
17885 if (outer_args)
17886 {
17887 int i;
17888
17889 /* Discard the outer levels of args, and then substitute in the
17890 template args from the enclosing class. */
17891 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17892 partial_spec_args = tsubst_template_args
17893 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17894
17895 /* PARMS already refers to just the innermost parms, but the
17896 template parms in partial_spec_args had their levels lowered
17897 by tsubst, so we need to do the same for the parm list. We
17898 can't just tsubst the TREE_VEC itself, as tsubst wants to
17899 treat a TREE_VEC as an argument vector. */
17900 parms = copy_node (parms);
17901 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17902 TREE_VEC_ELT (parms, i) =
17903 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17904
17905 }
17906
17907 partial_spec_args =
17908 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17909 add_to_template_args (outer_args,
17910 partial_spec_args),
17911 tmpl, tf_none,
17912 /*require_all_args=*/true,
17913 /*use_default_args=*/true);
17914
17915 --processing_template_decl;
17916
17917 if (partial_spec_args == error_mark_node)
17918 return error_mark_node;
17919
17920 spec_args = get_class_bindings (tmpl, parms,
17921 partial_spec_args,
17922 args);
17923 if (spec_args)
17924 {
17925 if (outer_args)
17926 spec_args = add_to_template_args (outer_args, spec_args);
17927 list = tree_cons (spec_args, TREE_VALUE (t), list);
17928 TREE_TYPE (list) = TREE_TYPE (t);
17929 }
17930 }
17931
17932 if (! list)
17933 return NULL_TREE;
17934
17935 ambiguous_p = false;
17936 t = list;
17937 champ = t;
17938 t = TREE_CHAIN (t);
17939 for (; t; t = TREE_CHAIN (t))
17940 {
17941 fate = more_specialized_class (tmpl, champ, t);
17942 if (fate == 1)
17943 ;
17944 else
17945 {
17946 if (fate == 0)
17947 {
17948 t = TREE_CHAIN (t);
17949 if (! t)
17950 {
17951 ambiguous_p = true;
17952 break;
17953 }
17954 }
17955 champ = t;
17956 }
17957 }
17958
17959 if (!ambiguous_p)
17960 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17961 {
17962 fate = more_specialized_class (tmpl, champ, t);
17963 if (fate != 1)
17964 {
17965 ambiguous_p = true;
17966 break;
17967 }
17968 }
17969
17970 if (ambiguous_p)
17971 {
17972 const char *str;
17973 char *spaces = NULL;
17974 if (!(complain & tf_error))
17975 return error_mark_node;
17976 error ("ambiguous class template instantiation for %q#T", type);
17977 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17978 for (t = list; t; t = TREE_CHAIN (t))
17979 {
17980 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17981 spaces = spaces ? spaces : get_spaces (str);
17982 }
17983 free (spaces);
17984 return error_mark_node;
17985 }
17986
17987 return champ;
17988 }
17989
17990 /* Explicitly instantiate DECL. */
17991
17992 void
17993 do_decl_instantiation (tree decl, tree storage)
17994 {
17995 tree result = NULL_TREE;
17996 int extern_p = 0;
17997
17998 if (!decl || decl == error_mark_node)
17999 /* An error occurred, for which grokdeclarator has already issued
18000 an appropriate message. */
18001 return;
18002 else if (! DECL_LANG_SPECIFIC (decl))
18003 {
18004 error ("explicit instantiation of non-template %q#D", decl);
18005 return;
18006 }
18007 else if (TREE_CODE (decl) == VAR_DECL)
18008 {
18009 /* There is an asymmetry here in the way VAR_DECLs and
18010 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18011 the latter, the DECL we get back will be marked as a
18012 template instantiation, and the appropriate
18013 DECL_TEMPLATE_INFO will be set up. This does not happen for
18014 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18015 should handle VAR_DECLs as it currently handles
18016 FUNCTION_DECLs. */
18017 if (!DECL_CLASS_SCOPE_P (decl))
18018 {
18019 error ("%qD is not a static data member of a class template", decl);
18020 return;
18021 }
18022 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18023 if (!result || TREE_CODE (result) != VAR_DECL)
18024 {
18025 error ("no matching template for %qD found", decl);
18026 return;
18027 }
18028 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18029 {
18030 error ("type %qT for explicit instantiation %qD does not match "
18031 "declared type %qT", TREE_TYPE (result), decl,
18032 TREE_TYPE (decl));
18033 return;
18034 }
18035 }
18036 else if (TREE_CODE (decl) != FUNCTION_DECL)
18037 {
18038 error ("explicit instantiation of %q#D", decl);
18039 return;
18040 }
18041 else
18042 result = decl;
18043
18044 /* Check for various error cases. Note that if the explicit
18045 instantiation is valid the RESULT will currently be marked as an
18046 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18047 until we get here. */
18048
18049 if (DECL_TEMPLATE_SPECIALIZATION (result))
18050 {
18051 /* DR 259 [temp.spec].
18052
18053 Both an explicit instantiation and a declaration of an explicit
18054 specialization shall not appear in a program unless the explicit
18055 instantiation follows a declaration of the explicit specialization.
18056
18057 For a given set of template parameters, if an explicit
18058 instantiation of a template appears after a declaration of an
18059 explicit specialization for that template, the explicit
18060 instantiation has no effect. */
18061 return;
18062 }
18063 else if (DECL_EXPLICIT_INSTANTIATION (result))
18064 {
18065 /* [temp.spec]
18066
18067 No program shall explicitly instantiate any template more
18068 than once.
18069
18070 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18071 the first instantiation was `extern' and the second is not,
18072 and EXTERN_P for the opposite case. */
18073 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18074 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18075 /* If an "extern" explicit instantiation follows an ordinary
18076 explicit instantiation, the template is instantiated. */
18077 if (extern_p)
18078 return;
18079 }
18080 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18081 {
18082 error ("no matching template for %qD found", result);
18083 return;
18084 }
18085 else if (!DECL_TEMPLATE_INFO (result))
18086 {
18087 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18088 return;
18089 }
18090
18091 if (storage == NULL_TREE)
18092 ;
18093 else if (storage == ridpointers[(int) RID_EXTERN])
18094 {
18095 if (!in_system_header && (cxx_dialect == cxx98))
18096 pedwarn (input_location, OPT_Wpedantic,
18097 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18098 "instantiations");
18099 extern_p = 1;
18100 }
18101 else
18102 error ("storage class %qD applied to template instantiation", storage);
18103
18104 check_explicit_instantiation_namespace (result);
18105 mark_decl_instantiated (result, extern_p);
18106 if (! extern_p)
18107 instantiate_decl (result, /*defer_ok=*/1,
18108 /*expl_inst_class_mem_p=*/false);
18109 }
18110
18111 static void
18112 mark_class_instantiated (tree t, int extern_p)
18113 {
18114 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18115 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18116 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18117 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18118 if (! extern_p)
18119 {
18120 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18121 rest_of_type_compilation (t, 1);
18122 }
18123 }
18124
18125 /* Called from do_type_instantiation through binding_table_foreach to
18126 do recursive instantiation for the type bound in ENTRY. */
18127 static void
18128 bt_instantiate_type_proc (binding_entry entry, void *data)
18129 {
18130 tree storage = *(tree *) data;
18131
18132 if (MAYBE_CLASS_TYPE_P (entry->type)
18133 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18134 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18135 }
18136
18137 /* Called from do_type_instantiation to instantiate a member
18138 (a member function or a static member variable) of an
18139 explicitly instantiated class template. */
18140 static void
18141 instantiate_class_member (tree decl, int extern_p)
18142 {
18143 mark_decl_instantiated (decl, extern_p);
18144 if (! extern_p)
18145 instantiate_decl (decl, /*defer_ok=*/1,
18146 /*expl_inst_class_mem_p=*/true);
18147 }
18148
18149 /* Perform an explicit instantiation of template class T. STORAGE, if
18150 non-null, is the RID for extern, inline or static. COMPLAIN is
18151 nonzero if this is called from the parser, zero if called recursively,
18152 since the standard is unclear (as detailed below). */
18153
18154 void
18155 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18156 {
18157 int extern_p = 0;
18158 int nomem_p = 0;
18159 int static_p = 0;
18160 int previous_instantiation_extern_p = 0;
18161
18162 if (TREE_CODE (t) == TYPE_DECL)
18163 t = TREE_TYPE (t);
18164
18165 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18166 {
18167 tree tmpl =
18168 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18169 if (tmpl)
18170 error ("explicit instantiation of non-class template %qD", tmpl);
18171 else
18172 error ("explicit instantiation of non-template type %qT", t);
18173 return;
18174 }
18175
18176 complete_type (t);
18177
18178 if (!COMPLETE_TYPE_P (t))
18179 {
18180 if (complain & tf_error)
18181 error ("explicit instantiation of %q#T before definition of template",
18182 t);
18183 return;
18184 }
18185
18186 if (storage != NULL_TREE)
18187 {
18188 if (!in_system_header)
18189 {
18190 if (storage == ridpointers[(int) RID_EXTERN])
18191 {
18192 if (cxx_dialect == cxx98)
18193 pedwarn (input_location, OPT_Wpedantic,
18194 "ISO C++ 1998 forbids the use of %<extern%> on "
18195 "explicit instantiations");
18196 }
18197 else
18198 pedwarn (input_location, OPT_Wpedantic,
18199 "ISO C++ forbids the use of %qE"
18200 " on explicit instantiations", storage);
18201 }
18202
18203 if (storage == ridpointers[(int) RID_INLINE])
18204 nomem_p = 1;
18205 else if (storage == ridpointers[(int) RID_EXTERN])
18206 extern_p = 1;
18207 else if (storage == ridpointers[(int) RID_STATIC])
18208 static_p = 1;
18209 else
18210 {
18211 error ("storage class %qD applied to template instantiation",
18212 storage);
18213 extern_p = 0;
18214 }
18215 }
18216
18217 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18218 {
18219 /* DR 259 [temp.spec].
18220
18221 Both an explicit instantiation and a declaration of an explicit
18222 specialization shall not appear in a program unless the explicit
18223 instantiation follows a declaration of the explicit specialization.
18224
18225 For a given set of template parameters, if an explicit
18226 instantiation of a template appears after a declaration of an
18227 explicit specialization for that template, the explicit
18228 instantiation has no effect. */
18229 return;
18230 }
18231 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18232 {
18233 /* [temp.spec]
18234
18235 No program shall explicitly instantiate any template more
18236 than once.
18237
18238 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18239 instantiation was `extern'. If EXTERN_P then the second is.
18240 These cases are OK. */
18241 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18242
18243 if (!previous_instantiation_extern_p && !extern_p
18244 && (complain & tf_error))
18245 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18246
18247 /* If we've already instantiated the template, just return now. */
18248 if (!CLASSTYPE_INTERFACE_ONLY (t))
18249 return;
18250 }
18251
18252 check_explicit_instantiation_namespace (TYPE_NAME (t));
18253 mark_class_instantiated (t, extern_p);
18254
18255 if (nomem_p)
18256 return;
18257
18258 {
18259 tree tmp;
18260
18261 /* In contrast to implicit instantiation, where only the
18262 declarations, and not the definitions, of members are
18263 instantiated, we have here:
18264
18265 [temp.explicit]
18266
18267 The explicit instantiation of a class template specialization
18268 implies the instantiation of all of its members not
18269 previously explicitly specialized in the translation unit
18270 containing the explicit instantiation.
18271
18272 Of course, we can't instantiate member template classes, since
18273 we don't have any arguments for them. Note that the standard
18274 is unclear on whether the instantiation of the members are
18275 *explicit* instantiations or not. However, the most natural
18276 interpretation is that it should be an explicit instantiation. */
18277
18278 if (! static_p)
18279 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18280 if (TREE_CODE (tmp) == FUNCTION_DECL
18281 && DECL_TEMPLATE_INSTANTIATION (tmp))
18282 instantiate_class_member (tmp, extern_p);
18283
18284 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18285 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
18286 instantiate_class_member (tmp, extern_p);
18287
18288 if (CLASSTYPE_NESTED_UTDS (t))
18289 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18290 bt_instantiate_type_proc, &storage);
18291 }
18292 }
18293
18294 /* Given a function DECL, which is a specialization of TMPL, modify
18295 DECL to be a re-instantiation of TMPL with the same template
18296 arguments. TMPL should be the template into which tsubst'ing
18297 should occur for DECL, not the most general template.
18298
18299 One reason for doing this is a scenario like this:
18300
18301 template <class T>
18302 void f(const T&, int i);
18303
18304 void g() { f(3, 7); }
18305
18306 template <class T>
18307 void f(const T& t, const int i) { }
18308
18309 Note that when the template is first instantiated, with
18310 instantiate_template, the resulting DECL will have no name for the
18311 first parameter, and the wrong type for the second. So, when we go
18312 to instantiate the DECL, we regenerate it. */
18313
18314 static void
18315 regenerate_decl_from_template (tree decl, tree tmpl)
18316 {
18317 /* The arguments used to instantiate DECL, from the most general
18318 template. */
18319 tree args;
18320 tree code_pattern;
18321
18322 args = DECL_TI_ARGS (decl);
18323 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
18324
18325 /* Make sure that we can see identifiers, and compute access
18326 correctly. */
18327 push_access_scope (decl);
18328
18329 if (TREE_CODE (decl) == FUNCTION_DECL)
18330 {
18331 tree decl_parm;
18332 tree pattern_parm;
18333 tree specs;
18334 int args_depth;
18335 int parms_depth;
18336
18337 args_depth = TMPL_ARGS_DEPTH (args);
18338 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
18339 if (args_depth > parms_depth)
18340 args = get_innermost_template_args (args, parms_depth);
18341
18342 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
18343 args, tf_error, NULL_TREE,
18344 /*defer_ok*/false);
18345 if (specs && specs != error_mark_node)
18346 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
18347 specs);
18348
18349 /* Merge parameter declarations. */
18350 decl_parm = skip_artificial_parms_for (decl,
18351 DECL_ARGUMENTS (decl));
18352 pattern_parm
18353 = skip_artificial_parms_for (code_pattern,
18354 DECL_ARGUMENTS (code_pattern));
18355 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
18356 {
18357 tree parm_type;
18358 tree attributes;
18359
18360 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18361 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
18362 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
18363 NULL_TREE);
18364 parm_type = type_decays_to (parm_type);
18365 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18366 TREE_TYPE (decl_parm) = parm_type;
18367 attributes = DECL_ATTRIBUTES (pattern_parm);
18368 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18369 {
18370 DECL_ATTRIBUTES (decl_parm) = attributes;
18371 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18372 }
18373 decl_parm = DECL_CHAIN (decl_parm);
18374 pattern_parm = DECL_CHAIN (pattern_parm);
18375 }
18376 /* Merge any parameters that match with the function parameter
18377 pack. */
18378 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
18379 {
18380 int i, len;
18381 tree expanded_types;
18382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18383 the parameters in this function parameter pack. */
18384 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
18385 args, tf_error, NULL_TREE);
18386 len = TREE_VEC_LENGTH (expanded_types);
18387 for (i = 0; i < len; i++)
18388 {
18389 tree parm_type;
18390 tree attributes;
18391
18392 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18393 /* Rename the parameter to include the index. */
18394 DECL_NAME (decl_parm) =
18395 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
18396 parm_type = TREE_VEC_ELT (expanded_types, i);
18397 parm_type = type_decays_to (parm_type);
18398 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18399 TREE_TYPE (decl_parm) = parm_type;
18400 attributes = DECL_ATTRIBUTES (pattern_parm);
18401 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18402 {
18403 DECL_ATTRIBUTES (decl_parm) = attributes;
18404 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18405 }
18406 decl_parm = DECL_CHAIN (decl_parm);
18407 }
18408 }
18409 /* Merge additional specifiers from the CODE_PATTERN. */
18410 if (DECL_DECLARED_INLINE_P (code_pattern)
18411 && !DECL_DECLARED_INLINE_P (decl))
18412 DECL_DECLARED_INLINE_P (decl) = 1;
18413 }
18414 else if (TREE_CODE (decl) == VAR_DECL)
18415 {
18416 DECL_INITIAL (decl) =
18417 tsubst_expr (DECL_INITIAL (code_pattern), args,
18418 tf_error, DECL_TI_TEMPLATE (decl),
18419 /*integral_constant_expression_p=*/false);
18420 if (VAR_HAD_UNKNOWN_BOUND (decl))
18421 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18422 tf_error, DECL_TI_TEMPLATE (decl));
18423 }
18424 else
18425 gcc_unreachable ();
18426
18427 pop_access_scope (decl);
18428 }
18429
18430 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18431 substituted to get DECL. */
18432
18433 tree
18434 template_for_substitution (tree decl)
18435 {
18436 tree tmpl = DECL_TI_TEMPLATE (decl);
18437
18438 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18439 for the instantiation. This is not always the most general
18440 template. Consider, for example:
18441
18442 template <class T>
18443 struct S { template <class U> void f();
18444 template <> void f<int>(); };
18445
18446 and an instantiation of S<double>::f<int>. We want TD to be the
18447 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18448 while (/* An instantiation cannot have a definition, so we need a
18449 more general template. */
18450 DECL_TEMPLATE_INSTANTIATION (tmpl)
18451 /* We must also deal with friend templates. Given:
18452
18453 template <class T> struct S {
18454 template <class U> friend void f() {};
18455 };
18456
18457 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18458 so far as the language is concerned, but that's still
18459 where we get the pattern for the instantiation from. On
18460 other hand, if the definition comes outside the class, say:
18461
18462 template <class T> struct S {
18463 template <class U> friend void f();
18464 };
18465 template <class U> friend void f() {}
18466
18467 we don't need to look any further. That's what the check for
18468 DECL_INITIAL is for. */
18469 || (TREE_CODE (decl) == FUNCTION_DECL
18470 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18471 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18472 {
18473 /* The present template, TD, should not be a definition. If it
18474 were a definition, we should be using it! Note that we
18475 cannot restructure the loop to just keep going until we find
18476 a template with a definition, since that might go too far if
18477 a specialization was declared, but not defined. */
18478 gcc_assert (TREE_CODE (decl) != VAR_DECL
18479 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18480
18481 /* Fetch the more general template. */
18482 tmpl = DECL_TI_TEMPLATE (tmpl);
18483 }
18484
18485 return tmpl;
18486 }
18487
18488 /* Returns true if we need to instantiate this template instance even if we
18489 know we aren't going to emit it.. */
18490
18491 bool
18492 always_instantiate_p (tree decl)
18493 {
18494 /* We always instantiate inline functions so that we can inline them. An
18495 explicit instantiation declaration prohibits implicit instantiation of
18496 non-inline functions. With high levels of optimization, we would
18497 normally inline non-inline functions -- but we're not allowed to do
18498 that for "extern template" functions. Therefore, we check
18499 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18500 return ((TREE_CODE (decl) == FUNCTION_DECL
18501 && (DECL_DECLARED_INLINE_P (decl)
18502 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
18503 /* And we need to instantiate static data members so that
18504 their initializers are available in integral constant
18505 expressions. */
18506 || (TREE_CODE (decl) == VAR_DECL
18507 && decl_maybe_constant_var_p (decl)));
18508 }
18509
18510 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18511 instantiate it now, modifying TREE_TYPE (fn). */
18512
18513 void
18514 maybe_instantiate_noexcept (tree fn)
18515 {
18516 tree fntype, spec, noex, clone;
18517
18518 if (DECL_CLONED_FUNCTION_P (fn))
18519 fn = DECL_CLONED_FUNCTION (fn);
18520 fntype = TREE_TYPE (fn);
18521 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18522
18523 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18524 return;
18525
18526 noex = TREE_PURPOSE (spec);
18527
18528 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18529 {
18530 if (push_tinst_level (fn))
18531 {
18532 push_access_scope (fn);
18533 push_deferring_access_checks (dk_no_deferred);
18534 input_location = DECL_SOURCE_LOCATION (fn);
18535 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18536 DEFERRED_NOEXCEPT_ARGS (noex),
18537 tf_warning_or_error, fn,
18538 /*function_p=*/false,
18539 /*integral_constant_expression_p=*/true);
18540 pop_deferring_access_checks ();
18541 pop_access_scope (fn);
18542 pop_tinst_level ();
18543 spec = build_noexcept_spec (noex, tf_warning_or_error);
18544 if (spec == error_mark_node)
18545 spec = noexcept_false_spec;
18546 }
18547 else
18548 spec = noexcept_false_spec;
18549 }
18550 else
18551 {
18552 /* This is an implicitly declared function, so NOEX is a list of
18553 other functions to evaluate and merge. */
18554 tree elt;
18555 spec = noexcept_true_spec;
18556 for (elt = noex; elt; elt = OVL_NEXT (elt))
18557 {
18558 tree fn = OVL_CURRENT (elt);
18559 tree subspec;
18560 maybe_instantiate_noexcept (fn);
18561 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18562 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18563 }
18564 }
18565
18566 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18567
18568 FOR_EACH_CLONE (clone, fn)
18569 {
18570 if (TREE_TYPE (clone) == fntype)
18571 TREE_TYPE (clone) = TREE_TYPE (fn);
18572 else
18573 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18574 }
18575 }
18576
18577 /* Produce the definition of D, a _DECL generated from a template. If
18578 DEFER_OK is nonzero, then we don't have to actually do the
18579 instantiation now; we just have to do it sometime. Normally it is
18580 an error if this is an explicit instantiation but D is undefined.
18581 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18582 explicitly instantiated class template. */
18583
18584 tree
18585 instantiate_decl (tree d, int defer_ok,
18586 bool expl_inst_class_mem_p)
18587 {
18588 tree tmpl = DECL_TI_TEMPLATE (d);
18589 tree gen_args;
18590 tree args;
18591 tree td;
18592 tree code_pattern;
18593 tree spec;
18594 tree gen_tmpl;
18595 bool pattern_defined;
18596 location_t saved_loc = input_location;
18597 bool external_p;
18598 tree fn_context;
18599 bool nested;
18600
18601 /* This function should only be used to instantiate templates for
18602 functions and static member variables. */
18603 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18604 || TREE_CODE (d) == VAR_DECL);
18605
18606 /* Variables are never deferred; if instantiation is required, they
18607 are instantiated right away. That allows for better code in the
18608 case that an expression refers to the value of the variable --
18609 if the variable has a constant value the referring expression can
18610 take advantage of that fact. */
18611 if (TREE_CODE (d) == VAR_DECL
18612 || DECL_DECLARED_CONSTEXPR_P (d))
18613 defer_ok = 0;
18614
18615 /* Don't instantiate cloned functions. Instead, instantiate the
18616 functions they cloned. */
18617 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18618 d = DECL_CLONED_FUNCTION (d);
18619
18620 if (DECL_TEMPLATE_INSTANTIATED (d)
18621 || (TREE_CODE (d) == FUNCTION_DECL
18622 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18623 || DECL_TEMPLATE_SPECIALIZATION (d))
18624 /* D has already been instantiated or explicitly specialized, so
18625 there's nothing for us to do here.
18626
18627 It might seem reasonable to check whether or not D is an explicit
18628 instantiation, and, if so, stop here. But when an explicit
18629 instantiation is deferred until the end of the compilation,
18630 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18631 the instantiation. */
18632 return d;
18633
18634 /* Check to see whether we know that this template will be
18635 instantiated in some other file, as with "extern template"
18636 extension. */
18637 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18638
18639 /* In general, we do not instantiate such templates. */
18640 if (external_p && !always_instantiate_p (d))
18641 return d;
18642
18643 gen_tmpl = most_general_template (tmpl);
18644 gen_args = DECL_TI_ARGS (d);
18645
18646 if (tmpl != gen_tmpl)
18647 /* We should already have the extra args. */
18648 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18649 == TMPL_ARGS_DEPTH (gen_args));
18650 /* And what's in the hash table should match D. */
18651 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18652 || spec == NULL_TREE);
18653
18654 /* This needs to happen before any tsubsting. */
18655 if (! push_tinst_level (d))
18656 return d;
18657
18658 timevar_push (TV_TEMPLATE_INST);
18659
18660 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18661 for the instantiation. */
18662 td = template_for_substitution (d);
18663 code_pattern = DECL_TEMPLATE_RESULT (td);
18664
18665 /* We should never be trying to instantiate a member of a class
18666 template or partial specialization. */
18667 gcc_assert (d != code_pattern);
18668
18669 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18670 || DECL_TEMPLATE_SPECIALIZATION (td))
18671 /* In the case of a friend template whose definition is provided
18672 outside the class, we may have too many arguments. Drop the
18673 ones we don't need. The same is true for specializations. */
18674 args = get_innermost_template_args
18675 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18676 else
18677 args = gen_args;
18678
18679 if (TREE_CODE (d) == FUNCTION_DECL)
18680 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18681 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18682 else
18683 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18684
18685 /* We may be in the middle of deferred access check. Disable it now. */
18686 push_deferring_access_checks (dk_no_deferred);
18687
18688 /* Unless an explicit instantiation directive has already determined
18689 the linkage of D, remember that a definition is available for
18690 this entity. */
18691 if (pattern_defined
18692 && !DECL_INTERFACE_KNOWN (d)
18693 && !DECL_NOT_REALLY_EXTERN (d))
18694 mark_definable (d);
18695
18696 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18697 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18698 input_location = DECL_SOURCE_LOCATION (d);
18699
18700 /* If D is a member of an explicitly instantiated class template,
18701 and no definition is available, treat it like an implicit
18702 instantiation. */
18703 if (!pattern_defined && expl_inst_class_mem_p
18704 && DECL_EXPLICIT_INSTANTIATION (d))
18705 {
18706 /* Leave linkage flags alone on instantiations with anonymous
18707 visibility. */
18708 if (TREE_PUBLIC (d))
18709 {
18710 DECL_NOT_REALLY_EXTERN (d) = 0;
18711 DECL_INTERFACE_KNOWN (d) = 0;
18712 }
18713 SET_DECL_IMPLICIT_INSTANTIATION (d);
18714 }
18715
18716 if (TREE_CODE (d) == FUNCTION_DECL)
18717 maybe_instantiate_noexcept (d);
18718
18719 /* Defer all other templates, unless we have been explicitly
18720 forbidden from doing so. */
18721 if (/* If there is no definition, we cannot instantiate the
18722 template. */
18723 ! pattern_defined
18724 /* If it's OK to postpone instantiation, do so. */
18725 || defer_ok
18726 /* If this is a static data member that will be defined
18727 elsewhere, we don't want to instantiate the entire data
18728 member, but we do want to instantiate the initializer so that
18729 we can substitute that elsewhere. */
18730 || (external_p && TREE_CODE (d) == VAR_DECL))
18731 {
18732 /* The definition of the static data member is now required so
18733 we must substitute the initializer. */
18734 if (TREE_CODE (d) == VAR_DECL
18735 && !DECL_INITIAL (d)
18736 && DECL_INITIAL (code_pattern))
18737 {
18738 tree ns;
18739 tree init;
18740 bool const_init = false;
18741
18742 ns = decl_namespace_context (d);
18743 push_nested_namespace (ns);
18744 push_nested_class (DECL_CONTEXT (d));
18745 init = tsubst_expr (DECL_INITIAL (code_pattern),
18746 args,
18747 tf_warning_or_error, NULL_TREE,
18748 /*integral_constant_expression_p=*/false);
18749 /* Make sure the initializer is still constant, in case of
18750 circular dependency (template/instantiate6.C). */
18751 const_init
18752 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18753 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18754 /*asmspec_tree=*/NULL_TREE,
18755 LOOKUP_ONLYCONVERTING);
18756 pop_nested_class ();
18757 pop_nested_namespace (ns);
18758 }
18759
18760 /* We restore the source position here because it's used by
18761 add_pending_template. */
18762 input_location = saved_loc;
18763
18764 if (at_eof && !pattern_defined
18765 && DECL_EXPLICIT_INSTANTIATION (d)
18766 && DECL_NOT_REALLY_EXTERN (d))
18767 /* [temp.explicit]
18768
18769 The definition of a non-exported function template, a
18770 non-exported member function template, or a non-exported
18771 member function or static data member of a class template
18772 shall be present in every translation unit in which it is
18773 explicitly instantiated. */
18774 permerror (input_location, "explicit instantiation of %qD "
18775 "but no definition available", d);
18776
18777 /* If we're in unevaluated context, we just wanted to get the
18778 constant value; this isn't an odr use, so don't queue
18779 a full instantiation. */
18780 if (cp_unevaluated_operand != 0)
18781 goto out;
18782 /* ??? Historically, we have instantiated inline functions, even
18783 when marked as "extern template". */
18784 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18785 add_pending_template (d);
18786 goto out;
18787 }
18788 /* Tell the repository that D is available in this translation unit
18789 -- and see if it is supposed to be instantiated here. */
18790 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18791 {
18792 /* In a PCH file, despite the fact that the repository hasn't
18793 requested instantiation in the PCH it is still possible that
18794 an instantiation will be required in a file that includes the
18795 PCH. */
18796 if (pch_file)
18797 add_pending_template (d);
18798 /* Instantiate inline functions so that the inliner can do its
18799 job, even though we'll not be emitting a copy of this
18800 function. */
18801 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18802 goto out;
18803 }
18804
18805 fn_context = decl_function_context (d);
18806 nested = (current_function_decl != NULL_TREE);
18807 if (!fn_context)
18808 push_to_top_level ();
18809 else if (nested)
18810 push_function_context ();
18811
18812 /* Mark D as instantiated so that recursive calls to
18813 instantiate_decl do not try to instantiate it again. */
18814 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18815
18816 /* Regenerate the declaration in case the template has been modified
18817 by a subsequent redeclaration. */
18818 regenerate_decl_from_template (d, td);
18819
18820 /* We already set the file and line above. Reset them now in case
18821 they changed as a result of calling regenerate_decl_from_template. */
18822 input_location = DECL_SOURCE_LOCATION (d);
18823
18824 if (TREE_CODE (d) == VAR_DECL)
18825 {
18826 tree init;
18827 bool const_init = false;
18828
18829 /* Clear out DECL_RTL; whatever was there before may not be right
18830 since we've reset the type of the declaration. */
18831 SET_DECL_RTL (d, NULL);
18832 DECL_IN_AGGR_P (d) = 0;
18833
18834 /* The initializer is placed in DECL_INITIAL by
18835 regenerate_decl_from_template so we don't need to
18836 push/pop_access_scope again here. Pull it out so that
18837 cp_finish_decl can process it. */
18838 init = DECL_INITIAL (d);
18839 DECL_INITIAL (d) = NULL_TREE;
18840 DECL_INITIALIZED_P (d) = 0;
18841
18842 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18843 initializer. That function will defer actual emission until
18844 we have a chance to determine linkage. */
18845 DECL_EXTERNAL (d) = 0;
18846
18847 /* Enter the scope of D so that access-checking works correctly. */
18848 push_nested_class (DECL_CONTEXT (d));
18849 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18850 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18851 pop_nested_class ();
18852 }
18853 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18854 synthesize_method (d);
18855 else if (TREE_CODE (d) == FUNCTION_DECL)
18856 {
18857 struct pointer_map_t *saved_local_specializations;
18858 tree subst_decl;
18859 tree tmpl_parm;
18860 tree spec_parm;
18861
18862 /* Save away the current list, in case we are instantiating one
18863 template from within the body of another. */
18864 saved_local_specializations = local_specializations;
18865
18866 /* Set up the list of local specializations. */
18867 local_specializations = pointer_map_create ();
18868
18869 /* Set up context. */
18870 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18871
18872 /* Some typedefs referenced from within the template code need to be
18873 access checked at template instantiation time, i.e now. These
18874 types were added to the template at parsing time. Let's get those
18875 and perform the access checks then. */
18876 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18877 gen_args);
18878
18879 /* Create substitution entries for the parameters. */
18880 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18881 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18882 spec_parm = DECL_ARGUMENTS (d);
18883 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18884 {
18885 register_local_specialization (spec_parm, tmpl_parm);
18886 spec_parm = skip_artificial_parms_for (d, spec_parm);
18887 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18888 }
18889 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18890 {
18891 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18892 {
18893 register_local_specialization (spec_parm, tmpl_parm);
18894 spec_parm = DECL_CHAIN (spec_parm);
18895 }
18896 else
18897 {
18898 /* Register the (value) argument pack as a specialization of
18899 TMPL_PARM, then move on. */
18900 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18901 register_local_specialization (argpack, tmpl_parm);
18902 }
18903 }
18904 gcc_assert (!spec_parm);
18905
18906 /* Substitute into the body of the function. */
18907 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18908 tf_warning_or_error, tmpl,
18909 /*integral_constant_expression_p=*/false);
18910
18911 /* Set the current input_location to the end of the function
18912 so that finish_function knows where we are. */
18913 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18914
18915 /* We don't need the local specializations any more. */
18916 pointer_map_destroy (local_specializations);
18917 local_specializations = saved_local_specializations;
18918
18919 /* Finish the function. */
18920 d = finish_function (0);
18921 expand_or_defer_fn (d);
18922 }
18923
18924 /* We're not deferring instantiation any more. */
18925 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18926
18927 if (!fn_context)
18928 pop_from_top_level ();
18929 else if (nested)
18930 pop_function_context ();
18931
18932 out:
18933 input_location = saved_loc;
18934 pop_deferring_access_checks ();
18935 pop_tinst_level ();
18936
18937 timevar_pop (TV_TEMPLATE_INST);
18938
18939 return d;
18940 }
18941
18942 /* Run through the list of templates that we wish we could
18943 instantiate, and instantiate any we can. RETRIES is the
18944 number of times we retry pending template instantiation. */
18945
18946 void
18947 instantiate_pending_templates (int retries)
18948 {
18949 int reconsider;
18950 location_t saved_loc = input_location;
18951
18952 /* Instantiating templates may trigger vtable generation. This in turn
18953 may require further template instantiations. We place a limit here
18954 to avoid infinite loop. */
18955 if (pending_templates && retries >= max_tinst_depth)
18956 {
18957 tree decl = pending_templates->tinst->decl;
18958
18959 error ("template instantiation depth exceeds maximum of %d"
18960 " instantiating %q+D, possibly from virtual table generation"
18961 " (use -ftemplate-depth= to increase the maximum)",
18962 max_tinst_depth, decl);
18963 if (TREE_CODE (decl) == FUNCTION_DECL)
18964 /* Pretend that we defined it. */
18965 DECL_INITIAL (decl) = error_mark_node;
18966 return;
18967 }
18968
18969 do
18970 {
18971 struct pending_template **t = &pending_templates;
18972 struct pending_template *last = NULL;
18973 reconsider = 0;
18974 while (*t)
18975 {
18976 tree instantiation = reopen_tinst_level ((*t)->tinst);
18977 bool complete = false;
18978
18979 if (TYPE_P (instantiation))
18980 {
18981 tree fn;
18982
18983 if (!COMPLETE_TYPE_P (instantiation))
18984 {
18985 instantiate_class_template (instantiation);
18986 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18987 for (fn = TYPE_METHODS (instantiation);
18988 fn;
18989 fn = TREE_CHAIN (fn))
18990 if (! DECL_ARTIFICIAL (fn))
18991 instantiate_decl (fn,
18992 /*defer_ok=*/0,
18993 /*expl_inst_class_mem_p=*/false);
18994 if (COMPLETE_TYPE_P (instantiation))
18995 reconsider = 1;
18996 }
18997
18998 complete = COMPLETE_TYPE_P (instantiation);
18999 }
19000 else
19001 {
19002 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19003 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19004 {
19005 instantiation
19006 = instantiate_decl (instantiation,
19007 /*defer_ok=*/0,
19008 /*expl_inst_class_mem_p=*/false);
19009 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19010 reconsider = 1;
19011 }
19012
19013 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19014 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19015 }
19016
19017 if (complete)
19018 /* If INSTANTIATION has been instantiated, then we don't
19019 need to consider it again in the future. */
19020 *t = (*t)->next;
19021 else
19022 {
19023 last = *t;
19024 t = &(*t)->next;
19025 }
19026 tinst_depth = 0;
19027 current_tinst_level = NULL;
19028 }
19029 last_pending_template = last;
19030 }
19031 while (reconsider);
19032
19033 input_location = saved_loc;
19034 }
19035
19036 /* Substitute ARGVEC into T, which is a list of initializers for
19037 either base class or a non-static data member. The TREE_PURPOSEs
19038 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19039 instantiate_decl. */
19040
19041 static tree
19042 tsubst_initializer_list (tree t, tree argvec)
19043 {
19044 tree inits = NULL_TREE;
19045
19046 for (; t; t = TREE_CHAIN (t))
19047 {
19048 tree decl;
19049 tree init;
19050 tree expanded_bases = NULL_TREE;
19051 tree expanded_arguments = NULL_TREE;
19052 int i, len = 1;
19053
19054 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19055 {
19056 tree expr;
19057 tree arg;
19058
19059 /* Expand the base class expansion type into separate base
19060 classes. */
19061 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19062 tf_warning_or_error,
19063 NULL_TREE);
19064 if (expanded_bases == error_mark_node)
19065 continue;
19066
19067 /* We'll be building separate TREE_LISTs of arguments for
19068 each base. */
19069 len = TREE_VEC_LENGTH (expanded_bases);
19070 expanded_arguments = make_tree_vec (len);
19071 for (i = 0; i < len; i++)
19072 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19073
19074 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19075 expand each argument in the TREE_VALUE of t. */
19076 expr = make_node (EXPR_PACK_EXPANSION);
19077 PACK_EXPANSION_LOCAL_P (expr) = true;
19078 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19079 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19080
19081 if (TREE_VALUE (t) == void_type_node)
19082 /* VOID_TYPE_NODE is used to indicate
19083 value-initialization. */
19084 {
19085 for (i = 0; i < len; i++)
19086 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19087 }
19088 else
19089 {
19090 /* Substitute parameter packs into each argument in the
19091 TREE_LIST. */
19092 in_base_initializer = 1;
19093 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19094 {
19095 tree expanded_exprs;
19096
19097 /* Expand the argument. */
19098 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19099 expanded_exprs
19100 = tsubst_pack_expansion (expr, argvec,
19101 tf_warning_or_error,
19102 NULL_TREE);
19103 if (expanded_exprs == error_mark_node)
19104 continue;
19105
19106 /* Prepend each of the expanded expressions to the
19107 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19108 for (i = 0; i < len; i++)
19109 {
19110 TREE_VEC_ELT (expanded_arguments, i) =
19111 tree_cons (NULL_TREE,
19112 TREE_VEC_ELT (expanded_exprs, i),
19113 TREE_VEC_ELT (expanded_arguments, i));
19114 }
19115 }
19116 in_base_initializer = 0;
19117
19118 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19119 since we built them backwards. */
19120 for (i = 0; i < len; i++)
19121 {
19122 TREE_VEC_ELT (expanded_arguments, i) =
19123 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19124 }
19125 }
19126 }
19127
19128 for (i = 0; i < len; ++i)
19129 {
19130 if (expanded_bases)
19131 {
19132 decl = TREE_VEC_ELT (expanded_bases, i);
19133 decl = expand_member_init (decl);
19134 init = TREE_VEC_ELT (expanded_arguments, i);
19135 }
19136 else
19137 {
19138 tree tmp;
19139 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19140 tf_warning_or_error, NULL_TREE);
19141
19142 decl = expand_member_init (decl);
19143 if (decl && !DECL_P (decl))
19144 in_base_initializer = 1;
19145
19146 init = TREE_VALUE (t);
19147 tmp = init;
19148 if (init != void_type_node)
19149 init = tsubst_expr (init, argvec,
19150 tf_warning_or_error, NULL_TREE,
19151 /*integral_constant_expression_p=*/false);
19152 if (init == NULL_TREE && tmp != NULL_TREE)
19153 /* If we had an initializer but it instantiated to nothing,
19154 value-initialize the object. This will only occur when
19155 the initializer was a pack expansion where the parameter
19156 packs used in that expansion were of length zero. */
19157 init = void_type_node;
19158 in_base_initializer = 0;
19159 }
19160
19161 if (decl)
19162 {
19163 init = build_tree_list (decl, init);
19164 TREE_CHAIN (init) = inits;
19165 inits = init;
19166 }
19167 }
19168 }
19169 return inits;
19170 }
19171
19172 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19173
19174 static void
19175 set_current_access_from_decl (tree decl)
19176 {
19177 if (TREE_PRIVATE (decl))
19178 current_access_specifier = access_private_node;
19179 else if (TREE_PROTECTED (decl))
19180 current_access_specifier = access_protected_node;
19181 else
19182 current_access_specifier = access_public_node;
19183 }
19184
19185 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19186 is the instantiation (which should have been created with
19187 start_enum) and ARGS are the template arguments to use. */
19188
19189 static void
19190 tsubst_enum (tree tag, tree newtag, tree args)
19191 {
19192 tree e;
19193
19194 if (SCOPED_ENUM_P (newtag))
19195 begin_scope (sk_scoped_enum, newtag);
19196
19197 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19198 {
19199 tree value;
19200 tree decl;
19201
19202 decl = TREE_VALUE (e);
19203 /* Note that in a template enum, the TREE_VALUE is the
19204 CONST_DECL, not the corresponding INTEGER_CST. */
19205 value = tsubst_expr (DECL_INITIAL (decl),
19206 args, tf_warning_or_error, NULL_TREE,
19207 /*integral_constant_expression_p=*/true);
19208
19209 /* Give this enumeration constant the correct access. */
19210 set_current_access_from_decl (decl);
19211
19212 /* Actually build the enumerator itself. */
19213 build_enumerator
19214 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19215 }
19216
19217 if (SCOPED_ENUM_P (newtag))
19218 finish_scope ();
19219
19220 finish_enum_value_list (newtag);
19221 finish_enum (newtag);
19222
19223 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19224 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19225 }
19226
19227 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19228 its type -- but without substituting the innermost set of template
19229 arguments. So, innermost set of template parameters will appear in
19230 the type. */
19231
19232 tree
19233 get_mostly_instantiated_function_type (tree decl)
19234 {
19235 tree fn_type;
19236 tree tmpl;
19237 tree targs;
19238 tree tparms;
19239 int parm_depth;
19240
19241 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19242 targs = DECL_TI_ARGS (decl);
19243 tparms = DECL_TEMPLATE_PARMS (tmpl);
19244 parm_depth = TMPL_PARMS_DEPTH (tparms);
19245
19246 /* There should be as many levels of arguments as there are levels
19247 of parameters. */
19248 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19249
19250 fn_type = TREE_TYPE (tmpl);
19251
19252 if (parm_depth == 1)
19253 /* No substitution is necessary. */
19254 ;
19255 else
19256 {
19257 int i;
19258 tree partial_args;
19259
19260 /* Replace the innermost level of the TARGS with NULL_TREEs to
19261 let tsubst know not to substitute for those parameters. */
19262 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19263 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19264 SET_TMPL_ARGS_LEVEL (partial_args, i,
19265 TMPL_ARGS_LEVEL (targs, i));
19266 SET_TMPL_ARGS_LEVEL (partial_args,
19267 TMPL_ARGS_DEPTH (targs),
19268 make_tree_vec (DECL_NTPARMS (tmpl)));
19269
19270 /* Make sure that we can see identifiers, and compute access
19271 correctly. */
19272 push_access_scope (decl);
19273
19274 ++processing_template_decl;
19275 /* Now, do the (partial) substitution to figure out the
19276 appropriate function type. */
19277 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
19278 --processing_template_decl;
19279
19280 /* Substitute into the template parameters to obtain the real
19281 innermost set of parameters. This step is important if the
19282 innermost set of template parameters contains value
19283 parameters whose types depend on outer template parameters. */
19284 TREE_VEC_LENGTH (partial_args)--;
19285 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
19286
19287 pop_access_scope (decl);
19288 }
19289
19290 return fn_type;
19291 }
19292
19293 /* Return truthvalue if we're processing a template different from
19294 the last one involved in diagnostics. */
19295 int
19296 problematic_instantiation_changed (void)
19297 {
19298 return current_tinst_level != last_error_tinst_level;
19299 }
19300
19301 /* Remember current template involved in diagnostics. */
19302 void
19303 record_last_problematic_instantiation (void)
19304 {
19305 last_error_tinst_level = current_tinst_level;
19306 }
19307
19308 struct tinst_level *
19309 current_instantiation (void)
19310 {
19311 return current_tinst_level;
19312 }
19313
19314 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19315 type. Return zero for ok, nonzero for disallowed. Issue error and
19316 warning messages under control of COMPLAIN. */
19317
19318 static int
19319 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
19320 {
19321 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
19322 return 0;
19323 else if (POINTER_TYPE_P (type))
19324 return 0;
19325 else if (TYPE_PTRMEM_P (type))
19326 return 0;
19327 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
19328 return 0;
19329 else if (TREE_CODE (type) == TYPENAME_TYPE)
19330 return 0;
19331 else if (TREE_CODE (type) == DECLTYPE_TYPE)
19332 return 0;
19333 else if (TREE_CODE (type) == NULLPTR_TYPE)
19334 return 0;
19335
19336 if (complain & tf_error)
19337 {
19338 if (type == error_mark_node)
19339 inform (input_location, "invalid template non-type parameter");
19340 else
19341 error ("%q#T is not a valid type for a template non-type parameter",
19342 type);
19343 }
19344 return 1;
19345 }
19346
19347 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19348 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19349
19350 static bool
19351 dependent_type_p_r (tree type)
19352 {
19353 tree scope;
19354
19355 /* [temp.dep.type]
19356
19357 A type is dependent if it is:
19358
19359 -- a template parameter. Template template parameters are types
19360 for us (since TYPE_P holds true for them) so we handle
19361 them here. */
19362 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19363 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
19364 return true;
19365 /* -- a qualified-id with a nested-name-specifier which contains a
19366 class-name that names a dependent type or whose unqualified-id
19367 names a dependent type. */
19368 if (TREE_CODE (type) == TYPENAME_TYPE)
19369 return true;
19370 /* -- a cv-qualified type where the cv-unqualified type is
19371 dependent. */
19372 type = TYPE_MAIN_VARIANT (type);
19373 /* -- a compound type constructed from any dependent type. */
19374 if (TYPE_PTRMEM_P (type))
19375 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
19376 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19377 (type)));
19378 else if (TREE_CODE (type) == POINTER_TYPE
19379 || TREE_CODE (type) == REFERENCE_TYPE)
19380 return dependent_type_p (TREE_TYPE (type));
19381 else if (TREE_CODE (type) == FUNCTION_TYPE
19382 || TREE_CODE (type) == METHOD_TYPE)
19383 {
19384 tree arg_type;
19385
19386 if (dependent_type_p (TREE_TYPE (type)))
19387 return true;
19388 for (arg_type = TYPE_ARG_TYPES (type);
19389 arg_type;
19390 arg_type = TREE_CHAIN (arg_type))
19391 if (dependent_type_p (TREE_VALUE (arg_type)))
19392 return true;
19393 return false;
19394 }
19395 /* -- an array type constructed from any dependent type or whose
19396 size is specified by a constant expression that is
19397 value-dependent.
19398
19399 We checked for type- and value-dependence of the bounds in
19400 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19401 if (TREE_CODE (type) == ARRAY_TYPE)
19402 {
19403 if (TYPE_DOMAIN (type)
19404 && dependent_type_p (TYPE_DOMAIN (type)))
19405 return true;
19406 return dependent_type_p (TREE_TYPE (type));
19407 }
19408
19409 /* -- a template-id in which either the template name is a template
19410 parameter ... */
19411 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19412 return true;
19413 /* ... or any of the template arguments is a dependent type or
19414 an expression that is type-dependent or value-dependent. */
19415 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19416 && (any_dependent_template_arguments_p
19417 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19418 return true;
19419
19420 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19421 dependent; if the argument of the `typeof' expression is not
19422 type-dependent, then it should already been have resolved. */
19423 if (TREE_CODE (type) == TYPEOF_TYPE
19424 || TREE_CODE (type) == DECLTYPE_TYPE
19425 || TREE_CODE (type) == UNDERLYING_TYPE)
19426 return true;
19427
19428 /* A template argument pack is dependent if any of its packed
19429 arguments are. */
19430 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19431 {
19432 tree args = ARGUMENT_PACK_ARGS (type);
19433 int i, len = TREE_VEC_LENGTH (args);
19434 for (i = 0; i < len; ++i)
19435 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19436 return true;
19437 }
19438
19439 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19440 be template parameters. */
19441 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19442 return true;
19443
19444 /* The standard does not specifically mention types that are local
19445 to template functions or local classes, but they should be
19446 considered dependent too. For example:
19447
19448 template <int I> void f() {
19449 enum E { a = I };
19450 S<sizeof (E)> s;
19451 }
19452
19453 The size of `E' cannot be known until the value of `I' has been
19454 determined. Therefore, `E' must be considered dependent. */
19455 scope = TYPE_CONTEXT (type);
19456 if (scope && TYPE_P (scope))
19457 return dependent_type_p (scope);
19458 /* Don't use type_dependent_expression_p here, as it can lead
19459 to infinite recursion trying to determine whether a lambda
19460 nested in a lambda is dependent (c++/47687). */
19461 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19462 && DECL_LANG_SPECIFIC (scope)
19463 && DECL_TEMPLATE_INFO (scope)
19464 && (any_dependent_template_arguments_p
19465 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19466 return true;
19467
19468 /* Other types are non-dependent. */
19469 return false;
19470 }
19471
19472 /* Returns TRUE if TYPE is dependent, in the sense of
19473 [temp.dep.type]. Note that a NULL type is considered dependent. */
19474
19475 bool
19476 dependent_type_p (tree type)
19477 {
19478 /* If there are no template parameters in scope, then there can't be
19479 any dependent types. */
19480 if (!processing_template_decl)
19481 {
19482 /* If we are not processing a template, then nobody should be
19483 providing us with a dependent type. */
19484 gcc_assert (type);
19485 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19486 return false;
19487 }
19488
19489 /* If the type is NULL, we have not computed a type for the entity
19490 in question; in that case, the type is dependent. */
19491 if (!type)
19492 return true;
19493
19494 /* Erroneous types can be considered non-dependent. */
19495 if (type == error_mark_node)
19496 return false;
19497
19498 /* If we have not already computed the appropriate value for TYPE,
19499 do so now. */
19500 if (!TYPE_DEPENDENT_P_VALID (type))
19501 {
19502 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19503 TYPE_DEPENDENT_P_VALID (type) = 1;
19504 }
19505
19506 return TYPE_DEPENDENT_P (type);
19507 }
19508
19509 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19510 lookup. In other words, a dependent type that is not the current
19511 instantiation. */
19512
19513 bool
19514 dependent_scope_p (tree scope)
19515 {
19516 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19517 && !currently_open_class (scope));
19518 }
19519
19520 /* T is a SCOPE_REF; return whether we need to consider it
19521 instantiation-dependent so that we can check access at instantiation
19522 time even though we know which member it resolves to. */
19523
19524 static bool
19525 instantiation_dependent_scope_ref_p (tree t)
19526 {
19527 if (DECL_P (TREE_OPERAND (t, 1))
19528 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
19529 && accessible_in_template_p (TREE_OPERAND (t, 0),
19530 TREE_OPERAND (t, 1)))
19531 return false;
19532 else
19533 return true;
19534 }
19535
19536 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19537 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19538 expression. */
19539
19540 /* Note that this predicate is not appropriate for general expressions;
19541 only constant expressions (that satisfy potential_constant_expression)
19542 can be tested for value dependence. */
19543
19544 bool
19545 value_dependent_expression_p (tree expression)
19546 {
19547 if (!processing_template_decl)
19548 return false;
19549
19550 /* A name declared with a dependent type. */
19551 if (DECL_P (expression) && type_dependent_expression_p (expression))
19552 return true;
19553
19554 switch (TREE_CODE (expression))
19555 {
19556 case IDENTIFIER_NODE:
19557 /* A name that has not been looked up -- must be dependent. */
19558 return true;
19559
19560 case TEMPLATE_PARM_INDEX:
19561 /* A non-type template parm. */
19562 return true;
19563
19564 case CONST_DECL:
19565 /* A non-type template parm. */
19566 if (DECL_TEMPLATE_PARM_P (expression))
19567 return true;
19568 return value_dependent_expression_p (DECL_INITIAL (expression));
19569
19570 case VAR_DECL:
19571 /* A constant with literal type and is initialized
19572 with an expression that is value-dependent.
19573
19574 Note that a non-dependent parenthesized initializer will have
19575 already been replaced with its constant value, so if we see
19576 a TREE_LIST it must be dependent. */
19577 if (DECL_INITIAL (expression)
19578 && decl_constant_var_p (expression)
19579 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
19580 || value_dependent_expression_p (DECL_INITIAL (expression))))
19581 return true;
19582 return false;
19583
19584 case DYNAMIC_CAST_EXPR:
19585 case STATIC_CAST_EXPR:
19586 case CONST_CAST_EXPR:
19587 case REINTERPRET_CAST_EXPR:
19588 case CAST_EXPR:
19589 /* These expressions are value-dependent if the type to which
19590 the cast occurs is dependent or the expression being casted
19591 is value-dependent. */
19592 {
19593 tree type = TREE_TYPE (expression);
19594
19595 if (dependent_type_p (type))
19596 return true;
19597
19598 /* A functional cast has a list of operands. */
19599 expression = TREE_OPERAND (expression, 0);
19600 if (!expression)
19601 {
19602 /* If there are no operands, it must be an expression such
19603 as "int()". This should not happen for aggregate types
19604 because it would form non-constant expressions. */
19605 gcc_assert (cxx_dialect >= cxx0x
19606 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19607
19608 return false;
19609 }
19610
19611 if (TREE_CODE (expression) == TREE_LIST)
19612 return any_value_dependent_elements_p (expression);
19613
19614 return value_dependent_expression_p (expression);
19615 }
19616
19617 case SIZEOF_EXPR:
19618 if (SIZEOF_EXPR_TYPE_P (expression))
19619 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
19620 /* FALLTHRU */
19621 case ALIGNOF_EXPR:
19622 case TYPEID_EXPR:
19623 /* A `sizeof' expression is value-dependent if the operand is
19624 type-dependent or is a pack expansion. */
19625 expression = TREE_OPERAND (expression, 0);
19626 if (PACK_EXPANSION_P (expression))
19627 return true;
19628 else if (TYPE_P (expression))
19629 return dependent_type_p (expression);
19630 return instantiation_dependent_expression_p (expression);
19631
19632 case AT_ENCODE_EXPR:
19633 /* An 'encode' expression is value-dependent if the operand is
19634 type-dependent. */
19635 expression = TREE_OPERAND (expression, 0);
19636 return dependent_type_p (expression);
19637
19638 case NOEXCEPT_EXPR:
19639 expression = TREE_OPERAND (expression, 0);
19640 return instantiation_dependent_expression_p (expression);
19641
19642 case SCOPE_REF:
19643 /* All instantiation-dependent expressions should also be considered
19644 value-dependent. */
19645 return instantiation_dependent_scope_ref_p (expression);
19646
19647 case COMPONENT_REF:
19648 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19649 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19650
19651 case NONTYPE_ARGUMENT_PACK:
19652 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19653 is value-dependent. */
19654 {
19655 tree values = ARGUMENT_PACK_ARGS (expression);
19656 int i, len = TREE_VEC_LENGTH (values);
19657
19658 for (i = 0; i < len; ++i)
19659 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19660 return true;
19661
19662 return false;
19663 }
19664
19665 case TRAIT_EXPR:
19666 {
19667 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19668 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19669 || (type2 ? dependent_type_p (type2) : false));
19670 }
19671
19672 case MODOP_EXPR:
19673 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19674 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19675
19676 case ARRAY_REF:
19677 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19678 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19679
19680 case ADDR_EXPR:
19681 {
19682 tree op = TREE_OPERAND (expression, 0);
19683 return (value_dependent_expression_p (op)
19684 || has_value_dependent_address (op));
19685 }
19686
19687 case CALL_EXPR:
19688 {
19689 tree fn = get_callee_fndecl (expression);
19690 int i, nargs;
19691 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19692 return true;
19693 nargs = call_expr_nargs (expression);
19694 for (i = 0; i < nargs; ++i)
19695 {
19696 tree op = CALL_EXPR_ARG (expression, i);
19697 /* In a call to a constexpr member function, look through the
19698 implicit ADDR_EXPR on the object argument so that it doesn't
19699 cause the call to be considered value-dependent. We also
19700 look through it in potential_constant_expression. */
19701 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19702 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19703 && TREE_CODE (op) == ADDR_EXPR)
19704 op = TREE_OPERAND (op, 0);
19705 if (value_dependent_expression_p (op))
19706 return true;
19707 }
19708 return false;
19709 }
19710
19711 case TEMPLATE_ID_EXPR:
19712 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19713 type-dependent. */
19714 return type_dependent_expression_p (expression);
19715
19716 case CONSTRUCTOR:
19717 {
19718 unsigned ix;
19719 tree val;
19720 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19721 if (value_dependent_expression_p (val))
19722 return true;
19723 return false;
19724 }
19725
19726 case STMT_EXPR:
19727 /* Treat a GNU statement expression as dependent to avoid crashing
19728 under fold_non_dependent_expr; it can't be constant. */
19729 return true;
19730
19731 default:
19732 /* A constant expression is value-dependent if any subexpression is
19733 value-dependent. */
19734 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19735 {
19736 case tcc_reference:
19737 case tcc_unary:
19738 case tcc_comparison:
19739 case tcc_binary:
19740 case tcc_expression:
19741 case tcc_vl_exp:
19742 {
19743 int i, len = cp_tree_operand_length (expression);
19744
19745 for (i = 0; i < len; i++)
19746 {
19747 tree t = TREE_OPERAND (expression, i);
19748
19749 /* In some cases, some of the operands may be missing.l
19750 (For example, in the case of PREDECREMENT_EXPR, the
19751 amount to increment by may be missing.) That doesn't
19752 make the expression dependent. */
19753 if (t && value_dependent_expression_p (t))
19754 return true;
19755 }
19756 }
19757 break;
19758 default:
19759 break;
19760 }
19761 break;
19762 }
19763
19764 /* The expression is not value-dependent. */
19765 return false;
19766 }
19767
19768 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19769 [temp.dep.expr]. Note that an expression with no type is
19770 considered dependent. Other parts of the compiler arrange for an
19771 expression with type-dependent subexpressions to have no type, so
19772 this function doesn't have to be fully recursive. */
19773
19774 bool
19775 type_dependent_expression_p (tree expression)
19776 {
19777 if (!processing_template_decl)
19778 return false;
19779
19780 if (expression == error_mark_node)
19781 return false;
19782
19783 /* An unresolved name is always dependent. */
19784 if (TREE_CODE (expression) == IDENTIFIER_NODE
19785 || TREE_CODE (expression) == USING_DECL)
19786 return true;
19787
19788 /* Some expression forms are never type-dependent. */
19789 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19790 || TREE_CODE (expression) == SIZEOF_EXPR
19791 || TREE_CODE (expression) == ALIGNOF_EXPR
19792 || TREE_CODE (expression) == AT_ENCODE_EXPR
19793 || TREE_CODE (expression) == NOEXCEPT_EXPR
19794 || TREE_CODE (expression) == TRAIT_EXPR
19795 || TREE_CODE (expression) == TYPEID_EXPR
19796 || TREE_CODE (expression) == DELETE_EXPR
19797 || TREE_CODE (expression) == VEC_DELETE_EXPR
19798 || TREE_CODE (expression) == THROW_EXPR)
19799 return false;
19800
19801 /* The types of these expressions depends only on the type to which
19802 the cast occurs. */
19803 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19804 || TREE_CODE (expression) == STATIC_CAST_EXPR
19805 || TREE_CODE (expression) == CONST_CAST_EXPR
19806 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19807 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19808 || TREE_CODE (expression) == CAST_EXPR)
19809 return dependent_type_p (TREE_TYPE (expression));
19810
19811 /* The types of these expressions depends only on the type created
19812 by the expression. */
19813 if (TREE_CODE (expression) == NEW_EXPR
19814 || TREE_CODE (expression) == VEC_NEW_EXPR)
19815 {
19816 /* For NEW_EXPR tree nodes created inside a template, either
19817 the object type itself or a TREE_LIST may appear as the
19818 operand 1. */
19819 tree type = TREE_OPERAND (expression, 1);
19820 if (TREE_CODE (type) == TREE_LIST)
19821 /* This is an array type. We need to check array dimensions
19822 as well. */
19823 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19824 || value_dependent_expression_p
19825 (TREE_OPERAND (TREE_VALUE (type), 1));
19826 else
19827 return dependent_type_p (type);
19828 }
19829
19830 if (TREE_CODE (expression) == SCOPE_REF)
19831 {
19832 tree scope = TREE_OPERAND (expression, 0);
19833 tree name = TREE_OPERAND (expression, 1);
19834
19835 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19836 contains an identifier associated by name lookup with one or more
19837 declarations declared with a dependent type, or...a
19838 nested-name-specifier or qualified-id that names a member of an
19839 unknown specialization. */
19840 return (type_dependent_expression_p (name)
19841 || dependent_scope_p (scope));
19842 }
19843
19844 if (TREE_CODE (expression) == FUNCTION_DECL
19845 && DECL_LANG_SPECIFIC (expression)
19846 && DECL_TEMPLATE_INFO (expression)
19847 && (any_dependent_template_arguments_p
19848 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19849 return true;
19850
19851 if (TREE_CODE (expression) == TEMPLATE_DECL
19852 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19853 return false;
19854
19855 if (TREE_CODE (expression) == STMT_EXPR)
19856 expression = stmt_expr_value_expr (expression);
19857
19858 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19859 {
19860 tree elt;
19861 unsigned i;
19862
19863 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19864 {
19865 if (type_dependent_expression_p (elt))
19866 return true;
19867 }
19868 return false;
19869 }
19870
19871 /* A static data member of the current instantiation with incomplete
19872 array type is type-dependent, as the definition and specializations
19873 can have different bounds. */
19874 if (TREE_CODE (expression) == VAR_DECL
19875 && DECL_CLASS_SCOPE_P (expression)
19876 && dependent_type_p (DECL_CONTEXT (expression))
19877 && VAR_HAD_UNKNOWN_BOUND (expression))
19878 return true;
19879
19880 if (TREE_TYPE (expression) == unknown_type_node)
19881 {
19882 if (TREE_CODE (expression) == ADDR_EXPR)
19883 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19884 if (TREE_CODE (expression) == COMPONENT_REF
19885 || TREE_CODE (expression) == OFFSET_REF)
19886 {
19887 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19888 return true;
19889 expression = TREE_OPERAND (expression, 1);
19890 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19891 return false;
19892 }
19893 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19894 if (TREE_CODE (expression) == SCOPE_REF)
19895 return false;
19896
19897 if (BASELINK_P (expression))
19898 expression = BASELINK_FUNCTIONS (expression);
19899
19900 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19901 {
19902 if (any_dependent_template_arguments_p
19903 (TREE_OPERAND (expression, 1)))
19904 return true;
19905 expression = TREE_OPERAND (expression, 0);
19906 }
19907 gcc_assert (TREE_CODE (expression) == OVERLOAD
19908 || TREE_CODE (expression) == FUNCTION_DECL);
19909
19910 while (expression)
19911 {
19912 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19913 return true;
19914 expression = OVL_NEXT (expression);
19915 }
19916 return false;
19917 }
19918
19919 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19920
19921 return (dependent_type_p (TREE_TYPE (expression)));
19922 }
19923
19924 /* walk_tree callback function for instantiation_dependent_expression_p,
19925 below. Returns non-zero if a dependent subexpression is found. */
19926
19927 static tree
19928 instantiation_dependent_r (tree *tp, int *walk_subtrees,
19929 void * /*data*/)
19930 {
19931 if (TYPE_P (*tp))
19932 {
19933 /* We don't have to worry about decltype currently because decltype
19934 of an instantiation-dependent expr is a dependent type. This
19935 might change depending on the resolution of DR 1172. */
19936 *walk_subtrees = false;
19937 return NULL_TREE;
19938 }
19939 enum tree_code code = TREE_CODE (*tp);
19940 switch (code)
19941 {
19942 /* Don't treat an argument list as dependent just because it has no
19943 TREE_TYPE. */
19944 case TREE_LIST:
19945 case TREE_VEC:
19946 return NULL_TREE;
19947
19948 case TEMPLATE_PARM_INDEX:
19949 return *tp;
19950
19951 /* Handle expressions with type operands. */
19952 case SIZEOF_EXPR:
19953 case ALIGNOF_EXPR:
19954 case TYPEID_EXPR:
19955 case AT_ENCODE_EXPR:
19956 {
19957 tree op = TREE_OPERAND (*tp, 0);
19958 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
19959 op = TREE_TYPE (op);
19960 if (TYPE_P (op))
19961 {
19962 if (dependent_type_p (op))
19963 return *tp;
19964 else
19965 {
19966 *walk_subtrees = false;
19967 return NULL_TREE;
19968 }
19969 }
19970 break;
19971 }
19972
19973 case TRAIT_EXPR:
19974 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
19975 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp)))
19976 return *tp;
19977 *walk_subtrees = false;
19978 return NULL_TREE;
19979
19980 case COMPONENT_REF:
19981 if (TREE_CODE (TREE_OPERAND (*tp, 1)) == IDENTIFIER_NODE)
19982 /* In a template, finish_class_member_access_expr creates a
19983 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19984 type-dependent, so that we can check access control at
19985 instantiation time (PR 42277). See also Core issue 1273. */
19986 return *tp;
19987 break;
19988
19989 case SCOPE_REF:
19990 if (instantiation_dependent_scope_ref_p (*tp))
19991 return *tp;
19992 else
19993 break;
19994
19995 /* Treat statement-expressions as dependent. */
19996 case BIND_EXPR:
19997 return *tp;
19998
19999 default:
20000 break;
20001 }
20002
20003 if (type_dependent_expression_p (*tp))
20004 return *tp;
20005 else
20006 return NULL_TREE;
20007 }
20008
20009 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20010 sense defined by the ABI:
20011
20012 "An expression is instantiation-dependent if it is type-dependent
20013 or value-dependent, or it has a subexpression that is type-dependent
20014 or value-dependent." */
20015
20016 bool
20017 instantiation_dependent_expression_p (tree expression)
20018 {
20019 tree result;
20020
20021 if (!processing_template_decl)
20022 return false;
20023
20024 if (expression == error_mark_node)
20025 return false;
20026
20027 result = cp_walk_tree_without_duplicates (&expression,
20028 instantiation_dependent_r, NULL);
20029 return result != NULL_TREE;
20030 }
20031
20032 /* Like type_dependent_expression_p, but it also works while not processing
20033 a template definition, i.e. during substitution or mangling. */
20034
20035 bool
20036 type_dependent_expression_p_push (tree expr)
20037 {
20038 bool b;
20039 ++processing_template_decl;
20040 b = type_dependent_expression_p (expr);
20041 --processing_template_decl;
20042 return b;
20043 }
20044
20045 /* Returns TRUE if ARGS contains a type-dependent expression. */
20046
20047 bool
20048 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20049 {
20050 unsigned int i;
20051 tree arg;
20052
20053 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20054 {
20055 if (type_dependent_expression_p (arg))
20056 return true;
20057 }
20058 return false;
20059 }
20060
20061 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20062 expressions) contains any type-dependent expressions. */
20063
20064 bool
20065 any_type_dependent_elements_p (const_tree list)
20066 {
20067 for (; list; list = TREE_CHAIN (list))
20068 if (value_dependent_expression_p (TREE_VALUE (list)))
20069 return true;
20070
20071 return false;
20072 }
20073
20074 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20075 expressions) contains any value-dependent expressions. */
20076
20077 bool
20078 any_value_dependent_elements_p (const_tree list)
20079 {
20080 for (; list; list = TREE_CHAIN (list))
20081 if (value_dependent_expression_p (TREE_VALUE (list)))
20082 return true;
20083
20084 return false;
20085 }
20086
20087 /* Returns TRUE if the ARG (a template argument) is dependent. */
20088
20089 bool
20090 dependent_template_arg_p (tree arg)
20091 {
20092 if (!processing_template_decl)
20093 return false;
20094
20095 /* Assume a template argument that was wrongly written by the user
20096 is dependent. This is consistent with what
20097 any_dependent_template_arguments_p [that calls this function]
20098 does. */
20099 if (!arg || arg == error_mark_node)
20100 return true;
20101
20102 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20103 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20104
20105 if (TREE_CODE (arg) == TEMPLATE_DECL
20106 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20107 return dependent_template_p (arg);
20108 else if (ARGUMENT_PACK_P (arg))
20109 {
20110 tree args = ARGUMENT_PACK_ARGS (arg);
20111 int i, len = TREE_VEC_LENGTH (args);
20112 for (i = 0; i < len; ++i)
20113 {
20114 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20115 return true;
20116 }
20117
20118 return false;
20119 }
20120 else if (TYPE_P (arg))
20121 return dependent_type_p (arg);
20122 else
20123 return (type_dependent_expression_p (arg)
20124 || value_dependent_expression_p (arg));
20125 }
20126
20127 /* Returns true if ARGS (a collection of template arguments) contains
20128 any types that require structural equality testing. */
20129
20130 bool
20131 any_template_arguments_need_structural_equality_p (tree args)
20132 {
20133 int i;
20134 int j;
20135
20136 if (!args)
20137 return false;
20138 if (args == error_mark_node)
20139 return true;
20140
20141 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20142 {
20143 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20144 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20145 {
20146 tree arg = TREE_VEC_ELT (level, j);
20147 tree packed_args = NULL_TREE;
20148 int k, len = 1;
20149
20150 if (ARGUMENT_PACK_P (arg))
20151 {
20152 /* Look inside the argument pack. */
20153 packed_args = ARGUMENT_PACK_ARGS (arg);
20154 len = TREE_VEC_LENGTH (packed_args);
20155 }
20156
20157 for (k = 0; k < len; ++k)
20158 {
20159 if (packed_args)
20160 arg = TREE_VEC_ELT (packed_args, k);
20161
20162 if (error_operand_p (arg))
20163 return true;
20164 else if (TREE_CODE (arg) == TEMPLATE_DECL
20165 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20166 continue;
20167 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20168 return true;
20169 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20170 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20171 return true;
20172 }
20173 }
20174 }
20175
20176 return false;
20177 }
20178
20179 /* Returns true if ARGS (a collection of template arguments) contains
20180 any dependent arguments. */
20181
20182 bool
20183 any_dependent_template_arguments_p (const_tree args)
20184 {
20185 int i;
20186 int j;
20187
20188 if (!args)
20189 return false;
20190 if (args == error_mark_node)
20191 return true;
20192
20193 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20194 {
20195 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20196 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20197 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20198 return true;
20199 }
20200
20201 return false;
20202 }
20203
20204 /* Returns TRUE if the template TMPL is dependent. */
20205
20206 bool
20207 dependent_template_p (tree tmpl)
20208 {
20209 if (TREE_CODE (tmpl) == OVERLOAD)
20210 {
20211 while (tmpl)
20212 {
20213 if (dependent_template_p (OVL_CURRENT (tmpl)))
20214 return true;
20215 tmpl = OVL_NEXT (tmpl);
20216 }
20217 return false;
20218 }
20219
20220 /* Template template parameters are dependent. */
20221 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20222 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20223 return true;
20224 /* So are names that have not been looked up. */
20225 if (TREE_CODE (tmpl) == SCOPE_REF
20226 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
20227 return true;
20228 /* So are member templates of dependent classes. */
20229 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20230 return dependent_type_p (DECL_CONTEXT (tmpl));
20231 return false;
20232 }
20233
20234 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20235
20236 bool
20237 dependent_template_id_p (tree tmpl, tree args)
20238 {
20239 return (dependent_template_p (tmpl)
20240 || any_dependent_template_arguments_p (args));
20241 }
20242
20243 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20244 is dependent. */
20245
20246 bool
20247 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
20248 {
20249 int i;
20250
20251 if (!processing_template_decl)
20252 return false;
20253
20254 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
20255 {
20256 tree decl = TREE_VEC_ELT (declv, i);
20257 tree init = TREE_VEC_ELT (initv, i);
20258 tree cond = TREE_VEC_ELT (condv, i);
20259 tree incr = TREE_VEC_ELT (incrv, i);
20260
20261 if (type_dependent_expression_p (decl))
20262 return true;
20263
20264 if (init && type_dependent_expression_p (init))
20265 return true;
20266
20267 if (type_dependent_expression_p (cond))
20268 return true;
20269
20270 if (COMPARISON_CLASS_P (cond)
20271 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
20272 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
20273 return true;
20274
20275 if (TREE_CODE (incr) == MODOP_EXPR)
20276 {
20277 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
20278 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
20279 return true;
20280 }
20281 else if (type_dependent_expression_p (incr))
20282 return true;
20283 else if (TREE_CODE (incr) == MODIFY_EXPR)
20284 {
20285 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
20286 return true;
20287 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
20288 {
20289 tree t = TREE_OPERAND (incr, 1);
20290 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
20291 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
20292 return true;
20293 }
20294 }
20295 }
20296
20297 return false;
20298 }
20299
20300 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20301 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20302 no such TYPE can be found. Note that this function peers inside
20303 uninstantiated templates and therefore should be used only in
20304 extremely limited situations. ONLY_CURRENT_P restricts this
20305 peering to the currently open classes hierarchy (which is required
20306 when comparing types). */
20307
20308 tree
20309 resolve_typename_type (tree type, bool only_current_p)
20310 {
20311 tree scope;
20312 tree name;
20313 tree decl;
20314 int quals;
20315 tree pushed_scope;
20316 tree result;
20317
20318 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
20319
20320 scope = TYPE_CONTEXT (type);
20321 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20322 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20323 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20324 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20325 identifier of the TYPENAME_TYPE anymore.
20326 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20327 TYPENAME_TYPE instead, we avoid messing up with a possible
20328 typedef variant case. */
20329 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
20330
20331 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20332 it first before we can figure out what NAME refers to. */
20333 if (TREE_CODE (scope) == TYPENAME_TYPE)
20334 {
20335 if (TYPENAME_IS_RESOLVING_P (scope))
20336 /* Given a class template A with a dependent base with nested type C,
20337 typedef typename A::C::C C will land us here, as trying to resolve
20338 the initial A::C leads to the local C typedef, which leads back to
20339 A::C::C. So we break the recursion now. */
20340 return type;
20341 else
20342 scope = resolve_typename_type (scope, only_current_p);
20343 }
20344 /* If we don't know what SCOPE refers to, then we cannot resolve the
20345 TYPENAME_TYPE. */
20346 if (TREE_CODE (scope) == TYPENAME_TYPE)
20347 return type;
20348 /* If the SCOPE is a template type parameter, we have no way of
20349 resolving the name. */
20350 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
20351 return type;
20352 /* If the SCOPE is not the current instantiation, there's no reason
20353 to look inside it. */
20354 if (only_current_p && !currently_open_class (scope))
20355 return type;
20356 /* If this is a typedef, we don't want to look inside (c++/11987). */
20357 if (typedef_variant_p (type))
20358 return type;
20359 /* If SCOPE isn't the template itself, it will not have a valid
20360 TYPE_FIELDS list. */
20361 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
20362 /* scope is either the template itself or a compatible instantiation
20363 like X<T>, so look up the name in the original template. */
20364 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
20365 else
20366 /* scope is a partial instantiation, so we can't do the lookup or we
20367 will lose the template arguments. */
20368 return type;
20369 /* Enter the SCOPE so that name lookup will be resolved as if we
20370 were in the class definition. In particular, SCOPE will no
20371 longer be considered a dependent type. */
20372 pushed_scope = push_scope (scope);
20373 /* Look up the declaration. */
20374 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
20375 tf_warning_or_error);
20376
20377 result = NULL_TREE;
20378
20379 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20380 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20381 if (!decl)
20382 /*nop*/;
20383 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
20384 && TREE_CODE (decl) == TYPE_DECL)
20385 {
20386 result = TREE_TYPE (decl);
20387 if (result == error_mark_node)
20388 result = NULL_TREE;
20389 }
20390 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
20391 && DECL_CLASS_TEMPLATE_P (decl))
20392 {
20393 tree tmpl;
20394 tree args;
20395 /* Obtain the template and the arguments. */
20396 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
20397 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
20398 /* Instantiate the template. */
20399 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
20400 /*entering_scope=*/0,
20401 tf_error | tf_user);
20402 if (result == error_mark_node)
20403 result = NULL_TREE;
20404 }
20405
20406 /* Leave the SCOPE. */
20407 if (pushed_scope)
20408 pop_scope (pushed_scope);
20409
20410 /* If we failed to resolve it, return the original typename. */
20411 if (!result)
20412 return type;
20413
20414 /* If lookup found a typename type, resolve that too. */
20415 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
20416 {
20417 /* Ill-formed programs can cause infinite recursion here, so we
20418 must catch that. */
20419 TYPENAME_IS_RESOLVING_P (type) = 1;
20420 result = resolve_typename_type (result, only_current_p);
20421 TYPENAME_IS_RESOLVING_P (type) = 0;
20422 }
20423
20424 /* Qualify the resulting type. */
20425 quals = cp_type_quals (type);
20426 if (quals)
20427 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
20428
20429 return result;
20430 }
20431
20432 /* EXPR is an expression which is not type-dependent. Return a proxy
20433 for EXPR that can be used to compute the types of larger
20434 expressions containing EXPR. */
20435
20436 tree
20437 build_non_dependent_expr (tree expr)
20438 {
20439 tree inner_expr;
20440
20441 #ifdef ENABLE_CHECKING
20442 /* Try to get a constant value for all non-dependent expressions in
20443 order to expose bugs in *_dependent_expression_p and constexpr. */
20444 if (cxx_dialect >= cxx0x
20445 && !instantiation_dependent_expression_p (expr))
20446 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
20447 #endif
20448
20449 /* Preserve OVERLOADs; the functions must be available to resolve
20450 types. */
20451 inner_expr = expr;
20452 if (TREE_CODE (inner_expr) == STMT_EXPR)
20453 inner_expr = stmt_expr_value_expr (inner_expr);
20454 if (TREE_CODE (inner_expr) == ADDR_EXPR)
20455 inner_expr = TREE_OPERAND (inner_expr, 0);
20456 if (TREE_CODE (inner_expr) == COMPONENT_REF)
20457 inner_expr = TREE_OPERAND (inner_expr, 1);
20458 if (is_overloaded_fn (inner_expr)
20459 || TREE_CODE (inner_expr) == OFFSET_REF)
20460 return expr;
20461 /* There is no need to return a proxy for a variable. */
20462 if (TREE_CODE (expr) == VAR_DECL)
20463 return expr;
20464 /* Preserve string constants; conversions from string constants to
20465 "char *" are allowed, even though normally a "const char *"
20466 cannot be used to initialize a "char *". */
20467 if (TREE_CODE (expr) == STRING_CST)
20468 return expr;
20469 /* Preserve arithmetic constants, as an optimization -- there is no
20470 reason to create a new node. */
20471 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
20472 return expr;
20473 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20474 There is at least one place where we want to know that a
20475 particular expression is a throw-expression: when checking a ?:
20476 expression, there are special rules if the second or third
20477 argument is a throw-expression. */
20478 if (TREE_CODE (expr) == THROW_EXPR)
20479 return expr;
20480
20481 /* Don't wrap an initializer list, we need to be able to look inside. */
20482 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
20483 return expr;
20484
20485 /* Don't wrap a dummy object, we need to be able to test for it. */
20486 if (is_dummy_object (expr))
20487 return expr;
20488
20489 if (TREE_CODE (expr) == COND_EXPR)
20490 return build3 (COND_EXPR,
20491 TREE_TYPE (expr),
20492 TREE_OPERAND (expr, 0),
20493 (TREE_OPERAND (expr, 1)
20494 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
20495 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
20496 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
20497 if (TREE_CODE (expr) == COMPOUND_EXPR
20498 && !COMPOUND_EXPR_OVERLOADED (expr))
20499 return build2 (COMPOUND_EXPR,
20500 TREE_TYPE (expr),
20501 TREE_OPERAND (expr, 0),
20502 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
20503
20504 /* If the type is unknown, it can't really be non-dependent */
20505 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
20506
20507 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20508 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20509 }
20510
20511 /* ARGS is a vector of expressions as arguments to a function call.
20512 Replace the arguments with equivalent non-dependent expressions.
20513 This modifies ARGS in place. */
20514
20515 void
20516 make_args_non_dependent (vec<tree, va_gc> *args)
20517 {
20518 unsigned int ix;
20519 tree arg;
20520
20521 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
20522 {
20523 tree newarg = build_non_dependent_expr (arg);
20524 if (newarg != arg)
20525 (*args)[ix] = newarg;
20526 }
20527 }
20528
20529 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20530 with a level one deeper than the actual template parms. */
20531
20532 tree
20533 make_auto (void)
20534 {
20535 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20536 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20537 TYPE_DECL, get_identifier ("auto"), au);
20538 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20539 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20540 (0, processing_template_decl + 1, processing_template_decl + 1,
20541 TYPE_NAME (au), NULL_TREE);
20542 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20543 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20544 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20545
20546 return au;
20547 }
20548
20549 /* Given type ARG, return std::initializer_list<ARG>. */
20550
20551 static tree
20552 listify (tree arg)
20553 {
20554 tree std_init_list = namespace_binding
20555 (get_identifier ("initializer_list"), std_node);
20556 tree argvec;
20557 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20558 {
20559 error ("deducing from brace-enclosed initializer list requires "
20560 "#include <initializer_list>");
20561 return error_mark_node;
20562 }
20563 argvec = make_tree_vec (1);
20564 TREE_VEC_ELT (argvec, 0) = arg;
20565 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20566 NULL_TREE, 0, tf_warning_or_error);
20567 }
20568
20569 /* Replace auto in TYPE with std::initializer_list<auto>. */
20570
20571 static tree
20572 listify_autos (tree type, tree auto_node)
20573 {
20574 tree init_auto = listify (auto_node);
20575 tree argvec = make_tree_vec (1);
20576 TREE_VEC_ELT (argvec, 0) = init_auto;
20577 if (processing_template_decl)
20578 argvec = add_to_template_args (current_template_args (), argvec);
20579 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20580 }
20581
20582 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20583 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20584
20585 tree
20586 do_auto_deduction (tree type, tree init, tree auto_node)
20587 {
20588 tree parms, tparms, targs;
20589 tree args[1];
20590 int val;
20591
20592 if (init == error_mark_node)
20593 return error_mark_node;
20594
20595 if (type_dependent_expression_p (init))
20596 /* Defining a subset of type-dependent expressions that we can deduce
20597 from ahead of time isn't worth the trouble. */
20598 return type;
20599
20600 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20601 with either a new invented type template parameter U or, if the
20602 initializer is a braced-init-list (8.5.4), with
20603 std::initializer_list<U>. */
20604 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20605 type = listify_autos (type, auto_node);
20606
20607 init = resolve_nondeduced_context (init);
20608
20609 parms = build_tree_list (NULL_TREE, type);
20610 args[0] = init;
20611 tparms = make_tree_vec (1);
20612 targs = make_tree_vec (1);
20613 TREE_VEC_ELT (tparms, 0)
20614 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20615 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20616 DEDUCE_CALL, LOOKUP_NORMAL,
20617 /*explain_p=*/false);
20618 if (val > 0)
20619 {
20620 if (processing_template_decl)
20621 /* Try again at instantiation time. */
20622 return type;
20623 if (type && type != error_mark_node)
20624 /* If type is error_mark_node a diagnostic must have been
20625 emitted by now. Also, having a mention to '<type error>'
20626 in the diagnostic is not really useful to the user. */
20627 {
20628 if (cfun && auto_node == current_function_auto_return_pattern
20629 && LAMBDA_FUNCTION_P (current_function_decl))
20630 error ("unable to deduce lambda return type from %qE", init);
20631 else
20632 error ("unable to deduce %qT from %qE", type, init);
20633 }
20634 return error_mark_node;
20635 }
20636
20637 /* If the list of declarators contains more than one declarator, the type
20638 of each declared variable is determined as described above. If the
20639 type deduced for the template parameter U is not the same in each
20640 deduction, the program is ill-formed. */
20641 if (TREE_TYPE (auto_node)
20642 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20643 {
20644 if (cfun && auto_node == current_function_auto_return_pattern
20645 && LAMBDA_FUNCTION_P (current_function_decl))
20646 error ("inconsistent types %qT and %qT deduced for "
20647 "lambda return type", TREE_TYPE (auto_node),
20648 TREE_VEC_ELT (targs, 0));
20649 else
20650 error ("inconsistent deduction for %qT: %qT and then %qT",
20651 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20652 return error_mark_node;
20653 }
20654 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20655
20656 if (processing_template_decl)
20657 targs = add_to_template_args (current_template_args (), targs);
20658 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20659 }
20660
20661 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20662 result. */
20663
20664 tree
20665 splice_late_return_type (tree type, tree late_return_type)
20666 {
20667 tree argvec;
20668
20669 if (late_return_type == NULL_TREE)
20670 return type;
20671 argvec = make_tree_vec (1);
20672 TREE_VEC_ELT (argvec, 0) = late_return_type;
20673 if (processing_template_parmlist)
20674 /* For a late-specified return type in a template type-parameter, we
20675 need to add a dummy argument level for its parmlist. */
20676 argvec = add_to_template_args
20677 (make_tree_vec (processing_template_parmlist), argvec);
20678 if (current_template_parms)
20679 argvec = add_to_template_args (current_template_args (), argvec);
20680 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20681 }
20682
20683 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20684
20685 bool
20686 is_auto (const_tree type)
20687 {
20688 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20689 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20690 return true;
20691 else
20692 return false;
20693 }
20694
20695 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20696 appear as a type-specifier for the declaration in question, we don't
20697 have to look through the whole type. */
20698
20699 tree
20700 type_uses_auto (tree type)
20701 {
20702 enum tree_code code;
20703 if (is_auto (type))
20704 return type;
20705
20706 code = TREE_CODE (type);
20707
20708 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20709 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20710 || code == METHOD_TYPE || code == ARRAY_TYPE)
20711 return type_uses_auto (TREE_TYPE (type));
20712
20713 if (TYPE_PTRMEMFUNC_P (type))
20714 return type_uses_auto (TREE_TYPE (TREE_TYPE
20715 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20716
20717 return NULL_TREE;
20718 }
20719
20720 /* For a given template T, return the vector of typedefs referenced
20721 in T for which access check is needed at T instantiation time.
20722 T is either a FUNCTION_DECL or a RECORD_TYPE.
20723 Those typedefs were added to T by the function
20724 append_type_to_template_for_access_check. */
20725
20726 vec<qualified_typedef_usage_t, va_gc> *
20727 get_types_needing_access_check (tree t)
20728 {
20729 tree ti;
20730 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
20731
20732 if (!t || t == error_mark_node)
20733 return NULL;
20734
20735 if (!(ti = get_template_info (t)))
20736 return NULL;
20737
20738 if (CLASS_TYPE_P (t)
20739 || TREE_CODE (t) == FUNCTION_DECL)
20740 {
20741 if (!TI_TEMPLATE (ti))
20742 return NULL;
20743
20744 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20745 }
20746
20747 return result;
20748 }
20749
20750 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20751 tied to T. That list of typedefs will be access checked at
20752 T instantiation time.
20753 T is either a FUNCTION_DECL or a RECORD_TYPE.
20754 TYPE_DECL is a TYPE_DECL node representing a typedef.
20755 SCOPE is the scope through which TYPE_DECL is accessed.
20756 LOCATION is the location of the usage point of TYPE_DECL.
20757
20758 This function is a subroutine of
20759 append_type_to_template_for_access_check. */
20760
20761 static void
20762 append_type_to_template_for_access_check_1 (tree t,
20763 tree type_decl,
20764 tree scope,
20765 location_t location)
20766 {
20767 qualified_typedef_usage_t typedef_usage;
20768 tree ti;
20769
20770 if (!t || t == error_mark_node)
20771 return;
20772
20773 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20774 || CLASS_TYPE_P (t))
20775 && type_decl
20776 && TREE_CODE (type_decl) == TYPE_DECL
20777 && scope);
20778
20779 if (!(ti = get_template_info (t)))
20780 return;
20781
20782 gcc_assert (TI_TEMPLATE (ti));
20783
20784 typedef_usage.typedef_decl = type_decl;
20785 typedef_usage.context = scope;
20786 typedef_usage.locus = location;
20787
20788 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
20789 }
20790
20791 /* Append TYPE_DECL to the template TEMPL.
20792 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20793 At TEMPL instanciation time, TYPE_DECL will be checked to see
20794 if it can be accessed through SCOPE.
20795 LOCATION is the location of the usage point of TYPE_DECL.
20796
20797 e.g. consider the following code snippet:
20798
20799 class C
20800 {
20801 typedef int myint;
20802 };
20803
20804 template<class U> struct S
20805 {
20806 C::myint mi; // <-- usage point of the typedef C::myint
20807 };
20808
20809 S<char> s;
20810
20811 At S<char> instantiation time, we need to check the access of C::myint
20812 In other words, we need to check the access of the myint typedef through
20813 the C scope. For that purpose, this function will add the myint typedef
20814 and the scope C through which its being accessed to a list of typedefs
20815 tied to the template S. That list will be walked at template instantiation
20816 time and access check performed on each typedefs it contains.
20817 Note that this particular code snippet should yield an error because
20818 myint is private to C. */
20819
20820 void
20821 append_type_to_template_for_access_check (tree templ,
20822 tree type_decl,
20823 tree scope,
20824 location_t location)
20825 {
20826 qualified_typedef_usage_t *iter;
20827 unsigned i;
20828
20829 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20830
20831 /* Make sure we don't append the type to the template twice. */
20832 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
20833 if (iter->typedef_decl == type_decl && scope == iter->context)
20834 return;
20835
20836 append_type_to_template_for_access_check_1 (templ, type_decl,
20837 scope, location);
20838 }
20839
20840 /* Set up the hash tables for template instantiations. */
20841
20842 void
20843 init_template_processing (void)
20844 {
20845 decl_specializations = htab_create_ggc (37,
20846 hash_specialization,
20847 eq_specializations,
20848 ggc_free);
20849 type_specializations = htab_create_ggc (37,
20850 hash_specialization,
20851 eq_specializations,
20852 ggc_free);
20853 }
20854
20855 /* Print stats about the template hash tables for -fstats. */
20856
20857 void
20858 print_template_statistics (void)
20859 {
20860 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20861 "%f collisions\n", (long) htab_size (decl_specializations),
20862 (long) htab_elements (decl_specializations),
20863 htab_collisions (decl_specializations));
20864 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20865 "%f collisions\n", (long) htab_size (type_specializations),
20866 (long) htab_elements (type_specializations),
20867 htab_collisions (type_specializations));
20868 }
20869
20870 #include "gt-cp-pt.h"