re PR c++/40405 (ICE with invalid initialization of template member)
[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 (args == error_mark_node)
1013 return NULL_TREE;
1014
1015 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1016
1017 /* There should be as many levels of arguments as there are
1018 levels of parameters. */
1019 gcc_assert (TMPL_ARGS_DEPTH (args)
1020 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1021
1022 if (optimize_specialization_lookup_p (tmpl))
1023 {
1024 tree class_template;
1025 tree class_specialization;
1026 vec<tree, va_gc> *methods;
1027 tree fns;
1028 int idx;
1029
1030 /* The template arguments actually apply to the containing
1031 class. Find the class specialization with those
1032 arguments. */
1033 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1034 class_specialization
1035 = retrieve_specialization (class_template, args, 0);
1036 if (!class_specialization)
1037 return NULL_TREE;
1038 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1039 for the specialization. */
1040 idx = class_method_index_for_fn (class_specialization, tmpl);
1041 if (idx == -1)
1042 return NULL_TREE;
1043 /* Iterate through the methods with the indicated name, looking
1044 for the one that has an instance of TMPL. */
1045 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1046 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1047 {
1048 tree fn = OVL_CURRENT (fns);
1049 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1050 /* using-declarations can add base methods to the method vec,
1051 and we don't want those here. */
1052 && DECL_CONTEXT (fn) == class_specialization)
1053 return fn;
1054 }
1055 return NULL_TREE;
1056 }
1057 else
1058 {
1059 spec_entry *found;
1060 spec_entry elt;
1061 htab_t specializations;
1062
1063 elt.tmpl = tmpl;
1064 elt.args = args;
1065 elt.spec = NULL_TREE;
1066
1067 if (DECL_CLASS_TEMPLATE_P (tmpl))
1068 specializations = type_specializations;
1069 else
1070 specializations = decl_specializations;
1071
1072 if (hash == 0)
1073 hash = hash_specialization (&elt);
1074 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1075 if (found)
1076 return found->spec;
1077 }
1078
1079 return NULL_TREE;
1080 }
1081
1082 /* Like retrieve_specialization, but for local declarations. */
1083
1084 static tree
1085 retrieve_local_specialization (tree tmpl)
1086 {
1087 void **slot;
1088
1089 if (local_specializations == NULL)
1090 return NULL_TREE;
1091
1092 slot = pointer_map_contains (local_specializations, tmpl);
1093 return slot ? (tree) *slot : NULL_TREE;
1094 }
1095
1096 /* Returns nonzero iff DECL is a specialization of TMPL. */
1097
1098 int
1099 is_specialization_of (tree decl, tree tmpl)
1100 {
1101 tree t;
1102
1103 if (TREE_CODE (decl) == FUNCTION_DECL)
1104 {
1105 for (t = decl;
1106 t != NULL_TREE;
1107 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1108 if (t == tmpl)
1109 return 1;
1110 }
1111 else
1112 {
1113 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1114
1115 for (t = TREE_TYPE (decl);
1116 t != NULL_TREE;
1117 t = CLASSTYPE_USE_TEMPLATE (t)
1118 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1119 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1120 return 1;
1121 }
1122
1123 return 0;
1124 }
1125
1126 /* Returns nonzero iff DECL is a specialization of friend declaration
1127 FRIEND_DECL according to [temp.friend]. */
1128
1129 bool
1130 is_specialization_of_friend (tree decl, tree friend_decl)
1131 {
1132 bool need_template = true;
1133 int template_depth;
1134
1135 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1136 || TREE_CODE (decl) == TYPE_DECL);
1137
1138 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1139 of a template class, we want to check if DECL is a specialization
1140 if this. */
1141 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1142 && DECL_TEMPLATE_INFO (friend_decl)
1143 && !DECL_USE_TEMPLATE (friend_decl))
1144 {
1145 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1146 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1147 need_template = false;
1148 }
1149 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1150 && !PRIMARY_TEMPLATE_P (friend_decl))
1151 need_template = false;
1152
1153 /* There is nothing to do if this is not a template friend. */
1154 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1155 return false;
1156
1157 if (is_specialization_of (decl, friend_decl))
1158 return true;
1159
1160 /* [temp.friend/6]
1161 A member of a class template may be declared to be a friend of a
1162 non-template class. In this case, the corresponding member of
1163 every specialization of the class template is a friend of the
1164 class granting friendship.
1165
1166 For example, given a template friend declaration
1167
1168 template <class T> friend void A<T>::f();
1169
1170 the member function below is considered a friend
1171
1172 template <> struct A<int> {
1173 void f();
1174 };
1175
1176 For this type of template friend, TEMPLATE_DEPTH below will be
1177 nonzero. To determine if DECL is a friend of FRIEND, we first
1178 check if the enclosing class is a specialization of another. */
1179
1180 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1181 if (template_depth
1182 && DECL_CLASS_SCOPE_P (decl)
1183 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1184 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1185 {
1186 /* Next, we check the members themselves. In order to handle
1187 a few tricky cases, such as when FRIEND_DECL's are
1188
1189 template <class T> friend void A<T>::g(T t);
1190 template <class T> template <T t> friend void A<T>::h();
1191
1192 and DECL's are
1193
1194 void A<int>::g(int);
1195 template <int> void A<int>::h();
1196
1197 we need to figure out ARGS, the template arguments from
1198 the context of DECL. This is required for template substitution
1199 of `T' in the function parameter of `g' and template parameter
1200 of `h' in the above examples. Here ARGS corresponds to `int'. */
1201
1202 tree context = DECL_CONTEXT (decl);
1203 tree args = NULL_TREE;
1204 int current_depth = 0;
1205
1206 while (current_depth < template_depth)
1207 {
1208 if (CLASSTYPE_TEMPLATE_INFO (context))
1209 {
1210 if (current_depth == 0)
1211 args = TYPE_TI_ARGS (context);
1212 else
1213 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1214 current_depth++;
1215 }
1216 context = TYPE_CONTEXT (context);
1217 }
1218
1219 if (TREE_CODE (decl) == FUNCTION_DECL)
1220 {
1221 bool is_template;
1222 tree friend_type;
1223 tree decl_type;
1224 tree friend_args_type;
1225 tree decl_args_type;
1226
1227 /* Make sure that both DECL and FRIEND_DECL are templates or
1228 non-templates. */
1229 is_template = DECL_TEMPLATE_INFO (decl)
1230 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1231 if (need_template ^ is_template)
1232 return false;
1233 else if (is_template)
1234 {
1235 /* If both are templates, check template parameter list. */
1236 tree friend_parms
1237 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1238 args, tf_none);
1239 if (!comp_template_parms
1240 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1241 friend_parms))
1242 return false;
1243
1244 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1245 }
1246 else
1247 decl_type = TREE_TYPE (decl);
1248
1249 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1250 tf_none, NULL_TREE);
1251 if (friend_type == error_mark_node)
1252 return false;
1253
1254 /* Check if return types match. */
1255 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1256 return false;
1257
1258 /* Check if function parameter types match, ignoring the
1259 `this' parameter. */
1260 friend_args_type = TYPE_ARG_TYPES (friend_type);
1261 decl_args_type = TYPE_ARG_TYPES (decl_type);
1262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1263 friend_args_type = TREE_CHAIN (friend_args_type);
1264 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1265 decl_args_type = TREE_CHAIN (decl_args_type);
1266
1267 return compparms (decl_args_type, friend_args_type);
1268 }
1269 else
1270 {
1271 /* DECL is a TYPE_DECL */
1272 bool is_template;
1273 tree decl_type = TREE_TYPE (decl);
1274
1275 /* Make sure that both DECL and FRIEND_DECL are templates or
1276 non-templates. */
1277 is_template
1278 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1279 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1280
1281 if (need_template ^ is_template)
1282 return false;
1283 else if (is_template)
1284 {
1285 tree friend_parms;
1286 /* If both are templates, check the name of the two
1287 TEMPLATE_DECL's first because is_friend didn't. */
1288 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1289 != DECL_NAME (friend_decl))
1290 return false;
1291
1292 /* Now check template parameter list. */
1293 friend_parms
1294 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1295 args, tf_none);
1296 return comp_template_parms
1297 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1298 friend_parms);
1299 }
1300 else
1301 return (DECL_NAME (decl)
1302 == DECL_NAME (friend_decl));
1303 }
1304 }
1305 return false;
1306 }
1307
1308 /* Register the specialization SPEC as a specialization of TMPL with
1309 the indicated ARGS. IS_FRIEND indicates whether the specialization
1310 is actually just a friend declaration. Returns SPEC, or an
1311 equivalent prior declaration, if available. */
1312
1313 static tree
1314 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1315 hashval_t hash)
1316 {
1317 tree fn;
1318 void **slot = NULL;
1319 spec_entry elt;
1320
1321 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1322
1323 if (TREE_CODE (spec) == FUNCTION_DECL
1324 && uses_template_parms (DECL_TI_ARGS (spec)))
1325 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1326 register it; we want the corresponding TEMPLATE_DECL instead.
1327 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1328 the more obvious `uses_template_parms (spec)' to avoid problems
1329 with default function arguments. In particular, given
1330 something like this:
1331
1332 template <class T> void f(T t1, T t = T())
1333
1334 the default argument expression is not substituted for in an
1335 instantiation unless and until it is actually needed. */
1336 return spec;
1337
1338 if (optimize_specialization_lookup_p (tmpl))
1339 /* We don't put these specializations in the hash table, but we might
1340 want to give an error about a mismatch. */
1341 fn = retrieve_specialization (tmpl, args, 0);
1342 else
1343 {
1344 elt.tmpl = tmpl;
1345 elt.args = args;
1346 elt.spec = spec;
1347
1348 if (hash == 0)
1349 hash = hash_specialization (&elt);
1350
1351 slot =
1352 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1353 if (*slot)
1354 fn = ((spec_entry *) *slot)->spec;
1355 else
1356 fn = NULL_TREE;
1357 }
1358
1359 /* We can sometimes try to re-register a specialization that we've
1360 already got. In particular, regenerate_decl_from_template calls
1361 duplicate_decls which will update the specialization list. But,
1362 we'll still get called again here anyhow. It's more convenient
1363 to simply allow this than to try to prevent it. */
1364 if (fn == spec)
1365 return spec;
1366 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1367 {
1368 if (DECL_TEMPLATE_INSTANTIATION (fn))
1369 {
1370 if (DECL_ODR_USED (fn)
1371 || DECL_EXPLICIT_INSTANTIATION (fn))
1372 {
1373 error ("specialization of %qD after instantiation",
1374 fn);
1375 return error_mark_node;
1376 }
1377 else
1378 {
1379 tree clone;
1380 /* This situation should occur only if the first
1381 specialization is an implicit instantiation, the
1382 second is an explicit specialization, and the
1383 implicit instantiation has not yet been used. That
1384 situation can occur if we have implicitly
1385 instantiated a member function and then specialized
1386 it later.
1387
1388 We can also wind up here if a friend declaration that
1389 looked like an instantiation turns out to be a
1390 specialization:
1391
1392 template <class T> void foo(T);
1393 class S { friend void foo<>(int) };
1394 template <> void foo(int);
1395
1396 We transform the existing DECL in place so that any
1397 pointers to it become pointers to the updated
1398 declaration.
1399
1400 If there was a definition for the template, but not
1401 for the specialization, we want this to look as if
1402 there were no definition, and vice versa. */
1403 DECL_INITIAL (fn) = NULL_TREE;
1404 duplicate_decls (spec, fn, is_friend);
1405 /* The call to duplicate_decls will have applied
1406 [temp.expl.spec]:
1407
1408 An explicit specialization of a function template
1409 is inline only if it is explicitly declared to be,
1410 and independently of whether its function template
1411 is.
1412
1413 to the primary function; now copy the inline bits to
1414 the various clones. */
1415 FOR_EACH_CLONE (clone, fn)
1416 {
1417 DECL_DECLARED_INLINE_P (clone)
1418 = DECL_DECLARED_INLINE_P (fn);
1419 DECL_SOURCE_LOCATION (clone)
1420 = DECL_SOURCE_LOCATION (fn);
1421 }
1422 check_specialization_namespace (tmpl);
1423
1424 return fn;
1425 }
1426 }
1427 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1428 {
1429 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1430 /* Dup decl failed, but this is a new definition. Set the
1431 line number so any errors match this new
1432 definition. */
1433 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1434
1435 return fn;
1436 }
1437 }
1438 else if (fn)
1439 return duplicate_decls (spec, fn, is_friend);
1440
1441 /* A specialization must be declared in the same namespace as the
1442 template it is specializing. */
1443 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1444 && !check_specialization_namespace (tmpl))
1445 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1446
1447 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1448 {
1449 spec_entry *entry = ggc_alloc_spec_entry ();
1450 gcc_assert (tmpl && args && spec);
1451 *entry = elt;
1452 *slot = entry;
1453 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1454 && PRIMARY_TEMPLATE_P (tmpl)
1455 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1456 /* TMPL is a forward declaration of a template function; keep a list
1457 of all specializations in case we need to reassign them to a friend
1458 template later in tsubst_friend_function. */
1459 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1460 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1461 }
1462
1463 return spec;
1464 }
1465
1466 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1467 TMPL and ARGS members, ignores SPEC. */
1468
1469 int comparing_specializations;
1470
1471 static int
1472 eq_specializations (const void *p1, const void *p2)
1473 {
1474 const spec_entry *e1 = (const spec_entry *)p1;
1475 const spec_entry *e2 = (const spec_entry *)p2;
1476 int equal;
1477
1478 ++comparing_specializations;
1479 equal = (e1->tmpl == e2->tmpl
1480 && comp_template_args (e1->args, e2->args));
1481 --comparing_specializations;
1482
1483 return equal;
1484 }
1485
1486 /* Returns a hash for a template TMPL and template arguments ARGS. */
1487
1488 static hashval_t
1489 hash_tmpl_and_args (tree tmpl, tree args)
1490 {
1491 hashval_t val = DECL_UID (tmpl);
1492 return iterative_hash_template_arg (args, val);
1493 }
1494
1495 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1496 ignoring SPEC. */
1497
1498 static hashval_t
1499 hash_specialization (const void *p)
1500 {
1501 const spec_entry *e = (const spec_entry *)p;
1502 return hash_tmpl_and_args (e->tmpl, e->args);
1503 }
1504
1505 /* Recursively calculate a hash value for a template argument ARG, for use
1506 in the hash tables of template specializations. */
1507
1508 hashval_t
1509 iterative_hash_template_arg (tree arg, hashval_t val)
1510 {
1511 unsigned HOST_WIDE_INT i;
1512 enum tree_code code;
1513 char tclass;
1514
1515 if (arg == NULL_TREE)
1516 return iterative_hash_object (arg, val);
1517
1518 if (!TYPE_P (arg))
1519 STRIP_NOPS (arg);
1520
1521 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1522 /* We can get one of these when re-hashing a previous entry in the middle
1523 of substituting into a pack expansion. Just look through it. */
1524 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1525
1526 code = TREE_CODE (arg);
1527 tclass = TREE_CODE_CLASS (code);
1528
1529 val = iterative_hash_object (code, val);
1530
1531 switch (code)
1532 {
1533 case ERROR_MARK:
1534 return val;
1535
1536 case IDENTIFIER_NODE:
1537 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1538
1539 case TREE_VEC:
1540 {
1541 int i, len = TREE_VEC_LENGTH (arg);
1542 for (i = 0; i < len; ++i)
1543 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1544 return val;
1545 }
1546
1547 case TYPE_PACK_EXPANSION:
1548 case EXPR_PACK_EXPANSION:
1549 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1550 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1551
1552 case TYPE_ARGUMENT_PACK:
1553 case NONTYPE_ARGUMENT_PACK:
1554 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1555
1556 case TREE_LIST:
1557 for (; arg; arg = TREE_CHAIN (arg))
1558 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1559 return val;
1560
1561 case OVERLOAD:
1562 for (; arg; arg = OVL_NEXT (arg))
1563 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1564 return val;
1565
1566 case CONSTRUCTOR:
1567 {
1568 tree field, value;
1569 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1570 {
1571 val = iterative_hash_template_arg (field, val);
1572 val = iterative_hash_template_arg (value, val);
1573 }
1574 return val;
1575 }
1576
1577 case PARM_DECL:
1578 if (!DECL_ARTIFICIAL (arg))
1579 {
1580 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1581 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1582 }
1583 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1584
1585 case TARGET_EXPR:
1586 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1587
1588 case PTRMEM_CST:
1589 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1590 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1591
1592 case TEMPLATE_PARM_INDEX:
1593 val = iterative_hash_template_arg
1594 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1595 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1596 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1597
1598 case TRAIT_EXPR:
1599 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1600 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1601 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1602
1603 case BASELINK:
1604 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1605 val);
1606 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1607 val);
1608
1609 case MODOP_EXPR:
1610 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1611 code = TREE_CODE (TREE_OPERAND (arg, 1));
1612 val = iterative_hash_object (code, val);
1613 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1614
1615 case LAMBDA_EXPR:
1616 /* A lambda can't appear in a template arg, but don't crash on
1617 erroneous input. */
1618 gcc_assert (seen_error ());
1619 return val;
1620
1621 case CAST_EXPR:
1622 case IMPLICIT_CONV_EXPR:
1623 case STATIC_CAST_EXPR:
1624 case REINTERPRET_CAST_EXPR:
1625 case CONST_CAST_EXPR:
1626 case DYNAMIC_CAST_EXPR:
1627 case NEW_EXPR:
1628 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1629 /* Now hash operands as usual. */
1630 break;
1631
1632 default:
1633 break;
1634 }
1635
1636 switch (tclass)
1637 {
1638 case tcc_type:
1639 if (TYPE_CANONICAL (arg))
1640 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1641 val);
1642 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1643 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1644 /* Otherwise just compare the types during lookup. */
1645 return val;
1646
1647 case tcc_declaration:
1648 case tcc_constant:
1649 return iterative_hash_expr (arg, val);
1650
1651 default:
1652 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1653 {
1654 unsigned n = cp_tree_operand_length (arg);
1655 for (i = 0; i < n; ++i)
1656 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1657 return val;
1658 }
1659 }
1660 gcc_unreachable ();
1661 return 0;
1662 }
1663
1664 /* Unregister the specialization SPEC as a specialization of TMPL.
1665 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1666 if the SPEC was listed as a specialization of TMPL.
1667
1668 Note that SPEC has been ggc_freed, so we can't look inside it. */
1669
1670 bool
1671 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1672 {
1673 spec_entry *entry;
1674 spec_entry elt;
1675
1676 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1677 elt.args = TI_ARGS (tinfo);
1678 elt.spec = NULL_TREE;
1679
1680 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1681 if (entry != NULL)
1682 {
1683 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1684 gcc_assert (new_spec != NULL_TREE);
1685 entry->spec = new_spec;
1686 return 1;
1687 }
1688
1689 return 0;
1690 }
1691
1692 /* Like register_specialization, but for local declarations. We are
1693 registering SPEC, an instantiation of TMPL. */
1694
1695 static void
1696 register_local_specialization (tree spec, tree tmpl)
1697 {
1698 void **slot;
1699
1700 slot = pointer_map_insert (local_specializations, tmpl);
1701 *slot = spec;
1702 }
1703
1704 /* TYPE is a class type. Returns true if TYPE is an explicitly
1705 specialized class. */
1706
1707 bool
1708 explicit_class_specialization_p (tree type)
1709 {
1710 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1711 return false;
1712 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1713 }
1714
1715 /* Print the list of functions at FNS, going through all the overloads
1716 for each element of the list. Alternatively, FNS can not be a
1717 TREE_LIST, in which case it will be printed together with all the
1718 overloads.
1719
1720 MORE and *STR should respectively be FALSE and NULL when the function
1721 is called from the outside. They are used internally on recursive
1722 calls. print_candidates manages the two parameters and leaves NULL
1723 in *STR when it ends. */
1724
1725 static void
1726 print_candidates_1 (tree fns, bool more, const char **str)
1727 {
1728 tree fn, fn2;
1729 char *spaces = NULL;
1730
1731 for (fn = fns; fn; fn = OVL_NEXT (fn))
1732 if (TREE_CODE (fn) == TREE_LIST)
1733 {
1734 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1735 print_candidates_1 (TREE_VALUE (fn2),
1736 TREE_CHAIN (fn2) || more, str);
1737 }
1738 else
1739 {
1740 tree cand = OVL_CURRENT (fn);
1741 if (!*str)
1742 {
1743 /* Pick the prefix string. */
1744 if (!more && !OVL_NEXT (fns))
1745 {
1746 inform (DECL_SOURCE_LOCATION (cand),
1747 "candidate is: %#D", cand);
1748 continue;
1749 }
1750
1751 *str = _("candidates are:");
1752 spaces = get_spaces (*str);
1753 }
1754 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1755 *str = spaces ? spaces : *str;
1756 }
1757
1758 if (!more)
1759 {
1760 free (spaces);
1761 *str = NULL;
1762 }
1763 }
1764
1765 /* Print the list of candidate FNS in an error message. FNS can also
1766 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1767
1768 void
1769 print_candidates (tree fns)
1770 {
1771 const char *str = NULL;
1772 print_candidates_1 (fns, false, &str);
1773 gcc_assert (str == NULL);
1774 }
1775
1776 /* Returns the template (one of the functions given by TEMPLATE_ID)
1777 which can be specialized to match the indicated DECL with the
1778 explicit template args given in TEMPLATE_ID. The DECL may be
1779 NULL_TREE if none is available. In that case, the functions in
1780 TEMPLATE_ID are non-members.
1781
1782 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1783 specialization of a member template.
1784
1785 The TEMPLATE_COUNT is the number of references to qualifying
1786 template classes that appeared in the name of the function. See
1787 check_explicit_specialization for a more accurate description.
1788
1789 TSK indicates what kind of template declaration (if any) is being
1790 declared. TSK_TEMPLATE indicates that the declaration given by
1791 DECL, though a FUNCTION_DECL, has template parameters, and is
1792 therefore a template function.
1793
1794 The template args (those explicitly specified and those deduced)
1795 are output in a newly created vector *TARGS_OUT.
1796
1797 If it is impossible to determine the result, an error message is
1798 issued. The error_mark_node is returned to indicate failure. */
1799
1800 static tree
1801 determine_specialization (tree template_id,
1802 tree decl,
1803 tree* targs_out,
1804 int need_member_template,
1805 int template_count,
1806 tmpl_spec_kind tsk)
1807 {
1808 tree fns;
1809 tree targs;
1810 tree explicit_targs;
1811 tree candidates = NULL_TREE;
1812 /* A TREE_LIST of templates of which DECL may be a specialization.
1813 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1814 corresponding TREE_PURPOSE is the set of template arguments that,
1815 when used to instantiate the template, would produce a function
1816 with the signature of DECL. */
1817 tree templates = NULL_TREE;
1818 int header_count;
1819 cp_binding_level *b;
1820
1821 *targs_out = NULL_TREE;
1822
1823 if (template_id == error_mark_node || decl == error_mark_node)
1824 return error_mark_node;
1825
1826 /* We shouldn't be specializing a member template of an
1827 unspecialized class template; we already gave an error in
1828 check_specialization_scope, now avoid crashing. */
1829 if (template_count && DECL_CLASS_SCOPE_P (decl)
1830 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1831 {
1832 gcc_assert (errorcount);
1833 return error_mark_node;
1834 }
1835
1836 fns = TREE_OPERAND (template_id, 0);
1837 explicit_targs = TREE_OPERAND (template_id, 1);
1838
1839 if (fns == error_mark_node)
1840 return error_mark_node;
1841
1842 /* Check for baselinks. */
1843 if (BASELINK_P (fns))
1844 fns = BASELINK_FUNCTIONS (fns);
1845
1846 if (!is_overloaded_fn (fns))
1847 {
1848 error ("%qD is not a function template", fns);
1849 return error_mark_node;
1850 }
1851
1852 /* Count the number of template headers specified for this
1853 specialization. */
1854 header_count = 0;
1855 for (b = current_binding_level;
1856 b->kind == sk_template_parms;
1857 b = b->level_chain)
1858 ++header_count;
1859
1860 for (; fns; fns = OVL_NEXT (fns))
1861 {
1862 tree fn = OVL_CURRENT (fns);
1863
1864 if (TREE_CODE (fn) == TEMPLATE_DECL)
1865 {
1866 tree decl_arg_types;
1867 tree fn_arg_types;
1868 tree insttype;
1869
1870 /* In case of explicit specialization, we need to check if
1871 the number of template headers appearing in the specialization
1872 is correct. This is usually done in check_explicit_specialization,
1873 but the check done there cannot be exhaustive when specializing
1874 member functions. Consider the following code:
1875
1876 template <> void A<int>::f(int);
1877 template <> template <> void A<int>::f(int);
1878
1879 Assuming that A<int> is not itself an explicit specialization
1880 already, the first line specializes "f" which is a non-template
1881 member function, whilst the second line specializes "f" which
1882 is a template member function. So both lines are syntactically
1883 correct, and check_explicit_specialization does not reject
1884 them.
1885
1886 Here, we can do better, as we are matching the specialization
1887 against the declarations. We count the number of template
1888 headers, and we check if they match TEMPLATE_COUNT + 1
1889 (TEMPLATE_COUNT is the number of qualifying template classes,
1890 plus there must be another header for the member template
1891 itself).
1892
1893 Notice that if header_count is zero, this is not a
1894 specialization but rather a template instantiation, so there
1895 is no check we can perform here. */
1896 if (header_count && header_count != template_count + 1)
1897 continue;
1898
1899 /* Check that the number of template arguments at the
1900 innermost level for DECL is the same as for FN. */
1901 if (current_binding_level->kind == sk_template_parms
1902 && !current_binding_level->explicit_spec_p
1903 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1904 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1905 (current_template_parms))))
1906 continue;
1907
1908 /* DECL might be a specialization of FN. */
1909 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1910 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1911
1912 /* For a non-static member function, we need to make sure
1913 that the const qualification is the same. Since
1914 get_bindings does not try to merge the "this" parameter,
1915 we must do the comparison explicitly. */
1916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1917 && !same_type_p (TREE_VALUE (fn_arg_types),
1918 TREE_VALUE (decl_arg_types)))
1919 continue;
1920
1921 /* Skip the "this" parameter and, for constructors of
1922 classes with virtual bases, the VTT parameter. A
1923 full specialization of a constructor will have a VTT
1924 parameter, but a template never will. */
1925 decl_arg_types
1926 = skip_artificial_parms_for (decl, decl_arg_types);
1927 fn_arg_types
1928 = skip_artificial_parms_for (fn, fn_arg_types);
1929
1930 /* Function templates cannot be specializations; there are
1931 no partial specializations of functions. Therefore, if
1932 the type of DECL does not match FN, there is no
1933 match. */
1934 if (tsk == tsk_template)
1935 {
1936 if (compparms (fn_arg_types, decl_arg_types))
1937 candidates = tree_cons (NULL_TREE, fn, candidates);
1938 continue;
1939 }
1940
1941 /* See whether this function might be a specialization of this
1942 template. Suppress access control because we might be trying
1943 to make this specialization a friend, and we have already done
1944 access control for the declaration of the specialization. */
1945 push_deferring_access_checks (dk_no_check);
1946 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1947 pop_deferring_access_checks ();
1948
1949 if (!targs)
1950 /* We cannot deduce template arguments that when used to
1951 specialize TMPL will produce DECL. */
1952 continue;
1953
1954 /* Make sure that the deduced arguments actually work. */
1955 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1956 if (insttype == error_mark_node)
1957 continue;
1958 fn_arg_types
1959 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1960 if (!compparms (fn_arg_types, decl_arg_types))
1961 continue;
1962
1963 /* Save this template, and the arguments deduced. */
1964 templates = tree_cons (targs, fn, templates);
1965 }
1966 else if (need_member_template)
1967 /* FN is an ordinary member function, and we need a
1968 specialization of a member template. */
1969 ;
1970 else if (TREE_CODE (fn) != FUNCTION_DECL)
1971 /* We can get IDENTIFIER_NODEs here in certain erroneous
1972 cases. */
1973 ;
1974 else if (!DECL_FUNCTION_MEMBER_P (fn))
1975 /* This is just an ordinary non-member function. Nothing can
1976 be a specialization of that. */
1977 ;
1978 else if (DECL_ARTIFICIAL (fn))
1979 /* Cannot specialize functions that are created implicitly. */
1980 ;
1981 else
1982 {
1983 tree decl_arg_types;
1984
1985 /* This is an ordinary member function. However, since
1986 we're here, we can assume it's enclosing class is a
1987 template class. For example,
1988
1989 template <typename T> struct S { void f(); };
1990 template <> void S<int>::f() {}
1991
1992 Here, S<int>::f is a non-template, but S<int> is a
1993 template class. If FN has the same type as DECL, we
1994 might be in business. */
1995
1996 if (!DECL_TEMPLATE_INFO (fn))
1997 /* Its enclosing class is an explicit specialization
1998 of a template class. This is not a candidate. */
1999 continue;
2000
2001 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2002 TREE_TYPE (TREE_TYPE (fn))))
2003 /* The return types differ. */
2004 continue;
2005
2006 /* Adjust the type of DECL in case FN is a static member. */
2007 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2008 if (DECL_STATIC_FUNCTION_P (fn)
2009 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2010 decl_arg_types = TREE_CHAIN (decl_arg_types);
2011
2012 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2013 decl_arg_types))
2014 /* They match! */
2015 candidates = tree_cons (NULL_TREE, fn, candidates);
2016 }
2017 }
2018
2019 if (templates && TREE_CHAIN (templates))
2020 {
2021 /* We have:
2022
2023 [temp.expl.spec]
2024
2025 It is possible for a specialization with a given function
2026 signature to be instantiated from more than one function
2027 template. In such cases, explicit specification of the
2028 template arguments must be used to uniquely identify the
2029 function template specialization being specialized.
2030
2031 Note that here, there's no suggestion that we're supposed to
2032 determine which of the candidate templates is most
2033 specialized. However, we, also have:
2034
2035 [temp.func.order]
2036
2037 Partial ordering of overloaded function template
2038 declarations is used in the following contexts to select
2039 the function template to which a function template
2040 specialization refers:
2041
2042 -- when an explicit specialization refers to a function
2043 template.
2044
2045 So, we do use the partial ordering rules, at least for now.
2046 This extension can only serve to make invalid programs valid,
2047 so it's safe. And, there is strong anecdotal evidence that
2048 the committee intended the partial ordering rules to apply;
2049 the EDG front end has that behavior, and John Spicer claims
2050 that the committee simply forgot to delete the wording in
2051 [temp.expl.spec]. */
2052 tree tmpl = most_specialized_instantiation (templates);
2053 if (tmpl != error_mark_node)
2054 {
2055 templates = tmpl;
2056 TREE_CHAIN (templates) = NULL_TREE;
2057 }
2058 }
2059
2060 if (templates == NULL_TREE && candidates == NULL_TREE)
2061 {
2062 error ("template-id %qD for %q+D does not match any template "
2063 "declaration", template_id, decl);
2064 if (header_count && header_count != template_count + 1)
2065 inform (input_location, "saw %d %<template<>%>, need %d for "
2066 "specializing a member function template",
2067 header_count, template_count + 1);
2068 return error_mark_node;
2069 }
2070 else if ((templates && TREE_CHAIN (templates))
2071 || (candidates && TREE_CHAIN (candidates))
2072 || (templates && candidates))
2073 {
2074 error ("ambiguous template specialization %qD for %q+D",
2075 template_id, decl);
2076 candidates = chainon (candidates, templates);
2077 print_candidates (candidates);
2078 return error_mark_node;
2079 }
2080
2081 /* We have one, and exactly one, match. */
2082 if (candidates)
2083 {
2084 tree fn = TREE_VALUE (candidates);
2085 *targs_out = copy_node (DECL_TI_ARGS (fn));
2086 /* DECL is a re-declaration or partial instantiation of a template
2087 function. */
2088 if (TREE_CODE (fn) == TEMPLATE_DECL)
2089 return fn;
2090 /* It was a specialization of an ordinary member function in a
2091 template class. */
2092 return DECL_TI_TEMPLATE (fn);
2093 }
2094
2095 /* It was a specialization of a template. */
2096 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2097 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2098 {
2099 *targs_out = copy_node (targs);
2100 SET_TMPL_ARGS_LEVEL (*targs_out,
2101 TMPL_ARGS_DEPTH (*targs_out),
2102 TREE_PURPOSE (templates));
2103 }
2104 else
2105 *targs_out = TREE_PURPOSE (templates);
2106 return TREE_VALUE (templates);
2107 }
2108
2109 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2110 but with the default argument values filled in from those in the
2111 TMPL_TYPES. */
2112
2113 static tree
2114 copy_default_args_to_explicit_spec_1 (tree spec_types,
2115 tree tmpl_types)
2116 {
2117 tree new_spec_types;
2118
2119 if (!spec_types)
2120 return NULL_TREE;
2121
2122 if (spec_types == void_list_node)
2123 return void_list_node;
2124
2125 /* Substitute into the rest of the list. */
2126 new_spec_types =
2127 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2128 TREE_CHAIN (tmpl_types));
2129
2130 /* Add the default argument for this parameter. */
2131 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2132 TREE_VALUE (spec_types),
2133 new_spec_types);
2134 }
2135
2136 /* DECL is an explicit specialization. Replicate default arguments
2137 from the template it specializes. (That way, code like:
2138
2139 template <class T> void f(T = 3);
2140 template <> void f(double);
2141 void g () { f (); }
2142
2143 works, as required.) An alternative approach would be to look up
2144 the correct default arguments at the call-site, but this approach
2145 is consistent with how implicit instantiations are handled. */
2146
2147 static void
2148 copy_default_args_to_explicit_spec (tree decl)
2149 {
2150 tree tmpl;
2151 tree spec_types;
2152 tree tmpl_types;
2153 tree new_spec_types;
2154 tree old_type;
2155 tree new_type;
2156 tree t;
2157 tree object_type = NULL_TREE;
2158 tree in_charge = NULL_TREE;
2159 tree vtt = NULL_TREE;
2160
2161 /* See if there's anything we need to do. */
2162 tmpl = DECL_TI_TEMPLATE (decl);
2163 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2164 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2165 if (TREE_PURPOSE (t))
2166 break;
2167 if (!t)
2168 return;
2169
2170 old_type = TREE_TYPE (decl);
2171 spec_types = TYPE_ARG_TYPES (old_type);
2172
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2174 {
2175 /* Remove the this pointer, but remember the object's type for
2176 CV quals. */
2177 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2178 spec_types = TREE_CHAIN (spec_types);
2179 tmpl_types = TREE_CHAIN (tmpl_types);
2180
2181 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2182 {
2183 /* DECL may contain more parameters than TMPL due to the extra
2184 in-charge parameter in constructors and destructors. */
2185 in_charge = spec_types;
2186 spec_types = TREE_CHAIN (spec_types);
2187 }
2188 if (DECL_HAS_VTT_PARM_P (decl))
2189 {
2190 vtt = spec_types;
2191 spec_types = TREE_CHAIN (spec_types);
2192 }
2193 }
2194
2195 /* Compute the merged default arguments. */
2196 new_spec_types =
2197 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2198
2199 /* Compute the new FUNCTION_TYPE. */
2200 if (object_type)
2201 {
2202 if (vtt)
2203 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2204 TREE_VALUE (vtt),
2205 new_spec_types);
2206
2207 if (in_charge)
2208 /* Put the in-charge parameter back. */
2209 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2210 TREE_VALUE (in_charge),
2211 new_spec_types);
2212
2213 new_type = build_method_type_directly (object_type,
2214 TREE_TYPE (old_type),
2215 new_spec_types);
2216 }
2217 else
2218 new_type = build_function_type (TREE_TYPE (old_type),
2219 new_spec_types);
2220 new_type = cp_build_type_attribute_variant (new_type,
2221 TYPE_ATTRIBUTES (old_type));
2222 new_type = build_exception_variant (new_type,
2223 TYPE_RAISES_EXCEPTIONS (old_type));
2224 TREE_TYPE (decl) = new_type;
2225 }
2226
2227 /* Return the number of template headers we expect to see for a definition
2228 or specialization of CTYPE or one of its non-template members. */
2229
2230 int
2231 num_template_headers_for_class (tree ctype)
2232 {
2233 int num_templates = 0;
2234
2235 while (ctype && CLASS_TYPE_P (ctype))
2236 {
2237 /* You're supposed to have one `template <...>' for every
2238 template class, but you don't need one for a full
2239 specialization. For example:
2240
2241 template <class T> struct S{};
2242 template <> struct S<int> { void f(); };
2243 void S<int>::f () {}
2244
2245 is correct; there shouldn't be a `template <>' for the
2246 definition of `S<int>::f'. */
2247 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2248 /* If CTYPE does not have template information of any
2249 kind, then it is not a template, nor is it nested
2250 within a template. */
2251 break;
2252 if (explicit_class_specialization_p (ctype))
2253 break;
2254 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2255 ++num_templates;
2256
2257 ctype = TYPE_CONTEXT (ctype);
2258 }
2259
2260 return num_templates;
2261 }
2262
2263 /* Do a simple sanity check on the template headers that precede the
2264 variable declaration DECL. */
2265
2266 void
2267 check_template_variable (tree decl)
2268 {
2269 tree ctx = CP_DECL_CONTEXT (decl);
2270 int wanted = num_template_headers_for_class (ctx);
2271 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2272 permerror (DECL_SOURCE_LOCATION (decl),
2273 "%qD is not a static data member of a class template", decl);
2274 else if (template_header_count > wanted)
2275 {
2276 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2277 "too many template headers for %D (should be %d)",
2278 decl, wanted);
2279 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2280 inform (DECL_SOURCE_LOCATION (decl),
2281 "members of an explicitly specialized class are defined "
2282 "without a template header");
2283 }
2284 }
2285
2286 /* Check to see if the function just declared, as indicated in
2287 DECLARATOR, and in DECL, is a specialization of a function
2288 template. We may also discover that the declaration is an explicit
2289 instantiation at this point.
2290
2291 Returns DECL, or an equivalent declaration that should be used
2292 instead if all goes well. Issues an error message if something is
2293 amiss. Returns error_mark_node if the error is not easily
2294 recoverable.
2295
2296 FLAGS is a bitmask consisting of the following flags:
2297
2298 2: The function has a definition.
2299 4: The function is a friend.
2300
2301 The TEMPLATE_COUNT is the number of references to qualifying
2302 template classes that appeared in the name of the function. For
2303 example, in
2304
2305 template <class T> struct S { void f(); };
2306 void S<int>::f();
2307
2308 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2309 classes are not counted in the TEMPLATE_COUNT, so that in
2310
2311 template <class T> struct S {};
2312 template <> struct S<int> { void f(); }
2313 template <> void S<int>::f();
2314
2315 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2316 invalid; there should be no template <>.)
2317
2318 If the function is a specialization, it is marked as such via
2319 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2320 is set up correctly, and it is added to the list of specializations
2321 for that template. */
2322
2323 tree
2324 check_explicit_specialization (tree declarator,
2325 tree decl,
2326 int template_count,
2327 int flags)
2328 {
2329 int have_def = flags & 2;
2330 int is_friend = flags & 4;
2331 int specialization = 0;
2332 int explicit_instantiation = 0;
2333 int member_specialization = 0;
2334 tree ctype = DECL_CLASS_CONTEXT (decl);
2335 tree dname = DECL_NAME (decl);
2336 tmpl_spec_kind tsk;
2337
2338 if (is_friend)
2339 {
2340 if (!processing_specialization)
2341 tsk = tsk_none;
2342 else
2343 tsk = tsk_excessive_parms;
2344 }
2345 else
2346 tsk = current_tmpl_spec_kind (template_count);
2347
2348 switch (tsk)
2349 {
2350 case tsk_none:
2351 if (processing_specialization)
2352 {
2353 specialization = 1;
2354 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2355 }
2356 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2357 {
2358 if (is_friend)
2359 /* This could be something like:
2360
2361 template <class T> void f(T);
2362 class S { friend void f<>(int); } */
2363 specialization = 1;
2364 else
2365 {
2366 /* This case handles bogus declarations like template <>
2367 template <class T> void f<int>(); */
2368
2369 error ("template-id %qD in declaration of primary template",
2370 declarator);
2371 return decl;
2372 }
2373 }
2374 break;
2375
2376 case tsk_invalid_member_spec:
2377 /* The error has already been reported in
2378 check_specialization_scope. */
2379 return error_mark_node;
2380
2381 case tsk_invalid_expl_inst:
2382 error ("template parameter list used in explicit instantiation");
2383
2384 /* Fall through. */
2385
2386 case tsk_expl_inst:
2387 if (have_def)
2388 error ("definition provided for explicit instantiation");
2389
2390 explicit_instantiation = 1;
2391 break;
2392
2393 case tsk_excessive_parms:
2394 case tsk_insufficient_parms:
2395 if (tsk == tsk_excessive_parms)
2396 error ("too many template parameter lists in declaration of %qD",
2397 decl);
2398 else if (template_header_count)
2399 error("too few template parameter lists in declaration of %qD", decl);
2400 else
2401 error("explicit specialization of %qD must be introduced by "
2402 "%<template <>%>", decl);
2403
2404 /* Fall through. */
2405 case tsk_expl_spec:
2406 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2407 if (ctype)
2408 member_specialization = 1;
2409 else
2410 specialization = 1;
2411 break;
2412
2413 case tsk_template:
2414 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2415 {
2416 /* This case handles bogus declarations like template <>
2417 template <class T> void f<int>(); */
2418
2419 if (uses_template_parms (declarator))
2420 error ("function template partial specialization %qD "
2421 "is not allowed", declarator);
2422 else
2423 error ("template-id %qD in declaration of primary template",
2424 declarator);
2425 return decl;
2426 }
2427
2428 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2429 /* This is a specialization of a member template, without
2430 specialization the containing class. Something like:
2431
2432 template <class T> struct S {
2433 template <class U> void f (U);
2434 };
2435 template <> template <class U> void S<int>::f(U) {}
2436
2437 That's a specialization -- but of the entire template. */
2438 specialization = 1;
2439 break;
2440
2441 default:
2442 gcc_unreachable ();
2443 }
2444
2445 if (specialization || member_specialization)
2446 {
2447 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2448 for (; t; t = TREE_CHAIN (t))
2449 if (TREE_PURPOSE (t))
2450 {
2451 permerror (input_location,
2452 "default argument specified in explicit specialization");
2453 break;
2454 }
2455 }
2456
2457 if (specialization || member_specialization || explicit_instantiation)
2458 {
2459 tree tmpl = NULL_TREE;
2460 tree targs = NULL_TREE;
2461
2462 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2463 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2464 {
2465 tree fns;
2466
2467 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2468 if (ctype)
2469 fns = dname;
2470 else
2471 {
2472 /* If there is no class context, the explicit instantiation
2473 must be at namespace scope. */
2474 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2475
2476 /* Find the namespace binding, using the declaration
2477 context. */
2478 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2479 false, true);
2480 if (fns == error_mark_node || !is_overloaded_fn (fns))
2481 {
2482 error ("%qD is not a template function", dname);
2483 fns = error_mark_node;
2484 }
2485 else
2486 {
2487 tree fn = OVL_CURRENT (fns);
2488 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2489 CP_DECL_CONTEXT (fn)))
2490 error ("%qD is not declared in %qD",
2491 decl, current_namespace);
2492 }
2493 }
2494
2495 declarator = lookup_template_function (fns, NULL_TREE);
2496 }
2497
2498 if (declarator == error_mark_node)
2499 return error_mark_node;
2500
2501 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2502 {
2503 if (!explicit_instantiation)
2504 /* A specialization in class scope. This is invalid,
2505 but the error will already have been flagged by
2506 check_specialization_scope. */
2507 return error_mark_node;
2508 else
2509 {
2510 /* It's not valid to write an explicit instantiation in
2511 class scope, e.g.:
2512
2513 class C { template void f(); }
2514
2515 This case is caught by the parser. However, on
2516 something like:
2517
2518 template class C { void f(); };
2519
2520 (which is invalid) we can get here. The error will be
2521 issued later. */
2522 ;
2523 }
2524
2525 return decl;
2526 }
2527 else if (ctype != NULL_TREE
2528 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2529 IDENTIFIER_NODE))
2530 {
2531 /* Find the list of functions in ctype that have the same
2532 name as the declared function. */
2533 tree name = TREE_OPERAND (declarator, 0);
2534 tree fns = NULL_TREE;
2535 int idx;
2536
2537 if (constructor_name_p (name, ctype))
2538 {
2539 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2540
2541 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2542 : !CLASSTYPE_DESTRUCTORS (ctype))
2543 {
2544 /* From [temp.expl.spec]:
2545
2546 If such an explicit specialization for the member
2547 of a class template names an implicitly-declared
2548 special member function (clause _special_), the
2549 program is ill-formed.
2550
2551 Similar language is found in [temp.explicit]. */
2552 error ("specialization of implicitly-declared special member function");
2553 return error_mark_node;
2554 }
2555
2556 name = is_constructor ? ctor_identifier : dtor_identifier;
2557 }
2558
2559 if (!DECL_CONV_FN_P (decl))
2560 {
2561 idx = lookup_fnfields_1 (ctype, name);
2562 if (idx >= 0)
2563 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2564 }
2565 else
2566 {
2567 vec<tree, va_gc> *methods;
2568 tree ovl;
2569
2570 /* For a type-conversion operator, we cannot do a
2571 name-based lookup. We might be looking for `operator
2572 int' which will be a specialization of `operator T'.
2573 So, we find *all* the conversion operators, and then
2574 select from them. */
2575 fns = NULL_TREE;
2576
2577 methods = CLASSTYPE_METHOD_VEC (ctype);
2578 if (methods)
2579 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2580 methods->iterate (idx, &ovl);
2581 ++idx)
2582 {
2583 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2584 /* There are no more conversion functions. */
2585 break;
2586
2587 /* Glue all these conversion functions together
2588 with those we already have. */
2589 for (; ovl; ovl = OVL_NEXT (ovl))
2590 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2591 }
2592 }
2593
2594 if (fns == NULL_TREE)
2595 {
2596 error ("no member function %qD declared in %qT", name, ctype);
2597 return error_mark_node;
2598 }
2599 else
2600 TREE_OPERAND (declarator, 0) = fns;
2601 }
2602
2603 /* Figure out what exactly is being specialized at this point.
2604 Note that for an explicit instantiation, even one for a
2605 member function, we cannot tell apriori whether the
2606 instantiation is for a member template, or just a member
2607 function of a template class. Even if a member template is
2608 being instantiated, the member template arguments may be
2609 elided if they can be deduced from the rest of the
2610 declaration. */
2611 tmpl = determine_specialization (declarator, decl,
2612 &targs,
2613 member_specialization,
2614 template_count,
2615 tsk);
2616
2617 if (!tmpl || tmpl == error_mark_node)
2618 /* We couldn't figure out what this declaration was
2619 specializing. */
2620 return error_mark_node;
2621 else
2622 {
2623 tree gen_tmpl = most_general_template (tmpl);
2624
2625 if (explicit_instantiation)
2626 {
2627 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2628 is done by do_decl_instantiation later. */
2629
2630 int arg_depth = TMPL_ARGS_DEPTH (targs);
2631 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2632
2633 if (arg_depth > parm_depth)
2634 {
2635 /* If TMPL is not the most general template (for
2636 example, if TMPL is a friend template that is
2637 injected into namespace scope), then there will
2638 be too many levels of TARGS. Remove some of them
2639 here. */
2640 int i;
2641 tree new_targs;
2642
2643 new_targs = make_tree_vec (parm_depth);
2644 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2645 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2646 = TREE_VEC_ELT (targs, i);
2647 targs = new_targs;
2648 }
2649
2650 return instantiate_template (tmpl, targs, tf_error);
2651 }
2652
2653 /* If we thought that the DECL was a member function, but it
2654 turns out to be specializing a static member function,
2655 make DECL a static member function as well. */
2656 if (DECL_STATIC_FUNCTION_P (tmpl)
2657 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2658 revert_static_member_fn (decl);
2659
2660 /* If this is a specialization of a member template of a
2661 template class, we want to return the TEMPLATE_DECL, not
2662 the specialization of it. */
2663 if (tsk == tsk_template)
2664 {
2665 tree result = DECL_TEMPLATE_RESULT (tmpl);
2666 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2667 DECL_INITIAL (result) = NULL_TREE;
2668 if (have_def)
2669 {
2670 tree parm;
2671 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2672 DECL_SOURCE_LOCATION (result)
2673 = DECL_SOURCE_LOCATION (decl);
2674 /* We want to use the argument list specified in the
2675 definition, not in the original declaration. */
2676 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2677 for (parm = DECL_ARGUMENTS (result); parm;
2678 parm = DECL_CHAIN (parm))
2679 DECL_CONTEXT (parm) = result;
2680 }
2681 return register_specialization (tmpl, gen_tmpl, targs,
2682 is_friend, 0);
2683 }
2684
2685 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2686 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2687
2688 /* Inherit default function arguments from the template
2689 DECL is specializing. */
2690 copy_default_args_to_explicit_spec (decl);
2691
2692 /* This specialization has the same protection as the
2693 template it specializes. */
2694 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2695 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2696
2697 /* 7.1.1-1 [dcl.stc]
2698
2699 A storage-class-specifier shall not be specified in an
2700 explicit specialization...
2701
2702 The parser rejects these, so unless action is taken here,
2703 explicit function specializations will always appear with
2704 global linkage.
2705
2706 The action recommended by the C++ CWG in response to C++
2707 defect report 605 is to make the storage class and linkage
2708 of the explicit specialization match the templated function:
2709
2710 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2711 */
2712 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2713 {
2714 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2715 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2716
2717 /* This specialization has the same linkage and visibility as
2718 the function template it specializes. */
2719 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2720 if (! TREE_PUBLIC (decl))
2721 {
2722 DECL_INTERFACE_KNOWN (decl) = 1;
2723 DECL_NOT_REALLY_EXTERN (decl) = 1;
2724 }
2725 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2726 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2727 {
2728 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2729 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2730 }
2731 }
2732
2733 /* If DECL is a friend declaration, declared using an
2734 unqualified name, the namespace associated with DECL may
2735 have been set incorrectly. For example, in:
2736
2737 template <typename T> void f(T);
2738 namespace N {
2739 struct S { friend void f<int>(int); }
2740 }
2741
2742 we will have set the DECL_CONTEXT for the friend
2743 declaration to N, rather than to the global namespace. */
2744 if (DECL_NAMESPACE_SCOPE_P (decl))
2745 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2746
2747 if (is_friend && !have_def)
2748 /* This is not really a declaration of a specialization.
2749 It's just the name of an instantiation. But, it's not
2750 a request for an instantiation, either. */
2751 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2752 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2753 /* This is indeed a specialization. In case of constructors
2754 and destructors, we need in-charge and not-in-charge
2755 versions in V3 ABI. */
2756 clone_function_decl (decl, /*update_method_vec_p=*/0);
2757
2758 /* Register this specialization so that we can find it
2759 again. */
2760 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2761 }
2762 }
2763
2764 return decl;
2765 }
2766
2767 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2768 parameters. These are represented in the same format used for
2769 DECL_TEMPLATE_PARMS. */
2770
2771 int
2772 comp_template_parms (const_tree parms1, const_tree parms2)
2773 {
2774 const_tree p1;
2775 const_tree p2;
2776
2777 if (parms1 == parms2)
2778 return 1;
2779
2780 for (p1 = parms1, p2 = parms2;
2781 p1 != NULL_TREE && p2 != NULL_TREE;
2782 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2783 {
2784 tree t1 = TREE_VALUE (p1);
2785 tree t2 = TREE_VALUE (p2);
2786 int i;
2787
2788 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2789 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2790
2791 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2792 return 0;
2793
2794 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2795 {
2796 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2797 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2798
2799 /* If either of the template parameters are invalid, assume
2800 they match for the sake of error recovery. */
2801 if (parm1 == error_mark_node || parm2 == error_mark_node)
2802 return 1;
2803
2804 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2805 return 0;
2806
2807 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2808 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2809 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2810 continue;
2811 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2812 return 0;
2813 }
2814 }
2815
2816 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2817 /* One set of parameters has more parameters lists than the
2818 other. */
2819 return 0;
2820
2821 return 1;
2822 }
2823
2824 /* Determine whether PARM is a parameter pack. */
2825
2826 bool
2827 template_parameter_pack_p (const_tree parm)
2828 {
2829 /* Determine if we have a non-type template parameter pack. */
2830 if (TREE_CODE (parm) == PARM_DECL)
2831 return (DECL_TEMPLATE_PARM_P (parm)
2832 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2833 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2834 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2835
2836 /* If this is a list of template parameters, we could get a
2837 TYPE_DECL or a TEMPLATE_DECL. */
2838 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2839 parm = TREE_TYPE (parm);
2840
2841 /* Otherwise it must be a type template parameter. */
2842 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2843 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2844 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2845 }
2846
2847 /* Determine if T is a function parameter pack. */
2848
2849 bool
2850 function_parameter_pack_p (const_tree t)
2851 {
2852 if (t && TREE_CODE (t) == PARM_DECL)
2853 return FUNCTION_PARAMETER_PACK_P (t);
2854 return false;
2855 }
2856
2857 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2858 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2859
2860 tree
2861 get_function_template_decl (const_tree primary_func_tmpl_inst)
2862 {
2863 if (! primary_func_tmpl_inst
2864 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2865 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2866 return NULL;
2867
2868 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2869 }
2870
2871 /* Return true iff the function parameter PARAM_DECL was expanded
2872 from the function parameter pack PACK. */
2873
2874 bool
2875 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2876 {
2877 if (DECL_ARTIFICIAL (param_decl)
2878 || !function_parameter_pack_p (pack))
2879 return false;
2880
2881 /* The parameter pack and its pack arguments have the same
2882 DECL_PARM_INDEX. */
2883 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2884 }
2885
2886 /* Determine whether ARGS describes a variadic template args list,
2887 i.e., one that is terminated by a template argument pack. */
2888
2889 static bool
2890 template_args_variadic_p (tree args)
2891 {
2892 int nargs;
2893 tree last_parm;
2894
2895 if (args == NULL_TREE)
2896 return false;
2897
2898 args = INNERMOST_TEMPLATE_ARGS (args);
2899 nargs = TREE_VEC_LENGTH (args);
2900
2901 if (nargs == 0)
2902 return false;
2903
2904 last_parm = TREE_VEC_ELT (args, nargs - 1);
2905
2906 return ARGUMENT_PACK_P (last_parm);
2907 }
2908
2909 /* Generate a new name for the parameter pack name NAME (an
2910 IDENTIFIER_NODE) that incorporates its */
2911
2912 static tree
2913 make_ith_pack_parameter_name (tree name, int i)
2914 {
2915 /* Munge the name to include the parameter index. */
2916 #define NUMBUF_LEN 128
2917 char numbuf[NUMBUF_LEN];
2918 char* newname;
2919 int newname_len;
2920
2921 if (name == NULL_TREE)
2922 return name;
2923 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2924 newname_len = IDENTIFIER_LENGTH (name)
2925 + strlen (numbuf) + 2;
2926 newname = (char*)alloca (newname_len);
2927 snprintf (newname, newname_len,
2928 "%s#%i", IDENTIFIER_POINTER (name), i);
2929 return get_identifier (newname);
2930 }
2931
2932 /* Return true if T is a primary function, class or alias template
2933 instantiation. */
2934
2935 bool
2936 primary_template_instantiation_p (const_tree t)
2937 {
2938 if (!t)
2939 return false;
2940
2941 if (TREE_CODE (t) == FUNCTION_DECL)
2942 return DECL_LANG_SPECIFIC (t)
2943 && DECL_TEMPLATE_INSTANTIATION (t)
2944 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2945 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2946 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2947 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2948 else if (alias_template_specialization_p (t))
2949 return true;
2950 return false;
2951 }
2952
2953 /* Return true if PARM is a template template parameter. */
2954
2955 bool
2956 template_template_parameter_p (const_tree parm)
2957 {
2958 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2959 }
2960
2961 /* Return true iff PARM is a DECL representing a type template
2962 parameter. */
2963
2964 bool
2965 template_type_parameter_p (const_tree parm)
2966 {
2967 return (parm
2968 && (TREE_CODE (parm) == TYPE_DECL
2969 || TREE_CODE (parm) == TEMPLATE_DECL)
2970 && DECL_TEMPLATE_PARM_P (parm));
2971 }
2972
2973 /* Return the template parameters of T if T is a
2974 primary template instantiation, NULL otherwise. */
2975
2976 tree
2977 get_primary_template_innermost_parameters (const_tree t)
2978 {
2979 tree parms = NULL, template_info = NULL;
2980
2981 if ((template_info = get_template_info (t))
2982 && primary_template_instantiation_p (t))
2983 parms = INNERMOST_TEMPLATE_PARMS
2984 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2985
2986 return parms;
2987 }
2988
2989 /* Return the template parameters of the LEVELth level from the full list
2990 of template parameters PARMS. */
2991
2992 tree
2993 get_template_parms_at_level (tree parms, int level)
2994 {
2995 tree p;
2996 if (!parms
2997 || TREE_CODE (parms) != TREE_LIST
2998 || level > TMPL_PARMS_DEPTH (parms))
2999 return NULL_TREE;
3000
3001 for (p = parms; p; p = TREE_CHAIN (p))
3002 if (TMPL_PARMS_DEPTH (p) == level)
3003 return p;
3004
3005 return NULL_TREE;
3006 }
3007
3008 /* Returns the template arguments of T if T is a template instantiation,
3009 NULL otherwise. */
3010
3011 tree
3012 get_template_innermost_arguments (const_tree t)
3013 {
3014 tree args = NULL, template_info = NULL;
3015
3016 if ((template_info = get_template_info (t))
3017 && TI_ARGS (template_info))
3018 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3019
3020 return args;
3021 }
3022
3023 /* Return the argument pack elements of T if T is a template argument pack,
3024 NULL otherwise. */
3025
3026 tree
3027 get_template_argument_pack_elems (const_tree t)
3028 {
3029 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3030 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3031 return NULL;
3032
3033 return ARGUMENT_PACK_ARGS (t);
3034 }
3035
3036 /* Structure used to track the progress of find_parameter_packs_r. */
3037 struct find_parameter_pack_data
3038 {
3039 /* TREE_LIST that will contain all of the parameter packs found by
3040 the traversal. */
3041 tree* parameter_packs;
3042
3043 /* Set of AST nodes that have been visited by the traversal. */
3044 struct pointer_set_t *visited;
3045 };
3046
3047 /* Identifies all of the argument packs that occur in a template
3048 argument and appends them to the TREE_LIST inside DATA, which is a
3049 find_parameter_pack_data structure. This is a subroutine of
3050 make_pack_expansion and uses_parameter_packs. */
3051 static tree
3052 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3053 {
3054 tree t = *tp;
3055 struct find_parameter_pack_data* ppd =
3056 (struct find_parameter_pack_data*)data;
3057 bool parameter_pack_p = false;
3058
3059 /* Handle type aliases/typedefs. */
3060 if (TYPE_P (t)
3061 && TYPE_NAME (t)
3062 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
3063 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3064 {
3065 if (TYPE_TEMPLATE_INFO (t))
3066 cp_walk_tree (&TYPE_TI_ARGS (t),
3067 &find_parameter_packs_r,
3068 ppd, ppd->visited);
3069 *walk_subtrees = 0;
3070 return NULL_TREE;
3071 }
3072
3073 /* Identify whether this is a parameter pack or not. */
3074 switch (TREE_CODE (t))
3075 {
3076 case TEMPLATE_PARM_INDEX:
3077 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3078 parameter_pack_p = true;
3079 break;
3080
3081 case TEMPLATE_TYPE_PARM:
3082 t = TYPE_MAIN_VARIANT (t);
3083 case TEMPLATE_TEMPLATE_PARM:
3084 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3085 parameter_pack_p = true;
3086 break;
3087
3088 case PARM_DECL:
3089 if (FUNCTION_PARAMETER_PACK_P (t))
3090 {
3091 /* We don't want to walk into the type of a PARM_DECL,
3092 because we don't want to see the type parameter pack. */
3093 *walk_subtrees = 0;
3094 parameter_pack_p = true;
3095 }
3096 break;
3097
3098 case BASES:
3099 parameter_pack_p = true;
3100 break;
3101 default:
3102 /* Not a parameter pack. */
3103 break;
3104 }
3105
3106 if (parameter_pack_p)
3107 {
3108 /* Add this parameter pack to the list. */
3109 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3110 }
3111
3112 if (TYPE_P (t))
3113 cp_walk_tree (&TYPE_CONTEXT (t),
3114 &find_parameter_packs_r, ppd, ppd->visited);
3115
3116 /* This switch statement will return immediately if we don't find a
3117 parameter pack. */
3118 switch (TREE_CODE (t))
3119 {
3120 case TEMPLATE_PARM_INDEX:
3121 return NULL_TREE;
3122
3123 case BOUND_TEMPLATE_TEMPLATE_PARM:
3124 /* Check the template itself. */
3125 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3126 &find_parameter_packs_r, ppd, ppd->visited);
3127 /* Check the template arguments. */
3128 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3129 ppd->visited);
3130 *walk_subtrees = 0;
3131 return NULL_TREE;
3132
3133 case TEMPLATE_TYPE_PARM:
3134 case TEMPLATE_TEMPLATE_PARM:
3135 return NULL_TREE;
3136
3137 case PARM_DECL:
3138 return NULL_TREE;
3139
3140 case RECORD_TYPE:
3141 if (TYPE_PTRMEMFUNC_P (t))
3142 return NULL_TREE;
3143 /* Fall through. */
3144
3145 case UNION_TYPE:
3146 case ENUMERAL_TYPE:
3147 if (TYPE_TEMPLATE_INFO (t))
3148 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3149 &find_parameter_packs_r, ppd, ppd->visited);
3150
3151 *walk_subtrees = 0;
3152 return NULL_TREE;
3153
3154 case CONSTRUCTOR:
3155 case TEMPLATE_DECL:
3156 cp_walk_tree (&TREE_TYPE (t),
3157 &find_parameter_packs_r, ppd, ppd->visited);
3158 return NULL_TREE;
3159
3160 case TYPENAME_TYPE:
3161 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3162 ppd, ppd->visited);
3163 *walk_subtrees = 0;
3164 return NULL_TREE;
3165
3166 case TYPE_PACK_EXPANSION:
3167 case EXPR_PACK_EXPANSION:
3168 *walk_subtrees = 0;
3169 return NULL_TREE;
3170
3171 case INTEGER_TYPE:
3172 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3173 ppd, ppd->visited);
3174 *walk_subtrees = 0;
3175 return NULL_TREE;
3176
3177 case IDENTIFIER_NODE:
3178 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3179 ppd->visited);
3180 *walk_subtrees = 0;
3181 return NULL_TREE;
3182
3183 default:
3184 return NULL_TREE;
3185 }
3186
3187 return NULL_TREE;
3188 }
3189
3190 /* Determines if the expression or type T uses any parameter packs. */
3191 bool
3192 uses_parameter_packs (tree t)
3193 {
3194 tree parameter_packs = NULL_TREE;
3195 struct find_parameter_pack_data ppd;
3196 ppd.parameter_packs = &parameter_packs;
3197 ppd.visited = pointer_set_create ();
3198 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3199 pointer_set_destroy (ppd.visited);
3200 return parameter_packs != NULL_TREE;
3201 }
3202
3203 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3204 representation a base-class initializer into a parameter pack
3205 expansion. If all goes well, the resulting node will be an
3206 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3207 respectively. */
3208 tree
3209 make_pack_expansion (tree arg)
3210 {
3211 tree result;
3212 tree parameter_packs = NULL_TREE;
3213 bool for_types = false;
3214 struct find_parameter_pack_data ppd;
3215
3216 if (!arg || arg == error_mark_node)
3217 return arg;
3218
3219 if (TREE_CODE (arg) == TREE_LIST)
3220 {
3221 /* The only time we will see a TREE_LIST here is for a base
3222 class initializer. In this case, the TREE_PURPOSE will be a
3223 _TYPE node (representing the base class expansion we're
3224 initializing) and the TREE_VALUE will be a TREE_LIST
3225 containing the initialization arguments.
3226
3227 The resulting expansion looks somewhat different from most
3228 expansions. Rather than returning just one _EXPANSION, we
3229 return a TREE_LIST whose TREE_PURPOSE is a
3230 TYPE_PACK_EXPANSION containing the bases that will be
3231 initialized. The TREE_VALUE will be identical to the
3232 original TREE_VALUE, which is a list of arguments that will
3233 be passed to each base. We do not introduce any new pack
3234 expansion nodes into the TREE_VALUE (although it is possible
3235 that some already exist), because the TREE_PURPOSE and
3236 TREE_VALUE all need to be expanded together with the same
3237 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3238 resulting TREE_PURPOSE will mention the parameter packs in
3239 both the bases and the arguments to the bases. */
3240 tree purpose;
3241 tree value;
3242 tree parameter_packs = NULL_TREE;
3243
3244 /* Determine which parameter packs will be used by the base
3245 class expansion. */
3246 ppd.visited = pointer_set_create ();
3247 ppd.parameter_packs = &parameter_packs;
3248 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3249 &ppd, ppd.visited);
3250
3251 if (parameter_packs == NULL_TREE)
3252 {
3253 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3254 pointer_set_destroy (ppd.visited);
3255 return error_mark_node;
3256 }
3257
3258 if (TREE_VALUE (arg) != void_type_node)
3259 {
3260 /* Collect the sets of parameter packs used in each of the
3261 initialization arguments. */
3262 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3263 {
3264 /* Determine which parameter packs will be expanded in this
3265 argument. */
3266 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3267 &ppd, ppd.visited);
3268 }
3269 }
3270
3271 pointer_set_destroy (ppd.visited);
3272
3273 /* Create the pack expansion type for the base type. */
3274 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3275 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3276 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3277
3278 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3279 they will rarely be compared to anything. */
3280 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3281
3282 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3283 }
3284
3285 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3286 for_types = true;
3287
3288 /* Build the PACK_EXPANSION_* node. */
3289 result = for_types
3290 ? cxx_make_type (TYPE_PACK_EXPANSION)
3291 : make_node (EXPR_PACK_EXPANSION);
3292 SET_PACK_EXPANSION_PATTERN (result, arg);
3293 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3294 {
3295 /* Propagate type and const-expression information. */
3296 TREE_TYPE (result) = TREE_TYPE (arg);
3297 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3298 }
3299 else
3300 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3301 they will rarely be compared to anything. */
3302 SET_TYPE_STRUCTURAL_EQUALITY (result);
3303
3304 /* Determine which parameter packs will be expanded. */
3305 ppd.parameter_packs = &parameter_packs;
3306 ppd.visited = pointer_set_create ();
3307 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3308 pointer_set_destroy (ppd.visited);
3309
3310 /* Make sure we found some parameter packs. */
3311 if (parameter_packs == NULL_TREE)
3312 {
3313 if (TYPE_P (arg))
3314 error ("expansion pattern %<%T%> contains no argument packs", arg);
3315 else
3316 error ("expansion pattern %<%E%> contains no argument packs", arg);
3317 return error_mark_node;
3318 }
3319 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3320
3321 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3322
3323 return result;
3324 }
3325
3326 /* Checks T for any "bare" parameter packs, which have not yet been
3327 expanded, and issues an error if any are found. This operation can
3328 only be done on full expressions or types (e.g., an expression
3329 statement, "if" condition, etc.), because we could have expressions like:
3330
3331 foo(f(g(h(args)))...)
3332
3333 where "args" is a parameter pack. check_for_bare_parameter_packs
3334 should not be called for the subexpressions args, h(args),
3335 g(h(args)), or f(g(h(args))), because we would produce erroneous
3336 error messages.
3337
3338 Returns TRUE and emits an error if there were bare parameter packs,
3339 returns FALSE otherwise. */
3340 bool
3341 check_for_bare_parameter_packs (tree t)
3342 {
3343 tree parameter_packs = NULL_TREE;
3344 struct find_parameter_pack_data ppd;
3345
3346 if (!processing_template_decl || !t || t == error_mark_node)
3347 return false;
3348
3349 if (TREE_CODE (t) == TYPE_DECL)
3350 t = TREE_TYPE (t);
3351
3352 ppd.parameter_packs = &parameter_packs;
3353 ppd.visited = pointer_set_create ();
3354 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3355 pointer_set_destroy (ppd.visited);
3356
3357 if (parameter_packs)
3358 {
3359 error ("parameter packs not expanded with %<...%>:");
3360 while (parameter_packs)
3361 {
3362 tree pack = TREE_VALUE (parameter_packs);
3363 tree name = NULL_TREE;
3364
3365 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3366 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3367 name = TYPE_NAME (pack);
3368 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3369 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3370 else
3371 name = DECL_NAME (pack);
3372
3373 if (name)
3374 inform (input_location, " %qD", name);
3375 else
3376 inform (input_location, " <anonymous>");
3377
3378 parameter_packs = TREE_CHAIN (parameter_packs);
3379 }
3380
3381 return true;
3382 }
3383
3384 return false;
3385 }
3386
3387 /* Expand any parameter packs that occur in the template arguments in
3388 ARGS. */
3389 tree
3390 expand_template_argument_pack (tree args)
3391 {
3392 tree result_args = NULL_TREE;
3393 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3394 int num_result_args = -1;
3395 int non_default_args_count = -1;
3396
3397 /* First, determine if we need to expand anything, and the number of
3398 slots we'll need. */
3399 for (in_arg = 0; in_arg < nargs; ++in_arg)
3400 {
3401 tree arg = TREE_VEC_ELT (args, in_arg);
3402 if (arg == NULL_TREE)
3403 return args;
3404 if (ARGUMENT_PACK_P (arg))
3405 {
3406 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3407 if (num_result_args < 0)
3408 num_result_args = in_arg + num_packed;
3409 else
3410 num_result_args += num_packed;
3411 }
3412 else
3413 {
3414 if (num_result_args >= 0)
3415 num_result_args++;
3416 }
3417 }
3418
3419 /* If no expansion is necessary, we're done. */
3420 if (num_result_args < 0)
3421 return args;
3422
3423 /* Expand arguments. */
3424 result_args = make_tree_vec (num_result_args);
3425 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3426 non_default_args_count =
3427 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3428 for (in_arg = 0; in_arg < nargs; ++in_arg)
3429 {
3430 tree arg = TREE_VEC_ELT (args, in_arg);
3431 if (ARGUMENT_PACK_P (arg))
3432 {
3433 tree packed = ARGUMENT_PACK_ARGS (arg);
3434 int i, num_packed = TREE_VEC_LENGTH (packed);
3435 for (i = 0; i < num_packed; ++i, ++out_arg)
3436 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3437 if (non_default_args_count > 0)
3438 non_default_args_count += num_packed;
3439 }
3440 else
3441 {
3442 TREE_VEC_ELT (result_args, out_arg) = arg;
3443 ++out_arg;
3444 }
3445 }
3446 if (non_default_args_count >= 0)
3447 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3448 return result_args;
3449 }
3450
3451 /* Checks if DECL shadows a template parameter.
3452
3453 [temp.local]: A template-parameter shall not be redeclared within its
3454 scope (including nested scopes).
3455
3456 Emits an error and returns TRUE if the DECL shadows a parameter,
3457 returns FALSE otherwise. */
3458
3459 bool
3460 check_template_shadow (tree decl)
3461 {
3462 tree olddecl;
3463
3464 /* If we're not in a template, we can't possibly shadow a template
3465 parameter. */
3466 if (!current_template_parms)
3467 return true;
3468
3469 /* Figure out what we're shadowing. */
3470 if (TREE_CODE (decl) == OVERLOAD)
3471 decl = OVL_CURRENT (decl);
3472 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3473
3474 /* If there's no previous binding for this name, we're not shadowing
3475 anything, let alone a template parameter. */
3476 if (!olddecl)
3477 return true;
3478
3479 /* If we're not shadowing a template parameter, we're done. Note
3480 that OLDDECL might be an OVERLOAD (or perhaps even an
3481 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3482 node. */
3483 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3484 return true;
3485
3486 /* We check for decl != olddecl to avoid bogus errors for using a
3487 name inside a class. We check TPFI to avoid duplicate errors for
3488 inline member templates. */
3489 if (decl == olddecl
3490 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3491 return true;
3492
3493 error ("declaration of %q+#D", decl);
3494 error (" shadows template parm %q+#D", olddecl);
3495 return false;
3496 }
3497
3498 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3499 ORIG_LEVEL, DECL, and TYPE. */
3500
3501 static tree
3502 build_template_parm_index (int index,
3503 int level,
3504 int orig_level,
3505 tree decl,
3506 tree type)
3507 {
3508 tree t = make_node (TEMPLATE_PARM_INDEX);
3509 TEMPLATE_PARM_IDX (t) = index;
3510 TEMPLATE_PARM_LEVEL (t) = level;
3511 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3512 TEMPLATE_PARM_DECL (t) = decl;
3513 TREE_TYPE (t) = type;
3514 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3515 TREE_READONLY (t) = TREE_READONLY (decl);
3516
3517 return t;
3518 }
3519
3520 /* Find the canonical type parameter for the given template type
3521 parameter. Returns the canonical type parameter, which may be TYPE
3522 if no such parameter existed. */
3523
3524 static tree
3525 canonical_type_parameter (tree type)
3526 {
3527 tree list;
3528 int idx = TEMPLATE_TYPE_IDX (type);
3529 if (!canonical_template_parms)
3530 vec_alloc (canonical_template_parms, idx+1);
3531
3532 while (canonical_template_parms->length () <= (unsigned)idx)
3533 vec_safe_push (canonical_template_parms, NULL_TREE);
3534
3535 list = (*canonical_template_parms)[idx];
3536 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3537 list = TREE_CHAIN (list);
3538
3539 if (list)
3540 return TREE_VALUE (list);
3541 else
3542 {
3543 (*canonical_template_parms)[idx]
3544 = tree_cons (NULL_TREE, type,
3545 (*canonical_template_parms)[idx]);
3546 return type;
3547 }
3548 }
3549
3550 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3551 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3552 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3553 new one is created. */
3554
3555 static tree
3556 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3557 tsubst_flags_t complain)
3558 {
3559 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3560 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3561 != TEMPLATE_PARM_LEVEL (index) - levels)
3562 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3563 {
3564 tree orig_decl = TEMPLATE_PARM_DECL (index);
3565 tree decl, t;
3566
3567 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3568 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3569 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3570 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3571 DECL_ARTIFICIAL (decl) = 1;
3572 SET_DECL_TEMPLATE_PARM_P (decl);
3573
3574 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3575 TEMPLATE_PARM_LEVEL (index) - levels,
3576 TEMPLATE_PARM_ORIG_LEVEL (index),
3577 decl, type);
3578 TEMPLATE_PARM_DESCENDANTS (index) = t;
3579 TEMPLATE_PARM_PARAMETER_PACK (t)
3580 = TEMPLATE_PARM_PARAMETER_PACK (index);
3581
3582 /* Template template parameters need this. */
3583 if (TREE_CODE (decl) == TEMPLATE_DECL)
3584 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3585 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3586 args, complain);
3587 }
3588
3589 return TEMPLATE_PARM_DESCENDANTS (index);
3590 }
3591
3592 /* Process information from new template parameter PARM and append it
3593 to the LIST being built. This new parameter is a non-type
3594 parameter iff IS_NON_TYPE is true. This new parameter is a
3595 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3596 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3597 parameter list PARM belongs to. This is used used to create a
3598 proper canonical type for the type of PARM that is to be created,
3599 iff PARM is a type. If the size is not known, this parameter shall
3600 be set to 0. */
3601
3602 tree
3603 process_template_parm (tree list, location_t parm_loc, tree parm,
3604 bool is_non_type, bool is_parameter_pack)
3605 {
3606 tree decl = 0;
3607 tree defval;
3608 tree err_parm_list;
3609 int idx = 0;
3610
3611 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3612 defval = TREE_PURPOSE (parm);
3613
3614 if (list)
3615 {
3616 tree p = tree_last (list);
3617
3618 if (p && TREE_VALUE (p) != error_mark_node)
3619 {
3620 p = TREE_VALUE (p);
3621 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3622 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3623 else
3624 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3625 }
3626
3627 ++idx;
3628 }
3629 else
3630 idx = 0;
3631
3632 if (is_non_type)
3633 {
3634 parm = TREE_VALUE (parm);
3635
3636 SET_DECL_TEMPLATE_PARM_P (parm);
3637
3638 if (TREE_TYPE (parm) == error_mark_node)
3639 {
3640 err_parm_list = build_tree_list (defval, parm);
3641 TREE_VALUE (err_parm_list) = error_mark_node;
3642 return chainon (list, err_parm_list);
3643 }
3644 else
3645 {
3646 /* [temp.param]
3647
3648 The top-level cv-qualifiers on the template-parameter are
3649 ignored when determining its type. */
3650 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3651 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3652 {
3653 err_parm_list = build_tree_list (defval, parm);
3654 TREE_VALUE (err_parm_list) = error_mark_node;
3655 return chainon (list, err_parm_list);
3656 }
3657
3658 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3659 {
3660 /* This template parameter is not a parameter pack, but it
3661 should be. Complain about "bare" parameter packs. */
3662 check_for_bare_parameter_packs (TREE_TYPE (parm));
3663
3664 /* Recover by calling this a parameter pack. */
3665 is_parameter_pack = true;
3666 }
3667 }
3668
3669 /* A template parameter is not modifiable. */
3670 TREE_CONSTANT (parm) = 1;
3671 TREE_READONLY (parm) = 1;
3672 decl = build_decl (parm_loc,
3673 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3674 TREE_CONSTANT (decl) = 1;
3675 TREE_READONLY (decl) = 1;
3676 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3677 = build_template_parm_index (idx, processing_template_decl,
3678 processing_template_decl,
3679 decl, TREE_TYPE (parm));
3680
3681 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3682 = is_parameter_pack;
3683 }
3684 else
3685 {
3686 tree t;
3687 parm = TREE_VALUE (TREE_VALUE (parm));
3688
3689 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3690 {
3691 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3692 /* This is for distinguishing between real templates and template
3693 template parameters */
3694 TREE_TYPE (parm) = t;
3695 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3696 decl = parm;
3697 }
3698 else
3699 {
3700 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3701 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3702 decl = build_decl (parm_loc,
3703 TYPE_DECL, parm, t);
3704 }
3705
3706 TYPE_NAME (t) = decl;
3707 TYPE_STUB_DECL (t) = decl;
3708 parm = decl;
3709 TEMPLATE_TYPE_PARM_INDEX (t)
3710 = build_template_parm_index (idx, processing_template_decl,
3711 processing_template_decl,
3712 decl, TREE_TYPE (parm));
3713 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3714 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3715 }
3716 DECL_ARTIFICIAL (decl) = 1;
3717 SET_DECL_TEMPLATE_PARM_P (decl);
3718 pushdecl (decl);
3719 parm = build_tree_list (defval, parm);
3720 return chainon (list, parm);
3721 }
3722
3723 /* The end of a template parameter list has been reached. Process the
3724 tree list into a parameter vector, converting each parameter into a more
3725 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3726 as PARM_DECLs. */
3727
3728 tree
3729 end_template_parm_list (tree parms)
3730 {
3731 int nparms;
3732 tree parm, next;
3733 tree saved_parmlist = make_tree_vec (list_length (parms));
3734
3735 current_template_parms
3736 = tree_cons (size_int (processing_template_decl),
3737 saved_parmlist, current_template_parms);
3738
3739 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3740 {
3741 next = TREE_CHAIN (parm);
3742 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3743 TREE_CHAIN (parm) = NULL_TREE;
3744 }
3745
3746 --processing_template_parmlist;
3747
3748 return saved_parmlist;
3749 }
3750
3751 /* end_template_decl is called after a template declaration is seen. */
3752
3753 void
3754 end_template_decl (void)
3755 {
3756 reset_specialization ();
3757
3758 if (! processing_template_decl)
3759 return;
3760
3761 /* This matches the pushlevel in begin_template_parm_list. */
3762 finish_scope ();
3763
3764 --processing_template_decl;
3765 current_template_parms = TREE_CHAIN (current_template_parms);
3766 }
3767
3768 /* Takes a TREE_LIST representing a template parameter and convert it
3769 into an argument suitable to be passed to the type substitution
3770 functions. Note that If the TREE_LIST contains an error_mark
3771 node, the returned argument is error_mark_node. */
3772
3773 static tree
3774 template_parm_to_arg (tree t)
3775 {
3776
3777 if (t == NULL_TREE
3778 || TREE_CODE (t) != TREE_LIST)
3779 return t;
3780
3781 if (error_operand_p (TREE_VALUE (t)))
3782 return error_mark_node;
3783
3784 t = TREE_VALUE (t);
3785
3786 if (TREE_CODE (t) == TYPE_DECL
3787 || TREE_CODE (t) == TEMPLATE_DECL)
3788 {
3789 t = TREE_TYPE (t);
3790
3791 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3792 {
3793 /* Turn this argument into a TYPE_ARGUMENT_PACK
3794 with a single element, which expands T. */
3795 tree vec = make_tree_vec (1);
3796 #ifdef ENABLE_CHECKING
3797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3798 (vec, TREE_VEC_LENGTH (vec));
3799 #endif
3800 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3801
3802 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3803 SET_ARGUMENT_PACK_ARGS (t, vec);
3804 }
3805 }
3806 else
3807 {
3808 t = DECL_INITIAL (t);
3809
3810 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3811 {
3812 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3813 with a single element, which expands T. */
3814 tree vec = make_tree_vec (1);
3815 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3816 #ifdef ENABLE_CHECKING
3817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3818 (vec, TREE_VEC_LENGTH (vec));
3819 #endif
3820 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3821
3822 t = make_node (NONTYPE_ARGUMENT_PACK);
3823 SET_ARGUMENT_PACK_ARGS (t, vec);
3824 TREE_TYPE (t) = type;
3825 }
3826 }
3827 return t;
3828 }
3829
3830 /* Given a set of template parameters, return them as a set of template
3831 arguments. The template parameters are represented as a TREE_VEC, in
3832 the form documented in cp-tree.h for template arguments. */
3833
3834 static tree
3835 template_parms_to_args (tree parms)
3836 {
3837 tree header;
3838 tree args = NULL_TREE;
3839 int length = TMPL_PARMS_DEPTH (parms);
3840 int l = length;
3841
3842 /* If there is only one level of template parameters, we do not
3843 create a TREE_VEC of TREE_VECs. Instead, we return a single
3844 TREE_VEC containing the arguments. */
3845 if (length > 1)
3846 args = make_tree_vec (length);
3847
3848 for (header = parms; header; header = TREE_CHAIN (header))
3849 {
3850 tree a = copy_node (TREE_VALUE (header));
3851 int i;
3852
3853 TREE_TYPE (a) = NULL_TREE;
3854 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3855 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3856
3857 #ifdef ENABLE_CHECKING
3858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3859 #endif
3860
3861 if (length > 1)
3862 TREE_VEC_ELT (args, --l) = a;
3863 else
3864 args = a;
3865 }
3866
3867 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3868 /* This can happen for template parms of a template template
3869 parameter, e.g:
3870
3871 template<template<class T, class U> class TT> struct S;
3872
3873 Consider the level of the parms of TT; T and U both have
3874 level 2; TT has no template parm of level 1. So in this case
3875 the first element of full_template_args is NULL_TREE. If we
3876 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3877 of 2. This will make tsubst wrongly consider that T and U
3878 have level 1. Instead, let's create a dummy vector as the
3879 first element of full_template_args so that TMPL_ARG_DEPTH
3880 returns the correct depth for args. */
3881 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3882 return args;
3883 }
3884
3885 /* Within the declaration of a template, return the currently active
3886 template parameters as an argument TREE_VEC. */
3887
3888 static tree
3889 current_template_args (void)
3890 {
3891 return template_parms_to_args (current_template_parms);
3892 }
3893
3894 /* Update the declared TYPE by doing any lookups which were thought to be
3895 dependent, but are not now that we know the SCOPE of the declarator. */
3896
3897 tree
3898 maybe_update_decl_type (tree orig_type, tree scope)
3899 {
3900 tree type = orig_type;
3901
3902 if (type == NULL_TREE)
3903 return type;
3904
3905 if (TREE_CODE (orig_type) == TYPE_DECL)
3906 type = TREE_TYPE (type);
3907
3908 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3909 && dependent_type_p (type)
3910 /* Don't bother building up the args in this case. */
3911 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3912 {
3913 /* tsubst in the args corresponding to the template parameters,
3914 including auto if present. Most things will be unchanged, but
3915 make_typename_type and tsubst_qualified_id will resolve
3916 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3917 tree args = current_template_args ();
3918 tree auto_node = type_uses_auto (type);
3919 tree pushed;
3920 if (auto_node)
3921 {
3922 tree auto_vec = make_tree_vec (1);
3923 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3924 args = add_to_template_args (args, auto_vec);
3925 }
3926 pushed = push_scope (scope);
3927 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3928 if (pushed)
3929 pop_scope (scope);
3930 }
3931
3932 if (type == error_mark_node)
3933 return orig_type;
3934
3935 if (TREE_CODE (orig_type) == TYPE_DECL)
3936 {
3937 if (same_type_p (type, TREE_TYPE (orig_type)))
3938 type = orig_type;
3939 else
3940 type = TYPE_NAME (type);
3941 }
3942 return type;
3943 }
3944
3945 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3946 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3947 a member template. Used by push_template_decl below. */
3948
3949 static tree
3950 build_template_decl (tree decl, tree parms, bool member_template_p)
3951 {
3952 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3953 DECL_TEMPLATE_PARMS (tmpl) = parms;
3954 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3955 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3956 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3957
3958 return tmpl;
3959 }
3960
3961 struct template_parm_data
3962 {
3963 /* The level of the template parameters we are currently
3964 processing. */
3965 int level;
3966
3967 /* The index of the specialization argument we are currently
3968 processing. */
3969 int current_arg;
3970
3971 /* An array whose size is the number of template parameters. The
3972 elements are nonzero if the parameter has been used in any one
3973 of the arguments processed so far. */
3974 int* parms;
3975
3976 /* An array whose size is the number of template arguments. The
3977 elements are nonzero if the argument makes use of template
3978 parameters of this level. */
3979 int* arg_uses_template_parms;
3980 };
3981
3982 /* Subroutine of push_template_decl used to see if each template
3983 parameter in a partial specialization is used in the explicit
3984 argument list. If T is of the LEVEL given in DATA (which is
3985 treated as a template_parm_data*), then DATA->PARMS is marked
3986 appropriately. */
3987
3988 static int
3989 mark_template_parm (tree t, void* data)
3990 {
3991 int level;
3992 int idx;
3993 struct template_parm_data* tpd = (struct template_parm_data*) data;
3994
3995 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3996 {
3997 level = TEMPLATE_PARM_LEVEL (t);
3998 idx = TEMPLATE_PARM_IDX (t);
3999 }
4000 else
4001 {
4002 level = TEMPLATE_TYPE_LEVEL (t);
4003 idx = TEMPLATE_TYPE_IDX (t);
4004 }
4005
4006 if (level == tpd->level)
4007 {
4008 tpd->parms[idx] = 1;
4009 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4010 }
4011
4012 /* Return zero so that for_each_template_parm will continue the
4013 traversal of the tree; we want to mark *every* template parm. */
4014 return 0;
4015 }
4016
4017 /* Process the partial specialization DECL. */
4018
4019 static tree
4020 process_partial_specialization (tree decl)
4021 {
4022 tree type = TREE_TYPE (decl);
4023 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4024 tree specargs = CLASSTYPE_TI_ARGS (type);
4025 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4026 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4027 tree inner_parms;
4028 tree inst;
4029 int nargs = TREE_VEC_LENGTH (inner_args);
4030 int ntparms;
4031 int i;
4032 bool did_error_intro = false;
4033 struct template_parm_data tpd;
4034 struct template_parm_data tpd2;
4035
4036 gcc_assert (current_template_parms);
4037
4038 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4039 ntparms = TREE_VEC_LENGTH (inner_parms);
4040
4041 /* We check that each of the template parameters given in the
4042 partial specialization is used in the argument list to the
4043 specialization. For example:
4044
4045 template <class T> struct S;
4046 template <class T> struct S<T*>;
4047
4048 The second declaration is OK because `T*' uses the template
4049 parameter T, whereas
4050
4051 template <class T> struct S<int>;
4052
4053 is no good. Even trickier is:
4054
4055 template <class T>
4056 struct S1
4057 {
4058 template <class U>
4059 struct S2;
4060 template <class U>
4061 struct S2<T>;
4062 };
4063
4064 The S2<T> declaration is actually invalid; it is a
4065 full-specialization. Of course,
4066
4067 template <class U>
4068 struct S2<T (*)(U)>;
4069
4070 or some such would have been OK. */
4071 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4072 tpd.parms = XALLOCAVEC (int, ntparms);
4073 memset (tpd.parms, 0, sizeof (int) * ntparms);
4074
4075 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4076 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4077 for (i = 0; i < nargs; ++i)
4078 {
4079 tpd.current_arg = i;
4080 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4081 &mark_template_parm,
4082 &tpd,
4083 NULL,
4084 /*include_nondeduced_p=*/false);
4085 }
4086 for (i = 0; i < ntparms; ++i)
4087 if (tpd.parms[i] == 0)
4088 {
4089 /* One of the template parms was not used in the
4090 specialization. */
4091 if (!did_error_intro)
4092 {
4093 error ("template parameters not used in partial specialization:");
4094 did_error_intro = true;
4095 }
4096
4097 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4098 }
4099
4100 if (did_error_intro)
4101 return error_mark_node;
4102
4103 /* [temp.class.spec]
4104
4105 The argument list of the specialization shall not be identical to
4106 the implicit argument list of the primary template. */
4107 if (comp_template_args
4108 (inner_args,
4109 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4110 (maintmpl)))))
4111 error ("partial specialization %qT does not specialize any template arguments", type);
4112
4113 /* A partial specialization that replaces multiple parameters of the
4114 primary template with a pack expansion is less specialized for those
4115 parameters. */
4116 if (nargs < DECL_NTPARMS (maintmpl))
4117 {
4118 error ("partial specialization is not more specialized than the "
4119 "primary template because it replaces multiple parameters "
4120 "with a pack expansion");
4121 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4122 return decl;
4123 }
4124
4125 /* [temp.class.spec]
4126
4127 A partially specialized non-type argument expression shall not
4128 involve template parameters of the partial specialization except
4129 when the argument expression is a simple identifier.
4130
4131 The type of a template parameter corresponding to a specialized
4132 non-type argument shall not be dependent on a parameter of the
4133 specialization.
4134
4135 Also, we verify that pack expansions only occur at the
4136 end of the argument list. */
4137 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4138 tpd2.parms = 0;
4139 for (i = 0; i < nargs; ++i)
4140 {
4141 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4142 tree arg = TREE_VEC_ELT (inner_args, i);
4143 tree packed_args = NULL_TREE;
4144 int j, len = 1;
4145
4146 if (ARGUMENT_PACK_P (arg))
4147 {
4148 /* Extract the arguments from the argument pack. We'll be
4149 iterating over these in the following loop. */
4150 packed_args = ARGUMENT_PACK_ARGS (arg);
4151 len = TREE_VEC_LENGTH (packed_args);
4152 }
4153
4154 for (j = 0; j < len; j++)
4155 {
4156 if (packed_args)
4157 /* Get the Jth argument in the parameter pack. */
4158 arg = TREE_VEC_ELT (packed_args, j);
4159
4160 if (PACK_EXPANSION_P (arg))
4161 {
4162 /* Pack expansions must come at the end of the
4163 argument list. */
4164 if ((packed_args && j < len - 1)
4165 || (!packed_args && i < nargs - 1))
4166 {
4167 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4168 error ("parameter pack argument %qE must be at the "
4169 "end of the template argument list", arg);
4170 else
4171 error ("parameter pack argument %qT must be at the "
4172 "end of the template argument list", arg);
4173 }
4174 }
4175
4176 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4177 /* We only care about the pattern. */
4178 arg = PACK_EXPANSION_PATTERN (arg);
4179
4180 if (/* These first two lines are the `non-type' bit. */
4181 !TYPE_P (arg)
4182 && TREE_CODE (arg) != TEMPLATE_DECL
4183 /* This next line is the `argument expression is not just a
4184 simple identifier' condition and also the `specialized
4185 non-type argument' bit. */
4186 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4187 {
4188 if ((!packed_args && tpd.arg_uses_template_parms[i])
4189 || (packed_args && uses_template_parms (arg)))
4190 error ("template argument %qE involves template parameter(s)",
4191 arg);
4192 else
4193 {
4194 /* Look at the corresponding template parameter,
4195 marking which template parameters its type depends
4196 upon. */
4197 tree type = TREE_TYPE (parm);
4198
4199 if (!tpd2.parms)
4200 {
4201 /* We haven't yet initialized TPD2. Do so now. */
4202 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4203 /* The number of parameters here is the number in the
4204 main template, which, as checked in the assertion
4205 above, is NARGS. */
4206 tpd2.parms = XALLOCAVEC (int, nargs);
4207 tpd2.level =
4208 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4209 }
4210
4211 /* Mark the template parameters. But this time, we're
4212 looking for the template parameters of the main
4213 template, not in the specialization. */
4214 tpd2.current_arg = i;
4215 tpd2.arg_uses_template_parms[i] = 0;
4216 memset (tpd2.parms, 0, sizeof (int) * nargs);
4217 for_each_template_parm (type,
4218 &mark_template_parm,
4219 &tpd2,
4220 NULL,
4221 /*include_nondeduced_p=*/false);
4222
4223 if (tpd2.arg_uses_template_parms [i])
4224 {
4225 /* The type depended on some template parameters.
4226 If they are fully specialized in the
4227 specialization, that's OK. */
4228 int j;
4229 int count = 0;
4230 for (j = 0; j < nargs; ++j)
4231 if (tpd2.parms[j] != 0
4232 && tpd.arg_uses_template_parms [j])
4233 ++count;
4234 if (count != 0)
4235 error_n (input_location, count,
4236 "type %qT of template argument %qE depends "
4237 "on a template parameter",
4238 "type %qT of template argument %qE depends "
4239 "on template parameters",
4240 type,
4241 arg);
4242 }
4243 }
4244 }
4245 }
4246 }
4247
4248 /* We should only get here once. */
4249 gcc_assert (!COMPLETE_TYPE_P (type));
4250
4251 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4252 = tree_cons (specargs, inner_parms,
4253 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4254 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4255
4256 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4257 inst = TREE_CHAIN (inst))
4258 {
4259 tree inst_type = TREE_VALUE (inst);
4260 if (COMPLETE_TYPE_P (inst_type)
4261 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4262 {
4263 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4264 if (spec && TREE_TYPE (spec) == type)
4265 permerror (input_location,
4266 "partial specialization of %qT after instantiation "
4267 "of %qT", type, inst_type);
4268 }
4269 }
4270
4271 return decl;
4272 }
4273
4274 /* Check that a template declaration's use of default arguments and
4275 parameter packs is not invalid. Here, PARMS are the template
4276 parameters. IS_PRIMARY is true if DECL is the thing declared by
4277 a primary template. IS_PARTIAL is true if DECL is a partial
4278 specialization.
4279
4280 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4281 declaration (but not a definition); 1 indicates a declaration, 2
4282 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4283 emitted for extraneous default arguments.
4284
4285 Returns TRUE if there were no errors found, FALSE otherwise. */
4286
4287 bool
4288 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4289 bool is_partial, int is_friend_decl)
4290 {
4291 const char *msg;
4292 int last_level_to_check;
4293 tree parm_level;
4294 bool no_errors = true;
4295
4296 /* [temp.param]
4297
4298 A default template-argument shall not be specified in a
4299 function template declaration or a function template definition, nor
4300 in the template-parameter-list of the definition of a member of a
4301 class template. */
4302
4303 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4304 /* You can't have a function template declaration in a local
4305 scope, nor you can you define a member of a class template in a
4306 local scope. */
4307 return true;
4308
4309 if (current_class_type
4310 && !TYPE_BEING_DEFINED (current_class_type)
4311 && DECL_LANG_SPECIFIC (decl)
4312 && DECL_DECLARES_FUNCTION_P (decl)
4313 /* If this is either a friend defined in the scope of the class
4314 or a member function. */
4315 && (DECL_FUNCTION_MEMBER_P (decl)
4316 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4317 : DECL_FRIEND_CONTEXT (decl)
4318 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4319 : false)
4320 /* And, if it was a member function, it really was defined in
4321 the scope of the class. */
4322 && (!DECL_FUNCTION_MEMBER_P (decl)
4323 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4324 /* We already checked these parameters when the template was
4325 declared, so there's no need to do it again now. This function
4326 was defined in class scope, but we're processing it's body now
4327 that the class is complete. */
4328 return true;
4329
4330 /* Core issue 226 (C++0x only): the following only applies to class
4331 templates. */
4332 if (is_primary
4333 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4334 {
4335 /* [temp.param]
4336
4337 If a template-parameter has a default template-argument, all
4338 subsequent template-parameters shall have a default
4339 template-argument supplied. */
4340 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4341 {
4342 tree inner_parms = TREE_VALUE (parm_level);
4343 int ntparms = TREE_VEC_LENGTH (inner_parms);
4344 int seen_def_arg_p = 0;
4345 int i;
4346
4347 for (i = 0; i < ntparms; ++i)
4348 {
4349 tree parm = TREE_VEC_ELT (inner_parms, i);
4350
4351 if (parm == error_mark_node)
4352 continue;
4353
4354 if (TREE_PURPOSE (parm))
4355 seen_def_arg_p = 1;
4356 else if (seen_def_arg_p
4357 && !template_parameter_pack_p (TREE_VALUE (parm)))
4358 {
4359 error ("no default argument for %qD", TREE_VALUE (parm));
4360 /* For better subsequent error-recovery, we indicate that
4361 there should have been a default argument. */
4362 TREE_PURPOSE (parm) = error_mark_node;
4363 no_errors = false;
4364 }
4365 else if (!is_partial
4366 && !is_friend_decl
4367 /* Don't complain about an enclosing partial
4368 specialization. */
4369 && parm_level == parms
4370 && TREE_CODE (decl) == TYPE_DECL
4371 && i < ntparms - 1
4372 && template_parameter_pack_p (TREE_VALUE (parm)))
4373 {
4374 /* A primary class template can only have one
4375 parameter pack, at the end of the template
4376 parameter list. */
4377
4378 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4379 error ("parameter pack %qE must be at the end of the"
4380 " template parameter list", TREE_VALUE (parm));
4381 else
4382 error ("parameter pack %qT must be at the end of the"
4383 " template parameter list",
4384 TREE_TYPE (TREE_VALUE (parm)));
4385
4386 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4387 = error_mark_node;
4388 no_errors = false;
4389 }
4390 }
4391 }
4392 }
4393
4394 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4395 || is_partial
4396 || !is_primary
4397 || is_friend_decl)
4398 /* For an ordinary class template, default template arguments are
4399 allowed at the innermost level, e.g.:
4400 template <class T = int>
4401 struct S {};
4402 but, in a partial specialization, they're not allowed even
4403 there, as we have in [temp.class.spec]:
4404
4405 The template parameter list of a specialization shall not
4406 contain default template argument values.
4407
4408 So, for a partial specialization, or for a function template
4409 (in C++98/C++03), we look at all of them. */
4410 ;
4411 else
4412 /* But, for a primary class template that is not a partial
4413 specialization we look at all template parameters except the
4414 innermost ones. */
4415 parms = TREE_CHAIN (parms);
4416
4417 /* Figure out what error message to issue. */
4418 if (is_friend_decl == 2)
4419 msg = G_("default template arguments may not be used in function template "
4420 "friend re-declaration");
4421 else if (is_friend_decl)
4422 msg = G_("default template arguments may not be used in function template "
4423 "friend declarations");
4424 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4425 msg = G_("default template arguments may not be used in function templates "
4426 "without -std=c++11 or -std=gnu++11");
4427 else if (is_partial)
4428 msg = G_("default template arguments may not be used in "
4429 "partial specializations");
4430 else
4431 msg = G_("default argument for template parameter for class enclosing %qD");
4432
4433 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4434 /* If we're inside a class definition, there's no need to
4435 examine the parameters to the class itself. On the one
4436 hand, they will be checked when the class is defined, and,
4437 on the other, default arguments are valid in things like:
4438 template <class T = double>
4439 struct S { template <class U> void f(U); };
4440 Here the default argument for `S' has no bearing on the
4441 declaration of `f'. */
4442 last_level_to_check = template_class_depth (current_class_type) + 1;
4443 else
4444 /* Check everything. */
4445 last_level_to_check = 0;
4446
4447 for (parm_level = parms;
4448 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4449 parm_level = TREE_CHAIN (parm_level))
4450 {
4451 tree inner_parms = TREE_VALUE (parm_level);
4452 int i;
4453 int ntparms;
4454
4455 ntparms = TREE_VEC_LENGTH (inner_parms);
4456 for (i = 0; i < ntparms; ++i)
4457 {
4458 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4459 continue;
4460
4461 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4462 {
4463 if (msg)
4464 {
4465 no_errors = false;
4466 if (is_friend_decl == 2)
4467 return no_errors;
4468
4469 error (msg, decl);
4470 msg = 0;
4471 }
4472
4473 /* Clear out the default argument so that we are not
4474 confused later. */
4475 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4476 }
4477 }
4478
4479 /* At this point, if we're still interested in issuing messages,
4480 they must apply to classes surrounding the object declared. */
4481 if (msg)
4482 msg = G_("default argument for template parameter for class "
4483 "enclosing %qD");
4484 }
4485
4486 return no_errors;
4487 }
4488
4489 /* Worker for push_template_decl_real, called via
4490 for_each_template_parm. DATA is really an int, indicating the
4491 level of the parameters we are interested in. If T is a template
4492 parameter of that level, return nonzero. */
4493
4494 static int
4495 template_parm_this_level_p (tree t, void* data)
4496 {
4497 int this_level = *(int *)data;
4498 int level;
4499
4500 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4501 level = TEMPLATE_PARM_LEVEL (t);
4502 else
4503 level = TEMPLATE_TYPE_LEVEL (t);
4504 return level == this_level;
4505 }
4506
4507 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4508 parameters given by current_template_args, or reuses a
4509 previously existing one, if appropriate. Returns the DECL, or an
4510 equivalent one, if it is replaced via a call to duplicate_decls.
4511
4512 If IS_FRIEND is true, DECL is a friend declaration. */
4513
4514 tree
4515 push_template_decl_real (tree decl, bool is_friend)
4516 {
4517 tree tmpl;
4518 tree args;
4519 tree info;
4520 tree ctx;
4521 bool is_primary;
4522 bool is_partial;
4523 int new_template_p = 0;
4524 /* True if the template is a member template, in the sense of
4525 [temp.mem]. */
4526 bool member_template_p = false;
4527
4528 if (decl == error_mark_node || !current_template_parms)
4529 return error_mark_node;
4530
4531 /* See if this is a partial specialization. */
4532 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4533 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4534 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4535
4536 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4537 is_friend = true;
4538
4539 if (is_friend)
4540 /* For a friend, we want the context of the friend function, not
4541 the type of which it is a friend. */
4542 ctx = CP_DECL_CONTEXT (decl);
4543 else if (CP_DECL_CONTEXT (decl)
4544 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4545 /* In the case of a virtual function, we want the class in which
4546 it is defined. */
4547 ctx = CP_DECL_CONTEXT (decl);
4548 else
4549 /* Otherwise, if we're currently defining some class, the DECL
4550 is assumed to be a member of the class. */
4551 ctx = current_scope ();
4552
4553 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4554 ctx = NULL_TREE;
4555
4556 if (!DECL_CONTEXT (decl))
4557 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4558
4559 /* See if this is a primary template. */
4560 if (is_friend && ctx)
4561 /* A friend template that specifies a class context, i.e.
4562 template <typename T> friend void A<T>::f();
4563 is not primary. */
4564 is_primary = false;
4565 else
4566 is_primary = template_parm_scope_p ();
4567
4568 if (is_primary)
4569 {
4570 if (DECL_CLASS_SCOPE_P (decl))
4571 member_template_p = true;
4572 if (TREE_CODE (decl) == TYPE_DECL
4573 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4574 {
4575 error ("template class without a name");
4576 return error_mark_node;
4577 }
4578 else if (TREE_CODE (decl) == FUNCTION_DECL)
4579 {
4580 if (DECL_DESTRUCTOR_P (decl))
4581 {
4582 /* [temp.mem]
4583
4584 A destructor shall not be a member template. */
4585 error ("destructor %qD declared as member template", decl);
4586 return error_mark_node;
4587 }
4588 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4589 && (!prototype_p (TREE_TYPE (decl))
4590 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4591 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4592 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4593 == void_list_node)))
4594 {
4595 /* [basic.stc.dynamic.allocation]
4596
4597 An allocation function can be a function
4598 template. ... Template allocation functions shall
4599 have two or more parameters. */
4600 error ("invalid template declaration of %qD", decl);
4601 return error_mark_node;
4602 }
4603 }
4604 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4605 && CLASS_TYPE_P (TREE_TYPE (decl)))
4606 /* OK */;
4607 else if (TREE_CODE (decl) == TYPE_DECL
4608 && TYPE_DECL_ALIAS_P (decl))
4609 /* alias-declaration */
4610 gcc_assert (!DECL_ARTIFICIAL (decl));
4611 else
4612 {
4613 error ("template declaration of %q#D", decl);
4614 return error_mark_node;
4615 }
4616 }
4617
4618 /* Check to see that the rules regarding the use of default
4619 arguments are not being violated. */
4620 check_default_tmpl_args (decl, current_template_parms,
4621 is_primary, is_partial, /*is_friend_decl=*/0);
4622
4623 /* Ensure that there are no parameter packs in the type of this
4624 declaration that have not been expanded. */
4625 if (TREE_CODE (decl) == FUNCTION_DECL)
4626 {
4627 /* Check each of the arguments individually to see if there are
4628 any bare parameter packs. */
4629 tree type = TREE_TYPE (decl);
4630 tree arg = DECL_ARGUMENTS (decl);
4631 tree argtype = TYPE_ARG_TYPES (type);
4632
4633 while (arg && argtype)
4634 {
4635 if (!FUNCTION_PARAMETER_PACK_P (arg)
4636 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4637 {
4638 /* This is a PARM_DECL that contains unexpanded parameter
4639 packs. We have already complained about this in the
4640 check_for_bare_parameter_packs call, so just replace
4641 these types with ERROR_MARK_NODE. */
4642 TREE_TYPE (arg) = error_mark_node;
4643 TREE_VALUE (argtype) = error_mark_node;
4644 }
4645
4646 arg = DECL_CHAIN (arg);
4647 argtype = TREE_CHAIN (argtype);
4648 }
4649
4650 /* Check for bare parameter packs in the return type and the
4651 exception specifiers. */
4652 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4653 /* Errors were already issued, set return type to int
4654 as the frontend doesn't expect error_mark_node as
4655 the return type. */
4656 TREE_TYPE (type) = integer_type_node;
4657 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4658 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4659 }
4660 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl))
4662 ? DECL_ORIGINAL_TYPE (decl)
4663 : TREE_TYPE (decl)))
4664 {
4665 TREE_TYPE (decl) = error_mark_node;
4666 return error_mark_node;
4667 }
4668
4669 if (is_partial)
4670 return process_partial_specialization (decl);
4671
4672 args = current_template_args ();
4673
4674 if (!ctx
4675 || TREE_CODE (ctx) == FUNCTION_DECL
4676 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4677 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4678 {
4679 if (DECL_LANG_SPECIFIC (decl)
4680 && DECL_TEMPLATE_INFO (decl)
4681 && DECL_TI_TEMPLATE (decl))
4682 tmpl = DECL_TI_TEMPLATE (decl);
4683 /* If DECL is a TYPE_DECL for a class-template, then there won't
4684 be DECL_LANG_SPECIFIC. The information equivalent to
4685 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4686 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4687 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4688 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4689 {
4690 /* Since a template declaration already existed for this
4691 class-type, we must be redeclaring it here. Make sure
4692 that the redeclaration is valid. */
4693 redeclare_class_template (TREE_TYPE (decl),
4694 current_template_parms);
4695 /* We don't need to create a new TEMPLATE_DECL; just use the
4696 one we already had. */
4697 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4698 }
4699 else
4700 {
4701 tmpl = build_template_decl (decl, current_template_parms,
4702 member_template_p);
4703 new_template_p = 1;
4704
4705 if (DECL_LANG_SPECIFIC (decl)
4706 && DECL_TEMPLATE_SPECIALIZATION (decl))
4707 {
4708 /* A specialization of a member template of a template
4709 class. */
4710 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4711 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4712 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4713 }
4714 }
4715 }
4716 else
4717 {
4718 tree a, t, current, parms;
4719 int i;
4720 tree tinfo = get_template_info (decl);
4721
4722 if (!tinfo)
4723 {
4724 error ("template definition of non-template %q#D", decl);
4725 return error_mark_node;
4726 }
4727
4728 tmpl = TI_TEMPLATE (tinfo);
4729
4730 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4731 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4732 && DECL_TEMPLATE_SPECIALIZATION (decl)
4733 && DECL_MEMBER_TEMPLATE_P (tmpl))
4734 {
4735 tree new_tmpl;
4736
4737 /* The declaration is a specialization of a member
4738 template, declared outside the class. Therefore, the
4739 innermost template arguments will be NULL, so we
4740 replace them with the arguments determined by the
4741 earlier call to check_explicit_specialization. */
4742 args = DECL_TI_ARGS (decl);
4743
4744 new_tmpl
4745 = build_template_decl (decl, current_template_parms,
4746 member_template_p);
4747 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4748 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4749 DECL_TI_TEMPLATE (decl) = new_tmpl;
4750 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4751 DECL_TEMPLATE_INFO (new_tmpl)
4752 = build_template_info (tmpl, args);
4753
4754 register_specialization (new_tmpl,
4755 most_general_template (tmpl),
4756 args,
4757 is_friend, 0);
4758 return decl;
4759 }
4760
4761 /* Make sure the template headers we got make sense. */
4762
4763 parms = DECL_TEMPLATE_PARMS (tmpl);
4764 i = TMPL_PARMS_DEPTH (parms);
4765 if (TMPL_ARGS_DEPTH (args) != i)
4766 {
4767 error ("expected %d levels of template parms for %q#D, got %d",
4768 i, decl, TMPL_ARGS_DEPTH (args));
4769 }
4770 else
4771 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4772 {
4773 a = TMPL_ARGS_LEVEL (args, i);
4774 t = INNERMOST_TEMPLATE_PARMS (parms);
4775
4776 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4777 {
4778 if (current == decl)
4779 error ("got %d template parameters for %q#D",
4780 TREE_VEC_LENGTH (a), decl);
4781 else
4782 error ("got %d template parameters for %q#T",
4783 TREE_VEC_LENGTH (a), current);
4784 error (" but %d required", TREE_VEC_LENGTH (t));
4785 /* Avoid crash in import_export_decl. */
4786 DECL_INTERFACE_KNOWN (decl) = 1;
4787 return error_mark_node;
4788 }
4789
4790 if (current == decl)
4791 current = ctx;
4792 else if (current == NULL_TREE)
4793 /* Can happen in erroneous input. */
4794 break;
4795 else
4796 current = (TYPE_P (current)
4797 ? TYPE_CONTEXT (current)
4798 : DECL_CONTEXT (current));
4799 }
4800
4801 /* Check that the parms are used in the appropriate qualifying scopes
4802 in the declarator. */
4803 if (!comp_template_args
4804 (TI_ARGS (tinfo),
4805 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4806 {
4807 error ("\
4808 template arguments to %qD do not match original template %qD",
4809 decl, DECL_TEMPLATE_RESULT (tmpl));
4810 if (!uses_template_parms (TI_ARGS (tinfo)))
4811 inform (input_location, "use template<> for an explicit specialization");
4812 /* Avoid crash in import_export_decl. */
4813 DECL_INTERFACE_KNOWN (decl) = 1;
4814 return error_mark_node;
4815 }
4816 }
4817
4818 DECL_TEMPLATE_RESULT (tmpl) = decl;
4819 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4820
4821 /* Push template declarations for global functions and types. Note
4822 that we do not try to push a global template friend declared in a
4823 template class; such a thing may well depend on the template
4824 parameters of the class. */
4825 if (new_template_p && !ctx
4826 && !(is_friend && template_class_depth (current_class_type) > 0))
4827 {
4828 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4829 if (tmpl == error_mark_node)
4830 return error_mark_node;
4831
4832 /* Hide template friend classes that haven't been declared yet. */
4833 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4834 {
4835 DECL_ANTICIPATED (tmpl) = 1;
4836 DECL_FRIEND_P (tmpl) = 1;
4837 }
4838 }
4839
4840 if (is_primary)
4841 {
4842 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4843 int i;
4844
4845 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4846 if (DECL_CONV_FN_P (tmpl))
4847 {
4848 int depth = TMPL_PARMS_DEPTH (parms);
4849
4850 /* It is a conversion operator. See if the type converted to
4851 depends on innermost template operands. */
4852
4853 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4854 depth))
4855 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4856 }
4857
4858 /* Give template template parms a DECL_CONTEXT of the template
4859 for which they are a parameter. */
4860 parms = INNERMOST_TEMPLATE_PARMS (parms);
4861 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4862 {
4863 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4864 if (TREE_CODE (parm) == TEMPLATE_DECL)
4865 DECL_CONTEXT (parm) = tmpl;
4866 }
4867 }
4868
4869 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4870 back to its most general template. If TMPL is a specialization,
4871 ARGS may only have the innermost set of arguments. Add the missing
4872 argument levels if necessary. */
4873 if (DECL_TEMPLATE_INFO (tmpl))
4874 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4875
4876 info = build_template_info (tmpl, args);
4877
4878 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4879 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4880 else
4881 {
4882 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4883 retrofit_lang_decl (decl);
4884 if (DECL_LANG_SPECIFIC (decl))
4885 DECL_TEMPLATE_INFO (decl) = info;
4886 }
4887
4888 return DECL_TEMPLATE_RESULT (tmpl);
4889 }
4890
4891 tree
4892 push_template_decl (tree decl)
4893 {
4894 return push_template_decl_real (decl, false);
4895 }
4896
4897 /* FN is an inheriting constructor that inherits from the constructor
4898 template INHERITED; turn FN into a constructor template with a matching
4899 template header. */
4900
4901 tree
4902 add_inherited_template_parms (tree fn, tree inherited)
4903 {
4904 tree inner_parms
4905 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4906 inner_parms = copy_node (inner_parms);
4907 tree parms
4908 = tree_cons (size_int (processing_template_decl + 1),
4909 inner_parms, current_template_parms);
4910 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4911 tree args = template_parms_to_args (parms);
4912 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4913 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4914 DECL_TEMPLATE_RESULT (tmpl) = fn;
4915 DECL_ARTIFICIAL (tmpl) = true;
4916 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4917 return tmpl;
4918 }
4919
4920 /* Called when a class template TYPE is redeclared with the indicated
4921 template PARMS, e.g.:
4922
4923 template <class T> struct S;
4924 template <class T> struct S {}; */
4925
4926 bool
4927 redeclare_class_template (tree type, tree parms)
4928 {
4929 tree tmpl;
4930 tree tmpl_parms;
4931 int i;
4932
4933 if (!TYPE_TEMPLATE_INFO (type))
4934 {
4935 error ("%qT is not a template type", type);
4936 return false;
4937 }
4938
4939 tmpl = TYPE_TI_TEMPLATE (type);
4940 if (!PRIMARY_TEMPLATE_P (tmpl))
4941 /* The type is nested in some template class. Nothing to worry
4942 about here; there are no new template parameters for the nested
4943 type. */
4944 return true;
4945
4946 if (!parms)
4947 {
4948 error ("template specifiers not specified in declaration of %qD",
4949 tmpl);
4950 return false;
4951 }
4952
4953 parms = INNERMOST_TEMPLATE_PARMS (parms);
4954 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4955
4956 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4957 {
4958 error_n (input_location, TREE_VEC_LENGTH (parms),
4959 "redeclared with %d template parameter",
4960 "redeclared with %d template parameters",
4961 TREE_VEC_LENGTH (parms));
4962 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4963 "previous declaration %q+D used %d template parameter",
4964 "previous declaration %q+D used %d template parameters",
4965 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4966 return false;
4967 }
4968
4969 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4970 {
4971 tree tmpl_parm;
4972 tree parm;
4973 tree tmpl_default;
4974 tree parm_default;
4975
4976 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4977 || TREE_VEC_ELT (parms, i) == error_mark_node)
4978 continue;
4979
4980 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4981 if (tmpl_parm == error_mark_node)
4982 return false;
4983
4984 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4985 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4986 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4987
4988 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4989 TEMPLATE_DECL. */
4990 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4991 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4992 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4993 || (TREE_CODE (tmpl_parm) != PARM_DECL
4994 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4995 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4996 || (TREE_CODE (tmpl_parm) == PARM_DECL
4997 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4998 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4999 {
5000 error ("template parameter %q+#D", tmpl_parm);
5001 error ("redeclared here as %q#D", parm);
5002 return false;
5003 }
5004
5005 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5006 {
5007 /* We have in [temp.param]:
5008
5009 A template-parameter may not be given default arguments
5010 by two different declarations in the same scope. */
5011 error_at (input_location, "redefinition of default argument for %q#D", parm);
5012 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5013 "original definition appeared here");
5014 return false;
5015 }
5016
5017 if (parm_default != NULL_TREE)
5018 /* Update the previous template parameters (which are the ones
5019 that will really count) with the new default value. */
5020 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5021 else if (tmpl_default != NULL_TREE)
5022 /* Update the new parameters, too; they'll be used as the
5023 parameters for any members. */
5024 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5025 }
5026
5027 return true;
5028 }
5029
5030 /* Simplify EXPR if it is a non-dependent expression. Returns the
5031 (possibly simplified) expression. */
5032
5033 tree
5034 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5035 {
5036 if (expr == NULL_TREE)
5037 return NULL_TREE;
5038
5039 /* If we're in a template, but EXPR isn't value dependent, simplify
5040 it. We're supposed to treat:
5041
5042 template <typename T> void f(T[1 + 1]);
5043 template <typename T> void f(T[2]);
5044
5045 as two declarations of the same function, for example. */
5046 if (processing_template_decl
5047 && !type_dependent_expression_p (expr)
5048 && potential_constant_expression (expr)
5049 && !value_dependent_expression_p (expr))
5050 {
5051 HOST_WIDE_INT saved_processing_template_decl;
5052
5053 saved_processing_template_decl = processing_template_decl;
5054 processing_template_decl = 0;
5055 expr = tsubst_copy_and_build (expr,
5056 /*args=*/NULL_TREE,
5057 complain,
5058 /*in_decl=*/NULL_TREE,
5059 /*function_p=*/false,
5060 /*integral_constant_expression_p=*/true);
5061 processing_template_decl = saved_processing_template_decl;
5062 }
5063 return expr;
5064 }
5065
5066 tree
5067 fold_non_dependent_expr (tree expr)
5068 {
5069 return fold_non_dependent_expr_sfinae (expr, tf_error);
5070 }
5071
5072 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5073 template declaration, or a TYPE_DECL for an alias declaration. */
5074
5075 bool
5076 alias_type_or_template_p (tree t)
5077 {
5078 if (t == NULL_TREE)
5079 return false;
5080 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5081 || (TYPE_P (t)
5082 && TYPE_NAME (t)
5083 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5084 || DECL_ALIAS_TEMPLATE_P (t));
5085 }
5086
5087 /* Return TRUE iff is a specialization of an alias template. */
5088
5089 bool
5090 alias_template_specialization_p (const_tree t)
5091 {
5092 if (t == NULL_TREE)
5093 return false;
5094
5095 return (TYPE_P (t)
5096 && TYPE_TEMPLATE_INFO (t)
5097 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5098 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5099 }
5100
5101 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5102 must be a function or a pointer-to-function type, as specified
5103 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5104 and check that the resulting function has external linkage. */
5105
5106 static tree
5107 convert_nontype_argument_function (tree type, tree expr)
5108 {
5109 tree fns = expr;
5110 tree fn, fn_no_ptr;
5111 linkage_kind linkage;
5112
5113 fn = instantiate_type (type, fns, tf_none);
5114 if (fn == error_mark_node)
5115 return error_mark_node;
5116
5117 fn_no_ptr = fn;
5118 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5119 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5120 if (BASELINK_P (fn_no_ptr))
5121 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5122
5123 /* [temp.arg.nontype]/1
5124
5125 A template-argument for a non-type, non-template template-parameter
5126 shall be one of:
5127 [...]
5128 -- the address of an object or function with external [C++11: or
5129 internal] linkage. */
5130
5131 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5132 {
5133 error ("%qE is not a valid template argument for type %qT", expr, type);
5134 if (TREE_CODE (type) == POINTER_TYPE)
5135 error ("it must be the address of a function with external linkage");
5136 else
5137 error ("it must be the name of a function with external linkage");
5138 return NULL_TREE;
5139 }
5140
5141 linkage = decl_linkage (fn_no_ptr);
5142 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5143 {
5144 if (cxx_dialect >= cxx0x)
5145 error ("%qE is not a valid template argument for type %qT "
5146 "because %qD has no linkage",
5147 expr, type, fn_no_ptr);
5148 else
5149 error ("%qE is not a valid template argument for type %qT "
5150 "because %qD does not have external linkage",
5151 expr, type, fn_no_ptr);
5152 return NULL_TREE;
5153 }
5154
5155 return fn;
5156 }
5157
5158 /* Subroutine of convert_nontype_argument.
5159 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5160 Emit an error otherwise. */
5161
5162 static bool
5163 check_valid_ptrmem_cst_expr (tree type, tree expr,
5164 tsubst_flags_t complain)
5165 {
5166 STRIP_NOPS (expr);
5167 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5168 return true;
5169 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5170 return true;
5171 if (complain & tf_error)
5172 {
5173 error ("%qE is not a valid template argument for type %qT",
5174 expr, type);
5175 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5176 }
5177 return false;
5178 }
5179
5180 /* Returns TRUE iff the address of OP is value-dependent.
5181
5182 14.6.2.4 [temp.dep.temp]:
5183 A non-integral non-type template-argument is dependent if its type is
5184 dependent or it has either of the following forms
5185 qualified-id
5186 & qualified-id
5187 and contains a nested-name-specifier which specifies a class-name that
5188 names a dependent type.
5189
5190 We generalize this to just say that the address of a member of a
5191 dependent class is value-dependent; the above doesn't cover the
5192 address of a static data member named with an unqualified-id. */
5193
5194 static bool
5195 has_value_dependent_address (tree op)
5196 {
5197 /* We could use get_inner_reference here, but there's no need;
5198 this is only relevant for template non-type arguments, which
5199 can only be expressed as &id-expression. */
5200 if (DECL_P (op))
5201 {
5202 tree ctx = CP_DECL_CONTEXT (op);
5203 if (TYPE_P (ctx) && dependent_type_p (ctx))
5204 return true;
5205 }
5206
5207 return false;
5208 }
5209
5210 /* The next set of functions are used for providing helpful explanatory
5211 diagnostics for failed overload resolution. Their messages should be
5212 indented by two spaces for consistency with the messages in
5213 call.c */
5214
5215 static int
5216 unify_success (bool /*explain_p*/)
5217 {
5218 return 0;
5219 }
5220
5221 static int
5222 unify_parameter_deduction_failure (bool explain_p, tree parm)
5223 {
5224 if (explain_p)
5225 inform (input_location,
5226 " couldn't deduce template parameter %qD", parm);
5227 return 1;
5228 }
5229
5230 static int
5231 unify_invalid (bool /*explain_p*/)
5232 {
5233 return 1;
5234 }
5235
5236 static int
5237 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5238 {
5239 if (explain_p)
5240 inform (input_location,
5241 " types %qT and %qT have incompatible cv-qualifiers",
5242 parm, arg);
5243 return 1;
5244 }
5245
5246 static int
5247 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5248 {
5249 if (explain_p)
5250 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5251 return 1;
5252 }
5253
5254 static int
5255 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5256 {
5257 if (explain_p)
5258 inform (input_location,
5259 " template parameter %qD is not a parameter pack, but "
5260 "argument %qD is",
5261 parm, arg);
5262 return 1;
5263 }
5264
5265 static int
5266 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5267 {
5268 if (explain_p)
5269 inform (input_location,
5270 " template argument %qE does not match "
5271 "pointer-to-member constant %qE",
5272 arg, parm);
5273 return 1;
5274 }
5275
5276 static int
5277 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5278 {
5279 if (explain_p)
5280 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5281 return 1;
5282 }
5283
5284 static int
5285 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5286 {
5287 if (explain_p)
5288 inform (input_location,
5289 " inconsistent parameter pack deduction with %qT and %qT",
5290 old_arg, new_arg);
5291 return 1;
5292 }
5293
5294 static int
5295 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5296 {
5297 if (explain_p)
5298 {
5299 if (TYPE_P (parm))
5300 inform (input_location,
5301 " deduced conflicting types for parameter %qT (%qT and %qT)",
5302 parm, first, second);
5303 else
5304 inform (input_location,
5305 " deduced conflicting values for non-type parameter "
5306 "%qE (%qE and %qE)", parm, first, second);
5307 }
5308 return 1;
5309 }
5310
5311 static int
5312 unify_vla_arg (bool explain_p, tree arg)
5313 {
5314 if (explain_p)
5315 inform (input_location,
5316 " variable-sized array type %qT is not "
5317 "a valid template argument",
5318 arg);
5319 return 1;
5320 }
5321
5322 static int
5323 unify_method_type_error (bool explain_p, tree arg)
5324 {
5325 if (explain_p)
5326 inform (input_location,
5327 " member function type %qT is not a valid template argument",
5328 arg);
5329 return 1;
5330 }
5331
5332 static int
5333 unify_arity (bool explain_p, int have, int wanted)
5334 {
5335 if (explain_p)
5336 inform_n (input_location, wanted,
5337 " candidate expects %d argument, %d provided",
5338 " candidate expects %d arguments, %d provided",
5339 wanted, have);
5340 return 1;
5341 }
5342
5343 static int
5344 unify_too_many_arguments (bool explain_p, int have, int wanted)
5345 {
5346 return unify_arity (explain_p, have, wanted);
5347 }
5348
5349 static int
5350 unify_too_few_arguments (bool explain_p, int have, int wanted)
5351 {
5352 return unify_arity (explain_p, have, wanted);
5353 }
5354
5355 static int
5356 unify_arg_conversion (bool explain_p, tree to_type,
5357 tree from_type, tree arg)
5358 {
5359 if (explain_p)
5360 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5361 arg, from_type, to_type);
5362 return 1;
5363 }
5364
5365 static int
5366 unify_no_common_base (bool explain_p, enum template_base_result r,
5367 tree parm, tree arg)
5368 {
5369 if (explain_p)
5370 switch (r)
5371 {
5372 case tbr_ambiguous_baseclass:
5373 inform (input_location, " %qT is an ambiguous base class of %qT",
5374 arg, parm);
5375 break;
5376 default:
5377 inform (input_location, " %qT is not derived from %qT", arg, parm);
5378 break;
5379 }
5380 return 1;
5381 }
5382
5383 static int
5384 unify_inconsistent_template_template_parameters (bool explain_p)
5385 {
5386 if (explain_p)
5387 inform (input_location,
5388 " template parameters of a template template argument are "
5389 "inconsistent with other deduced template arguments");
5390 return 1;
5391 }
5392
5393 static int
5394 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5395 {
5396 if (explain_p)
5397 inform (input_location,
5398 " can't deduce a template for %qT from non-template type %qT",
5399 parm, arg);
5400 return 1;
5401 }
5402
5403 static int
5404 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5405 {
5406 if (explain_p)
5407 inform (input_location,
5408 " template argument %qE does not match %qD", arg, parm);
5409 return 1;
5410 }
5411
5412 static int
5413 unify_overload_resolution_failure (bool explain_p, tree arg)
5414 {
5415 if (explain_p)
5416 inform (input_location,
5417 " could not resolve address from overloaded function %qE",
5418 arg);
5419 return 1;
5420 }
5421
5422 /* Attempt to convert the non-type template parameter EXPR to the
5423 indicated TYPE. If the conversion is successful, return the
5424 converted value. If the conversion is unsuccessful, return
5425 NULL_TREE if we issued an error message, or error_mark_node if we
5426 did not. We issue error messages for out-and-out bad template
5427 parameters, but not simply because the conversion failed, since we
5428 might be just trying to do argument deduction. Both TYPE and EXPR
5429 must be non-dependent.
5430
5431 The conversion follows the special rules described in
5432 [temp.arg.nontype], and it is much more strict than an implicit
5433 conversion.
5434
5435 This function is called twice for each template argument (see
5436 lookup_template_class for a more accurate description of this
5437 problem). This means that we need to handle expressions which
5438 are not valid in a C++ source, but can be created from the
5439 first call (for instance, casts to perform conversions). These
5440 hacks can go away after we fix the double coercion problem. */
5441
5442 static tree
5443 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5444 {
5445 tree expr_type;
5446
5447 /* Detect immediately string literals as invalid non-type argument.
5448 This special-case is not needed for correctness (we would easily
5449 catch this later), but only to provide better diagnostic for this
5450 common user mistake. As suggested by DR 100, we do not mention
5451 linkage issues in the diagnostic as this is not the point. */
5452 /* FIXME we're making this OK. */
5453 if (TREE_CODE (expr) == STRING_CST)
5454 {
5455 if (complain & tf_error)
5456 error ("%qE is not a valid template argument for type %qT "
5457 "because string literals can never be used in this context",
5458 expr, type);
5459 return NULL_TREE;
5460 }
5461
5462 /* Add the ADDR_EXPR now for the benefit of
5463 value_dependent_expression_p. */
5464 if (TYPE_PTROBV_P (type)
5465 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5466 {
5467 expr = decay_conversion (expr, complain);
5468 if (expr == error_mark_node)
5469 return error_mark_node;
5470 }
5471
5472 /* If we are in a template, EXPR may be non-dependent, but still
5473 have a syntactic, rather than semantic, form. For example, EXPR
5474 might be a SCOPE_REF, rather than the VAR_DECL to which the
5475 SCOPE_REF refers. Preserving the qualifying scope is necessary
5476 so that access checking can be performed when the template is
5477 instantiated -- but here we need the resolved form so that we can
5478 convert the argument. */
5479 if (TYPE_REF_OBJ_P (type)
5480 && has_value_dependent_address (expr))
5481 /* If we want the address and it's value-dependent, don't fold. */;
5482 else if (!type_unknown_p (expr))
5483 expr = fold_non_dependent_expr_sfinae (expr, complain);
5484 if (error_operand_p (expr))
5485 return error_mark_node;
5486 expr_type = TREE_TYPE (expr);
5487 if (TREE_CODE (type) == REFERENCE_TYPE)
5488 expr = mark_lvalue_use (expr);
5489 else
5490 expr = mark_rvalue_use (expr);
5491
5492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5493 to a non-type argument of "nullptr". */
5494 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5495 expr = convert (type, expr);
5496
5497 /* In C++11, integral or enumeration non-type template arguments can be
5498 arbitrary constant expressions. Pointer and pointer to
5499 member arguments can be general constant expressions that evaluate
5500 to a null value, but otherwise still need to be of a specific form. */
5501 if (cxx_dialect >= cxx0x)
5502 {
5503 if (TREE_CODE (expr) == PTRMEM_CST)
5504 /* A PTRMEM_CST is already constant, and a valid template
5505 argument for a parameter of pointer to member type, we just want
5506 to leave it in that form rather than lower it to a
5507 CONSTRUCTOR. */;
5508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5509 expr = maybe_constant_value (expr);
5510 else if (TYPE_PTR_OR_PTRMEM_P (type))
5511 {
5512 tree folded = maybe_constant_value (expr);
5513 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5514 : null_member_pointer_value_p (folded))
5515 expr = folded;
5516 }
5517 }
5518
5519 /* HACK: Due to double coercion, we can get a
5520 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5521 which is the tree that we built on the first call (see
5522 below when coercing to reference to object or to reference to
5523 function). We just strip everything and get to the arg.
5524 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5525 for examples. */
5526 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5527 {
5528 tree probe_type, probe = expr;
5529 if (REFERENCE_REF_P (probe))
5530 probe = TREE_OPERAND (probe, 0);
5531 probe_type = TREE_TYPE (probe);
5532 if (TREE_CODE (probe) == NOP_EXPR)
5533 {
5534 /* ??? Maybe we could use convert_from_reference here, but we
5535 would need to relax its constraints because the NOP_EXPR
5536 could actually change the type to something more cv-qualified,
5537 and this is not folded by convert_from_reference. */
5538 tree addr = TREE_OPERAND (probe, 0);
5539 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5540 && TREE_CODE (addr) == ADDR_EXPR
5541 && TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE
5542 && (same_type_ignoring_top_level_qualifiers_p
5543 (TREE_TYPE (probe_type),
5544 TREE_TYPE (TREE_TYPE (addr)))))
5545 {
5546 expr = TREE_OPERAND (addr, 0);
5547 expr_type = TREE_TYPE (expr);
5548 }
5549 }
5550 }
5551
5552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5553 parameter is a pointer to object, through decay and
5554 qualification conversion. Let's strip everything. */
5555 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5556 {
5557 STRIP_NOPS (expr);
5558 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5559 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5560 /* Skip the ADDR_EXPR only if it is part of the decay for
5561 an array. Otherwise, it is part of the original argument
5562 in the source code. */
5563 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5564 expr = TREE_OPERAND (expr, 0);
5565 expr_type = TREE_TYPE (expr);
5566 }
5567
5568 /* [temp.arg.nontype]/5, bullet 1
5569
5570 For a non-type template-parameter of integral or enumeration type,
5571 integral promotions (_conv.prom_) and integral conversions
5572 (_conv.integral_) are applied. */
5573 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5574 {
5575 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5576 t = maybe_constant_value (t);
5577 if (t != error_mark_node)
5578 expr = t;
5579
5580 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5581 return error_mark_node;
5582
5583 /* Notice that there are constant expressions like '4 % 0' which
5584 do not fold into integer constants. */
5585 if (TREE_CODE (expr) != INTEGER_CST)
5586 {
5587 if (complain & tf_error)
5588 {
5589 int errs = errorcount, warns = warningcount;
5590 if (processing_template_decl
5591 && !require_potential_constant_expression (expr))
5592 return NULL_TREE;
5593 expr = cxx_constant_value (expr);
5594 if (errorcount > errs || warningcount > warns)
5595 inform (EXPR_LOC_OR_HERE (expr),
5596 "in template argument for type %qT ", type);
5597 if (expr == error_mark_node)
5598 return NULL_TREE;
5599 /* else cxx_constant_value complained but gave us
5600 a real constant, so go ahead. */
5601 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5602 }
5603 else
5604 return NULL_TREE;
5605 }
5606 }
5607 /* [temp.arg.nontype]/5, bullet 2
5608
5609 For a non-type template-parameter of type pointer to object,
5610 qualification conversions (_conv.qual_) and the array-to-pointer
5611 conversion (_conv.array_) are applied. */
5612 else if (TYPE_PTROBV_P (type))
5613 {
5614 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5615
5616 A template-argument for a non-type, non-template template-parameter
5617 shall be one of: [...]
5618
5619 -- the name of a non-type template-parameter;
5620 -- the address of an object or function with external linkage, [...]
5621 expressed as "& id-expression" where the & is optional if the name
5622 refers to a function or array, or if the corresponding
5623 template-parameter is a reference.
5624
5625 Here, we do not care about functions, as they are invalid anyway
5626 for a parameter of type pointer-to-object. */
5627
5628 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5629 /* Non-type template parameters are OK. */
5630 ;
5631 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5632 /* Null pointer values are OK in C++11. */;
5633 else if (TREE_CODE (expr) != ADDR_EXPR
5634 && TREE_CODE (expr_type) != ARRAY_TYPE)
5635 {
5636 if (TREE_CODE (expr) == VAR_DECL)
5637 {
5638 error ("%qD is not a valid template argument "
5639 "because %qD is a variable, not the address of "
5640 "a variable",
5641 expr, expr);
5642 return NULL_TREE;
5643 }
5644 /* Other values, like integer constants, might be valid
5645 non-type arguments of some other type. */
5646 return error_mark_node;
5647 }
5648 else
5649 {
5650 tree decl;
5651
5652 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5653 ? TREE_OPERAND (expr, 0) : expr);
5654 if (TREE_CODE (decl) != VAR_DECL)
5655 {
5656 error ("%qE is not a valid template argument of type %qT "
5657 "because %qE is not a variable",
5658 expr, type, decl);
5659 return NULL_TREE;
5660 }
5661 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5662 {
5663 error ("%qE is not a valid template argument of type %qT "
5664 "because %qD does not have external linkage",
5665 expr, type, decl);
5666 return NULL_TREE;
5667 }
5668 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5669 {
5670 error ("%qE is not a valid template argument of type %qT "
5671 "because %qD has no linkage",
5672 expr, type, decl);
5673 return NULL_TREE;
5674 }
5675 }
5676
5677 expr = decay_conversion (expr, complain);
5678 if (expr == error_mark_node)
5679 return error_mark_node;
5680
5681 expr = perform_qualification_conversions (type, expr);
5682 if (expr == error_mark_node)
5683 return error_mark_node;
5684 }
5685 /* [temp.arg.nontype]/5, bullet 3
5686
5687 For a non-type template-parameter of type reference to object, no
5688 conversions apply. The type referred to by the reference may be more
5689 cv-qualified than the (otherwise identical) type of the
5690 template-argument. The template-parameter is bound directly to the
5691 template-argument, which must be an lvalue. */
5692 else if (TYPE_REF_OBJ_P (type))
5693 {
5694 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5695 expr_type))
5696 return error_mark_node;
5697
5698 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5699 {
5700 error ("%qE is not a valid template argument for type %qT "
5701 "because of conflicts in cv-qualification", expr, type);
5702 return NULL_TREE;
5703 }
5704
5705 if (!real_lvalue_p (expr))
5706 {
5707 error ("%qE is not a valid template argument for type %qT "
5708 "because it is not an lvalue", expr, type);
5709 return NULL_TREE;
5710 }
5711
5712 /* [temp.arg.nontype]/1
5713
5714 A template-argument for a non-type, non-template template-parameter
5715 shall be one of: [...]
5716
5717 -- the address of an object or function with external linkage. */
5718 if (TREE_CODE (expr) == INDIRECT_REF
5719 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5720 {
5721 expr = TREE_OPERAND (expr, 0);
5722 if (DECL_P (expr))
5723 {
5724 error ("%q#D is not a valid template argument for type %qT "
5725 "because a reference variable does not have a constant "
5726 "address", expr, type);
5727 return NULL_TREE;
5728 }
5729 }
5730
5731 if (!DECL_P (expr))
5732 {
5733 error ("%qE is not a valid template argument for type %qT "
5734 "because it is not an object with external linkage",
5735 expr, type);
5736 return NULL_TREE;
5737 }
5738
5739 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5740 {
5741 error ("%qE is not a valid template argument for type %qT "
5742 "because object %qD has not external linkage",
5743 expr, type, expr);
5744 return NULL_TREE;
5745 }
5746
5747 expr = build_nop (type, build_address (expr));
5748 }
5749 /* [temp.arg.nontype]/5, bullet 4
5750
5751 For a non-type template-parameter of type pointer to function, only
5752 the function-to-pointer conversion (_conv.func_) is applied. If the
5753 template-argument represents a set of overloaded functions (or a
5754 pointer to such), the matching function is selected from the set
5755 (_over.over_). */
5756 else if (TYPE_PTRFN_P (type))
5757 {
5758 /* If the argument is a template-id, we might not have enough
5759 context information to decay the pointer. */
5760 if (!type_unknown_p (expr_type))
5761 {
5762 expr = decay_conversion (expr, complain);
5763 if (expr == error_mark_node)
5764 return error_mark_node;
5765 }
5766
5767 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5768 /* Null pointer values are OK in C++11. */
5769 return perform_qualification_conversions (type, expr);
5770
5771 expr = convert_nontype_argument_function (type, expr);
5772 if (!expr || expr == error_mark_node)
5773 return expr;
5774 }
5775 /* [temp.arg.nontype]/5, bullet 5
5776
5777 For a non-type template-parameter of type reference to function, no
5778 conversions apply. If the template-argument represents a set of
5779 overloaded functions, the matching function is selected from the set
5780 (_over.over_). */
5781 else if (TYPE_REFFN_P (type))
5782 {
5783 if (TREE_CODE (expr) == ADDR_EXPR)
5784 {
5785 error ("%qE is not a valid template argument for type %qT "
5786 "because it is a pointer", expr, type);
5787 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5788 return NULL_TREE;
5789 }
5790
5791 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5792 if (!expr || expr == error_mark_node)
5793 return expr;
5794
5795 expr = build_nop (type, build_address (expr));
5796 }
5797 /* [temp.arg.nontype]/5, bullet 6
5798
5799 For a non-type template-parameter of type pointer to member function,
5800 no conversions apply. If the template-argument represents a set of
5801 overloaded member functions, the matching member function is selected
5802 from the set (_over.over_). */
5803 else if (TYPE_PTRMEMFUNC_P (type))
5804 {
5805 expr = instantiate_type (type, expr, tf_none);
5806 if (expr == error_mark_node)
5807 return error_mark_node;
5808
5809 /* [temp.arg.nontype] bullet 1 says the pointer to member
5810 expression must be a pointer-to-member constant. */
5811 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5812 return error_mark_node;
5813
5814 /* There is no way to disable standard conversions in
5815 resolve_address_of_overloaded_function (called by
5816 instantiate_type). It is possible that the call succeeded by
5817 converting &B::I to &D::I (where B is a base of D), so we need
5818 to reject this conversion here.
5819
5820 Actually, even if there was a way to disable standard conversions,
5821 it would still be better to reject them here so that we can
5822 provide a superior diagnostic. */
5823 if (!same_type_p (TREE_TYPE (expr), type))
5824 {
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because it is of type %qT", expr, type,
5827 TREE_TYPE (expr));
5828 /* If we are just one standard conversion off, explain. */
5829 if (can_convert (type, TREE_TYPE (expr), complain))
5830 inform (input_location,
5831 "standard conversions are not allowed in this context");
5832 return NULL_TREE;
5833 }
5834 }
5835 /* [temp.arg.nontype]/5, bullet 7
5836
5837 For a non-type template-parameter of type pointer to data member,
5838 qualification conversions (_conv.qual_) are applied. */
5839 else if (TYPE_PTRDATAMEM_P (type))
5840 {
5841 /* [temp.arg.nontype] bullet 1 says the pointer to member
5842 expression must be a pointer-to-member constant. */
5843 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5844 return error_mark_node;
5845
5846 expr = perform_qualification_conversions (type, expr);
5847 if (expr == error_mark_node)
5848 return expr;
5849 }
5850 else if (NULLPTR_TYPE_P (type))
5851 {
5852 if (expr != nullptr_node)
5853 {
5854 error ("%qE is not a valid template argument for type %qT "
5855 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5856 return NULL_TREE;
5857 }
5858 return expr;
5859 }
5860 /* A template non-type parameter must be one of the above. */
5861 else
5862 gcc_unreachable ();
5863
5864 /* Sanity check: did we actually convert the argument to the
5865 right type? */
5866 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5867 (type, TREE_TYPE (expr)));
5868 return expr;
5869 }
5870
5871 /* Subroutine of coerce_template_template_parms, which returns 1 if
5872 PARM_PARM and ARG_PARM match using the rule for the template
5873 parameters of template template parameters. Both PARM and ARG are
5874 template parameters; the rest of the arguments are the same as for
5875 coerce_template_template_parms.
5876 */
5877 static int
5878 coerce_template_template_parm (tree parm,
5879 tree arg,
5880 tsubst_flags_t complain,
5881 tree in_decl,
5882 tree outer_args)
5883 {
5884 if (arg == NULL_TREE || arg == error_mark_node
5885 || parm == NULL_TREE || parm == error_mark_node)
5886 return 0;
5887
5888 if (TREE_CODE (arg) != TREE_CODE (parm))
5889 return 0;
5890
5891 switch (TREE_CODE (parm))
5892 {
5893 case TEMPLATE_DECL:
5894 /* We encounter instantiations of templates like
5895 template <template <template <class> class> class TT>
5896 class C; */
5897 {
5898 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5899 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5900
5901 if (!coerce_template_template_parms
5902 (parmparm, argparm, complain, in_decl, outer_args))
5903 return 0;
5904 }
5905 /* Fall through. */
5906
5907 case TYPE_DECL:
5908 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5909 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5910 /* Argument is a parameter pack but parameter is not. */
5911 return 0;
5912 break;
5913
5914 case PARM_DECL:
5915 /* The tsubst call is used to handle cases such as
5916
5917 template <int> class C {};
5918 template <class T, template <T> class TT> class D {};
5919 D<int, C> d;
5920
5921 i.e. the parameter list of TT depends on earlier parameters. */
5922 if (!uses_template_parms (TREE_TYPE (arg))
5923 && !same_type_p
5924 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5925 TREE_TYPE (arg)))
5926 return 0;
5927
5928 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5929 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5930 /* Argument is a parameter pack but parameter is not. */
5931 return 0;
5932
5933 break;
5934
5935 default:
5936 gcc_unreachable ();
5937 }
5938
5939 return 1;
5940 }
5941
5942
5943 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5944 template template parameters. Both PARM_PARMS and ARG_PARMS are
5945 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5946 or PARM_DECL.
5947
5948 Consider the example:
5949 template <class T> class A;
5950 template<template <class U> class TT> class B;
5951
5952 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5953 the parameters to A, and OUTER_ARGS contains A. */
5954
5955 static int
5956 coerce_template_template_parms (tree parm_parms,
5957 tree arg_parms,
5958 tsubst_flags_t complain,
5959 tree in_decl,
5960 tree outer_args)
5961 {
5962 int nparms, nargs, i;
5963 tree parm, arg;
5964 int variadic_p = 0;
5965
5966 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5967 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5968
5969 nparms = TREE_VEC_LENGTH (parm_parms);
5970 nargs = TREE_VEC_LENGTH (arg_parms);
5971
5972 /* Determine whether we have a parameter pack at the end of the
5973 template template parameter's template parameter list. */
5974 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5975 {
5976 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5977
5978 if (parm == error_mark_node)
5979 return 0;
5980
5981 switch (TREE_CODE (parm))
5982 {
5983 case TEMPLATE_DECL:
5984 case TYPE_DECL:
5985 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5986 variadic_p = 1;
5987 break;
5988
5989 case PARM_DECL:
5990 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5991 variadic_p = 1;
5992 break;
5993
5994 default:
5995 gcc_unreachable ();
5996 }
5997 }
5998
5999 if (nargs != nparms
6000 && !(variadic_p && nargs >= nparms - 1))
6001 return 0;
6002
6003 /* Check all of the template parameters except the parameter pack at
6004 the end (if any). */
6005 for (i = 0; i < nparms - variadic_p; ++i)
6006 {
6007 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6008 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6009 continue;
6010
6011 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6012 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6013
6014 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6015 outer_args))
6016 return 0;
6017
6018 }
6019
6020 if (variadic_p)
6021 {
6022 /* Check each of the template parameters in the template
6023 argument against the template parameter pack at the end of
6024 the template template parameter. */
6025 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6026 return 0;
6027
6028 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6029
6030 for (; i < nargs; ++i)
6031 {
6032 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6033 continue;
6034
6035 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6036
6037 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6038 outer_args))
6039 return 0;
6040 }
6041 }
6042
6043 return 1;
6044 }
6045
6046 /* Verifies that the deduced template arguments (in TARGS) for the
6047 template template parameters (in TPARMS) represent valid bindings,
6048 by comparing the template parameter list of each template argument
6049 to the template parameter list of its corresponding template
6050 template parameter, in accordance with DR150. This
6051 routine can only be called after all template arguments have been
6052 deduced. It will return TRUE if all of the template template
6053 parameter bindings are okay, FALSE otherwise. */
6054 bool
6055 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6056 {
6057 int i, ntparms = TREE_VEC_LENGTH (tparms);
6058 bool ret = true;
6059
6060 /* We're dealing with template parms in this process. */
6061 ++processing_template_decl;
6062
6063 targs = INNERMOST_TEMPLATE_ARGS (targs);
6064
6065 for (i = 0; i < ntparms; ++i)
6066 {
6067 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6068 tree targ = TREE_VEC_ELT (targs, i);
6069
6070 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6071 {
6072 tree packed_args = NULL_TREE;
6073 int idx, len = 1;
6074
6075 if (ARGUMENT_PACK_P (targ))
6076 {
6077 /* Look inside the argument pack. */
6078 packed_args = ARGUMENT_PACK_ARGS (targ);
6079 len = TREE_VEC_LENGTH (packed_args);
6080 }
6081
6082 for (idx = 0; idx < len; ++idx)
6083 {
6084 tree targ_parms = NULL_TREE;
6085
6086 if (packed_args)
6087 /* Extract the next argument from the argument
6088 pack. */
6089 targ = TREE_VEC_ELT (packed_args, idx);
6090
6091 if (PACK_EXPANSION_P (targ))
6092 /* Look at the pattern of the pack expansion. */
6093 targ = PACK_EXPANSION_PATTERN (targ);
6094
6095 /* Extract the template parameters from the template
6096 argument. */
6097 if (TREE_CODE (targ) == TEMPLATE_DECL)
6098 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6099 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6100 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6101
6102 /* Verify that we can coerce the template template
6103 parameters from the template argument to the template
6104 parameter. This requires an exact match. */
6105 if (targ_parms
6106 && !coerce_template_template_parms
6107 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6108 targ_parms,
6109 tf_none,
6110 tparm,
6111 targs))
6112 {
6113 ret = false;
6114 goto out;
6115 }
6116 }
6117 }
6118 }
6119
6120 out:
6121
6122 --processing_template_decl;
6123 return ret;
6124 }
6125
6126 /* Since type attributes aren't mangled, we need to strip them from
6127 template type arguments. */
6128
6129 static tree
6130 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6131 {
6132 tree mv;
6133 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6134 return arg;
6135 mv = TYPE_MAIN_VARIANT (arg);
6136 arg = strip_typedefs (arg);
6137 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6138 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6139 {
6140 if (complain & tf_warning)
6141 warning (0, "ignoring attributes on template argument %qT", arg);
6142 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6143 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6144 }
6145 return arg;
6146 }
6147
6148 /* Convert the indicated template ARG as necessary to match the
6149 indicated template PARM. Returns the converted ARG, or
6150 error_mark_node if the conversion was unsuccessful. Error and
6151 warning messages are issued under control of COMPLAIN. This
6152 conversion is for the Ith parameter in the parameter list. ARGS is
6153 the full set of template arguments deduced so far. */
6154
6155 static tree
6156 convert_template_argument (tree parm,
6157 tree arg,
6158 tree args,
6159 tsubst_flags_t complain,
6160 int i,
6161 tree in_decl)
6162 {
6163 tree orig_arg;
6164 tree val;
6165 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6166
6167 if (TREE_CODE (arg) == TREE_LIST
6168 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6169 {
6170 /* The template argument was the name of some
6171 member function. That's usually
6172 invalid, but static members are OK. In any
6173 case, grab the underlying fields/functions
6174 and issue an error later if required. */
6175 orig_arg = TREE_VALUE (arg);
6176 TREE_TYPE (arg) = unknown_type_node;
6177 }
6178
6179 orig_arg = arg;
6180
6181 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6182 requires_type = (TREE_CODE (parm) == TYPE_DECL
6183 || requires_tmpl_type);
6184
6185 /* When determining whether an argument pack expansion is a template,
6186 look at the pattern. */
6187 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6188 arg = PACK_EXPANSION_PATTERN (arg);
6189
6190 /* Deal with an injected-class-name used as a template template arg. */
6191 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6192 {
6193 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6194 if (TREE_CODE (t) == TEMPLATE_DECL)
6195 {
6196 if (cxx_dialect >= cxx0x)
6197 /* OK under DR 1004. */;
6198 else if (complain & tf_warning_or_error)
6199 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6200 " used as template template argument", TYPE_NAME (arg));
6201 else if (flag_pedantic_errors)
6202 t = arg;
6203
6204 arg = t;
6205 }
6206 }
6207
6208 is_tmpl_type =
6209 ((TREE_CODE (arg) == TEMPLATE_DECL
6210 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6211 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6212 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6213 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6214
6215 if (is_tmpl_type
6216 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6217 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6218 arg = TYPE_STUB_DECL (arg);
6219
6220 is_type = TYPE_P (arg) || is_tmpl_type;
6221
6222 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6223 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6224 {
6225 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6226 {
6227 if (complain & tf_error)
6228 error ("invalid use of destructor %qE as a type", orig_arg);
6229 return error_mark_node;
6230 }
6231
6232 permerror (input_location,
6233 "to refer to a type member of a template parameter, "
6234 "use %<typename %E%>", orig_arg);
6235
6236 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6237 TREE_OPERAND (arg, 1),
6238 typename_type,
6239 complain);
6240 arg = orig_arg;
6241 is_type = 1;
6242 }
6243 if (is_type != requires_type)
6244 {
6245 if (in_decl)
6246 {
6247 if (complain & tf_error)
6248 {
6249 error ("type/value mismatch at argument %d in template "
6250 "parameter list for %qD",
6251 i + 1, in_decl);
6252 if (is_type)
6253 error (" expected a constant of type %qT, got %qT",
6254 TREE_TYPE (parm),
6255 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6256 else if (requires_tmpl_type)
6257 error (" expected a class template, got %qE", orig_arg);
6258 else
6259 error (" expected a type, got %qE", orig_arg);
6260 }
6261 }
6262 return error_mark_node;
6263 }
6264 if (is_tmpl_type ^ requires_tmpl_type)
6265 {
6266 if (in_decl && (complain & tf_error))
6267 {
6268 error ("type/value mismatch at argument %d in template "
6269 "parameter list for %qD",
6270 i + 1, in_decl);
6271 if (is_tmpl_type)
6272 error (" expected a type, got %qT", DECL_NAME (arg));
6273 else
6274 error (" expected a class template, got %qT", orig_arg);
6275 }
6276 return error_mark_node;
6277 }
6278
6279 if (is_type)
6280 {
6281 if (requires_tmpl_type)
6282 {
6283 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6284 val = orig_arg;
6285 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6286 /* The number of argument required is not known yet.
6287 Just accept it for now. */
6288 val = TREE_TYPE (arg);
6289 else
6290 {
6291 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6292 tree argparm;
6293
6294 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6295
6296 if (coerce_template_template_parms (parmparm, argparm,
6297 complain, in_decl,
6298 args))
6299 {
6300 val = arg;
6301
6302 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6303 TEMPLATE_DECL. */
6304 if (val != error_mark_node)
6305 {
6306 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6307 val = TREE_TYPE (val);
6308 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6309 val = make_pack_expansion (val);
6310 }
6311 }
6312 else
6313 {
6314 if (in_decl && (complain & tf_error))
6315 {
6316 error ("type/value mismatch at argument %d in "
6317 "template parameter list for %qD",
6318 i + 1, in_decl);
6319 error (" expected a template of type %qD, got %qT",
6320 parm, orig_arg);
6321 }
6322
6323 val = error_mark_node;
6324 }
6325 }
6326 }
6327 else
6328 val = orig_arg;
6329 /* We only form one instance of each template specialization.
6330 Therefore, if we use a non-canonical variant (i.e., a
6331 typedef), any future messages referring to the type will use
6332 the typedef, which is confusing if those future uses do not
6333 themselves also use the typedef. */
6334 if (TYPE_P (val))
6335 val = canonicalize_type_argument (val, complain);
6336 }
6337 else
6338 {
6339 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6340
6341 if (invalid_nontype_parm_type_p (t, complain))
6342 return error_mark_node;
6343
6344 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6345 {
6346 if (same_type_p (t, TREE_TYPE (orig_arg)))
6347 val = orig_arg;
6348 else
6349 {
6350 /* Not sure if this is reachable, but it doesn't hurt
6351 to be robust. */
6352 error ("type mismatch in nontype parameter pack");
6353 val = error_mark_node;
6354 }
6355 }
6356 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6357 /* We used to call digest_init here. However, digest_init
6358 will report errors, which we don't want when complain
6359 is zero. More importantly, digest_init will try too
6360 hard to convert things: for example, `0' should not be
6361 converted to pointer type at this point according to
6362 the standard. Accepting this is not merely an
6363 extension, since deciding whether or not these
6364 conversions can occur is part of determining which
6365 function template to call, or whether a given explicit
6366 argument specification is valid. */
6367 val = convert_nontype_argument (t, orig_arg, complain);
6368 else
6369 val = strip_typedefs_expr (orig_arg);
6370
6371 if (val == NULL_TREE)
6372 val = error_mark_node;
6373 else if (val == error_mark_node && (complain & tf_error))
6374 error ("could not convert template argument %qE to %qT", orig_arg, t);
6375
6376 if (TREE_CODE (val) == SCOPE_REF)
6377 {
6378 /* Strip typedefs from the SCOPE_REF. */
6379 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6380 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6381 complain);
6382 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6383 QUALIFIED_NAME_IS_TEMPLATE (val));
6384 }
6385 }
6386
6387 return val;
6388 }
6389
6390 /* Coerces the remaining template arguments in INNER_ARGS (from
6391 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6392 Returns the coerced argument pack. PARM_IDX is the position of this
6393 parameter in the template parameter list. ARGS is the original
6394 template argument list. */
6395 static tree
6396 coerce_template_parameter_pack (tree parms,
6397 int parm_idx,
6398 tree args,
6399 tree inner_args,
6400 int arg_idx,
6401 tree new_args,
6402 int* lost,
6403 tree in_decl,
6404 tsubst_flags_t complain)
6405 {
6406 tree parm = TREE_VEC_ELT (parms, parm_idx);
6407 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6408 tree packed_args;
6409 tree argument_pack;
6410 tree packed_types = NULL_TREE;
6411
6412 if (arg_idx > nargs)
6413 arg_idx = nargs;
6414
6415 packed_args = make_tree_vec (nargs - arg_idx);
6416
6417 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6418 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6419 {
6420 /* When the template parameter is a non-type template
6421 parameter pack whose type uses parameter packs, we need
6422 to look at each of the template arguments
6423 separately. Build a vector of the types for these
6424 non-type template parameters in PACKED_TYPES. */
6425 tree expansion
6426 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6427 packed_types = tsubst_pack_expansion (expansion, args,
6428 complain, in_decl);
6429
6430 if (packed_types == error_mark_node)
6431 return error_mark_node;
6432
6433 /* Check that we have the right number of arguments. */
6434 if (arg_idx < nargs
6435 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6436 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6437 {
6438 int needed_parms
6439 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6440 error ("wrong number of template arguments (%d, should be %d)",
6441 nargs, needed_parms);
6442 return error_mark_node;
6443 }
6444
6445 /* If we aren't able to check the actual arguments now
6446 (because they haven't been expanded yet), we can at least
6447 verify that all of the types used for the non-type
6448 template parameter pack are, in fact, valid for non-type
6449 template parameters. */
6450 if (arg_idx < nargs
6451 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6452 {
6453 int j, len = TREE_VEC_LENGTH (packed_types);
6454 for (j = 0; j < len; ++j)
6455 {
6456 tree t = TREE_VEC_ELT (packed_types, j);
6457 if (invalid_nontype_parm_type_p (t, complain))
6458 return error_mark_node;
6459 }
6460 }
6461 }
6462
6463 /* Convert the remaining arguments, which will be a part of the
6464 parameter pack "parm". */
6465 for (; arg_idx < nargs; ++arg_idx)
6466 {
6467 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6468 tree actual_parm = TREE_VALUE (parm);
6469
6470 if (packed_types && !PACK_EXPANSION_P (arg))
6471 {
6472 /* When we have a vector of types (corresponding to the
6473 non-type template parameter pack that uses parameter
6474 packs in its type, as mention above), and the
6475 argument is not an expansion (which expands to a
6476 currently unknown number of arguments), clone the
6477 parm and give it the next type in PACKED_TYPES. */
6478 actual_parm = copy_node (actual_parm);
6479 TREE_TYPE (actual_parm) =
6480 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6481 }
6482
6483 if (arg == error_mark_node)
6484 {
6485 if (complain & tf_error)
6486 error ("template argument %d is invalid", arg_idx + 1);
6487 }
6488 else
6489 arg = convert_template_argument (actual_parm,
6490 arg, new_args, complain, parm_idx,
6491 in_decl);
6492 if (arg == error_mark_node)
6493 (*lost)++;
6494 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6495 }
6496
6497 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6498 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6499 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6500 else
6501 {
6502 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6503 TREE_TYPE (argument_pack)
6504 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6505 TREE_CONSTANT (argument_pack) = 1;
6506 }
6507
6508 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6509 #ifdef ENABLE_CHECKING
6510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6511 TREE_VEC_LENGTH (packed_args));
6512 #endif
6513 return argument_pack;
6514 }
6515
6516 /* Returns true if the template argument vector ARGS contains
6517 any pack expansions, false otherwise. */
6518
6519 static bool
6520 any_pack_expanson_args_p (tree args)
6521 {
6522 int i;
6523 if (args)
6524 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6525 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6526 return true;
6527 return false;
6528 }
6529
6530 /* Convert all template arguments to their appropriate types, and
6531 return a vector containing the innermost resulting template
6532 arguments. If any error occurs, return error_mark_node. Error and
6533 warning messages are issued under control of COMPLAIN.
6534
6535 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6536 for arguments not specified in ARGS. Otherwise, if
6537 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6538 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6539 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6540 ARGS. */
6541
6542 static tree
6543 coerce_template_parms (tree parms,
6544 tree args,
6545 tree in_decl,
6546 tsubst_flags_t complain,
6547 bool require_all_args,
6548 bool use_default_args)
6549 {
6550 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6551 tree inner_args;
6552 tree new_args;
6553 tree new_inner_args;
6554 int saved_unevaluated_operand;
6555 int saved_inhibit_evaluation_warnings;
6556
6557 /* When used as a boolean value, indicates whether this is a
6558 variadic template parameter list. Since it's an int, we can also
6559 subtract it from nparms to get the number of non-variadic
6560 parameters. */
6561 int variadic_p = 0;
6562 int post_variadic_parms = 0;
6563
6564 if (args == error_mark_node)
6565 return error_mark_node;
6566
6567 nparms = TREE_VEC_LENGTH (parms);
6568
6569 /* Determine if there are any parameter packs. */
6570 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6571 {
6572 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6573 if (variadic_p)
6574 ++post_variadic_parms;
6575 if (template_parameter_pack_p (tparm))
6576 ++variadic_p;
6577 }
6578
6579 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6580 /* If there are no parameters that follow a parameter pack, we need to
6581 expand any argument packs so that we can deduce a parameter pack from
6582 some non-packed args followed by an argument pack, as in variadic85.C.
6583 If there are such parameters, we need to leave argument packs intact
6584 so the arguments are assigned properly. This can happen when dealing
6585 with a nested class inside a partial specialization of a class
6586 template, as in variadic92.C, or when deducing a template parameter pack
6587 from a sub-declarator, as in variadic114.C. */
6588 if (!post_variadic_parms)
6589 inner_args = expand_template_argument_pack (inner_args);
6590
6591 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6592 if ((nargs > nparms && !variadic_p)
6593 || (nargs < nparms - variadic_p
6594 && require_all_args
6595 && !any_pack_expanson_args_p (inner_args)
6596 && (!use_default_args
6597 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6598 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6599 {
6600 if (complain & tf_error)
6601 {
6602 if (variadic_p)
6603 {
6604 nparms -= variadic_p;
6605 error ("wrong number of template arguments "
6606 "(%d, should be %d or more)", nargs, nparms);
6607 }
6608 else
6609 error ("wrong number of template arguments "
6610 "(%d, should be %d)", nargs, nparms);
6611
6612 if (in_decl)
6613 error ("provided for %q+D", in_decl);
6614 }
6615
6616 return error_mark_node;
6617 }
6618
6619 /* We need to evaluate the template arguments, even though this
6620 template-id may be nested within a "sizeof". */
6621 saved_unevaluated_operand = cp_unevaluated_operand;
6622 cp_unevaluated_operand = 0;
6623 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6624 c_inhibit_evaluation_warnings = 0;
6625 new_inner_args = make_tree_vec (nparms);
6626 new_args = add_outermost_template_args (args, new_inner_args);
6627 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6628 {
6629 tree arg;
6630 tree parm;
6631
6632 /* Get the Ith template parameter. */
6633 parm = TREE_VEC_ELT (parms, parm_idx);
6634
6635 if (parm == error_mark_node)
6636 {
6637 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6638 continue;
6639 }
6640
6641 /* Calculate the next argument. */
6642 if (arg_idx < nargs)
6643 arg = TREE_VEC_ELT (inner_args, arg_idx);
6644 else
6645 arg = NULL_TREE;
6646
6647 if (template_parameter_pack_p (TREE_VALUE (parm))
6648 && !(arg && ARGUMENT_PACK_P (arg)))
6649 {
6650 /* All remaining arguments will be placed in the
6651 template parameter pack PARM. */
6652 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6653 inner_args, arg_idx,
6654 new_args, &lost,
6655 in_decl, complain);
6656
6657 /* Store this argument. */
6658 if (arg == error_mark_node)
6659 lost++;
6660 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6661
6662 /* We are done with all of the arguments. */
6663 arg_idx = nargs;
6664
6665 continue;
6666 }
6667 else if (arg)
6668 {
6669 if (PACK_EXPANSION_P (arg))
6670 {
6671 /* We don't know how many args we have yet, just
6672 use the unconverted ones for now. */
6673 new_inner_args = inner_args;
6674 break;
6675 }
6676 }
6677 else if (require_all_args)
6678 {
6679 /* There must be a default arg in this case. */
6680 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6681 complain, in_decl);
6682 /* The position of the first default template argument,
6683 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6684 Record that. */
6685 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6687 }
6688 else
6689 break;
6690
6691 if (arg == error_mark_node)
6692 {
6693 if (complain & tf_error)
6694 error ("template argument %d is invalid", arg_idx + 1);
6695 }
6696 else if (!arg)
6697 /* This only occurs if there was an error in the template
6698 parameter list itself (which we would already have
6699 reported) that we are trying to recover from, e.g., a class
6700 template with a parameter list such as
6701 template<typename..., typename>. */
6702 ++lost;
6703 else
6704 arg = convert_template_argument (TREE_VALUE (parm),
6705 arg, new_args, complain,
6706 parm_idx, in_decl);
6707
6708 if (arg == error_mark_node)
6709 lost++;
6710 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6711 }
6712 cp_unevaluated_operand = saved_unevaluated_operand;
6713 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6714
6715 if (lost)
6716 return error_mark_node;
6717
6718 #ifdef ENABLE_CHECKING
6719 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6720 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6721 TREE_VEC_LENGTH (new_inner_args));
6722 #endif
6723
6724 return new_inner_args;
6725 }
6726
6727 /* Like coerce_template_parms. If PARMS represents all template
6728 parameters levels, this function returns a vector of vectors
6729 representing all the resulting argument levels. Note that in this
6730 case, only the innermost arguments are coerced because the
6731 outermost ones are supposed to have been coerced already.
6732
6733 Otherwise, if PARMS represents only (the innermost) vector of
6734 parameters, this function returns a vector containing just the
6735 innermost resulting arguments. */
6736
6737 static tree
6738 coerce_innermost_template_parms (tree parms,
6739 tree args,
6740 tree in_decl,
6741 tsubst_flags_t complain,
6742 bool require_all_args,
6743 bool use_default_args)
6744 {
6745 int parms_depth = TMPL_PARMS_DEPTH (parms);
6746 int args_depth = TMPL_ARGS_DEPTH (args);
6747 tree coerced_args;
6748
6749 if (parms_depth > 1)
6750 {
6751 coerced_args = make_tree_vec (parms_depth);
6752 tree level;
6753 int cur_depth;
6754
6755 for (level = parms, cur_depth = parms_depth;
6756 parms_depth > 0 && level != NULL_TREE;
6757 level = TREE_CHAIN (level), --cur_depth)
6758 {
6759 tree l;
6760 if (cur_depth == args_depth)
6761 l = coerce_template_parms (TREE_VALUE (level),
6762 args, in_decl, complain,
6763 require_all_args,
6764 use_default_args);
6765 else
6766 l = TMPL_ARGS_LEVEL (args, cur_depth);
6767
6768 if (l == error_mark_node)
6769 return error_mark_node;
6770
6771 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6772 }
6773 }
6774 else
6775 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6776 args, in_decl, complain,
6777 require_all_args,
6778 use_default_args);
6779 return coerced_args;
6780 }
6781
6782 /* Returns 1 if template args OT and NT are equivalent. */
6783
6784 static int
6785 template_args_equal (tree ot, tree nt)
6786 {
6787 if (nt == ot)
6788 return 1;
6789 if (nt == NULL_TREE || ot == NULL_TREE)
6790 return false;
6791
6792 if (TREE_CODE (nt) == TREE_VEC)
6793 /* For member templates */
6794 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6795 else if (PACK_EXPANSION_P (ot))
6796 return (PACK_EXPANSION_P (nt)
6797 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6798 PACK_EXPANSION_PATTERN (nt))
6799 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6800 PACK_EXPANSION_EXTRA_ARGS (nt)));
6801 else if (ARGUMENT_PACK_P (ot))
6802 {
6803 int i, len;
6804 tree opack, npack;
6805
6806 if (!ARGUMENT_PACK_P (nt))
6807 return 0;
6808
6809 opack = ARGUMENT_PACK_ARGS (ot);
6810 npack = ARGUMENT_PACK_ARGS (nt);
6811 len = TREE_VEC_LENGTH (opack);
6812 if (TREE_VEC_LENGTH (npack) != len)
6813 return 0;
6814 for (i = 0; i < len; ++i)
6815 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6816 TREE_VEC_ELT (npack, i)))
6817 return 0;
6818 return 1;
6819 }
6820 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6821 {
6822 /* We get here probably because we are in the middle of substituting
6823 into the pattern of a pack expansion. In that case the
6824 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6825 interested in. So we want to use the initial pack argument for
6826 the comparison. */
6827 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6828 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6829 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6830 return template_args_equal (ot, nt);
6831 }
6832 else if (TYPE_P (nt))
6833 return TYPE_P (ot) && same_type_p (ot, nt);
6834 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6835 return 0;
6836 else
6837 return cp_tree_equal (ot, nt);
6838 }
6839
6840 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6841 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6842 NEWARG_PTR with the offending arguments if they are non-NULL. */
6843
6844 static int
6845 comp_template_args_with_info (tree oldargs, tree newargs,
6846 tree *oldarg_ptr, tree *newarg_ptr)
6847 {
6848 int i;
6849
6850 if (oldargs == newargs)
6851 return 1;
6852
6853 if (!oldargs || !newargs)
6854 return 0;
6855
6856 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6857 return 0;
6858
6859 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6860 {
6861 tree nt = TREE_VEC_ELT (newargs, i);
6862 tree ot = TREE_VEC_ELT (oldargs, i);
6863
6864 if (! template_args_equal (ot, nt))
6865 {
6866 if (oldarg_ptr != NULL)
6867 *oldarg_ptr = ot;
6868 if (newarg_ptr != NULL)
6869 *newarg_ptr = nt;
6870 return 0;
6871 }
6872 }
6873 return 1;
6874 }
6875
6876 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6877 of template arguments. Returns 0 otherwise. */
6878
6879 int
6880 comp_template_args (tree oldargs, tree newargs)
6881 {
6882 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6883 }
6884
6885 static void
6886 add_pending_template (tree d)
6887 {
6888 tree ti = (TYPE_P (d)
6889 ? CLASSTYPE_TEMPLATE_INFO (d)
6890 : DECL_TEMPLATE_INFO (d));
6891 struct pending_template *pt;
6892 int level;
6893
6894 if (TI_PENDING_TEMPLATE_FLAG (ti))
6895 return;
6896
6897 /* We are called both from instantiate_decl, where we've already had a
6898 tinst_level pushed, and instantiate_template, where we haven't.
6899 Compensate. */
6900 level = !current_tinst_level || current_tinst_level->decl != d;
6901
6902 if (level)
6903 push_tinst_level (d);
6904
6905 pt = ggc_alloc_pending_template ();
6906 pt->next = NULL;
6907 pt->tinst = current_tinst_level;
6908 if (last_pending_template)
6909 last_pending_template->next = pt;
6910 else
6911 pending_templates = pt;
6912
6913 last_pending_template = pt;
6914
6915 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6916
6917 if (level)
6918 pop_tinst_level ();
6919 }
6920
6921
6922 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6923 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6924 documentation for TEMPLATE_ID_EXPR. */
6925
6926 tree
6927 lookup_template_function (tree fns, tree arglist)
6928 {
6929 tree type;
6930
6931 if (fns == error_mark_node || arglist == error_mark_node)
6932 return error_mark_node;
6933
6934 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6935
6936 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6937 {
6938 error ("%q#D is not a function template", fns);
6939 return error_mark_node;
6940 }
6941
6942 if (BASELINK_P (fns))
6943 {
6944 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6945 unknown_type_node,
6946 BASELINK_FUNCTIONS (fns),
6947 arglist);
6948 return fns;
6949 }
6950
6951 type = TREE_TYPE (fns);
6952 if (TREE_CODE (fns) == OVERLOAD || !type)
6953 type = unknown_type_node;
6954
6955 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6956 }
6957
6958 /* Within the scope of a template class S<T>, the name S gets bound
6959 (in build_self_reference) to a TYPE_DECL for the class, not a
6960 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6961 or one of its enclosing classes, and that type is a template,
6962 return the associated TEMPLATE_DECL. Otherwise, the original
6963 DECL is returned.
6964
6965 Also handle the case when DECL is a TREE_LIST of ambiguous
6966 injected-class-names from different bases. */
6967
6968 tree
6969 maybe_get_template_decl_from_type_decl (tree decl)
6970 {
6971 if (decl == NULL_TREE)
6972 return decl;
6973
6974 /* DR 176: A lookup that finds an injected-class-name (10.2
6975 [class.member.lookup]) can result in an ambiguity in certain cases
6976 (for example, if it is found in more than one base class). If all of
6977 the injected-class-names that are found refer to specializations of
6978 the same class template, and if the name is followed by a
6979 template-argument-list, the reference refers to the class template
6980 itself and not a specialization thereof, and is not ambiguous. */
6981 if (TREE_CODE (decl) == TREE_LIST)
6982 {
6983 tree t, tmpl = NULL_TREE;
6984 for (t = decl; t; t = TREE_CHAIN (t))
6985 {
6986 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6987 if (!tmpl)
6988 tmpl = elt;
6989 else if (tmpl != elt)
6990 break;
6991 }
6992 if (tmpl && t == NULL_TREE)
6993 return tmpl;
6994 else
6995 return decl;
6996 }
6997
6998 return (decl != NULL_TREE
6999 && DECL_SELF_REFERENCE_P (decl)
7000 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7001 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7002 }
7003
7004 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7005 parameters, find the desired type.
7006
7007 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7008
7009 IN_DECL, if non-NULL, is the template declaration we are trying to
7010 instantiate.
7011
7012 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7013 the class we are looking up.
7014
7015 Issue error and warning messages under control of COMPLAIN.
7016
7017 If the template class is really a local class in a template
7018 function, then the FUNCTION_CONTEXT is the function in which it is
7019 being instantiated.
7020
7021 ??? Note that this function is currently called *twice* for each
7022 template-id: the first time from the parser, while creating the
7023 incomplete type (finish_template_type), and the second type during the
7024 real instantiation (instantiate_template_class). This is surely something
7025 that we want to avoid. It also causes some problems with argument
7026 coercion (see convert_nontype_argument for more information on this). */
7027
7028 static tree
7029 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7030 int entering_scope, tsubst_flags_t complain)
7031 {
7032 tree templ = NULL_TREE, parmlist;
7033 tree t;
7034 void **slot;
7035 spec_entry *entry;
7036 spec_entry elt;
7037 hashval_t hash;
7038
7039 if (TREE_CODE (d1) == IDENTIFIER_NODE)
7040 {
7041 tree value = innermost_non_namespace_value (d1);
7042 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7043 templ = value;
7044 else
7045 {
7046 if (context)
7047 push_decl_namespace (context);
7048 templ = lookup_name (d1);
7049 templ = maybe_get_template_decl_from_type_decl (templ);
7050 if (context)
7051 pop_decl_namespace ();
7052 }
7053 if (templ)
7054 context = DECL_CONTEXT (templ);
7055 }
7056 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7057 {
7058 tree type = TREE_TYPE (d1);
7059
7060 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7061 an implicit typename for the second A. Deal with it. */
7062 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7063 type = TREE_TYPE (type);
7064
7065 if (CLASSTYPE_TEMPLATE_INFO (type))
7066 {
7067 templ = CLASSTYPE_TI_TEMPLATE (type);
7068 d1 = DECL_NAME (templ);
7069 }
7070 }
7071 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7072 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7073 {
7074 templ = TYPE_TI_TEMPLATE (d1);
7075 d1 = DECL_NAME (templ);
7076 }
7077 else if (TREE_CODE (d1) == TEMPLATE_DECL
7078 && DECL_TEMPLATE_RESULT (d1)
7079 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7080 {
7081 templ = d1;
7082 d1 = DECL_NAME (templ);
7083 context = DECL_CONTEXT (templ);
7084 }
7085
7086 /* Issue an error message if we didn't find a template. */
7087 if (! templ)
7088 {
7089 if (complain & tf_error)
7090 error ("%qT is not a template", d1);
7091 return error_mark_node;
7092 }
7093
7094 if (TREE_CODE (templ) != TEMPLATE_DECL
7095 /* Make sure it's a user visible template, if it was named by
7096 the user. */
7097 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7098 && !PRIMARY_TEMPLATE_P (templ)))
7099 {
7100 if (complain & tf_error)
7101 {
7102 error ("non-template type %qT used as a template", d1);
7103 if (in_decl)
7104 error ("for template declaration %q+D", in_decl);
7105 }
7106 return error_mark_node;
7107 }
7108
7109 complain &= ~tf_user;
7110
7111 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7112 {
7113 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7114 template arguments */
7115
7116 tree parm;
7117 tree arglist2;
7118 tree outer;
7119
7120 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7121
7122 /* Consider an example where a template template parameter declared as
7123
7124 template <class T, class U = std::allocator<T> > class TT
7125
7126 The template parameter level of T and U are one level larger than
7127 of TT. To proper process the default argument of U, say when an
7128 instantiation `TT<int>' is seen, we need to build the full
7129 arguments containing {int} as the innermost level. Outer levels,
7130 available when not appearing as default template argument, can be
7131 obtained from the arguments of the enclosing template.
7132
7133 Suppose that TT is later substituted with std::vector. The above
7134 instantiation is `TT<int, std::allocator<T> >' with TT at
7135 level 1, and T at level 2, while the template arguments at level 1
7136 becomes {std::vector} and the inner level 2 is {int}. */
7137
7138 outer = DECL_CONTEXT (templ);
7139 if (outer)
7140 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7141 else if (current_template_parms)
7142 /* This is an argument of the current template, so we haven't set
7143 DECL_CONTEXT yet. */
7144 outer = current_template_args ();
7145
7146 if (outer)
7147 arglist = add_to_template_args (outer, arglist);
7148
7149 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7150 complain,
7151 /*require_all_args=*/true,
7152 /*use_default_args=*/true);
7153 if (arglist2 == error_mark_node
7154 || (!uses_template_parms (arglist2)
7155 && check_instantiated_args (templ, arglist2, complain)))
7156 return error_mark_node;
7157
7158 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7159 return parm;
7160 }
7161 else
7162 {
7163 tree template_type = TREE_TYPE (templ);
7164 tree gen_tmpl;
7165 tree type_decl;
7166 tree found = NULL_TREE;
7167 int arg_depth;
7168 int parm_depth;
7169 int is_dependent_type;
7170 int use_partial_inst_tmpl = false;
7171
7172 if (template_type == error_mark_node)
7173 /* An error occured while building the template TEMPL, and a
7174 diagnostic has most certainly been emitted for that
7175 already. Let's propagate that error. */
7176 return error_mark_node;
7177
7178 gen_tmpl = most_general_template (templ);
7179 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7180 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7181 arg_depth = TMPL_ARGS_DEPTH (arglist);
7182
7183 if (arg_depth == 1 && parm_depth > 1)
7184 {
7185 /* We've been given an incomplete set of template arguments.
7186 For example, given:
7187
7188 template <class T> struct S1 {
7189 template <class U> struct S2 {};
7190 template <class U> struct S2<U*> {};
7191 };
7192
7193 we will be called with an ARGLIST of `U*', but the
7194 TEMPLATE will be `template <class T> template
7195 <class U> struct S1<T>::S2'. We must fill in the missing
7196 arguments. */
7197 arglist
7198 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7199 arglist);
7200 arg_depth = TMPL_ARGS_DEPTH (arglist);
7201 }
7202
7203 /* Now we should have enough arguments. */
7204 gcc_assert (parm_depth == arg_depth);
7205
7206 /* From here on, we're only interested in the most general
7207 template. */
7208
7209 /* Calculate the BOUND_ARGS. These will be the args that are
7210 actually tsubst'd into the definition to create the
7211 instantiation. */
7212 if (parm_depth > 1)
7213 {
7214 /* We have multiple levels of arguments to coerce, at once. */
7215 int i;
7216 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7217
7218 tree bound_args = make_tree_vec (parm_depth);
7219
7220 for (i = saved_depth,
7221 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7222 i > 0 && t != NULL_TREE;
7223 --i, t = TREE_CHAIN (t))
7224 {
7225 tree a;
7226 if (i == saved_depth)
7227 a = coerce_template_parms (TREE_VALUE (t),
7228 arglist, gen_tmpl,
7229 complain,
7230 /*require_all_args=*/true,
7231 /*use_default_args=*/true);
7232 else
7233 /* Outer levels should have already been coerced. */
7234 a = TMPL_ARGS_LEVEL (arglist, i);
7235
7236 /* Don't process further if one of the levels fails. */
7237 if (a == error_mark_node)
7238 {
7239 /* Restore the ARGLIST to its full size. */
7240 TREE_VEC_LENGTH (arglist) = saved_depth;
7241 return error_mark_node;
7242 }
7243
7244 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7245
7246 /* We temporarily reduce the length of the ARGLIST so
7247 that coerce_template_parms will see only the arguments
7248 corresponding to the template parameters it is
7249 examining. */
7250 TREE_VEC_LENGTH (arglist)--;
7251 }
7252
7253 /* Restore the ARGLIST to its full size. */
7254 TREE_VEC_LENGTH (arglist) = saved_depth;
7255
7256 arglist = bound_args;
7257 }
7258 else
7259 arglist
7260 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7261 INNERMOST_TEMPLATE_ARGS (arglist),
7262 gen_tmpl,
7263 complain,
7264 /*require_all_args=*/true,
7265 /*use_default_args=*/true);
7266
7267 if (arglist == error_mark_node)
7268 /* We were unable to bind the arguments. */
7269 return error_mark_node;
7270
7271 /* In the scope of a template class, explicit references to the
7272 template class refer to the type of the template, not any
7273 instantiation of it. For example, in:
7274
7275 template <class T> class C { void f(C<T>); }
7276
7277 the `C<T>' is just the same as `C'. Outside of the
7278 class, however, such a reference is an instantiation. */
7279 if ((entering_scope
7280 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7281 || currently_open_class (template_type))
7282 /* comp_template_args is expensive, check it last. */
7283 && comp_template_args (TYPE_TI_ARGS (template_type),
7284 arglist))
7285 return template_type;
7286
7287 /* If we already have this specialization, return it. */
7288 elt.tmpl = gen_tmpl;
7289 elt.args = arglist;
7290 hash = hash_specialization (&elt);
7291 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7292 &elt, hash);
7293
7294 if (entry)
7295 return entry->spec;
7296
7297 is_dependent_type = uses_template_parms (arglist);
7298
7299 /* If the deduced arguments are invalid, then the binding
7300 failed. */
7301 if (!is_dependent_type
7302 && check_instantiated_args (gen_tmpl,
7303 INNERMOST_TEMPLATE_ARGS (arglist),
7304 complain))
7305 return error_mark_node;
7306
7307 if (!is_dependent_type
7308 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7309 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7310 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7311 {
7312 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7313 DECL_NAME (gen_tmpl),
7314 /*tag_scope=*/ts_global);
7315 return found;
7316 }
7317
7318 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7319 complain, in_decl);
7320 if (context == error_mark_node)
7321 return error_mark_node;
7322
7323 if (!context)
7324 context = global_namespace;
7325
7326 /* Create the type. */
7327 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7328 {
7329 if (!is_dependent_type)
7330 {
7331 set_current_access_from_decl (TYPE_NAME (template_type));
7332 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7333 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7334 arglist, complain, in_decl),
7335 SCOPED_ENUM_P (template_type), NULL);
7336 }
7337 else
7338 {
7339 /* We don't want to call start_enum for this type, since
7340 the values for the enumeration constants may involve
7341 template parameters. And, no one should be interested
7342 in the enumeration constants for such a type. */
7343 t = cxx_make_type (ENUMERAL_TYPE);
7344 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7345 }
7346 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7347 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7348 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7349 }
7350 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7351 {
7352 /* The user referred to a specialization of an alias
7353 template represented by GEN_TMPL.
7354
7355 [temp.alias]/2 says:
7356
7357 When a template-id refers to the specialization of an
7358 alias template, it is equivalent to the associated
7359 type obtained by substitution of its
7360 template-arguments for the template-parameters in the
7361 type-id of the alias template. */
7362
7363 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7364 /* Note that the call above (by indirectly calling
7365 register_specialization in tsubst_decl) registers the
7366 TYPE_DECL representing the specialization of the alias
7367 template. So next time someone substitutes ARGLIST for
7368 the template parms into the alias template (GEN_TMPL),
7369 she'll get that TYPE_DECL back. */
7370
7371 if (t == error_mark_node)
7372 return t;
7373 }
7374 else if (CLASS_TYPE_P (template_type))
7375 {
7376 t = make_class_type (TREE_CODE (template_type));
7377 CLASSTYPE_DECLARED_CLASS (t)
7378 = CLASSTYPE_DECLARED_CLASS (template_type);
7379 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7380 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7381
7382 /* A local class. Make sure the decl gets registered properly. */
7383 if (context == current_function_decl)
7384 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7385
7386 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7387 /* This instantiation is another name for the primary
7388 template type. Set the TYPE_CANONICAL field
7389 appropriately. */
7390 TYPE_CANONICAL (t) = template_type;
7391 else if (any_template_arguments_need_structural_equality_p (arglist))
7392 /* Some of the template arguments require structural
7393 equality testing, so this template class requires
7394 structural equality testing. */
7395 SET_TYPE_STRUCTURAL_EQUALITY (t);
7396 }
7397 else
7398 gcc_unreachable ();
7399
7400 /* If we called start_enum or pushtag above, this information
7401 will already be set up. */
7402 if (!TYPE_NAME (t))
7403 {
7404 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7405
7406 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7407 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7408 DECL_SOURCE_LOCATION (type_decl)
7409 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7410 }
7411 else
7412 type_decl = TYPE_NAME (t);
7413
7414 if (CLASS_TYPE_P (template_type))
7415 {
7416 TREE_PRIVATE (type_decl)
7417 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7418 TREE_PROTECTED (type_decl)
7419 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7420 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7421 {
7422 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7423 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7424 }
7425 }
7426
7427 /* Let's consider the explicit specialization of a member
7428 of a class template specialization that is implicitely instantiated,
7429 e.g.:
7430 template<class T>
7431 struct S
7432 {
7433 template<class U> struct M {}; //#0
7434 };
7435
7436 template<>
7437 template<>
7438 struct S<int>::M<char> //#1
7439 {
7440 int i;
7441 };
7442 [temp.expl.spec]/4 says this is valid.
7443
7444 In this case, when we write:
7445 S<int>::M<char> m;
7446
7447 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7448 the one of #0.
7449
7450 When we encounter #1, we want to store the partial instantiation
7451 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7452
7453 For all cases other than this "explicit specialization of member of a
7454 class template", we just want to store the most general template into
7455 the CLASSTYPE_TI_TEMPLATE of M.
7456
7457 This case of "explicit specialization of member of a class template"
7458 only happens when:
7459 1/ the enclosing class is an instantiation of, and therefore not
7460 the same as, the context of the most general template, and
7461 2/ we aren't looking at the partial instantiation itself, i.e.
7462 the innermost arguments are not the same as the innermost parms of
7463 the most general template.
7464
7465 So it's only when 1/ and 2/ happens that we want to use the partial
7466 instantiation of the member template in lieu of its most general
7467 template. */
7468
7469 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7470 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7471 /* the enclosing class must be an instantiation... */
7472 && CLASS_TYPE_P (context)
7473 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7474 {
7475 tree partial_inst_args;
7476 TREE_VEC_LENGTH (arglist)--;
7477 ++processing_template_decl;
7478 partial_inst_args =
7479 tsubst (INNERMOST_TEMPLATE_ARGS
7480 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7481 arglist, complain, NULL_TREE);
7482 --processing_template_decl;
7483 TREE_VEC_LENGTH (arglist)++;
7484 use_partial_inst_tmpl =
7485 /*...and we must not be looking at the partial instantiation
7486 itself. */
7487 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7488 partial_inst_args);
7489 }
7490
7491 if (!use_partial_inst_tmpl)
7492 /* This case is easy; there are no member templates involved. */
7493 found = gen_tmpl;
7494 else
7495 {
7496 /* This is a full instantiation of a member template. Find
7497 the partial instantiation of which this is an instance. */
7498
7499 /* Temporarily reduce by one the number of levels in the ARGLIST
7500 so as to avoid comparing the last set of arguments. */
7501 TREE_VEC_LENGTH (arglist)--;
7502 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7503 TREE_VEC_LENGTH (arglist)++;
7504 /* FOUND is either a proper class type, or an alias
7505 template specialization. In the later case, it's a
7506 TYPE_DECL, resulting from the substituting of arguments
7507 for parameters in the TYPE_DECL of the alias template
7508 done earlier. So be careful while getting the template
7509 of FOUND. */
7510 found = TREE_CODE (found) == TYPE_DECL
7511 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7512 : CLASSTYPE_TI_TEMPLATE (found);
7513 }
7514
7515 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7516
7517 elt.spec = t;
7518 slot = htab_find_slot_with_hash (type_specializations,
7519 &elt, hash, INSERT);
7520 entry = ggc_alloc_spec_entry ();
7521 *entry = elt;
7522 *slot = entry;
7523
7524 /* Note this use of the partial instantiation so we can check it
7525 later in maybe_process_partial_specialization. */
7526 DECL_TEMPLATE_INSTANTIATIONS (templ)
7527 = tree_cons (arglist, t,
7528 DECL_TEMPLATE_INSTANTIATIONS (templ));
7529
7530 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7531 /* Now that the type has been registered on the instantiations
7532 list, we set up the enumerators. Because the enumeration
7533 constants may involve the enumeration type itself, we make
7534 sure to register the type first, and then create the
7535 constants. That way, doing tsubst_expr for the enumeration
7536 constants won't result in recursive calls here; we'll find
7537 the instantiation and exit above. */
7538 tsubst_enum (template_type, t, arglist);
7539
7540 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7541 /* If the type makes use of template parameters, the
7542 code that generates debugging information will crash. */
7543 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7544
7545 /* Possibly limit visibility based on template args. */
7546 TREE_PUBLIC (type_decl) = 1;
7547 determine_visibility (type_decl);
7548
7549 return t;
7550 }
7551 }
7552
7553 /* Wrapper for lookup_template_class_1. */
7554
7555 tree
7556 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7557 int entering_scope, tsubst_flags_t complain)
7558 {
7559 tree ret;
7560 timevar_push (TV_TEMPLATE_INST);
7561 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7562 entering_scope, complain);
7563 timevar_pop (TV_TEMPLATE_INST);
7564 return ret;
7565 }
7566 \f
7567 struct pair_fn_data
7568 {
7569 tree_fn_t fn;
7570 void *data;
7571 /* True when we should also visit template parameters that occur in
7572 non-deduced contexts. */
7573 bool include_nondeduced_p;
7574 struct pointer_set_t *visited;
7575 };
7576
7577 /* Called from for_each_template_parm via walk_tree. */
7578
7579 static tree
7580 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7581 {
7582 tree t = *tp;
7583 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7584 tree_fn_t fn = pfd->fn;
7585 void *data = pfd->data;
7586
7587 if (TYPE_P (t)
7588 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7589 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7590 pfd->include_nondeduced_p))
7591 return error_mark_node;
7592
7593 switch (TREE_CODE (t))
7594 {
7595 case RECORD_TYPE:
7596 if (TYPE_PTRMEMFUNC_P (t))
7597 break;
7598 /* Fall through. */
7599
7600 case UNION_TYPE:
7601 case ENUMERAL_TYPE:
7602 if (!TYPE_TEMPLATE_INFO (t))
7603 *walk_subtrees = 0;
7604 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7605 fn, data, pfd->visited,
7606 pfd->include_nondeduced_p))
7607 return error_mark_node;
7608 break;
7609
7610 case INTEGER_TYPE:
7611 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7612 fn, data, pfd->visited,
7613 pfd->include_nondeduced_p)
7614 || for_each_template_parm (TYPE_MAX_VALUE (t),
7615 fn, data, pfd->visited,
7616 pfd->include_nondeduced_p))
7617 return error_mark_node;
7618 break;
7619
7620 case METHOD_TYPE:
7621 /* Since we're not going to walk subtrees, we have to do this
7622 explicitly here. */
7623 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7624 pfd->visited, pfd->include_nondeduced_p))
7625 return error_mark_node;
7626 /* Fall through. */
7627
7628 case FUNCTION_TYPE:
7629 /* Check the return type. */
7630 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7631 pfd->include_nondeduced_p))
7632 return error_mark_node;
7633
7634 /* Check the parameter types. Since default arguments are not
7635 instantiated until they are needed, the TYPE_ARG_TYPES may
7636 contain expressions that involve template parameters. But,
7637 no-one should be looking at them yet. And, once they're
7638 instantiated, they don't contain template parameters, so
7639 there's no point in looking at them then, either. */
7640 {
7641 tree parm;
7642
7643 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7644 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7645 pfd->visited, pfd->include_nondeduced_p))
7646 return error_mark_node;
7647
7648 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7649 want walk_tree walking into them itself. */
7650 *walk_subtrees = 0;
7651 }
7652 break;
7653
7654 case TYPEOF_TYPE:
7655 case UNDERLYING_TYPE:
7656 if (pfd->include_nondeduced_p
7657 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7658 pfd->visited,
7659 pfd->include_nondeduced_p))
7660 return error_mark_node;
7661 break;
7662
7663 case FUNCTION_DECL:
7664 case VAR_DECL:
7665 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7666 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7667 pfd->visited, pfd->include_nondeduced_p))
7668 return error_mark_node;
7669 /* Fall through. */
7670
7671 case PARM_DECL:
7672 case CONST_DECL:
7673 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7674 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7675 pfd->visited, pfd->include_nondeduced_p))
7676 return error_mark_node;
7677 if (DECL_CONTEXT (t)
7678 && pfd->include_nondeduced_p
7679 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7680 pfd->visited, pfd->include_nondeduced_p))
7681 return error_mark_node;
7682 break;
7683
7684 case BOUND_TEMPLATE_TEMPLATE_PARM:
7685 /* Record template parameters such as `T' inside `TT<T>'. */
7686 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7687 pfd->include_nondeduced_p))
7688 return error_mark_node;
7689 /* Fall through. */
7690
7691 case TEMPLATE_TEMPLATE_PARM:
7692 case TEMPLATE_TYPE_PARM:
7693 case TEMPLATE_PARM_INDEX:
7694 if (fn && (*fn)(t, data))
7695 return error_mark_node;
7696 else if (!fn)
7697 return error_mark_node;
7698 break;
7699
7700 case TEMPLATE_DECL:
7701 /* A template template parameter is encountered. */
7702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7703 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7704 pfd->include_nondeduced_p))
7705 return error_mark_node;
7706
7707 /* Already substituted template template parameter */
7708 *walk_subtrees = 0;
7709 break;
7710
7711 case TYPENAME_TYPE:
7712 if (!fn
7713 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7714 data, pfd->visited,
7715 pfd->include_nondeduced_p))
7716 return error_mark_node;
7717 break;
7718
7719 case CONSTRUCTOR:
7720 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7721 && pfd->include_nondeduced_p
7722 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7723 (TREE_TYPE (t)), fn, data,
7724 pfd->visited, pfd->include_nondeduced_p))
7725 return error_mark_node;
7726 break;
7727
7728 case INDIRECT_REF:
7729 case COMPONENT_REF:
7730 /* If there's no type, then this thing must be some expression
7731 involving template parameters. */
7732 if (!fn && !TREE_TYPE (t))
7733 return error_mark_node;
7734 break;
7735
7736 case MODOP_EXPR:
7737 case CAST_EXPR:
7738 case IMPLICIT_CONV_EXPR:
7739 case REINTERPRET_CAST_EXPR:
7740 case CONST_CAST_EXPR:
7741 case STATIC_CAST_EXPR:
7742 case DYNAMIC_CAST_EXPR:
7743 case ARROW_EXPR:
7744 case DOTSTAR_EXPR:
7745 case TYPEID_EXPR:
7746 case PSEUDO_DTOR_EXPR:
7747 if (!fn)
7748 return error_mark_node;
7749 break;
7750
7751 default:
7752 break;
7753 }
7754
7755 /* We didn't find any template parameters we liked. */
7756 return NULL_TREE;
7757 }
7758
7759 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7760 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7761 call FN with the parameter and the DATA.
7762 If FN returns nonzero, the iteration is terminated, and
7763 for_each_template_parm returns 1. Otherwise, the iteration
7764 continues. If FN never returns a nonzero value, the value
7765 returned by for_each_template_parm is 0. If FN is NULL, it is
7766 considered to be the function which always returns 1.
7767
7768 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7769 parameters that occur in non-deduced contexts. When false, only
7770 visits those template parameters that can be deduced. */
7771
7772 static int
7773 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7774 struct pointer_set_t *visited,
7775 bool include_nondeduced_p)
7776 {
7777 struct pair_fn_data pfd;
7778 int result;
7779
7780 /* Set up. */
7781 pfd.fn = fn;
7782 pfd.data = data;
7783 pfd.include_nondeduced_p = include_nondeduced_p;
7784
7785 /* Walk the tree. (Conceptually, we would like to walk without
7786 duplicates, but for_each_template_parm_r recursively calls
7787 for_each_template_parm, so we would need to reorganize a fair
7788 bit to use walk_tree_without_duplicates, so we keep our own
7789 visited list.) */
7790 if (visited)
7791 pfd.visited = visited;
7792 else
7793 pfd.visited = pointer_set_create ();
7794 result = cp_walk_tree (&t,
7795 for_each_template_parm_r,
7796 &pfd,
7797 pfd.visited) != NULL_TREE;
7798
7799 /* Clean up. */
7800 if (!visited)
7801 {
7802 pointer_set_destroy (pfd.visited);
7803 pfd.visited = 0;
7804 }
7805
7806 return result;
7807 }
7808
7809 /* Returns true if T depends on any template parameter. */
7810
7811 int
7812 uses_template_parms (tree t)
7813 {
7814 bool dependent_p;
7815 int saved_processing_template_decl;
7816
7817 saved_processing_template_decl = processing_template_decl;
7818 if (!saved_processing_template_decl)
7819 processing_template_decl = 1;
7820 if (TYPE_P (t))
7821 dependent_p = dependent_type_p (t);
7822 else if (TREE_CODE (t) == TREE_VEC)
7823 dependent_p = any_dependent_template_arguments_p (t);
7824 else if (TREE_CODE (t) == TREE_LIST)
7825 dependent_p = (uses_template_parms (TREE_VALUE (t))
7826 || uses_template_parms (TREE_CHAIN (t)));
7827 else if (TREE_CODE (t) == TYPE_DECL)
7828 dependent_p = dependent_type_p (TREE_TYPE (t));
7829 else if (DECL_P (t)
7830 || EXPR_P (t)
7831 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7832 || TREE_CODE (t) == OVERLOAD
7833 || BASELINK_P (t)
7834 || TREE_CODE (t) == IDENTIFIER_NODE
7835 || TREE_CODE (t) == TRAIT_EXPR
7836 || TREE_CODE (t) == CONSTRUCTOR
7837 || CONSTANT_CLASS_P (t))
7838 dependent_p = (type_dependent_expression_p (t)
7839 || value_dependent_expression_p (t));
7840 else
7841 {
7842 gcc_assert (t == error_mark_node);
7843 dependent_p = false;
7844 }
7845
7846 processing_template_decl = saved_processing_template_decl;
7847
7848 return dependent_p;
7849 }
7850
7851 /* Returns true iff current_function_decl is an incompletely instantiated
7852 template. Useful instead of processing_template_decl because the latter
7853 is set to 0 during fold_non_dependent_expr. */
7854
7855 bool
7856 in_template_function (void)
7857 {
7858 tree fn = current_function_decl;
7859 bool ret;
7860 ++processing_template_decl;
7861 ret = (fn && DECL_LANG_SPECIFIC (fn)
7862 && DECL_TEMPLATE_INFO (fn)
7863 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7864 --processing_template_decl;
7865 return ret;
7866 }
7867
7868 /* Returns true if T depends on any template parameter with level LEVEL. */
7869
7870 int
7871 uses_template_parms_level (tree t, int level)
7872 {
7873 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7874 /*include_nondeduced_p=*/true);
7875 }
7876
7877 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7878 ill-formed translation unit, i.e. a variable or function that isn't
7879 usable in a constant expression. */
7880
7881 static inline bool
7882 neglectable_inst_p (tree d)
7883 {
7884 return (DECL_P (d)
7885 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7886 : decl_maybe_constant_var_p (d)));
7887 }
7888
7889 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7890 neglectable and instantiated from within an erroneous instantiation. */
7891
7892 static bool
7893 limit_bad_template_recursion (tree decl)
7894 {
7895 struct tinst_level *lev = current_tinst_level;
7896 int errs = errorcount + sorrycount;
7897 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7898 return false;
7899
7900 for (; lev; lev = lev->next)
7901 if (neglectable_inst_p (lev->decl))
7902 break;
7903
7904 return (lev && errs > lev->errors);
7905 }
7906
7907 static int tinst_depth;
7908 extern int max_tinst_depth;
7909 int depth_reached;
7910
7911 static GTY(()) struct tinst_level *last_error_tinst_level;
7912
7913 /* We're starting to instantiate D; record the template instantiation context
7914 for diagnostics and to restore it later. */
7915
7916 int
7917 push_tinst_level (tree d)
7918 {
7919 struct tinst_level *new_level;
7920
7921 if (tinst_depth >= max_tinst_depth)
7922 {
7923 last_error_tinst_level = current_tinst_level;
7924 if (TREE_CODE (d) == TREE_LIST)
7925 error ("template instantiation depth exceeds maximum of %d (use "
7926 "-ftemplate-depth= to increase the maximum) substituting %qS",
7927 max_tinst_depth, d);
7928 else
7929 error ("template instantiation depth exceeds maximum of %d (use "
7930 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7931 max_tinst_depth, d);
7932
7933 print_instantiation_context ();
7934
7935 return 0;
7936 }
7937
7938 /* If the current instantiation caused problems, don't let it instantiate
7939 anything else. Do allow deduction substitution and decls usable in
7940 constant expressions. */
7941 if (limit_bad_template_recursion (d))
7942 return 0;
7943
7944 new_level = ggc_alloc_tinst_level ();
7945 new_level->decl = d;
7946 new_level->locus = input_location;
7947 new_level->errors = errorcount+sorrycount;
7948 new_level->in_system_header_p = in_system_header;
7949 new_level->next = current_tinst_level;
7950 current_tinst_level = new_level;
7951
7952 ++tinst_depth;
7953 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
7954 depth_reached = tinst_depth;
7955
7956 return 1;
7957 }
7958
7959 /* We're done instantiating this template; return to the instantiation
7960 context. */
7961
7962 void
7963 pop_tinst_level (void)
7964 {
7965 /* Restore the filename and line number stashed away when we started
7966 this instantiation. */
7967 input_location = current_tinst_level->locus;
7968 current_tinst_level = current_tinst_level->next;
7969 --tinst_depth;
7970 }
7971
7972 /* We're instantiating a deferred template; restore the template
7973 instantiation context in which the instantiation was requested, which
7974 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7975
7976 static tree
7977 reopen_tinst_level (struct tinst_level *level)
7978 {
7979 struct tinst_level *t;
7980
7981 tinst_depth = 0;
7982 for (t = level; t; t = t->next)
7983 ++tinst_depth;
7984
7985 current_tinst_level = level;
7986 pop_tinst_level ();
7987 if (current_tinst_level)
7988 current_tinst_level->errors = errorcount+sorrycount;
7989 return level->decl;
7990 }
7991
7992 /* Returns the TINST_LEVEL which gives the original instantiation
7993 context. */
7994
7995 struct tinst_level *
7996 outermost_tinst_level (void)
7997 {
7998 struct tinst_level *level = current_tinst_level;
7999 if (level)
8000 while (level->next)
8001 level = level->next;
8002 return level;
8003 }
8004
8005 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8006 vector of template arguments, as for tsubst.
8007
8008 Returns an appropriate tsubst'd friend declaration. */
8009
8010 static tree
8011 tsubst_friend_function (tree decl, tree args)
8012 {
8013 tree new_friend;
8014
8015 if (TREE_CODE (decl) == FUNCTION_DECL
8016 && DECL_TEMPLATE_INSTANTIATION (decl)
8017 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8018 /* This was a friend declared with an explicit template
8019 argument list, e.g.:
8020
8021 friend void f<>(T);
8022
8023 to indicate that f was a template instantiation, not a new
8024 function declaration. Now, we have to figure out what
8025 instantiation of what template. */
8026 {
8027 tree template_id, arglist, fns;
8028 tree new_args;
8029 tree tmpl;
8030 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8031
8032 /* Friend functions are looked up in the containing namespace scope.
8033 We must enter that scope, to avoid finding member functions of the
8034 current class with same name. */
8035 push_nested_namespace (ns);
8036 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8037 tf_warning_or_error, NULL_TREE,
8038 /*integral_constant_expression_p=*/false);
8039 pop_nested_namespace (ns);
8040 arglist = tsubst (DECL_TI_ARGS (decl), args,
8041 tf_warning_or_error, NULL_TREE);
8042 template_id = lookup_template_function (fns, arglist);
8043
8044 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8045 tmpl = determine_specialization (template_id, new_friend,
8046 &new_args,
8047 /*need_member_template=*/0,
8048 TREE_VEC_LENGTH (args),
8049 tsk_none);
8050 return instantiate_template (tmpl, new_args, tf_error);
8051 }
8052
8053 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8054
8055 /* The NEW_FRIEND will look like an instantiation, to the
8056 compiler, but is not an instantiation from the point of view of
8057 the language. For example, we might have had:
8058
8059 template <class T> struct S {
8060 template <class U> friend void f(T, U);
8061 };
8062
8063 Then, in S<int>, template <class U> void f(int, U) is not an
8064 instantiation of anything. */
8065 if (new_friend == error_mark_node)
8066 return error_mark_node;
8067
8068 DECL_USE_TEMPLATE (new_friend) = 0;
8069 if (TREE_CODE (decl) == TEMPLATE_DECL)
8070 {
8071 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8072 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8073 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8074 }
8075
8076 /* The mangled name for the NEW_FRIEND is incorrect. The function
8077 is not a template instantiation and should not be mangled like
8078 one. Therefore, we forget the mangling here; we'll recompute it
8079 later if we need it. */
8080 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8081 {
8082 SET_DECL_RTL (new_friend, NULL);
8083 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8084 }
8085
8086 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8087 {
8088 tree old_decl;
8089 tree new_friend_template_info;
8090 tree new_friend_result_template_info;
8091 tree ns;
8092 int new_friend_is_defn;
8093
8094 /* We must save some information from NEW_FRIEND before calling
8095 duplicate decls since that function will free NEW_FRIEND if
8096 possible. */
8097 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8098 new_friend_is_defn =
8099 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8100 (template_for_substitution (new_friend)))
8101 != NULL_TREE);
8102 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8103 {
8104 /* This declaration is a `primary' template. */
8105 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8106
8107 new_friend_result_template_info
8108 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8109 }
8110 else
8111 new_friend_result_template_info = NULL_TREE;
8112
8113 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8114 if (new_friend_is_defn)
8115 DECL_INITIAL (new_friend) = error_mark_node;
8116
8117 /* Inside pushdecl_namespace_level, we will push into the
8118 current namespace. However, the friend function should go
8119 into the namespace of the template. */
8120 ns = decl_namespace_context (new_friend);
8121 push_nested_namespace (ns);
8122 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8123 pop_nested_namespace (ns);
8124
8125 if (old_decl == error_mark_node)
8126 return error_mark_node;
8127
8128 if (old_decl != new_friend)
8129 {
8130 /* This new friend declaration matched an existing
8131 declaration. For example, given:
8132
8133 template <class T> void f(T);
8134 template <class U> class C {
8135 template <class T> friend void f(T) {}
8136 };
8137
8138 the friend declaration actually provides the definition
8139 of `f', once C has been instantiated for some type. So,
8140 old_decl will be the out-of-class template declaration,
8141 while new_friend is the in-class definition.
8142
8143 But, if `f' was called before this point, the
8144 instantiation of `f' will have DECL_TI_ARGS corresponding
8145 to `T' but not to `U', references to which might appear
8146 in the definition of `f'. Previously, the most general
8147 template for an instantiation of `f' was the out-of-class
8148 version; now it is the in-class version. Therefore, we
8149 run through all specialization of `f', adding to their
8150 DECL_TI_ARGS appropriately. In particular, they need a
8151 new set of outer arguments, corresponding to the
8152 arguments for this class instantiation.
8153
8154 The same situation can arise with something like this:
8155
8156 friend void f(int);
8157 template <class T> class C {
8158 friend void f(T) {}
8159 };
8160
8161 when `C<int>' is instantiated. Now, `f(int)' is defined
8162 in the class. */
8163
8164 if (!new_friend_is_defn)
8165 /* On the other hand, if the in-class declaration does
8166 *not* provide a definition, then we don't want to alter
8167 existing definitions. We can just leave everything
8168 alone. */
8169 ;
8170 else
8171 {
8172 tree new_template = TI_TEMPLATE (new_friend_template_info);
8173 tree new_args = TI_ARGS (new_friend_template_info);
8174
8175 /* Overwrite whatever template info was there before, if
8176 any, with the new template information pertaining to
8177 the declaration. */
8178 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8179
8180 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8181 {
8182 /* We should have called reregister_specialization in
8183 duplicate_decls. */
8184 gcc_assert (retrieve_specialization (new_template,
8185 new_args, 0)
8186 == old_decl);
8187
8188 /* Instantiate it if the global has already been used. */
8189 if (DECL_ODR_USED (old_decl))
8190 instantiate_decl (old_decl, /*defer_ok=*/true,
8191 /*expl_inst_class_mem_p=*/false);
8192 }
8193 else
8194 {
8195 tree t;
8196
8197 /* Indicate that the old function template is a partial
8198 instantiation. */
8199 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8200 = new_friend_result_template_info;
8201
8202 gcc_assert (new_template
8203 == most_general_template (new_template));
8204 gcc_assert (new_template != old_decl);
8205
8206 /* Reassign any specializations already in the hash table
8207 to the new more general template, and add the
8208 additional template args. */
8209 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8210 t != NULL_TREE;
8211 t = TREE_CHAIN (t))
8212 {
8213 tree spec = TREE_VALUE (t);
8214 spec_entry elt;
8215
8216 elt.tmpl = old_decl;
8217 elt.args = DECL_TI_ARGS (spec);
8218 elt.spec = NULL_TREE;
8219
8220 htab_remove_elt (decl_specializations, &elt);
8221
8222 DECL_TI_ARGS (spec)
8223 = add_outermost_template_args (new_args,
8224 DECL_TI_ARGS (spec));
8225
8226 register_specialization
8227 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8228
8229 }
8230 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8231 }
8232 }
8233
8234 /* The information from NEW_FRIEND has been merged into OLD_DECL
8235 by duplicate_decls. */
8236 new_friend = old_decl;
8237 }
8238 }
8239 else
8240 {
8241 tree context = DECL_CONTEXT (new_friend);
8242 bool dependent_p;
8243
8244 /* In the code
8245 template <class T> class C {
8246 template <class U> friend void C1<U>::f (); // case 1
8247 friend void C2<T>::f (); // case 2
8248 };
8249 we only need to make sure CONTEXT is a complete type for
8250 case 2. To distinguish between the two cases, we note that
8251 CONTEXT of case 1 remains dependent type after tsubst while
8252 this isn't true for case 2. */
8253 ++processing_template_decl;
8254 dependent_p = dependent_type_p (context);
8255 --processing_template_decl;
8256
8257 if (!dependent_p
8258 && !complete_type_or_else (context, NULL_TREE))
8259 return error_mark_node;
8260
8261 if (COMPLETE_TYPE_P (context))
8262 {
8263 /* Check to see that the declaration is really present, and,
8264 possibly obtain an improved declaration. */
8265 tree fn = check_classfn (context,
8266 new_friend, NULL_TREE);
8267
8268 if (fn)
8269 new_friend = fn;
8270 }
8271 }
8272
8273 return new_friend;
8274 }
8275
8276 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8277 template arguments, as for tsubst.
8278
8279 Returns an appropriate tsubst'd friend type or error_mark_node on
8280 failure. */
8281
8282 static tree
8283 tsubst_friend_class (tree friend_tmpl, tree args)
8284 {
8285 tree friend_type;
8286 tree tmpl;
8287 tree context;
8288
8289 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8290 {
8291 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8292 return TREE_TYPE (t);
8293 }
8294
8295 context = CP_DECL_CONTEXT (friend_tmpl);
8296
8297 if (context != global_namespace)
8298 {
8299 if (TREE_CODE (context) == NAMESPACE_DECL)
8300 push_nested_namespace (context);
8301 else
8302 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8303 }
8304
8305 /* Look for a class template declaration. We look for hidden names
8306 because two friend declarations of the same template are the
8307 same. For example, in:
8308
8309 struct A {
8310 template <typename> friend class F;
8311 };
8312 template <typename> struct B {
8313 template <typename> friend class F;
8314 };
8315
8316 both F templates are the same. */
8317 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8318 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8319
8320 /* But, if we don't find one, it might be because we're in a
8321 situation like this:
8322
8323 template <class T>
8324 struct S {
8325 template <class U>
8326 friend struct S;
8327 };
8328
8329 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8330 for `S<int>', not the TEMPLATE_DECL. */
8331 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8332 {
8333 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8334 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8335 }
8336
8337 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8338 {
8339 /* The friend template has already been declared. Just
8340 check to see that the declarations match, and install any new
8341 default parameters. We must tsubst the default parameters,
8342 of course. We only need the innermost template parameters
8343 because that is all that redeclare_class_template will look
8344 at. */
8345 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8346 > TMPL_ARGS_DEPTH (args))
8347 {
8348 tree parms;
8349 location_t saved_input_location;
8350 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8351 args, tf_warning_or_error);
8352
8353 saved_input_location = input_location;
8354 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8355 redeclare_class_template (TREE_TYPE (tmpl), parms);
8356 input_location = saved_input_location;
8357
8358 }
8359
8360 friend_type = TREE_TYPE (tmpl);
8361 }
8362 else
8363 {
8364 /* The friend template has not already been declared. In this
8365 case, the instantiation of the template class will cause the
8366 injection of this template into the global scope. */
8367 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8368 if (tmpl == error_mark_node)
8369 return error_mark_node;
8370
8371 /* The new TMPL is not an instantiation of anything, so we
8372 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8373 the new type because that is supposed to be the corresponding
8374 template decl, i.e., TMPL. */
8375 DECL_USE_TEMPLATE (tmpl) = 0;
8376 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8377 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8378 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8379 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8380
8381 /* Inject this template into the global scope. */
8382 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8383 }
8384
8385 if (context != global_namespace)
8386 {
8387 if (TREE_CODE (context) == NAMESPACE_DECL)
8388 pop_nested_namespace (context);
8389 else
8390 pop_nested_class ();
8391 }
8392
8393 return friend_type;
8394 }
8395
8396 /* Returns zero if TYPE cannot be completed later due to circularity.
8397 Otherwise returns one. */
8398
8399 static int
8400 can_complete_type_without_circularity (tree type)
8401 {
8402 if (type == NULL_TREE || type == error_mark_node)
8403 return 0;
8404 else if (COMPLETE_TYPE_P (type))
8405 return 1;
8406 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8407 return can_complete_type_without_circularity (TREE_TYPE (type));
8408 else if (CLASS_TYPE_P (type)
8409 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8410 return 0;
8411 else
8412 return 1;
8413 }
8414
8415 /* Apply any attributes which had to be deferred until instantiation
8416 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8417 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8418
8419 static void
8420 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8421 tree args, tsubst_flags_t complain, tree in_decl)
8422 {
8423 tree last_dep = NULL_TREE;
8424 tree t;
8425 tree *p;
8426
8427 for (t = attributes; t; t = TREE_CHAIN (t))
8428 if (ATTR_IS_DEPENDENT (t))
8429 {
8430 last_dep = t;
8431 attributes = copy_list (attributes);
8432 break;
8433 }
8434
8435 if (DECL_P (*decl_p))
8436 {
8437 if (TREE_TYPE (*decl_p) == error_mark_node)
8438 return;
8439 p = &DECL_ATTRIBUTES (*decl_p);
8440 }
8441 else
8442 p = &TYPE_ATTRIBUTES (*decl_p);
8443
8444 if (last_dep)
8445 {
8446 tree late_attrs = NULL_TREE;
8447 tree *q = &late_attrs;
8448
8449 for (*p = attributes; *p; )
8450 {
8451 t = *p;
8452 if (ATTR_IS_DEPENDENT (t))
8453 {
8454 *p = TREE_CHAIN (t);
8455 TREE_CHAIN (t) = NULL_TREE;
8456 /* If the first attribute argument is an identifier, don't
8457 pass it through tsubst. Attributes like mode, format,
8458 cleanup and several target specific attributes expect it
8459 unmodified. */
8460 if (TREE_VALUE (t)
8461 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8462 && TREE_VALUE (TREE_VALUE (t))
8463 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8464 == IDENTIFIER_NODE))
8465 {
8466 tree chain
8467 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8468 in_decl,
8469 /*integral_constant_expression_p=*/false);
8470 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8471 TREE_VALUE (t)
8472 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8473 chain);
8474 }
8475 else
8476 TREE_VALUE (t)
8477 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8478 /*integral_constant_expression_p=*/false);
8479 *q = t;
8480 q = &TREE_CHAIN (t);
8481 }
8482 else
8483 p = &TREE_CHAIN (t);
8484 }
8485
8486 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8487 }
8488 }
8489
8490 /* Perform (or defer) access check for typedefs that were referenced
8491 from within the template TMPL code.
8492 This is a subroutine of instantiate_decl and instantiate_class_template.
8493 TMPL is the template to consider and TARGS is the list of arguments of
8494 that template. */
8495
8496 static void
8497 perform_typedefs_access_check (tree tmpl, tree targs)
8498 {
8499 location_t saved_location;
8500 unsigned i;
8501 qualified_typedef_usage_t *iter;
8502
8503 if (!tmpl
8504 || (!CLASS_TYPE_P (tmpl)
8505 && TREE_CODE (tmpl) != FUNCTION_DECL))
8506 return;
8507
8508 saved_location = input_location;
8509 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8510 {
8511 tree type_decl = iter->typedef_decl;
8512 tree type_scope = iter->context;
8513
8514 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8515 continue;
8516
8517 if (uses_template_parms (type_decl))
8518 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8519 if (uses_template_parms (type_scope))
8520 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8521
8522 /* Make access check error messages point to the location
8523 of the use of the typedef. */
8524 input_location = iter->locus;
8525 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8526 type_decl, type_decl,
8527 tf_warning_or_error);
8528 }
8529 input_location = saved_location;
8530 }
8531
8532 static tree
8533 instantiate_class_template_1 (tree type)
8534 {
8535 tree templ, args, pattern, t, member;
8536 tree typedecl;
8537 tree pbinfo;
8538 tree base_list;
8539 unsigned int saved_maximum_field_alignment;
8540 tree fn_context;
8541
8542 if (type == error_mark_node)
8543 return error_mark_node;
8544
8545 if (COMPLETE_OR_OPEN_TYPE_P (type)
8546 || uses_template_parms (type))
8547 return type;
8548
8549 /* Figure out which template is being instantiated. */
8550 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8551 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8552
8553 /* Determine what specialization of the original template to
8554 instantiate. */
8555 t = most_specialized_class (type, templ, tf_warning_or_error);
8556 if (t == error_mark_node)
8557 {
8558 TYPE_BEING_DEFINED (type) = 1;
8559 return error_mark_node;
8560 }
8561 else if (t)
8562 {
8563 /* This TYPE is actually an instantiation of a partial
8564 specialization. We replace the innermost set of ARGS with
8565 the arguments appropriate for substitution. For example,
8566 given:
8567
8568 template <class T> struct S {};
8569 template <class T> struct S<T*> {};
8570
8571 and supposing that we are instantiating S<int*>, ARGS will
8572 presently be {int*} -- but we need {int}. */
8573 pattern = TREE_TYPE (t);
8574 args = TREE_PURPOSE (t);
8575 }
8576 else
8577 {
8578 pattern = TREE_TYPE (templ);
8579 args = CLASSTYPE_TI_ARGS (type);
8580 }
8581
8582 /* If the template we're instantiating is incomplete, then clearly
8583 there's nothing we can do. */
8584 if (!COMPLETE_TYPE_P (pattern))
8585 return type;
8586
8587 /* If we've recursively instantiated too many templates, stop. */
8588 if (! push_tinst_level (type))
8589 return type;
8590
8591 /* Now we're really doing the instantiation. Mark the type as in
8592 the process of being defined. */
8593 TYPE_BEING_DEFINED (type) = 1;
8594
8595 /* We may be in the middle of deferred access check. Disable
8596 it now. */
8597 push_deferring_access_checks (dk_no_deferred);
8598
8599 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8600 if (!fn_context)
8601 push_to_top_level ();
8602 /* Use #pragma pack from the template context. */
8603 saved_maximum_field_alignment = maximum_field_alignment;
8604 maximum_field_alignment = TYPE_PRECISION (pattern);
8605
8606 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8607
8608 /* Set the input location to the most specialized template definition.
8609 This is needed if tsubsting causes an error. */
8610 typedecl = TYPE_MAIN_DECL (pattern);
8611 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8612 DECL_SOURCE_LOCATION (typedecl);
8613
8614 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8615 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8616 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8617 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8618 if (ANON_AGGR_TYPE_P (pattern))
8619 SET_ANON_AGGR_TYPE_P (type);
8620 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8621 {
8622 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8623 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8624 /* Adjust visibility for template arguments. */
8625 determine_visibility (TYPE_MAIN_DECL (type));
8626 }
8627 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8628
8629 pbinfo = TYPE_BINFO (pattern);
8630
8631 /* We should never instantiate a nested class before its enclosing
8632 class; we need to look up the nested class by name before we can
8633 instantiate it, and that lookup should instantiate the enclosing
8634 class. */
8635 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8636 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8637
8638 base_list = NULL_TREE;
8639 if (BINFO_N_BASE_BINFOS (pbinfo))
8640 {
8641 tree pbase_binfo;
8642 tree pushed_scope;
8643 int i;
8644
8645 /* We must enter the scope containing the type, as that is where
8646 the accessibility of types named in dependent bases are
8647 looked up from. */
8648 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8649
8650 /* Substitute into each of the bases to determine the actual
8651 basetypes. */
8652 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8653 {
8654 tree base;
8655 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8656 tree expanded_bases = NULL_TREE;
8657 int idx, len = 1;
8658
8659 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8660 {
8661 expanded_bases =
8662 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8663 args, tf_error, NULL_TREE);
8664 if (expanded_bases == error_mark_node)
8665 continue;
8666
8667 len = TREE_VEC_LENGTH (expanded_bases);
8668 }
8669
8670 for (idx = 0; idx < len; idx++)
8671 {
8672 if (expanded_bases)
8673 /* Extract the already-expanded base class. */
8674 base = TREE_VEC_ELT (expanded_bases, idx);
8675 else
8676 /* Substitute to figure out the base class. */
8677 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8678 NULL_TREE);
8679
8680 if (base == error_mark_node)
8681 continue;
8682
8683 base_list = tree_cons (access, base, base_list);
8684 if (BINFO_VIRTUAL_P (pbase_binfo))
8685 TREE_TYPE (base_list) = integer_type_node;
8686 }
8687 }
8688
8689 /* The list is now in reverse order; correct that. */
8690 base_list = nreverse (base_list);
8691
8692 if (pushed_scope)
8693 pop_scope (pushed_scope);
8694 }
8695 /* Now call xref_basetypes to set up all the base-class
8696 information. */
8697 xref_basetypes (type, base_list);
8698
8699 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8700 (int) ATTR_FLAG_TYPE_IN_PLACE,
8701 args, tf_error, NULL_TREE);
8702 fixup_attribute_variants (type);
8703
8704 /* Now that our base classes are set up, enter the scope of the
8705 class, so that name lookups into base classes, etc. will work
8706 correctly. This is precisely analogous to what we do in
8707 begin_class_definition when defining an ordinary non-template
8708 class, except we also need to push the enclosing classes. */
8709 push_nested_class (type);
8710
8711 /* Now members are processed in the order of declaration. */
8712 for (member = CLASSTYPE_DECL_LIST (pattern);
8713 member; member = TREE_CHAIN (member))
8714 {
8715 tree t = TREE_VALUE (member);
8716
8717 if (TREE_PURPOSE (member))
8718 {
8719 if (TYPE_P (t))
8720 {
8721 /* Build new CLASSTYPE_NESTED_UTDS. */
8722
8723 tree newtag;
8724 bool class_template_p;
8725
8726 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8727 && TYPE_LANG_SPECIFIC (t)
8728 && CLASSTYPE_IS_TEMPLATE (t));
8729 /* If the member is a class template, then -- even after
8730 substitution -- there may be dependent types in the
8731 template argument list for the class. We increment
8732 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8733 that function will assume that no types are dependent
8734 when outside of a template. */
8735 if (class_template_p)
8736 ++processing_template_decl;
8737 newtag = tsubst (t, args, tf_error, NULL_TREE);
8738 if (class_template_p)
8739 --processing_template_decl;
8740 if (newtag == error_mark_node)
8741 continue;
8742
8743 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8744 {
8745 tree name = TYPE_IDENTIFIER (t);
8746
8747 if (class_template_p)
8748 /* Unfortunately, lookup_template_class sets
8749 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8750 instantiation (i.e., for the type of a member
8751 template class nested within a template class.)
8752 This behavior is required for
8753 maybe_process_partial_specialization to work
8754 correctly, but is not accurate in this case;
8755 the TAG is not an instantiation of anything.
8756 (The corresponding TEMPLATE_DECL is an
8757 instantiation, but the TYPE is not.) */
8758 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8759
8760 /* Now, we call pushtag to put this NEWTAG into the scope of
8761 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8762 pushtag calling push_template_decl. We don't have to do
8763 this for enums because it will already have been done in
8764 tsubst_enum. */
8765 if (name)
8766 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8767 pushtag (name, newtag, /*tag_scope=*/ts_current);
8768 }
8769 }
8770 else if (TREE_CODE (t) == FUNCTION_DECL
8771 || DECL_FUNCTION_TEMPLATE_P (t))
8772 {
8773 /* Build new TYPE_METHODS. */
8774 tree r;
8775
8776 if (TREE_CODE (t) == TEMPLATE_DECL)
8777 ++processing_template_decl;
8778 r = tsubst (t, args, tf_error, NULL_TREE);
8779 if (TREE_CODE (t) == TEMPLATE_DECL)
8780 --processing_template_decl;
8781 set_current_access_from_decl (r);
8782 finish_member_declaration (r);
8783 /* Instantiate members marked with attribute used. */
8784 if (r != error_mark_node && DECL_PRESERVE_P (r))
8785 mark_used (r);
8786 }
8787 else
8788 {
8789 /* Build new TYPE_FIELDS. */
8790 if (TREE_CODE (t) == STATIC_ASSERT)
8791 {
8792 tree condition;
8793
8794 ++c_inhibit_evaluation_warnings;
8795 condition =
8796 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8797 tf_warning_or_error, NULL_TREE,
8798 /*integral_constant_expression_p=*/true);
8799 --c_inhibit_evaluation_warnings;
8800
8801 finish_static_assert (condition,
8802 STATIC_ASSERT_MESSAGE (t),
8803 STATIC_ASSERT_SOURCE_LOCATION (t),
8804 /*member_p=*/true);
8805 }
8806 else if (TREE_CODE (t) != CONST_DECL)
8807 {
8808 tree r;
8809
8810 /* The file and line for this declaration, to
8811 assist in error message reporting. Since we
8812 called push_tinst_level above, we don't need to
8813 restore these. */
8814 input_location = DECL_SOURCE_LOCATION (t);
8815
8816 if (TREE_CODE (t) == TEMPLATE_DECL)
8817 ++processing_template_decl;
8818 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8819 if (TREE_CODE (t) == TEMPLATE_DECL)
8820 --processing_template_decl;
8821 if (TREE_CODE (r) == VAR_DECL)
8822 {
8823 /* In [temp.inst]:
8824
8825 [t]he initialization (and any associated
8826 side-effects) of a static data member does
8827 not occur unless the static data member is
8828 itself used in a way that requires the
8829 definition of the static data member to
8830 exist.
8831
8832 Therefore, we do not substitute into the
8833 initialized for the static data member here. */
8834 finish_static_data_member_decl
8835 (r,
8836 /*init=*/NULL_TREE,
8837 /*init_const_expr_p=*/false,
8838 /*asmspec_tree=*/NULL_TREE,
8839 /*flags=*/0);
8840 /* Instantiate members marked with attribute used. */
8841 if (r != error_mark_node && DECL_PRESERVE_P (r))
8842 mark_used (r);
8843 }
8844 else if (TREE_CODE (r) == FIELD_DECL)
8845 {
8846 /* Determine whether R has a valid type and can be
8847 completed later. If R is invalid, then it is
8848 replaced by error_mark_node so that it will not be
8849 added to TYPE_FIELDS. */
8850 tree rtype = TREE_TYPE (r);
8851 if (can_complete_type_without_circularity (rtype))
8852 complete_type (rtype);
8853
8854 if (!COMPLETE_TYPE_P (rtype))
8855 {
8856 cxx_incomplete_type_error (r, rtype);
8857 r = error_mark_node;
8858 }
8859 }
8860
8861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8862 such a thing will already have been added to the field
8863 list by tsubst_enum in finish_member_declaration in the
8864 CLASSTYPE_NESTED_UTDS case above. */
8865 if (!(TREE_CODE (r) == TYPE_DECL
8866 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8867 && DECL_ARTIFICIAL (r)))
8868 {
8869 set_current_access_from_decl (r);
8870 finish_member_declaration (r);
8871 }
8872 }
8873 }
8874 }
8875 else
8876 {
8877 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
8878 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8879 {
8880 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8881
8882 tree friend_type = t;
8883 bool adjust_processing_template_decl = false;
8884
8885 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8886 {
8887 /* template <class T> friend class C; */
8888 friend_type = tsubst_friend_class (friend_type, args);
8889 adjust_processing_template_decl = true;
8890 }
8891 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8892 {
8893 /* template <class T> friend class C::D; */
8894 friend_type = tsubst (friend_type, args,
8895 tf_warning_or_error, NULL_TREE);
8896 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8897 friend_type = TREE_TYPE (friend_type);
8898 adjust_processing_template_decl = true;
8899 }
8900 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8901 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8902 {
8903 /* This could be either
8904
8905 friend class T::C;
8906
8907 when dependent_type_p is false or
8908
8909 template <class U> friend class T::C;
8910
8911 otherwise. */
8912 friend_type = tsubst (friend_type, args,
8913 tf_warning_or_error, NULL_TREE);
8914 /* Bump processing_template_decl for correct
8915 dependent_type_p calculation. */
8916 ++processing_template_decl;
8917 if (dependent_type_p (friend_type))
8918 adjust_processing_template_decl = true;
8919 --processing_template_decl;
8920 }
8921 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8922 && hidden_name_p (TYPE_NAME (friend_type)))
8923 {
8924 /* friend class C;
8925
8926 where C hasn't been declared yet. Let's lookup name
8927 from namespace scope directly, bypassing any name that
8928 come from dependent base class. */
8929 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8930
8931 /* The call to xref_tag_from_type does injection for friend
8932 classes. */
8933 push_nested_namespace (ns);
8934 friend_type =
8935 xref_tag_from_type (friend_type, NULL_TREE,
8936 /*tag_scope=*/ts_current);
8937 pop_nested_namespace (ns);
8938 }
8939 else if (uses_template_parms (friend_type))
8940 /* friend class C<T>; */
8941 friend_type = tsubst (friend_type, args,
8942 tf_warning_or_error, NULL_TREE);
8943 /* Otherwise it's
8944
8945 friend class C;
8946
8947 where C is already declared or
8948
8949 friend class C<int>;
8950
8951 We don't have to do anything in these cases. */
8952
8953 if (adjust_processing_template_decl)
8954 /* Trick make_friend_class into realizing that the friend
8955 we're adding is a template, not an ordinary class. It's
8956 important that we use make_friend_class since it will
8957 perform some error-checking and output cross-reference
8958 information. */
8959 ++processing_template_decl;
8960
8961 if (friend_type != error_mark_node)
8962 make_friend_class (type, friend_type, /*complain=*/false);
8963
8964 if (adjust_processing_template_decl)
8965 --processing_template_decl;
8966 }
8967 else
8968 {
8969 /* Build new DECL_FRIENDLIST. */
8970 tree r;
8971
8972 /* The file and line for this declaration, to
8973 assist in error message reporting. Since we
8974 called push_tinst_level above, we don't need to
8975 restore these. */
8976 input_location = DECL_SOURCE_LOCATION (t);
8977
8978 if (TREE_CODE (t) == TEMPLATE_DECL)
8979 {
8980 ++processing_template_decl;
8981 push_deferring_access_checks (dk_no_check);
8982 }
8983
8984 r = tsubst_friend_function (t, args);
8985 add_friend (type, r, /*complain=*/false);
8986 if (TREE_CODE (t) == TEMPLATE_DECL)
8987 {
8988 pop_deferring_access_checks ();
8989 --processing_template_decl;
8990 }
8991 }
8992 }
8993 }
8994
8995 if (CLASSTYPE_LAMBDA_EXPR (type))
8996 {
8997 tree decl = lambda_function (type);
8998 if (decl)
8999 {
9000 instantiate_decl (decl, false, false);
9001 maybe_add_lambda_conv_op (type);
9002 }
9003 else
9004 gcc_assert (errorcount);
9005 }
9006
9007 /* Set the file and line number information to whatever is given for
9008 the class itself. This puts error messages involving generated
9009 implicit functions at a predictable point, and the same point
9010 that would be used for non-template classes. */
9011 input_location = DECL_SOURCE_LOCATION (typedecl);
9012
9013 unreverse_member_declarations (type);
9014 finish_struct_1 (type);
9015 TYPE_BEING_DEFINED (type) = 0;
9016
9017 /* We don't instantiate default arguments for member functions. 14.7.1:
9018
9019 The implicit instantiation of a class template specialization causes
9020 the implicit instantiation of the declarations, but not of the
9021 definitions or default arguments, of the class member functions,
9022 member classes, static data members and member templates.... */
9023
9024 /* Some typedefs referenced from within the template code need to be access
9025 checked at template instantiation time, i.e now. These types were
9026 added to the template at parsing time. Let's get those and perform
9027 the access checks then. */
9028 perform_typedefs_access_check (pattern, args);
9029 perform_deferred_access_checks (tf_warning_or_error);
9030 pop_nested_class ();
9031 maximum_field_alignment = saved_maximum_field_alignment;
9032 if (!fn_context)
9033 pop_from_top_level ();
9034 pop_deferring_access_checks ();
9035 pop_tinst_level ();
9036
9037 /* The vtable for a template class can be emitted in any translation
9038 unit in which the class is instantiated. When there is no key
9039 method, however, finish_struct_1 will already have added TYPE to
9040 the keyed_classes list. */
9041 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9042 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9043
9044 return type;
9045 }
9046
9047 /* Wrapper for instantiate_class_template_1. */
9048
9049 tree
9050 instantiate_class_template (tree type)
9051 {
9052 tree ret;
9053 timevar_push (TV_TEMPLATE_INST);
9054 ret = instantiate_class_template_1 (type);
9055 timevar_pop (TV_TEMPLATE_INST);
9056 return ret;
9057 }
9058
9059 static tree
9060 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9061 {
9062 tree r;
9063
9064 if (!t)
9065 r = t;
9066 else if (TYPE_P (t))
9067 r = tsubst (t, args, complain, in_decl);
9068 else
9069 {
9070 if (!(complain & tf_warning))
9071 ++c_inhibit_evaluation_warnings;
9072 r = tsubst_expr (t, args, complain, in_decl,
9073 /*integral_constant_expression_p=*/true);
9074 if (!(complain & tf_warning))
9075 --c_inhibit_evaluation_warnings;
9076 /* Preserve the raw-reference nature of T. */
9077 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9078 && REFERENCE_REF_P (r))
9079 r = TREE_OPERAND (r, 0);
9080 }
9081 return r;
9082 }
9083
9084 /* Given a function parameter pack TMPL_PARM and some function parameters
9085 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9086 and set *SPEC_P to point at the next point in the list. */
9087
9088 static tree
9089 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9090 {
9091 /* Collect all of the extra "packed" parameters into an
9092 argument pack. */
9093 tree parmvec;
9094 tree parmtypevec;
9095 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9096 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9097 tree spec_parm = *spec_p;
9098 int i, len;
9099
9100 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9101 if (tmpl_parm
9102 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9103 break;
9104
9105 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9106 parmvec = make_tree_vec (len);
9107 parmtypevec = make_tree_vec (len);
9108 spec_parm = *spec_p;
9109 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9110 {
9111 TREE_VEC_ELT (parmvec, i) = spec_parm;
9112 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9113 }
9114
9115 /* Build the argument packs. */
9116 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9117 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9118 TREE_TYPE (argpack) = argtypepack;
9119 *spec_p = spec_parm;
9120
9121 return argpack;
9122 }
9123
9124 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9125 NONTYPE_ARGUMENT_PACK. */
9126
9127 static tree
9128 make_fnparm_pack (tree spec_parm)
9129 {
9130 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9131 }
9132
9133 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9134 pack expansion. */
9135
9136 static bool
9137 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9138 {
9139 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9140 if (i >= TREE_VEC_LENGTH (vec))
9141 return false;
9142 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9143 }
9144
9145
9146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9147
9148 static tree
9149 make_argument_pack_select (tree arg_pack, unsigned index)
9150 {
9151 tree aps = make_node (ARGUMENT_PACK_SELECT);
9152
9153 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9154 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9155
9156 return aps;
9157 }
9158
9159 /* This is a subroutine of tsubst_pack_expansion.
9160
9161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9162 mechanism to store the (non complete list of) arguments of the
9163 substitution and return a non substituted pack expansion, in order
9164 to wait for when we have enough arguments to really perform the
9165 substitution. */
9166
9167 static bool
9168 use_pack_expansion_extra_args_p (tree parm_packs,
9169 int arg_pack_len,
9170 bool has_empty_arg)
9171 {
9172 if (parm_packs == NULL_TREE)
9173 return false;
9174
9175 bool has_expansion_arg = false;
9176 for (int i = 0 ; i < arg_pack_len; ++i)
9177 {
9178 bool has_non_expansion_arg = false;
9179 for (tree parm_pack = parm_packs;
9180 parm_pack;
9181 parm_pack = TREE_CHAIN (parm_pack))
9182 {
9183 tree arg = TREE_VALUE (parm_pack);
9184
9185 if (argument_pack_element_is_expansion_p (arg, i))
9186 has_expansion_arg = true;
9187 else
9188 has_non_expansion_arg = true;
9189 }
9190
9191 /* If one pack has an expansion and another pack has a normal
9192 argument or if one pack has an empty argument another one
9193 hasn't then tsubst_pack_expansion cannot perform the
9194 substitution and need to fall back on the
9195 PACK_EXPANSION_EXTRA mechanism. */
9196 if ((has_expansion_arg && has_non_expansion_arg)
9197 || (has_empty_arg && (has_expansion_arg || has_non_expansion_arg)))
9198 return true;
9199 }
9200 return false;
9201 }
9202
9203 /* [temp.variadic]/6 says that:
9204
9205 The instantiation of a pack expansion [...]
9206 produces a list E1,E2, ..., En, where N is the number of elements
9207 in the pack expansion parameters.
9208
9209 This subroutine of tsubst_pack_expansion produces one of these Ei.
9210
9211 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9212 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9213 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9214 INDEX is the index 'i' of the element Ei to produce. ARGS,
9215 COMPLAIN, and IN_DECL are the same parameters as for the
9216 tsubst_pack_expansion function.
9217
9218 The function returns the resulting Ei upon successful completion,
9219 or error_mark_node.
9220
9221 Note that this function possibly modifies the ARGS parameter, so
9222 it's the responsibility of the caller to restore it. */
9223
9224 static tree
9225 gen_elem_of_pack_expansion_instantiation (tree pattern,
9226 tree parm_packs,
9227 unsigned index,
9228 tree args /* This parm gets
9229 modified. */,
9230 tsubst_flags_t complain,
9231 tree in_decl)
9232 {
9233 tree t;
9234 bool ith_elem_is_expansion = false;
9235
9236 /* For each parameter pack, change the substitution of the parameter
9237 pack to the ith argument in its argument pack, then expand the
9238 pattern. */
9239 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9240 {
9241 tree parm = TREE_PURPOSE (pack);
9242 tree arg_pack = TREE_VALUE (pack);
9243 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9244
9245 ith_elem_is_expansion |=
9246 argument_pack_element_is_expansion_p (arg_pack, index);
9247
9248 /* Select the Ith argument from the pack. */
9249 if (TREE_CODE (parm) == PARM_DECL)
9250 {
9251 if (index == 0)
9252 {
9253 aps = make_argument_pack_select (arg_pack, index);
9254 mark_used (parm);
9255 register_local_specialization (aps, parm);
9256 }
9257 else
9258 aps = retrieve_local_specialization (parm);
9259 }
9260 else
9261 {
9262 int idx, level;
9263 template_parm_level_and_index (parm, &level, &idx);
9264
9265 if (index == 0)
9266 {
9267 aps = make_argument_pack_select (arg_pack, index);
9268 /* Update the corresponding argument. */
9269 TMPL_ARG (args, level, idx) = aps;
9270 }
9271 else
9272 /* Re-use the ARGUMENT_PACK_SELECT. */
9273 aps = TMPL_ARG (args, level, idx);
9274 }
9275 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9276 }
9277
9278 /* Substitute into the PATTERN with the (possibly altered)
9279 arguments. */
9280 if (!TYPE_P (pattern))
9281 t = tsubst_expr (pattern, args, complain, in_decl,
9282 /*integral_constant_expression_p=*/false);
9283 else
9284 t = tsubst (pattern, args, complain, in_decl);
9285
9286 /* If the Ith argument pack element is a pack expansion, then
9287 the Ith element resulting from the substituting is going to
9288 be a pack expansion as well. */
9289 if (ith_elem_is_expansion)
9290 t = make_pack_expansion (t);
9291
9292 return t;
9293 }
9294
9295 /* Substitute ARGS into T, which is an pack expansion
9296 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9297 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9298 (if only a partial substitution could be performed) or
9299 ERROR_MARK_NODE if there was an error. */
9300 tree
9301 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9302 tree in_decl)
9303 {
9304 tree pattern;
9305 tree pack, packs = NULL_TREE;
9306 bool unsubstituted_packs = false;
9307 int i, len = -1;
9308 tree result;
9309 struct pointer_map_t *saved_local_specializations = NULL;
9310 bool need_local_specializations = false;
9311 int levels;
9312
9313 gcc_assert (PACK_EXPANSION_P (t));
9314 pattern = PACK_EXPANSION_PATTERN (t);
9315
9316 /* Add in any args remembered from an earlier partial instantiation. */
9317 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9318
9319 levels = TMPL_ARGS_DEPTH (args);
9320
9321 /* Determine the argument packs that will instantiate the parameter
9322 packs used in the expansion expression. While we're at it,
9323 compute the number of arguments to be expanded and make sure it
9324 is consistent. */
9325 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9326 pack = TREE_CHAIN (pack))
9327 {
9328 tree parm_pack = TREE_VALUE (pack);
9329 tree arg_pack = NULL_TREE;
9330 tree orig_arg = NULL_TREE;
9331 int level = 0;
9332
9333 if (TREE_CODE (parm_pack) == BASES)
9334 {
9335 if (BASES_DIRECT (parm_pack))
9336 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9337 args, complain, in_decl, false));
9338 else
9339 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9340 args, complain, in_decl, false));
9341 }
9342 if (TREE_CODE (parm_pack) == PARM_DECL)
9343 {
9344 if (PACK_EXPANSION_LOCAL_P (t))
9345 arg_pack = retrieve_local_specialization (parm_pack);
9346 else
9347 {
9348 /* We can't rely on local_specializations for a parameter
9349 name used later in a function declaration (such as in a
9350 late-specified return type). Even if it exists, it might
9351 have the wrong value for a recursive call. Just make a
9352 dummy decl, since it's only used for its type. */
9353 arg_pack = tsubst_decl (parm_pack, args, complain);
9354 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9355 /* Partial instantiation of the parm_pack, we can't build
9356 up an argument pack yet. */
9357 arg_pack = NULL_TREE;
9358 else
9359 arg_pack = make_fnparm_pack (arg_pack);
9360 need_local_specializations = true;
9361 }
9362 }
9363 else
9364 {
9365 int idx;
9366 template_parm_level_and_index (parm_pack, &level, &idx);
9367
9368 if (level <= levels)
9369 arg_pack = TMPL_ARG (args, level, idx);
9370 }
9371
9372 orig_arg = arg_pack;
9373 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9374 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9375
9376 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9377 /* This can only happen if we forget to expand an argument
9378 pack somewhere else. Just return an error, silently. */
9379 {
9380 result = make_tree_vec (1);
9381 TREE_VEC_ELT (result, 0) = error_mark_node;
9382 return result;
9383 }
9384
9385 if (arg_pack)
9386 {
9387 int my_len =
9388 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9389
9390 /* Don't bother trying to do a partial substitution with
9391 incomplete packs; we'll try again after deduction. */
9392 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9393 return t;
9394
9395 if (len < 0)
9396 len = my_len;
9397 else if (len != my_len)
9398 {
9399 if (!(complain & tf_error))
9400 /* Fail quietly. */;
9401 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9402 error ("mismatched argument pack lengths while expanding "
9403 "%<%T%>",
9404 pattern);
9405 else
9406 error ("mismatched argument pack lengths while expanding "
9407 "%<%E%>",
9408 pattern);
9409 return error_mark_node;
9410 }
9411
9412 /* Keep track of the parameter packs and their corresponding
9413 argument packs. */
9414 packs = tree_cons (parm_pack, arg_pack, packs);
9415 TREE_TYPE (packs) = orig_arg;
9416 }
9417 else
9418 {
9419 /* We can't substitute for this parameter pack. We use a flag as
9420 well as the missing_level counter because function parameter
9421 packs don't have a level. */
9422 unsubstituted_packs = true;
9423 }
9424 }
9425
9426 /* We cannot expand this expansion expression, because we don't have
9427 all of the argument packs we need. */
9428 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9429 {
9430 /* We got some full packs, but we can't substitute them in until we
9431 have values for all the packs. So remember these until then. */
9432
9433 t = make_pack_expansion (pattern);
9434 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9435 return t;
9436 }
9437 else if (unsubstituted_packs)
9438 {
9439 /* There were no real arguments, we're just replacing a parameter
9440 pack with another version of itself. Substitute into the
9441 pattern and return a PACK_EXPANSION_*. The caller will need to
9442 deal with that. */
9443 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9444 t = tsubst_expr (pattern, args, complain, in_decl,
9445 /*integral_constant_expression_p=*/false);
9446 else
9447 t = tsubst (pattern, args, complain, in_decl);
9448 t = make_pack_expansion (t);
9449 return t;
9450 }
9451
9452 gcc_assert (len >= 0);
9453
9454 if (need_local_specializations)
9455 {
9456 /* We're in a late-specified return type, so create our own local
9457 specializations map; the current map is either NULL or (in the
9458 case of recursive unification) might have bindings that we don't
9459 want to use or alter. */
9460 saved_local_specializations = local_specializations;
9461 local_specializations = pointer_map_create ();
9462 }
9463
9464 /* For each argument in each argument pack, substitute into the
9465 pattern. */
9466 result = make_tree_vec (len);
9467 for (i = 0; i < len; ++i)
9468 {
9469 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9470 i,
9471 args, complain,
9472 in_decl);
9473 TREE_VEC_ELT (result, i) = t;
9474 if (t == error_mark_node)
9475 {
9476 result = error_mark_node;
9477 break;
9478 }
9479 }
9480
9481 /* Update ARGS to restore the substitution from parameter packs to
9482 their argument packs. */
9483 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9484 {
9485 tree parm = TREE_PURPOSE (pack);
9486
9487 if (TREE_CODE (parm) == PARM_DECL)
9488 register_local_specialization (TREE_TYPE (pack), parm);
9489 else
9490 {
9491 int idx, level;
9492
9493 if (TREE_VALUE (pack) == NULL_TREE)
9494 continue;
9495
9496 template_parm_level_and_index (parm, &level, &idx);
9497
9498 /* Update the corresponding argument. */
9499 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9500 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9501 TREE_TYPE (pack);
9502 else
9503 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9504 }
9505 }
9506
9507 if (need_local_specializations)
9508 {
9509 pointer_map_destroy (local_specializations);
9510 local_specializations = saved_local_specializations;
9511 }
9512
9513 return result;
9514 }
9515
9516 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9517 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9518 parameter packs; all parms generated from a function parameter pack will
9519 have the same DECL_PARM_INDEX. */
9520
9521 tree
9522 get_pattern_parm (tree parm, tree tmpl)
9523 {
9524 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9525 tree patparm;
9526
9527 if (DECL_ARTIFICIAL (parm))
9528 {
9529 for (patparm = DECL_ARGUMENTS (pattern);
9530 patparm; patparm = DECL_CHAIN (patparm))
9531 if (DECL_ARTIFICIAL (patparm)
9532 && DECL_NAME (parm) == DECL_NAME (patparm))
9533 break;
9534 }
9535 else
9536 {
9537 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9538 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9539 gcc_assert (DECL_PARM_INDEX (patparm)
9540 == DECL_PARM_INDEX (parm));
9541 }
9542
9543 return patparm;
9544 }
9545
9546 /* Substitute ARGS into the vector or list of template arguments T. */
9547
9548 static tree
9549 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9550 {
9551 tree orig_t = t;
9552 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9553 tree *elts;
9554
9555 if (t == error_mark_node)
9556 return error_mark_node;
9557
9558 len = TREE_VEC_LENGTH (t);
9559 elts = XALLOCAVEC (tree, len);
9560
9561 for (i = 0; i < len; i++)
9562 {
9563 tree orig_arg = TREE_VEC_ELT (t, i);
9564 tree new_arg;
9565
9566 if (TREE_CODE (orig_arg) == TREE_VEC)
9567 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9568 else if (PACK_EXPANSION_P (orig_arg))
9569 {
9570 /* Substitute into an expansion expression. */
9571 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9572
9573 if (TREE_CODE (new_arg) == TREE_VEC)
9574 /* Add to the expanded length adjustment the number of
9575 expanded arguments. We subtract one from this
9576 measurement, because the argument pack expression
9577 itself is already counted as 1 in
9578 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9579 the argument pack is empty. */
9580 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9581 }
9582 else if (ARGUMENT_PACK_P (orig_arg))
9583 {
9584 /* Substitute into each of the arguments. */
9585 new_arg = TYPE_P (orig_arg)
9586 ? cxx_make_type (TREE_CODE (orig_arg))
9587 : make_node (TREE_CODE (orig_arg));
9588
9589 SET_ARGUMENT_PACK_ARGS (
9590 new_arg,
9591 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9592 args, complain, in_decl));
9593
9594 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9595 new_arg = error_mark_node;
9596
9597 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9598 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9599 complain, in_decl);
9600 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9601
9602 if (TREE_TYPE (new_arg) == error_mark_node)
9603 new_arg = error_mark_node;
9604 }
9605 }
9606 else
9607 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9608
9609 if (new_arg == error_mark_node)
9610 return error_mark_node;
9611
9612 elts[i] = new_arg;
9613 if (new_arg != orig_arg)
9614 need_new = 1;
9615 }
9616
9617 if (!need_new)
9618 return t;
9619
9620 /* Make space for the expanded arguments coming from template
9621 argument packs. */
9622 t = make_tree_vec (len + expanded_len_adjust);
9623 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9624 arguments for a member template.
9625 In that case each TREE_VEC in ORIG_T represents a level of template
9626 arguments, and ORIG_T won't carry any non defaulted argument count.
9627 It will rather be the nested TREE_VECs that will carry one.
9628 In other words, ORIG_T carries a non defaulted argument count only
9629 if it doesn't contain any nested TREE_VEC. */
9630 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9631 {
9632 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9633 count += expanded_len_adjust;
9634 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9635 }
9636 for (i = 0, out = 0; i < len; i++)
9637 {
9638 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9639 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9640 && TREE_CODE (elts[i]) == TREE_VEC)
9641 {
9642 int idx;
9643
9644 /* Now expand the template argument pack "in place". */
9645 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9646 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9647 }
9648 else
9649 {
9650 TREE_VEC_ELT (t, out) = elts[i];
9651 out++;
9652 }
9653 }
9654
9655 return t;
9656 }
9657
9658 /* Return the result of substituting ARGS into the template parameters
9659 given by PARMS. If there are m levels of ARGS and m + n levels of
9660 PARMS, then the result will contain n levels of PARMS. For
9661 example, if PARMS is `template <class T> template <class U>
9662 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9663 result will be `template <int*, double, class V>'. */
9664
9665 static tree
9666 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9667 {
9668 tree r = NULL_TREE;
9669 tree* new_parms;
9670
9671 /* When substituting into a template, we must set
9672 PROCESSING_TEMPLATE_DECL as the template parameters may be
9673 dependent if they are based on one-another, and the dependency
9674 predicates are short-circuit outside of templates. */
9675 ++processing_template_decl;
9676
9677 for (new_parms = &r;
9678 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9679 new_parms = &(TREE_CHAIN (*new_parms)),
9680 parms = TREE_CHAIN (parms))
9681 {
9682 tree new_vec =
9683 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9684 int i;
9685
9686 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9687 {
9688 tree tuple;
9689
9690 if (parms == error_mark_node)
9691 continue;
9692
9693 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9694
9695 if (tuple == error_mark_node)
9696 continue;
9697
9698 TREE_VEC_ELT (new_vec, i) =
9699 tsubst_template_parm (tuple, args, complain);
9700 }
9701
9702 *new_parms =
9703 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9704 - TMPL_ARGS_DEPTH (args)),
9705 new_vec, NULL_TREE);
9706 }
9707
9708 --processing_template_decl;
9709
9710 return r;
9711 }
9712
9713 /* Return the result of substituting ARGS into one template parameter
9714 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9715 parameter and which TREE_PURPOSE is the default argument of the
9716 template parameter. */
9717
9718 static tree
9719 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9720 {
9721 tree default_value, parm_decl;
9722
9723 if (args == NULL_TREE
9724 || t == NULL_TREE
9725 || t == error_mark_node)
9726 return t;
9727
9728 gcc_assert (TREE_CODE (t) == TREE_LIST);
9729
9730 default_value = TREE_PURPOSE (t);
9731 parm_decl = TREE_VALUE (t);
9732
9733 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9734 if (TREE_CODE (parm_decl) == PARM_DECL
9735 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9736 parm_decl = error_mark_node;
9737 default_value = tsubst_template_arg (default_value, args,
9738 complain, NULL_TREE);
9739
9740 return build_tree_list (default_value, parm_decl);
9741 }
9742
9743 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9744 type T. If T is not an aggregate or enumeration type, it is
9745 handled as if by tsubst. IN_DECL is as for tsubst. If
9746 ENTERING_SCOPE is nonzero, T is the context for a template which
9747 we are presently tsubst'ing. Return the substituted value. */
9748
9749 static tree
9750 tsubst_aggr_type (tree t,
9751 tree args,
9752 tsubst_flags_t complain,
9753 tree in_decl,
9754 int entering_scope)
9755 {
9756 if (t == NULL_TREE)
9757 return NULL_TREE;
9758
9759 switch (TREE_CODE (t))
9760 {
9761 case RECORD_TYPE:
9762 if (TYPE_PTRMEMFUNC_P (t))
9763 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9764
9765 /* Else fall through. */
9766 case ENUMERAL_TYPE:
9767 case UNION_TYPE:
9768 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9769 {
9770 tree argvec;
9771 tree context;
9772 tree r;
9773 int saved_unevaluated_operand;
9774 int saved_inhibit_evaluation_warnings;
9775
9776 /* In "sizeof(X<I>)" we need to evaluate "I". */
9777 saved_unevaluated_operand = cp_unevaluated_operand;
9778 cp_unevaluated_operand = 0;
9779 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9780 c_inhibit_evaluation_warnings = 0;
9781
9782 /* First, determine the context for the type we are looking
9783 up. */
9784 context = TYPE_CONTEXT (t);
9785 if (context && TYPE_P (context))
9786 {
9787 context = tsubst_aggr_type (context, args, complain,
9788 in_decl, /*entering_scope=*/1);
9789 /* If context is a nested class inside a class template,
9790 it may still need to be instantiated (c++/33959). */
9791 context = complete_type (context);
9792 }
9793
9794 /* Then, figure out what arguments are appropriate for the
9795 type we are trying to find. For example, given:
9796
9797 template <class T> struct S;
9798 template <class T, class U> void f(T, U) { S<U> su; }
9799
9800 and supposing that we are instantiating f<int, double>,
9801 then our ARGS will be {int, double}, but, when looking up
9802 S we only want {double}. */
9803 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9804 complain, in_decl);
9805 if (argvec == error_mark_node)
9806 r = error_mark_node;
9807 else
9808 {
9809 r = lookup_template_class (t, argvec, in_decl, context,
9810 entering_scope, complain);
9811 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9812 }
9813
9814 cp_unevaluated_operand = saved_unevaluated_operand;
9815 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9816
9817 return r;
9818 }
9819 else
9820 /* This is not a template type, so there's nothing to do. */
9821 return t;
9822
9823 default:
9824 return tsubst (t, args, complain, in_decl);
9825 }
9826 }
9827
9828 /* Substitute into the default argument ARG (a default argument for
9829 FN), which has the indicated TYPE. */
9830
9831 tree
9832 tsubst_default_argument (tree fn, tree type, tree arg)
9833 {
9834 tree saved_class_ptr = NULL_TREE;
9835 tree saved_class_ref = NULL_TREE;
9836 int errs = errorcount + sorrycount;
9837
9838 /* This can happen in invalid code. */
9839 if (TREE_CODE (arg) == DEFAULT_ARG)
9840 return arg;
9841
9842 /* This default argument came from a template. Instantiate the
9843 default argument here, not in tsubst. In the case of
9844 something like:
9845
9846 template <class T>
9847 struct S {
9848 static T t();
9849 void f(T = t());
9850 };
9851
9852 we must be careful to do name lookup in the scope of S<T>,
9853 rather than in the current class. */
9854 push_access_scope (fn);
9855 /* The "this" pointer is not valid in a default argument. */
9856 if (cfun)
9857 {
9858 saved_class_ptr = current_class_ptr;
9859 cp_function_chain->x_current_class_ptr = NULL_TREE;
9860 saved_class_ref = current_class_ref;
9861 cp_function_chain->x_current_class_ref = NULL_TREE;
9862 }
9863
9864 push_deferring_access_checks(dk_no_deferred);
9865 /* The default argument expression may cause implicitly defined
9866 member functions to be synthesized, which will result in garbage
9867 collection. We must treat this situation as if we were within
9868 the body of function so as to avoid collecting live data on the
9869 stack. */
9870 ++function_depth;
9871 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9872 tf_warning_or_error, NULL_TREE,
9873 /*integral_constant_expression_p=*/false);
9874 --function_depth;
9875 pop_deferring_access_checks();
9876
9877 /* Restore the "this" pointer. */
9878 if (cfun)
9879 {
9880 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9881 cp_function_chain->x_current_class_ref = saved_class_ref;
9882 }
9883
9884 if (errorcount+sorrycount > errs)
9885 inform (input_location,
9886 " when instantiating default argument for call to %D", fn);
9887
9888 /* Make sure the default argument is reasonable. */
9889 arg = check_default_argument (type, arg);
9890
9891 pop_access_scope (fn);
9892
9893 return arg;
9894 }
9895
9896 /* Substitute into all the default arguments for FN. */
9897
9898 static void
9899 tsubst_default_arguments (tree fn)
9900 {
9901 tree arg;
9902 tree tmpl_args;
9903
9904 tmpl_args = DECL_TI_ARGS (fn);
9905
9906 /* If this function is not yet instantiated, we certainly don't need
9907 its default arguments. */
9908 if (uses_template_parms (tmpl_args))
9909 return;
9910 /* Don't do this again for clones. */
9911 if (DECL_CLONED_FUNCTION_P (fn))
9912 return;
9913
9914 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9915 arg;
9916 arg = TREE_CHAIN (arg))
9917 if (TREE_PURPOSE (arg))
9918 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9919 TREE_VALUE (arg),
9920 TREE_PURPOSE (arg));
9921 }
9922
9923 /* Substitute the ARGS into the T, which is a _DECL. Return the
9924 result of the substitution. Issue error and warning messages under
9925 control of COMPLAIN. */
9926
9927 static tree
9928 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9929 {
9930 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9931 location_t saved_loc;
9932 tree r = NULL_TREE;
9933 tree in_decl = t;
9934 hashval_t hash = 0;
9935
9936 /* Set the filename and linenumber to improve error-reporting. */
9937 saved_loc = input_location;
9938 input_location = DECL_SOURCE_LOCATION (t);
9939
9940 switch (TREE_CODE (t))
9941 {
9942 case TEMPLATE_DECL:
9943 {
9944 /* We can get here when processing a member function template,
9945 member class template, or template template parameter. */
9946 tree decl = DECL_TEMPLATE_RESULT (t);
9947 tree spec;
9948 tree tmpl_args;
9949 tree full_args;
9950
9951 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9952 {
9953 /* Template template parameter is treated here. */
9954 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9955 if (new_type == error_mark_node)
9956 RETURN (error_mark_node);
9957 /* If we get a real template back, return it. This can happen in
9958 the context of most_specialized_class. */
9959 if (TREE_CODE (new_type) == TEMPLATE_DECL)
9960 return new_type;
9961
9962 r = copy_decl (t);
9963 DECL_CHAIN (r) = NULL_TREE;
9964 TREE_TYPE (r) = new_type;
9965 DECL_TEMPLATE_RESULT (r)
9966 = build_decl (DECL_SOURCE_LOCATION (decl),
9967 TYPE_DECL, DECL_NAME (decl), new_type);
9968 DECL_TEMPLATE_PARMS (r)
9969 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9970 complain);
9971 TYPE_NAME (new_type) = r;
9972 break;
9973 }
9974
9975 /* We might already have an instance of this template.
9976 The ARGS are for the surrounding class type, so the
9977 full args contain the tsubst'd args for the context,
9978 plus the innermost args from the template decl. */
9979 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9980 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9981 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9982 /* Because this is a template, the arguments will still be
9983 dependent, even after substitution. If
9984 PROCESSING_TEMPLATE_DECL is not set, the dependency
9985 predicates will short-circuit. */
9986 ++processing_template_decl;
9987 full_args = tsubst_template_args (tmpl_args, args,
9988 complain, in_decl);
9989 --processing_template_decl;
9990 if (full_args == error_mark_node)
9991 RETURN (error_mark_node);
9992
9993 /* If this is a default template template argument,
9994 tsubst might not have changed anything. */
9995 if (full_args == tmpl_args)
9996 RETURN (t);
9997
9998 hash = hash_tmpl_and_args (t, full_args);
9999 spec = retrieve_specialization (t, full_args, hash);
10000 if (spec != NULL_TREE)
10001 {
10002 r = spec;
10003 break;
10004 }
10005
10006 /* Make a new template decl. It will be similar to the
10007 original, but will record the current template arguments.
10008 We also create a new function declaration, which is just
10009 like the old one, but points to this new template, rather
10010 than the old one. */
10011 r = copy_decl (t);
10012 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10013 DECL_CHAIN (r) = NULL_TREE;
10014
10015 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10016
10017 if (TREE_CODE (decl) == TYPE_DECL
10018 && !TYPE_DECL_ALIAS_P (decl))
10019 {
10020 tree new_type;
10021 ++processing_template_decl;
10022 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10023 --processing_template_decl;
10024 if (new_type == error_mark_node)
10025 RETURN (error_mark_node);
10026
10027 TREE_TYPE (r) = new_type;
10028 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10029 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10030 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10031 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10032 }
10033 else
10034 {
10035 tree new_decl;
10036 ++processing_template_decl;
10037 new_decl = tsubst (decl, args, complain, in_decl);
10038 --processing_template_decl;
10039 if (new_decl == error_mark_node)
10040 RETURN (error_mark_node);
10041
10042 DECL_TEMPLATE_RESULT (r) = new_decl;
10043 DECL_TI_TEMPLATE (new_decl) = r;
10044 TREE_TYPE (r) = TREE_TYPE (new_decl);
10045 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10046 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10047 }
10048
10049 SET_DECL_IMPLICIT_INSTANTIATION (r);
10050 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10051 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10052
10053 /* The template parameters for this new template are all the
10054 template parameters for the old template, except the
10055 outermost level of parameters. */
10056 DECL_TEMPLATE_PARMS (r)
10057 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10058 complain);
10059
10060 if (PRIMARY_TEMPLATE_P (t))
10061 DECL_PRIMARY_TEMPLATE (r) = r;
10062
10063 if (TREE_CODE (decl) != TYPE_DECL)
10064 /* Record this non-type partial instantiation. */
10065 register_specialization (r, t,
10066 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10067 false, hash);
10068 }
10069 break;
10070
10071 case FUNCTION_DECL:
10072 {
10073 tree ctx;
10074 tree argvec = NULL_TREE;
10075 tree *friends;
10076 tree gen_tmpl;
10077 tree type;
10078 int member;
10079 int args_depth;
10080 int parms_depth;
10081
10082 /* Nobody should be tsubst'ing into non-template functions. */
10083 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10084
10085 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10086 {
10087 tree spec;
10088 bool dependent_p;
10089
10090 /* If T is not dependent, just return it. We have to
10091 increment PROCESSING_TEMPLATE_DECL because
10092 value_dependent_expression_p assumes that nothing is
10093 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10094 ++processing_template_decl;
10095 dependent_p = value_dependent_expression_p (t);
10096 --processing_template_decl;
10097 if (!dependent_p)
10098 RETURN (t);
10099
10100 /* Calculate the most general template of which R is a
10101 specialization, and the complete set of arguments used to
10102 specialize R. */
10103 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10104 argvec = tsubst_template_args (DECL_TI_ARGS
10105 (DECL_TEMPLATE_RESULT
10106 (DECL_TI_TEMPLATE (t))),
10107 args, complain, in_decl);
10108 if (argvec == error_mark_node)
10109 RETURN (error_mark_node);
10110
10111 /* Check to see if we already have this specialization. */
10112 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10113 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10114
10115 if (spec)
10116 {
10117 r = spec;
10118 break;
10119 }
10120
10121 /* We can see more levels of arguments than parameters if
10122 there was a specialization of a member template, like
10123 this:
10124
10125 template <class T> struct S { template <class U> void f(); }
10126 template <> template <class U> void S<int>::f(U);
10127
10128 Here, we'll be substituting into the specialization,
10129 because that's where we can find the code we actually
10130 want to generate, but we'll have enough arguments for
10131 the most general template.
10132
10133 We also deal with the peculiar case:
10134
10135 template <class T> struct S {
10136 template <class U> friend void f();
10137 };
10138 template <class U> void f() {}
10139 template S<int>;
10140 template void f<double>();
10141
10142 Here, the ARGS for the instantiation of will be {int,
10143 double}. But, we only need as many ARGS as there are
10144 levels of template parameters in CODE_PATTERN. We are
10145 careful not to get fooled into reducing the ARGS in
10146 situations like:
10147
10148 template <class T> struct S { template <class U> void f(U); }
10149 template <class T> template <> void S<T>::f(int) {}
10150
10151 which we can spot because the pattern will be a
10152 specialization in this case. */
10153 args_depth = TMPL_ARGS_DEPTH (args);
10154 parms_depth =
10155 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10156 if (args_depth > parms_depth
10157 && !DECL_TEMPLATE_SPECIALIZATION (t))
10158 args = get_innermost_template_args (args, parms_depth);
10159 }
10160 else
10161 {
10162 /* This special case arises when we have something like this:
10163
10164 template <class T> struct S {
10165 friend void f<int>(int, double);
10166 };
10167
10168 Here, the DECL_TI_TEMPLATE for the friend declaration
10169 will be an IDENTIFIER_NODE. We are being called from
10170 tsubst_friend_function, and we want only to create a
10171 new decl (R) with appropriate types so that we can call
10172 determine_specialization. */
10173 gen_tmpl = NULL_TREE;
10174 }
10175
10176 if (DECL_CLASS_SCOPE_P (t))
10177 {
10178 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10179 member = 2;
10180 else
10181 member = 1;
10182 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10183 complain, t, /*entering_scope=*/1);
10184 }
10185 else
10186 {
10187 member = 0;
10188 ctx = DECL_CONTEXT (t);
10189 }
10190 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10191 if (type == error_mark_node)
10192 RETURN (error_mark_node);
10193
10194 /* If we hit excessive deduction depth, the type is bogus even if
10195 it isn't error_mark_node, so don't build a decl. */
10196 if (excessive_deduction_depth)
10197 RETURN (error_mark_node);
10198
10199 /* We do NOT check for matching decls pushed separately at this
10200 point, as they may not represent instantiations of this
10201 template, and in any case are considered separate under the
10202 discrete model. */
10203 r = copy_decl (t);
10204 DECL_USE_TEMPLATE (r) = 0;
10205 TREE_TYPE (r) = type;
10206 /* Clear out the mangled name and RTL for the instantiation. */
10207 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10208 SET_DECL_RTL (r, NULL);
10209 /* Leave DECL_INITIAL set on deleted instantiations. */
10210 if (!DECL_DELETED_FN (r))
10211 DECL_INITIAL (r) = NULL_TREE;
10212 DECL_CONTEXT (r) = ctx;
10213
10214 if (member && DECL_CONV_FN_P (r))
10215 /* Type-conversion operator. Reconstruct the name, in
10216 case it's the name of one of the template's parameters. */
10217 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10218
10219 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10220 complain, t);
10221 DECL_RESULT (r) = NULL_TREE;
10222
10223 TREE_STATIC (r) = 0;
10224 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10225 DECL_EXTERNAL (r) = 1;
10226 /* If this is an instantiation of a function with internal
10227 linkage, we already know what object file linkage will be
10228 assigned to the instantiation. */
10229 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10230 DECL_DEFER_OUTPUT (r) = 0;
10231 DECL_CHAIN (r) = NULL_TREE;
10232 DECL_PENDING_INLINE_INFO (r) = 0;
10233 DECL_PENDING_INLINE_P (r) = 0;
10234 DECL_SAVED_TREE (r) = NULL_TREE;
10235 DECL_STRUCT_FUNCTION (r) = NULL;
10236 TREE_USED (r) = 0;
10237 /* We'll re-clone as appropriate in instantiate_template. */
10238 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10239
10240 /* If we aren't complaining now, return on error before we register
10241 the specialization so that we'll complain eventually. */
10242 if ((complain & tf_error) == 0
10243 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10244 && !grok_op_properties (r, /*complain=*/false))
10245 RETURN (error_mark_node);
10246
10247 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10248 this in the special friend case mentioned above where
10249 GEN_TMPL is NULL. */
10250 if (gen_tmpl)
10251 {
10252 DECL_TEMPLATE_INFO (r)
10253 = build_template_info (gen_tmpl, argvec);
10254 SET_DECL_IMPLICIT_INSTANTIATION (r);
10255 register_specialization (r, gen_tmpl, argvec, false, hash);
10256
10257 /* We're not supposed to instantiate default arguments
10258 until they are called, for a template. But, for a
10259 declaration like:
10260
10261 template <class T> void f ()
10262 { extern void g(int i = T()); }
10263
10264 we should do the substitution when the template is
10265 instantiated. We handle the member function case in
10266 instantiate_class_template since the default arguments
10267 might refer to other members of the class. */
10268 if (!member
10269 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10270 && !uses_template_parms (argvec))
10271 tsubst_default_arguments (r);
10272 }
10273 else
10274 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10275
10276 /* Copy the list of befriending classes. */
10277 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10278 *friends;
10279 friends = &TREE_CHAIN (*friends))
10280 {
10281 *friends = copy_node (*friends);
10282 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10283 args, complain,
10284 in_decl);
10285 }
10286
10287 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10288 {
10289 maybe_retrofit_in_chrg (r);
10290 if (DECL_CONSTRUCTOR_P (r))
10291 grok_ctor_properties (ctx, r);
10292 if (DECL_INHERITED_CTOR_BASE (r))
10293 deduce_inheriting_ctor (r);
10294 /* If this is an instantiation of a member template, clone it.
10295 If it isn't, that'll be handled by
10296 clone_constructors_and_destructors. */
10297 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10298 clone_function_decl (r, /*update_method_vec_p=*/0);
10299 }
10300 else if ((complain & tf_error) != 0
10301 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10302 && !grok_op_properties (r, /*complain=*/true))
10303 RETURN (error_mark_node);
10304
10305 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10306 SET_DECL_FRIEND_CONTEXT (r,
10307 tsubst (DECL_FRIEND_CONTEXT (t),
10308 args, complain, in_decl));
10309
10310 /* Possibly limit visibility based on template args. */
10311 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10312 if (DECL_VISIBILITY_SPECIFIED (t))
10313 {
10314 DECL_VISIBILITY_SPECIFIED (r) = 0;
10315 DECL_ATTRIBUTES (r)
10316 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10317 }
10318 determine_visibility (r);
10319 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10320 && !processing_template_decl)
10321 defaulted_late_check (r);
10322
10323 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10324 args, complain, in_decl);
10325 }
10326 break;
10327
10328 case PARM_DECL:
10329 {
10330 tree type = NULL_TREE;
10331 int i, len = 1;
10332 tree expanded_types = NULL_TREE;
10333 tree prev_r = NULL_TREE;
10334 tree first_r = NULL_TREE;
10335
10336 if (FUNCTION_PARAMETER_PACK_P (t))
10337 {
10338 /* If there is a local specialization that isn't a
10339 parameter pack, it means that we're doing a "simple"
10340 substitution from inside tsubst_pack_expansion. Just
10341 return the local specialization (which will be a single
10342 parm). */
10343 tree spec = retrieve_local_specialization (t);
10344 if (spec
10345 && TREE_CODE (spec) == PARM_DECL
10346 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10347 RETURN (spec);
10348
10349 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10350 the parameters in this function parameter pack. */
10351 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10352 complain, in_decl);
10353 if (TREE_CODE (expanded_types) == TREE_VEC)
10354 {
10355 len = TREE_VEC_LENGTH (expanded_types);
10356
10357 /* Zero-length parameter packs are boring. Just substitute
10358 into the chain. */
10359 if (len == 0)
10360 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10361 TREE_CHAIN (t)));
10362 }
10363 else
10364 {
10365 /* All we did was update the type. Make a note of that. */
10366 type = expanded_types;
10367 expanded_types = NULL_TREE;
10368 }
10369 }
10370
10371 /* Loop through all of the parameter's we'll build. When T is
10372 a function parameter pack, LEN is the number of expanded
10373 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10374 r = NULL_TREE;
10375 for (i = 0; i < len; ++i)
10376 {
10377 prev_r = r;
10378 r = copy_node (t);
10379 if (DECL_TEMPLATE_PARM_P (t))
10380 SET_DECL_TEMPLATE_PARM_P (r);
10381
10382 if (expanded_types)
10383 /* We're on the Ith parameter of the function parameter
10384 pack. */
10385 {
10386 /* An argument of a function parameter pack is not a parameter
10387 pack. */
10388 FUNCTION_PARAMETER_PACK_P (r) = false;
10389
10390 /* Get the Ith type. */
10391 type = TREE_VEC_ELT (expanded_types, i);
10392
10393 /* Rename the parameter to include the index. */
10394 DECL_NAME (r)
10395 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10396 }
10397 else if (!type)
10398 /* We're dealing with a normal parameter. */
10399 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10400
10401 type = type_decays_to (type);
10402 TREE_TYPE (r) = type;
10403 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10404
10405 if (DECL_INITIAL (r))
10406 {
10407 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10408 DECL_INITIAL (r) = TREE_TYPE (r);
10409 else
10410 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10411 complain, in_decl);
10412 }
10413
10414 DECL_CONTEXT (r) = NULL_TREE;
10415
10416 if (!DECL_TEMPLATE_PARM_P (r))
10417 DECL_ARG_TYPE (r) = type_passed_as (type);
10418
10419 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10420 args, complain, in_decl);
10421
10422 /* Keep track of the first new parameter we
10423 generate. That's what will be returned to the
10424 caller. */
10425 if (!first_r)
10426 first_r = r;
10427
10428 /* Build a proper chain of parameters when substituting
10429 into a function parameter pack. */
10430 if (prev_r)
10431 DECL_CHAIN (prev_r) = r;
10432 }
10433
10434 /* If cp_unevaluated_operand is set, we're just looking for a
10435 single dummy parameter, so don't keep going. */
10436 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10437 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10438 complain, DECL_CHAIN (t));
10439
10440 /* FIRST_R contains the start of the chain we've built. */
10441 r = first_r;
10442 }
10443 break;
10444
10445 case FIELD_DECL:
10446 {
10447 tree type;
10448
10449 r = copy_decl (t);
10450 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10451 if (type == error_mark_node)
10452 RETURN (error_mark_node);
10453 TREE_TYPE (r) = type;
10454 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10455
10456 if (DECL_C_BIT_FIELD (r))
10457 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10458 non-bit-fields DECL_INITIAL is a non-static data member
10459 initializer, which gets deferred instantiation. */
10460 DECL_INITIAL (r)
10461 = tsubst_expr (DECL_INITIAL (t), args,
10462 complain, in_decl,
10463 /*integral_constant_expression_p=*/true);
10464 else if (DECL_INITIAL (t))
10465 {
10466 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10467 NSDMI in perform_member_init. Still set DECL_INITIAL
10468 so that we know there is one. */
10469 DECL_INITIAL (r) = void_zero_node;
10470 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10471 retrofit_lang_decl (r);
10472 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10473 }
10474 /* We don't have to set DECL_CONTEXT here; it is set by
10475 finish_member_declaration. */
10476 DECL_CHAIN (r) = NULL_TREE;
10477 if (VOID_TYPE_P (type))
10478 error ("instantiation of %q+D as type %qT", r, type);
10479
10480 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10481 args, complain, in_decl);
10482 }
10483 break;
10484
10485 case USING_DECL:
10486 /* We reach here only for member using decls. We also need to check
10487 uses_template_parms because DECL_DEPENDENT_P is not set for a
10488 using-declaration that designates a member of the current
10489 instantiation (c++/53549). */
10490 if (DECL_DEPENDENT_P (t)
10491 || uses_template_parms (USING_DECL_SCOPE (t)))
10492 {
10493 tree scope = USING_DECL_SCOPE (t);
10494 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10495 complain, in_decl);
10496 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10497 /* Handle 'using T::T'. */
10498 if (TYPE_NAME (scope)
10499 && name == TYPE_IDENTIFIER (scope))
10500 name = TYPE_IDENTIFIER (inst_scope);
10501 r = do_class_using_decl (inst_scope, name);
10502 if (!r)
10503 r = error_mark_node;
10504 else
10505 {
10506 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10507 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10508 }
10509 }
10510 else
10511 {
10512 r = copy_node (t);
10513 DECL_CHAIN (r) = NULL_TREE;
10514 }
10515 break;
10516
10517 case TYPE_DECL:
10518 case VAR_DECL:
10519 {
10520 tree argvec = NULL_TREE;
10521 tree gen_tmpl = NULL_TREE;
10522 tree spec;
10523 tree tmpl = NULL_TREE;
10524 tree ctx;
10525 tree type = NULL_TREE;
10526 bool local_p;
10527
10528 if (TREE_CODE (t) == TYPE_DECL
10529 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10530 {
10531 /* If this is the canonical decl, we don't have to
10532 mess with instantiations, and often we can't (for
10533 typename, template type parms and such). Note that
10534 TYPE_NAME is not correct for the above test if
10535 we've copied the type for a typedef. */
10536 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10537 if (type == error_mark_node)
10538 RETURN (error_mark_node);
10539 r = TYPE_NAME (type);
10540 break;
10541 }
10542
10543 /* Check to see if we already have the specialization we
10544 need. */
10545 spec = NULL_TREE;
10546 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10547 {
10548 /* T is a static data member or namespace-scope entity.
10549 We have to substitute into namespace-scope variables
10550 (even though such entities are never templates) because
10551 of cases like:
10552
10553 template <class T> void f() { extern T t; }
10554
10555 where the entity referenced is not known until
10556 instantiation time. */
10557 local_p = false;
10558 ctx = DECL_CONTEXT (t);
10559 if (DECL_CLASS_SCOPE_P (t))
10560 {
10561 ctx = tsubst_aggr_type (ctx, args,
10562 complain,
10563 in_decl, /*entering_scope=*/1);
10564 /* If CTX is unchanged, then T is in fact the
10565 specialization we want. That situation occurs when
10566 referencing a static data member within in its own
10567 class. We can use pointer equality, rather than
10568 same_type_p, because DECL_CONTEXT is always
10569 canonical... */
10570 if (ctx == DECL_CONTEXT (t)
10571 && (TREE_CODE (t) != TYPE_DECL
10572 /* ... unless T is a member template; in which
10573 case our caller can be willing to create a
10574 specialization of that template represented
10575 by T. */
10576 || !(DECL_TI_TEMPLATE (t)
10577 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10578 spec = t;
10579 }
10580
10581 if (!spec)
10582 {
10583 tmpl = DECL_TI_TEMPLATE (t);
10584 gen_tmpl = most_general_template (tmpl);
10585 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10586 if (argvec == error_mark_node)
10587 RETURN (error_mark_node);
10588 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10589 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10590 }
10591 }
10592 else
10593 {
10594 /* A local variable. */
10595 local_p = true;
10596 /* Subsequent calls to pushdecl will fill this in. */
10597 ctx = NULL_TREE;
10598 spec = retrieve_local_specialization (t);
10599 }
10600 /* If we already have the specialization we need, there is
10601 nothing more to do. */
10602 if (spec)
10603 {
10604 r = spec;
10605 break;
10606 }
10607
10608 if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t))
10609 {
10610 /* Just use name lookup to find a member alias for an anonymous
10611 union, but then add it to the hash table. */
10612 r = lookup_name (DECL_NAME (t));
10613 gcc_assert (DECL_ANON_UNION_VAR_P (r));
10614 register_local_specialization (r, t);
10615 break;
10616 }
10617
10618 /* Create a new node for the specialization we need. */
10619 r = copy_decl (t);
10620 if (type == NULL_TREE)
10621 {
10622 if (is_typedef_decl (t))
10623 type = DECL_ORIGINAL_TYPE (t);
10624 else
10625 type = TREE_TYPE (t);
10626 if (TREE_CODE (t) == VAR_DECL
10627 && VAR_HAD_UNKNOWN_BOUND (t)
10628 && type != error_mark_node)
10629 type = strip_array_domain (type);
10630 type = tsubst (type, args, complain, in_decl);
10631 }
10632 if (TREE_CODE (r) == VAR_DECL)
10633 {
10634 /* Even if the original location is out of scope, the
10635 newly substituted one is not. */
10636 DECL_DEAD_FOR_LOCAL (r) = 0;
10637 DECL_INITIALIZED_P (r) = 0;
10638 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10639 if (type == error_mark_node)
10640 RETURN (error_mark_node);
10641 if (TREE_CODE (type) == FUNCTION_TYPE)
10642 {
10643 /* It may seem that this case cannot occur, since:
10644
10645 typedef void f();
10646 void g() { f x; }
10647
10648 declares a function, not a variable. However:
10649
10650 typedef void f();
10651 template <typename T> void g() { T t; }
10652 template void g<f>();
10653
10654 is an attempt to declare a variable with function
10655 type. */
10656 error ("variable %qD has function type",
10657 /* R is not yet sufficiently initialized, so we
10658 just use its name. */
10659 DECL_NAME (r));
10660 RETURN (error_mark_node);
10661 }
10662 type = complete_type (type);
10663 /* Wait until cp_finish_decl to set this again, to handle
10664 circular dependency (template/instantiate6.C). */
10665 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10666 type = check_var_type (DECL_NAME (r), type);
10667
10668 if (DECL_HAS_VALUE_EXPR_P (t))
10669 {
10670 tree ve = DECL_VALUE_EXPR (t);
10671 ve = tsubst_expr (ve, args, complain, in_decl,
10672 /*constant_expression_p=*/false);
10673 if (REFERENCE_REF_P (ve))
10674 {
10675 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10676 ve = TREE_OPERAND (ve, 0);
10677 }
10678 SET_DECL_VALUE_EXPR (r, ve);
10679 }
10680 }
10681 else if (DECL_SELF_REFERENCE_P (t))
10682 SET_DECL_SELF_REFERENCE_P (r);
10683 TREE_TYPE (r) = type;
10684 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10685 DECL_CONTEXT (r) = ctx;
10686 /* Clear out the mangled name and RTL for the instantiation. */
10687 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10688 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10689 SET_DECL_RTL (r, NULL);
10690 /* The initializer must not be expanded until it is required;
10691 see [temp.inst]. */
10692 DECL_INITIAL (r) = NULL_TREE;
10693 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10694 SET_DECL_RTL (r, NULL);
10695 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10696 if (TREE_CODE (r) == VAR_DECL)
10697 {
10698 /* Possibly limit visibility based on template args. */
10699 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10700 if (DECL_VISIBILITY_SPECIFIED (t))
10701 {
10702 DECL_VISIBILITY_SPECIFIED (r) = 0;
10703 DECL_ATTRIBUTES (r)
10704 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10705 }
10706 determine_visibility (r);
10707 }
10708
10709 if (!local_p)
10710 {
10711 /* A static data member declaration is always marked
10712 external when it is declared in-class, even if an
10713 initializer is present. We mimic the non-template
10714 processing here. */
10715 DECL_EXTERNAL (r) = 1;
10716
10717 register_specialization (r, gen_tmpl, argvec, false, hash);
10718 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10719 SET_DECL_IMPLICIT_INSTANTIATION (r);
10720 }
10721 else if (cp_unevaluated_operand)
10722 {
10723 /* We're substituting this var in a decltype outside of its
10724 scope, such as for a lambda return type. Don't add it to
10725 local_specializations, do perform auto deduction. */
10726 tree auto_node = type_uses_auto (type);
10727 if (auto_node)
10728 {
10729 tree init
10730 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10731 /*constant_expression_p=*/false);
10732 init = resolve_nondeduced_context (init);
10733 TREE_TYPE (r) = type
10734 = do_auto_deduction (type, init, auto_node);
10735 }
10736 }
10737 else
10738 register_local_specialization (r, t);
10739
10740 DECL_CHAIN (r) = NULL_TREE;
10741
10742 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10743 /*flags=*/0,
10744 args, complain, in_decl);
10745
10746 /* Preserve a typedef that names a type. */
10747 if (is_typedef_decl (r))
10748 {
10749 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10750 set_underlying_type (r);
10751 }
10752
10753 layout_decl (r, 0);
10754 }
10755 break;
10756
10757 default:
10758 gcc_unreachable ();
10759 }
10760 #undef RETURN
10761
10762 out:
10763 /* Restore the file and line information. */
10764 input_location = saved_loc;
10765
10766 return r;
10767 }
10768
10769 /* Substitute into the ARG_TYPES of a function type.
10770 If END is a TREE_CHAIN, leave it and any following types
10771 un-substituted. */
10772
10773 static tree
10774 tsubst_arg_types (tree arg_types,
10775 tree args,
10776 tree end,
10777 tsubst_flags_t complain,
10778 tree in_decl)
10779 {
10780 tree remaining_arg_types;
10781 tree type = NULL_TREE;
10782 int i = 1;
10783 tree expanded_args = NULL_TREE;
10784 tree default_arg;
10785
10786 if (!arg_types || arg_types == void_list_node || arg_types == end)
10787 return arg_types;
10788
10789 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10790 args, end, complain, in_decl);
10791 if (remaining_arg_types == error_mark_node)
10792 return error_mark_node;
10793
10794 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10795 {
10796 /* For a pack expansion, perform substitution on the
10797 entire expression. Later on, we'll handle the arguments
10798 one-by-one. */
10799 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10800 args, complain, in_decl);
10801
10802 if (TREE_CODE (expanded_args) == TREE_VEC)
10803 /* So that we'll spin through the parameters, one by one. */
10804 i = TREE_VEC_LENGTH (expanded_args);
10805 else
10806 {
10807 /* We only partially substituted into the parameter
10808 pack. Our type is TYPE_PACK_EXPANSION. */
10809 type = expanded_args;
10810 expanded_args = NULL_TREE;
10811 }
10812 }
10813
10814 while (i > 0) {
10815 --i;
10816
10817 if (expanded_args)
10818 type = TREE_VEC_ELT (expanded_args, i);
10819 else if (!type)
10820 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10821
10822 if (type == error_mark_node)
10823 return error_mark_node;
10824 if (VOID_TYPE_P (type))
10825 {
10826 if (complain & tf_error)
10827 {
10828 error ("invalid parameter type %qT", type);
10829 if (in_decl)
10830 error ("in declaration %q+D", in_decl);
10831 }
10832 return error_mark_node;
10833 }
10834
10835 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10836 top-level qualifiers as required. */
10837 type = cv_unqualified (type_decays_to (type));
10838
10839 /* We do not substitute into default arguments here. The standard
10840 mandates that they be instantiated only when needed, which is
10841 done in build_over_call. */
10842 default_arg = TREE_PURPOSE (arg_types);
10843
10844 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10845 {
10846 /* We've instantiated a template before its default arguments
10847 have been parsed. This can happen for a nested template
10848 class, and is not an error unless we require the default
10849 argument in a call of this function. */
10850 remaining_arg_types =
10851 tree_cons (default_arg, type, remaining_arg_types);
10852 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
10853 }
10854 else
10855 remaining_arg_types =
10856 hash_tree_cons (default_arg, type, remaining_arg_types);
10857 }
10858
10859 return remaining_arg_types;
10860 }
10861
10862 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10863 *not* handle the exception-specification for FNTYPE, because the
10864 initial substitution of explicitly provided template parameters
10865 during argument deduction forbids substitution into the
10866 exception-specification:
10867
10868 [temp.deduct]
10869
10870 All references in the function type of the function template to the
10871 corresponding template parameters are replaced by the specified tem-
10872 plate argument values. If a substitution in a template parameter or
10873 in the function type of the function template results in an invalid
10874 type, type deduction fails. [Note: The equivalent substitution in
10875 exception specifications is done only when the function is instanti-
10876 ated, at which point a program is ill-formed if the substitution
10877 results in an invalid type.] */
10878
10879 static tree
10880 tsubst_function_type (tree t,
10881 tree args,
10882 tsubst_flags_t complain,
10883 tree in_decl)
10884 {
10885 tree return_type;
10886 tree arg_types;
10887 tree fntype;
10888
10889 /* The TYPE_CONTEXT is not used for function/method types. */
10890 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10891
10892 /* Substitute the return type. */
10893 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10894 if (return_type == error_mark_node)
10895 return error_mark_node;
10896 /* The standard does not presently indicate that creation of a
10897 function type with an invalid return type is a deduction failure.
10898 However, that is clearly analogous to creating an array of "void"
10899 or a reference to a reference. This is core issue #486. */
10900 if (TREE_CODE (return_type) == ARRAY_TYPE
10901 || TREE_CODE (return_type) == FUNCTION_TYPE)
10902 {
10903 if (complain & tf_error)
10904 {
10905 if (TREE_CODE (return_type) == ARRAY_TYPE)
10906 error ("function returning an array");
10907 else
10908 error ("function returning a function");
10909 }
10910 return error_mark_node;
10911 }
10912
10913 /* Substitute the argument types. */
10914 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10915 complain, in_decl);
10916 if (arg_types == error_mark_node)
10917 return error_mark_node;
10918
10919 /* Construct a new type node and return it. */
10920 if (TREE_CODE (t) == FUNCTION_TYPE)
10921 {
10922 fntype = build_function_type (return_type, arg_types);
10923 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10924 }
10925 else
10926 {
10927 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10928 if (! MAYBE_CLASS_TYPE_P (r))
10929 {
10930 /* [temp.deduct]
10931
10932 Type deduction may fail for any of the following
10933 reasons:
10934
10935 -- Attempting to create "pointer to member of T" when T
10936 is not a class type. */
10937 if (complain & tf_error)
10938 error ("creating pointer to member function of non-class type %qT",
10939 r);
10940 return error_mark_node;
10941 }
10942
10943 fntype = build_method_type_directly (r, return_type,
10944 TREE_CHAIN (arg_types));
10945 }
10946 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10947
10948 return fntype;
10949 }
10950
10951 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10952 ARGS into that specification, and return the substituted
10953 specification. If there is no specification, return NULL_TREE. */
10954
10955 static tree
10956 tsubst_exception_specification (tree fntype,
10957 tree args,
10958 tsubst_flags_t complain,
10959 tree in_decl,
10960 bool defer_ok)
10961 {
10962 tree specs;
10963 tree new_specs;
10964
10965 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10966 new_specs = NULL_TREE;
10967 if (specs && TREE_PURPOSE (specs))
10968 {
10969 /* A noexcept-specifier. */
10970 tree expr = TREE_PURPOSE (specs);
10971 if (TREE_CODE (expr) == INTEGER_CST)
10972 new_specs = expr;
10973 else if (defer_ok)
10974 {
10975 /* Defer instantiation of noexcept-specifiers to avoid
10976 excessive instantiations (c++/49107). */
10977 new_specs = make_node (DEFERRED_NOEXCEPT);
10978 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10979 {
10980 /* We already partially instantiated this member template,
10981 so combine the new args with the old. */
10982 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10983 = DEFERRED_NOEXCEPT_PATTERN (expr);
10984 DEFERRED_NOEXCEPT_ARGS (new_specs)
10985 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10986 }
10987 else
10988 {
10989 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10990 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10991 }
10992 }
10993 else
10994 new_specs = tsubst_copy_and_build
10995 (expr, args, complain, in_decl, /*function_p=*/false,
10996 /*integral_constant_expression_p=*/true);
10997 new_specs = build_noexcept_spec (new_specs, complain);
10998 }
10999 else if (specs)
11000 {
11001 if (! TREE_VALUE (specs))
11002 new_specs = specs;
11003 else
11004 while (specs)
11005 {
11006 tree spec;
11007 int i, len = 1;
11008 tree expanded_specs = NULL_TREE;
11009
11010 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11011 {
11012 /* Expand the pack expansion type. */
11013 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11014 args, complain,
11015 in_decl);
11016
11017 if (expanded_specs == error_mark_node)
11018 return error_mark_node;
11019 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11020 len = TREE_VEC_LENGTH (expanded_specs);
11021 else
11022 {
11023 /* We're substituting into a member template, so
11024 we got a TYPE_PACK_EXPANSION back. Add that
11025 expansion and move on. */
11026 gcc_assert (TREE_CODE (expanded_specs)
11027 == TYPE_PACK_EXPANSION);
11028 new_specs = add_exception_specifier (new_specs,
11029 expanded_specs,
11030 complain);
11031 specs = TREE_CHAIN (specs);
11032 continue;
11033 }
11034 }
11035
11036 for (i = 0; i < len; ++i)
11037 {
11038 if (expanded_specs)
11039 spec = TREE_VEC_ELT (expanded_specs, i);
11040 else
11041 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11042 if (spec == error_mark_node)
11043 return spec;
11044 new_specs = add_exception_specifier (new_specs, spec,
11045 complain);
11046 }
11047
11048 specs = TREE_CHAIN (specs);
11049 }
11050 }
11051 return new_specs;
11052 }
11053
11054 /* Take the tree structure T and replace template parameters used
11055 therein with the argument vector ARGS. IN_DECL is an associated
11056 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11057 Issue error and warning messages under control of COMPLAIN. Note
11058 that we must be relatively non-tolerant of extensions here, in
11059 order to preserve conformance; if we allow substitutions that
11060 should not be allowed, we may allow argument deductions that should
11061 not succeed, and therefore report ambiguous overload situations
11062 where there are none. In theory, we could allow the substitution,
11063 but indicate that it should have failed, and allow our caller to
11064 make sure that the right thing happens, but we don't try to do this
11065 yet.
11066
11067 This function is used for dealing with types, decls and the like;
11068 for expressions, use tsubst_expr or tsubst_copy. */
11069
11070 tree
11071 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11072 {
11073 enum tree_code code;
11074 tree type, r = NULL_TREE;
11075
11076 if (t == NULL_TREE || t == error_mark_node
11077 || t == integer_type_node
11078 || t == void_type_node
11079 || t == char_type_node
11080 || t == unknown_type_node
11081 || TREE_CODE (t) == NAMESPACE_DECL
11082 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11083 return t;
11084
11085 if (DECL_P (t))
11086 return tsubst_decl (t, args, complain);
11087
11088 if (args == NULL_TREE)
11089 return t;
11090
11091 code = TREE_CODE (t);
11092
11093 if (code == IDENTIFIER_NODE)
11094 type = IDENTIFIER_TYPE_VALUE (t);
11095 else
11096 type = TREE_TYPE (t);
11097
11098 gcc_assert (type != unknown_type_node);
11099
11100 /* Reuse typedefs. We need to do this to handle dependent attributes,
11101 such as attribute aligned. */
11102 if (TYPE_P (t)
11103 && typedef_variant_p (t))
11104 {
11105 tree decl = TYPE_NAME (t);
11106
11107 if (alias_template_specialization_p (t))
11108 {
11109 /* DECL represents an alias template and we want to
11110 instantiate it. */
11111 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11112 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11113 r = instantiate_alias_template (tmpl, gen_args, complain);
11114 }
11115 else if (DECL_CLASS_SCOPE_P (decl)
11116 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11117 && uses_template_parms (DECL_CONTEXT (decl)))
11118 {
11119 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11120 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11121 r = retrieve_specialization (tmpl, gen_args, 0);
11122 }
11123 else if (DECL_FUNCTION_SCOPE_P (decl)
11124 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11125 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11126 r = retrieve_local_specialization (decl);
11127 else
11128 /* The typedef is from a non-template context. */
11129 return t;
11130
11131 if (r)
11132 {
11133 r = TREE_TYPE (r);
11134 r = cp_build_qualified_type_real
11135 (r, cp_type_quals (t) | cp_type_quals (r),
11136 complain | tf_ignore_bad_quals);
11137 return r;
11138 }
11139 else
11140 {
11141 /* We don't have an instantiation yet, so drop the typedef. */
11142 int quals = cp_type_quals (t);
11143 t = DECL_ORIGINAL_TYPE (decl);
11144 t = cp_build_qualified_type_real (t, quals,
11145 complain | tf_ignore_bad_quals);
11146 }
11147 }
11148
11149 if (type
11150 && code != TYPENAME_TYPE
11151 && code != TEMPLATE_TYPE_PARM
11152 && code != IDENTIFIER_NODE
11153 && code != FUNCTION_TYPE
11154 && code != METHOD_TYPE)
11155 type = tsubst (type, args, complain, in_decl);
11156 if (type == error_mark_node)
11157 return error_mark_node;
11158
11159 switch (code)
11160 {
11161 case RECORD_TYPE:
11162 case UNION_TYPE:
11163 case ENUMERAL_TYPE:
11164 return tsubst_aggr_type (t, args, complain, in_decl,
11165 /*entering_scope=*/0);
11166
11167 case ERROR_MARK:
11168 case IDENTIFIER_NODE:
11169 case VOID_TYPE:
11170 case REAL_TYPE:
11171 case COMPLEX_TYPE:
11172 case VECTOR_TYPE:
11173 case BOOLEAN_TYPE:
11174 case NULLPTR_TYPE:
11175 case LANG_TYPE:
11176 return t;
11177
11178 case INTEGER_TYPE:
11179 if (t == integer_type_node)
11180 return t;
11181
11182 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11183 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11184 return t;
11185
11186 {
11187 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11188
11189 max = tsubst_expr (omax, args, complain, in_decl,
11190 /*integral_constant_expression_p=*/false);
11191
11192 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11193 needed. */
11194 if (TREE_CODE (max) == NOP_EXPR
11195 && TREE_SIDE_EFFECTS (omax)
11196 && !TREE_TYPE (max))
11197 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11198
11199 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11200 with TREE_SIDE_EFFECTS that indicates this is not an integral
11201 constant expression. */
11202 if (processing_template_decl
11203 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11204 {
11205 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11206 TREE_SIDE_EFFECTS (max) = 1;
11207 }
11208
11209 return compute_array_index_type (NULL_TREE, max, complain);
11210 }
11211
11212 case TEMPLATE_TYPE_PARM:
11213 case TEMPLATE_TEMPLATE_PARM:
11214 case BOUND_TEMPLATE_TEMPLATE_PARM:
11215 case TEMPLATE_PARM_INDEX:
11216 {
11217 int idx;
11218 int level;
11219 int levels;
11220 tree arg = NULL_TREE;
11221
11222 r = NULL_TREE;
11223
11224 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11225 template_parm_level_and_index (t, &level, &idx);
11226
11227 levels = TMPL_ARGS_DEPTH (args);
11228 if (level <= levels)
11229 {
11230 arg = TMPL_ARG (args, level, idx);
11231
11232 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11233 {
11234 /* See through ARGUMENT_PACK_SELECT arguments. */
11235 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11236 /* If the selected argument is an expansion E, that most
11237 likely means we were called from
11238 gen_elem_of_pack_expansion_instantiation during the
11239 substituting of pack an argument pack (which Ith
11240 element is a pack expansion, where I is
11241 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11242 In this case, the Ith element resulting from this
11243 substituting is going to be a pack expansion, which
11244 pattern is the pattern of E. Let's return the
11245 pattern of E, and
11246 gen_elem_of_pack_expansion_instantiation will
11247 build the resulting pack expansion from it. */
11248 if (PACK_EXPANSION_P (arg))
11249 arg = PACK_EXPANSION_PATTERN (arg);
11250 }
11251 }
11252
11253 if (arg == error_mark_node)
11254 return error_mark_node;
11255 else if (arg != NULL_TREE)
11256 {
11257 if (ARGUMENT_PACK_P (arg))
11258 /* If ARG is an argument pack, we don't actually want to
11259 perform a substitution here, because substitutions
11260 for argument packs are only done
11261 element-by-element. We can get to this point when
11262 substituting the type of a non-type template
11263 parameter pack, when that type actually contains
11264 template parameter packs from an outer template, e.g.,
11265
11266 template<typename... Types> struct A {
11267 template<Types... Values> struct B { };
11268 }; */
11269 return t;
11270
11271 if (code == TEMPLATE_TYPE_PARM)
11272 {
11273 int quals;
11274 gcc_assert (TYPE_P (arg));
11275
11276 quals = cp_type_quals (arg) | cp_type_quals (t);
11277
11278 return cp_build_qualified_type_real
11279 (arg, quals, complain | tf_ignore_bad_quals);
11280 }
11281 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11282 {
11283 /* We are processing a type constructed from a
11284 template template parameter. */
11285 tree argvec = tsubst (TYPE_TI_ARGS (t),
11286 args, complain, in_decl);
11287 if (argvec == error_mark_node)
11288 return error_mark_node;
11289
11290 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11291 || TREE_CODE (arg) == TEMPLATE_DECL
11292 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11293
11294 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11295 /* Consider this code:
11296
11297 template <template <class> class Template>
11298 struct Internal {
11299 template <class Arg> using Bind = Template<Arg>;
11300 };
11301
11302 template <template <class> class Template, class Arg>
11303 using Instantiate = Template<Arg>; //#0
11304
11305 template <template <class> class Template,
11306 class Argument>
11307 using Bind =
11308 Instantiate<Internal<Template>::template Bind,
11309 Argument>; //#1
11310
11311 When #1 is parsed, the
11312 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11313 parameter `Template' in #0 matches the
11314 UNBOUND_CLASS_TEMPLATE representing the argument
11315 `Internal<Template>::template Bind'; We then want
11316 to assemble the type `Bind<Argument>' that can't
11317 be fully created right now, because
11318 `Internal<Template>' not being complete, the Bind
11319 template cannot be looked up in that context. So
11320 we need to "store" `Bind<Argument>' for later
11321 when the context of Bind becomes complete. Let's
11322 store that in a TYPENAME_TYPE. */
11323 return make_typename_type (TYPE_CONTEXT (arg),
11324 build_nt (TEMPLATE_ID_EXPR,
11325 TYPE_IDENTIFIER (arg),
11326 argvec),
11327 typename_type,
11328 complain);
11329
11330 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11331 are resolving nested-types in the signature of a
11332 member function templates. Otherwise ARG is a
11333 TEMPLATE_DECL and is the real template to be
11334 instantiated. */
11335 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11336 arg = TYPE_NAME (arg);
11337
11338 r = lookup_template_class (arg,
11339 argvec, in_decl,
11340 DECL_CONTEXT (arg),
11341 /*entering_scope=*/0,
11342 complain);
11343 return cp_build_qualified_type_real
11344 (r, cp_type_quals (t), complain);
11345 }
11346 else
11347 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11348 return convert_from_reference (unshare_expr (arg));
11349 }
11350
11351 if (level == 1)
11352 /* This can happen during the attempted tsubst'ing in
11353 unify. This means that we don't yet have any information
11354 about the template parameter in question. */
11355 return t;
11356
11357 /* Early in template argument deduction substitution, we don't
11358 want to reduce the level of 'auto', or it will be confused
11359 with a normal template parm in subsequent deduction. */
11360 if (is_auto (t) && (complain & tf_partial))
11361 return t;
11362
11363 /* If we get here, we must have been looking at a parm for a
11364 more deeply nested template. Make a new version of this
11365 template parameter, but with a lower level. */
11366 switch (code)
11367 {
11368 case TEMPLATE_TYPE_PARM:
11369 case TEMPLATE_TEMPLATE_PARM:
11370 case BOUND_TEMPLATE_TEMPLATE_PARM:
11371 if (cp_type_quals (t))
11372 {
11373 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11374 r = cp_build_qualified_type_real
11375 (r, cp_type_quals (t),
11376 complain | (code == TEMPLATE_TYPE_PARM
11377 ? tf_ignore_bad_quals : 0));
11378 }
11379 else
11380 {
11381 r = copy_type (t);
11382 TEMPLATE_TYPE_PARM_INDEX (r)
11383 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11384 r, levels, args, complain);
11385 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11386 TYPE_MAIN_VARIANT (r) = r;
11387 TYPE_POINTER_TO (r) = NULL_TREE;
11388 TYPE_REFERENCE_TO (r) = NULL_TREE;
11389
11390 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11391 /* We have reduced the level of the template
11392 template parameter, but not the levels of its
11393 template parameters, so canonical_type_parameter
11394 will not be able to find the canonical template
11395 template parameter for this level. Thus, we
11396 require structural equality checking to compare
11397 TEMPLATE_TEMPLATE_PARMs. */
11398 SET_TYPE_STRUCTURAL_EQUALITY (r);
11399 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11400 SET_TYPE_STRUCTURAL_EQUALITY (r);
11401 else
11402 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11403
11404 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11405 {
11406 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11407 complain, in_decl);
11408 if (argvec == error_mark_node)
11409 return error_mark_node;
11410
11411 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11412 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11413 }
11414 }
11415 break;
11416
11417 case TEMPLATE_PARM_INDEX:
11418 r = reduce_template_parm_level (t, type, levels, args, complain);
11419 break;
11420
11421 default:
11422 gcc_unreachable ();
11423 }
11424
11425 return r;
11426 }
11427
11428 case TREE_LIST:
11429 {
11430 tree purpose, value, chain;
11431
11432 if (t == void_list_node)
11433 return t;
11434
11435 purpose = TREE_PURPOSE (t);
11436 if (purpose)
11437 {
11438 purpose = tsubst (purpose, args, complain, in_decl);
11439 if (purpose == error_mark_node)
11440 return error_mark_node;
11441 }
11442 value = TREE_VALUE (t);
11443 if (value)
11444 {
11445 value = tsubst (value, args, complain, in_decl);
11446 if (value == error_mark_node)
11447 return error_mark_node;
11448 }
11449 chain = TREE_CHAIN (t);
11450 if (chain && chain != void_type_node)
11451 {
11452 chain = tsubst (chain, args, complain, in_decl);
11453 if (chain == error_mark_node)
11454 return error_mark_node;
11455 }
11456 if (purpose == TREE_PURPOSE (t)
11457 && value == TREE_VALUE (t)
11458 && chain == TREE_CHAIN (t))
11459 return t;
11460 return hash_tree_cons (purpose, value, chain);
11461 }
11462
11463 case TREE_BINFO:
11464 /* We should never be tsubsting a binfo. */
11465 gcc_unreachable ();
11466
11467 case TREE_VEC:
11468 /* A vector of template arguments. */
11469 gcc_assert (!type);
11470 return tsubst_template_args (t, args, complain, in_decl);
11471
11472 case POINTER_TYPE:
11473 case REFERENCE_TYPE:
11474 {
11475 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11476 return t;
11477
11478 /* [temp.deduct]
11479
11480 Type deduction may fail for any of the following
11481 reasons:
11482
11483 -- Attempting to create a pointer to reference type.
11484 -- Attempting to create a reference to a reference type or
11485 a reference to void.
11486
11487 Core issue 106 says that creating a reference to a reference
11488 during instantiation is no longer a cause for failure. We
11489 only enforce this check in strict C++98 mode. */
11490 if ((TREE_CODE (type) == REFERENCE_TYPE
11491 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11492 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11493 {
11494 static location_t last_loc;
11495
11496 /* We keep track of the last time we issued this error
11497 message to avoid spewing a ton of messages during a
11498 single bad template instantiation. */
11499 if (complain & tf_error
11500 && last_loc != input_location)
11501 {
11502 if (TREE_CODE (type) == VOID_TYPE)
11503 error ("forming reference to void");
11504 else if (code == POINTER_TYPE)
11505 error ("forming pointer to reference type %qT", type);
11506 else
11507 error ("forming reference to reference type %qT", type);
11508 last_loc = input_location;
11509 }
11510
11511 return error_mark_node;
11512 }
11513 else if (code == POINTER_TYPE)
11514 {
11515 r = build_pointer_type (type);
11516 if (TREE_CODE (type) == METHOD_TYPE)
11517 r = build_ptrmemfunc_type (r);
11518 }
11519 else if (TREE_CODE (type) == REFERENCE_TYPE)
11520 /* In C++0x, during template argument substitution, when there is an
11521 attempt to create a reference to a reference type, reference
11522 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11523
11524 "If a template-argument for a template-parameter T names a type
11525 that is a reference to a type A, an attempt to create the type
11526 'lvalue reference to cv T' creates the type 'lvalue reference to
11527 A,' while an attempt to create the type type rvalue reference to
11528 cv T' creates the type T"
11529 */
11530 r = cp_build_reference_type
11531 (TREE_TYPE (type),
11532 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11533 else
11534 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11535 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11536
11537 if (r != error_mark_node)
11538 /* Will this ever be needed for TYPE_..._TO values? */
11539 layout_type (r);
11540
11541 return r;
11542 }
11543 case OFFSET_TYPE:
11544 {
11545 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11546 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11547 {
11548 /* [temp.deduct]
11549
11550 Type deduction may fail for any of the following
11551 reasons:
11552
11553 -- Attempting to create "pointer to member of T" when T
11554 is not a class type. */
11555 if (complain & tf_error)
11556 error ("creating pointer to member of non-class type %qT", r);
11557 return error_mark_node;
11558 }
11559 if (TREE_CODE (type) == REFERENCE_TYPE)
11560 {
11561 if (complain & tf_error)
11562 error ("creating pointer to member reference type %qT", type);
11563 return error_mark_node;
11564 }
11565 if (TREE_CODE (type) == VOID_TYPE)
11566 {
11567 if (complain & tf_error)
11568 error ("creating pointer to member of type void");
11569 return error_mark_node;
11570 }
11571 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11572 if (TREE_CODE (type) == FUNCTION_TYPE)
11573 {
11574 /* The type of the implicit object parameter gets its
11575 cv-qualifiers from the FUNCTION_TYPE. */
11576 tree memptr;
11577 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11578 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11579 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11580 complain);
11581 }
11582 else
11583 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11584 cp_type_quals (t),
11585 complain);
11586 }
11587 case FUNCTION_TYPE:
11588 case METHOD_TYPE:
11589 {
11590 tree fntype;
11591 tree specs;
11592 fntype = tsubst_function_type (t, args, complain, in_decl);
11593 if (fntype == error_mark_node)
11594 return error_mark_node;
11595
11596 /* Substitute the exception specification. */
11597 specs = tsubst_exception_specification (t, args, complain,
11598 in_decl, /*defer_ok*/true);
11599 if (specs == error_mark_node)
11600 return error_mark_node;
11601 if (specs)
11602 fntype = build_exception_variant (fntype, specs);
11603 return fntype;
11604 }
11605 case ARRAY_TYPE:
11606 {
11607 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11608 if (domain == error_mark_node)
11609 return error_mark_node;
11610
11611 /* As an optimization, we avoid regenerating the array type if
11612 it will obviously be the same as T. */
11613 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11614 return t;
11615
11616 /* These checks should match the ones in grokdeclarator.
11617
11618 [temp.deduct]
11619
11620 The deduction may fail for any of the following reasons:
11621
11622 -- Attempting to create an array with an element type that
11623 is void, a function type, or a reference type, or [DR337]
11624 an abstract class type. */
11625 if (TREE_CODE (type) == VOID_TYPE
11626 || TREE_CODE (type) == FUNCTION_TYPE
11627 || TREE_CODE (type) == REFERENCE_TYPE)
11628 {
11629 if (complain & tf_error)
11630 error ("creating array of %qT", type);
11631 return error_mark_node;
11632 }
11633 if (ABSTRACT_CLASS_TYPE_P (type))
11634 {
11635 if (complain & tf_error)
11636 error ("creating array of %qT, which is an abstract class type",
11637 type);
11638 return error_mark_node;
11639 }
11640
11641 r = build_cplus_array_type (type, domain);
11642
11643 if (TYPE_USER_ALIGN (t))
11644 {
11645 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11646 TYPE_USER_ALIGN (r) = 1;
11647 }
11648
11649 return r;
11650 }
11651
11652 case TYPENAME_TYPE:
11653 {
11654 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11655 in_decl, /*entering_scope=*/1);
11656 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11657 complain, in_decl);
11658
11659 if (ctx == error_mark_node || f == error_mark_node)
11660 return error_mark_node;
11661
11662 if (!MAYBE_CLASS_TYPE_P (ctx))
11663 {
11664 if (complain & tf_error)
11665 error ("%qT is not a class, struct, or union type", ctx);
11666 return error_mark_node;
11667 }
11668 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11669 {
11670 /* Normally, make_typename_type does not require that the CTX
11671 have complete type in order to allow things like:
11672
11673 template <class T> struct S { typename S<T>::X Y; };
11674
11675 But, such constructs have already been resolved by this
11676 point, so here CTX really should have complete type, unless
11677 it's a partial instantiation. */
11678 ctx = complete_type (ctx);
11679 if (!COMPLETE_TYPE_P (ctx))
11680 {
11681 if (complain & tf_error)
11682 cxx_incomplete_type_error (NULL_TREE, ctx);
11683 return error_mark_node;
11684 }
11685 }
11686
11687 f = make_typename_type (ctx, f, typename_type,
11688 complain | tf_keep_type_decl);
11689 if (f == error_mark_node)
11690 return f;
11691 if (TREE_CODE (f) == TYPE_DECL)
11692 {
11693 complain |= tf_ignore_bad_quals;
11694 f = TREE_TYPE (f);
11695 }
11696
11697 if (TREE_CODE (f) != TYPENAME_TYPE)
11698 {
11699 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11700 {
11701 if (complain & tf_error)
11702 error ("%qT resolves to %qT, which is not an enumeration type",
11703 t, f);
11704 else
11705 return error_mark_node;
11706 }
11707 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11708 {
11709 if (complain & tf_error)
11710 error ("%qT resolves to %qT, which is is not a class type",
11711 t, f);
11712 else
11713 return error_mark_node;
11714 }
11715 }
11716
11717 return cp_build_qualified_type_real
11718 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11719 }
11720
11721 case UNBOUND_CLASS_TEMPLATE:
11722 {
11723 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11724 in_decl, /*entering_scope=*/1);
11725 tree name = TYPE_IDENTIFIER (t);
11726 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11727
11728 if (ctx == error_mark_node || name == error_mark_node)
11729 return error_mark_node;
11730
11731 if (parm_list)
11732 parm_list = tsubst_template_parms (parm_list, args, complain);
11733 return make_unbound_class_template (ctx, name, parm_list, complain);
11734 }
11735
11736 case TYPEOF_TYPE:
11737 {
11738 tree type;
11739
11740 ++cp_unevaluated_operand;
11741 ++c_inhibit_evaluation_warnings;
11742
11743 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11744 complain, in_decl,
11745 /*integral_constant_expression_p=*/false);
11746
11747 --cp_unevaluated_operand;
11748 --c_inhibit_evaluation_warnings;
11749
11750 type = finish_typeof (type);
11751 return cp_build_qualified_type_real (type,
11752 cp_type_quals (t)
11753 | cp_type_quals (type),
11754 complain);
11755 }
11756
11757 case DECLTYPE_TYPE:
11758 {
11759 tree type;
11760
11761 ++cp_unevaluated_operand;
11762 ++c_inhibit_evaluation_warnings;
11763
11764 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11765 complain, in_decl,
11766 /*integral_constant_expression_p=*/false);
11767
11768 --cp_unevaluated_operand;
11769 --c_inhibit_evaluation_warnings;
11770
11771 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11772 type = lambda_capture_field_type (type);
11773 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11774 type = lambda_proxy_type (type);
11775 else
11776 type = finish_decltype_type
11777 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11778 return cp_build_qualified_type_real (type,
11779 cp_type_quals (t)
11780 | cp_type_quals (type),
11781 complain);
11782 }
11783
11784 case UNDERLYING_TYPE:
11785 {
11786 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11787 complain, in_decl);
11788 return finish_underlying_type (type);
11789 }
11790
11791 case TYPE_ARGUMENT_PACK:
11792 case NONTYPE_ARGUMENT_PACK:
11793 {
11794 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11795 tree packed_out =
11796 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11797 args,
11798 complain,
11799 in_decl);
11800 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11801
11802 /* For template nontype argument packs, also substitute into
11803 the type. */
11804 if (code == NONTYPE_ARGUMENT_PACK)
11805 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11806
11807 return r;
11808 }
11809 break;
11810
11811 case INTEGER_CST:
11812 case REAL_CST:
11813 case STRING_CST:
11814 case PLUS_EXPR:
11815 case MINUS_EXPR:
11816 case NEGATE_EXPR:
11817 case NOP_EXPR:
11818 case INDIRECT_REF:
11819 case ADDR_EXPR:
11820 case CALL_EXPR:
11821 case ARRAY_REF:
11822 case SCOPE_REF:
11823 /* We should use one of the expression tsubsts for these codes. */
11824 gcc_unreachable ();
11825
11826 default:
11827 sorry ("use of %qs in template", tree_code_name [(int) code]);
11828 return error_mark_node;
11829 }
11830 }
11831
11832 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11833 type of the expression on the left-hand side of the "." or "->"
11834 operator. */
11835
11836 static tree
11837 tsubst_baselink (tree baselink, tree object_type,
11838 tree args, tsubst_flags_t complain, tree in_decl)
11839 {
11840 tree name;
11841 tree qualifying_scope;
11842 tree fns;
11843 tree optype;
11844 tree template_args = 0;
11845 bool template_id_p = false;
11846 bool qualified = BASELINK_QUALIFIED_P (baselink);
11847
11848 /* A baselink indicates a function from a base class. Both the
11849 BASELINK_ACCESS_BINFO and the base class referenced may
11850 indicate bases of the template class, rather than the
11851 instantiated class. In addition, lookups that were not
11852 ambiguous before may be ambiguous now. Therefore, we perform
11853 the lookup again. */
11854 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11855 qualifying_scope = tsubst (qualifying_scope, args,
11856 complain, in_decl);
11857 fns = BASELINK_FUNCTIONS (baselink);
11858 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11859 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11860 {
11861 template_id_p = true;
11862 template_args = TREE_OPERAND (fns, 1);
11863 fns = TREE_OPERAND (fns, 0);
11864 if (template_args)
11865 template_args = tsubst_template_args (template_args, args,
11866 complain, in_decl);
11867 }
11868 name = DECL_NAME (get_first_fn (fns));
11869 if (IDENTIFIER_TYPENAME_P (name))
11870 name = mangle_conv_op_name_for_type (optype);
11871 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11872 if (!baselink)
11873 return error_mark_node;
11874
11875 /* If lookup found a single function, mark it as used at this
11876 point. (If it lookup found multiple functions the one selected
11877 later by overload resolution will be marked as used at that
11878 point.) */
11879 if (BASELINK_P (baselink))
11880 fns = BASELINK_FUNCTIONS (baselink);
11881 if (!template_id_p && !really_overloaded_fn (fns))
11882 mark_used (OVL_CURRENT (fns));
11883
11884 /* Add back the template arguments, if present. */
11885 if (BASELINK_P (baselink) && template_id_p)
11886 BASELINK_FUNCTIONS (baselink)
11887 = build_nt (TEMPLATE_ID_EXPR,
11888 BASELINK_FUNCTIONS (baselink),
11889 template_args);
11890 /* Update the conversion operator type. */
11891 BASELINK_OPTYPE (baselink) = optype;
11892
11893 if (!object_type)
11894 object_type = current_class_type;
11895
11896 if (qualified)
11897 baselink = adjust_result_of_qualified_name_lookup (baselink,
11898 qualifying_scope,
11899 object_type);
11900 return baselink;
11901 }
11902
11903 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11904 true if the qualified-id will be a postfix-expression in-and-of
11905 itself; false if more of the postfix-expression follows the
11906 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11907 of "&". */
11908
11909 static tree
11910 tsubst_qualified_id (tree qualified_id, tree args,
11911 tsubst_flags_t complain, tree in_decl,
11912 bool done, bool address_p)
11913 {
11914 tree expr;
11915 tree scope;
11916 tree name;
11917 bool is_template;
11918 tree template_args;
11919 location_t loc = UNKNOWN_LOCATION;
11920
11921 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11922
11923 /* Figure out what name to look up. */
11924 name = TREE_OPERAND (qualified_id, 1);
11925 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11926 {
11927 is_template = true;
11928 loc = EXPR_LOCATION (name);
11929 template_args = TREE_OPERAND (name, 1);
11930 if (template_args)
11931 template_args = tsubst_template_args (template_args, args,
11932 complain, in_decl);
11933 name = TREE_OPERAND (name, 0);
11934 }
11935 else
11936 {
11937 is_template = false;
11938 template_args = NULL_TREE;
11939 }
11940
11941 /* Substitute into the qualifying scope. When there are no ARGS, we
11942 are just trying to simplify a non-dependent expression. In that
11943 case the qualifying scope may be dependent, and, in any case,
11944 substituting will not help. */
11945 scope = TREE_OPERAND (qualified_id, 0);
11946 if (args)
11947 {
11948 scope = tsubst (scope, args, complain, in_decl);
11949 expr = tsubst_copy (name, args, complain, in_decl);
11950 }
11951 else
11952 expr = name;
11953
11954 if (dependent_scope_p (scope))
11955 {
11956 if (is_template)
11957 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11958 return build_qualified_name (NULL_TREE, scope, expr,
11959 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11960 }
11961
11962 if (!BASELINK_P (name) && !DECL_P (expr))
11963 {
11964 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11965 {
11966 /* A BIT_NOT_EXPR is used to represent a destructor. */
11967 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11968 {
11969 error ("qualifying type %qT does not match destructor name ~%qT",
11970 scope, TREE_OPERAND (expr, 0));
11971 expr = error_mark_node;
11972 }
11973 else
11974 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11975 /*is_type_p=*/0, false);
11976 }
11977 else
11978 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11979 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11980 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11981 {
11982 if (complain & tf_error)
11983 {
11984 error ("dependent-name %qE is parsed as a non-type, but "
11985 "instantiation yields a type", qualified_id);
11986 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11987 }
11988 return error_mark_node;
11989 }
11990 }
11991
11992 if (DECL_P (expr))
11993 {
11994 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11995 scope);
11996 /* Remember that there was a reference to this entity. */
11997 mark_used (expr);
11998 }
11999
12000 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12001 {
12002 if (complain & tf_error)
12003 qualified_name_lookup_error (scope,
12004 TREE_OPERAND (qualified_id, 1),
12005 expr, input_location);
12006 return error_mark_node;
12007 }
12008
12009 if (is_template)
12010 expr = lookup_template_function (expr, template_args);
12011
12012 if (expr == error_mark_node && complain & tf_error)
12013 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12014 expr, input_location);
12015 else if (TYPE_P (scope))
12016 {
12017 expr = (adjust_result_of_qualified_name_lookup
12018 (expr, scope, current_class_type));
12019 expr = (finish_qualified_id_expr
12020 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12021 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12022 /*template_arg_p=*/false));
12023 }
12024
12025 /* Expressions do not generally have reference type. */
12026 if (TREE_CODE (expr) != SCOPE_REF
12027 /* However, if we're about to form a pointer-to-member, we just
12028 want the referenced member referenced. */
12029 && TREE_CODE (expr) != OFFSET_REF)
12030 expr = convert_from_reference (expr);
12031
12032 return expr;
12033 }
12034
12035 /* Like tsubst, but deals with expressions. This function just replaces
12036 template parms; to finish processing the resultant expression, use
12037 tsubst_copy_and_build or tsubst_expr. */
12038
12039 static tree
12040 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12041 {
12042 enum tree_code code;
12043 tree r;
12044
12045 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12046 return t;
12047
12048 code = TREE_CODE (t);
12049
12050 switch (code)
12051 {
12052 case PARM_DECL:
12053 r = retrieve_local_specialization (t);
12054
12055 if (r == NULL_TREE)
12056 {
12057 /* We get here for a use of 'this' in an NSDMI. */
12058 if (DECL_NAME (t) == this_identifier
12059 && at_function_scope_p ()
12060 && DECL_CONSTRUCTOR_P (current_function_decl))
12061 return current_class_ptr;
12062
12063 /* This can happen for a parameter name used later in a function
12064 declaration (such as in a late-specified return type). Just
12065 make a dummy decl, since it's only used for its type. */
12066 gcc_assert (cp_unevaluated_operand != 0);
12067 r = tsubst_decl (t, args, complain);
12068 /* Give it the template pattern as its context; its true context
12069 hasn't been instantiated yet and this is good enough for
12070 mangling. */
12071 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12072 }
12073
12074 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12075 r = ARGUMENT_PACK_SELECT_ARG (r);
12076 mark_used (r);
12077 return r;
12078
12079 case CONST_DECL:
12080 {
12081 tree enum_type;
12082 tree v;
12083
12084 if (DECL_TEMPLATE_PARM_P (t))
12085 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12086 /* There is no need to substitute into namespace-scope
12087 enumerators. */
12088 if (DECL_NAMESPACE_SCOPE_P (t))
12089 return t;
12090 /* If ARGS is NULL, then T is known to be non-dependent. */
12091 if (args == NULL_TREE)
12092 return integral_constant_value (t);
12093
12094 /* Unfortunately, we cannot just call lookup_name here.
12095 Consider:
12096
12097 template <int I> int f() {
12098 enum E { a = I };
12099 struct S { void g() { E e = a; } };
12100 };
12101
12102 When we instantiate f<7>::S::g(), say, lookup_name is not
12103 clever enough to find f<7>::a. */
12104 enum_type
12105 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12106 /*entering_scope=*/0);
12107
12108 for (v = TYPE_VALUES (enum_type);
12109 v != NULL_TREE;
12110 v = TREE_CHAIN (v))
12111 if (TREE_PURPOSE (v) == DECL_NAME (t))
12112 return TREE_VALUE (v);
12113
12114 /* We didn't find the name. That should never happen; if
12115 name-lookup found it during preliminary parsing, we
12116 should find it again here during instantiation. */
12117 gcc_unreachable ();
12118 }
12119 return t;
12120
12121 case FIELD_DECL:
12122 if (DECL_CONTEXT (t))
12123 {
12124 tree ctx;
12125
12126 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12127 /*entering_scope=*/1);
12128 if (ctx != DECL_CONTEXT (t))
12129 {
12130 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12131 if (!r)
12132 {
12133 if (complain & tf_error)
12134 error ("using invalid field %qD", t);
12135 return error_mark_node;
12136 }
12137 return r;
12138 }
12139 }
12140
12141 return t;
12142
12143 case VAR_DECL:
12144 case FUNCTION_DECL:
12145 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12146 || local_variable_p (t))
12147 t = tsubst (t, args, complain, in_decl);
12148 mark_used (t);
12149 return t;
12150
12151 case NAMESPACE_DECL:
12152 return t;
12153
12154 case OVERLOAD:
12155 /* An OVERLOAD will always be a non-dependent overload set; an
12156 overload set from function scope will just be represented with an
12157 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12158 gcc_assert (!uses_template_parms (t));
12159 return t;
12160
12161 case BASELINK:
12162 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
12163
12164 case TEMPLATE_DECL:
12165 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12166 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12167 args, complain, in_decl);
12168 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12169 return tsubst (t, args, complain, in_decl);
12170 else if (DECL_CLASS_SCOPE_P (t)
12171 && uses_template_parms (DECL_CONTEXT (t)))
12172 {
12173 /* Template template argument like the following example need
12174 special treatment:
12175
12176 template <template <class> class TT> struct C {};
12177 template <class T> struct D {
12178 template <class U> struct E {};
12179 C<E> c; // #1
12180 };
12181 D<int> d; // #2
12182
12183 We are processing the template argument `E' in #1 for
12184 the template instantiation #2. Originally, `E' is a
12185 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12186 have to substitute this with one having context `D<int>'. */
12187
12188 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12189 return lookup_field (context, DECL_NAME(t), 0, false);
12190 }
12191 else
12192 /* Ordinary template template argument. */
12193 return t;
12194
12195 case CAST_EXPR:
12196 case REINTERPRET_CAST_EXPR:
12197 case CONST_CAST_EXPR:
12198 case STATIC_CAST_EXPR:
12199 case DYNAMIC_CAST_EXPR:
12200 case IMPLICIT_CONV_EXPR:
12201 case CONVERT_EXPR:
12202 case NOP_EXPR:
12203 return build1
12204 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12205 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12206
12207 case SIZEOF_EXPR:
12208 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12209 {
12210
12211 tree expanded, op = TREE_OPERAND (t, 0);
12212 int len = 0;
12213
12214 if (SIZEOF_EXPR_TYPE_P (t))
12215 op = TREE_TYPE (op);
12216
12217 ++cp_unevaluated_operand;
12218 ++c_inhibit_evaluation_warnings;
12219 /* We only want to compute the number of arguments. */
12220 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12221 --cp_unevaluated_operand;
12222 --c_inhibit_evaluation_warnings;
12223
12224 if (TREE_CODE (expanded) == TREE_VEC)
12225 len = TREE_VEC_LENGTH (expanded);
12226
12227 if (expanded == error_mark_node)
12228 return error_mark_node;
12229 else if (PACK_EXPANSION_P (expanded)
12230 || (TREE_CODE (expanded) == TREE_VEC
12231 && len > 0
12232 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12233 {
12234 if (TREE_CODE (expanded) == TREE_VEC)
12235 expanded = TREE_VEC_ELT (expanded, len - 1);
12236
12237 if (TYPE_P (expanded))
12238 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12239 complain & tf_error);
12240 else
12241 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12242 complain & tf_error);
12243 }
12244 else
12245 return build_int_cst (size_type_node, len);
12246 }
12247 if (SIZEOF_EXPR_TYPE_P (t))
12248 {
12249 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12250 args, complain, in_decl);
12251 r = build1 (NOP_EXPR, r, error_mark_node);
12252 r = build1 (SIZEOF_EXPR,
12253 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12254 SIZEOF_EXPR_TYPE_P (r) = 1;
12255 return r;
12256 }
12257 /* Fall through */
12258
12259 case INDIRECT_REF:
12260 case NEGATE_EXPR:
12261 case TRUTH_NOT_EXPR:
12262 case BIT_NOT_EXPR:
12263 case ADDR_EXPR:
12264 case UNARY_PLUS_EXPR: /* Unary + */
12265 case ALIGNOF_EXPR:
12266 case AT_ENCODE_EXPR:
12267 case ARROW_EXPR:
12268 case THROW_EXPR:
12269 case TYPEID_EXPR:
12270 case REALPART_EXPR:
12271 case IMAGPART_EXPR:
12272 return build1
12273 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12274 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12275
12276 case COMPONENT_REF:
12277 {
12278 tree object;
12279 tree name;
12280
12281 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12282 name = TREE_OPERAND (t, 1);
12283 if (TREE_CODE (name) == BIT_NOT_EXPR)
12284 {
12285 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12286 complain, in_decl);
12287 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12288 }
12289 else if (TREE_CODE (name) == SCOPE_REF
12290 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12291 {
12292 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12293 complain, in_decl);
12294 name = TREE_OPERAND (name, 1);
12295 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12296 complain, in_decl);
12297 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12298 name = build_qualified_name (/*type=*/NULL_TREE,
12299 base, name,
12300 /*template_p=*/false);
12301 }
12302 else if (BASELINK_P (name))
12303 name = tsubst_baselink (name,
12304 non_reference (TREE_TYPE (object)),
12305 args, complain,
12306 in_decl);
12307 else
12308 name = tsubst_copy (name, args, complain, in_decl);
12309 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12310 }
12311
12312 case PLUS_EXPR:
12313 case MINUS_EXPR:
12314 case MULT_EXPR:
12315 case TRUNC_DIV_EXPR:
12316 case CEIL_DIV_EXPR:
12317 case FLOOR_DIV_EXPR:
12318 case ROUND_DIV_EXPR:
12319 case EXACT_DIV_EXPR:
12320 case BIT_AND_EXPR:
12321 case BIT_IOR_EXPR:
12322 case BIT_XOR_EXPR:
12323 case TRUNC_MOD_EXPR:
12324 case FLOOR_MOD_EXPR:
12325 case TRUTH_ANDIF_EXPR:
12326 case TRUTH_ORIF_EXPR:
12327 case TRUTH_AND_EXPR:
12328 case TRUTH_OR_EXPR:
12329 case RSHIFT_EXPR:
12330 case LSHIFT_EXPR:
12331 case RROTATE_EXPR:
12332 case LROTATE_EXPR:
12333 case EQ_EXPR:
12334 case NE_EXPR:
12335 case MAX_EXPR:
12336 case MIN_EXPR:
12337 case LE_EXPR:
12338 case GE_EXPR:
12339 case LT_EXPR:
12340 case GT_EXPR:
12341 case COMPOUND_EXPR:
12342 case DOTSTAR_EXPR:
12343 case MEMBER_REF:
12344 case PREDECREMENT_EXPR:
12345 case PREINCREMENT_EXPR:
12346 case POSTDECREMENT_EXPR:
12347 case POSTINCREMENT_EXPR:
12348 return build_nt
12349 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12350 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12351
12352 case SCOPE_REF:
12353 return build_qualified_name (/*type=*/NULL_TREE,
12354 tsubst_copy (TREE_OPERAND (t, 0),
12355 args, complain, in_decl),
12356 tsubst_copy (TREE_OPERAND (t, 1),
12357 args, complain, in_decl),
12358 QUALIFIED_NAME_IS_TEMPLATE (t));
12359
12360 case ARRAY_REF:
12361 return build_nt
12362 (ARRAY_REF,
12363 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12364 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12365 NULL_TREE, NULL_TREE);
12366
12367 case CALL_EXPR:
12368 {
12369 int n = VL_EXP_OPERAND_LENGTH (t);
12370 tree result = build_vl_exp (CALL_EXPR, n);
12371 int i;
12372 for (i = 0; i < n; i++)
12373 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12374 complain, in_decl);
12375 return result;
12376 }
12377
12378 case COND_EXPR:
12379 case MODOP_EXPR:
12380 case PSEUDO_DTOR_EXPR:
12381 {
12382 r = build_nt
12383 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12384 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12385 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12386 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12387 return r;
12388 }
12389
12390 case NEW_EXPR:
12391 {
12392 r = build_nt
12393 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12394 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12395 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12396 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12397 return r;
12398 }
12399
12400 case DELETE_EXPR:
12401 {
12402 r = build_nt
12403 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12404 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12405 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12406 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12407 return r;
12408 }
12409
12410 case TEMPLATE_ID_EXPR:
12411 {
12412 /* Substituted template arguments */
12413 tree fn = TREE_OPERAND (t, 0);
12414 tree targs = TREE_OPERAND (t, 1);
12415
12416 fn = tsubst_copy (fn, args, complain, in_decl);
12417 if (targs)
12418 targs = tsubst_template_args (targs, args, complain, in_decl);
12419
12420 return lookup_template_function (fn, targs);
12421 }
12422
12423 case TREE_LIST:
12424 {
12425 tree purpose, value, chain;
12426
12427 if (t == void_list_node)
12428 return t;
12429
12430 purpose = TREE_PURPOSE (t);
12431 if (purpose)
12432 purpose = tsubst_copy (purpose, args, complain, in_decl);
12433 value = TREE_VALUE (t);
12434 if (value)
12435 value = tsubst_copy (value, args, complain, in_decl);
12436 chain = TREE_CHAIN (t);
12437 if (chain && chain != void_type_node)
12438 chain = tsubst_copy (chain, args, complain, in_decl);
12439 if (purpose == TREE_PURPOSE (t)
12440 && value == TREE_VALUE (t)
12441 && chain == TREE_CHAIN (t))
12442 return t;
12443 return tree_cons (purpose, value, chain);
12444 }
12445
12446 case RECORD_TYPE:
12447 case UNION_TYPE:
12448 case ENUMERAL_TYPE:
12449 case INTEGER_TYPE:
12450 case TEMPLATE_TYPE_PARM:
12451 case TEMPLATE_TEMPLATE_PARM:
12452 case BOUND_TEMPLATE_TEMPLATE_PARM:
12453 case TEMPLATE_PARM_INDEX:
12454 case POINTER_TYPE:
12455 case REFERENCE_TYPE:
12456 case OFFSET_TYPE:
12457 case FUNCTION_TYPE:
12458 case METHOD_TYPE:
12459 case ARRAY_TYPE:
12460 case TYPENAME_TYPE:
12461 case UNBOUND_CLASS_TEMPLATE:
12462 case TYPEOF_TYPE:
12463 case DECLTYPE_TYPE:
12464 case TYPE_DECL:
12465 return tsubst (t, args, complain, in_decl);
12466
12467 case IDENTIFIER_NODE:
12468 if (IDENTIFIER_TYPENAME_P (t))
12469 {
12470 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12471 return mangle_conv_op_name_for_type (new_type);
12472 }
12473 else
12474 return t;
12475
12476 case CONSTRUCTOR:
12477 /* This is handled by tsubst_copy_and_build. */
12478 gcc_unreachable ();
12479
12480 case VA_ARG_EXPR:
12481 return build_x_va_arg (EXPR_LOCATION (t),
12482 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12483 in_decl),
12484 tsubst (TREE_TYPE (t), args, complain, in_decl));
12485
12486 case CLEANUP_POINT_EXPR:
12487 /* We shouldn't have built any of these during initial template
12488 generation. Instead, they should be built during instantiation
12489 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12490 gcc_unreachable ();
12491
12492 case OFFSET_REF:
12493 r = build2
12494 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12495 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12496 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12497 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12498 mark_used (TREE_OPERAND (r, 1));
12499 return r;
12500
12501 case EXPR_PACK_EXPANSION:
12502 error ("invalid use of pack expansion expression");
12503 return error_mark_node;
12504
12505 case NONTYPE_ARGUMENT_PACK:
12506 error ("use %<...%> to expand argument pack");
12507 return error_mark_node;
12508
12509 case INTEGER_CST:
12510 case REAL_CST:
12511 case STRING_CST:
12512 case COMPLEX_CST:
12513 {
12514 /* Instantiate any typedefs in the type. */
12515 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12516 r = fold_convert (type, t);
12517 gcc_assert (TREE_CODE (r) == code);
12518 return r;
12519 }
12520
12521 case PTRMEM_CST:
12522 /* These can sometimes show up in a partial instantiation, but never
12523 involve template parms. */
12524 gcc_assert (!uses_template_parms (t));
12525 return t;
12526
12527 default:
12528 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12529 gcc_checking_assert (false);
12530 return t;
12531 }
12532 }
12533
12534 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12535
12536 static tree
12537 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12538 tree in_decl)
12539 {
12540 tree new_clauses = NULL, nc, oc;
12541
12542 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12543 {
12544 nc = copy_node (oc);
12545 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12546 new_clauses = nc;
12547
12548 switch (OMP_CLAUSE_CODE (nc))
12549 {
12550 case OMP_CLAUSE_LASTPRIVATE:
12551 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12552 {
12553 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12554 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12555 in_decl, /*integral_constant_expression_p=*/false);
12556 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12557 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12558 }
12559 /* FALLTHRU */
12560 case OMP_CLAUSE_PRIVATE:
12561 case OMP_CLAUSE_SHARED:
12562 case OMP_CLAUSE_FIRSTPRIVATE:
12563 case OMP_CLAUSE_REDUCTION:
12564 case OMP_CLAUSE_COPYIN:
12565 case OMP_CLAUSE_COPYPRIVATE:
12566 case OMP_CLAUSE_IF:
12567 case OMP_CLAUSE_NUM_THREADS:
12568 case OMP_CLAUSE_SCHEDULE:
12569 case OMP_CLAUSE_COLLAPSE:
12570 case OMP_CLAUSE_FINAL:
12571 OMP_CLAUSE_OPERAND (nc, 0)
12572 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12573 in_decl, /*integral_constant_expression_p=*/false);
12574 break;
12575 case OMP_CLAUSE_NOWAIT:
12576 case OMP_CLAUSE_ORDERED:
12577 case OMP_CLAUSE_DEFAULT:
12578 case OMP_CLAUSE_UNTIED:
12579 case OMP_CLAUSE_MERGEABLE:
12580 break;
12581 default:
12582 gcc_unreachable ();
12583 }
12584 }
12585
12586 return finish_omp_clauses (nreverse (new_clauses));
12587 }
12588
12589 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12590
12591 static tree
12592 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12593 tree in_decl)
12594 {
12595 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12596
12597 tree purpose, value, chain;
12598
12599 if (t == NULL)
12600 return t;
12601
12602 if (TREE_CODE (t) != TREE_LIST)
12603 return tsubst_copy_and_build (t, args, complain, in_decl,
12604 /*function_p=*/false,
12605 /*integral_constant_expression_p=*/false);
12606
12607 if (t == void_list_node)
12608 return t;
12609
12610 purpose = TREE_PURPOSE (t);
12611 if (purpose)
12612 purpose = RECUR (purpose);
12613 value = TREE_VALUE (t);
12614 if (value)
12615 {
12616 if (TREE_CODE (value) != LABEL_DECL)
12617 value = RECUR (value);
12618 else
12619 {
12620 value = lookup_label (DECL_NAME (value));
12621 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12622 TREE_USED (value) = 1;
12623 }
12624 }
12625 chain = TREE_CHAIN (t);
12626 if (chain && chain != void_type_node)
12627 chain = RECUR (chain);
12628 return tree_cons (purpose, value, chain);
12629 #undef RECUR
12630 }
12631
12632 /* Substitute one OMP_FOR iterator. */
12633
12634 static void
12635 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12636 tree condv, tree incrv, tree *clauses,
12637 tree args, tsubst_flags_t complain, tree in_decl,
12638 bool integral_constant_expression_p)
12639 {
12640 #define RECUR(NODE) \
12641 tsubst_expr ((NODE), args, complain, in_decl, \
12642 integral_constant_expression_p)
12643 tree decl, init, cond, incr;
12644 bool init_decl;
12645
12646 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12647 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12648 decl = TREE_OPERAND (init, 0);
12649 init = TREE_OPERAND (init, 1);
12650 /* Do this before substituting into decl to handle 'auto'. */
12651 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12652 init = RECUR (init);
12653 decl = RECUR (decl);
12654 if (init_decl)
12655 {
12656 gcc_assert (!processing_template_decl);
12657 init = DECL_INITIAL (decl);
12658 DECL_INITIAL (decl) = NULL_TREE;
12659 }
12660
12661 gcc_assert (!type_dependent_expression_p (decl));
12662
12663 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12664 {
12665 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12666 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12667 if (TREE_CODE (incr) == MODIFY_EXPR)
12668 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12669 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12670 RECUR (TREE_OPERAND (incr, 1)),
12671 complain);
12672 else
12673 incr = RECUR (incr);
12674 TREE_VEC_ELT (declv, i) = decl;
12675 TREE_VEC_ELT (initv, i) = init;
12676 TREE_VEC_ELT (condv, i) = cond;
12677 TREE_VEC_ELT (incrv, i) = incr;
12678 return;
12679 }
12680
12681 if (init && !init_decl)
12682 {
12683 tree c;
12684 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12685 {
12686 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12687 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12688 && OMP_CLAUSE_DECL (c) == decl)
12689 break;
12690 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12691 && OMP_CLAUSE_DECL (c) == decl)
12692 error ("iteration variable %qD should not be firstprivate", decl);
12693 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12694 && OMP_CLAUSE_DECL (c) == decl)
12695 error ("iteration variable %qD should not be reduction", decl);
12696 }
12697 if (c == NULL)
12698 {
12699 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12700 OMP_CLAUSE_DECL (c) = decl;
12701 c = finish_omp_clauses (c);
12702 if (c)
12703 {
12704 OMP_CLAUSE_CHAIN (c) = *clauses;
12705 *clauses = c;
12706 }
12707 }
12708 }
12709 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12710 if (COMPARISON_CLASS_P (cond))
12711 cond = build2 (TREE_CODE (cond), boolean_type_node,
12712 RECUR (TREE_OPERAND (cond, 0)),
12713 RECUR (TREE_OPERAND (cond, 1)));
12714 else
12715 cond = RECUR (cond);
12716 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12717 switch (TREE_CODE (incr))
12718 {
12719 case PREINCREMENT_EXPR:
12720 case PREDECREMENT_EXPR:
12721 case POSTINCREMENT_EXPR:
12722 case POSTDECREMENT_EXPR:
12723 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12724 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12725 break;
12726 case MODIFY_EXPR:
12727 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12728 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12729 {
12730 tree rhs = TREE_OPERAND (incr, 1);
12731 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12732 RECUR (TREE_OPERAND (incr, 0)),
12733 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12734 RECUR (TREE_OPERAND (rhs, 0)),
12735 RECUR (TREE_OPERAND (rhs, 1))));
12736 }
12737 else
12738 incr = RECUR (incr);
12739 break;
12740 case MODOP_EXPR:
12741 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12742 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12743 {
12744 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12745 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12746 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12747 TREE_TYPE (decl), lhs,
12748 RECUR (TREE_OPERAND (incr, 2))));
12749 }
12750 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12751 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12752 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12753 {
12754 tree rhs = TREE_OPERAND (incr, 2);
12755 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12756 RECUR (TREE_OPERAND (incr, 0)),
12757 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12758 RECUR (TREE_OPERAND (rhs, 0)),
12759 RECUR (TREE_OPERAND (rhs, 1))));
12760 }
12761 else
12762 incr = RECUR (incr);
12763 break;
12764 default:
12765 incr = RECUR (incr);
12766 break;
12767 }
12768
12769 TREE_VEC_ELT (declv, i) = decl;
12770 TREE_VEC_ELT (initv, i) = init;
12771 TREE_VEC_ELT (condv, i) = cond;
12772 TREE_VEC_ELT (incrv, i) = incr;
12773 #undef RECUR
12774 }
12775
12776 /* Like tsubst_copy for expressions, etc. but also does semantic
12777 processing. */
12778
12779 static tree
12780 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12781 bool integral_constant_expression_p)
12782 {
12783 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12784 #define RECUR(NODE) \
12785 tsubst_expr ((NODE), args, complain, in_decl, \
12786 integral_constant_expression_p)
12787
12788 tree stmt, tmp;
12789 tree r;
12790 location_t loc;
12791
12792 if (t == NULL_TREE || t == error_mark_node)
12793 return t;
12794
12795 loc = input_location;
12796 if (EXPR_HAS_LOCATION (t))
12797 input_location = EXPR_LOCATION (t);
12798 if (STATEMENT_CODE_P (TREE_CODE (t)))
12799 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12800
12801 switch (TREE_CODE (t))
12802 {
12803 case STATEMENT_LIST:
12804 {
12805 tree_stmt_iterator i;
12806 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12807 RECUR (tsi_stmt (i));
12808 break;
12809 }
12810
12811 case CTOR_INITIALIZER:
12812 finish_mem_initializers (tsubst_initializer_list
12813 (TREE_OPERAND (t, 0), args));
12814 break;
12815
12816 case RETURN_EXPR:
12817 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12818 break;
12819
12820 case EXPR_STMT:
12821 tmp = RECUR (EXPR_STMT_EXPR (t));
12822 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12823 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12824 else
12825 finish_expr_stmt (tmp);
12826 break;
12827
12828 case USING_STMT:
12829 do_using_directive (USING_STMT_NAMESPACE (t));
12830 break;
12831
12832 case DECL_EXPR:
12833 {
12834 tree decl, pattern_decl;
12835 tree init;
12836
12837 pattern_decl = decl = DECL_EXPR_DECL (t);
12838 if (TREE_CODE (decl) == LABEL_DECL)
12839 finish_label_decl (DECL_NAME (decl));
12840 else if (TREE_CODE (decl) == USING_DECL)
12841 {
12842 tree scope = USING_DECL_SCOPE (decl);
12843 tree name = DECL_NAME (decl);
12844 tree decl;
12845
12846 scope = tsubst (scope, args, complain, in_decl);
12847 decl = lookup_qualified_name (scope, name,
12848 /*is_type_p=*/false,
12849 /*complain=*/false);
12850 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12851 qualified_name_lookup_error (scope, name, decl, input_location);
12852 else
12853 do_local_using_decl (decl, scope, name);
12854 }
12855 else
12856 {
12857 init = DECL_INITIAL (decl);
12858 decl = tsubst (decl, args, complain, in_decl);
12859 if (decl != error_mark_node)
12860 {
12861 /* By marking the declaration as instantiated, we avoid
12862 trying to instantiate it. Since instantiate_decl can't
12863 handle local variables, and since we've already done
12864 all that needs to be done, that's the right thing to
12865 do. */
12866 if (TREE_CODE (decl) == VAR_DECL)
12867 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12868 if (TREE_CODE (decl) == VAR_DECL
12869 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12870 /* Anonymous aggregates are a special case. */
12871 finish_anon_union (decl);
12872 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12873 {
12874 DECL_CONTEXT (decl) = current_function_decl;
12875 if (DECL_NAME (decl) == this_identifier)
12876 {
12877 tree lam = DECL_CONTEXT (current_function_decl);
12878 lam = CLASSTYPE_LAMBDA_EXPR (lam);
12879 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
12880 }
12881 insert_capture_proxy (decl);
12882 }
12883 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12884 /* We already did a pushtag. */;
12885 else
12886 {
12887 int const_init = false;
12888 maybe_push_decl (decl);
12889 if (TREE_CODE (decl) == VAR_DECL
12890 && DECL_PRETTY_FUNCTION_P (decl))
12891 {
12892 /* For __PRETTY_FUNCTION__ we have to adjust the
12893 initializer. */
12894 const char *const name
12895 = cxx_printable_name (current_function_decl, 2);
12896 init = cp_fname_init (name, &TREE_TYPE (decl));
12897 }
12898 else
12899 {
12900 tree t = RECUR (init);
12901
12902 if (init && !t)
12903 {
12904 /* If we had an initializer but it
12905 instantiated to nothing,
12906 value-initialize the object. This will
12907 only occur when the initializer was a
12908 pack expansion where the parameter packs
12909 used in that expansion were of length
12910 zero. */
12911 init = build_value_init (TREE_TYPE (decl),
12912 complain);
12913 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12914 init = get_target_expr_sfinae (init, complain);
12915 }
12916 else
12917 init = t;
12918 }
12919
12920 if (TREE_CODE (decl) == VAR_DECL)
12921 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12922 (pattern_decl));
12923 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12924 }
12925 }
12926 }
12927
12928 break;
12929 }
12930
12931 case FOR_STMT:
12932 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12933 RECUR (FOR_INIT_STMT (t));
12934 finish_for_init_stmt (stmt);
12935 tmp = RECUR (FOR_COND (t));
12936 finish_for_cond (tmp, stmt);
12937 tmp = RECUR (FOR_EXPR (t));
12938 finish_for_expr (tmp, stmt);
12939 RECUR (FOR_BODY (t));
12940 finish_for_stmt (stmt);
12941 break;
12942
12943 case RANGE_FOR_STMT:
12944 {
12945 tree decl, expr;
12946 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12947 decl = RANGE_FOR_DECL (t);
12948 decl = tsubst (decl, args, complain, in_decl);
12949 maybe_push_decl (decl);
12950 expr = RECUR (RANGE_FOR_EXPR (t));
12951 stmt = cp_convert_range_for (stmt, decl, expr);
12952 RECUR (RANGE_FOR_BODY (t));
12953 finish_for_stmt (stmt);
12954 }
12955 break;
12956
12957 case WHILE_STMT:
12958 stmt = begin_while_stmt ();
12959 tmp = RECUR (WHILE_COND (t));
12960 finish_while_stmt_cond (tmp, stmt);
12961 RECUR (WHILE_BODY (t));
12962 finish_while_stmt (stmt);
12963 break;
12964
12965 case DO_STMT:
12966 stmt = begin_do_stmt ();
12967 RECUR (DO_BODY (t));
12968 finish_do_body (stmt);
12969 tmp = RECUR (DO_COND (t));
12970 finish_do_stmt (tmp, stmt);
12971 break;
12972
12973 case IF_STMT:
12974 stmt = begin_if_stmt ();
12975 tmp = RECUR (IF_COND (t));
12976 finish_if_stmt_cond (tmp, stmt);
12977 RECUR (THEN_CLAUSE (t));
12978 finish_then_clause (stmt);
12979
12980 if (ELSE_CLAUSE (t))
12981 {
12982 begin_else_clause (stmt);
12983 RECUR (ELSE_CLAUSE (t));
12984 finish_else_clause (stmt);
12985 }
12986
12987 finish_if_stmt (stmt);
12988 break;
12989
12990 case BIND_EXPR:
12991 if (BIND_EXPR_BODY_BLOCK (t))
12992 stmt = begin_function_body ();
12993 else
12994 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12995 ? BCS_TRY_BLOCK : 0);
12996
12997 RECUR (BIND_EXPR_BODY (t));
12998
12999 if (BIND_EXPR_BODY_BLOCK (t))
13000 finish_function_body (stmt);
13001 else
13002 finish_compound_stmt (stmt);
13003 break;
13004
13005 case BREAK_STMT:
13006 finish_break_stmt ();
13007 break;
13008
13009 case CONTINUE_STMT:
13010 finish_continue_stmt ();
13011 break;
13012
13013 case SWITCH_STMT:
13014 stmt = begin_switch_stmt ();
13015 tmp = RECUR (SWITCH_STMT_COND (t));
13016 finish_switch_cond (tmp, stmt);
13017 RECUR (SWITCH_STMT_BODY (t));
13018 finish_switch_stmt (stmt);
13019 break;
13020
13021 case CASE_LABEL_EXPR:
13022 finish_case_label (EXPR_LOCATION (t),
13023 RECUR (CASE_LOW (t)),
13024 RECUR (CASE_HIGH (t)));
13025 break;
13026
13027 case LABEL_EXPR:
13028 {
13029 tree decl = LABEL_EXPR_LABEL (t);
13030 tree label;
13031
13032 label = finish_label_stmt (DECL_NAME (decl));
13033 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13034 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13035 }
13036 break;
13037
13038 case GOTO_EXPR:
13039 tmp = GOTO_DESTINATION (t);
13040 if (TREE_CODE (tmp) != LABEL_DECL)
13041 /* Computed goto's must be tsubst'd into. On the other hand,
13042 non-computed gotos must not be; the identifier in question
13043 will have no binding. */
13044 tmp = RECUR (tmp);
13045 else
13046 tmp = DECL_NAME (tmp);
13047 finish_goto_stmt (tmp);
13048 break;
13049
13050 case ASM_EXPR:
13051 tmp = finish_asm_stmt
13052 (ASM_VOLATILE_P (t),
13053 RECUR (ASM_STRING (t)),
13054 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13055 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13056 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13057 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13058 {
13059 tree asm_expr = tmp;
13060 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13061 asm_expr = TREE_OPERAND (asm_expr, 0);
13062 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13063 }
13064 break;
13065
13066 case TRY_BLOCK:
13067 if (CLEANUP_P (t))
13068 {
13069 stmt = begin_try_block ();
13070 RECUR (TRY_STMTS (t));
13071 finish_cleanup_try_block (stmt);
13072 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13073 }
13074 else
13075 {
13076 tree compound_stmt = NULL_TREE;
13077
13078 if (FN_TRY_BLOCK_P (t))
13079 stmt = begin_function_try_block (&compound_stmt);
13080 else
13081 stmt = begin_try_block ();
13082
13083 RECUR (TRY_STMTS (t));
13084
13085 if (FN_TRY_BLOCK_P (t))
13086 finish_function_try_block (stmt);
13087 else
13088 finish_try_block (stmt);
13089
13090 RECUR (TRY_HANDLERS (t));
13091 if (FN_TRY_BLOCK_P (t))
13092 finish_function_handler_sequence (stmt, compound_stmt);
13093 else
13094 finish_handler_sequence (stmt);
13095 }
13096 break;
13097
13098 case HANDLER:
13099 {
13100 tree decl = HANDLER_PARMS (t);
13101
13102 if (decl)
13103 {
13104 decl = tsubst (decl, args, complain, in_decl);
13105 /* Prevent instantiate_decl from trying to instantiate
13106 this variable. We've already done all that needs to be
13107 done. */
13108 if (decl != error_mark_node)
13109 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13110 }
13111 stmt = begin_handler ();
13112 finish_handler_parms (decl, stmt);
13113 RECUR (HANDLER_BODY (t));
13114 finish_handler (stmt);
13115 }
13116 break;
13117
13118 case TAG_DEFN:
13119 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13120 if (CLASS_TYPE_P (tmp))
13121 {
13122 /* Local classes are not independent templates; they are
13123 instantiated along with their containing function. And this
13124 way we don't have to deal with pushing out of one local class
13125 to instantiate a member of another local class. */
13126 tree fn;
13127 /* Closures are handled by the LAMBDA_EXPR. */
13128 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13129 complete_type (tmp);
13130 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13131 if (!DECL_ARTIFICIAL (fn))
13132 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13133 }
13134 break;
13135
13136 case STATIC_ASSERT:
13137 {
13138 tree condition;
13139
13140 ++c_inhibit_evaluation_warnings;
13141 condition =
13142 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13143 args,
13144 complain, in_decl,
13145 /*integral_constant_expression_p=*/true);
13146 --c_inhibit_evaluation_warnings;
13147
13148 finish_static_assert (condition,
13149 STATIC_ASSERT_MESSAGE (t),
13150 STATIC_ASSERT_SOURCE_LOCATION (t),
13151 /*member_p=*/false);
13152 }
13153 break;
13154
13155 case OMP_PARALLEL:
13156 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
13157 args, complain, in_decl);
13158 stmt = begin_omp_parallel ();
13159 RECUR (OMP_PARALLEL_BODY (t));
13160 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13161 = OMP_PARALLEL_COMBINED (t);
13162 break;
13163
13164 case OMP_TASK:
13165 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
13166 args, complain, in_decl);
13167 stmt = begin_omp_task ();
13168 RECUR (OMP_TASK_BODY (t));
13169 finish_omp_task (tmp, stmt);
13170 break;
13171
13172 case OMP_FOR:
13173 {
13174 tree clauses, body, pre_body;
13175 tree declv, initv, condv, incrv;
13176 int i;
13177
13178 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
13179 args, complain, in_decl);
13180 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13181 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13182 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13183 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13184
13185 stmt = begin_omp_structured_block ();
13186
13187 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13188 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13189 &clauses, args, complain, in_decl,
13190 integral_constant_expression_p);
13191
13192 pre_body = push_stmt_list ();
13193 RECUR (OMP_FOR_PRE_BODY (t));
13194 pre_body = pop_stmt_list (pre_body);
13195
13196 body = push_stmt_list ();
13197 RECUR (OMP_FOR_BODY (t));
13198 body = pop_stmt_list (body);
13199
13200 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
13201 body, pre_body, clauses);
13202
13203 add_stmt (finish_omp_structured_block (stmt));
13204 }
13205 break;
13206
13207 case OMP_SECTIONS:
13208 case OMP_SINGLE:
13209 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
13210 stmt = push_stmt_list ();
13211 RECUR (OMP_BODY (t));
13212 stmt = pop_stmt_list (stmt);
13213
13214 t = copy_node (t);
13215 OMP_BODY (t) = stmt;
13216 OMP_CLAUSES (t) = tmp;
13217 add_stmt (t);
13218 break;
13219
13220 case OMP_SECTION:
13221 case OMP_CRITICAL:
13222 case OMP_MASTER:
13223 case OMP_ORDERED:
13224 stmt = push_stmt_list ();
13225 RECUR (OMP_BODY (t));
13226 stmt = pop_stmt_list (stmt);
13227
13228 t = copy_node (t);
13229 OMP_BODY (t) = stmt;
13230 add_stmt (t);
13231 break;
13232
13233 case OMP_ATOMIC:
13234 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13235 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13236 {
13237 tree op1 = TREE_OPERAND (t, 1);
13238 tree rhs1 = NULL_TREE;
13239 tree lhs, rhs;
13240 if (TREE_CODE (op1) == COMPOUND_EXPR)
13241 {
13242 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13243 op1 = TREE_OPERAND (op1, 1);
13244 }
13245 lhs = RECUR (TREE_OPERAND (op1, 0));
13246 rhs = RECUR (TREE_OPERAND (op1, 1));
13247 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13248 NULL_TREE, NULL_TREE, rhs1);
13249 }
13250 else
13251 {
13252 tree op1 = TREE_OPERAND (t, 1);
13253 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13254 tree rhs1 = NULL_TREE;
13255 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13256 enum tree_code opcode = NOP_EXPR;
13257 if (code == OMP_ATOMIC_READ)
13258 {
13259 v = RECUR (TREE_OPERAND (op1, 0));
13260 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13261 }
13262 else if (code == OMP_ATOMIC_CAPTURE_OLD
13263 || code == OMP_ATOMIC_CAPTURE_NEW)
13264 {
13265 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13266 v = RECUR (TREE_OPERAND (op1, 0));
13267 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13268 if (TREE_CODE (op11) == COMPOUND_EXPR)
13269 {
13270 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13271 op11 = TREE_OPERAND (op11, 1);
13272 }
13273 lhs = RECUR (TREE_OPERAND (op11, 0));
13274 rhs = RECUR (TREE_OPERAND (op11, 1));
13275 opcode = TREE_CODE (op11);
13276 }
13277 else
13278 {
13279 code = OMP_ATOMIC;
13280 lhs = RECUR (TREE_OPERAND (op1, 0));
13281 rhs = RECUR (TREE_OPERAND (op1, 1));
13282 }
13283 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13284 }
13285 break;
13286
13287 case TRANSACTION_EXPR:
13288 {
13289 int flags = 0;
13290 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13291 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13292
13293 if (TRANSACTION_EXPR_IS_STMT (t))
13294 {
13295 tree body = TRANSACTION_EXPR_BODY (t);
13296 tree noex = NULL_TREE;
13297 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13298 {
13299 noex = MUST_NOT_THROW_COND (body);
13300 if (noex == NULL_TREE)
13301 noex = boolean_true_node;
13302 body = TREE_OPERAND (body, 0);
13303 }
13304 stmt = begin_transaction_stmt (input_location, NULL, flags);
13305 RECUR (body);
13306 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13307 }
13308 else
13309 {
13310 stmt = build_transaction_expr (EXPR_LOCATION (t),
13311 RECUR (TRANSACTION_EXPR_BODY (t)),
13312 flags, NULL_TREE);
13313 RETURN (stmt);
13314 }
13315 }
13316 break;
13317
13318 case MUST_NOT_THROW_EXPR:
13319 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13320 RECUR (MUST_NOT_THROW_COND (t))));
13321
13322 case EXPR_PACK_EXPANSION:
13323 error ("invalid use of pack expansion expression");
13324 RETURN (error_mark_node);
13325
13326 case NONTYPE_ARGUMENT_PACK:
13327 error ("use %<...%> to expand argument pack");
13328 RETURN (error_mark_node);
13329
13330 case COMPOUND_EXPR:
13331 tmp = RECUR (TREE_OPERAND (t, 0));
13332 if (tmp == NULL_TREE)
13333 /* If the first operand was a statement, we're done with it. */
13334 RETURN (RECUR (TREE_OPERAND (t, 1)));
13335 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13336 RECUR (TREE_OPERAND (t, 1)),
13337 complain));
13338
13339 default:
13340 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13341
13342 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13343 /*function_p=*/false,
13344 integral_constant_expression_p));
13345 }
13346
13347 RETURN (NULL_TREE);
13348 out:
13349 input_location = loc;
13350 return r;
13351 #undef RECUR
13352 #undef RETURN
13353 }
13354
13355 /* T is a postfix-expression that is not being used in a function
13356 call. Return the substituted version of T. */
13357
13358 static tree
13359 tsubst_non_call_postfix_expression (tree t, tree args,
13360 tsubst_flags_t complain,
13361 tree in_decl)
13362 {
13363 if (TREE_CODE (t) == SCOPE_REF)
13364 t = tsubst_qualified_id (t, args, complain, in_decl,
13365 /*done=*/false, /*address_p=*/false);
13366 else
13367 t = tsubst_copy_and_build (t, args, complain, in_decl,
13368 /*function_p=*/false,
13369 /*integral_constant_expression_p=*/false);
13370
13371 return t;
13372 }
13373
13374 /* Like tsubst but deals with expressions and performs semantic
13375 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13376
13377 tree
13378 tsubst_copy_and_build (tree t,
13379 tree args,
13380 tsubst_flags_t complain,
13381 tree in_decl,
13382 bool function_p,
13383 bool integral_constant_expression_p)
13384 {
13385 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13386 #define RECUR(NODE) \
13387 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13388 /*function_p=*/false, \
13389 integral_constant_expression_p)
13390
13391 tree retval, op1;
13392 location_t loc;
13393
13394 if (t == NULL_TREE || t == error_mark_node)
13395 return t;
13396
13397 loc = input_location;
13398 if (EXPR_HAS_LOCATION (t))
13399 input_location = EXPR_LOCATION (t);
13400
13401 switch (TREE_CODE (t))
13402 {
13403 case USING_DECL:
13404 t = DECL_NAME (t);
13405 /* Fall through. */
13406 case IDENTIFIER_NODE:
13407 {
13408 tree decl;
13409 cp_id_kind idk;
13410 bool non_integral_constant_expression_p;
13411 const char *error_msg;
13412
13413 if (IDENTIFIER_TYPENAME_P (t))
13414 {
13415 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13416 t = mangle_conv_op_name_for_type (new_type);
13417 }
13418
13419 /* Look up the name. */
13420 decl = lookup_name (t);
13421
13422 /* By convention, expressions use ERROR_MARK_NODE to indicate
13423 failure, not NULL_TREE. */
13424 if (decl == NULL_TREE)
13425 decl = error_mark_node;
13426
13427 decl = finish_id_expression (t, decl, NULL_TREE,
13428 &idk,
13429 integral_constant_expression_p,
13430 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13431 &non_integral_constant_expression_p,
13432 /*template_p=*/false,
13433 /*done=*/true,
13434 /*address_p=*/false,
13435 /*template_arg_p=*/false,
13436 &error_msg,
13437 input_location);
13438 if (error_msg)
13439 error (error_msg);
13440 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13441 {
13442 if (complain & tf_error)
13443 unqualified_name_lookup_error (decl);
13444 decl = error_mark_node;
13445 }
13446 RETURN (decl);
13447 }
13448
13449 case TEMPLATE_ID_EXPR:
13450 {
13451 tree object;
13452 tree templ = RECUR (TREE_OPERAND (t, 0));
13453 tree targs = TREE_OPERAND (t, 1);
13454
13455 if (targs)
13456 targs = tsubst_template_args (targs, args, complain, in_decl);
13457
13458 if (TREE_CODE (templ) == COMPONENT_REF)
13459 {
13460 object = TREE_OPERAND (templ, 0);
13461 templ = TREE_OPERAND (templ, 1);
13462 }
13463 else
13464 object = NULL_TREE;
13465 templ = lookup_template_function (templ, targs);
13466
13467 if (object)
13468 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13469 object, templ, NULL_TREE));
13470 else
13471 RETURN (baselink_for_fns (templ));
13472 }
13473
13474 case INDIRECT_REF:
13475 {
13476 tree r = RECUR (TREE_OPERAND (t, 0));
13477
13478 if (REFERENCE_REF_P (t))
13479 {
13480 /* A type conversion to reference type will be enclosed in
13481 such an indirect ref, but the substitution of the cast
13482 will have also added such an indirect ref. */
13483 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13484 r = convert_from_reference (r);
13485 }
13486 else
13487 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13488 RETURN (r);
13489 }
13490
13491 case NOP_EXPR:
13492 RETURN (build_nop
13493 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13494 RECUR (TREE_OPERAND (t, 0))));
13495
13496 case IMPLICIT_CONV_EXPR:
13497 {
13498 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13499 tree expr = RECUR (TREE_OPERAND (t, 0));
13500 int flags = LOOKUP_IMPLICIT;
13501 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13502 flags = LOOKUP_NORMAL;
13503 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13504 flags));
13505 }
13506
13507 case CONVERT_EXPR:
13508 RETURN (build1
13509 (CONVERT_EXPR,
13510 tsubst (TREE_TYPE (t), args, complain, in_decl),
13511 RECUR (TREE_OPERAND (t, 0))));
13512
13513 case CAST_EXPR:
13514 case REINTERPRET_CAST_EXPR:
13515 case CONST_CAST_EXPR:
13516 case DYNAMIC_CAST_EXPR:
13517 case STATIC_CAST_EXPR:
13518 {
13519 tree type;
13520 tree op, r = NULL_TREE;
13521
13522 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13523 if (integral_constant_expression_p
13524 && !cast_valid_in_integral_constant_expression_p (type))
13525 {
13526 if (complain & tf_error)
13527 error ("a cast to a type other than an integral or "
13528 "enumeration type cannot appear in a constant-expression");
13529 RETURN (error_mark_node);
13530 }
13531
13532 op = RECUR (TREE_OPERAND (t, 0));
13533
13534 ++c_inhibit_evaluation_warnings;
13535 switch (TREE_CODE (t))
13536 {
13537 case CAST_EXPR:
13538 r = build_functional_cast (type, op, complain);
13539 break;
13540 case REINTERPRET_CAST_EXPR:
13541 r = build_reinterpret_cast (type, op, complain);
13542 break;
13543 case CONST_CAST_EXPR:
13544 r = build_const_cast (type, op, complain);
13545 break;
13546 case DYNAMIC_CAST_EXPR:
13547 r = build_dynamic_cast (type, op, complain);
13548 break;
13549 case STATIC_CAST_EXPR:
13550 r = build_static_cast (type, op, complain);
13551 break;
13552 default:
13553 gcc_unreachable ();
13554 }
13555 --c_inhibit_evaluation_warnings;
13556
13557 RETURN (r);
13558 }
13559
13560 case POSTDECREMENT_EXPR:
13561 case POSTINCREMENT_EXPR:
13562 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13563 args, complain, in_decl);
13564 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
13565
13566 case PREDECREMENT_EXPR:
13567 case PREINCREMENT_EXPR:
13568 case NEGATE_EXPR:
13569 case BIT_NOT_EXPR:
13570 case ABS_EXPR:
13571 case TRUTH_NOT_EXPR:
13572 case UNARY_PLUS_EXPR: /* Unary + */
13573 case REALPART_EXPR:
13574 case IMAGPART_EXPR:
13575 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
13576 RECUR (TREE_OPERAND (t, 0)), complain));
13577
13578 case FIX_TRUNC_EXPR:
13579 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13580 0, complain));
13581
13582 case ADDR_EXPR:
13583 op1 = TREE_OPERAND (t, 0);
13584 if (TREE_CODE (op1) == LABEL_DECL)
13585 RETURN (finish_label_address_expr (DECL_NAME (op1),
13586 EXPR_LOCATION (op1)));
13587 if (TREE_CODE (op1) == SCOPE_REF)
13588 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13589 /*done=*/true, /*address_p=*/true);
13590 else
13591 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13592 in_decl);
13593 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
13594
13595 case PLUS_EXPR:
13596 case MINUS_EXPR:
13597 case MULT_EXPR:
13598 case TRUNC_DIV_EXPR:
13599 case CEIL_DIV_EXPR:
13600 case FLOOR_DIV_EXPR:
13601 case ROUND_DIV_EXPR:
13602 case EXACT_DIV_EXPR:
13603 case BIT_AND_EXPR:
13604 case BIT_IOR_EXPR:
13605 case BIT_XOR_EXPR:
13606 case TRUNC_MOD_EXPR:
13607 case FLOOR_MOD_EXPR:
13608 case TRUTH_ANDIF_EXPR:
13609 case TRUTH_ORIF_EXPR:
13610 case TRUTH_AND_EXPR:
13611 case TRUTH_OR_EXPR:
13612 case RSHIFT_EXPR:
13613 case LSHIFT_EXPR:
13614 case RROTATE_EXPR:
13615 case LROTATE_EXPR:
13616 case EQ_EXPR:
13617 case NE_EXPR:
13618 case MAX_EXPR:
13619 case MIN_EXPR:
13620 case LE_EXPR:
13621 case GE_EXPR:
13622 case LT_EXPR:
13623 case GT_EXPR:
13624 case MEMBER_REF:
13625 case DOTSTAR_EXPR:
13626 {
13627 tree r;
13628
13629 ++c_inhibit_evaluation_warnings;
13630
13631 r = build_x_binary_op
13632 (input_location, TREE_CODE (t),
13633 RECUR (TREE_OPERAND (t, 0)),
13634 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13635 ? ERROR_MARK
13636 : TREE_CODE (TREE_OPERAND (t, 0))),
13637 RECUR (TREE_OPERAND (t, 1)),
13638 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13639 ? ERROR_MARK
13640 : TREE_CODE (TREE_OPERAND (t, 1))),
13641 /*overload=*/NULL,
13642 complain);
13643 if (EXPR_P (r) && TREE_NO_WARNING (t))
13644 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13645
13646 --c_inhibit_evaluation_warnings;
13647
13648 RETURN (r);
13649 }
13650
13651 case SCOPE_REF:
13652 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13653 /*address_p=*/false));
13654 case ARRAY_REF:
13655 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13656 args, complain, in_decl);
13657 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
13658 RECUR (TREE_OPERAND (t, 1)), complain));
13659
13660 case SIZEOF_EXPR:
13661 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13662 RETURN (tsubst_copy (t, args, complain, in_decl));
13663 /* Fall through */
13664
13665 case ALIGNOF_EXPR:
13666 {
13667 tree r;
13668
13669 op1 = TREE_OPERAND (t, 0);
13670 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
13671 op1 = TREE_TYPE (op1);
13672 if (!args)
13673 {
13674 /* When there are no ARGS, we are trying to evaluate a
13675 non-dependent expression from the parser. Trying to do
13676 the substitutions may not work. */
13677 if (!TYPE_P (op1))
13678 op1 = TREE_TYPE (op1);
13679 }
13680 else
13681 {
13682 ++cp_unevaluated_operand;
13683 ++c_inhibit_evaluation_warnings;
13684 if (TYPE_P (op1))
13685 op1 = tsubst (op1, args, complain, in_decl);
13686 else
13687 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13688 /*function_p=*/false,
13689 /*integral_constant_expression_p=*/
13690 false);
13691 --cp_unevaluated_operand;
13692 --c_inhibit_evaluation_warnings;
13693 }
13694 if (TYPE_P (op1))
13695 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13696 complain & tf_error);
13697 else
13698 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13699 complain & tf_error);
13700 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
13701 {
13702 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
13703 {
13704 if (!processing_template_decl && TYPE_P (op1))
13705 {
13706 r = build_min (SIZEOF_EXPR, size_type_node,
13707 build1 (NOP_EXPR, op1, error_mark_node));
13708 SIZEOF_EXPR_TYPE_P (r) = 1;
13709 }
13710 else
13711 r = build_min (SIZEOF_EXPR, size_type_node, op1);
13712 TREE_SIDE_EFFECTS (r) = 0;
13713 TREE_READONLY (r) = 1;
13714 }
13715 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
13716 }
13717 RETURN (r);
13718 }
13719
13720 case AT_ENCODE_EXPR:
13721 {
13722 op1 = TREE_OPERAND (t, 0);
13723 ++cp_unevaluated_operand;
13724 ++c_inhibit_evaluation_warnings;
13725 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13726 /*function_p=*/false,
13727 /*integral_constant_expression_p=*/false);
13728 --cp_unevaluated_operand;
13729 --c_inhibit_evaluation_warnings;
13730 RETURN (objc_build_encode_expr (op1));
13731 }
13732
13733 case NOEXCEPT_EXPR:
13734 op1 = TREE_OPERAND (t, 0);
13735 ++cp_unevaluated_operand;
13736 ++c_inhibit_evaluation_warnings;
13737 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13738 /*function_p=*/false,
13739 /*integral_constant_expression_p=*/false);
13740 --cp_unevaluated_operand;
13741 --c_inhibit_evaluation_warnings;
13742 RETURN (finish_noexcept_expr (op1, complain));
13743
13744 case MODOP_EXPR:
13745 {
13746 tree r = build_x_modify_expr
13747 (EXPR_LOCATION (t),
13748 RECUR (TREE_OPERAND (t, 0)),
13749 TREE_CODE (TREE_OPERAND (t, 1)),
13750 RECUR (TREE_OPERAND (t, 2)),
13751 complain);
13752 /* TREE_NO_WARNING must be set if either the expression was
13753 parenthesized or it uses an operator such as >>= rather
13754 than plain assignment. In the former case, it was already
13755 set and must be copied. In the latter case,
13756 build_x_modify_expr sets it and it must not be reset
13757 here. */
13758 if (TREE_NO_WARNING (t))
13759 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13760 RETURN (r);
13761 }
13762
13763 case ARROW_EXPR:
13764 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13765 args, complain, in_decl);
13766 /* Remember that there was a reference to this entity. */
13767 if (DECL_P (op1))
13768 mark_used (op1);
13769 RETURN (build_x_arrow (input_location, op1, complain));
13770
13771 case NEW_EXPR:
13772 {
13773 tree placement = RECUR (TREE_OPERAND (t, 0));
13774 tree init = RECUR (TREE_OPERAND (t, 3));
13775 vec<tree, va_gc> *placement_vec;
13776 vec<tree, va_gc> *init_vec;
13777 tree ret;
13778
13779 if (placement == NULL_TREE)
13780 placement_vec = NULL;
13781 else
13782 {
13783 placement_vec = make_tree_vector ();
13784 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13785 vec_safe_push (placement_vec, TREE_VALUE (placement));
13786 }
13787
13788 /* If there was an initializer in the original tree, but it
13789 instantiated to an empty list, then we should pass a
13790 non-NULL empty vector to tell build_new that it was an
13791 empty initializer() rather than no initializer. This can
13792 only happen when the initializer is a pack expansion whose
13793 parameter packs are of length zero. */
13794 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13795 init_vec = NULL;
13796 else
13797 {
13798 init_vec = make_tree_vector ();
13799 if (init == void_zero_node)
13800 gcc_assert (init_vec != NULL);
13801 else
13802 {
13803 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13804 vec_safe_push (init_vec, TREE_VALUE (init));
13805 }
13806 }
13807
13808 ret = build_new (&placement_vec,
13809 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13810 RECUR (TREE_OPERAND (t, 2)),
13811 &init_vec,
13812 NEW_EXPR_USE_GLOBAL (t),
13813 complain);
13814
13815 if (placement_vec != NULL)
13816 release_tree_vector (placement_vec);
13817 if (init_vec != NULL)
13818 release_tree_vector (init_vec);
13819
13820 RETURN (ret);
13821 }
13822
13823 case DELETE_EXPR:
13824 RETURN (delete_sanity
13825 (RECUR (TREE_OPERAND (t, 0)),
13826 RECUR (TREE_OPERAND (t, 1)),
13827 DELETE_EXPR_USE_VEC (t),
13828 DELETE_EXPR_USE_GLOBAL (t),
13829 complain));
13830
13831 case COMPOUND_EXPR:
13832 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
13833 RECUR (TREE_OPERAND (t, 0)),
13834 RECUR (TREE_OPERAND (t, 1)),
13835 complain));
13836
13837 case CALL_EXPR:
13838 {
13839 tree function;
13840 vec<tree, va_gc> *call_args;
13841 unsigned int nargs, i;
13842 bool qualified_p;
13843 bool koenig_p;
13844 tree ret;
13845
13846 function = CALL_EXPR_FN (t);
13847 /* When we parsed the expression, we determined whether or
13848 not Koenig lookup should be performed. */
13849 koenig_p = KOENIG_LOOKUP_P (t);
13850 if (TREE_CODE (function) == SCOPE_REF)
13851 {
13852 qualified_p = true;
13853 function = tsubst_qualified_id (function, args, complain, in_decl,
13854 /*done=*/false,
13855 /*address_p=*/false);
13856 }
13857 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13858 {
13859 /* Do nothing; calling tsubst_copy_and_build on an identifier
13860 would incorrectly perform unqualified lookup again.
13861
13862 Note that we can also have an IDENTIFIER_NODE if the earlier
13863 unqualified lookup found a member function; in that case
13864 koenig_p will be false and we do want to do the lookup
13865 again to find the instantiated member function.
13866
13867 FIXME but doing that causes c++/15272, so we need to stop
13868 using IDENTIFIER_NODE in that situation. */
13869 qualified_p = false;
13870 }
13871 else
13872 {
13873 if (TREE_CODE (function) == COMPONENT_REF)
13874 {
13875 tree op = TREE_OPERAND (function, 1);
13876
13877 qualified_p = (TREE_CODE (op) == SCOPE_REF
13878 || (BASELINK_P (op)
13879 && BASELINK_QUALIFIED_P (op)));
13880 }
13881 else
13882 qualified_p = false;
13883
13884 if (TREE_CODE (function) == ADDR_EXPR
13885 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
13886 /* Avoid error about taking the address of a constructor. */
13887 function = TREE_OPERAND (function, 0);
13888
13889 function = tsubst_copy_and_build (function, args, complain,
13890 in_decl,
13891 !qualified_p,
13892 integral_constant_expression_p);
13893
13894 if (BASELINK_P (function))
13895 qualified_p = true;
13896 }
13897
13898 nargs = call_expr_nargs (t);
13899 call_args = make_tree_vector ();
13900 for (i = 0; i < nargs; ++i)
13901 {
13902 tree arg = CALL_EXPR_ARG (t, i);
13903
13904 if (!PACK_EXPANSION_P (arg))
13905 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
13906 else
13907 {
13908 /* Expand the pack expansion and push each entry onto
13909 CALL_ARGS. */
13910 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13911 if (TREE_CODE (arg) == TREE_VEC)
13912 {
13913 unsigned int len, j;
13914
13915 len = TREE_VEC_LENGTH (arg);
13916 for (j = 0; j < len; ++j)
13917 {
13918 tree value = TREE_VEC_ELT (arg, j);
13919 if (value != NULL_TREE)
13920 value = convert_from_reference (value);
13921 vec_safe_push (call_args, value);
13922 }
13923 }
13924 else
13925 {
13926 /* A partial substitution. Add one entry. */
13927 vec_safe_push (call_args, arg);
13928 }
13929 }
13930 }
13931
13932 /* We do not perform argument-dependent lookup if normal
13933 lookup finds a non-function, in accordance with the
13934 expected resolution of DR 218. */
13935 if (koenig_p
13936 && ((is_overloaded_fn (function)
13937 /* If lookup found a member function, the Koenig lookup is
13938 not appropriate, even if an unqualified-name was used
13939 to denote the function. */
13940 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13941 || TREE_CODE (function) == IDENTIFIER_NODE)
13942 /* Only do this when substitution turns a dependent call
13943 into a non-dependent call. */
13944 && type_dependent_expression_p_push (t)
13945 && !any_type_dependent_arguments_p (call_args))
13946 function = perform_koenig_lookup (function, call_args, false,
13947 tf_none);
13948
13949 if (TREE_CODE (function) == IDENTIFIER_NODE
13950 && !any_type_dependent_arguments_p (call_args))
13951 {
13952 if (koenig_p && (complain & tf_warning_or_error))
13953 {
13954 /* For backwards compatibility and good diagnostics, try
13955 the unqualified lookup again if we aren't in SFINAE
13956 context. */
13957 tree unq = (tsubst_copy_and_build
13958 (function, args, complain, in_decl, true,
13959 integral_constant_expression_p));
13960 if (unq == error_mark_node)
13961 RETURN (error_mark_node);
13962
13963 if (unq != function)
13964 {
13965 tree fn = unq;
13966 if (TREE_CODE (fn) == INDIRECT_REF)
13967 fn = TREE_OPERAND (fn, 0);
13968 if (TREE_CODE (fn) == COMPONENT_REF)
13969 fn = TREE_OPERAND (fn, 1);
13970 if (is_overloaded_fn (fn))
13971 fn = get_first_fn (fn);
13972 permerror (EXPR_LOC_OR_HERE (t),
13973 "%qD was not declared in this scope, "
13974 "and no declarations were found by "
13975 "argument-dependent lookup at the point "
13976 "of instantiation", function);
13977 if (!DECL_P (fn))
13978 /* Can't say anything more. */;
13979 else if (DECL_CLASS_SCOPE_P (fn))
13980 {
13981 inform (EXPR_LOC_OR_HERE (t),
13982 "declarations in dependent base %qT are "
13983 "not found by unqualified lookup",
13984 DECL_CLASS_CONTEXT (fn));
13985 if (current_class_ptr)
13986 inform (EXPR_LOC_OR_HERE (t),
13987 "use %<this->%D%> instead", function);
13988 else
13989 inform (EXPR_LOC_OR_HERE (t),
13990 "use %<%T::%D%> instead",
13991 current_class_name, function);
13992 }
13993 else
13994 inform (0, "%q+D declared here, later in the "
13995 "translation unit", fn);
13996 function = unq;
13997 }
13998 }
13999 if (TREE_CODE (function) == IDENTIFIER_NODE)
14000 {
14001 if (complain & tf_error)
14002 unqualified_name_lookup_error (function);
14003 release_tree_vector (call_args);
14004 RETURN (error_mark_node);
14005 }
14006 }
14007
14008 /* Remember that there was a reference to this entity. */
14009 if (DECL_P (function))
14010 mark_used (function);
14011
14012 if (TREE_CODE (function) == OFFSET_REF)
14013 ret = build_offset_ref_call_from_tree (function, &call_args,
14014 complain);
14015 else if (TREE_CODE (function) == COMPONENT_REF)
14016 {
14017 tree instance = TREE_OPERAND (function, 0);
14018 tree fn = TREE_OPERAND (function, 1);
14019
14020 if (processing_template_decl
14021 && (type_dependent_expression_p (instance)
14022 || (!BASELINK_P (fn)
14023 && TREE_CODE (fn) != FIELD_DECL)
14024 || type_dependent_expression_p (fn)
14025 || any_type_dependent_arguments_p (call_args)))
14026 ret = build_nt_call_vec (function, call_args);
14027 else if (!BASELINK_P (fn))
14028 ret = finish_call_expr (function, &call_args,
14029 /*disallow_virtual=*/false,
14030 /*koenig_p=*/false,
14031 complain);
14032 else
14033 ret = (build_new_method_call
14034 (instance, fn,
14035 &call_args, NULL_TREE,
14036 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14037 /*fn_p=*/NULL,
14038 complain));
14039 }
14040 else
14041 ret = finish_call_expr (function, &call_args,
14042 /*disallow_virtual=*/qualified_p,
14043 koenig_p,
14044 complain);
14045
14046 release_tree_vector (call_args);
14047
14048 RETURN (ret);
14049 }
14050
14051 case COND_EXPR:
14052 {
14053 tree cond = RECUR (TREE_OPERAND (t, 0));
14054 tree exp1, exp2;
14055
14056 if (TREE_CODE (cond) == INTEGER_CST)
14057 {
14058 if (integer_zerop (cond))
14059 {
14060 ++c_inhibit_evaluation_warnings;
14061 exp1 = RECUR (TREE_OPERAND (t, 1));
14062 --c_inhibit_evaluation_warnings;
14063 exp2 = RECUR (TREE_OPERAND (t, 2));
14064 }
14065 else
14066 {
14067 exp1 = RECUR (TREE_OPERAND (t, 1));
14068 ++c_inhibit_evaluation_warnings;
14069 exp2 = RECUR (TREE_OPERAND (t, 2));
14070 --c_inhibit_evaluation_warnings;
14071 }
14072 }
14073 else
14074 {
14075 exp1 = RECUR (TREE_OPERAND (t, 1));
14076 exp2 = RECUR (TREE_OPERAND (t, 2));
14077 }
14078
14079 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14080 cond, exp1, exp2, complain));
14081 }
14082
14083 case PSEUDO_DTOR_EXPR:
14084 RETURN (finish_pseudo_destructor_expr
14085 (RECUR (TREE_OPERAND (t, 0)),
14086 RECUR (TREE_OPERAND (t, 1)),
14087 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)));
14088
14089 case TREE_LIST:
14090 {
14091 tree purpose, value, chain;
14092
14093 if (t == void_list_node)
14094 RETURN (t);
14095
14096 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14097 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14098 {
14099 /* We have pack expansions, so expand those and
14100 create a new list out of it. */
14101 tree purposevec = NULL_TREE;
14102 tree valuevec = NULL_TREE;
14103 tree chain;
14104 int i, len = -1;
14105
14106 /* Expand the argument expressions. */
14107 if (TREE_PURPOSE (t))
14108 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14109 complain, in_decl);
14110 if (TREE_VALUE (t))
14111 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14112 complain, in_decl);
14113
14114 /* Build the rest of the list. */
14115 chain = TREE_CHAIN (t);
14116 if (chain && chain != void_type_node)
14117 chain = RECUR (chain);
14118
14119 /* Determine the number of arguments. */
14120 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14121 {
14122 len = TREE_VEC_LENGTH (purposevec);
14123 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14124 }
14125 else if (TREE_CODE (valuevec) == TREE_VEC)
14126 len = TREE_VEC_LENGTH (valuevec);
14127 else
14128 {
14129 /* Since we only performed a partial substitution into
14130 the argument pack, we only RETURN (a single list
14131 node. */
14132 if (purposevec == TREE_PURPOSE (t)
14133 && valuevec == TREE_VALUE (t)
14134 && chain == TREE_CHAIN (t))
14135 RETURN (t);
14136
14137 RETURN (tree_cons (purposevec, valuevec, chain));
14138 }
14139
14140 /* Convert the argument vectors into a TREE_LIST */
14141 i = len;
14142 while (i > 0)
14143 {
14144 /* Grab the Ith values. */
14145 i--;
14146 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14147 : NULL_TREE;
14148 value
14149 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14150 : NULL_TREE;
14151
14152 /* Build the list (backwards). */
14153 chain = tree_cons (purpose, value, chain);
14154 }
14155
14156 RETURN (chain);
14157 }
14158
14159 purpose = TREE_PURPOSE (t);
14160 if (purpose)
14161 purpose = RECUR (purpose);
14162 value = TREE_VALUE (t);
14163 if (value)
14164 value = RECUR (value);
14165 chain = TREE_CHAIN (t);
14166 if (chain && chain != void_type_node)
14167 chain = RECUR (chain);
14168 if (purpose == TREE_PURPOSE (t)
14169 && value == TREE_VALUE (t)
14170 && chain == TREE_CHAIN (t))
14171 RETURN (t);
14172 RETURN (tree_cons (purpose, value, chain));
14173 }
14174
14175 case COMPONENT_REF:
14176 {
14177 tree object;
14178 tree object_type;
14179 tree member;
14180
14181 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14182 args, complain, in_decl);
14183 /* Remember that there was a reference to this entity. */
14184 if (DECL_P (object))
14185 mark_used (object);
14186 object_type = TREE_TYPE (object);
14187
14188 member = TREE_OPERAND (t, 1);
14189 if (BASELINK_P (member))
14190 member = tsubst_baselink (member,
14191 non_reference (TREE_TYPE (object)),
14192 args, complain, in_decl);
14193 else
14194 member = tsubst_copy (member, args, complain, in_decl);
14195 if (member == error_mark_node)
14196 RETURN (error_mark_node);
14197
14198 if (type_dependent_expression_p (object))
14199 /* We can't do much here. */;
14200 else if (!CLASS_TYPE_P (object_type))
14201 {
14202 if (scalarish_type_p (object_type))
14203 {
14204 tree s = NULL_TREE;
14205 tree dtor = member;
14206
14207 if (TREE_CODE (dtor) == SCOPE_REF)
14208 {
14209 s = TREE_OPERAND (dtor, 0);
14210 dtor = TREE_OPERAND (dtor, 1);
14211 }
14212 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14213 {
14214 dtor = TREE_OPERAND (dtor, 0);
14215 if (TYPE_P (dtor))
14216 RETURN (finish_pseudo_destructor_expr (object, s, dtor));
14217 }
14218 }
14219 }
14220 else if (TREE_CODE (member) == SCOPE_REF
14221 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14222 {
14223 /* Lookup the template functions now that we know what the
14224 scope is. */
14225 tree scope = TREE_OPERAND (member, 0);
14226 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14227 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14228 member = lookup_qualified_name (scope, tmpl,
14229 /*is_type_p=*/false,
14230 /*complain=*/false);
14231 if (BASELINK_P (member))
14232 {
14233 BASELINK_FUNCTIONS (member)
14234 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14235 args);
14236 member = (adjust_result_of_qualified_name_lookup
14237 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14238 object_type));
14239 }
14240 else
14241 {
14242 qualified_name_lookup_error (scope, tmpl, member,
14243 input_location);
14244 RETURN (error_mark_node);
14245 }
14246 }
14247 else if (TREE_CODE (member) == SCOPE_REF
14248 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14249 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14250 {
14251 if (complain & tf_error)
14252 {
14253 if (TYPE_P (TREE_OPERAND (member, 0)))
14254 error ("%qT is not a class or namespace",
14255 TREE_OPERAND (member, 0));
14256 else
14257 error ("%qD is not a class or namespace",
14258 TREE_OPERAND (member, 0));
14259 }
14260 RETURN (error_mark_node);
14261 }
14262 else if (TREE_CODE (member) == FIELD_DECL)
14263 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14264
14265 RETURN (finish_class_member_access_expr (object, member,
14266 /*template_p=*/false,
14267 complain));
14268 }
14269
14270 case THROW_EXPR:
14271 RETURN (build_throw
14272 (RECUR (TREE_OPERAND (t, 0))));
14273
14274 case CONSTRUCTOR:
14275 {
14276 vec<constructor_elt, va_gc> *n;
14277 constructor_elt *ce;
14278 unsigned HOST_WIDE_INT idx;
14279 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14280 bool process_index_p;
14281 int newlen;
14282 bool need_copy_p = false;
14283 tree r;
14284
14285 if (type == error_mark_node)
14286 RETURN (error_mark_node);
14287
14288 /* digest_init will do the wrong thing if we let it. */
14289 if (type && TYPE_PTRMEMFUNC_P (type))
14290 RETURN (t);
14291
14292 /* We do not want to process the index of aggregate
14293 initializers as they are identifier nodes which will be
14294 looked up by digest_init. */
14295 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14296
14297 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14298 newlen = vec_safe_length (n);
14299 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14300 {
14301 if (ce->index && process_index_p)
14302 ce->index = RECUR (ce->index);
14303
14304 if (PACK_EXPANSION_P (ce->value))
14305 {
14306 /* Substitute into the pack expansion. */
14307 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14308 in_decl);
14309
14310 if (ce->value == error_mark_node
14311 || PACK_EXPANSION_P (ce->value))
14312 ;
14313 else if (TREE_VEC_LENGTH (ce->value) == 1)
14314 /* Just move the argument into place. */
14315 ce->value = TREE_VEC_ELT (ce->value, 0);
14316 else
14317 {
14318 /* Update the length of the final CONSTRUCTOR
14319 arguments vector, and note that we will need to
14320 copy.*/
14321 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14322 need_copy_p = true;
14323 }
14324 }
14325 else
14326 ce->value = RECUR (ce->value);
14327 }
14328
14329 if (need_copy_p)
14330 {
14331 vec<constructor_elt, va_gc> *old_n = n;
14332
14333 vec_alloc (n, newlen);
14334 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14335 {
14336 if (TREE_CODE (ce->value) == TREE_VEC)
14337 {
14338 int i, len = TREE_VEC_LENGTH (ce->value);
14339 for (i = 0; i < len; ++i)
14340 CONSTRUCTOR_APPEND_ELT (n, 0,
14341 TREE_VEC_ELT (ce->value, i));
14342 }
14343 else
14344 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14345 }
14346 }
14347
14348 r = build_constructor (init_list_type_node, n);
14349 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14350
14351 if (TREE_HAS_CONSTRUCTOR (t))
14352 RETURN (finish_compound_literal (type, r, complain));
14353
14354 TREE_TYPE (r) = type;
14355 RETURN (r);
14356 }
14357
14358 case TYPEID_EXPR:
14359 {
14360 tree operand_0 = TREE_OPERAND (t, 0);
14361 if (TYPE_P (operand_0))
14362 {
14363 operand_0 = tsubst (operand_0, args, complain, in_decl);
14364 RETURN (get_typeid (operand_0, complain));
14365 }
14366 else
14367 {
14368 operand_0 = RECUR (operand_0);
14369 RETURN (build_typeid (operand_0, complain));
14370 }
14371 }
14372
14373 case VAR_DECL:
14374 if (!args)
14375 RETURN (t);
14376 /* Fall through */
14377
14378 case PARM_DECL:
14379 {
14380 tree r = tsubst_copy (t, args, complain, in_decl);
14381
14382 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14383 /* If the original type was a reference, we'll be wrapped in
14384 the appropriate INDIRECT_REF. */
14385 r = convert_from_reference (r);
14386 RETURN (r);
14387 }
14388
14389 case VA_ARG_EXPR:
14390 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14391 RECUR (TREE_OPERAND (t, 0)),
14392 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14393
14394 case OFFSETOF_EXPR:
14395 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14396
14397 case TRAIT_EXPR:
14398 {
14399 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14400 complain, in_decl);
14401
14402 tree type2 = TRAIT_EXPR_TYPE2 (t);
14403 if (type2)
14404 type2 = tsubst_copy (type2, args, complain, in_decl);
14405
14406 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14407 }
14408
14409 case STMT_EXPR:
14410 {
14411 tree old_stmt_expr = cur_stmt_expr;
14412 tree stmt_expr = begin_stmt_expr ();
14413
14414 cur_stmt_expr = stmt_expr;
14415 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14416 integral_constant_expression_p);
14417 stmt_expr = finish_stmt_expr (stmt_expr, false);
14418 cur_stmt_expr = old_stmt_expr;
14419
14420 /* If the resulting list of expression statement is empty,
14421 fold it further into void_zero_node. */
14422 if (empty_expr_stmt_p (stmt_expr))
14423 stmt_expr = void_zero_node;
14424
14425 RETURN (stmt_expr);
14426 }
14427
14428 case LAMBDA_EXPR:
14429 {
14430 tree r = build_lambda_expr ();
14431
14432 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14433 LAMBDA_EXPR_CLOSURE (r) = type;
14434 CLASSTYPE_LAMBDA_EXPR (type) = r;
14435
14436 LAMBDA_EXPR_LOCATION (r)
14437 = LAMBDA_EXPR_LOCATION (t);
14438 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14439 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14440 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14441 LAMBDA_EXPR_DISCRIMINATOR (r)
14442 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14443 /* For a function scope, we want to use tsubst so that we don't
14444 complain about referring to an auto function before its return
14445 type has been deduced. Otherwise, we want to use tsubst_copy so
14446 that we look up the existing field/parameter/variable rather
14447 than build a new one. */
14448 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14449 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14450 scope = tsubst (scope, args, complain, in_decl);
14451 else if (scope && TREE_CODE (scope) == PARM_DECL)
14452 {
14453 /* Look up the parameter we want directly, as tsubst_copy
14454 doesn't do what we need. */
14455 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14456 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14457 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14458 parm = DECL_CHAIN (parm);
14459 scope = parm;
14460 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14461 if (DECL_CONTEXT (scope) == NULL_TREE)
14462 DECL_CONTEXT (scope) = fn;
14463 }
14464 else
14465 scope = RECUR (scope);
14466 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
14467 LAMBDA_EXPR_RETURN_TYPE (r)
14468 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14469
14470 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14471 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14472
14473 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14474 determine_visibility (TYPE_NAME (type));
14475 /* Now that we know visibility, instantiate the type so we have a
14476 declaration of the op() for later calls to lambda_function. */
14477 complete_type (type);
14478
14479 /* The capture list refers to closure members, so this needs to
14480 wait until after we finish instantiating the type. Also keep
14481 any captures that may have been added during instantiation. */
14482 LAMBDA_EXPR_CAPTURE_LIST (r)
14483 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)),
14484 LAMBDA_EXPR_CAPTURE_LIST (r));
14485 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
14486
14487 RETURN (build_lambda_object (r));
14488 }
14489
14490 case TARGET_EXPR:
14491 /* We can get here for a constant initializer of non-dependent type.
14492 FIXME stop folding in cp_parser_initializer_clause. */
14493 {
14494 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
14495 complain);
14496 RETURN (r);
14497 }
14498
14499 case TRANSACTION_EXPR:
14500 RETURN (tsubst_expr(t, args, complain, in_decl,
14501 integral_constant_expression_p));
14502
14503 default:
14504 /* Handle Objective-C++ constructs, if appropriate. */
14505 {
14506 tree subst
14507 = objcp_tsubst_copy_and_build (t, args, complain,
14508 in_decl, /*function_p=*/false);
14509 if (subst)
14510 RETURN (subst);
14511 }
14512 RETURN (tsubst_copy (t, args, complain, in_decl));
14513 }
14514
14515 #undef RECUR
14516 #undef RETURN
14517 out:
14518 input_location = loc;
14519 return retval;
14520 }
14521
14522 /* Verify that the instantiated ARGS are valid. For type arguments,
14523 make sure that the type's linkage is ok. For non-type arguments,
14524 make sure they are constants if they are integral or enumerations.
14525 Emit an error under control of COMPLAIN, and return TRUE on error. */
14526
14527 static bool
14528 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14529 {
14530 if (dependent_template_arg_p (t))
14531 return false;
14532 if (ARGUMENT_PACK_P (t))
14533 {
14534 tree vec = ARGUMENT_PACK_ARGS (t);
14535 int len = TREE_VEC_LENGTH (vec);
14536 bool result = false;
14537 int i;
14538
14539 for (i = 0; i < len; ++i)
14540 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14541 result = true;
14542 return result;
14543 }
14544 else if (TYPE_P (t))
14545 {
14546 /* [basic.link]: A name with no linkage (notably, the name
14547 of a class or enumeration declared in a local scope)
14548 shall not be used to declare an entity with linkage.
14549 This implies that names with no linkage cannot be used as
14550 template arguments
14551
14552 DR 757 relaxes this restriction for C++0x. */
14553 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14554 : no_linkage_check (t, /*relaxed_p=*/false));
14555
14556 if (nt)
14557 {
14558 /* DR 488 makes use of a type with no linkage cause
14559 type deduction to fail. */
14560 if (complain & tf_error)
14561 {
14562 if (TYPE_ANONYMOUS_P (nt))
14563 error ("%qT is/uses anonymous type", t);
14564 else
14565 error ("template argument for %qD uses local type %qT",
14566 tmpl, t);
14567 }
14568 return true;
14569 }
14570 /* In order to avoid all sorts of complications, we do not
14571 allow variably-modified types as template arguments. */
14572 else if (variably_modified_type_p (t, NULL_TREE))
14573 {
14574 if (complain & tf_error)
14575 error ("%qT is a variably modified type", t);
14576 return true;
14577 }
14578 }
14579 /* Class template and alias template arguments should be OK. */
14580 else if (DECL_TYPE_TEMPLATE_P (t))
14581 ;
14582 /* A non-type argument of integral or enumerated type must be a
14583 constant. */
14584 else if (TREE_TYPE (t)
14585 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14586 && !TREE_CONSTANT (t))
14587 {
14588 if (complain & tf_error)
14589 error ("integral expression %qE is not constant", t);
14590 return true;
14591 }
14592 return false;
14593 }
14594
14595 static bool
14596 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14597 {
14598 int ix, len = DECL_NTPARMS (tmpl);
14599 bool result = false;
14600
14601 for (ix = 0; ix != len; ix++)
14602 {
14603 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14604 result = true;
14605 }
14606 if (result && (complain & tf_error))
14607 error (" trying to instantiate %qD", tmpl);
14608 return result;
14609 }
14610
14611 /* We're out of SFINAE context now, so generate diagnostics for the access
14612 errors we saw earlier when instantiating D from TMPL and ARGS. */
14613
14614 static void
14615 recheck_decl_substitution (tree d, tree tmpl, tree args)
14616 {
14617 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
14618 tree type = TREE_TYPE (pattern);
14619 location_t loc = input_location;
14620
14621 push_access_scope (d);
14622 push_deferring_access_checks (dk_no_deferred);
14623 input_location = DECL_SOURCE_LOCATION (pattern);
14624 tsubst (type, args, tf_warning_or_error, d);
14625 input_location = loc;
14626 pop_deferring_access_checks ();
14627 pop_access_scope (d);
14628 }
14629
14630 /* Instantiate the indicated variable, function, or alias template TMPL with
14631 the template arguments in TARG_PTR. */
14632
14633 static tree
14634 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14635 {
14636 tree targ_ptr = orig_args;
14637 tree fndecl;
14638 tree gen_tmpl;
14639 tree spec;
14640 bool access_ok = true;
14641
14642 if (tmpl == error_mark_node)
14643 return error_mark_node;
14644
14645 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14646
14647 /* If this function is a clone, handle it specially. */
14648 if (DECL_CLONED_FUNCTION_P (tmpl))
14649 {
14650 tree spec;
14651 tree clone;
14652
14653 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14654 DECL_CLONED_FUNCTION. */
14655 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14656 targ_ptr, complain);
14657 if (spec == error_mark_node)
14658 return error_mark_node;
14659
14660 /* Look for the clone. */
14661 FOR_EACH_CLONE (clone, spec)
14662 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14663 return clone;
14664 /* We should always have found the clone by now. */
14665 gcc_unreachable ();
14666 return NULL_TREE;
14667 }
14668
14669 /* Check to see if we already have this specialization. */
14670 gen_tmpl = most_general_template (tmpl);
14671 if (tmpl != gen_tmpl)
14672 /* The TMPL is a partial instantiation. To get a full set of
14673 arguments we must add the arguments used to perform the
14674 partial instantiation. */
14675 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14676 targ_ptr);
14677
14678 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14679 but it doesn't seem to be on the hot path. */
14680 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14681
14682 gcc_assert (tmpl == gen_tmpl
14683 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14684 == spec)
14685 || fndecl == NULL_TREE);
14686
14687 if (spec != NULL_TREE)
14688 {
14689 if (FNDECL_HAS_ACCESS_ERRORS (spec))
14690 {
14691 if (complain & tf_error)
14692 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
14693 return error_mark_node;
14694 }
14695 return spec;
14696 }
14697
14698 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14699 complain))
14700 return error_mark_node;
14701
14702 /* We are building a FUNCTION_DECL, during which the access of its
14703 parameters and return types have to be checked. However this
14704 FUNCTION_DECL which is the desired context for access checking
14705 is not built yet. We solve this chicken-and-egg problem by
14706 deferring all checks until we have the FUNCTION_DECL. */
14707 push_deferring_access_checks (dk_deferred);
14708
14709 /* Instantiation of the function happens in the context of the function
14710 template, not the context of the overload resolution we're doing. */
14711 push_to_top_level ();
14712 /* If there are dependent arguments, e.g. because we're doing partial
14713 ordering, make sure processing_template_decl stays set. */
14714 if (uses_template_parms (targ_ptr))
14715 ++processing_template_decl;
14716 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14717 {
14718 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14719 complain, gen_tmpl);
14720 push_nested_class (ctx);
14721 }
14722 /* Substitute template parameters to obtain the specialization. */
14723 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14724 targ_ptr, complain, gen_tmpl);
14725 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14726 pop_nested_class ();
14727 pop_from_top_level ();
14728
14729 if (fndecl == error_mark_node)
14730 {
14731 pop_deferring_access_checks ();
14732 return error_mark_node;
14733 }
14734
14735 /* The DECL_TI_TEMPLATE should always be the immediate parent
14736 template, not the most general template. */
14737 DECL_TI_TEMPLATE (fndecl) = tmpl;
14738
14739 /* Now we know the specialization, compute access previously
14740 deferred. */
14741 push_access_scope (fndecl);
14742 if (!perform_deferred_access_checks (complain))
14743 access_ok = false;
14744 pop_access_scope (fndecl);
14745 pop_deferring_access_checks ();
14746
14747 /* If we've just instantiated the main entry point for a function,
14748 instantiate all the alternate entry points as well. We do this
14749 by cloning the instantiation of the main entry point, not by
14750 instantiating the template clones. */
14751 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14752 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14753
14754 if (!access_ok)
14755 {
14756 if (!(complain & tf_error))
14757 {
14758 /* Remember to reinstantiate when we're out of SFINAE so the user
14759 can see the errors. */
14760 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
14761 }
14762 return error_mark_node;
14763 }
14764 return fndecl;
14765 }
14766
14767 /* Wrapper for instantiate_template_1. */
14768
14769 tree
14770 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14771 {
14772 tree ret;
14773 timevar_push (TV_TEMPLATE_INST);
14774 ret = instantiate_template_1 (tmpl, orig_args, complain);
14775 timevar_pop (TV_TEMPLATE_INST);
14776 return ret;
14777 }
14778
14779 /* Instantiate the alias template TMPL with ARGS. Also push a template
14780 instantiation level, which instantiate_template doesn't do because
14781 functions and variables have sufficient context established by the
14782 callers. */
14783
14784 static tree
14785 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
14786 {
14787 struct pending_template *old_last_pend = last_pending_template;
14788 struct tinst_level *old_error_tinst = last_error_tinst_level;
14789 if (tmpl == error_mark_node || args == error_mark_node)
14790 return error_mark_node;
14791 tree tinst = build_tree_list (tmpl, args);
14792 if (!push_tinst_level (tinst))
14793 {
14794 ggc_free (tinst);
14795 return error_mark_node;
14796 }
14797
14798 args =
14799 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
14800 args, tmpl, complain,
14801 /*require_all_args=*/true,
14802 /*use_default_args=*/true);
14803
14804 tree r = instantiate_template (tmpl, args, complain);
14805 pop_tinst_level ();
14806 /* We can't free this if a pending_template entry or last_error_tinst_level
14807 is pointing at it. */
14808 if (last_pending_template == old_last_pend
14809 && last_error_tinst_level == old_error_tinst)
14810 ggc_free (tinst);
14811
14812 return r;
14813 }
14814
14815 /* PARM is a template parameter pack for FN. Returns true iff
14816 PARM is used in a deducible way in the argument list of FN. */
14817
14818 static bool
14819 pack_deducible_p (tree parm, tree fn)
14820 {
14821 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14822 for (; t; t = TREE_CHAIN (t))
14823 {
14824 tree type = TREE_VALUE (t);
14825 tree packs;
14826 if (!PACK_EXPANSION_P (type))
14827 continue;
14828 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14829 packs; packs = TREE_CHAIN (packs))
14830 if (TREE_VALUE (packs) == parm)
14831 {
14832 /* The template parameter pack is used in a function parameter
14833 pack. If this is the end of the parameter list, the
14834 template parameter pack is deducible. */
14835 if (TREE_CHAIN (t) == void_list_node)
14836 return true;
14837 else
14838 /* Otherwise, not. Well, it could be deduced from
14839 a non-pack parameter, but doing so would end up with
14840 a deduction mismatch, so don't bother. */
14841 return false;
14842 }
14843 }
14844 /* The template parameter pack isn't used in any function parameter
14845 packs, but it might be used deeper, e.g. tuple<Args...>. */
14846 return true;
14847 }
14848
14849 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14850 NARGS elements of the arguments that are being used when calling
14851 it. TARGS is a vector into which the deduced template arguments
14852 are placed.
14853
14854 Return zero for success, 2 for an incomplete match that doesn't resolve
14855 all the types, and 1 for complete failure. An error message will be
14856 printed only for an incomplete match.
14857
14858 If FN is a conversion operator, or we are trying to produce a specific
14859 specialization, RETURN_TYPE is the return type desired.
14860
14861 The EXPLICIT_TARGS are explicit template arguments provided via a
14862 template-id.
14863
14864 The parameter STRICT is one of:
14865
14866 DEDUCE_CALL:
14867 We are deducing arguments for a function call, as in
14868 [temp.deduct.call].
14869
14870 DEDUCE_CONV:
14871 We are deducing arguments for a conversion function, as in
14872 [temp.deduct.conv].
14873
14874 DEDUCE_EXACT:
14875 We are deducing arguments when doing an explicit instantiation
14876 as in [temp.explicit], when determining an explicit specialization
14877 as in [temp.expl.spec], or when taking the address of a function
14878 template, as in [temp.deduct.funcaddr]. */
14879
14880 tree
14881 fn_type_unification (tree fn,
14882 tree explicit_targs,
14883 tree targs,
14884 const tree *args,
14885 unsigned int nargs,
14886 tree return_type,
14887 unification_kind_t strict,
14888 int flags,
14889 bool explain_p)
14890 {
14891 tree parms;
14892 tree fntype;
14893 tree decl = NULL_TREE;
14894 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
14895 bool ok;
14896 static int deduction_depth;
14897 struct pending_template *old_last_pend = last_pending_template;
14898 struct tinst_level *old_error_tinst = last_error_tinst_level;
14899 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14900 tree tinst;
14901 tree r = error_mark_node;
14902
14903 /* Adjust any explicit template arguments before entering the
14904 substitution context. */
14905 if (explicit_targs)
14906 {
14907 explicit_targs
14908 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14909 complain,
14910 /*require_all_args=*/false,
14911 /*use_default_args=*/false));
14912 if (explicit_targs == error_mark_node)
14913 return error_mark_node;
14914 }
14915
14916 /* In C++0x, it's possible to have a function template whose type depends
14917 on itself recursively. This is most obvious with decltype, but can also
14918 occur with enumeration scope (c++/48969). So we need to catch infinite
14919 recursion and reject the substitution at deduction time; this function
14920 will return error_mark_node for any repeated substitution.
14921
14922 This also catches excessive recursion such as when f<N> depends on
14923 f<N-1> across all integers, and returns error_mark_node for all the
14924 substitutions back up to the initial one.
14925
14926 This is, of course, not reentrant. */
14927 if (excessive_deduction_depth)
14928 return error_mark_node;
14929 tinst = build_tree_list (fn, targs);
14930 if (!push_tinst_level (tinst))
14931 {
14932 excessive_deduction_depth = true;
14933 ggc_free (tinst);
14934 return error_mark_node;
14935 }
14936 ++deduction_depth;
14937 push_deferring_access_checks (dk_deferred);
14938
14939 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14940
14941 fntype = TREE_TYPE (fn);
14942 if (explicit_targs)
14943 {
14944 /* [temp.deduct]
14945
14946 The specified template arguments must match the template
14947 parameters in kind (i.e., type, nontype, template), and there
14948 must not be more arguments than there are parameters;
14949 otherwise type deduction fails.
14950
14951 Nontype arguments must match the types of the corresponding
14952 nontype template parameters, or must be convertible to the
14953 types of the corresponding nontype parameters as specified in
14954 _temp.arg.nontype_, otherwise type deduction fails.
14955
14956 All references in the function type of the function template
14957 to the corresponding template parameters are replaced by the
14958 specified template argument values. If a substitution in a
14959 template parameter or in the function type of the function
14960 template results in an invalid type, type deduction fails. */
14961 int i, len = TREE_VEC_LENGTH (tparms);
14962 location_t loc = input_location;
14963 bool incomplete = false;
14964
14965 /* Substitute the explicit args into the function type. This is
14966 necessary so that, for instance, explicitly declared function
14967 arguments can match null pointed constants. If we were given
14968 an incomplete set of explicit args, we must not do semantic
14969 processing during substitution as we could create partial
14970 instantiations. */
14971 for (i = 0; i < len; i++)
14972 {
14973 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14974 bool parameter_pack = false;
14975 tree targ = TREE_VEC_ELT (explicit_targs, i);
14976
14977 /* Dig out the actual parm. */
14978 if (TREE_CODE (parm) == TYPE_DECL
14979 || TREE_CODE (parm) == TEMPLATE_DECL)
14980 {
14981 parm = TREE_TYPE (parm);
14982 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14983 }
14984 else if (TREE_CODE (parm) == PARM_DECL)
14985 {
14986 parm = DECL_INITIAL (parm);
14987 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14988 }
14989
14990 if (!parameter_pack && targ == NULL_TREE)
14991 /* No explicit argument for this template parameter. */
14992 incomplete = true;
14993
14994 if (parameter_pack && pack_deducible_p (parm, fn))
14995 {
14996 /* Mark the argument pack as "incomplete". We could
14997 still deduce more arguments during unification.
14998 We remove this mark in type_unification_real. */
14999 if (targ)
15000 {
15001 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15002 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15003 = ARGUMENT_PACK_ARGS (targ);
15004 }
15005
15006 /* We have some incomplete argument packs. */
15007 incomplete = true;
15008 }
15009 }
15010
15011 processing_template_decl += incomplete;
15012 input_location = DECL_SOURCE_LOCATION (fn);
15013 TREE_VALUE (tinst) = explicit_targs;
15014 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15015 complain | tf_partial, NULL_TREE);
15016 TREE_VALUE (tinst) = targs;
15017 input_location = loc;
15018 processing_template_decl -= incomplete;
15019
15020 if (fntype == error_mark_node)
15021 goto fail;
15022
15023 /* Throw away these access checks; we'll see them again in
15024 instantiate_template and they might have the wrong
15025 access path at this point. */
15026 pop_deferring_access_checks ();
15027 push_deferring_access_checks (dk_deferred);
15028
15029 /* Place the explicitly specified arguments in TARGS. */
15030 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15031 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15032 }
15033
15034 /* Never do unification on the 'this' parameter. */
15035 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15036
15037 if (return_type)
15038 {
15039 tree *new_args;
15040
15041 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15042 new_args = XALLOCAVEC (tree, nargs + 1);
15043 new_args[0] = return_type;
15044 memcpy (new_args + 1, args, nargs * sizeof (tree));
15045 args = new_args;
15046 ++nargs;
15047 }
15048
15049 /* We allow incomplete unification without an error message here
15050 because the standard doesn't seem to explicitly prohibit it. Our
15051 callers must be ready to deal with unification failures in any
15052 event. */
15053
15054 pop_tinst_level ();
15055 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15056 targs, parms, args, nargs, /*subr=*/0,
15057 strict, flags, explain_p);
15058 push_tinst_level (tinst);
15059 if (!ok)
15060 goto fail;
15061
15062 /* Now that we have bindings for all of the template arguments,
15063 ensure that the arguments deduced for the template template
15064 parameters have compatible template parameter lists. We cannot
15065 check this property before we have deduced all template
15066 arguments, because the template parameter types of a template
15067 template parameter might depend on prior template parameters
15068 deduced after the template template parameter. The following
15069 ill-formed example illustrates this issue:
15070
15071 template<typename T, template<T> class C> void f(C<5>, T);
15072
15073 template<int N> struct X {};
15074
15075 void g() {
15076 f(X<5>(), 5l); // error: template argument deduction fails
15077 }
15078
15079 The template parameter list of 'C' depends on the template type
15080 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15081 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15082 time that we deduce 'C'. */
15083 if (!template_template_parm_bindings_ok_p
15084 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15085 {
15086 unify_inconsistent_template_template_parameters (explain_p);
15087 goto fail;
15088 }
15089
15090 /* All is well so far. Now, check:
15091
15092 [temp.deduct]
15093
15094 When all template arguments have been deduced, all uses of
15095 template parameters in nondeduced contexts are replaced with
15096 the corresponding deduced argument values. If the
15097 substitution results in an invalid type, as described above,
15098 type deduction fails. */
15099 decl = instantiate_template (fn, targs, complain);
15100 if (decl == error_mark_node)
15101 goto fail;
15102
15103 /* Now perform any access checks encountered during deduction, such as
15104 for default template arguments. */
15105 push_access_scope (decl);
15106 ok = perform_deferred_access_checks (complain);
15107 pop_access_scope (decl);
15108 if (!ok)
15109 goto fail;
15110
15111 /* If we're looking for an exact match, check that what we got
15112 is indeed an exact match. It might not be if some template
15113 parameters are used in non-deduced contexts. */
15114 if (strict == DEDUCE_EXACT)
15115 {
15116 tree substed = TREE_TYPE (decl);
15117 unsigned int i;
15118
15119 tree sarg
15120 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15121 if (return_type)
15122 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15123 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15124 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15125 {
15126 unify_type_mismatch (explain_p, args[i],
15127 TREE_VALUE (sarg));
15128 goto fail;
15129 }
15130 }
15131
15132 r = decl;
15133
15134 fail:
15135 pop_deferring_access_checks ();
15136 --deduction_depth;
15137 if (excessive_deduction_depth)
15138 {
15139 if (deduction_depth == 0)
15140 /* Reset once we're all the way out. */
15141 excessive_deduction_depth = false;
15142 }
15143
15144 pop_tinst_level ();
15145 /* We can't free this if a pending_template entry or last_error_tinst_level
15146 is pointing at it. */
15147 if (last_pending_template == old_last_pend
15148 && last_error_tinst_level == old_error_tinst)
15149 ggc_free (tinst);
15150
15151 return r;
15152 }
15153
15154 /* Adjust types before performing type deduction, as described in
15155 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15156 sections are symmetric. PARM is the type of a function parameter
15157 or the return type of the conversion function. ARG is the type of
15158 the argument passed to the call, or the type of the value
15159 initialized with the result of the conversion function.
15160 ARG_EXPR is the original argument expression, which may be null. */
15161
15162 static int
15163 maybe_adjust_types_for_deduction (unification_kind_t strict,
15164 tree* parm,
15165 tree* arg,
15166 tree arg_expr)
15167 {
15168 int result = 0;
15169
15170 switch (strict)
15171 {
15172 case DEDUCE_CALL:
15173 break;
15174
15175 case DEDUCE_CONV:
15176 {
15177 /* Swap PARM and ARG throughout the remainder of this
15178 function; the handling is precisely symmetric since PARM
15179 will initialize ARG rather than vice versa. */
15180 tree* temp = parm;
15181 parm = arg;
15182 arg = temp;
15183 break;
15184 }
15185
15186 case DEDUCE_EXACT:
15187 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15188 too, but here handle it by stripping the reference from PARM
15189 rather than by adding it to ARG. */
15190 if (TREE_CODE (*parm) == REFERENCE_TYPE
15191 && TYPE_REF_IS_RVALUE (*parm)
15192 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15193 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15194 && TREE_CODE (*arg) == REFERENCE_TYPE
15195 && !TYPE_REF_IS_RVALUE (*arg))
15196 *parm = TREE_TYPE (*parm);
15197 /* Nothing else to do in this case. */
15198 return 0;
15199
15200 default:
15201 gcc_unreachable ();
15202 }
15203
15204 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15205 {
15206 /* [temp.deduct.call]
15207
15208 If P is not a reference type:
15209
15210 --If A is an array type, the pointer type produced by the
15211 array-to-pointer standard conversion (_conv.array_) is
15212 used in place of A for type deduction; otherwise,
15213
15214 --If A is a function type, the pointer type produced by
15215 the function-to-pointer standard conversion
15216 (_conv.func_) is used in place of A for type deduction;
15217 otherwise,
15218
15219 --If A is a cv-qualified type, the top level
15220 cv-qualifiers of A's type are ignored for type
15221 deduction. */
15222 if (TREE_CODE (*arg) == ARRAY_TYPE)
15223 *arg = build_pointer_type (TREE_TYPE (*arg));
15224 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15225 *arg = build_pointer_type (*arg);
15226 else
15227 *arg = TYPE_MAIN_VARIANT (*arg);
15228 }
15229
15230 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15231 of the form T&&, where T is a template parameter, and the argument
15232 is an lvalue, T is deduced as A& */
15233 if (TREE_CODE (*parm) == REFERENCE_TYPE
15234 && TYPE_REF_IS_RVALUE (*parm)
15235 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15236 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15237 && (arg_expr ? real_lvalue_p (arg_expr)
15238 /* try_one_overload doesn't provide an arg_expr, but
15239 functions are always lvalues. */
15240 : TREE_CODE (*arg) == FUNCTION_TYPE))
15241 *arg = build_reference_type (*arg);
15242
15243 /* [temp.deduct.call]
15244
15245 If P is a cv-qualified type, the top level cv-qualifiers
15246 of P's type are ignored for type deduction. If P is a
15247 reference type, the type referred to by P is used for
15248 type deduction. */
15249 *parm = TYPE_MAIN_VARIANT (*parm);
15250 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15251 {
15252 *parm = TREE_TYPE (*parm);
15253 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15254 }
15255
15256 /* DR 322. For conversion deduction, remove a reference type on parm
15257 too (which has been swapped into ARG). */
15258 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15259 *arg = TREE_TYPE (*arg);
15260
15261 return result;
15262 }
15263
15264 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15265 template which does contain any deducible template parameters; check if
15266 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15267 unify_one_argument. */
15268
15269 static int
15270 check_non_deducible_conversion (tree parm, tree arg, int strict,
15271 int flags, bool explain_p)
15272 {
15273 tree type;
15274
15275 if (!TYPE_P (arg))
15276 type = TREE_TYPE (arg);
15277 else
15278 type = arg;
15279
15280 if (same_type_p (parm, type))
15281 return unify_success (explain_p);
15282
15283 if (strict == DEDUCE_CONV)
15284 {
15285 if (can_convert_arg (type, parm, NULL_TREE, flags,
15286 explain_p ? tf_warning_or_error : tf_none))
15287 return unify_success (explain_p);
15288 }
15289 else if (strict != DEDUCE_EXACT)
15290 {
15291 if (can_convert_arg (parm, type,
15292 TYPE_P (arg) ? NULL_TREE : arg,
15293 flags, explain_p ? tf_warning_or_error : tf_none))
15294 return unify_success (explain_p);
15295 }
15296
15297 if (strict == DEDUCE_EXACT)
15298 return unify_type_mismatch (explain_p, parm, arg);
15299 else
15300 return unify_arg_conversion (explain_p, parm, type, arg);
15301 }
15302
15303 /* Subroutine of type_unification_real and unify_pack_expansion to
15304 handle unification of a single P/A pair. Parameters are as
15305 for those functions. */
15306
15307 static int
15308 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
15309 int subr, unification_kind_t strict, int flags,
15310 bool explain_p)
15311 {
15312 tree arg_expr = NULL_TREE;
15313 int arg_strict;
15314
15315 if (arg == error_mark_node || parm == error_mark_node)
15316 return unify_invalid (explain_p);
15317 if (arg == unknown_type_node)
15318 /* We can't deduce anything from this, but we might get all the
15319 template args from other function args. */
15320 return unify_success (explain_p);
15321
15322 /* FIXME uses_deducible_template_parms */
15323 if (TYPE_P (parm) && !uses_template_parms (parm))
15324 return check_non_deducible_conversion (parm, arg, strict, flags,
15325 explain_p);
15326
15327 switch (strict)
15328 {
15329 case DEDUCE_CALL:
15330 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
15331 | UNIFY_ALLOW_MORE_CV_QUAL
15332 | UNIFY_ALLOW_DERIVED);
15333 break;
15334
15335 case DEDUCE_CONV:
15336 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15337 break;
15338
15339 case DEDUCE_EXACT:
15340 arg_strict = UNIFY_ALLOW_NONE;
15341 break;
15342
15343 default:
15344 gcc_unreachable ();
15345 }
15346
15347 /* We only do these transformations if this is the top-level
15348 parameter_type_list in a call or declaration matching; in other
15349 situations (nested function declarators, template argument lists) we
15350 won't be comparing a type to an expression, and we don't do any type
15351 adjustments. */
15352 if (!subr)
15353 {
15354 if (!TYPE_P (arg))
15355 {
15356 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15357 if (type_unknown_p (arg))
15358 {
15359 /* [temp.deduct.type] A template-argument can be
15360 deduced from a pointer to function or pointer
15361 to member function argument if the set of
15362 overloaded functions does not contain function
15363 templates and at most one of a set of
15364 overloaded functions provides a unique
15365 match. */
15366
15367 if (resolve_overloaded_unification
15368 (tparms, targs, parm, arg, strict,
15369 arg_strict, explain_p))
15370 return unify_success (explain_p);
15371 return unify_overload_resolution_failure (explain_p, arg);
15372 }
15373
15374 arg_expr = arg;
15375 arg = unlowered_expr_type (arg);
15376 if (arg == error_mark_node)
15377 return unify_invalid (explain_p);
15378 }
15379
15380 arg_strict |=
15381 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
15382 }
15383 else
15384 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
15385 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
15386
15387 /* For deduction from an init-list we need the actual list. */
15388 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15389 arg = arg_expr;
15390 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
15391 }
15392
15393 /* Most parms like fn_type_unification.
15394
15395 If SUBR is 1, we're being called recursively (to unify the
15396 arguments of a function or method parameter of a function
15397 template). */
15398
15399 static int
15400 type_unification_real (tree tparms,
15401 tree targs,
15402 tree xparms,
15403 const tree *xargs,
15404 unsigned int xnargs,
15405 int subr,
15406 unification_kind_t strict,
15407 int flags,
15408 bool explain_p)
15409 {
15410 tree parm, arg;
15411 int i;
15412 int ntparms = TREE_VEC_LENGTH (tparms);
15413 int saw_undeduced = 0;
15414 tree parms;
15415 const tree *args;
15416 unsigned int nargs;
15417 unsigned int ia;
15418
15419 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15420 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15421 gcc_assert (ntparms > 0);
15422
15423 /* Reset the number of non-defaulted template arguments contained
15424 in TARGS. */
15425 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15426
15427 again:
15428 parms = xparms;
15429 args = xargs;
15430 nargs = xnargs;
15431
15432 ia = 0;
15433 while (parms && parms != void_list_node
15434 && ia < nargs)
15435 {
15436 parm = TREE_VALUE (parms);
15437
15438 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15439 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15440 /* For a function parameter pack that occurs at the end of the
15441 parameter-declaration-list, the type A of each remaining
15442 argument of the call is compared with the type P of the
15443 declarator-id of the function parameter pack. */
15444 break;
15445
15446 parms = TREE_CHAIN (parms);
15447
15448 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15449 /* For a function parameter pack that does not occur at the
15450 end of the parameter-declaration-list, the type of the
15451 parameter pack is a non-deduced context. */
15452 continue;
15453
15454 arg = args[ia];
15455 ++ia;
15456
15457 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15458 flags, explain_p))
15459 return 1;
15460 }
15461
15462 if (parms
15463 && parms != void_list_node
15464 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15465 {
15466 /* Unify the remaining arguments with the pack expansion type. */
15467 tree argvec;
15468 tree parmvec = make_tree_vec (1);
15469
15470 /* Allocate a TREE_VEC and copy in all of the arguments */
15471 argvec = make_tree_vec (nargs - ia);
15472 for (i = 0; ia < nargs; ++ia, ++i)
15473 TREE_VEC_ELT (argvec, i) = args[ia];
15474
15475 /* Copy the parameter into parmvec. */
15476 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15477 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15478 /*subr=*/subr, explain_p))
15479 return 1;
15480
15481 /* Advance to the end of the list of parameters. */
15482 parms = TREE_CHAIN (parms);
15483 }
15484
15485 /* Fail if we've reached the end of the parm list, and more args
15486 are present, and the parm list isn't variadic. */
15487 if (ia < nargs && parms == void_list_node)
15488 return unify_too_many_arguments (explain_p, nargs, ia);
15489 /* Fail if parms are left and they don't have default values. */
15490 if (parms && parms != void_list_node
15491 && TREE_PURPOSE (parms) == NULL_TREE)
15492 {
15493 unsigned int count = nargs;
15494 tree p = parms;
15495 while (p && p != void_list_node)
15496 {
15497 count++;
15498 p = TREE_CHAIN (p);
15499 }
15500 return unify_too_few_arguments (explain_p, ia, count);
15501 }
15502
15503 if (!subr)
15504 {
15505 tsubst_flags_t complain = (explain_p
15506 ? tf_warning_or_error
15507 : tf_none);
15508
15509 for (i = 0; i < ntparms; i++)
15510 {
15511 tree targ = TREE_VEC_ELT (targs, i);
15512 tree tparm = TREE_VEC_ELT (tparms, i);
15513
15514 /* Clear the "incomplete" flags on all argument packs now so that
15515 substituting them into later default arguments works. */
15516 if (targ && ARGUMENT_PACK_P (targ))
15517 {
15518 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15519 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15520 }
15521
15522 if (targ || tparm == error_mark_node)
15523 continue;
15524 tparm = TREE_VALUE (tparm);
15525
15526 /* If this is an undeduced nontype parameter that depends on
15527 a type parameter, try another pass; its type may have been
15528 deduced from a later argument than the one from which
15529 this parameter can be deduced. */
15530 if (TREE_CODE (tparm) == PARM_DECL
15531 && uses_template_parms (TREE_TYPE (tparm))
15532 && !saw_undeduced++)
15533 goto again;
15534
15535 /* Core issue #226 (C++0x) [temp.deduct]:
15536
15537 If a template argument has not been deduced, its
15538 default template argument, if any, is used.
15539
15540 When we are in C++98 mode, TREE_PURPOSE will either
15541 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15542 to explicitly check cxx_dialect here. */
15543 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15544 {
15545 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15546 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15547 location_t save_loc = input_location;
15548 if (DECL_P (parm))
15549 input_location = DECL_SOURCE_LOCATION (parm);
15550 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15551 arg = convert_template_argument (parm, arg, targs, complain,
15552 i, NULL_TREE);
15553 input_location = save_loc;
15554 if (arg == error_mark_node)
15555 return 1;
15556 else
15557 {
15558 TREE_VEC_ELT (targs, i) = arg;
15559 /* The position of the first default template argument,
15560 is also the number of non-defaulted arguments in TARGS.
15561 Record that. */
15562 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15563 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15564 continue;
15565 }
15566 }
15567
15568 /* If the type parameter is a parameter pack, then it will
15569 be deduced to an empty parameter pack. */
15570 if (template_parameter_pack_p (tparm))
15571 {
15572 tree arg;
15573
15574 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15575 {
15576 arg = make_node (NONTYPE_ARGUMENT_PACK);
15577 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15578 TREE_CONSTANT (arg) = 1;
15579 }
15580 else
15581 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15582
15583 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15584
15585 TREE_VEC_ELT (targs, i) = arg;
15586 continue;
15587 }
15588
15589 return unify_parameter_deduction_failure (explain_p, tparm);
15590 }
15591 }
15592 #ifdef ENABLE_CHECKING
15593 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15594 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15595 #endif
15596
15597 return unify_success (explain_p);
15598 }
15599
15600 /* Subroutine of type_unification_real. Args are like the variables
15601 at the call site. ARG is an overloaded function (or template-id);
15602 we try deducing template args from each of the overloads, and if
15603 only one succeeds, we go with that. Modifies TARGS and returns
15604 true on success. */
15605
15606 static bool
15607 resolve_overloaded_unification (tree tparms,
15608 tree targs,
15609 tree parm,
15610 tree arg,
15611 unification_kind_t strict,
15612 int sub_strict,
15613 bool explain_p)
15614 {
15615 tree tempargs = copy_node (targs);
15616 int good = 0;
15617 tree goodfn = NULL_TREE;
15618 bool addr_p;
15619
15620 if (TREE_CODE (arg) == ADDR_EXPR)
15621 {
15622 arg = TREE_OPERAND (arg, 0);
15623 addr_p = true;
15624 }
15625 else
15626 addr_p = false;
15627
15628 if (TREE_CODE (arg) == COMPONENT_REF)
15629 /* Handle `&x' where `x' is some static or non-static member
15630 function name. */
15631 arg = TREE_OPERAND (arg, 1);
15632
15633 if (TREE_CODE (arg) == OFFSET_REF)
15634 arg = TREE_OPERAND (arg, 1);
15635
15636 /* Strip baselink information. */
15637 if (BASELINK_P (arg))
15638 arg = BASELINK_FUNCTIONS (arg);
15639
15640 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15641 {
15642 /* If we got some explicit template args, we need to plug them into
15643 the affected templates before we try to unify, in case the
15644 explicit args will completely resolve the templates in question. */
15645
15646 int ok = 0;
15647 tree expl_subargs = TREE_OPERAND (arg, 1);
15648 arg = TREE_OPERAND (arg, 0);
15649
15650 for (; arg; arg = OVL_NEXT (arg))
15651 {
15652 tree fn = OVL_CURRENT (arg);
15653 tree subargs, elem;
15654
15655 if (TREE_CODE (fn) != TEMPLATE_DECL)
15656 continue;
15657
15658 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15659 expl_subargs, NULL_TREE, tf_none,
15660 /*require_all_args=*/true,
15661 /*use_default_args=*/true);
15662 if (subargs != error_mark_node
15663 && !any_dependent_template_arguments_p (subargs))
15664 {
15665 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15666 if (try_one_overload (tparms, targs, tempargs, parm,
15667 elem, strict, sub_strict, addr_p, explain_p)
15668 && (!goodfn || !same_type_p (goodfn, elem)))
15669 {
15670 goodfn = elem;
15671 ++good;
15672 }
15673 }
15674 else if (subargs)
15675 ++ok;
15676 }
15677 /* If no templates (or more than one) are fully resolved by the
15678 explicit arguments, this template-id is a non-deduced context; it
15679 could still be OK if we deduce all template arguments for the
15680 enclosing call through other arguments. */
15681 if (good != 1)
15682 good = ok;
15683 }
15684 else if (TREE_CODE (arg) != OVERLOAD
15685 && TREE_CODE (arg) != FUNCTION_DECL)
15686 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15687 -- but the deduction does not succeed because the expression is
15688 not just the function on its own. */
15689 return false;
15690 else
15691 for (; arg; arg = OVL_NEXT (arg))
15692 if (try_one_overload (tparms, targs, tempargs, parm,
15693 TREE_TYPE (OVL_CURRENT (arg)),
15694 strict, sub_strict, addr_p, explain_p)
15695 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15696 {
15697 goodfn = OVL_CURRENT (arg);
15698 ++good;
15699 }
15700
15701 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15702 to function or pointer to member function argument if the set of
15703 overloaded functions does not contain function templates and at most
15704 one of a set of overloaded functions provides a unique match.
15705
15706 So if we found multiple possibilities, we return success but don't
15707 deduce anything. */
15708
15709 if (good == 1)
15710 {
15711 int i = TREE_VEC_LENGTH (targs);
15712 for (; i--; )
15713 if (TREE_VEC_ELT (tempargs, i))
15714 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15715 }
15716 if (good)
15717 return true;
15718
15719 return false;
15720 }
15721
15722 /* Core DR 115: In contexts where deduction is done and fails, or in
15723 contexts where deduction is not done, if a template argument list is
15724 specified and it, along with any default template arguments, identifies
15725 a single function template specialization, then the template-id is an
15726 lvalue for the function template specialization. */
15727
15728 tree
15729 resolve_nondeduced_context (tree orig_expr)
15730 {
15731 tree expr, offset, baselink;
15732 bool addr;
15733
15734 if (!type_unknown_p (orig_expr))
15735 return orig_expr;
15736
15737 expr = orig_expr;
15738 addr = false;
15739 offset = NULL_TREE;
15740 baselink = NULL_TREE;
15741
15742 if (TREE_CODE (expr) == ADDR_EXPR)
15743 {
15744 expr = TREE_OPERAND (expr, 0);
15745 addr = true;
15746 }
15747 if (TREE_CODE (expr) == OFFSET_REF)
15748 {
15749 offset = expr;
15750 expr = TREE_OPERAND (expr, 1);
15751 }
15752 if (BASELINK_P (expr))
15753 {
15754 baselink = expr;
15755 expr = BASELINK_FUNCTIONS (expr);
15756 }
15757
15758 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15759 {
15760 int good = 0;
15761 tree goodfn = NULL_TREE;
15762
15763 /* If we got some explicit template args, we need to plug them into
15764 the affected templates before we try to unify, in case the
15765 explicit args will completely resolve the templates in question. */
15766
15767 tree expl_subargs = TREE_OPERAND (expr, 1);
15768 tree arg = TREE_OPERAND (expr, 0);
15769 tree badfn = NULL_TREE;
15770 tree badargs = NULL_TREE;
15771
15772 for (; arg; arg = OVL_NEXT (arg))
15773 {
15774 tree fn = OVL_CURRENT (arg);
15775 tree subargs, elem;
15776
15777 if (TREE_CODE (fn) != TEMPLATE_DECL)
15778 continue;
15779
15780 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15781 expl_subargs, NULL_TREE, tf_none,
15782 /*require_all_args=*/true,
15783 /*use_default_args=*/true);
15784 if (subargs != error_mark_node
15785 && !any_dependent_template_arguments_p (subargs))
15786 {
15787 elem = instantiate_template (fn, subargs, tf_none);
15788 if (elem == error_mark_node)
15789 {
15790 badfn = fn;
15791 badargs = subargs;
15792 }
15793 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15794 {
15795 goodfn = elem;
15796 ++good;
15797 }
15798 }
15799 }
15800 if (good == 1)
15801 {
15802 mark_used (goodfn);
15803 expr = goodfn;
15804 if (baselink)
15805 expr = build_baselink (BASELINK_BINFO (baselink),
15806 BASELINK_ACCESS_BINFO (baselink),
15807 expr, BASELINK_OPTYPE (baselink));
15808 if (offset)
15809 {
15810 tree base
15811 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15812 expr = build_offset_ref (base, expr, addr);
15813 }
15814 if (addr)
15815 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15816 return expr;
15817 }
15818 else if (good == 0 && badargs)
15819 /* There were no good options and at least one bad one, so let the
15820 user know what the problem is. */
15821 instantiate_template (badfn, badargs, tf_warning_or_error);
15822 }
15823 return orig_expr;
15824 }
15825
15826 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15827 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15828 different overloads deduce different arguments for a given parm.
15829 ADDR_P is true if the expression for which deduction is being
15830 performed was of the form "& fn" rather than simply "fn".
15831
15832 Returns 1 on success. */
15833
15834 static int
15835 try_one_overload (tree tparms,
15836 tree orig_targs,
15837 tree targs,
15838 tree parm,
15839 tree arg,
15840 unification_kind_t strict,
15841 int sub_strict,
15842 bool addr_p,
15843 bool explain_p)
15844 {
15845 int nargs;
15846 tree tempargs;
15847 int i;
15848
15849 if (arg == error_mark_node)
15850 return 0;
15851
15852 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15853 to function or pointer to member function argument if the set of
15854 overloaded functions does not contain function templates and at most
15855 one of a set of overloaded functions provides a unique match.
15856
15857 So if this is a template, just return success. */
15858
15859 if (uses_template_parms (arg))
15860 return 1;
15861
15862 if (TREE_CODE (arg) == METHOD_TYPE)
15863 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15864 else if (addr_p)
15865 arg = build_pointer_type (arg);
15866
15867 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15868
15869 /* We don't copy orig_targs for this because if we have already deduced
15870 some template args from previous args, unify would complain when we
15871 try to deduce a template parameter for the same argument, even though
15872 there isn't really a conflict. */
15873 nargs = TREE_VEC_LENGTH (targs);
15874 tempargs = make_tree_vec (nargs);
15875
15876 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15877 return 0;
15878
15879 /* First make sure we didn't deduce anything that conflicts with
15880 explicitly specified args. */
15881 for (i = nargs; i--; )
15882 {
15883 tree elt = TREE_VEC_ELT (tempargs, i);
15884 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15885
15886 if (!elt)
15887 /*NOP*/;
15888 else if (uses_template_parms (elt))
15889 /* Since we're unifying against ourselves, we will fill in
15890 template args used in the function parm list with our own
15891 template parms. Discard them. */
15892 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15893 else if (oldelt && !template_args_equal (oldelt, elt))
15894 return 0;
15895 }
15896
15897 for (i = nargs; i--; )
15898 {
15899 tree elt = TREE_VEC_ELT (tempargs, i);
15900
15901 if (elt)
15902 TREE_VEC_ELT (targs, i) = elt;
15903 }
15904
15905 return 1;
15906 }
15907
15908 /* PARM is a template class (perhaps with unbound template
15909 parameters). ARG is a fully instantiated type. If ARG can be
15910 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15911 TARGS are as for unify. */
15912
15913 static tree
15914 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15915 bool explain_p)
15916 {
15917 tree copy_of_targs;
15918
15919 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15920 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15921 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15922 return NULL_TREE;
15923
15924 /* We need to make a new template argument vector for the call to
15925 unify. If we used TARGS, we'd clutter it up with the result of
15926 the attempted unification, even if this class didn't work out.
15927 We also don't want to commit ourselves to all the unifications
15928 we've already done, since unification is supposed to be done on
15929 an argument-by-argument basis. In other words, consider the
15930 following pathological case:
15931
15932 template <int I, int J, int K>
15933 struct S {};
15934
15935 template <int I, int J>
15936 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15937
15938 template <int I, int J, int K>
15939 void f(S<I, J, K>, S<I, I, I>);
15940
15941 void g() {
15942 S<0, 0, 0> s0;
15943 S<0, 1, 2> s2;
15944
15945 f(s0, s2);
15946 }
15947
15948 Now, by the time we consider the unification involving `s2', we
15949 already know that we must have `f<0, 0, 0>'. But, even though
15950 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15951 because there are two ways to unify base classes of S<0, 1, 2>
15952 with S<I, I, I>. If we kept the already deduced knowledge, we
15953 would reject the possibility I=1. */
15954 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15955
15956 /* If unification failed, we're done. */
15957 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15958 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15959 return NULL_TREE;
15960
15961 return arg;
15962 }
15963
15964 /* Given a template type PARM and a class type ARG, find the unique
15965 base type in ARG that is an instance of PARM. We do not examine
15966 ARG itself; only its base-classes. If there is not exactly one
15967 appropriate base class, return NULL_TREE. PARM may be the type of
15968 a partial specialization, as well as a plain template type. Used
15969 by unify. */
15970
15971 static enum template_base_result
15972 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15973 bool explain_p, tree *result)
15974 {
15975 tree rval = NULL_TREE;
15976 tree binfo;
15977
15978 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15979
15980 binfo = TYPE_BINFO (complete_type (arg));
15981 if (!binfo)
15982 {
15983 /* The type could not be completed. */
15984 *result = NULL_TREE;
15985 return tbr_incomplete_type;
15986 }
15987
15988 /* Walk in inheritance graph order. The search order is not
15989 important, and this avoids multiple walks of virtual bases. */
15990 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15991 {
15992 tree r = try_class_unification (tparms, targs, parm,
15993 BINFO_TYPE (binfo), explain_p);
15994
15995 if (r)
15996 {
15997 /* If there is more than one satisfactory baseclass, then:
15998
15999 [temp.deduct.call]
16000
16001 If they yield more than one possible deduced A, the type
16002 deduction fails.
16003
16004 applies. */
16005 if (rval && !same_type_p (r, rval))
16006 {
16007 *result = NULL_TREE;
16008 return tbr_ambiguous_baseclass;
16009 }
16010
16011 rval = r;
16012 }
16013 }
16014
16015 *result = rval;
16016 return tbr_success;
16017 }
16018
16019 /* Returns the level of DECL, which declares a template parameter. */
16020
16021 static int
16022 template_decl_level (tree decl)
16023 {
16024 switch (TREE_CODE (decl))
16025 {
16026 case TYPE_DECL:
16027 case TEMPLATE_DECL:
16028 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16029
16030 case PARM_DECL:
16031 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16032
16033 default:
16034 gcc_unreachable ();
16035 }
16036 return 0;
16037 }
16038
16039 /* Decide whether ARG can be unified with PARM, considering only the
16040 cv-qualifiers of each type, given STRICT as documented for unify.
16041 Returns nonzero iff the unification is OK on that basis. */
16042
16043 static int
16044 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16045 {
16046 int arg_quals = cp_type_quals (arg);
16047 int parm_quals = cp_type_quals (parm);
16048
16049 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16050 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16051 {
16052 /* Although a CVR qualifier is ignored when being applied to a
16053 substituted template parameter ([8.3.2]/1 for example), that
16054 does not allow us to unify "const T" with "int&" because both
16055 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16056 It is ok when we're allowing additional CV qualifiers
16057 at the outer level [14.8.2.1]/3,1st bullet. */
16058 if ((TREE_CODE (arg) == REFERENCE_TYPE
16059 || TREE_CODE (arg) == FUNCTION_TYPE
16060 || TREE_CODE (arg) == METHOD_TYPE)
16061 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16062 return 0;
16063
16064 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16065 && (parm_quals & TYPE_QUAL_RESTRICT))
16066 return 0;
16067 }
16068
16069 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16070 && (arg_quals & parm_quals) != parm_quals)
16071 return 0;
16072
16073 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16074 && (parm_quals & arg_quals) != arg_quals)
16075 return 0;
16076
16077 return 1;
16078 }
16079
16080 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16081 void
16082 template_parm_level_and_index (tree parm, int* level, int* index)
16083 {
16084 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16085 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16086 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16087 {
16088 *index = TEMPLATE_TYPE_IDX (parm);
16089 *level = TEMPLATE_TYPE_LEVEL (parm);
16090 }
16091 else
16092 {
16093 *index = TEMPLATE_PARM_IDX (parm);
16094 *level = TEMPLATE_PARM_LEVEL (parm);
16095 }
16096 }
16097
16098 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16099 do { \
16100 if (unify (TP, TA, P, A, S, EP)) \
16101 return 1; \
16102 } while (0);
16103
16104 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16105 expansion at the end of PACKED_PARMS. Returns 0 if the type
16106 deduction succeeds, 1 otherwise. STRICT is the same as in
16107 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16108 call argument list. We'll need to adjust the arguments to make them
16109 types. SUBR tells us if this is from a recursive call to
16110 type_unification_real, or for comparing two template argument
16111 lists. */
16112
16113 static int
16114 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16115 tree packed_args, unification_kind_t strict,
16116 bool subr, bool explain_p)
16117 {
16118 tree parm
16119 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16120 tree pattern = PACK_EXPANSION_PATTERN (parm);
16121 tree pack, packs = NULL_TREE;
16122 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16123 int len = TREE_VEC_LENGTH (packed_args);
16124
16125 /* Determine the parameter packs we will be deducing from the
16126 pattern, and record their current deductions. */
16127 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16128 pack; pack = TREE_CHAIN (pack))
16129 {
16130 tree parm_pack = TREE_VALUE (pack);
16131 int idx, level;
16132
16133 /* Determine the index and level of this parameter pack. */
16134 template_parm_level_and_index (parm_pack, &level, &idx);
16135
16136 /* Keep track of the parameter packs and their corresponding
16137 argument packs. */
16138 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16139 TREE_TYPE (packs) = make_tree_vec (len - start);
16140 }
16141
16142 /* Loop through all of the arguments that have not yet been
16143 unified and unify each with the pattern. */
16144 for (i = start; i < len; i++)
16145 {
16146 tree parm;
16147 bool any_explicit = false;
16148 tree arg = TREE_VEC_ELT (packed_args, i);
16149
16150 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16151 or the element of its argument pack at the current index if
16152 this argument was explicitly specified. */
16153 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16154 {
16155 int idx, level;
16156 tree arg, pargs;
16157 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16158
16159 arg = NULL_TREE;
16160 if (TREE_VALUE (pack)
16161 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16162 && (i < TREE_VEC_LENGTH (pargs)))
16163 {
16164 any_explicit = true;
16165 arg = TREE_VEC_ELT (pargs, i);
16166 }
16167 TMPL_ARG (targs, level, idx) = arg;
16168 }
16169
16170 /* If we had explicit template arguments, substitute them into the
16171 pattern before deduction. */
16172 if (any_explicit)
16173 {
16174 /* Some arguments might still be unspecified or dependent. */
16175 bool dependent;
16176 ++processing_template_decl;
16177 dependent = any_dependent_template_arguments_p (targs);
16178 if (!dependent)
16179 --processing_template_decl;
16180 parm = tsubst (pattern, targs,
16181 explain_p ? tf_warning_or_error : tf_none,
16182 NULL_TREE);
16183 if (dependent)
16184 --processing_template_decl;
16185 if (parm == error_mark_node)
16186 return 1;
16187 }
16188 else
16189 parm = pattern;
16190
16191 /* Unify the pattern with the current argument. */
16192 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16193 LOOKUP_IMPLICIT, explain_p))
16194 return 1;
16195
16196 /* For each parameter pack, collect the deduced value. */
16197 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16198 {
16199 int idx, level;
16200 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16201
16202 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16203 TMPL_ARG (targs, level, idx);
16204 }
16205 }
16206
16207 /* Verify that the results of unification with the parameter packs
16208 produce results consistent with what we've seen before, and make
16209 the deduced argument packs available. */
16210 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16211 {
16212 tree old_pack = TREE_VALUE (pack);
16213 tree new_args = TREE_TYPE (pack);
16214 int i, len = TREE_VEC_LENGTH (new_args);
16215 int idx, level;
16216 bool nondeduced_p = false;
16217
16218 /* By default keep the original deduced argument pack.
16219 If necessary, more specific code is going to update the
16220 resulting deduced argument later down in this function. */
16221 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16222 TMPL_ARG (targs, level, idx) = old_pack;
16223
16224 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16225 actually deduce anything. */
16226 for (i = 0; i < len && !nondeduced_p; ++i)
16227 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16228 nondeduced_p = true;
16229 if (nondeduced_p)
16230 continue;
16231
16232 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16233 {
16234 /* If we had fewer function args than explicit template args,
16235 just use the explicits. */
16236 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16237 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16238 if (len < explicit_len)
16239 new_args = explicit_args;
16240 }
16241
16242 if (!old_pack)
16243 {
16244 tree result;
16245 /* Build the deduced *_ARGUMENT_PACK. */
16246 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16247 {
16248 result = make_node (NONTYPE_ARGUMENT_PACK);
16249 TREE_TYPE (result) =
16250 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16251 TREE_CONSTANT (result) = 1;
16252 }
16253 else
16254 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16255
16256 SET_ARGUMENT_PACK_ARGS (result, new_args);
16257
16258 /* Note the deduced argument packs for this parameter
16259 pack. */
16260 TMPL_ARG (targs, level, idx) = result;
16261 }
16262 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16263 && (ARGUMENT_PACK_ARGS (old_pack)
16264 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16265 {
16266 /* We only had the explicitly-provided arguments before, but
16267 now we have a complete set of arguments. */
16268 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16269
16270 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16271 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16272 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16273 }
16274 else
16275 {
16276 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16277 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16278
16279 if (!comp_template_args_with_info (old_args, new_args,
16280 &bad_old_arg, &bad_new_arg))
16281 /* Inconsistent unification of this parameter pack. */
16282 return unify_parameter_pack_inconsistent (explain_p,
16283 bad_old_arg,
16284 bad_new_arg);
16285 }
16286 }
16287
16288 return unify_success (explain_p);
16289 }
16290
16291 /* Deduce the value of template parameters. TPARMS is the (innermost)
16292 set of template parameters to a template. TARGS is the bindings
16293 for those template parameters, as determined thus far; TARGS may
16294 include template arguments for outer levels of template parameters
16295 as well. PARM is a parameter to a template function, or a
16296 subcomponent of that parameter; ARG is the corresponding argument.
16297 This function attempts to match PARM with ARG in a manner
16298 consistent with the existing assignments in TARGS. If more values
16299 are deduced, then TARGS is updated.
16300
16301 Returns 0 if the type deduction succeeds, 1 otherwise. The
16302 parameter STRICT is a bitwise or of the following flags:
16303
16304 UNIFY_ALLOW_NONE:
16305 Require an exact match between PARM and ARG.
16306 UNIFY_ALLOW_MORE_CV_QUAL:
16307 Allow the deduced ARG to be more cv-qualified (by qualification
16308 conversion) than ARG.
16309 UNIFY_ALLOW_LESS_CV_QUAL:
16310 Allow the deduced ARG to be less cv-qualified than ARG.
16311 UNIFY_ALLOW_DERIVED:
16312 Allow the deduced ARG to be a template base class of ARG,
16313 or a pointer to a template base class of the type pointed to by
16314 ARG.
16315 UNIFY_ALLOW_INTEGER:
16316 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16317 case for more information.
16318 UNIFY_ALLOW_OUTER_LEVEL:
16319 This is the outermost level of a deduction. Used to determine validity
16320 of qualification conversions. A valid qualification conversion must
16321 have const qualified pointers leading up to the inner type which
16322 requires additional CV quals, except at the outer level, where const
16323 is not required [conv.qual]. It would be normal to set this flag in
16324 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16325 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16326 This is the outermost level of a deduction, and PARM can be more CV
16327 qualified at this point.
16328 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16329 This is the outermost level of a deduction, and PARM can be less CV
16330 qualified at this point. */
16331
16332 static int
16333 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
16334 bool explain_p)
16335 {
16336 int idx;
16337 tree targ;
16338 tree tparm;
16339 int strict_in = strict;
16340
16341 /* I don't think this will do the right thing with respect to types.
16342 But the only case I've seen it in so far has been array bounds, where
16343 signedness is the only information lost, and I think that will be
16344 okay. */
16345 while (TREE_CODE (parm) == NOP_EXPR)
16346 parm = TREE_OPERAND (parm, 0);
16347
16348 if (arg == error_mark_node)
16349 return unify_invalid (explain_p);
16350 if (arg == unknown_type_node
16351 || arg == init_list_type_node)
16352 /* We can't deduce anything from this, but we might get all the
16353 template args from other function args. */
16354 return unify_success (explain_p);
16355
16356 /* If PARM uses template parameters, then we can't bail out here,
16357 even if ARG == PARM, since we won't record unifications for the
16358 template parameters. We might need them if we're trying to
16359 figure out which of two things is more specialized. */
16360 if (arg == parm && !uses_template_parms (parm))
16361 return unify_success (explain_p);
16362
16363 /* Handle init lists early, so the rest of the function can assume
16364 we're dealing with a type. */
16365 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
16366 {
16367 tree elt, elttype;
16368 unsigned i;
16369 tree orig_parm = parm;
16370
16371 /* Replace T with std::initializer_list<T> for deduction. */
16372 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16373 && flag_deduce_init_list)
16374 parm = listify (parm);
16375
16376 if (!is_std_init_list (parm))
16377 /* We can only deduce from an initializer list argument if the
16378 parameter is std::initializer_list; otherwise this is a
16379 non-deduced context. */
16380 return unify_success (explain_p);
16381
16382 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
16383
16384 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
16385 {
16386 int elt_strict = strict;
16387
16388 if (elt == error_mark_node)
16389 return unify_invalid (explain_p);
16390
16391 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
16392 {
16393 tree type = TREE_TYPE (elt);
16394 /* It should only be possible to get here for a call. */
16395 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
16396 elt_strict |= maybe_adjust_types_for_deduction
16397 (DEDUCE_CALL, &elttype, &type, elt);
16398 elt = type;
16399 }
16400
16401 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
16402 explain_p);
16403 }
16404
16405 /* If the std::initializer_list<T> deduction worked, replace the
16406 deduced A with std::initializer_list<A>. */
16407 if (orig_parm != parm)
16408 {
16409 idx = TEMPLATE_TYPE_IDX (orig_parm);
16410 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16411 targ = listify (targ);
16412 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
16413 }
16414 return unify_success (explain_p);
16415 }
16416
16417 /* Immediately reject some pairs that won't unify because of
16418 cv-qualification mismatches. */
16419 if (TREE_CODE (arg) == TREE_CODE (parm)
16420 && TYPE_P (arg)
16421 /* It is the elements of the array which hold the cv quals of an array
16422 type, and the elements might be template type parms. We'll check
16423 when we recurse. */
16424 && TREE_CODE (arg) != ARRAY_TYPE
16425 /* We check the cv-qualifiers when unifying with template type
16426 parameters below. We want to allow ARG `const T' to unify with
16427 PARM `T' for example, when computing which of two templates
16428 is more specialized, for example. */
16429 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16430 && !check_cv_quals_for_unify (strict_in, arg, parm))
16431 return unify_cv_qual_mismatch (explain_p, parm, arg);
16432
16433 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16434 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16435 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16436 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16437 strict &= ~UNIFY_ALLOW_DERIVED;
16438 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16439 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16440
16441 switch (TREE_CODE (parm))
16442 {
16443 case TYPENAME_TYPE:
16444 case SCOPE_REF:
16445 case UNBOUND_CLASS_TEMPLATE:
16446 /* In a type which contains a nested-name-specifier, template
16447 argument values cannot be deduced for template parameters used
16448 within the nested-name-specifier. */
16449 return unify_success (explain_p);
16450
16451 case TEMPLATE_TYPE_PARM:
16452 case TEMPLATE_TEMPLATE_PARM:
16453 case BOUND_TEMPLATE_TEMPLATE_PARM:
16454 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16455 if (tparm == error_mark_node)
16456 return unify_invalid (explain_p);
16457
16458 if (TEMPLATE_TYPE_LEVEL (parm)
16459 != template_decl_level (tparm))
16460 /* The PARM is not one we're trying to unify. Just check
16461 to see if it matches ARG. */
16462 {
16463 if (TREE_CODE (arg) == TREE_CODE (parm)
16464 && (is_auto (parm) ? is_auto (arg)
16465 : same_type_p (parm, arg)))
16466 return unify_success (explain_p);
16467 else
16468 return unify_type_mismatch (explain_p, parm, arg);
16469 }
16470 idx = TEMPLATE_TYPE_IDX (parm);
16471 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16472 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16473 if (tparm == error_mark_node)
16474 return unify_invalid (explain_p);
16475
16476 /* Check for mixed types and values. */
16477 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16478 && TREE_CODE (tparm) != TYPE_DECL)
16479 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16480 && TREE_CODE (tparm) != TEMPLATE_DECL))
16481 gcc_unreachable ();
16482
16483 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16484 {
16485 /* ARG must be constructed from a template class or a template
16486 template parameter. */
16487 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16488 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16489 return unify_template_deduction_failure (explain_p, parm, arg);
16490
16491 {
16492 tree parmvec = TYPE_TI_ARGS (parm);
16493 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16494 tree full_argvec = add_to_template_args (targs, argvec);
16495 tree parm_parms
16496 = DECL_INNERMOST_TEMPLATE_PARMS
16497 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16498 int i, len;
16499 int parm_variadic_p = 0;
16500
16501 /* The resolution to DR150 makes clear that default
16502 arguments for an N-argument may not be used to bind T
16503 to a template template parameter with fewer than N
16504 parameters. It is not safe to permit the binding of
16505 default arguments as an extension, as that may change
16506 the meaning of a conforming program. Consider:
16507
16508 struct Dense { static const unsigned int dim = 1; };
16509
16510 template <template <typename> class View,
16511 typename Block>
16512 void operator+(float, View<Block> const&);
16513
16514 template <typename Block,
16515 unsigned int Dim = Block::dim>
16516 struct Lvalue_proxy { operator float() const; };
16517
16518 void
16519 test_1d (void) {
16520 Lvalue_proxy<Dense> p;
16521 float b;
16522 b + p;
16523 }
16524
16525 Here, if Lvalue_proxy is permitted to bind to View, then
16526 the global operator+ will be used; if they are not, the
16527 Lvalue_proxy will be converted to float. */
16528 if (coerce_template_parms (parm_parms,
16529 full_argvec,
16530 TYPE_TI_TEMPLATE (parm),
16531 (explain_p
16532 ? tf_warning_or_error
16533 : tf_none),
16534 /*require_all_args=*/true,
16535 /*use_default_args=*/false)
16536 == error_mark_node)
16537 return 1;
16538
16539 /* Deduce arguments T, i from TT<T> or TT<i>.
16540 We check each element of PARMVEC and ARGVEC individually
16541 rather than the whole TREE_VEC since they can have
16542 different number of elements. */
16543
16544 parmvec = expand_template_argument_pack (parmvec);
16545 argvec = expand_template_argument_pack (argvec);
16546
16547 len = TREE_VEC_LENGTH (parmvec);
16548
16549 /* Check if the parameters end in a pack, making them
16550 variadic. */
16551 if (len > 0
16552 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16553 parm_variadic_p = 1;
16554
16555 for (i = 0; i < len - parm_variadic_p; ++i)
16556 /* If the template argument list of P contains a pack
16557 expansion that is not the last template argument, the
16558 entire template argument list is a non-deduced
16559 context. */
16560 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
16561 return unify_success (explain_p);
16562
16563 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16564 return unify_too_few_arguments (explain_p,
16565 TREE_VEC_LENGTH (argvec), len);
16566
16567 for (i = 0; i < len - parm_variadic_p; ++i)
16568 {
16569 RECUR_AND_CHECK_FAILURE (tparms, targs,
16570 TREE_VEC_ELT (parmvec, i),
16571 TREE_VEC_ELT (argvec, i),
16572 UNIFY_ALLOW_NONE, explain_p);
16573 }
16574
16575 if (parm_variadic_p
16576 && unify_pack_expansion (tparms, targs,
16577 parmvec, argvec,
16578 DEDUCE_EXACT,
16579 /*subr=*/true, explain_p))
16580 return 1;
16581 }
16582 arg = TYPE_TI_TEMPLATE (arg);
16583
16584 /* Fall through to deduce template name. */
16585 }
16586
16587 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16588 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16589 {
16590 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16591
16592 /* Simple cases: Value already set, does match or doesn't. */
16593 if (targ != NULL_TREE && template_args_equal (targ, arg))
16594 return unify_success (explain_p);
16595 else if (targ)
16596 return unify_inconsistency (explain_p, parm, targ, arg);
16597 }
16598 else
16599 {
16600 /* If PARM is `const T' and ARG is only `int', we don't have
16601 a match unless we are allowing additional qualification.
16602 If ARG is `const int' and PARM is just `T' that's OK;
16603 that binds `const int' to `T'. */
16604 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16605 arg, parm))
16606 return unify_cv_qual_mismatch (explain_p, parm, arg);
16607
16608 /* Consider the case where ARG is `const volatile int' and
16609 PARM is `const T'. Then, T should be `volatile int'. */
16610 arg = cp_build_qualified_type_real
16611 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16612 if (arg == error_mark_node)
16613 return unify_invalid (explain_p);
16614
16615 /* Simple cases: Value already set, does match or doesn't. */
16616 if (targ != NULL_TREE && same_type_p (targ, arg))
16617 return unify_success (explain_p);
16618 else if (targ)
16619 return unify_inconsistency (explain_p, parm, targ, arg);
16620
16621 /* Make sure that ARG is not a variable-sized array. (Note
16622 that were talking about variable-sized arrays (like
16623 `int[n]'), rather than arrays of unknown size (like
16624 `int[]').) We'll get very confused by such a type since
16625 the bound of the array is not constant, and therefore
16626 not mangleable. Besides, such types are not allowed in
16627 ISO C++, so we can do as we please here. We do allow
16628 them for 'auto' deduction, since that isn't ABI-exposed. */
16629 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16630 return unify_vla_arg (explain_p, arg);
16631
16632 /* Strip typedefs as in convert_template_argument. */
16633 arg = canonicalize_type_argument (arg, tf_none);
16634 }
16635
16636 /* If ARG is a parameter pack or an expansion, we cannot unify
16637 against it unless PARM is also a parameter pack. */
16638 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16639 && !template_parameter_pack_p (parm))
16640 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16641
16642 /* If the argument deduction results is a METHOD_TYPE,
16643 then there is a problem.
16644 METHOD_TYPE doesn't map to any real C++ type the result of
16645 the deduction can not be of that type. */
16646 if (TREE_CODE (arg) == METHOD_TYPE)
16647 return unify_method_type_error (explain_p, arg);
16648
16649 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16650 return unify_success (explain_p);
16651
16652 case TEMPLATE_PARM_INDEX:
16653 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16654 if (tparm == error_mark_node)
16655 return unify_invalid (explain_p);
16656
16657 if (TEMPLATE_PARM_LEVEL (parm)
16658 != template_decl_level (tparm))
16659 {
16660 /* The PARM is not one we're trying to unify. Just check
16661 to see if it matches ARG. */
16662 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16663 && cp_tree_equal (parm, arg));
16664 if (result)
16665 unify_expression_unequal (explain_p, parm, arg);
16666 return result;
16667 }
16668
16669 idx = TEMPLATE_PARM_IDX (parm);
16670 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16671
16672 if (targ)
16673 {
16674 int x = !cp_tree_equal (targ, arg);
16675 if (x)
16676 unify_inconsistency (explain_p, parm, targ, arg);
16677 return x;
16678 }
16679
16680 /* [temp.deduct.type] If, in the declaration of a function template
16681 with a non-type template-parameter, the non-type
16682 template-parameter is used in an expression in the function
16683 parameter-list and, if the corresponding template-argument is
16684 deduced, the template-argument type shall match the type of the
16685 template-parameter exactly, except that a template-argument
16686 deduced from an array bound may be of any integral type.
16687 The non-type parameter might use already deduced type parameters. */
16688 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16689 if (!TREE_TYPE (arg))
16690 /* Template-parameter dependent expression. Just accept it for now.
16691 It will later be processed in convert_template_argument. */
16692 ;
16693 else if (same_type_p (TREE_TYPE (arg), tparm))
16694 /* OK */;
16695 else if ((strict & UNIFY_ALLOW_INTEGER)
16696 && (TREE_CODE (tparm) == INTEGER_TYPE
16697 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16698 /* Convert the ARG to the type of PARM; the deduced non-type
16699 template argument must exactly match the types of the
16700 corresponding parameter. */
16701 arg = fold (build_nop (tparm, arg));
16702 else if (uses_template_parms (tparm))
16703 /* We haven't deduced the type of this parameter yet. Try again
16704 later. */
16705 return unify_success (explain_p);
16706 else
16707 return unify_type_mismatch (explain_p, tparm, arg);
16708
16709 /* If ARG is a parameter pack or an expansion, we cannot unify
16710 against it unless PARM is also a parameter pack. */
16711 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16712 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16713 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16714
16715 arg = strip_typedefs_expr (arg);
16716 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16717 return unify_success (explain_p);
16718
16719 case PTRMEM_CST:
16720 {
16721 /* A pointer-to-member constant can be unified only with
16722 another constant. */
16723 if (TREE_CODE (arg) != PTRMEM_CST)
16724 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16725
16726 /* Just unify the class member. It would be useless (and possibly
16727 wrong, depending on the strict flags) to unify also
16728 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16729 arg refer to the same variable, even if through different
16730 classes. For instance:
16731
16732 struct A { int x; };
16733 struct B : A { };
16734
16735 Unification of &A::x and &B::x must succeed. */
16736 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16737 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16738 }
16739
16740 case POINTER_TYPE:
16741 {
16742 if (TREE_CODE (arg) != POINTER_TYPE)
16743 return unify_type_mismatch (explain_p, parm, arg);
16744
16745 /* [temp.deduct.call]
16746
16747 A can be another pointer or pointer to member type that can
16748 be converted to the deduced A via a qualification
16749 conversion (_conv.qual_).
16750
16751 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16752 This will allow for additional cv-qualification of the
16753 pointed-to types if appropriate. */
16754
16755 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16756 /* The derived-to-base conversion only persists through one
16757 level of pointers. */
16758 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16759
16760 return unify (tparms, targs, TREE_TYPE (parm),
16761 TREE_TYPE (arg), strict, explain_p);
16762 }
16763
16764 case REFERENCE_TYPE:
16765 if (TREE_CODE (arg) != REFERENCE_TYPE)
16766 return unify_type_mismatch (explain_p, parm, arg);
16767 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16768 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16769
16770 case ARRAY_TYPE:
16771 if (TREE_CODE (arg) != ARRAY_TYPE)
16772 return unify_type_mismatch (explain_p, parm, arg);
16773 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16774 != (TYPE_DOMAIN (arg) == NULL_TREE))
16775 return unify_type_mismatch (explain_p, parm, arg);
16776 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16777 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16778 if (TYPE_DOMAIN (parm) != NULL_TREE)
16779 {
16780 tree parm_max;
16781 tree arg_max;
16782 bool parm_cst;
16783 bool arg_cst;
16784
16785 /* Our representation of array types uses "N - 1" as the
16786 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16787 not an integer constant. We cannot unify arbitrarily
16788 complex expressions, so we eliminate the MINUS_EXPRs
16789 here. */
16790 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16791 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16792 if (!parm_cst)
16793 {
16794 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16795 parm_max = TREE_OPERAND (parm_max, 0);
16796 }
16797 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16798 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16799 if (!arg_cst)
16800 {
16801 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16802 trying to unify the type of a variable with the type
16803 of a template parameter. For example:
16804
16805 template <unsigned int N>
16806 void f (char (&) [N]);
16807 int g();
16808 void h(int i) {
16809 char a[g(i)];
16810 f(a);
16811 }
16812
16813 Here, the type of the ARG will be "int [g(i)]", and
16814 may be a SAVE_EXPR, etc. */
16815 if (TREE_CODE (arg_max) != MINUS_EXPR)
16816 return unify_vla_arg (explain_p, arg);
16817 arg_max = TREE_OPERAND (arg_max, 0);
16818 }
16819
16820 /* If only one of the bounds used a MINUS_EXPR, compensate
16821 by adding one to the other bound. */
16822 if (parm_cst && !arg_cst)
16823 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16824 integer_type_node,
16825 parm_max,
16826 integer_one_node);
16827 else if (arg_cst && !parm_cst)
16828 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16829 integer_type_node,
16830 arg_max,
16831 integer_one_node);
16832
16833 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16834 UNIFY_ALLOW_INTEGER, explain_p);
16835 }
16836 return unify_success (explain_p);
16837
16838 case REAL_TYPE:
16839 case COMPLEX_TYPE:
16840 case VECTOR_TYPE:
16841 case INTEGER_TYPE:
16842 case BOOLEAN_TYPE:
16843 case ENUMERAL_TYPE:
16844 case VOID_TYPE:
16845 case NULLPTR_TYPE:
16846 if (TREE_CODE (arg) != TREE_CODE (parm))
16847 return unify_type_mismatch (explain_p, parm, arg);
16848
16849 /* We have already checked cv-qualification at the top of the
16850 function. */
16851 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16852 return unify_type_mismatch (explain_p, parm, arg);
16853
16854 /* As far as unification is concerned, this wins. Later checks
16855 will invalidate it if necessary. */
16856 return unify_success (explain_p);
16857
16858 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16859 /* Type INTEGER_CST can come from ordinary constant template args. */
16860 case INTEGER_CST:
16861 while (TREE_CODE (arg) == NOP_EXPR)
16862 arg = TREE_OPERAND (arg, 0);
16863
16864 if (TREE_CODE (arg) != INTEGER_CST)
16865 return unify_template_argument_mismatch (explain_p, parm, arg);
16866 return (tree_int_cst_equal (parm, arg)
16867 ? unify_success (explain_p)
16868 : unify_template_argument_mismatch (explain_p, parm, arg));
16869
16870 case TREE_VEC:
16871 {
16872 int i, len, argslen;
16873 int parm_variadic_p = 0;
16874
16875 if (TREE_CODE (arg) != TREE_VEC)
16876 return unify_template_argument_mismatch (explain_p, parm, arg);
16877
16878 len = TREE_VEC_LENGTH (parm);
16879 argslen = TREE_VEC_LENGTH (arg);
16880
16881 /* Check for pack expansions in the parameters. */
16882 for (i = 0; i < len; ++i)
16883 {
16884 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16885 {
16886 if (i == len - 1)
16887 /* We can unify against something with a trailing
16888 parameter pack. */
16889 parm_variadic_p = 1;
16890 else
16891 /* [temp.deduct.type]/9: If the template argument list of
16892 P contains a pack expansion that is not the last
16893 template argument, the entire template argument list
16894 is a non-deduced context. */
16895 return unify_success (explain_p);
16896 }
16897 }
16898
16899 /* If we don't have enough arguments to satisfy the parameters
16900 (not counting the pack expression at the end), or we have
16901 too many arguments for a parameter list that doesn't end in
16902 a pack expression, we can't unify. */
16903 if (parm_variadic_p
16904 ? argslen < len - parm_variadic_p
16905 : argslen != len)
16906 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16907
16908 /* Unify all of the parameters that precede the (optional)
16909 pack expression. */
16910 for (i = 0; i < len - parm_variadic_p; ++i)
16911 {
16912 RECUR_AND_CHECK_FAILURE (tparms, targs,
16913 TREE_VEC_ELT (parm, i),
16914 TREE_VEC_ELT (arg, i),
16915 UNIFY_ALLOW_NONE, explain_p);
16916 }
16917 if (parm_variadic_p)
16918 return unify_pack_expansion (tparms, targs, parm, arg,
16919 DEDUCE_EXACT,
16920 /*subr=*/true, explain_p);
16921 return unify_success (explain_p);
16922 }
16923
16924 case RECORD_TYPE:
16925 case UNION_TYPE:
16926 if (TREE_CODE (arg) != TREE_CODE (parm))
16927 return unify_type_mismatch (explain_p, parm, arg);
16928
16929 if (TYPE_PTRMEMFUNC_P (parm))
16930 {
16931 if (!TYPE_PTRMEMFUNC_P (arg))
16932 return unify_type_mismatch (explain_p, parm, arg);
16933
16934 return unify (tparms, targs,
16935 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16936 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16937 strict, explain_p);
16938 }
16939
16940 if (CLASSTYPE_TEMPLATE_INFO (parm))
16941 {
16942 tree t = NULL_TREE;
16943
16944 if (strict_in & UNIFY_ALLOW_DERIVED)
16945 {
16946 /* First, we try to unify the PARM and ARG directly. */
16947 t = try_class_unification (tparms, targs,
16948 parm, arg, explain_p);
16949
16950 if (!t)
16951 {
16952 /* Fallback to the special case allowed in
16953 [temp.deduct.call]:
16954
16955 If P is a class, and P has the form
16956 template-id, then A can be a derived class of
16957 the deduced A. Likewise, if P is a pointer to
16958 a class of the form template-id, A can be a
16959 pointer to a derived class pointed to by the
16960 deduced A. */
16961 enum template_base_result r;
16962 r = get_template_base (tparms, targs, parm, arg,
16963 explain_p, &t);
16964
16965 if (!t)
16966 return unify_no_common_base (explain_p, r, parm, arg);
16967 }
16968 }
16969 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16970 && (CLASSTYPE_TI_TEMPLATE (parm)
16971 == CLASSTYPE_TI_TEMPLATE (arg)))
16972 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16973 Then, we should unify `int' and `U'. */
16974 t = arg;
16975 else
16976 /* There's no chance of unification succeeding. */
16977 return unify_type_mismatch (explain_p, parm, arg);
16978
16979 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16980 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16981 }
16982 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16983 return unify_type_mismatch (explain_p, parm, arg);
16984 return unify_success (explain_p);
16985
16986 case METHOD_TYPE:
16987 case FUNCTION_TYPE:
16988 {
16989 unsigned int nargs;
16990 tree *args;
16991 tree a;
16992 unsigned int i;
16993
16994 if (TREE_CODE (arg) != TREE_CODE (parm))
16995 return unify_type_mismatch (explain_p, parm, arg);
16996
16997 /* CV qualifications for methods can never be deduced, they must
16998 match exactly. We need to check them explicitly here,
16999 because type_unification_real treats them as any other
17000 cv-qualified parameter. */
17001 if (TREE_CODE (parm) == METHOD_TYPE
17002 && (!check_cv_quals_for_unify
17003 (UNIFY_ALLOW_NONE,
17004 class_of_this_parm (arg),
17005 class_of_this_parm (parm))))
17006 return unify_cv_qual_mismatch (explain_p, parm, arg);
17007
17008 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17009 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17010
17011 nargs = list_length (TYPE_ARG_TYPES (arg));
17012 args = XALLOCAVEC (tree, nargs);
17013 for (a = TYPE_ARG_TYPES (arg), i = 0;
17014 a != NULL_TREE && a != void_list_node;
17015 a = TREE_CHAIN (a), ++i)
17016 args[i] = TREE_VALUE (a);
17017 nargs = i;
17018
17019 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17020 args, nargs, 1, DEDUCE_EXACT,
17021 LOOKUP_NORMAL, explain_p);
17022 }
17023
17024 case OFFSET_TYPE:
17025 /* Unify a pointer to member with a pointer to member function, which
17026 deduces the type of the member as a function type. */
17027 if (TYPE_PTRMEMFUNC_P (arg))
17028 {
17029 tree method_type;
17030 tree fntype;
17031
17032 /* Check top-level cv qualifiers */
17033 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17034 return unify_cv_qual_mismatch (explain_p, parm, arg);
17035
17036 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17037 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17038 UNIFY_ALLOW_NONE, explain_p);
17039
17040 /* Determine the type of the function we are unifying against. */
17041 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
17042 fntype =
17043 build_function_type (TREE_TYPE (method_type),
17044 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
17045
17046 /* Extract the cv-qualifiers of the member function from the
17047 implicit object parameter and place them on the function
17048 type to be restored later. */
17049 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
17050 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17051 }
17052
17053 if (TREE_CODE (arg) != OFFSET_TYPE)
17054 return unify_type_mismatch (explain_p, parm, arg);
17055 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17056 TYPE_OFFSET_BASETYPE (arg),
17057 UNIFY_ALLOW_NONE, explain_p);
17058 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17059 strict, explain_p);
17060
17061 case CONST_DECL:
17062 if (DECL_TEMPLATE_PARM_P (parm))
17063 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17064 if (arg != integral_constant_value (parm))
17065 return unify_template_argument_mismatch (explain_p, parm, arg);
17066 return unify_success (explain_p);
17067
17068 case FIELD_DECL:
17069 case TEMPLATE_DECL:
17070 /* Matched cases are handled by the ARG == PARM test above. */
17071 return unify_template_argument_mismatch (explain_p, parm, arg);
17072
17073 case VAR_DECL:
17074 /* A non-type template parameter that is a variable should be a
17075 an integral constant, in which case, it whould have been
17076 folded into its (constant) value. So we should not be getting
17077 a variable here. */
17078 gcc_unreachable ();
17079
17080 case TYPE_ARGUMENT_PACK:
17081 case NONTYPE_ARGUMENT_PACK:
17082 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17083 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17084
17085 case TYPEOF_TYPE:
17086 case DECLTYPE_TYPE:
17087 case UNDERLYING_TYPE:
17088 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17089 or UNDERLYING_TYPE nodes. */
17090 return unify_success (explain_p);
17091
17092 case ERROR_MARK:
17093 /* Unification fails if we hit an error node. */
17094 return unify_invalid (explain_p);
17095
17096 default:
17097 /* An unresolved overload is a nondeduced context. */
17098 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17099 return unify_success (explain_p);
17100 gcc_assert (EXPR_P (parm));
17101
17102 /* We must be looking at an expression. This can happen with
17103 something like:
17104
17105 template <int I>
17106 void foo(S<I>, S<I + 2>);
17107
17108 This is a "nondeduced context":
17109
17110 [deduct.type]
17111
17112 The nondeduced contexts are:
17113
17114 --A type that is a template-id in which one or more of
17115 the template-arguments is an expression that references
17116 a template-parameter.
17117
17118 In these cases, we assume deduction succeeded, but don't
17119 actually infer any unifications. */
17120
17121 if (!uses_template_parms (parm)
17122 && !template_args_equal (parm, arg))
17123 return unify_expression_unequal (explain_p, parm, arg);
17124 else
17125 return unify_success (explain_p);
17126 }
17127 }
17128 #undef RECUR_AND_CHECK_FAILURE
17129 \f
17130 /* Note that DECL can be defined in this translation unit, if
17131 required. */
17132
17133 static void
17134 mark_definable (tree decl)
17135 {
17136 tree clone;
17137 DECL_NOT_REALLY_EXTERN (decl) = 1;
17138 FOR_EACH_CLONE (clone, decl)
17139 DECL_NOT_REALLY_EXTERN (clone) = 1;
17140 }
17141
17142 /* Called if RESULT is explicitly instantiated, or is a member of an
17143 explicitly instantiated class. */
17144
17145 void
17146 mark_decl_instantiated (tree result, int extern_p)
17147 {
17148 SET_DECL_EXPLICIT_INSTANTIATION (result);
17149
17150 /* If this entity has already been written out, it's too late to
17151 make any modifications. */
17152 if (TREE_ASM_WRITTEN (result))
17153 return;
17154
17155 if (TREE_CODE (result) != FUNCTION_DECL)
17156 /* The TREE_PUBLIC flag for function declarations will have been
17157 set correctly by tsubst. */
17158 TREE_PUBLIC (result) = 1;
17159
17160 /* This might have been set by an earlier implicit instantiation. */
17161 DECL_COMDAT (result) = 0;
17162
17163 if (extern_p)
17164 DECL_NOT_REALLY_EXTERN (result) = 0;
17165 else
17166 {
17167 mark_definable (result);
17168 /* Always make artificials weak. */
17169 if (DECL_ARTIFICIAL (result) && flag_weak)
17170 comdat_linkage (result);
17171 /* For WIN32 we also want to put explicit instantiations in
17172 linkonce sections. */
17173 else if (TREE_PUBLIC (result))
17174 maybe_make_one_only (result);
17175 }
17176
17177 /* If EXTERN_P, then this function will not be emitted -- unless
17178 followed by an explicit instantiation, at which point its linkage
17179 will be adjusted. If !EXTERN_P, then this function will be
17180 emitted here. In neither circumstance do we want
17181 import_export_decl to adjust the linkage. */
17182 DECL_INTERFACE_KNOWN (result) = 1;
17183 }
17184
17185 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17186 important template arguments. If any are missing, we check whether
17187 they're important by using error_mark_node for substituting into any
17188 args that were used for partial ordering (the ones between ARGS and END)
17189 and seeing if it bubbles up. */
17190
17191 static bool
17192 check_undeduced_parms (tree targs, tree args, tree end)
17193 {
17194 bool found = false;
17195 int i;
17196 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17197 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17198 {
17199 found = true;
17200 TREE_VEC_ELT (targs, i) = error_mark_node;
17201 }
17202 if (found)
17203 {
17204 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17205 if (substed == error_mark_node)
17206 return true;
17207 }
17208 return false;
17209 }
17210
17211 /* Given two function templates PAT1 and PAT2, return:
17212
17213 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17214 -1 if PAT2 is more specialized than PAT1.
17215 0 if neither is more specialized.
17216
17217 LEN indicates the number of parameters we should consider
17218 (defaulted parameters should not be considered).
17219
17220 The 1998 std underspecified function template partial ordering, and
17221 DR214 addresses the issue. We take pairs of arguments, one from
17222 each of the templates, and deduce them against each other. One of
17223 the templates will be more specialized if all the *other*
17224 template's arguments deduce against its arguments and at least one
17225 of its arguments *does* *not* deduce against the other template's
17226 corresponding argument. Deduction is done as for class templates.
17227 The arguments used in deduction have reference and top level cv
17228 qualifiers removed. Iff both arguments were originally reference
17229 types *and* deduction succeeds in both directions, the template
17230 with the more cv-qualified argument wins for that pairing (if
17231 neither is more cv-qualified, they both are equal). Unlike regular
17232 deduction, after all the arguments have been deduced in this way,
17233 we do *not* verify the deduced template argument values can be
17234 substituted into non-deduced contexts.
17235
17236 The logic can be a bit confusing here, because we look at deduce1 and
17237 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17238 can find template arguments for pat1 to make arg1 look like arg2, that
17239 means that arg2 is at least as specialized as arg1. */
17240
17241 int
17242 more_specialized_fn (tree pat1, tree pat2, int len)
17243 {
17244 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17245 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17246 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17247 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17248 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17249 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17250 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17251 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17252 tree origs1, origs2;
17253 bool lose1 = false;
17254 bool lose2 = false;
17255
17256 /* Remove the this parameter from non-static member functions. If
17257 one is a non-static member function and the other is not a static
17258 member function, remove the first parameter from that function
17259 also. This situation occurs for operator functions where we
17260 locate both a member function (with this pointer) and non-member
17261 operator (with explicit first operand). */
17262 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
17263 {
17264 len--; /* LEN is the number of significant arguments for DECL1 */
17265 args1 = TREE_CHAIN (args1);
17266 if (!DECL_STATIC_FUNCTION_P (decl2))
17267 args2 = TREE_CHAIN (args2);
17268 }
17269 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
17270 {
17271 args2 = TREE_CHAIN (args2);
17272 if (!DECL_STATIC_FUNCTION_P (decl1))
17273 {
17274 len--;
17275 args1 = TREE_CHAIN (args1);
17276 }
17277 }
17278
17279 /* If only one is a conversion operator, they are unordered. */
17280 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
17281 return 0;
17282
17283 /* Consider the return type for a conversion function */
17284 if (DECL_CONV_FN_P (decl1))
17285 {
17286 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
17287 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
17288 len++;
17289 }
17290
17291 processing_template_decl++;
17292
17293 origs1 = args1;
17294 origs2 = args2;
17295
17296 while (len--
17297 /* Stop when an ellipsis is seen. */
17298 && args1 != NULL_TREE && args2 != NULL_TREE)
17299 {
17300 tree arg1 = TREE_VALUE (args1);
17301 tree arg2 = TREE_VALUE (args2);
17302 int deduce1, deduce2;
17303 int quals1 = -1;
17304 int quals2 = -1;
17305
17306 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17307 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17308 {
17309 /* When both arguments are pack expansions, we need only
17310 unify the patterns themselves. */
17311 arg1 = PACK_EXPANSION_PATTERN (arg1);
17312 arg2 = PACK_EXPANSION_PATTERN (arg2);
17313
17314 /* This is the last comparison we need to do. */
17315 len = 0;
17316 }
17317
17318 if (TREE_CODE (arg1) == REFERENCE_TYPE)
17319 {
17320 arg1 = TREE_TYPE (arg1);
17321 quals1 = cp_type_quals (arg1);
17322 }
17323
17324 if (TREE_CODE (arg2) == REFERENCE_TYPE)
17325 {
17326 arg2 = TREE_TYPE (arg2);
17327 quals2 = cp_type_quals (arg2);
17328 }
17329
17330 arg1 = TYPE_MAIN_VARIANT (arg1);
17331 arg2 = TYPE_MAIN_VARIANT (arg2);
17332
17333 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
17334 {
17335 int i, len2 = list_length (args2);
17336 tree parmvec = make_tree_vec (1);
17337 tree argvec = make_tree_vec (len2);
17338 tree ta = args2;
17339
17340 /* Setup the parameter vector, which contains only ARG1. */
17341 TREE_VEC_ELT (parmvec, 0) = arg1;
17342
17343 /* Setup the argument vector, which contains the remaining
17344 arguments. */
17345 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
17346 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17347
17348 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
17349 argvec, DEDUCE_EXACT,
17350 /*subr=*/true, /*explain_p=*/false)
17351 == 0);
17352
17353 /* We cannot deduce in the other direction, because ARG1 is
17354 a pack expansion but ARG2 is not. */
17355 deduce2 = 0;
17356 }
17357 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17358 {
17359 int i, len1 = list_length (args1);
17360 tree parmvec = make_tree_vec (1);
17361 tree argvec = make_tree_vec (len1);
17362 tree ta = args1;
17363
17364 /* Setup the parameter vector, which contains only ARG1. */
17365 TREE_VEC_ELT (parmvec, 0) = arg2;
17366
17367 /* Setup the argument vector, which contains the remaining
17368 arguments. */
17369 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
17370 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17371
17372 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
17373 argvec, DEDUCE_EXACT,
17374 /*subr=*/true, /*explain_p=*/false)
17375 == 0);
17376
17377 /* We cannot deduce in the other direction, because ARG2 is
17378 a pack expansion but ARG1 is not.*/
17379 deduce1 = 0;
17380 }
17381
17382 else
17383 {
17384 /* The normal case, where neither argument is a pack
17385 expansion. */
17386 deduce1 = (unify (tparms1, targs1, arg1, arg2,
17387 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17388 == 0);
17389 deduce2 = (unify (tparms2, targs2, arg2, arg1,
17390 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17391 == 0);
17392 }
17393
17394 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17395 arg2, then arg2 is not as specialized as arg1. */
17396 if (!deduce1)
17397 lose2 = true;
17398 if (!deduce2)
17399 lose1 = true;
17400
17401 /* "If, for a given type, deduction succeeds in both directions
17402 (i.e., the types are identical after the transformations above)
17403 and if the type from the argument template is more cv-qualified
17404 than the type from the parameter template (as described above)
17405 that type is considered to be more specialized than the other. If
17406 neither type is more cv-qualified than the other then neither type
17407 is more specialized than the other." */
17408
17409 if (deduce1 && deduce2
17410 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17411 {
17412 if ((quals1 & quals2) == quals2)
17413 lose2 = true;
17414 if ((quals1 & quals2) == quals1)
17415 lose1 = true;
17416 }
17417
17418 if (lose1 && lose2)
17419 /* We've failed to deduce something in either direction.
17420 These must be unordered. */
17421 break;
17422
17423 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17424 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17425 /* We have already processed all of the arguments in our
17426 handing of the pack expansion type. */
17427 len = 0;
17428
17429 args1 = TREE_CHAIN (args1);
17430 args2 = TREE_CHAIN (args2);
17431 }
17432
17433 /* "In most cases, all template parameters must have values in order for
17434 deduction to succeed, but for partial ordering purposes a template
17435 parameter may remain without a value provided it is not used in the
17436 types being used for partial ordering."
17437
17438 Thus, if we are missing any of the targs1 we need to substitute into
17439 origs1, then pat2 is not as specialized as pat1. This can happen when
17440 there is a nondeduced context. */
17441 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17442 lose2 = true;
17443 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17444 lose1 = true;
17445
17446 processing_template_decl--;
17447
17448 /* All things being equal, if the next argument is a pack expansion
17449 for one function but not for the other, prefer the
17450 non-variadic function. FIXME this is bogus; see c++/41958. */
17451 if (lose1 == lose2
17452 && args1 && TREE_VALUE (args1)
17453 && args2 && TREE_VALUE (args2))
17454 {
17455 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17456 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17457 }
17458
17459 if (lose1 == lose2)
17460 return 0;
17461 else if (!lose1)
17462 return 1;
17463 else
17464 return -1;
17465 }
17466
17467 /* Determine which of two partial specializations of MAIN_TMPL is more
17468 specialized.
17469
17470 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17471 to the first partial specialization. The TREE_VALUE is the
17472 innermost set of template parameters for the partial
17473 specialization. PAT2 is similar, but for the second template.
17474
17475 Return 1 if the first partial specialization is more specialized;
17476 -1 if the second is more specialized; 0 if neither is more
17477 specialized.
17478
17479 See [temp.class.order] for information about determining which of
17480 two templates is more specialized. */
17481
17482 static int
17483 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
17484 {
17485 tree targs;
17486 tree tmpl1, tmpl2;
17487 int winner = 0;
17488 bool any_deductions = false;
17489
17490 tmpl1 = TREE_TYPE (pat1);
17491 tmpl2 = TREE_TYPE (pat2);
17492
17493 /* Just like what happens for functions, if we are ordering between
17494 different class template specializations, we may encounter dependent
17495 types in the arguments, and we need our dependency check functions
17496 to behave correctly. */
17497 ++processing_template_decl;
17498 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
17499 CLASSTYPE_TI_ARGS (tmpl1),
17500 CLASSTYPE_TI_ARGS (tmpl2));
17501 if (targs)
17502 {
17503 --winner;
17504 any_deductions = true;
17505 }
17506
17507 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
17508 CLASSTYPE_TI_ARGS (tmpl2),
17509 CLASSTYPE_TI_ARGS (tmpl1));
17510 if (targs)
17511 {
17512 ++winner;
17513 any_deductions = true;
17514 }
17515 --processing_template_decl;
17516
17517 /* In the case of a tie where at least one of the class templates
17518 has a parameter pack at the end, the template with the most
17519 non-packed parameters wins. */
17520 if (winner == 0
17521 && any_deductions
17522 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17523 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17524 {
17525 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17526 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17527 int len1 = TREE_VEC_LENGTH (args1);
17528 int len2 = TREE_VEC_LENGTH (args2);
17529
17530 /* We don't count the pack expansion at the end. */
17531 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17532 --len1;
17533 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17534 --len2;
17535
17536 if (len1 > len2)
17537 return 1;
17538 else if (len1 < len2)
17539 return -1;
17540 }
17541
17542 return winner;
17543 }
17544
17545 /* Return the template arguments that will produce the function signature
17546 DECL from the function template FN, with the explicit template
17547 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17548 also match. Return NULL_TREE if no satisfactory arguments could be
17549 found. */
17550
17551 static tree
17552 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17553 {
17554 int ntparms = DECL_NTPARMS (fn);
17555 tree targs = make_tree_vec (ntparms);
17556 tree decl_type = TREE_TYPE (decl);
17557 tree decl_arg_types;
17558 tree *args;
17559 unsigned int nargs, ix;
17560 tree arg;
17561
17562 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
17563
17564 /* Never do unification on the 'this' parameter. */
17565 decl_arg_types = skip_artificial_parms_for (decl,
17566 TYPE_ARG_TYPES (decl_type));
17567
17568 nargs = list_length (decl_arg_types);
17569 args = XALLOCAVEC (tree, nargs);
17570 for (arg = decl_arg_types, ix = 0;
17571 arg != NULL_TREE && arg != void_list_node;
17572 arg = TREE_CHAIN (arg), ++ix)
17573 args[ix] = TREE_VALUE (arg);
17574
17575 if (fn_type_unification (fn, explicit_args, targs,
17576 args, ix,
17577 (check_rettype || DECL_CONV_FN_P (fn)
17578 ? TREE_TYPE (decl_type) : NULL_TREE),
17579 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)
17580 == error_mark_node)
17581 return NULL_TREE;
17582
17583 return targs;
17584 }
17585
17586 /* Return the innermost template arguments that, when applied to a partial
17587 specialization of MAIN_TMPL whose innermost template parameters are
17588 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17589 ARGS.
17590
17591 For example, suppose we have:
17592
17593 template <class T, class U> struct S {};
17594 template <class T> struct S<T*, int> {};
17595
17596 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17597 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17598 int}. The resulting vector will be {double}, indicating that `T'
17599 is bound to `double'. */
17600
17601 static tree
17602 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
17603 {
17604 int i, ntparms = TREE_VEC_LENGTH (tparms);
17605 tree deduced_args;
17606 tree innermost_deduced_args;
17607
17608 innermost_deduced_args = make_tree_vec (ntparms);
17609 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17610 {
17611 deduced_args = copy_node (args);
17612 SET_TMPL_ARGS_LEVEL (deduced_args,
17613 TMPL_ARGS_DEPTH (deduced_args),
17614 innermost_deduced_args);
17615 }
17616 else
17617 deduced_args = innermost_deduced_args;
17618
17619 if (unify (tparms, deduced_args,
17620 INNERMOST_TEMPLATE_ARGS (spec_args),
17621 INNERMOST_TEMPLATE_ARGS (args),
17622 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17623 return NULL_TREE;
17624
17625 for (i = 0; i < ntparms; ++i)
17626 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17627 return NULL_TREE;
17628
17629 /* Verify that nondeduced template arguments agree with the type
17630 obtained from argument deduction.
17631
17632 For example:
17633
17634 struct A { typedef int X; };
17635 template <class T, class U> struct C {};
17636 template <class T> struct C<T, typename T::X> {};
17637
17638 Then with the instantiation `C<A, int>', we can deduce that
17639 `T' is `A' but unify () does not check whether `typename T::X'
17640 is `int'. */
17641 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17642 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
17643 spec_args, main_tmpl,
17644 tf_none, false, false);
17645 if (spec_args == error_mark_node
17646 /* We only need to check the innermost arguments; the other
17647 arguments will always agree. */
17648 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17649 INNERMOST_TEMPLATE_ARGS (args)))
17650 return NULL_TREE;
17651
17652 /* Now that we have bindings for all of the template arguments,
17653 ensure that the arguments deduced for the template template
17654 parameters have compatible template parameter lists. See the use
17655 of template_template_parm_bindings_ok_p in fn_type_unification
17656 for more information. */
17657 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17658 return NULL_TREE;
17659
17660 return deduced_args;
17661 }
17662
17663 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17664 Return the TREE_LIST node with the most specialized template, if
17665 any. If there is no most specialized template, the error_mark_node
17666 is returned.
17667
17668 Note that this function does not look at, or modify, the
17669 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17670 returned is one of the elements of INSTANTIATIONS, callers may
17671 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17672 and retrieve it from the value returned. */
17673
17674 tree
17675 most_specialized_instantiation (tree templates)
17676 {
17677 tree fn, champ;
17678
17679 ++processing_template_decl;
17680
17681 champ = templates;
17682 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17683 {
17684 int fate = 0;
17685
17686 if (get_bindings (TREE_VALUE (champ),
17687 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17688 NULL_TREE, /*check_ret=*/true))
17689 fate--;
17690
17691 if (get_bindings (TREE_VALUE (fn),
17692 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17693 NULL_TREE, /*check_ret=*/true))
17694 fate++;
17695
17696 if (fate == -1)
17697 champ = fn;
17698 else if (!fate)
17699 {
17700 /* Equally specialized, move to next function. If there
17701 is no next function, nothing's most specialized. */
17702 fn = TREE_CHAIN (fn);
17703 champ = fn;
17704 if (!fn)
17705 break;
17706 }
17707 }
17708
17709 if (champ)
17710 /* Now verify that champ is better than everything earlier in the
17711 instantiation list. */
17712 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17713 if (get_bindings (TREE_VALUE (champ),
17714 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17715 NULL_TREE, /*check_ret=*/true)
17716 || !get_bindings (TREE_VALUE (fn),
17717 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17718 NULL_TREE, /*check_ret=*/true))
17719 {
17720 champ = NULL_TREE;
17721 break;
17722 }
17723
17724 processing_template_decl--;
17725
17726 if (!champ)
17727 return error_mark_node;
17728
17729 return champ;
17730 }
17731
17732 /* If DECL is a specialization of some template, return the most
17733 general such template. Otherwise, returns NULL_TREE.
17734
17735 For example, given:
17736
17737 template <class T> struct S { template <class U> void f(U); };
17738
17739 if TMPL is `template <class U> void S<int>::f(U)' this will return
17740 the full template. This function will not trace past partial
17741 specializations, however. For example, given in addition:
17742
17743 template <class T> struct S<T*> { template <class U> void f(U); };
17744
17745 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17746 `template <class T> template <class U> S<T*>::f(U)'. */
17747
17748 tree
17749 most_general_template (tree decl)
17750 {
17751 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17752 an immediate specialization. */
17753 if (TREE_CODE (decl) == FUNCTION_DECL)
17754 {
17755 if (DECL_TEMPLATE_INFO (decl)) {
17756 decl = DECL_TI_TEMPLATE (decl);
17757
17758 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17759 template friend. */
17760 if (TREE_CODE (decl) != TEMPLATE_DECL)
17761 return NULL_TREE;
17762 } else
17763 return NULL_TREE;
17764 }
17765
17766 /* Look for more and more general templates. */
17767 while (DECL_TEMPLATE_INFO (decl))
17768 {
17769 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17770 (See cp-tree.h for details.) */
17771 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17772 break;
17773
17774 if (CLASS_TYPE_P (TREE_TYPE (decl))
17775 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17776 break;
17777
17778 /* Stop if we run into an explicitly specialized class template. */
17779 if (!DECL_NAMESPACE_SCOPE_P (decl)
17780 && DECL_CONTEXT (decl)
17781 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17782 break;
17783
17784 decl = DECL_TI_TEMPLATE (decl);
17785 }
17786
17787 return decl;
17788 }
17789
17790 /* Return the most specialized of the class template partial
17791 specializations of TMPL which can produce TYPE, a specialization of
17792 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17793 a _TYPE node corresponding to the partial specialization, while the
17794 TREE_PURPOSE is the set of template arguments that must be
17795 substituted into the TREE_TYPE in order to generate TYPE.
17796
17797 If the choice of partial specialization is ambiguous, a diagnostic
17798 is issued, and the error_mark_node is returned. If there are no
17799 partial specializations of TMPL matching TYPE, then NULL_TREE is
17800 returned. */
17801
17802 static tree
17803 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17804 {
17805 tree list = NULL_TREE;
17806 tree t;
17807 tree champ;
17808 int fate;
17809 bool ambiguous_p;
17810 tree args;
17811 tree outer_args = NULL_TREE;
17812
17813 tmpl = most_general_template (tmpl);
17814 args = CLASSTYPE_TI_ARGS (type);
17815
17816 /* For determining which partial specialization to use, only the
17817 innermost args are interesting. */
17818 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17819 {
17820 outer_args = strip_innermost_template_args (args, 1);
17821 args = INNERMOST_TEMPLATE_ARGS (args);
17822 }
17823
17824 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17825 {
17826 tree partial_spec_args;
17827 tree spec_args;
17828 tree parms = TREE_VALUE (t);
17829
17830 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17831
17832 ++processing_template_decl;
17833
17834 if (outer_args)
17835 {
17836 int i;
17837
17838 /* Discard the outer levels of args, and then substitute in the
17839 template args from the enclosing class. */
17840 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17841 partial_spec_args = tsubst_template_args
17842 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17843
17844 /* PARMS already refers to just the innermost parms, but the
17845 template parms in partial_spec_args had their levels lowered
17846 by tsubst, so we need to do the same for the parm list. We
17847 can't just tsubst the TREE_VEC itself, as tsubst wants to
17848 treat a TREE_VEC as an argument vector. */
17849 parms = copy_node (parms);
17850 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17851 TREE_VEC_ELT (parms, i) =
17852 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17853
17854 }
17855
17856 partial_spec_args =
17857 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17858 add_to_template_args (outer_args,
17859 partial_spec_args),
17860 tmpl, tf_none,
17861 /*require_all_args=*/true,
17862 /*use_default_args=*/true);
17863
17864 --processing_template_decl;
17865
17866 if (partial_spec_args == error_mark_node)
17867 return error_mark_node;
17868
17869 spec_args = get_class_bindings (tmpl, parms,
17870 partial_spec_args,
17871 args);
17872 if (spec_args)
17873 {
17874 if (outer_args)
17875 spec_args = add_to_template_args (outer_args, spec_args);
17876 list = tree_cons (spec_args, TREE_VALUE (t), list);
17877 TREE_TYPE (list) = TREE_TYPE (t);
17878 }
17879 }
17880
17881 if (! list)
17882 return NULL_TREE;
17883
17884 ambiguous_p = false;
17885 t = list;
17886 champ = t;
17887 t = TREE_CHAIN (t);
17888 for (; t; t = TREE_CHAIN (t))
17889 {
17890 fate = more_specialized_class (tmpl, champ, t);
17891 if (fate == 1)
17892 ;
17893 else
17894 {
17895 if (fate == 0)
17896 {
17897 t = TREE_CHAIN (t);
17898 if (! t)
17899 {
17900 ambiguous_p = true;
17901 break;
17902 }
17903 }
17904 champ = t;
17905 }
17906 }
17907
17908 if (!ambiguous_p)
17909 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17910 {
17911 fate = more_specialized_class (tmpl, champ, t);
17912 if (fate != 1)
17913 {
17914 ambiguous_p = true;
17915 break;
17916 }
17917 }
17918
17919 if (ambiguous_p)
17920 {
17921 const char *str;
17922 char *spaces = NULL;
17923 if (!(complain & tf_error))
17924 return error_mark_node;
17925 error ("ambiguous class template instantiation for %q#T", type);
17926 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17927 for (t = list; t; t = TREE_CHAIN (t))
17928 {
17929 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17930 spaces = spaces ? spaces : get_spaces (str);
17931 }
17932 free (spaces);
17933 return error_mark_node;
17934 }
17935
17936 return champ;
17937 }
17938
17939 /* Explicitly instantiate DECL. */
17940
17941 void
17942 do_decl_instantiation (tree decl, tree storage)
17943 {
17944 tree result = NULL_TREE;
17945 int extern_p = 0;
17946
17947 if (!decl || decl == error_mark_node)
17948 /* An error occurred, for which grokdeclarator has already issued
17949 an appropriate message. */
17950 return;
17951 else if (! DECL_LANG_SPECIFIC (decl))
17952 {
17953 error ("explicit instantiation of non-template %q#D", decl);
17954 return;
17955 }
17956 else if (TREE_CODE (decl) == VAR_DECL)
17957 {
17958 /* There is an asymmetry here in the way VAR_DECLs and
17959 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17960 the latter, the DECL we get back will be marked as a
17961 template instantiation, and the appropriate
17962 DECL_TEMPLATE_INFO will be set up. This does not happen for
17963 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17964 should handle VAR_DECLs as it currently handles
17965 FUNCTION_DECLs. */
17966 if (!DECL_CLASS_SCOPE_P (decl))
17967 {
17968 error ("%qD is not a static data member of a class template", decl);
17969 return;
17970 }
17971 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17972 if (!result || TREE_CODE (result) != VAR_DECL)
17973 {
17974 error ("no matching template for %qD found", decl);
17975 return;
17976 }
17977 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17978 {
17979 error ("type %qT for explicit instantiation %qD does not match "
17980 "declared type %qT", TREE_TYPE (result), decl,
17981 TREE_TYPE (decl));
17982 return;
17983 }
17984 }
17985 else if (TREE_CODE (decl) != FUNCTION_DECL)
17986 {
17987 error ("explicit instantiation of %q#D", decl);
17988 return;
17989 }
17990 else
17991 result = decl;
17992
17993 /* Check for various error cases. Note that if the explicit
17994 instantiation is valid the RESULT will currently be marked as an
17995 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17996 until we get here. */
17997
17998 if (DECL_TEMPLATE_SPECIALIZATION (result))
17999 {
18000 /* DR 259 [temp.spec].
18001
18002 Both an explicit instantiation and a declaration of an explicit
18003 specialization shall not appear in a program unless the explicit
18004 instantiation follows a declaration of the explicit specialization.
18005
18006 For a given set of template parameters, if an explicit
18007 instantiation of a template appears after a declaration of an
18008 explicit specialization for that template, the explicit
18009 instantiation has no effect. */
18010 return;
18011 }
18012 else if (DECL_EXPLICIT_INSTANTIATION (result))
18013 {
18014 /* [temp.spec]
18015
18016 No program shall explicitly instantiate any template more
18017 than once.
18018
18019 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18020 the first instantiation was `extern' and the second is not,
18021 and EXTERN_P for the opposite case. */
18022 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18023 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18024 /* If an "extern" explicit instantiation follows an ordinary
18025 explicit instantiation, the template is instantiated. */
18026 if (extern_p)
18027 return;
18028 }
18029 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18030 {
18031 error ("no matching template for %qD found", result);
18032 return;
18033 }
18034 else if (!DECL_TEMPLATE_INFO (result))
18035 {
18036 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18037 return;
18038 }
18039
18040 if (storage == NULL_TREE)
18041 ;
18042 else if (storage == ridpointers[(int) RID_EXTERN])
18043 {
18044 if (!in_system_header && (cxx_dialect == cxx98))
18045 pedwarn (input_location, OPT_Wpedantic,
18046 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18047 "instantiations");
18048 extern_p = 1;
18049 }
18050 else
18051 error ("storage class %qD applied to template instantiation", storage);
18052
18053 check_explicit_instantiation_namespace (result);
18054 mark_decl_instantiated (result, extern_p);
18055 if (! extern_p)
18056 instantiate_decl (result, /*defer_ok=*/1,
18057 /*expl_inst_class_mem_p=*/false);
18058 }
18059
18060 static void
18061 mark_class_instantiated (tree t, int extern_p)
18062 {
18063 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18064 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18065 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18066 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18067 if (! extern_p)
18068 {
18069 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18070 rest_of_type_compilation (t, 1);
18071 }
18072 }
18073
18074 /* Called from do_type_instantiation through binding_table_foreach to
18075 do recursive instantiation for the type bound in ENTRY. */
18076 static void
18077 bt_instantiate_type_proc (binding_entry entry, void *data)
18078 {
18079 tree storage = *(tree *) data;
18080
18081 if (MAYBE_CLASS_TYPE_P (entry->type)
18082 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18083 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18084 }
18085
18086 /* Called from do_type_instantiation to instantiate a member
18087 (a member function or a static member variable) of an
18088 explicitly instantiated class template. */
18089 static void
18090 instantiate_class_member (tree decl, int extern_p)
18091 {
18092 mark_decl_instantiated (decl, extern_p);
18093 if (! extern_p)
18094 instantiate_decl (decl, /*defer_ok=*/1,
18095 /*expl_inst_class_mem_p=*/true);
18096 }
18097
18098 /* Perform an explicit instantiation of template class T. STORAGE, if
18099 non-null, is the RID for extern, inline or static. COMPLAIN is
18100 nonzero if this is called from the parser, zero if called recursively,
18101 since the standard is unclear (as detailed below). */
18102
18103 void
18104 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18105 {
18106 int extern_p = 0;
18107 int nomem_p = 0;
18108 int static_p = 0;
18109 int previous_instantiation_extern_p = 0;
18110
18111 if (TREE_CODE (t) == TYPE_DECL)
18112 t = TREE_TYPE (t);
18113
18114 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18115 {
18116 tree tmpl =
18117 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18118 if (tmpl)
18119 error ("explicit instantiation of non-class template %qD", tmpl);
18120 else
18121 error ("explicit instantiation of non-template type %qT", t);
18122 return;
18123 }
18124
18125 complete_type (t);
18126
18127 if (!COMPLETE_TYPE_P (t))
18128 {
18129 if (complain & tf_error)
18130 error ("explicit instantiation of %q#T before definition of template",
18131 t);
18132 return;
18133 }
18134
18135 if (storage != NULL_TREE)
18136 {
18137 if (!in_system_header)
18138 {
18139 if (storage == ridpointers[(int) RID_EXTERN])
18140 {
18141 if (cxx_dialect == cxx98)
18142 pedwarn (input_location, OPT_Wpedantic,
18143 "ISO C++ 1998 forbids the use of %<extern%> on "
18144 "explicit instantiations");
18145 }
18146 else
18147 pedwarn (input_location, OPT_Wpedantic,
18148 "ISO C++ forbids the use of %qE"
18149 " on explicit instantiations", storage);
18150 }
18151
18152 if (storage == ridpointers[(int) RID_INLINE])
18153 nomem_p = 1;
18154 else if (storage == ridpointers[(int) RID_EXTERN])
18155 extern_p = 1;
18156 else if (storage == ridpointers[(int) RID_STATIC])
18157 static_p = 1;
18158 else
18159 {
18160 error ("storage class %qD applied to template instantiation",
18161 storage);
18162 extern_p = 0;
18163 }
18164 }
18165
18166 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18167 {
18168 /* DR 259 [temp.spec].
18169
18170 Both an explicit instantiation and a declaration of an explicit
18171 specialization shall not appear in a program unless the explicit
18172 instantiation follows a declaration of the explicit specialization.
18173
18174 For a given set of template parameters, if an explicit
18175 instantiation of a template appears after a declaration of an
18176 explicit specialization for that template, the explicit
18177 instantiation has no effect. */
18178 return;
18179 }
18180 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18181 {
18182 /* [temp.spec]
18183
18184 No program shall explicitly instantiate any template more
18185 than once.
18186
18187 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18188 instantiation was `extern'. If EXTERN_P then the second is.
18189 These cases are OK. */
18190 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18191
18192 if (!previous_instantiation_extern_p && !extern_p
18193 && (complain & tf_error))
18194 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18195
18196 /* If we've already instantiated the template, just return now. */
18197 if (!CLASSTYPE_INTERFACE_ONLY (t))
18198 return;
18199 }
18200
18201 check_explicit_instantiation_namespace (TYPE_NAME (t));
18202 mark_class_instantiated (t, extern_p);
18203
18204 if (nomem_p)
18205 return;
18206
18207 {
18208 tree tmp;
18209
18210 /* In contrast to implicit instantiation, where only the
18211 declarations, and not the definitions, of members are
18212 instantiated, we have here:
18213
18214 [temp.explicit]
18215
18216 The explicit instantiation of a class template specialization
18217 implies the instantiation of all of its members not
18218 previously explicitly specialized in the translation unit
18219 containing the explicit instantiation.
18220
18221 Of course, we can't instantiate member template classes, since
18222 we don't have any arguments for them. Note that the standard
18223 is unclear on whether the instantiation of the members are
18224 *explicit* instantiations or not. However, the most natural
18225 interpretation is that it should be an explicit instantiation. */
18226
18227 if (! static_p)
18228 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18229 if (TREE_CODE (tmp) == FUNCTION_DECL
18230 && DECL_TEMPLATE_INSTANTIATION (tmp))
18231 instantiate_class_member (tmp, extern_p);
18232
18233 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18234 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
18235 instantiate_class_member (tmp, extern_p);
18236
18237 if (CLASSTYPE_NESTED_UTDS (t))
18238 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18239 bt_instantiate_type_proc, &storage);
18240 }
18241 }
18242
18243 /* Given a function DECL, which is a specialization of TMPL, modify
18244 DECL to be a re-instantiation of TMPL with the same template
18245 arguments. TMPL should be the template into which tsubst'ing
18246 should occur for DECL, not the most general template.
18247
18248 One reason for doing this is a scenario like this:
18249
18250 template <class T>
18251 void f(const T&, int i);
18252
18253 void g() { f(3, 7); }
18254
18255 template <class T>
18256 void f(const T& t, const int i) { }
18257
18258 Note that when the template is first instantiated, with
18259 instantiate_template, the resulting DECL will have no name for the
18260 first parameter, and the wrong type for the second. So, when we go
18261 to instantiate the DECL, we regenerate it. */
18262
18263 static void
18264 regenerate_decl_from_template (tree decl, tree tmpl)
18265 {
18266 /* The arguments used to instantiate DECL, from the most general
18267 template. */
18268 tree args;
18269 tree code_pattern;
18270
18271 args = DECL_TI_ARGS (decl);
18272 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
18273
18274 /* Make sure that we can see identifiers, and compute access
18275 correctly. */
18276 push_access_scope (decl);
18277
18278 if (TREE_CODE (decl) == FUNCTION_DECL)
18279 {
18280 tree decl_parm;
18281 tree pattern_parm;
18282 tree specs;
18283 int args_depth;
18284 int parms_depth;
18285
18286 args_depth = TMPL_ARGS_DEPTH (args);
18287 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
18288 if (args_depth > parms_depth)
18289 args = get_innermost_template_args (args, parms_depth);
18290
18291 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
18292 args, tf_error, NULL_TREE,
18293 /*defer_ok*/false);
18294 if (specs && specs != error_mark_node)
18295 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
18296 specs);
18297
18298 /* Merge parameter declarations. */
18299 decl_parm = skip_artificial_parms_for (decl,
18300 DECL_ARGUMENTS (decl));
18301 pattern_parm
18302 = skip_artificial_parms_for (code_pattern,
18303 DECL_ARGUMENTS (code_pattern));
18304 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
18305 {
18306 tree parm_type;
18307 tree attributes;
18308
18309 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18310 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
18311 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
18312 NULL_TREE);
18313 parm_type = type_decays_to (parm_type);
18314 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18315 TREE_TYPE (decl_parm) = parm_type;
18316 attributes = DECL_ATTRIBUTES (pattern_parm);
18317 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18318 {
18319 DECL_ATTRIBUTES (decl_parm) = attributes;
18320 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18321 }
18322 decl_parm = DECL_CHAIN (decl_parm);
18323 pattern_parm = DECL_CHAIN (pattern_parm);
18324 }
18325 /* Merge any parameters that match with the function parameter
18326 pack. */
18327 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
18328 {
18329 int i, len;
18330 tree expanded_types;
18331 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18332 the parameters in this function parameter pack. */
18333 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
18334 args, tf_error, NULL_TREE);
18335 len = TREE_VEC_LENGTH (expanded_types);
18336 for (i = 0; i < len; i++)
18337 {
18338 tree parm_type;
18339 tree attributes;
18340
18341 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18342 /* Rename the parameter to include the index. */
18343 DECL_NAME (decl_parm) =
18344 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
18345 parm_type = TREE_VEC_ELT (expanded_types, i);
18346 parm_type = type_decays_to (parm_type);
18347 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18348 TREE_TYPE (decl_parm) = parm_type;
18349 attributes = DECL_ATTRIBUTES (pattern_parm);
18350 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18351 {
18352 DECL_ATTRIBUTES (decl_parm) = attributes;
18353 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18354 }
18355 decl_parm = DECL_CHAIN (decl_parm);
18356 }
18357 }
18358 /* Merge additional specifiers from the CODE_PATTERN. */
18359 if (DECL_DECLARED_INLINE_P (code_pattern)
18360 && !DECL_DECLARED_INLINE_P (decl))
18361 DECL_DECLARED_INLINE_P (decl) = 1;
18362 }
18363 else if (TREE_CODE (decl) == VAR_DECL)
18364 {
18365 DECL_INITIAL (decl) =
18366 tsubst_expr (DECL_INITIAL (code_pattern), args,
18367 tf_error, DECL_TI_TEMPLATE (decl),
18368 /*integral_constant_expression_p=*/false);
18369 if (VAR_HAD_UNKNOWN_BOUND (decl))
18370 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18371 tf_error, DECL_TI_TEMPLATE (decl));
18372 }
18373 else
18374 gcc_unreachable ();
18375
18376 pop_access_scope (decl);
18377 }
18378
18379 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18380 substituted to get DECL. */
18381
18382 tree
18383 template_for_substitution (tree decl)
18384 {
18385 tree tmpl = DECL_TI_TEMPLATE (decl);
18386
18387 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18388 for the instantiation. This is not always the most general
18389 template. Consider, for example:
18390
18391 template <class T>
18392 struct S { template <class U> void f();
18393 template <> void f<int>(); };
18394
18395 and an instantiation of S<double>::f<int>. We want TD to be the
18396 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18397 while (/* An instantiation cannot have a definition, so we need a
18398 more general template. */
18399 DECL_TEMPLATE_INSTANTIATION (tmpl)
18400 /* We must also deal with friend templates. Given:
18401
18402 template <class T> struct S {
18403 template <class U> friend void f() {};
18404 };
18405
18406 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18407 so far as the language is concerned, but that's still
18408 where we get the pattern for the instantiation from. On
18409 other hand, if the definition comes outside the class, say:
18410
18411 template <class T> struct S {
18412 template <class U> friend void f();
18413 };
18414 template <class U> friend void f() {}
18415
18416 we don't need to look any further. That's what the check for
18417 DECL_INITIAL is for. */
18418 || (TREE_CODE (decl) == FUNCTION_DECL
18419 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18420 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18421 {
18422 /* The present template, TD, should not be a definition. If it
18423 were a definition, we should be using it! Note that we
18424 cannot restructure the loop to just keep going until we find
18425 a template with a definition, since that might go too far if
18426 a specialization was declared, but not defined. */
18427 gcc_assert (TREE_CODE (decl) != VAR_DECL
18428 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18429
18430 /* Fetch the more general template. */
18431 tmpl = DECL_TI_TEMPLATE (tmpl);
18432 }
18433
18434 return tmpl;
18435 }
18436
18437 /* Returns true if we need to instantiate this template instance even if we
18438 know we aren't going to emit it.. */
18439
18440 bool
18441 always_instantiate_p (tree decl)
18442 {
18443 /* We always instantiate inline functions so that we can inline them. An
18444 explicit instantiation declaration prohibits implicit instantiation of
18445 non-inline functions. With high levels of optimization, we would
18446 normally inline non-inline functions -- but we're not allowed to do
18447 that for "extern template" functions. Therefore, we check
18448 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18449 return ((TREE_CODE (decl) == FUNCTION_DECL
18450 && (DECL_DECLARED_INLINE_P (decl)
18451 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
18452 /* And we need to instantiate static data members so that
18453 their initializers are available in integral constant
18454 expressions. */
18455 || (TREE_CODE (decl) == VAR_DECL
18456 && decl_maybe_constant_var_p (decl)));
18457 }
18458
18459 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18460 instantiate it now, modifying TREE_TYPE (fn). */
18461
18462 void
18463 maybe_instantiate_noexcept (tree fn)
18464 {
18465 tree fntype, spec, noex, clone;
18466
18467 if (DECL_CLONED_FUNCTION_P (fn))
18468 fn = DECL_CLONED_FUNCTION (fn);
18469 fntype = TREE_TYPE (fn);
18470 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18471
18472 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18473 return;
18474
18475 noex = TREE_PURPOSE (spec);
18476
18477 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18478 {
18479 if (push_tinst_level (fn))
18480 {
18481 push_access_scope (fn);
18482 push_deferring_access_checks (dk_no_deferred);
18483 input_location = DECL_SOURCE_LOCATION (fn);
18484 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18485 DEFERRED_NOEXCEPT_ARGS (noex),
18486 tf_warning_or_error, fn,
18487 /*function_p=*/false,
18488 /*integral_constant_expression_p=*/true);
18489 pop_deferring_access_checks ();
18490 pop_access_scope (fn);
18491 pop_tinst_level ();
18492 spec = build_noexcept_spec (noex, tf_warning_or_error);
18493 if (spec == error_mark_node)
18494 spec = noexcept_false_spec;
18495 }
18496 else
18497 spec = noexcept_false_spec;
18498 }
18499 else
18500 {
18501 /* This is an implicitly declared function, so NOEX is a list of
18502 other functions to evaluate and merge. */
18503 tree elt;
18504 spec = noexcept_true_spec;
18505 for (elt = noex; elt; elt = OVL_NEXT (elt))
18506 {
18507 tree fn = OVL_CURRENT (elt);
18508 tree subspec;
18509 maybe_instantiate_noexcept (fn);
18510 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18511 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18512 }
18513 }
18514
18515 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18516
18517 FOR_EACH_CLONE (clone, fn)
18518 {
18519 if (TREE_TYPE (clone) == fntype)
18520 TREE_TYPE (clone) = TREE_TYPE (fn);
18521 else
18522 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18523 }
18524 }
18525
18526 /* Produce the definition of D, a _DECL generated from a template. If
18527 DEFER_OK is nonzero, then we don't have to actually do the
18528 instantiation now; we just have to do it sometime. Normally it is
18529 an error if this is an explicit instantiation but D is undefined.
18530 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18531 explicitly instantiated class template. */
18532
18533 tree
18534 instantiate_decl (tree d, int defer_ok,
18535 bool expl_inst_class_mem_p)
18536 {
18537 tree tmpl = DECL_TI_TEMPLATE (d);
18538 tree gen_args;
18539 tree args;
18540 tree td;
18541 tree code_pattern;
18542 tree spec;
18543 tree gen_tmpl;
18544 bool pattern_defined;
18545 location_t saved_loc = input_location;
18546 bool external_p;
18547 tree fn_context;
18548 bool nested;
18549
18550 /* This function should only be used to instantiate templates for
18551 functions and static member variables. */
18552 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18553 || TREE_CODE (d) == VAR_DECL);
18554
18555 /* Variables are never deferred; if instantiation is required, they
18556 are instantiated right away. That allows for better code in the
18557 case that an expression refers to the value of the variable --
18558 if the variable has a constant value the referring expression can
18559 take advantage of that fact. */
18560 if (TREE_CODE (d) == VAR_DECL
18561 || DECL_DECLARED_CONSTEXPR_P (d))
18562 defer_ok = 0;
18563
18564 /* Don't instantiate cloned functions. Instead, instantiate the
18565 functions they cloned. */
18566 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18567 d = DECL_CLONED_FUNCTION (d);
18568
18569 if (DECL_TEMPLATE_INSTANTIATED (d)
18570 || (TREE_CODE (d) == FUNCTION_DECL
18571 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18572 || DECL_TEMPLATE_SPECIALIZATION (d))
18573 /* D has already been instantiated or explicitly specialized, so
18574 there's nothing for us to do here.
18575
18576 It might seem reasonable to check whether or not D is an explicit
18577 instantiation, and, if so, stop here. But when an explicit
18578 instantiation is deferred until the end of the compilation,
18579 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18580 the instantiation. */
18581 return d;
18582
18583 /* Check to see whether we know that this template will be
18584 instantiated in some other file, as with "extern template"
18585 extension. */
18586 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18587
18588 /* In general, we do not instantiate such templates. */
18589 if (external_p && !always_instantiate_p (d))
18590 return d;
18591
18592 gen_tmpl = most_general_template (tmpl);
18593 gen_args = DECL_TI_ARGS (d);
18594
18595 if (tmpl != gen_tmpl)
18596 /* We should already have the extra args. */
18597 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18598 == TMPL_ARGS_DEPTH (gen_args));
18599 /* And what's in the hash table should match D. */
18600 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18601 || spec == NULL_TREE);
18602
18603 /* This needs to happen before any tsubsting. */
18604 if (! push_tinst_level (d))
18605 return d;
18606
18607 timevar_push (TV_TEMPLATE_INST);
18608
18609 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18610 for the instantiation. */
18611 td = template_for_substitution (d);
18612 code_pattern = DECL_TEMPLATE_RESULT (td);
18613
18614 /* We should never be trying to instantiate a member of a class
18615 template or partial specialization. */
18616 gcc_assert (d != code_pattern);
18617
18618 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18619 || DECL_TEMPLATE_SPECIALIZATION (td))
18620 /* In the case of a friend template whose definition is provided
18621 outside the class, we may have too many arguments. Drop the
18622 ones we don't need. The same is true for specializations. */
18623 args = get_innermost_template_args
18624 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18625 else
18626 args = gen_args;
18627
18628 if (TREE_CODE (d) == FUNCTION_DECL)
18629 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18630 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18631 else
18632 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18633
18634 /* We may be in the middle of deferred access check. Disable it now. */
18635 push_deferring_access_checks (dk_no_deferred);
18636
18637 /* Unless an explicit instantiation directive has already determined
18638 the linkage of D, remember that a definition is available for
18639 this entity. */
18640 if (pattern_defined
18641 && !DECL_INTERFACE_KNOWN (d)
18642 && !DECL_NOT_REALLY_EXTERN (d))
18643 mark_definable (d);
18644
18645 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18646 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18647 input_location = DECL_SOURCE_LOCATION (d);
18648
18649 /* If D is a member of an explicitly instantiated class template,
18650 and no definition is available, treat it like an implicit
18651 instantiation. */
18652 if (!pattern_defined && expl_inst_class_mem_p
18653 && DECL_EXPLICIT_INSTANTIATION (d))
18654 {
18655 /* Leave linkage flags alone on instantiations with anonymous
18656 visibility. */
18657 if (TREE_PUBLIC (d))
18658 {
18659 DECL_NOT_REALLY_EXTERN (d) = 0;
18660 DECL_INTERFACE_KNOWN (d) = 0;
18661 }
18662 SET_DECL_IMPLICIT_INSTANTIATION (d);
18663 }
18664
18665 if (TREE_CODE (d) == FUNCTION_DECL)
18666 maybe_instantiate_noexcept (d);
18667
18668 /* Defer all other templates, unless we have been explicitly
18669 forbidden from doing so. */
18670 if (/* If there is no definition, we cannot instantiate the
18671 template. */
18672 ! pattern_defined
18673 /* If it's OK to postpone instantiation, do so. */
18674 || defer_ok
18675 /* If this is a static data member that will be defined
18676 elsewhere, we don't want to instantiate the entire data
18677 member, but we do want to instantiate the initializer so that
18678 we can substitute that elsewhere. */
18679 || (external_p && TREE_CODE (d) == VAR_DECL))
18680 {
18681 /* The definition of the static data member is now required so
18682 we must substitute the initializer. */
18683 if (TREE_CODE (d) == VAR_DECL
18684 && !DECL_INITIAL (d)
18685 && DECL_INITIAL (code_pattern))
18686 {
18687 tree ns;
18688 tree init;
18689 bool const_init = false;
18690
18691 ns = decl_namespace_context (d);
18692 push_nested_namespace (ns);
18693 push_nested_class (DECL_CONTEXT (d));
18694 init = tsubst_expr (DECL_INITIAL (code_pattern),
18695 args,
18696 tf_warning_or_error, NULL_TREE,
18697 /*integral_constant_expression_p=*/false);
18698 /* Make sure the initializer is still constant, in case of
18699 circular dependency (template/instantiate6.C). */
18700 const_init
18701 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18702 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18703 /*asmspec_tree=*/NULL_TREE,
18704 LOOKUP_ONLYCONVERTING);
18705 pop_nested_class ();
18706 pop_nested_namespace (ns);
18707 }
18708
18709 /* We restore the source position here because it's used by
18710 add_pending_template. */
18711 input_location = saved_loc;
18712
18713 if (at_eof && !pattern_defined
18714 && DECL_EXPLICIT_INSTANTIATION (d)
18715 && DECL_NOT_REALLY_EXTERN (d))
18716 /* [temp.explicit]
18717
18718 The definition of a non-exported function template, a
18719 non-exported member function template, or a non-exported
18720 member function or static data member of a class template
18721 shall be present in every translation unit in which it is
18722 explicitly instantiated. */
18723 permerror (input_location, "explicit instantiation of %qD "
18724 "but no definition available", d);
18725
18726 /* If we're in unevaluated context, we just wanted to get the
18727 constant value; this isn't an odr use, so don't queue
18728 a full instantiation. */
18729 if (cp_unevaluated_operand != 0)
18730 goto out;
18731 /* ??? Historically, we have instantiated inline functions, even
18732 when marked as "extern template". */
18733 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18734 add_pending_template (d);
18735 goto out;
18736 }
18737 /* Tell the repository that D is available in this translation unit
18738 -- and see if it is supposed to be instantiated here. */
18739 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18740 {
18741 /* In a PCH file, despite the fact that the repository hasn't
18742 requested instantiation in the PCH it is still possible that
18743 an instantiation will be required in a file that includes the
18744 PCH. */
18745 if (pch_file)
18746 add_pending_template (d);
18747 /* Instantiate inline functions so that the inliner can do its
18748 job, even though we'll not be emitting a copy of this
18749 function. */
18750 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18751 goto out;
18752 }
18753
18754 fn_context = decl_function_context (d);
18755 nested = (current_function_decl != NULL_TREE);
18756 if (!fn_context)
18757 push_to_top_level ();
18758 else if (nested)
18759 push_function_context ();
18760
18761 /* Mark D as instantiated so that recursive calls to
18762 instantiate_decl do not try to instantiate it again. */
18763 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18764
18765 /* Regenerate the declaration in case the template has been modified
18766 by a subsequent redeclaration. */
18767 regenerate_decl_from_template (d, td);
18768
18769 /* We already set the file and line above. Reset them now in case
18770 they changed as a result of calling regenerate_decl_from_template. */
18771 input_location = DECL_SOURCE_LOCATION (d);
18772
18773 if (TREE_CODE (d) == VAR_DECL)
18774 {
18775 tree init;
18776 bool const_init = false;
18777
18778 /* Clear out DECL_RTL; whatever was there before may not be right
18779 since we've reset the type of the declaration. */
18780 SET_DECL_RTL (d, NULL);
18781 DECL_IN_AGGR_P (d) = 0;
18782
18783 /* The initializer is placed in DECL_INITIAL by
18784 regenerate_decl_from_template so we don't need to
18785 push/pop_access_scope again here. Pull it out so that
18786 cp_finish_decl can process it. */
18787 init = DECL_INITIAL (d);
18788 DECL_INITIAL (d) = NULL_TREE;
18789 DECL_INITIALIZED_P (d) = 0;
18790
18791 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18792 initializer. That function will defer actual emission until
18793 we have a chance to determine linkage. */
18794 DECL_EXTERNAL (d) = 0;
18795
18796 /* Enter the scope of D so that access-checking works correctly. */
18797 push_nested_class (DECL_CONTEXT (d));
18798 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18799 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18800 pop_nested_class ();
18801 }
18802 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18803 synthesize_method (d);
18804 else if (TREE_CODE (d) == FUNCTION_DECL)
18805 {
18806 struct pointer_map_t *saved_local_specializations;
18807 tree subst_decl;
18808 tree tmpl_parm;
18809 tree spec_parm;
18810
18811 /* Save away the current list, in case we are instantiating one
18812 template from within the body of another. */
18813 saved_local_specializations = local_specializations;
18814
18815 /* Set up the list of local specializations. */
18816 local_specializations = pointer_map_create ();
18817
18818 /* Set up context. */
18819 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18820
18821 /* Some typedefs referenced from within the template code need to be
18822 access checked at template instantiation time, i.e now. These
18823 types were added to the template at parsing time. Let's get those
18824 and perform the access checks then. */
18825 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18826 gen_args);
18827
18828 /* Create substitution entries for the parameters. */
18829 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18830 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18831 spec_parm = DECL_ARGUMENTS (d);
18832 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18833 {
18834 register_local_specialization (spec_parm, tmpl_parm);
18835 spec_parm = skip_artificial_parms_for (d, spec_parm);
18836 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18837 }
18838 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18839 {
18840 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18841 {
18842 register_local_specialization (spec_parm, tmpl_parm);
18843 spec_parm = DECL_CHAIN (spec_parm);
18844 }
18845 else
18846 {
18847 /* Register the (value) argument pack as a specialization of
18848 TMPL_PARM, then move on. */
18849 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18850 register_local_specialization (argpack, tmpl_parm);
18851 }
18852 }
18853 gcc_assert (!spec_parm);
18854
18855 /* Substitute into the body of the function. */
18856 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18857 tf_warning_or_error, tmpl,
18858 /*integral_constant_expression_p=*/false);
18859
18860 /* Set the current input_location to the end of the function
18861 so that finish_function knows where we are. */
18862 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18863
18864 /* We don't need the local specializations any more. */
18865 pointer_map_destroy (local_specializations);
18866 local_specializations = saved_local_specializations;
18867
18868 /* Finish the function. */
18869 d = finish_function (0);
18870 expand_or_defer_fn (d);
18871 }
18872
18873 /* We're not deferring instantiation any more. */
18874 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18875
18876 if (!fn_context)
18877 pop_from_top_level ();
18878 else if (nested)
18879 pop_function_context ();
18880
18881 out:
18882 input_location = saved_loc;
18883 pop_deferring_access_checks ();
18884 pop_tinst_level ();
18885
18886 timevar_pop (TV_TEMPLATE_INST);
18887
18888 return d;
18889 }
18890
18891 /* Run through the list of templates that we wish we could
18892 instantiate, and instantiate any we can. RETRIES is the
18893 number of times we retry pending template instantiation. */
18894
18895 void
18896 instantiate_pending_templates (int retries)
18897 {
18898 int reconsider;
18899 location_t saved_loc = input_location;
18900
18901 /* Instantiating templates may trigger vtable generation. This in turn
18902 may require further template instantiations. We place a limit here
18903 to avoid infinite loop. */
18904 if (pending_templates && retries >= max_tinst_depth)
18905 {
18906 tree decl = pending_templates->tinst->decl;
18907
18908 error ("template instantiation depth exceeds maximum of %d"
18909 " instantiating %q+D, possibly from virtual table generation"
18910 " (use -ftemplate-depth= to increase the maximum)",
18911 max_tinst_depth, decl);
18912 if (TREE_CODE (decl) == FUNCTION_DECL)
18913 /* Pretend that we defined it. */
18914 DECL_INITIAL (decl) = error_mark_node;
18915 return;
18916 }
18917
18918 do
18919 {
18920 struct pending_template **t = &pending_templates;
18921 struct pending_template *last = NULL;
18922 reconsider = 0;
18923 while (*t)
18924 {
18925 tree instantiation = reopen_tinst_level ((*t)->tinst);
18926 bool complete = false;
18927
18928 if (TYPE_P (instantiation))
18929 {
18930 tree fn;
18931
18932 if (!COMPLETE_TYPE_P (instantiation))
18933 {
18934 instantiate_class_template (instantiation);
18935 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18936 for (fn = TYPE_METHODS (instantiation);
18937 fn;
18938 fn = TREE_CHAIN (fn))
18939 if (! DECL_ARTIFICIAL (fn))
18940 instantiate_decl (fn,
18941 /*defer_ok=*/0,
18942 /*expl_inst_class_mem_p=*/false);
18943 if (COMPLETE_TYPE_P (instantiation))
18944 reconsider = 1;
18945 }
18946
18947 complete = COMPLETE_TYPE_P (instantiation);
18948 }
18949 else
18950 {
18951 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18952 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18953 {
18954 instantiation
18955 = instantiate_decl (instantiation,
18956 /*defer_ok=*/0,
18957 /*expl_inst_class_mem_p=*/false);
18958 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18959 reconsider = 1;
18960 }
18961
18962 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18963 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18964 }
18965
18966 if (complete)
18967 /* If INSTANTIATION has been instantiated, then we don't
18968 need to consider it again in the future. */
18969 *t = (*t)->next;
18970 else
18971 {
18972 last = *t;
18973 t = &(*t)->next;
18974 }
18975 tinst_depth = 0;
18976 current_tinst_level = NULL;
18977 }
18978 last_pending_template = last;
18979 }
18980 while (reconsider);
18981
18982 input_location = saved_loc;
18983 }
18984
18985 /* Substitute ARGVEC into T, which is a list of initializers for
18986 either base class or a non-static data member. The TREE_PURPOSEs
18987 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18988 instantiate_decl. */
18989
18990 static tree
18991 tsubst_initializer_list (tree t, tree argvec)
18992 {
18993 tree inits = NULL_TREE;
18994
18995 for (; t; t = TREE_CHAIN (t))
18996 {
18997 tree decl;
18998 tree init;
18999 tree expanded_bases = NULL_TREE;
19000 tree expanded_arguments = NULL_TREE;
19001 int i, len = 1;
19002
19003 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19004 {
19005 tree expr;
19006 tree arg;
19007
19008 /* Expand the base class expansion type into separate base
19009 classes. */
19010 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19011 tf_warning_or_error,
19012 NULL_TREE);
19013 if (expanded_bases == error_mark_node)
19014 continue;
19015
19016 /* We'll be building separate TREE_LISTs of arguments for
19017 each base. */
19018 len = TREE_VEC_LENGTH (expanded_bases);
19019 expanded_arguments = make_tree_vec (len);
19020 for (i = 0; i < len; i++)
19021 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19022
19023 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19024 expand each argument in the TREE_VALUE of t. */
19025 expr = make_node (EXPR_PACK_EXPANSION);
19026 PACK_EXPANSION_LOCAL_P (expr) = true;
19027 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19028 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19029
19030 if (TREE_VALUE (t) == void_type_node)
19031 /* VOID_TYPE_NODE is used to indicate
19032 value-initialization. */
19033 {
19034 for (i = 0; i < len; i++)
19035 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19036 }
19037 else
19038 {
19039 /* Substitute parameter packs into each argument in the
19040 TREE_LIST. */
19041 in_base_initializer = 1;
19042 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19043 {
19044 tree expanded_exprs;
19045
19046 /* Expand the argument. */
19047 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19048 expanded_exprs
19049 = tsubst_pack_expansion (expr, argvec,
19050 tf_warning_or_error,
19051 NULL_TREE);
19052 if (expanded_exprs == error_mark_node)
19053 continue;
19054
19055 /* Prepend each of the expanded expressions to the
19056 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19057 for (i = 0; i < len; i++)
19058 {
19059 TREE_VEC_ELT (expanded_arguments, i) =
19060 tree_cons (NULL_TREE,
19061 TREE_VEC_ELT (expanded_exprs, i),
19062 TREE_VEC_ELT (expanded_arguments, i));
19063 }
19064 }
19065 in_base_initializer = 0;
19066
19067 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19068 since we built them backwards. */
19069 for (i = 0; i < len; i++)
19070 {
19071 TREE_VEC_ELT (expanded_arguments, i) =
19072 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19073 }
19074 }
19075 }
19076
19077 for (i = 0; i < len; ++i)
19078 {
19079 if (expanded_bases)
19080 {
19081 decl = TREE_VEC_ELT (expanded_bases, i);
19082 decl = expand_member_init (decl);
19083 init = TREE_VEC_ELT (expanded_arguments, i);
19084 }
19085 else
19086 {
19087 tree tmp;
19088 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19089 tf_warning_or_error, NULL_TREE);
19090
19091 decl = expand_member_init (decl);
19092 if (decl && !DECL_P (decl))
19093 in_base_initializer = 1;
19094
19095 init = TREE_VALUE (t);
19096 tmp = init;
19097 if (init != void_type_node)
19098 init = tsubst_expr (init, argvec,
19099 tf_warning_or_error, NULL_TREE,
19100 /*integral_constant_expression_p=*/false);
19101 if (init == NULL_TREE && tmp != NULL_TREE)
19102 /* If we had an initializer but it instantiated to nothing,
19103 value-initialize the object. This will only occur when
19104 the initializer was a pack expansion where the parameter
19105 packs used in that expansion were of length zero. */
19106 init = void_type_node;
19107 in_base_initializer = 0;
19108 }
19109
19110 if (decl)
19111 {
19112 init = build_tree_list (decl, init);
19113 TREE_CHAIN (init) = inits;
19114 inits = init;
19115 }
19116 }
19117 }
19118 return inits;
19119 }
19120
19121 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19122
19123 static void
19124 set_current_access_from_decl (tree decl)
19125 {
19126 if (TREE_PRIVATE (decl))
19127 current_access_specifier = access_private_node;
19128 else if (TREE_PROTECTED (decl))
19129 current_access_specifier = access_protected_node;
19130 else
19131 current_access_specifier = access_public_node;
19132 }
19133
19134 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19135 is the instantiation (which should have been created with
19136 start_enum) and ARGS are the template arguments to use. */
19137
19138 static void
19139 tsubst_enum (tree tag, tree newtag, tree args)
19140 {
19141 tree e;
19142
19143 if (SCOPED_ENUM_P (newtag))
19144 begin_scope (sk_scoped_enum, newtag);
19145
19146 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19147 {
19148 tree value;
19149 tree decl;
19150
19151 decl = TREE_VALUE (e);
19152 /* Note that in a template enum, the TREE_VALUE is the
19153 CONST_DECL, not the corresponding INTEGER_CST. */
19154 value = tsubst_expr (DECL_INITIAL (decl),
19155 args, tf_warning_or_error, NULL_TREE,
19156 /*integral_constant_expression_p=*/true);
19157
19158 /* Give this enumeration constant the correct access. */
19159 set_current_access_from_decl (decl);
19160
19161 /* Actually build the enumerator itself. */
19162 build_enumerator
19163 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19164 }
19165
19166 if (SCOPED_ENUM_P (newtag))
19167 finish_scope ();
19168
19169 finish_enum_value_list (newtag);
19170 finish_enum (newtag);
19171
19172 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19173 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19174 }
19175
19176 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19177 its type -- but without substituting the innermost set of template
19178 arguments. So, innermost set of template parameters will appear in
19179 the type. */
19180
19181 tree
19182 get_mostly_instantiated_function_type (tree decl)
19183 {
19184 tree fn_type;
19185 tree tmpl;
19186 tree targs;
19187 tree tparms;
19188 int parm_depth;
19189
19190 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19191 targs = DECL_TI_ARGS (decl);
19192 tparms = DECL_TEMPLATE_PARMS (tmpl);
19193 parm_depth = TMPL_PARMS_DEPTH (tparms);
19194
19195 /* There should be as many levels of arguments as there are levels
19196 of parameters. */
19197 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19198
19199 fn_type = TREE_TYPE (tmpl);
19200
19201 if (parm_depth == 1)
19202 /* No substitution is necessary. */
19203 ;
19204 else
19205 {
19206 int i;
19207 tree partial_args;
19208
19209 /* Replace the innermost level of the TARGS with NULL_TREEs to
19210 let tsubst know not to substitute for those parameters. */
19211 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19212 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19213 SET_TMPL_ARGS_LEVEL (partial_args, i,
19214 TMPL_ARGS_LEVEL (targs, i));
19215 SET_TMPL_ARGS_LEVEL (partial_args,
19216 TMPL_ARGS_DEPTH (targs),
19217 make_tree_vec (DECL_NTPARMS (tmpl)));
19218
19219 /* Make sure that we can see identifiers, and compute access
19220 correctly. */
19221 push_access_scope (decl);
19222
19223 ++processing_template_decl;
19224 /* Now, do the (partial) substitution to figure out the
19225 appropriate function type. */
19226 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
19227 --processing_template_decl;
19228
19229 /* Substitute into the template parameters to obtain the real
19230 innermost set of parameters. This step is important if the
19231 innermost set of template parameters contains value
19232 parameters whose types depend on outer template parameters. */
19233 TREE_VEC_LENGTH (partial_args)--;
19234 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
19235
19236 pop_access_scope (decl);
19237 }
19238
19239 return fn_type;
19240 }
19241
19242 /* Return truthvalue if we're processing a template different from
19243 the last one involved in diagnostics. */
19244 int
19245 problematic_instantiation_changed (void)
19246 {
19247 return current_tinst_level != last_error_tinst_level;
19248 }
19249
19250 /* Remember current template involved in diagnostics. */
19251 void
19252 record_last_problematic_instantiation (void)
19253 {
19254 last_error_tinst_level = current_tinst_level;
19255 }
19256
19257 struct tinst_level *
19258 current_instantiation (void)
19259 {
19260 return current_tinst_level;
19261 }
19262
19263 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19264 type. Return zero for ok, nonzero for disallowed. Issue error and
19265 warning messages under control of COMPLAIN. */
19266
19267 static int
19268 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
19269 {
19270 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
19271 return 0;
19272 else if (POINTER_TYPE_P (type))
19273 return 0;
19274 else if (TYPE_PTRMEM_P (type))
19275 return 0;
19276 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
19277 return 0;
19278 else if (TREE_CODE (type) == TYPENAME_TYPE)
19279 return 0;
19280 else if (TREE_CODE (type) == DECLTYPE_TYPE)
19281 return 0;
19282 else if (TREE_CODE (type) == NULLPTR_TYPE)
19283 return 0;
19284
19285 if (complain & tf_error)
19286 {
19287 if (type == error_mark_node)
19288 inform (input_location, "invalid template non-type parameter");
19289 else
19290 error ("%q#T is not a valid type for a template non-type parameter",
19291 type);
19292 }
19293 return 1;
19294 }
19295
19296 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19297 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19298
19299 static bool
19300 dependent_type_p_r (tree type)
19301 {
19302 tree scope;
19303
19304 /* [temp.dep.type]
19305
19306 A type is dependent if it is:
19307
19308 -- a template parameter. Template template parameters are types
19309 for us (since TYPE_P holds true for them) so we handle
19310 them here. */
19311 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19312 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
19313 return true;
19314 /* -- a qualified-id with a nested-name-specifier which contains a
19315 class-name that names a dependent type or whose unqualified-id
19316 names a dependent type. */
19317 if (TREE_CODE (type) == TYPENAME_TYPE)
19318 return true;
19319 /* -- a cv-qualified type where the cv-unqualified type is
19320 dependent. */
19321 type = TYPE_MAIN_VARIANT (type);
19322 /* -- a compound type constructed from any dependent type. */
19323 if (TYPE_PTRMEM_P (type))
19324 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
19325 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19326 (type)));
19327 else if (TREE_CODE (type) == POINTER_TYPE
19328 || TREE_CODE (type) == REFERENCE_TYPE)
19329 return dependent_type_p (TREE_TYPE (type));
19330 else if (TREE_CODE (type) == FUNCTION_TYPE
19331 || TREE_CODE (type) == METHOD_TYPE)
19332 {
19333 tree arg_type;
19334
19335 if (dependent_type_p (TREE_TYPE (type)))
19336 return true;
19337 for (arg_type = TYPE_ARG_TYPES (type);
19338 arg_type;
19339 arg_type = TREE_CHAIN (arg_type))
19340 if (dependent_type_p (TREE_VALUE (arg_type)))
19341 return true;
19342 return false;
19343 }
19344 /* -- an array type constructed from any dependent type or whose
19345 size is specified by a constant expression that is
19346 value-dependent.
19347
19348 We checked for type- and value-dependence of the bounds in
19349 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19350 if (TREE_CODE (type) == ARRAY_TYPE)
19351 {
19352 if (TYPE_DOMAIN (type)
19353 && dependent_type_p (TYPE_DOMAIN (type)))
19354 return true;
19355 return dependent_type_p (TREE_TYPE (type));
19356 }
19357
19358 /* -- a template-id in which either the template name is a template
19359 parameter ... */
19360 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19361 return true;
19362 /* ... or any of the template arguments is a dependent type or
19363 an expression that is type-dependent or value-dependent. */
19364 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19365 && (any_dependent_template_arguments_p
19366 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19367 return true;
19368
19369 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19370 dependent; if the argument of the `typeof' expression is not
19371 type-dependent, then it should already been have resolved. */
19372 if (TREE_CODE (type) == TYPEOF_TYPE
19373 || TREE_CODE (type) == DECLTYPE_TYPE
19374 || TREE_CODE (type) == UNDERLYING_TYPE)
19375 return true;
19376
19377 /* A template argument pack is dependent if any of its packed
19378 arguments are. */
19379 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19380 {
19381 tree args = ARGUMENT_PACK_ARGS (type);
19382 int i, len = TREE_VEC_LENGTH (args);
19383 for (i = 0; i < len; ++i)
19384 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19385 return true;
19386 }
19387
19388 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19389 be template parameters. */
19390 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19391 return true;
19392
19393 /* The standard does not specifically mention types that are local
19394 to template functions or local classes, but they should be
19395 considered dependent too. For example:
19396
19397 template <int I> void f() {
19398 enum E { a = I };
19399 S<sizeof (E)> s;
19400 }
19401
19402 The size of `E' cannot be known until the value of `I' has been
19403 determined. Therefore, `E' must be considered dependent. */
19404 scope = TYPE_CONTEXT (type);
19405 if (scope && TYPE_P (scope))
19406 return dependent_type_p (scope);
19407 /* Don't use type_dependent_expression_p here, as it can lead
19408 to infinite recursion trying to determine whether a lambda
19409 nested in a lambda is dependent (c++/47687). */
19410 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19411 && DECL_LANG_SPECIFIC (scope)
19412 && DECL_TEMPLATE_INFO (scope)
19413 && (any_dependent_template_arguments_p
19414 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19415 return true;
19416
19417 /* Other types are non-dependent. */
19418 return false;
19419 }
19420
19421 /* Returns TRUE if TYPE is dependent, in the sense of
19422 [temp.dep.type]. Note that a NULL type is considered dependent. */
19423
19424 bool
19425 dependent_type_p (tree type)
19426 {
19427 /* If there are no template parameters in scope, then there can't be
19428 any dependent types. */
19429 if (!processing_template_decl)
19430 {
19431 /* If we are not processing a template, then nobody should be
19432 providing us with a dependent type. */
19433 gcc_assert (type);
19434 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19435 return false;
19436 }
19437
19438 /* If the type is NULL, we have not computed a type for the entity
19439 in question; in that case, the type is dependent. */
19440 if (!type)
19441 return true;
19442
19443 /* Erroneous types can be considered non-dependent. */
19444 if (type == error_mark_node)
19445 return false;
19446
19447 /* If we have not already computed the appropriate value for TYPE,
19448 do so now. */
19449 if (!TYPE_DEPENDENT_P_VALID (type))
19450 {
19451 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19452 TYPE_DEPENDENT_P_VALID (type) = 1;
19453 }
19454
19455 return TYPE_DEPENDENT_P (type);
19456 }
19457
19458 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19459 lookup. In other words, a dependent type that is not the current
19460 instantiation. */
19461
19462 bool
19463 dependent_scope_p (tree scope)
19464 {
19465 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19466 && !currently_open_class (scope));
19467 }
19468
19469 /* T is a SCOPE_REF; return whether we need to consider it
19470 instantiation-dependent so that we can check access at instantiation
19471 time even though we know which member it resolves to. */
19472
19473 static bool
19474 instantiation_dependent_scope_ref_p (tree t)
19475 {
19476 if (DECL_P (TREE_OPERAND (t, 1))
19477 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
19478 && accessible_in_template_p (TREE_OPERAND (t, 0),
19479 TREE_OPERAND (t, 1)))
19480 return false;
19481 else
19482 return true;
19483 }
19484
19485 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19486 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19487 expression. */
19488
19489 /* Note that this predicate is not appropriate for general expressions;
19490 only constant expressions (that satisfy potential_constant_expression)
19491 can be tested for value dependence. */
19492
19493 bool
19494 value_dependent_expression_p (tree expression)
19495 {
19496 if (!processing_template_decl)
19497 return false;
19498
19499 /* A name declared with a dependent type. */
19500 if (DECL_P (expression) && type_dependent_expression_p (expression))
19501 return true;
19502
19503 switch (TREE_CODE (expression))
19504 {
19505 case IDENTIFIER_NODE:
19506 /* A name that has not been looked up -- must be dependent. */
19507 return true;
19508
19509 case TEMPLATE_PARM_INDEX:
19510 /* A non-type template parm. */
19511 return true;
19512
19513 case CONST_DECL:
19514 /* A non-type template parm. */
19515 if (DECL_TEMPLATE_PARM_P (expression))
19516 return true;
19517 return value_dependent_expression_p (DECL_INITIAL (expression));
19518
19519 case VAR_DECL:
19520 /* A constant with literal type and is initialized
19521 with an expression that is value-dependent.
19522
19523 Note that a non-dependent parenthesized initializer will have
19524 already been replaced with its constant value, so if we see
19525 a TREE_LIST it must be dependent. */
19526 if (DECL_INITIAL (expression)
19527 && decl_constant_var_p (expression)
19528 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
19529 || value_dependent_expression_p (DECL_INITIAL (expression))))
19530 return true;
19531 return false;
19532
19533 case DYNAMIC_CAST_EXPR:
19534 case STATIC_CAST_EXPR:
19535 case CONST_CAST_EXPR:
19536 case REINTERPRET_CAST_EXPR:
19537 case CAST_EXPR:
19538 /* These expressions are value-dependent if the type to which
19539 the cast occurs is dependent or the expression being casted
19540 is value-dependent. */
19541 {
19542 tree type = TREE_TYPE (expression);
19543
19544 if (dependent_type_p (type))
19545 return true;
19546
19547 /* A functional cast has a list of operands. */
19548 expression = TREE_OPERAND (expression, 0);
19549 if (!expression)
19550 {
19551 /* If there are no operands, it must be an expression such
19552 as "int()". This should not happen for aggregate types
19553 because it would form non-constant expressions. */
19554 gcc_assert (cxx_dialect >= cxx0x
19555 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19556
19557 return false;
19558 }
19559
19560 if (TREE_CODE (expression) == TREE_LIST)
19561 return any_value_dependent_elements_p (expression);
19562
19563 return value_dependent_expression_p (expression);
19564 }
19565
19566 case SIZEOF_EXPR:
19567 if (SIZEOF_EXPR_TYPE_P (expression))
19568 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
19569 /* FALLTHRU */
19570 case ALIGNOF_EXPR:
19571 case TYPEID_EXPR:
19572 /* A `sizeof' expression is value-dependent if the operand is
19573 type-dependent or is a pack expansion. */
19574 expression = TREE_OPERAND (expression, 0);
19575 if (PACK_EXPANSION_P (expression))
19576 return true;
19577 else if (TYPE_P (expression))
19578 return dependent_type_p (expression);
19579 return instantiation_dependent_expression_p (expression);
19580
19581 case AT_ENCODE_EXPR:
19582 /* An 'encode' expression is value-dependent if the operand is
19583 type-dependent. */
19584 expression = TREE_OPERAND (expression, 0);
19585 return dependent_type_p (expression);
19586
19587 case NOEXCEPT_EXPR:
19588 expression = TREE_OPERAND (expression, 0);
19589 return instantiation_dependent_expression_p (expression);
19590
19591 case SCOPE_REF:
19592 /* All instantiation-dependent expressions should also be considered
19593 value-dependent. */
19594 return instantiation_dependent_scope_ref_p (expression);
19595
19596 case COMPONENT_REF:
19597 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19598 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19599
19600 case NONTYPE_ARGUMENT_PACK:
19601 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19602 is value-dependent. */
19603 {
19604 tree values = ARGUMENT_PACK_ARGS (expression);
19605 int i, len = TREE_VEC_LENGTH (values);
19606
19607 for (i = 0; i < len; ++i)
19608 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19609 return true;
19610
19611 return false;
19612 }
19613
19614 case TRAIT_EXPR:
19615 {
19616 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19617 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19618 || (type2 ? dependent_type_p (type2) : false));
19619 }
19620
19621 case MODOP_EXPR:
19622 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19623 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19624
19625 case ARRAY_REF:
19626 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19627 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19628
19629 case ADDR_EXPR:
19630 {
19631 tree op = TREE_OPERAND (expression, 0);
19632 return (value_dependent_expression_p (op)
19633 || has_value_dependent_address (op));
19634 }
19635
19636 case CALL_EXPR:
19637 {
19638 tree fn = get_callee_fndecl (expression);
19639 int i, nargs;
19640 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19641 return true;
19642 nargs = call_expr_nargs (expression);
19643 for (i = 0; i < nargs; ++i)
19644 {
19645 tree op = CALL_EXPR_ARG (expression, i);
19646 /* In a call to a constexpr member function, look through the
19647 implicit ADDR_EXPR on the object argument so that it doesn't
19648 cause the call to be considered value-dependent. We also
19649 look through it in potential_constant_expression. */
19650 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19651 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19652 && TREE_CODE (op) == ADDR_EXPR)
19653 op = TREE_OPERAND (op, 0);
19654 if (value_dependent_expression_p (op))
19655 return true;
19656 }
19657 return false;
19658 }
19659
19660 case TEMPLATE_ID_EXPR:
19661 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19662 type-dependent. */
19663 return type_dependent_expression_p (expression);
19664
19665 case CONSTRUCTOR:
19666 {
19667 unsigned ix;
19668 tree val;
19669 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19670 if (value_dependent_expression_p (val))
19671 return true;
19672 return false;
19673 }
19674
19675 case STMT_EXPR:
19676 /* Treat a GNU statement expression as dependent to avoid crashing
19677 under fold_non_dependent_expr; it can't be constant. */
19678 return true;
19679
19680 default:
19681 /* A constant expression is value-dependent if any subexpression is
19682 value-dependent. */
19683 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19684 {
19685 case tcc_reference:
19686 case tcc_unary:
19687 case tcc_comparison:
19688 case tcc_binary:
19689 case tcc_expression:
19690 case tcc_vl_exp:
19691 {
19692 int i, len = cp_tree_operand_length (expression);
19693
19694 for (i = 0; i < len; i++)
19695 {
19696 tree t = TREE_OPERAND (expression, i);
19697
19698 /* In some cases, some of the operands may be missing.l
19699 (For example, in the case of PREDECREMENT_EXPR, the
19700 amount to increment by may be missing.) That doesn't
19701 make the expression dependent. */
19702 if (t && value_dependent_expression_p (t))
19703 return true;
19704 }
19705 }
19706 break;
19707 default:
19708 break;
19709 }
19710 break;
19711 }
19712
19713 /* The expression is not value-dependent. */
19714 return false;
19715 }
19716
19717 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19718 [temp.dep.expr]. Note that an expression with no type is
19719 considered dependent. Other parts of the compiler arrange for an
19720 expression with type-dependent subexpressions to have no type, so
19721 this function doesn't have to be fully recursive. */
19722
19723 bool
19724 type_dependent_expression_p (tree expression)
19725 {
19726 if (!processing_template_decl)
19727 return false;
19728
19729 if (expression == error_mark_node)
19730 return false;
19731
19732 /* An unresolved name is always dependent. */
19733 if (TREE_CODE (expression) == IDENTIFIER_NODE
19734 || TREE_CODE (expression) == USING_DECL)
19735 return true;
19736
19737 /* Some expression forms are never type-dependent. */
19738 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19739 || TREE_CODE (expression) == SIZEOF_EXPR
19740 || TREE_CODE (expression) == ALIGNOF_EXPR
19741 || TREE_CODE (expression) == AT_ENCODE_EXPR
19742 || TREE_CODE (expression) == NOEXCEPT_EXPR
19743 || TREE_CODE (expression) == TRAIT_EXPR
19744 || TREE_CODE (expression) == TYPEID_EXPR
19745 || TREE_CODE (expression) == DELETE_EXPR
19746 || TREE_CODE (expression) == VEC_DELETE_EXPR
19747 || TREE_CODE (expression) == THROW_EXPR)
19748 return false;
19749
19750 /* The types of these expressions depends only on the type to which
19751 the cast occurs. */
19752 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19753 || TREE_CODE (expression) == STATIC_CAST_EXPR
19754 || TREE_CODE (expression) == CONST_CAST_EXPR
19755 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19756 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19757 || TREE_CODE (expression) == CAST_EXPR)
19758 return dependent_type_p (TREE_TYPE (expression));
19759
19760 /* The types of these expressions depends only on the type created
19761 by the expression. */
19762 if (TREE_CODE (expression) == NEW_EXPR
19763 || TREE_CODE (expression) == VEC_NEW_EXPR)
19764 {
19765 /* For NEW_EXPR tree nodes created inside a template, either
19766 the object type itself or a TREE_LIST may appear as the
19767 operand 1. */
19768 tree type = TREE_OPERAND (expression, 1);
19769 if (TREE_CODE (type) == TREE_LIST)
19770 /* This is an array type. We need to check array dimensions
19771 as well. */
19772 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19773 || value_dependent_expression_p
19774 (TREE_OPERAND (TREE_VALUE (type), 1));
19775 else
19776 return dependent_type_p (type);
19777 }
19778
19779 if (TREE_CODE (expression) == SCOPE_REF)
19780 {
19781 tree scope = TREE_OPERAND (expression, 0);
19782 tree name = TREE_OPERAND (expression, 1);
19783
19784 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19785 contains an identifier associated by name lookup with one or more
19786 declarations declared with a dependent type, or...a
19787 nested-name-specifier or qualified-id that names a member of an
19788 unknown specialization. */
19789 return (type_dependent_expression_p (name)
19790 || dependent_scope_p (scope));
19791 }
19792
19793 if (TREE_CODE (expression) == FUNCTION_DECL
19794 && DECL_LANG_SPECIFIC (expression)
19795 && DECL_TEMPLATE_INFO (expression)
19796 && (any_dependent_template_arguments_p
19797 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19798 return true;
19799
19800 if (TREE_CODE (expression) == TEMPLATE_DECL
19801 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19802 return false;
19803
19804 if (TREE_CODE (expression) == STMT_EXPR)
19805 expression = stmt_expr_value_expr (expression);
19806
19807 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19808 {
19809 tree elt;
19810 unsigned i;
19811
19812 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19813 {
19814 if (type_dependent_expression_p (elt))
19815 return true;
19816 }
19817 return false;
19818 }
19819
19820 /* A static data member of the current instantiation with incomplete
19821 array type is type-dependent, as the definition and specializations
19822 can have different bounds. */
19823 if (TREE_CODE (expression) == VAR_DECL
19824 && DECL_CLASS_SCOPE_P (expression)
19825 && dependent_type_p (DECL_CONTEXT (expression))
19826 && VAR_HAD_UNKNOWN_BOUND (expression))
19827 return true;
19828
19829 if (TREE_TYPE (expression) == unknown_type_node)
19830 {
19831 if (TREE_CODE (expression) == ADDR_EXPR)
19832 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19833 if (TREE_CODE (expression) == COMPONENT_REF
19834 || TREE_CODE (expression) == OFFSET_REF)
19835 {
19836 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19837 return true;
19838 expression = TREE_OPERAND (expression, 1);
19839 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19840 return false;
19841 }
19842 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19843 if (TREE_CODE (expression) == SCOPE_REF)
19844 return false;
19845
19846 if (BASELINK_P (expression))
19847 expression = BASELINK_FUNCTIONS (expression);
19848
19849 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19850 {
19851 if (any_dependent_template_arguments_p
19852 (TREE_OPERAND (expression, 1)))
19853 return true;
19854 expression = TREE_OPERAND (expression, 0);
19855 }
19856 gcc_assert (TREE_CODE (expression) == OVERLOAD
19857 || TREE_CODE (expression) == FUNCTION_DECL);
19858
19859 while (expression)
19860 {
19861 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19862 return true;
19863 expression = OVL_NEXT (expression);
19864 }
19865 return false;
19866 }
19867
19868 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19869
19870 return (dependent_type_p (TREE_TYPE (expression)));
19871 }
19872
19873 /* walk_tree callback function for instantiation_dependent_expression_p,
19874 below. Returns non-zero if a dependent subexpression is found. */
19875
19876 static tree
19877 instantiation_dependent_r (tree *tp, int *walk_subtrees,
19878 void * /*data*/)
19879 {
19880 if (TYPE_P (*tp))
19881 {
19882 /* We don't have to worry about decltype currently because decltype
19883 of an instantiation-dependent expr is a dependent type. This
19884 might change depending on the resolution of DR 1172. */
19885 *walk_subtrees = false;
19886 return NULL_TREE;
19887 }
19888 enum tree_code code = TREE_CODE (*tp);
19889 switch (code)
19890 {
19891 /* Don't treat an argument list as dependent just because it has no
19892 TREE_TYPE. */
19893 case TREE_LIST:
19894 case TREE_VEC:
19895 return NULL_TREE;
19896
19897 case TEMPLATE_PARM_INDEX:
19898 return *tp;
19899
19900 /* Handle expressions with type operands. */
19901 case SIZEOF_EXPR:
19902 case ALIGNOF_EXPR:
19903 case TYPEID_EXPR:
19904 case AT_ENCODE_EXPR:
19905 {
19906 tree op = TREE_OPERAND (*tp, 0);
19907 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
19908 op = TREE_TYPE (op);
19909 if (TYPE_P (op))
19910 {
19911 if (dependent_type_p (op))
19912 return *tp;
19913 else
19914 {
19915 *walk_subtrees = false;
19916 return NULL_TREE;
19917 }
19918 }
19919 break;
19920 }
19921
19922 case TRAIT_EXPR:
19923 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
19924 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp)))
19925 return *tp;
19926 *walk_subtrees = false;
19927 return NULL_TREE;
19928
19929 case COMPONENT_REF:
19930 if (TREE_CODE (TREE_OPERAND (*tp, 1)) == IDENTIFIER_NODE)
19931 /* In a template, finish_class_member_access_expr creates a
19932 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19933 type-dependent, so that we can check access control at
19934 instantiation time (PR 42277). See also Core issue 1273. */
19935 return *tp;
19936 break;
19937
19938 case SCOPE_REF:
19939 if (instantiation_dependent_scope_ref_p (*tp))
19940 return *tp;
19941 else
19942 break;
19943
19944 /* Treat statement-expressions as dependent. */
19945 case BIND_EXPR:
19946 return *tp;
19947
19948 default:
19949 break;
19950 }
19951
19952 if (type_dependent_expression_p (*tp))
19953 return *tp;
19954 else
19955 return NULL_TREE;
19956 }
19957
19958 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19959 sense defined by the ABI:
19960
19961 "An expression is instantiation-dependent if it is type-dependent
19962 or value-dependent, or it has a subexpression that is type-dependent
19963 or value-dependent." */
19964
19965 bool
19966 instantiation_dependent_expression_p (tree expression)
19967 {
19968 tree result;
19969
19970 if (!processing_template_decl)
19971 return false;
19972
19973 if (expression == error_mark_node)
19974 return false;
19975
19976 result = cp_walk_tree_without_duplicates (&expression,
19977 instantiation_dependent_r, NULL);
19978 return result != NULL_TREE;
19979 }
19980
19981 /* Like type_dependent_expression_p, but it also works while not processing
19982 a template definition, i.e. during substitution or mangling. */
19983
19984 bool
19985 type_dependent_expression_p_push (tree expr)
19986 {
19987 bool b;
19988 ++processing_template_decl;
19989 b = type_dependent_expression_p (expr);
19990 --processing_template_decl;
19991 return b;
19992 }
19993
19994 /* Returns TRUE if ARGS contains a type-dependent expression. */
19995
19996 bool
19997 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
19998 {
19999 unsigned int i;
20000 tree arg;
20001
20002 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20003 {
20004 if (type_dependent_expression_p (arg))
20005 return true;
20006 }
20007 return false;
20008 }
20009
20010 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20011 expressions) contains any type-dependent expressions. */
20012
20013 bool
20014 any_type_dependent_elements_p (const_tree list)
20015 {
20016 for (; list; list = TREE_CHAIN (list))
20017 if (value_dependent_expression_p (TREE_VALUE (list)))
20018 return true;
20019
20020 return false;
20021 }
20022
20023 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20024 expressions) contains any value-dependent expressions. */
20025
20026 bool
20027 any_value_dependent_elements_p (const_tree list)
20028 {
20029 for (; list; list = TREE_CHAIN (list))
20030 if (value_dependent_expression_p (TREE_VALUE (list)))
20031 return true;
20032
20033 return false;
20034 }
20035
20036 /* Returns TRUE if the ARG (a template argument) is dependent. */
20037
20038 bool
20039 dependent_template_arg_p (tree arg)
20040 {
20041 if (!processing_template_decl)
20042 return false;
20043
20044 /* Assume a template argument that was wrongly written by the user
20045 is dependent. This is consistent with what
20046 any_dependent_template_arguments_p [that calls this function]
20047 does. */
20048 if (!arg || arg == error_mark_node)
20049 return true;
20050
20051 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20052 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20053
20054 if (TREE_CODE (arg) == TEMPLATE_DECL
20055 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20056 return dependent_template_p (arg);
20057 else if (ARGUMENT_PACK_P (arg))
20058 {
20059 tree args = ARGUMENT_PACK_ARGS (arg);
20060 int i, len = TREE_VEC_LENGTH (args);
20061 for (i = 0; i < len; ++i)
20062 {
20063 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20064 return true;
20065 }
20066
20067 return false;
20068 }
20069 else if (TYPE_P (arg))
20070 return dependent_type_p (arg);
20071 else
20072 return (type_dependent_expression_p (arg)
20073 || value_dependent_expression_p (arg));
20074 }
20075
20076 /* Returns true if ARGS (a collection of template arguments) contains
20077 any types that require structural equality testing. */
20078
20079 bool
20080 any_template_arguments_need_structural_equality_p (tree args)
20081 {
20082 int i;
20083 int j;
20084
20085 if (!args)
20086 return false;
20087 if (args == error_mark_node)
20088 return true;
20089
20090 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20091 {
20092 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20093 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20094 {
20095 tree arg = TREE_VEC_ELT (level, j);
20096 tree packed_args = NULL_TREE;
20097 int k, len = 1;
20098
20099 if (ARGUMENT_PACK_P (arg))
20100 {
20101 /* Look inside the argument pack. */
20102 packed_args = ARGUMENT_PACK_ARGS (arg);
20103 len = TREE_VEC_LENGTH (packed_args);
20104 }
20105
20106 for (k = 0; k < len; ++k)
20107 {
20108 if (packed_args)
20109 arg = TREE_VEC_ELT (packed_args, k);
20110
20111 if (error_operand_p (arg))
20112 return true;
20113 else if (TREE_CODE (arg) == TEMPLATE_DECL
20114 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20115 continue;
20116 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20117 return true;
20118 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20119 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20120 return true;
20121 }
20122 }
20123 }
20124
20125 return false;
20126 }
20127
20128 /* Returns true if ARGS (a collection of template arguments) contains
20129 any dependent arguments. */
20130
20131 bool
20132 any_dependent_template_arguments_p (const_tree args)
20133 {
20134 int i;
20135 int j;
20136
20137 if (!args)
20138 return false;
20139 if (args == error_mark_node)
20140 return true;
20141
20142 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20143 {
20144 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20145 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20146 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20147 return true;
20148 }
20149
20150 return false;
20151 }
20152
20153 /* Returns TRUE if the template TMPL is dependent. */
20154
20155 bool
20156 dependent_template_p (tree tmpl)
20157 {
20158 if (TREE_CODE (tmpl) == OVERLOAD)
20159 {
20160 while (tmpl)
20161 {
20162 if (dependent_template_p (OVL_CURRENT (tmpl)))
20163 return true;
20164 tmpl = OVL_NEXT (tmpl);
20165 }
20166 return false;
20167 }
20168
20169 /* Template template parameters are dependent. */
20170 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20171 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20172 return true;
20173 /* So are names that have not been looked up. */
20174 if (TREE_CODE (tmpl) == SCOPE_REF
20175 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
20176 return true;
20177 /* So are member templates of dependent classes. */
20178 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20179 return dependent_type_p (DECL_CONTEXT (tmpl));
20180 return false;
20181 }
20182
20183 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20184
20185 bool
20186 dependent_template_id_p (tree tmpl, tree args)
20187 {
20188 return (dependent_template_p (tmpl)
20189 || any_dependent_template_arguments_p (args));
20190 }
20191
20192 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20193 is dependent. */
20194
20195 bool
20196 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
20197 {
20198 int i;
20199
20200 if (!processing_template_decl)
20201 return false;
20202
20203 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
20204 {
20205 tree decl = TREE_VEC_ELT (declv, i);
20206 tree init = TREE_VEC_ELT (initv, i);
20207 tree cond = TREE_VEC_ELT (condv, i);
20208 tree incr = TREE_VEC_ELT (incrv, i);
20209
20210 if (type_dependent_expression_p (decl))
20211 return true;
20212
20213 if (init && type_dependent_expression_p (init))
20214 return true;
20215
20216 if (type_dependent_expression_p (cond))
20217 return true;
20218
20219 if (COMPARISON_CLASS_P (cond)
20220 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
20221 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
20222 return true;
20223
20224 if (TREE_CODE (incr) == MODOP_EXPR)
20225 {
20226 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
20227 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
20228 return true;
20229 }
20230 else if (type_dependent_expression_p (incr))
20231 return true;
20232 else if (TREE_CODE (incr) == MODIFY_EXPR)
20233 {
20234 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
20235 return true;
20236 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
20237 {
20238 tree t = TREE_OPERAND (incr, 1);
20239 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
20240 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
20241 return true;
20242 }
20243 }
20244 }
20245
20246 return false;
20247 }
20248
20249 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20250 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20251 no such TYPE can be found. Note that this function peers inside
20252 uninstantiated templates and therefore should be used only in
20253 extremely limited situations. ONLY_CURRENT_P restricts this
20254 peering to the currently open classes hierarchy (which is required
20255 when comparing types). */
20256
20257 tree
20258 resolve_typename_type (tree type, bool only_current_p)
20259 {
20260 tree scope;
20261 tree name;
20262 tree decl;
20263 int quals;
20264 tree pushed_scope;
20265 tree result;
20266
20267 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
20268
20269 scope = TYPE_CONTEXT (type);
20270 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20271 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20272 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20273 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20274 identifier of the TYPENAME_TYPE anymore.
20275 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20276 TYPENAME_TYPE instead, we avoid messing up with a possible
20277 typedef variant case. */
20278 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
20279
20280 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20281 it first before we can figure out what NAME refers to. */
20282 if (TREE_CODE (scope) == TYPENAME_TYPE)
20283 {
20284 if (TYPENAME_IS_RESOLVING_P (scope))
20285 /* Given a class template A with a dependent base with nested type C,
20286 typedef typename A::C::C C will land us here, as trying to resolve
20287 the initial A::C leads to the local C typedef, which leads back to
20288 A::C::C. So we break the recursion now. */
20289 return type;
20290 else
20291 scope = resolve_typename_type (scope, only_current_p);
20292 }
20293 /* If we don't know what SCOPE refers to, then we cannot resolve the
20294 TYPENAME_TYPE. */
20295 if (TREE_CODE (scope) == TYPENAME_TYPE)
20296 return type;
20297 /* If the SCOPE is a template type parameter, we have no way of
20298 resolving the name. */
20299 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
20300 return type;
20301 /* If the SCOPE is not the current instantiation, there's no reason
20302 to look inside it. */
20303 if (only_current_p && !currently_open_class (scope))
20304 return type;
20305 /* If this is a typedef, we don't want to look inside (c++/11987). */
20306 if (typedef_variant_p (type))
20307 return type;
20308 /* If SCOPE isn't the template itself, it will not have a valid
20309 TYPE_FIELDS list. */
20310 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
20311 /* scope is either the template itself or a compatible instantiation
20312 like X<T>, so look up the name in the original template. */
20313 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
20314 else
20315 /* scope is a partial instantiation, so we can't do the lookup or we
20316 will lose the template arguments. */
20317 return type;
20318 /* Enter the SCOPE so that name lookup will be resolved as if we
20319 were in the class definition. In particular, SCOPE will no
20320 longer be considered a dependent type. */
20321 pushed_scope = push_scope (scope);
20322 /* Look up the declaration. */
20323 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
20324 tf_warning_or_error);
20325
20326 result = NULL_TREE;
20327
20328 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20329 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20330 if (!decl)
20331 /*nop*/;
20332 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
20333 && TREE_CODE (decl) == TYPE_DECL)
20334 {
20335 result = TREE_TYPE (decl);
20336 if (result == error_mark_node)
20337 result = NULL_TREE;
20338 }
20339 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
20340 && DECL_CLASS_TEMPLATE_P (decl))
20341 {
20342 tree tmpl;
20343 tree args;
20344 /* Obtain the template and the arguments. */
20345 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
20346 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
20347 /* Instantiate the template. */
20348 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
20349 /*entering_scope=*/0,
20350 tf_error | tf_user);
20351 if (result == error_mark_node)
20352 result = NULL_TREE;
20353 }
20354
20355 /* Leave the SCOPE. */
20356 if (pushed_scope)
20357 pop_scope (pushed_scope);
20358
20359 /* If we failed to resolve it, return the original typename. */
20360 if (!result)
20361 return type;
20362
20363 /* If lookup found a typename type, resolve that too. */
20364 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
20365 {
20366 /* Ill-formed programs can cause infinite recursion here, so we
20367 must catch that. */
20368 TYPENAME_IS_RESOLVING_P (type) = 1;
20369 result = resolve_typename_type (result, only_current_p);
20370 TYPENAME_IS_RESOLVING_P (type) = 0;
20371 }
20372
20373 /* Qualify the resulting type. */
20374 quals = cp_type_quals (type);
20375 if (quals)
20376 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
20377
20378 return result;
20379 }
20380
20381 /* EXPR is an expression which is not type-dependent. Return a proxy
20382 for EXPR that can be used to compute the types of larger
20383 expressions containing EXPR. */
20384
20385 tree
20386 build_non_dependent_expr (tree expr)
20387 {
20388 tree inner_expr;
20389
20390 #ifdef ENABLE_CHECKING
20391 /* Try to get a constant value for all non-dependent expressions in
20392 order to expose bugs in *_dependent_expression_p and constexpr. */
20393 if (cxx_dialect >= cxx0x
20394 && !instantiation_dependent_expression_p (expr))
20395 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
20396 #endif
20397
20398 /* Preserve OVERLOADs; the functions must be available to resolve
20399 types. */
20400 inner_expr = expr;
20401 if (TREE_CODE (inner_expr) == STMT_EXPR)
20402 inner_expr = stmt_expr_value_expr (inner_expr);
20403 if (TREE_CODE (inner_expr) == ADDR_EXPR)
20404 inner_expr = TREE_OPERAND (inner_expr, 0);
20405 if (TREE_CODE (inner_expr) == COMPONENT_REF)
20406 inner_expr = TREE_OPERAND (inner_expr, 1);
20407 if (is_overloaded_fn (inner_expr)
20408 || TREE_CODE (inner_expr) == OFFSET_REF)
20409 return expr;
20410 /* There is no need to return a proxy for a variable. */
20411 if (TREE_CODE (expr) == VAR_DECL)
20412 return expr;
20413 /* Preserve string constants; conversions from string constants to
20414 "char *" are allowed, even though normally a "const char *"
20415 cannot be used to initialize a "char *". */
20416 if (TREE_CODE (expr) == STRING_CST)
20417 return expr;
20418 /* Preserve arithmetic constants, as an optimization -- there is no
20419 reason to create a new node. */
20420 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
20421 return expr;
20422 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20423 There is at least one place where we want to know that a
20424 particular expression is a throw-expression: when checking a ?:
20425 expression, there are special rules if the second or third
20426 argument is a throw-expression. */
20427 if (TREE_CODE (expr) == THROW_EXPR)
20428 return expr;
20429
20430 /* Don't wrap an initializer list, we need to be able to look inside. */
20431 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
20432 return expr;
20433
20434 /* Don't wrap a dummy object, we need to be able to test for it. */
20435 if (is_dummy_object (expr))
20436 return expr;
20437
20438 if (TREE_CODE (expr) == COND_EXPR)
20439 return build3 (COND_EXPR,
20440 TREE_TYPE (expr),
20441 TREE_OPERAND (expr, 0),
20442 (TREE_OPERAND (expr, 1)
20443 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
20444 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
20445 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
20446 if (TREE_CODE (expr) == COMPOUND_EXPR
20447 && !COMPOUND_EXPR_OVERLOADED (expr))
20448 return build2 (COMPOUND_EXPR,
20449 TREE_TYPE (expr),
20450 TREE_OPERAND (expr, 0),
20451 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
20452
20453 /* If the type is unknown, it can't really be non-dependent */
20454 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
20455
20456 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20457 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20458 }
20459
20460 /* ARGS is a vector of expressions as arguments to a function call.
20461 Replace the arguments with equivalent non-dependent expressions.
20462 This modifies ARGS in place. */
20463
20464 void
20465 make_args_non_dependent (vec<tree, va_gc> *args)
20466 {
20467 unsigned int ix;
20468 tree arg;
20469
20470 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
20471 {
20472 tree newarg = build_non_dependent_expr (arg);
20473 if (newarg != arg)
20474 (*args)[ix] = newarg;
20475 }
20476 }
20477
20478 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20479 with a level one deeper than the actual template parms. */
20480
20481 tree
20482 make_auto (void)
20483 {
20484 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20485 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20486 TYPE_DECL, get_identifier ("auto"), au);
20487 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20488 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20489 (0, processing_template_decl + 1, processing_template_decl + 1,
20490 TYPE_NAME (au), NULL_TREE);
20491 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20492 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20493 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20494
20495 return au;
20496 }
20497
20498 /* Given type ARG, return std::initializer_list<ARG>. */
20499
20500 static tree
20501 listify (tree arg)
20502 {
20503 tree std_init_list = namespace_binding
20504 (get_identifier ("initializer_list"), std_node);
20505 tree argvec;
20506 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20507 {
20508 error ("deducing from brace-enclosed initializer list requires "
20509 "#include <initializer_list>");
20510 return error_mark_node;
20511 }
20512 argvec = make_tree_vec (1);
20513 TREE_VEC_ELT (argvec, 0) = arg;
20514 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20515 NULL_TREE, 0, tf_warning_or_error);
20516 }
20517
20518 /* Replace auto in TYPE with std::initializer_list<auto>. */
20519
20520 static tree
20521 listify_autos (tree type, tree auto_node)
20522 {
20523 tree init_auto = listify (auto_node);
20524 tree argvec = make_tree_vec (1);
20525 TREE_VEC_ELT (argvec, 0) = init_auto;
20526 if (processing_template_decl)
20527 argvec = add_to_template_args (current_template_args (), argvec);
20528 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20529 }
20530
20531 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20532 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20533
20534 tree
20535 do_auto_deduction (tree type, tree init, tree auto_node)
20536 {
20537 tree parms, tparms, targs;
20538 tree args[1];
20539 int val;
20540
20541 if (init == error_mark_node)
20542 return error_mark_node;
20543
20544 if (type_dependent_expression_p (init))
20545 /* Defining a subset of type-dependent expressions that we can deduce
20546 from ahead of time isn't worth the trouble. */
20547 return type;
20548
20549 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20550 with either a new invented type template parameter U or, if the
20551 initializer is a braced-init-list (8.5.4), with
20552 std::initializer_list<U>. */
20553 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20554 type = listify_autos (type, auto_node);
20555
20556 init = resolve_nondeduced_context (init);
20557
20558 parms = build_tree_list (NULL_TREE, type);
20559 args[0] = init;
20560 tparms = make_tree_vec (1);
20561 targs = make_tree_vec (1);
20562 TREE_VEC_ELT (tparms, 0)
20563 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20564 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20565 DEDUCE_CALL, LOOKUP_NORMAL,
20566 /*explain_p=*/false);
20567 if (val > 0)
20568 {
20569 if (processing_template_decl)
20570 /* Try again at instantiation time. */
20571 return type;
20572 if (type && type != error_mark_node)
20573 /* If type is error_mark_node a diagnostic must have been
20574 emitted by now. Also, having a mention to '<type error>'
20575 in the diagnostic is not really useful to the user. */
20576 {
20577 if (cfun && auto_node == current_function_auto_return_pattern
20578 && LAMBDA_FUNCTION_P (current_function_decl))
20579 error ("unable to deduce lambda return type from %qE", init);
20580 else
20581 error ("unable to deduce %qT from %qE", type, init);
20582 }
20583 return error_mark_node;
20584 }
20585
20586 /* If the list of declarators contains more than one declarator, the type
20587 of each declared variable is determined as described above. If the
20588 type deduced for the template parameter U is not the same in each
20589 deduction, the program is ill-formed. */
20590 if (TREE_TYPE (auto_node)
20591 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20592 {
20593 if (cfun && auto_node == current_function_auto_return_pattern
20594 && LAMBDA_FUNCTION_P (current_function_decl))
20595 error ("inconsistent types %qT and %qT deduced for "
20596 "lambda return type", TREE_TYPE (auto_node),
20597 TREE_VEC_ELT (targs, 0));
20598 else
20599 error ("inconsistent deduction for %qT: %qT and then %qT",
20600 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20601 return error_mark_node;
20602 }
20603 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20604
20605 if (processing_template_decl)
20606 targs = add_to_template_args (current_template_args (), targs);
20607 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20608 }
20609
20610 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20611 result. */
20612
20613 tree
20614 splice_late_return_type (tree type, tree late_return_type)
20615 {
20616 tree argvec;
20617
20618 if (late_return_type == NULL_TREE)
20619 return type;
20620 argvec = make_tree_vec (1);
20621 TREE_VEC_ELT (argvec, 0) = late_return_type;
20622 if (processing_template_parmlist)
20623 /* For a late-specified return type in a template type-parameter, we
20624 need to add a dummy argument level for its parmlist. */
20625 argvec = add_to_template_args
20626 (make_tree_vec (processing_template_parmlist), argvec);
20627 if (current_template_parms)
20628 argvec = add_to_template_args (current_template_args (), argvec);
20629 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20630 }
20631
20632 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20633
20634 bool
20635 is_auto (const_tree type)
20636 {
20637 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20638 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20639 return true;
20640 else
20641 return false;
20642 }
20643
20644 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20645 appear as a type-specifier for the declaration in question, we don't
20646 have to look through the whole type. */
20647
20648 tree
20649 type_uses_auto (tree type)
20650 {
20651 enum tree_code code;
20652 if (is_auto (type))
20653 return type;
20654
20655 code = TREE_CODE (type);
20656
20657 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20658 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20659 || code == METHOD_TYPE || code == ARRAY_TYPE)
20660 return type_uses_auto (TREE_TYPE (type));
20661
20662 if (TYPE_PTRMEMFUNC_P (type))
20663 return type_uses_auto (TREE_TYPE (TREE_TYPE
20664 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20665
20666 return NULL_TREE;
20667 }
20668
20669 /* For a given template T, return the vector of typedefs referenced
20670 in T for which access check is needed at T instantiation time.
20671 T is either a FUNCTION_DECL or a RECORD_TYPE.
20672 Those typedefs were added to T by the function
20673 append_type_to_template_for_access_check. */
20674
20675 vec<qualified_typedef_usage_t, va_gc> *
20676 get_types_needing_access_check (tree t)
20677 {
20678 tree ti;
20679 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
20680
20681 if (!t || t == error_mark_node)
20682 return NULL;
20683
20684 if (!(ti = get_template_info (t)))
20685 return NULL;
20686
20687 if (CLASS_TYPE_P (t)
20688 || TREE_CODE (t) == FUNCTION_DECL)
20689 {
20690 if (!TI_TEMPLATE (ti))
20691 return NULL;
20692
20693 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20694 }
20695
20696 return result;
20697 }
20698
20699 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20700 tied to T. That list of typedefs will be access checked at
20701 T instantiation time.
20702 T is either a FUNCTION_DECL or a RECORD_TYPE.
20703 TYPE_DECL is a TYPE_DECL node representing a typedef.
20704 SCOPE is the scope through which TYPE_DECL is accessed.
20705 LOCATION is the location of the usage point of TYPE_DECL.
20706
20707 This function is a subroutine of
20708 append_type_to_template_for_access_check. */
20709
20710 static void
20711 append_type_to_template_for_access_check_1 (tree t,
20712 tree type_decl,
20713 tree scope,
20714 location_t location)
20715 {
20716 qualified_typedef_usage_t typedef_usage;
20717 tree ti;
20718
20719 if (!t || t == error_mark_node)
20720 return;
20721
20722 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20723 || CLASS_TYPE_P (t))
20724 && type_decl
20725 && TREE_CODE (type_decl) == TYPE_DECL
20726 && scope);
20727
20728 if (!(ti = get_template_info (t)))
20729 return;
20730
20731 gcc_assert (TI_TEMPLATE (ti));
20732
20733 typedef_usage.typedef_decl = type_decl;
20734 typedef_usage.context = scope;
20735 typedef_usage.locus = location;
20736
20737 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
20738 }
20739
20740 /* Append TYPE_DECL to the template TEMPL.
20741 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20742 At TEMPL instanciation time, TYPE_DECL will be checked to see
20743 if it can be accessed through SCOPE.
20744 LOCATION is the location of the usage point of TYPE_DECL.
20745
20746 e.g. consider the following code snippet:
20747
20748 class C
20749 {
20750 typedef int myint;
20751 };
20752
20753 template<class U> struct S
20754 {
20755 C::myint mi; // <-- usage point of the typedef C::myint
20756 };
20757
20758 S<char> s;
20759
20760 At S<char> instantiation time, we need to check the access of C::myint
20761 In other words, we need to check the access of the myint typedef through
20762 the C scope. For that purpose, this function will add the myint typedef
20763 and the scope C through which its being accessed to a list of typedefs
20764 tied to the template S. That list will be walked at template instantiation
20765 time and access check performed on each typedefs it contains.
20766 Note that this particular code snippet should yield an error because
20767 myint is private to C. */
20768
20769 void
20770 append_type_to_template_for_access_check (tree templ,
20771 tree type_decl,
20772 tree scope,
20773 location_t location)
20774 {
20775 qualified_typedef_usage_t *iter;
20776 unsigned i;
20777
20778 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20779
20780 /* Make sure we don't append the type to the template twice. */
20781 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
20782 if (iter->typedef_decl == type_decl && scope == iter->context)
20783 return;
20784
20785 append_type_to_template_for_access_check_1 (templ, type_decl,
20786 scope, location);
20787 }
20788
20789 /* Set up the hash tables for template instantiations. */
20790
20791 void
20792 init_template_processing (void)
20793 {
20794 decl_specializations = htab_create_ggc (37,
20795 hash_specialization,
20796 eq_specializations,
20797 ggc_free);
20798 type_specializations = htab_create_ggc (37,
20799 hash_specialization,
20800 eq_specializations,
20801 ggc_free);
20802 }
20803
20804 /* Print stats about the template hash tables for -fstats. */
20805
20806 void
20807 print_template_statistics (void)
20808 {
20809 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20810 "%f collisions\n", (long) htab_size (decl_specializations),
20811 (long) htab_elements (decl_specializations),
20812 htab_collisions (decl_specializations));
20813 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20814 "%f collisions\n", (long) htab_size (type_specializations),
20815 (long) htab_elements (type_specializations),
20816 htab_collisions (type_specializations));
20817 }
20818
20819 #include "gt-cp-pt.h"