a39d114818f6012f8e77d0e2cad0304b740e62bf
[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 return error_mark_node;
4786 }
4787
4788 if (current == decl)
4789 current = ctx;
4790 else if (current == NULL_TREE)
4791 /* Can happen in erroneous input. */
4792 break;
4793 else
4794 current = (TYPE_P (current)
4795 ? TYPE_CONTEXT (current)
4796 : DECL_CONTEXT (current));
4797 }
4798
4799 /* Check that the parms are used in the appropriate qualifying scopes
4800 in the declarator. */
4801 if (!comp_template_args
4802 (TI_ARGS (tinfo),
4803 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4804 {
4805 error ("\
4806 template arguments to %qD do not match original template %qD",
4807 decl, DECL_TEMPLATE_RESULT (tmpl));
4808 if (!uses_template_parms (TI_ARGS (tinfo)))
4809 inform (input_location, "use template<> for an explicit specialization");
4810 /* Avoid crash in import_export_decl. */
4811 DECL_INTERFACE_KNOWN (decl) = 1;
4812 return error_mark_node;
4813 }
4814 }
4815
4816 DECL_TEMPLATE_RESULT (tmpl) = decl;
4817 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4818
4819 /* Push template declarations for global functions and types. Note
4820 that we do not try to push a global template friend declared in a
4821 template class; such a thing may well depend on the template
4822 parameters of the class. */
4823 if (new_template_p && !ctx
4824 && !(is_friend && template_class_depth (current_class_type) > 0))
4825 {
4826 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4827 if (tmpl == error_mark_node)
4828 return error_mark_node;
4829
4830 /* Hide template friend classes that haven't been declared yet. */
4831 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4832 {
4833 DECL_ANTICIPATED (tmpl) = 1;
4834 DECL_FRIEND_P (tmpl) = 1;
4835 }
4836 }
4837
4838 if (is_primary)
4839 {
4840 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4841 int i;
4842
4843 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4844 if (DECL_CONV_FN_P (tmpl))
4845 {
4846 int depth = TMPL_PARMS_DEPTH (parms);
4847
4848 /* It is a conversion operator. See if the type converted to
4849 depends on innermost template operands. */
4850
4851 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4852 depth))
4853 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4854 }
4855
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 parms = INNERMOST_TEMPLATE_PARMS (parms);
4859 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4860 {
4861 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4862 if (TREE_CODE (parm) == TEMPLATE_DECL)
4863 DECL_CONTEXT (parm) = tmpl;
4864 }
4865 }
4866
4867 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4868 back to its most general template. If TMPL is a specialization,
4869 ARGS may only have the innermost set of arguments. Add the missing
4870 argument levels if necessary. */
4871 if (DECL_TEMPLATE_INFO (tmpl))
4872 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4873
4874 info = build_template_info (tmpl, args);
4875
4876 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4877 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4878 else
4879 {
4880 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4881 retrofit_lang_decl (decl);
4882 if (DECL_LANG_SPECIFIC (decl))
4883 DECL_TEMPLATE_INFO (decl) = info;
4884 }
4885
4886 return DECL_TEMPLATE_RESULT (tmpl);
4887 }
4888
4889 tree
4890 push_template_decl (tree decl)
4891 {
4892 return push_template_decl_real (decl, false);
4893 }
4894
4895 /* FN is an inheriting constructor that inherits from the constructor
4896 template INHERITED; turn FN into a constructor template with a matching
4897 template header. */
4898
4899 tree
4900 add_inherited_template_parms (tree fn, tree inherited)
4901 {
4902 tree inner_parms
4903 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4904 inner_parms = copy_node (inner_parms);
4905 tree parms
4906 = tree_cons (size_int (processing_template_decl + 1),
4907 inner_parms, current_template_parms);
4908 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4909 tree args = template_parms_to_args (parms);
4910 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4911 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4912 DECL_TEMPLATE_RESULT (tmpl) = fn;
4913 DECL_ARTIFICIAL (tmpl) = true;
4914 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4915 return tmpl;
4916 }
4917
4918 /* Called when a class template TYPE is redeclared with the indicated
4919 template PARMS, e.g.:
4920
4921 template <class T> struct S;
4922 template <class T> struct S {}; */
4923
4924 bool
4925 redeclare_class_template (tree type, tree parms)
4926 {
4927 tree tmpl;
4928 tree tmpl_parms;
4929 int i;
4930
4931 if (!TYPE_TEMPLATE_INFO (type))
4932 {
4933 error ("%qT is not a template type", type);
4934 return false;
4935 }
4936
4937 tmpl = TYPE_TI_TEMPLATE (type);
4938 if (!PRIMARY_TEMPLATE_P (tmpl))
4939 /* The type is nested in some template class. Nothing to worry
4940 about here; there are no new template parameters for the nested
4941 type. */
4942 return true;
4943
4944 if (!parms)
4945 {
4946 error ("template specifiers not specified in declaration of %qD",
4947 tmpl);
4948 return false;
4949 }
4950
4951 parms = INNERMOST_TEMPLATE_PARMS (parms);
4952 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4953
4954 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4955 {
4956 error_n (input_location, TREE_VEC_LENGTH (parms),
4957 "redeclared with %d template parameter",
4958 "redeclared with %d template parameters",
4959 TREE_VEC_LENGTH (parms));
4960 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4961 "previous declaration %q+D used %d template parameter",
4962 "previous declaration %q+D used %d template parameters",
4963 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4964 return false;
4965 }
4966
4967 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4968 {
4969 tree tmpl_parm;
4970 tree parm;
4971 tree tmpl_default;
4972 tree parm_default;
4973
4974 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4975 || TREE_VEC_ELT (parms, i) == error_mark_node)
4976 continue;
4977
4978 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4979 if (tmpl_parm == error_mark_node)
4980 return false;
4981
4982 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4983 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4984 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4985
4986 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4987 TEMPLATE_DECL. */
4988 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4989 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4990 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4991 || (TREE_CODE (tmpl_parm) != PARM_DECL
4992 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4993 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4994 || (TREE_CODE (tmpl_parm) == PARM_DECL
4995 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4996 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4997 {
4998 error ("template parameter %q+#D", tmpl_parm);
4999 error ("redeclared here as %q#D", parm);
5000 return false;
5001 }
5002
5003 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5004 {
5005 /* We have in [temp.param]:
5006
5007 A template-parameter may not be given default arguments
5008 by two different declarations in the same scope. */
5009 error_at (input_location, "redefinition of default argument for %q#D", parm);
5010 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5011 "original definition appeared here");
5012 return false;
5013 }
5014
5015 if (parm_default != NULL_TREE)
5016 /* Update the previous template parameters (which are the ones
5017 that will really count) with the new default value. */
5018 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5019 else if (tmpl_default != NULL_TREE)
5020 /* Update the new parameters, too; they'll be used as the
5021 parameters for any members. */
5022 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5023 }
5024
5025 return true;
5026 }
5027
5028 /* Simplify EXPR if it is a non-dependent expression. Returns the
5029 (possibly simplified) expression. */
5030
5031 tree
5032 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5033 {
5034 if (expr == NULL_TREE)
5035 return NULL_TREE;
5036
5037 /* If we're in a template, but EXPR isn't value dependent, simplify
5038 it. We're supposed to treat:
5039
5040 template <typename T> void f(T[1 + 1]);
5041 template <typename T> void f(T[2]);
5042
5043 as two declarations of the same function, for example. */
5044 if (processing_template_decl
5045 && !type_dependent_expression_p (expr)
5046 && potential_constant_expression (expr)
5047 && !value_dependent_expression_p (expr))
5048 {
5049 HOST_WIDE_INT saved_processing_template_decl;
5050
5051 saved_processing_template_decl = processing_template_decl;
5052 processing_template_decl = 0;
5053 expr = tsubst_copy_and_build (expr,
5054 /*args=*/NULL_TREE,
5055 complain,
5056 /*in_decl=*/NULL_TREE,
5057 /*function_p=*/false,
5058 /*integral_constant_expression_p=*/true);
5059 processing_template_decl = saved_processing_template_decl;
5060 }
5061 return expr;
5062 }
5063
5064 tree
5065 fold_non_dependent_expr (tree expr)
5066 {
5067 return fold_non_dependent_expr_sfinae (expr, tf_error);
5068 }
5069
5070 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5071 template declaration, or a TYPE_DECL for an alias declaration. */
5072
5073 bool
5074 alias_type_or_template_p (tree t)
5075 {
5076 if (t == NULL_TREE)
5077 return false;
5078 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5079 || (TYPE_P (t)
5080 && TYPE_NAME (t)
5081 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5082 || DECL_ALIAS_TEMPLATE_P (t));
5083 }
5084
5085 /* Return TRUE iff is a specialization of an alias template. */
5086
5087 bool
5088 alias_template_specialization_p (const_tree t)
5089 {
5090 if (t == NULL_TREE)
5091 return false;
5092
5093 return (TYPE_P (t)
5094 && TYPE_TEMPLATE_INFO (t)
5095 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5096 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5097 }
5098
5099 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5100 must be a function or a pointer-to-function type, as specified
5101 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5102 and check that the resulting function has external linkage. */
5103
5104 static tree
5105 convert_nontype_argument_function (tree type, tree expr)
5106 {
5107 tree fns = expr;
5108 tree fn, fn_no_ptr;
5109 linkage_kind linkage;
5110
5111 fn = instantiate_type (type, fns, tf_none);
5112 if (fn == error_mark_node)
5113 return error_mark_node;
5114
5115 fn_no_ptr = fn;
5116 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5117 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5118 if (BASELINK_P (fn_no_ptr))
5119 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5120
5121 /* [temp.arg.nontype]/1
5122
5123 A template-argument for a non-type, non-template template-parameter
5124 shall be one of:
5125 [...]
5126 -- the address of an object or function with external [C++11: or
5127 internal] linkage. */
5128
5129 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5130 {
5131 error ("%qE is not a valid template argument for type %qT", expr, type);
5132 if (TREE_CODE (type) == POINTER_TYPE)
5133 error ("it must be the address of a function with external linkage");
5134 else
5135 error ("it must be the name of a function with external linkage");
5136 return NULL_TREE;
5137 }
5138
5139 linkage = decl_linkage (fn_no_ptr);
5140 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5141 {
5142 if (cxx_dialect >= cxx0x)
5143 error ("%qE is not a valid template argument for type %qT "
5144 "because %qD has no linkage",
5145 expr, type, fn_no_ptr);
5146 else
5147 error ("%qE is not a valid template argument for type %qT "
5148 "because %qD does not have external linkage",
5149 expr, type, fn_no_ptr);
5150 return NULL_TREE;
5151 }
5152
5153 return fn;
5154 }
5155
5156 /* Subroutine of convert_nontype_argument.
5157 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5158 Emit an error otherwise. */
5159
5160 static bool
5161 check_valid_ptrmem_cst_expr (tree type, tree expr,
5162 tsubst_flags_t complain)
5163 {
5164 STRIP_NOPS (expr);
5165 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5166 return true;
5167 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5168 return true;
5169 if (complain & tf_error)
5170 {
5171 error ("%qE is not a valid template argument for type %qT",
5172 expr, type);
5173 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5174 }
5175 return false;
5176 }
5177
5178 /* Returns TRUE iff the address of OP is value-dependent.
5179
5180 14.6.2.4 [temp.dep.temp]:
5181 A non-integral non-type template-argument is dependent if its type is
5182 dependent or it has either of the following forms
5183 qualified-id
5184 & qualified-id
5185 and contains a nested-name-specifier which specifies a class-name that
5186 names a dependent type.
5187
5188 We generalize this to just say that the address of a member of a
5189 dependent class is value-dependent; the above doesn't cover the
5190 address of a static data member named with an unqualified-id. */
5191
5192 static bool
5193 has_value_dependent_address (tree op)
5194 {
5195 /* We could use get_inner_reference here, but there's no need;
5196 this is only relevant for template non-type arguments, which
5197 can only be expressed as &id-expression. */
5198 if (DECL_P (op))
5199 {
5200 tree ctx = CP_DECL_CONTEXT (op);
5201 if (TYPE_P (ctx) && dependent_type_p (ctx))
5202 return true;
5203 }
5204
5205 return false;
5206 }
5207
5208 /* The next set of functions are used for providing helpful explanatory
5209 diagnostics for failed overload resolution. Their messages should be
5210 indented by two spaces for consistency with the messages in
5211 call.c */
5212
5213 static int
5214 unify_success (bool /*explain_p*/)
5215 {
5216 return 0;
5217 }
5218
5219 static int
5220 unify_parameter_deduction_failure (bool explain_p, tree parm)
5221 {
5222 if (explain_p)
5223 inform (input_location,
5224 " couldn't deduce template parameter %qD", parm);
5225 return 1;
5226 }
5227
5228 static int
5229 unify_invalid (bool /*explain_p*/)
5230 {
5231 return 1;
5232 }
5233
5234 static int
5235 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5236 {
5237 if (explain_p)
5238 inform (input_location,
5239 " types %qT and %qT have incompatible cv-qualifiers",
5240 parm, arg);
5241 return 1;
5242 }
5243
5244 static int
5245 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5246 {
5247 if (explain_p)
5248 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5249 return 1;
5250 }
5251
5252 static int
5253 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5254 {
5255 if (explain_p)
5256 inform (input_location,
5257 " template parameter %qD is not a parameter pack, but "
5258 "argument %qD is",
5259 parm, arg);
5260 return 1;
5261 }
5262
5263 static int
5264 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5265 {
5266 if (explain_p)
5267 inform (input_location,
5268 " template argument %qE does not match "
5269 "pointer-to-member constant %qE",
5270 arg, parm);
5271 return 1;
5272 }
5273
5274 static int
5275 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5276 {
5277 if (explain_p)
5278 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5279 return 1;
5280 }
5281
5282 static int
5283 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5284 {
5285 if (explain_p)
5286 inform (input_location,
5287 " inconsistent parameter pack deduction with %qT and %qT",
5288 old_arg, new_arg);
5289 return 1;
5290 }
5291
5292 static int
5293 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5294 {
5295 if (explain_p)
5296 {
5297 if (TYPE_P (parm))
5298 inform (input_location,
5299 " deduced conflicting types for parameter %qT (%qT and %qT)",
5300 parm, first, second);
5301 else
5302 inform (input_location,
5303 " deduced conflicting values for non-type parameter "
5304 "%qE (%qE and %qE)", parm, first, second);
5305 }
5306 return 1;
5307 }
5308
5309 static int
5310 unify_vla_arg (bool explain_p, tree arg)
5311 {
5312 if (explain_p)
5313 inform (input_location,
5314 " variable-sized array type %qT is not "
5315 "a valid template argument",
5316 arg);
5317 return 1;
5318 }
5319
5320 static int
5321 unify_method_type_error (bool explain_p, tree arg)
5322 {
5323 if (explain_p)
5324 inform (input_location,
5325 " member function type %qT is not a valid template argument",
5326 arg);
5327 return 1;
5328 }
5329
5330 static int
5331 unify_arity (bool explain_p, int have, int wanted)
5332 {
5333 if (explain_p)
5334 inform_n (input_location, wanted,
5335 " candidate expects %d argument, %d provided",
5336 " candidate expects %d arguments, %d provided",
5337 wanted, have);
5338 return 1;
5339 }
5340
5341 static int
5342 unify_too_many_arguments (bool explain_p, int have, int wanted)
5343 {
5344 return unify_arity (explain_p, have, wanted);
5345 }
5346
5347 static int
5348 unify_too_few_arguments (bool explain_p, int have, int wanted)
5349 {
5350 return unify_arity (explain_p, have, wanted);
5351 }
5352
5353 static int
5354 unify_arg_conversion (bool explain_p, tree to_type,
5355 tree from_type, tree arg)
5356 {
5357 if (explain_p)
5358 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5359 arg, from_type, to_type);
5360 return 1;
5361 }
5362
5363 static int
5364 unify_no_common_base (bool explain_p, enum template_base_result r,
5365 tree parm, tree arg)
5366 {
5367 if (explain_p)
5368 switch (r)
5369 {
5370 case tbr_ambiguous_baseclass:
5371 inform (input_location, " %qT is an ambiguous base class of %qT",
5372 arg, parm);
5373 break;
5374 default:
5375 inform (input_location, " %qT is not derived from %qT", arg, parm);
5376 break;
5377 }
5378 return 1;
5379 }
5380
5381 static int
5382 unify_inconsistent_template_template_parameters (bool explain_p)
5383 {
5384 if (explain_p)
5385 inform (input_location,
5386 " template parameters of a template template argument are "
5387 "inconsistent with other deduced template arguments");
5388 return 1;
5389 }
5390
5391 static int
5392 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5393 {
5394 if (explain_p)
5395 inform (input_location,
5396 " can't deduce a template for %qT from non-template type %qT",
5397 parm, arg);
5398 return 1;
5399 }
5400
5401 static int
5402 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5403 {
5404 if (explain_p)
5405 inform (input_location,
5406 " template argument %qE does not match %qD", arg, parm);
5407 return 1;
5408 }
5409
5410 static int
5411 unify_overload_resolution_failure (bool explain_p, tree arg)
5412 {
5413 if (explain_p)
5414 inform (input_location,
5415 " could not resolve address from overloaded function %qE",
5416 arg);
5417 return 1;
5418 }
5419
5420 /* Attempt to convert the non-type template parameter EXPR to the
5421 indicated TYPE. If the conversion is successful, return the
5422 converted value. If the conversion is unsuccessful, return
5423 NULL_TREE if we issued an error message, or error_mark_node if we
5424 did not. We issue error messages for out-and-out bad template
5425 parameters, but not simply because the conversion failed, since we
5426 might be just trying to do argument deduction. Both TYPE and EXPR
5427 must be non-dependent.
5428
5429 The conversion follows the special rules described in
5430 [temp.arg.nontype], and it is much more strict than an implicit
5431 conversion.
5432
5433 This function is called twice for each template argument (see
5434 lookup_template_class for a more accurate description of this
5435 problem). This means that we need to handle expressions which
5436 are not valid in a C++ source, but can be created from the
5437 first call (for instance, casts to perform conversions). These
5438 hacks can go away after we fix the double coercion problem. */
5439
5440 static tree
5441 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5442 {
5443 tree expr_type;
5444
5445 /* Detect immediately string literals as invalid non-type argument.
5446 This special-case is not needed for correctness (we would easily
5447 catch this later), but only to provide better diagnostic for this
5448 common user mistake. As suggested by DR 100, we do not mention
5449 linkage issues in the diagnostic as this is not the point. */
5450 /* FIXME we're making this OK. */
5451 if (TREE_CODE (expr) == STRING_CST)
5452 {
5453 if (complain & tf_error)
5454 error ("%qE is not a valid template argument for type %qT "
5455 "because string literals can never be used in this context",
5456 expr, type);
5457 return NULL_TREE;
5458 }
5459
5460 /* Add the ADDR_EXPR now for the benefit of
5461 value_dependent_expression_p. */
5462 if (TYPE_PTROBV_P (type)
5463 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5464 {
5465 expr = decay_conversion (expr, complain);
5466 if (expr == error_mark_node)
5467 return error_mark_node;
5468 }
5469
5470 /* If we are in a template, EXPR may be non-dependent, but still
5471 have a syntactic, rather than semantic, form. For example, EXPR
5472 might be a SCOPE_REF, rather than the VAR_DECL to which the
5473 SCOPE_REF refers. Preserving the qualifying scope is necessary
5474 so that access checking can be performed when the template is
5475 instantiated -- but here we need the resolved form so that we can
5476 convert the argument. */
5477 if (TYPE_REF_OBJ_P (type)
5478 && has_value_dependent_address (expr))
5479 /* If we want the address and it's value-dependent, don't fold. */;
5480 else if (!type_unknown_p (expr))
5481 expr = fold_non_dependent_expr_sfinae (expr, complain);
5482 if (error_operand_p (expr))
5483 return error_mark_node;
5484 expr_type = TREE_TYPE (expr);
5485 if (TREE_CODE (type) == REFERENCE_TYPE)
5486 expr = mark_lvalue_use (expr);
5487 else
5488 expr = mark_rvalue_use (expr);
5489
5490 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5491 to a non-type argument of "nullptr". */
5492 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5493 expr = convert (type, expr);
5494
5495 /* In C++11, integral or enumeration non-type template arguments can be
5496 arbitrary constant expressions. Pointer and pointer to
5497 member arguments can be general constant expressions that evaluate
5498 to a null value, but otherwise still need to be of a specific form. */
5499 if (cxx_dialect >= cxx0x)
5500 {
5501 if (TREE_CODE (expr) == PTRMEM_CST)
5502 /* A PTRMEM_CST is already constant, and a valid template
5503 argument for a parameter of pointer to member type, we just want
5504 to leave it in that form rather than lower it to a
5505 CONSTRUCTOR. */;
5506 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5507 expr = maybe_constant_value (expr);
5508 else if (TYPE_PTR_OR_PTRMEM_P (type))
5509 {
5510 tree folded = maybe_constant_value (expr);
5511 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5512 : null_member_pointer_value_p (folded))
5513 expr = folded;
5514 }
5515 }
5516
5517 /* HACK: Due to double coercion, we can get a
5518 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5519 which is the tree that we built on the first call (see
5520 below when coercing to reference to object or to reference to
5521 function). We just strip everything and get to the arg.
5522 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5523 for examples. */
5524 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5525 {
5526 tree probe_type, probe = expr;
5527 if (REFERENCE_REF_P (probe))
5528 probe = TREE_OPERAND (probe, 0);
5529 probe_type = TREE_TYPE (probe);
5530 if (TREE_CODE (probe) == NOP_EXPR)
5531 {
5532 /* ??? Maybe we could use convert_from_reference here, but we
5533 would need to relax its constraints because the NOP_EXPR
5534 could actually change the type to something more cv-qualified,
5535 and this is not folded by convert_from_reference. */
5536 tree addr = TREE_OPERAND (probe, 0);
5537 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5538 && TREE_CODE (addr) == ADDR_EXPR
5539 && TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE
5540 && (same_type_ignoring_top_level_qualifiers_p
5541 (TREE_TYPE (probe_type),
5542 TREE_TYPE (TREE_TYPE (addr)))))
5543 {
5544 expr = TREE_OPERAND (addr, 0);
5545 expr_type = TREE_TYPE (expr);
5546 }
5547 }
5548 }
5549
5550 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5551 parameter is a pointer to object, through decay and
5552 qualification conversion. Let's strip everything. */
5553 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5554 {
5555 STRIP_NOPS (expr);
5556 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5557 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5558 /* Skip the ADDR_EXPR only if it is part of the decay for
5559 an array. Otherwise, it is part of the original argument
5560 in the source code. */
5561 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5562 expr = TREE_OPERAND (expr, 0);
5563 expr_type = TREE_TYPE (expr);
5564 }
5565
5566 /* [temp.arg.nontype]/5, bullet 1
5567
5568 For a non-type template-parameter of integral or enumeration type,
5569 integral promotions (_conv.prom_) and integral conversions
5570 (_conv.integral_) are applied. */
5571 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5572 {
5573 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5574 t = maybe_constant_value (t);
5575 if (t != error_mark_node)
5576 expr = t;
5577
5578 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5579 return error_mark_node;
5580
5581 /* Notice that there are constant expressions like '4 % 0' which
5582 do not fold into integer constants. */
5583 if (TREE_CODE (expr) != INTEGER_CST)
5584 {
5585 if (complain & tf_error)
5586 {
5587 int errs = errorcount, warns = warningcount;
5588 if (processing_template_decl
5589 && !require_potential_constant_expression (expr))
5590 return NULL_TREE;
5591 expr = cxx_constant_value (expr);
5592 if (errorcount > errs || warningcount > warns)
5593 inform (EXPR_LOC_OR_HERE (expr),
5594 "in template argument for type %qT ", type);
5595 if (expr == error_mark_node)
5596 return NULL_TREE;
5597 /* else cxx_constant_value complained but gave us
5598 a real constant, so go ahead. */
5599 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5600 }
5601 else
5602 return NULL_TREE;
5603 }
5604 }
5605 /* [temp.arg.nontype]/5, bullet 2
5606
5607 For a non-type template-parameter of type pointer to object,
5608 qualification conversions (_conv.qual_) and the array-to-pointer
5609 conversion (_conv.array_) are applied. */
5610 else if (TYPE_PTROBV_P (type))
5611 {
5612 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5613
5614 A template-argument for a non-type, non-template template-parameter
5615 shall be one of: [...]
5616
5617 -- the name of a non-type template-parameter;
5618 -- the address of an object or function with external linkage, [...]
5619 expressed as "& id-expression" where the & is optional if the name
5620 refers to a function or array, or if the corresponding
5621 template-parameter is a reference.
5622
5623 Here, we do not care about functions, as they are invalid anyway
5624 for a parameter of type pointer-to-object. */
5625
5626 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5627 /* Non-type template parameters are OK. */
5628 ;
5629 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5630 /* Null pointer values are OK in C++11. */;
5631 else if (TREE_CODE (expr) != ADDR_EXPR
5632 && TREE_CODE (expr_type) != ARRAY_TYPE)
5633 {
5634 if (TREE_CODE (expr) == VAR_DECL)
5635 {
5636 error ("%qD is not a valid template argument "
5637 "because %qD is a variable, not the address of "
5638 "a variable",
5639 expr, expr);
5640 return NULL_TREE;
5641 }
5642 /* Other values, like integer constants, might be valid
5643 non-type arguments of some other type. */
5644 return error_mark_node;
5645 }
5646 else
5647 {
5648 tree decl;
5649
5650 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5651 ? TREE_OPERAND (expr, 0) : expr);
5652 if (TREE_CODE (decl) != VAR_DECL)
5653 {
5654 error ("%qE is not a valid template argument of type %qT "
5655 "because %qE is not a variable",
5656 expr, type, decl);
5657 return NULL_TREE;
5658 }
5659 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5660 {
5661 error ("%qE is not a valid template argument of type %qT "
5662 "because %qD does not have external linkage",
5663 expr, type, decl);
5664 return NULL_TREE;
5665 }
5666 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5667 {
5668 error ("%qE is not a valid template argument of type %qT "
5669 "because %qD has no linkage",
5670 expr, type, decl);
5671 return NULL_TREE;
5672 }
5673 }
5674
5675 expr = decay_conversion (expr, complain);
5676 if (expr == error_mark_node)
5677 return error_mark_node;
5678
5679 expr = perform_qualification_conversions (type, expr);
5680 if (expr == error_mark_node)
5681 return error_mark_node;
5682 }
5683 /* [temp.arg.nontype]/5, bullet 3
5684
5685 For a non-type template-parameter of type reference to object, no
5686 conversions apply. The type referred to by the reference may be more
5687 cv-qualified than the (otherwise identical) type of the
5688 template-argument. The template-parameter is bound directly to the
5689 template-argument, which must be an lvalue. */
5690 else if (TYPE_REF_OBJ_P (type))
5691 {
5692 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5693 expr_type))
5694 return error_mark_node;
5695
5696 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5697 {
5698 error ("%qE is not a valid template argument for type %qT "
5699 "because of conflicts in cv-qualification", expr, type);
5700 return NULL_TREE;
5701 }
5702
5703 if (!real_lvalue_p (expr))
5704 {
5705 error ("%qE is not a valid template argument for type %qT "
5706 "because it is not an lvalue", expr, type);
5707 return NULL_TREE;
5708 }
5709
5710 /* [temp.arg.nontype]/1
5711
5712 A template-argument for a non-type, non-template template-parameter
5713 shall be one of: [...]
5714
5715 -- the address of an object or function with external linkage. */
5716 if (TREE_CODE (expr) == INDIRECT_REF
5717 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5718 {
5719 expr = TREE_OPERAND (expr, 0);
5720 if (DECL_P (expr))
5721 {
5722 error ("%q#D is not a valid template argument for type %qT "
5723 "because a reference variable does not have a constant "
5724 "address", expr, type);
5725 return NULL_TREE;
5726 }
5727 }
5728
5729 if (!DECL_P (expr))
5730 {
5731 error ("%qE is not a valid template argument for type %qT "
5732 "because it is not an object with external linkage",
5733 expr, type);
5734 return NULL_TREE;
5735 }
5736
5737 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5738 {
5739 error ("%qE is not a valid template argument for type %qT "
5740 "because object %qD has not external linkage",
5741 expr, type, expr);
5742 return NULL_TREE;
5743 }
5744
5745 expr = build_nop (type, build_address (expr));
5746 }
5747 /* [temp.arg.nontype]/5, bullet 4
5748
5749 For a non-type template-parameter of type pointer to function, only
5750 the function-to-pointer conversion (_conv.func_) is applied. If the
5751 template-argument represents a set of overloaded functions (or a
5752 pointer to such), the matching function is selected from the set
5753 (_over.over_). */
5754 else if (TYPE_PTRFN_P (type))
5755 {
5756 /* If the argument is a template-id, we might not have enough
5757 context information to decay the pointer. */
5758 if (!type_unknown_p (expr_type))
5759 {
5760 expr = decay_conversion (expr, complain);
5761 if (expr == error_mark_node)
5762 return error_mark_node;
5763 }
5764
5765 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5766 /* Null pointer values are OK in C++11. */
5767 return perform_qualification_conversions (type, expr);
5768
5769 expr = convert_nontype_argument_function (type, expr);
5770 if (!expr || expr == error_mark_node)
5771 return expr;
5772 }
5773 /* [temp.arg.nontype]/5, bullet 5
5774
5775 For a non-type template-parameter of type reference to function, no
5776 conversions apply. If the template-argument represents a set of
5777 overloaded functions, the matching function is selected from the set
5778 (_over.over_). */
5779 else if (TYPE_REFFN_P (type))
5780 {
5781 if (TREE_CODE (expr) == ADDR_EXPR)
5782 {
5783 error ("%qE is not a valid template argument for type %qT "
5784 "because it is a pointer", expr, type);
5785 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5786 return NULL_TREE;
5787 }
5788
5789 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5790 if (!expr || expr == error_mark_node)
5791 return expr;
5792
5793 expr = build_nop (type, build_address (expr));
5794 }
5795 /* [temp.arg.nontype]/5, bullet 6
5796
5797 For a non-type template-parameter of type pointer to member function,
5798 no conversions apply. If the template-argument represents a set of
5799 overloaded member functions, the matching member function is selected
5800 from the set (_over.over_). */
5801 else if (TYPE_PTRMEMFUNC_P (type))
5802 {
5803 expr = instantiate_type (type, expr, tf_none);
5804 if (expr == error_mark_node)
5805 return error_mark_node;
5806
5807 /* [temp.arg.nontype] bullet 1 says the pointer to member
5808 expression must be a pointer-to-member constant. */
5809 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5810 return error_mark_node;
5811
5812 /* There is no way to disable standard conversions in
5813 resolve_address_of_overloaded_function (called by
5814 instantiate_type). It is possible that the call succeeded by
5815 converting &B::I to &D::I (where B is a base of D), so we need
5816 to reject this conversion here.
5817
5818 Actually, even if there was a way to disable standard conversions,
5819 it would still be better to reject them here so that we can
5820 provide a superior diagnostic. */
5821 if (!same_type_p (TREE_TYPE (expr), type))
5822 {
5823 error ("%qE is not a valid template argument for type %qT "
5824 "because it is of type %qT", expr, type,
5825 TREE_TYPE (expr));
5826 /* If we are just one standard conversion off, explain. */
5827 if (can_convert (type, TREE_TYPE (expr), complain))
5828 inform (input_location,
5829 "standard conversions are not allowed in this context");
5830 return NULL_TREE;
5831 }
5832 }
5833 /* [temp.arg.nontype]/5, bullet 7
5834
5835 For a non-type template-parameter of type pointer to data member,
5836 qualification conversions (_conv.qual_) are applied. */
5837 else if (TYPE_PTRDATAMEM_P (type))
5838 {
5839 /* [temp.arg.nontype] bullet 1 says the pointer to member
5840 expression must be a pointer-to-member constant. */
5841 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5842 return error_mark_node;
5843
5844 expr = perform_qualification_conversions (type, expr);
5845 if (expr == error_mark_node)
5846 return expr;
5847 }
5848 else if (NULLPTR_TYPE_P (type))
5849 {
5850 if (expr != nullptr_node)
5851 {
5852 error ("%qE is not a valid template argument for type %qT "
5853 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5854 return NULL_TREE;
5855 }
5856 return expr;
5857 }
5858 /* A template non-type parameter must be one of the above. */
5859 else
5860 gcc_unreachable ();
5861
5862 /* Sanity check: did we actually convert the argument to the
5863 right type? */
5864 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5865 (type, TREE_TYPE (expr)));
5866 return expr;
5867 }
5868
5869 /* Subroutine of coerce_template_template_parms, which returns 1 if
5870 PARM_PARM and ARG_PARM match using the rule for the template
5871 parameters of template template parameters. Both PARM and ARG are
5872 template parameters; the rest of the arguments are the same as for
5873 coerce_template_template_parms.
5874 */
5875 static int
5876 coerce_template_template_parm (tree parm,
5877 tree arg,
5878 tsubst_flags_t complain,
5879 tree in_decl,
5880 tree outer_args)
5881 {
5882 if (arg == NULL_TREE || arg == error_mark_node
5883 || parm == NULL_TREE || parm == error_mark_node)
5884 return 0;
5885
5886 if (TREE_CODE (arg) != TREE_CODE (parm))
5887 return 0;
5888
5889 switch (TREE_CODE (parm))
5890 {
5891 case TEMPLATE_DECL:
5892 /* We encounter instantiations of templates like
5893 template <template <template <class> class> class TT>
5894 class C; */
5895 {
5896 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5897 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5898
5899 if (!coerce_template_template_parms
5900 (parmparm, argparm, complain, in_decl, outer_args))
5901 return 0;
5902 }
5903 /* Fall through. */
5904
5905 case TYPE_DECL:
5906 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5907 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5908 /* Argument is a parameter pack but parameter is not. */
5909 return 0;
5910 break;
5911
5912 case PARM_DECL:
5913 /* The tsubst call is used to handle cases such as
5914
5915 template <int> class C {};
5916 template <class T, template <T> class TT> class D {};
5917 D<int, C> d;
5918
5919 i.e. the parameter list of TT depends on earlier parameters. */
5920 if (!uses_template_parms (TREE_TYPE (arg))
5921 && !same_type_p
5922 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5923 TREE_TYPE (arg)))
5924 return 0;
5925
5926 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5927 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5928 /* Argument is a parameter pack but parameter is not. */
5929 return 0;
5930
5931 break;
5932
5933 default:
5934 gcc_unreachable ();
5935 }
5936
5937 return 1;
5938 }
5939
5940
5941 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5942 template template parameters. Both PARM_PARMS and ARG_PARMS are
5943 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5944 or PARM_DECL.
5945
5946 Consider the example:
5947 template <class T> class A;
5948 template<template <class U> class TT> class B;
5949
5950 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5951 the parameters to A, and OUTER_ARGS contains A. */
5952
5953 static int
5954 coerce_template_template_parms (tree parm_parms,
5955 tree arg_parms,
5956 tsubst_flags_t complain,
5957 tree in_decl,
5958 tree outer_args)
5959 {
5960 int nparms, nargs, i;
5961 tree parm, arg;
5962 int variadic_p = 0;
5963
5964 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5965 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5966
5967 nparms = TREE_VEC_LENGTH (parm_parms);
5968 nargs = TREE_VEC_LENGTH (arg_parms);
5969
5970 /* Determine whether we have a parameter pack at the end of the
5971 template template parameter's template parameter list. */
5972 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5973 {
5974 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5975
5976 if (parm == error_mark_node)
5977 return 0;
5978
5979 switch (TREE_CODE (parm))
5980 {
5981 case TEMPLATE_DECL:
5982 case TYPE_DECL:
5983 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5984 variadic_p = 1;
5985 break;
5986
5987 case PARM_DECL:
5988 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5989 variadic_p = 1;
5990 break;
5991
5992 default:
5993 gcc_unreachable ();
5994 }
5995 }
5996
5997 if (nargs != nparms
5998 && !(variadic_p && nargs >= nparms - 1))
5999 return 0;
6000
6001 /* Check all of the template parameters except the parameter pack at
6002 the end (if any). */
6003 for (i = 0; i < nparms - variadic_p; ++i)
6004 {
6005 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6006 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6007 continue;
6008
6009 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6010 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6011
6012 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6013 outer_args))
6014 return 0;
6015
6016 }
6017
6018 if (variadic_p)
6019 {
6020 /* Check each of the template parameters in the template
6021 argument against the template parameter pack at the end of
6022 the template template parameter. */
6023 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6024 return 0;
6025
6026 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6027
6028 for (; i < nargs; ++i)
6029 {
6030 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6031 continue;
6032
6033 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6034
6035 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6036 outer_args))
6037 return 0;
6038 }
6039 }
6040
6041 return 1;
6042 }
6043
6044 /* Verifies that the deduced template arguments (in TARGS) for the
6045 template template parameters (in TPARMS) represent valid bindings,
6046 by comparing the template parameter list of each template argument
6047 to the template parameter list of its corresponding template
6048 template parameter, in accordance with DR150. This
6049 routine can only be called after all template arguments have been
6050 deduced. It will return TRUE if all of the template template
6051 parameter bindings are okay, FALSE otherwise. */
6052 bool
6053 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6054 {
6055 int i, ntparms = TREE_VEC_LENGTH (tparms);
6056 bool ret = true;
6057
6058 /* We're dealing with template parms in this process. */
6059 ++processing_template_decl;
6060
6061 targs = INNERMOST_TEMPLATE_ARGS (targs);
6062
6063 for (i = 0; i < ntparms; ++i)
6064 {
6065 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6066 tree targ = TREE_VEC_ELT (targs, i);
6067
6068 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6069 {
6070 tree packed_args = NULL_TREE;
6071 int idx, len = 1;
6072
6073 if (ARGUMENT_PACK_P (targ))
6074 {
6075 /* Look inside the argument pack. */
6076 packed_args = ARGUMENT_PACK_ARGS (targ);
6077 len = TREE_VEC_LENGTH (packed_args);
6078 }
6079
6080 for (idx = 0; idx < len; ++idx)
6081 {
6082 tree targ_parms = NULL_TREE;
6083
6084 if (packed_args)
6085 /* Extract the next argument from the argument
6086 pack. */
6087 targ = TREE_VEC_ELT (packed_args, idx);
6088
6089 if (PACK_EXPANSION_P (targ))
6090 /* Look at the pattern of the pack expansion. */
6091 targ = PACK_EXPANSION_PATTERN (targ);
6092
6093 /* Extract the template parameters from the template
6094 argument. */
6095 if (TREE_CODE (targ) == TEMPLATE_DECL)
6096 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6097 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6098 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6099
6100 /* Verify that we can coerce the template template
6101 parameters from the template argument to the template
6102 parameter. This requires an exact match. */
6103 if (targ_parms
6104 && !coerce_template_template_parms
6105 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6106 targ_parms,
6107 tf_none,
6108 tparm,
6109 targs))
6110 {
6111 ret = false;
6112 goto out;
6113 }
6114 }
6115 }
6116 }
6117
6118 out:
6119
6120 --processing_template_decl;
6121 return ret;
6122 }
6123
6124 /* Since type attributes aren't mangled, we need to strip them from
6125 template type arguments. */
6126
6127 static tree
6128 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6129 {
6130 tree mv;
6131 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6132 return arg;
6133 mv = TYPE_MAIN_VARIANT (arg);
6134 arg = strip_typedefs (arg);
6135 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6136 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6137 {
6138 if (complain & tf_warning)
6139 warning (0, "ignoring attributes on template argument %qT", arg);
6140 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6141 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6142 }
6143 return arg;
6144 }
6145
6146 /* Convert the indicated template ARG as necessary to match the
6147 indicated template PARM. Returns the converted ARG, or
6148 error_mark_node if the conversion was unsuccessful. Error and
6149 warning messages are issued under control of COMPLAIN. This
6150 conversion is for the Ith parameter in the parameter list. ARGS is
6151 the full set of template arguments deduced so far. */
6152
6153 static tree
6154 convert_template_argument (tree parm,
6155 tree arg,
6156 tree args,
6157 tsubst_flags_t complain,
6158 int i,
6159 tree in_decl)
6160 {
6161 tree orig_arg;
6162 tree val;
6163 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6164
6165 if (TREE_CODE (arg) == TREE_LIST
6166 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6167 {
6168 /* The template argument was the name of some
6169 member function. That's usually
6170 invalid, but static members are OK. In any
6171 case, grab the underlying fields/functions
6172 and issue an error later if required. */
6173 orig_arg = TREE_VALUE (arg);
6174 TREE_TYPE (arg) = unknown_type_node;
6175 }
6176
6177 orig_arg = arg;
6178
6179 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6180 requires_type = (TREE_CODE (parm) == TYPE_DECL
6181 || requires_tmpl_type);
6182
6183 /* When determining whether an argument pack expansion is a template,
6184 look at the pattern. */
6185 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6186 arg = PACK_EXPANSION_PATTERN (arg);
6187
6188 /* Deal with an injected-class-name used as a template template arg. */
6189 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6190 {
6191 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6192 if (TREE_CODE (t) == TEMPLATE_DECL)
6193 {
6194 if (cxx_dialect >= cxx0x)
6195 /* OK under DR 1004. */;
6196 else if (complain & tf_warning_or_error)
6197 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6198 " used as template template argument", TYPE_NAME (arg));
6199 else if (flag_pedantic_errors)
6200 t = arg;
6201
6202 arg = t;
6203 }
6204 }
6205
6206 is_tmpl_type =
6207 ((TREE_CODE (arg) == TEMPLATE_DECL
6208 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6209 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6210 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6211 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6212
6213 if (is_tmpl_type
6214 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6215 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6216 arg = TYPE_STUB_DECL (arg);
6217
6218 is_type = TYPE_P (arg) || is_tmpl_type;
6219
6220 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6221 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6222 {
6223 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6224 {
6225 if (complain & tf_error)
6226 error ("invalid use of destructor %qE as a type", orig_arg);
6227 return error_mark_node;
6228 }
6229
6230 permerror (input_location,
6231 "to refer to a type member of a template parameter, "
6232 "use %<typename %E%>", orig_arg);
6233
6234 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6235 TREE_OPERAND (arg, 1),
6236 typename_type,
6237 complain);
6238 arg = orig_arg;
6239 is_type = 1;
6240 }
6241 if (is_type != requires_type)
6242 {
6243 if (in_decl)
6244 {
6245 if (complain & tf_error)
6246 {
6247 error ("type/value mismatch at argument %d in template "
6248 "parameter list for %qD",
6249 i + 1, in_decl);
6250 if (is_type)
6251 error (" expected a constant of type %qT, got %qT",
6252 TREE_TYPE (parm),
6253 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6254 else if (requires_tmpl_type)
6255 error (" expected a class template, got %qE", orig_arg);
6256 else
6257 error (" expected a type, got %qE", orig_arg);
6258 }
6259 }
6260 return error_mark_node;
6261 }
6262 if (is_tmpl_type ^ requires_tmpl_type)
6263 {
6264 if (in_decl && (complain & tf_error))
6265 {
6266 error ("type/value mismatch at argument %d in template "
6267 "parameter list for %qD",
6268 i + 1, in_decl);
6269 if (is_tmpl_type)
6270 error (" expected a type, got %qT", DECL_NAME (arg));
6271 else
6272 error (" expected a class template, got %qT", orig_arg);
6273 }
6274 return error_mark_node;
6275 }
6276
6277 if (is_type)
6278 {
6279 if (requires_tmpl_type)
6280 {
6281 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6282 val = orig_arg;
6283 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6284 /* The number of argument required is not known yet.
6285 Just accept it for now. */
6286 val = TREE_TYPE (arg);
6287 else
6288 {
6289 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6290 tree argparm;
6291
6292 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6293
6294 if (coerce_template_template_parms (parmparm, argparm,
6295 complain, in_decl,
6296 args))
6297 {
6298 val = arg;
6299
6300 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6301 TEMPLATE_DECL. */
6302 if (val != error_mark_node)
6303 {
6304 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6305 val = TREE_TYPE (val);
6306 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6307 val = make_pack_expansion (val);
6308 }
6309 }
6310 else
6311 {
6312 if (in_decl && (complain & tf_error))
6313 {
6314 error ("type/value mismatch at argument %d in "
6315 "template parameter list for %qD",
6316 i + 1, in_decl);
6317 error (" expected a template of type %qD, got %qT",
6318 parm, orig_arg);
6319 }
6320
6321 val = error_mark_node;
6322 }
6323 }
6324 }
6325 else
6326 val = orig_arg;
6327 /* We only form one instance of each template specialization.
6328 Therefore, if we use a non-canonical variant (i.e., a
6329 typedef), any future messages referring to the type will use
6330 the typedef, which is confusing if those future uses do not
6331 themselves also use the typedef. */
6332 if (TYPE_P (val))
6333 val = canonicalize_type_argument (val, complain);
6334 }
6335 else
6336 {
6337 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6338
6339 if (invalid_nontype_parm_type_p (t, complain))
6340 return error_mark_node;
6341
6342 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6343 {
6344 if (same_type_p (t, TREE_TYPE (orig_arg)))
6345 val = orig_arg;
6346 else
6347 {
6348 /* Not sure if this is reachable, but it doesn't hurt
6349 to be robust. */
6350 error ("type mismatch in nontype parameter pack");
6351 val = error_mark_node;
6352 }
6353 }
6354 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6355 /* We used to call digest_init here. However, digest_init
6356 will report errors, which we don't want when complain
6357 is zero. More importantly, digest_init will try too
6358 hard to convert things: for example, `0' should not be
6359 converted to pointer type at this point according to
6360 the standard. Accepting this is not merely an
6361 extension, since deciding whether or not these
6362 conversions can occur is part of determining which
6363 function template to call, or whether a given explicit
6364 argument specification is valid. */
6365 val = convert_nontype_argument (t, orig_arg, complain);
6366 else
6367 val = strip_typedefs_expr (orig_arg);
6368
6369 if (val == NULL_TREE)
6370 val = error_mark_node;
6371 else if (val == error_mark_node && (complain & tf_error))
6372 error ("could not convert template argument %qE to %qT", orig_arg, t);
6373
6374 if (TREE_CODE (val) == SCOPE_REF)
6375 {
6376 /* Strip typedefs from the SCOPE_REF. */
6377 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6378 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6379 complain);
6380 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6381 QUALIFIED_NAME_IS_TEMPLATE (val));
6382 }
6383 }
6384
6385 return val;
6386 }
6387
6388 /* Coerces the remaining template arguments in INNER_ARGS (from
6389 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6390 Returns the coerced argument pack. PARM_IDX is the position of this
6391 parameter in the template parameter list. ARGS is the original
6392 template argument list. */
6393 static tree
6394 coerce_template_parameter_pack (tree parms,
6395 int parm_idx,
6396 tree args,
6397 tree inner_args,
6398 int arg_idx,
6399 tree new_args,
6400 int* lost,
6401 tree in_decl,
6402 tsubst_flags_t complain)
6403 {
6404 tree parm = TREE_VEC_ELT (parms, parm_idx);
6405 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6406 tree packed_args;
6407 tree argument_pack;
6408 tree packed_types = NULL_TREE;
6409
6410 if (arg_idx > nargs)
6411 arg_idx = nargs;
6412
6413 packed_args = make_tree_vec (nargs - arg_idx);
6414
6415 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6416 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6417 {
6418 /* When the template parameter is a non-type template
6419 parameter pack whose type uses parameter packs, we need
6420 to look at each of the template arguments
6421 separately. Build a vector of the types for these
6422 non-type template parameters in PACKED_TYPES. */
6423 tree expansion
6424 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6425 packed_types = tsubst_pack_expansion (expansion, args,
6426 complain, in_decl);
6427
6428 if (packed_types == error_mark_node)
6429 return error_mark_node;
6430
6431 /* Check that we have the right number of arguments. */
6432 if (arg_idx < nargs
6433 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6434 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6435 {
6436 int needed_parms
6437 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6438 error ("wrong number of template arguments (%d, should be %d)",
6439 nargs, needed_parms);
6440 return error_mark_node;
6441 }
6442
6443 /* If we aren't able to check the actual arguments now
6444 (because they haven't been expanded yet), we can at least
6445 verify that all of the types used for the non-type
6446 template parameter pack are, in fact, valid for non-type
6447 template parameters. */
6448 if (arg_idx < nargs
6449 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6450 {
6451 int j, len = TREE_VEC_LENGTH (packed_types);
6452 for (j = 0; j < len; ++j)
6453 {
6454 tree t = TREE_VEC_ELT (packed_types, j);
6455 if (invalid_nontype_parm_type_p (t, complain))
6456 return error_mark_node;
6457 }
6458 }
6459 }
6460
6461 /* Convert the remaining arguments, which will be a part of the
6462 parameter pack "parm". */
6463 for (; arg_idx < nargs; ++arg_idx)
6464 {
6465 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6466 tree actual_parm = TREE_VALUE (parm);
6467
6468 if (packed_types && !PACK_EXPANSION_P (arg))
6469 {
6470 /* When we have a vector of types (corresponding to the
6471 non-type template parameter pack that uses parameter
6472 packs in its type, as mention above), and the
6473 argument is not an expansion (which expands to a
6474 currently unknown number of arguments), clone the
6475 parm and give it the next type in PACKED_TYPES. */
6476 actual_parm = copy_node (actual_parm);
6477 TREE_TYPE (actual_parm) =
6478 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6479 }
6480
6481 if (arg == error_mark_node)
6482 {
6483 if (complain & tf_error)
6484 error ("template argument %d is invalid", arg_idx + 1);
6485 }
6486 else
6487 arg = convert_template_argument (actual_parm,
6488 arg, new_args, complain, parm_idx,
6489 in_decl);
6490 if (arg == error_mark_node)
6491 (*lost)++;
6492 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6493 }
6494
6495 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6496 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6497 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6498 else
6499 {
6500 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6501 TREE_TYPE (argument_pack)
6502 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6503 TREE_CONSTANT (argument_pack) = 1;
6504 }
6505
6506 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6507 #ifdef ENABLE_CHECKING
6508 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6509 TREE_VEC_LENGTH (packed_args));
6510 #endif
6511 return argument_pack;
6512 }
6513
6514 /* Returns true if the template argument vector ARGS contains
6515 any pack expansions, false otherwise. */
6516
6517 static bool
6518 any_pack_expanson_args_p (tree args)
6519 {
6520 int i;
6521 if (args)
6522 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6523 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6524 return true;
6525 return false;
6526 }
6527
6528 /* Convert all template arguments to their appropriate types, and
6529 return a vector containing the innermost resulting template
6530 arguments. If any error occurs, return error_mark_node. Error and
6531 warning messages are issued under control of COMPLAIN.
6532
6533 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6534 for arguments not specified in ARGS. Otherwise, if
6535 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6536 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6537 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6538 ARGS. */
6539
6540 static tree
6541 coerce_template_parms (tree parms,
6542 tree args,
6543 tree in_decl,
6544 tsubst_flags_t complain,
6545 bool require_all_args,
6546 bool use_default_args)
6547 {
6548 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6549 tree inner_args;
6550 tree new_args;
6551 tree new_inner_args;
6552 int saved_unevaluated_operand;
6553 int saved_inhibit_evaluation_warnings;
6554
6555 /* When used as a boolean value, indicates whether this is a
6556 variadic template parameter list. Since it's an int, we can also
6557 subtract it from nparms to get the number of non-variadic
6558 parameters. */
6559 int variadic_p = 0;
6560 int post_variadic_parms = 0;
6561
6562 if (args == error_mark_node)
6563 return error_mark_node;
6564
6565 nparms = TREE_VEC_LENGTH (parms);
6566
6567 /* Determine if there are any parameter packs. */
6568 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6569 {
6570 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6571 if (variadic_p)
6572 ++post_variadic_parms;
6573 if (template_parameter_pack_p (tparm))
6574 ++variadic_p;
6575 }
6576
6577 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6578 /* If there are no parameters that follow a parameter pack, we need to
6579 expand any argument packs so that we can deduce a parameter pack from
6580 some non-packed args followed by an argument pack, as in variadic85.C.
6581 If there are such parameters, we need to leave argument packs intact
6582 so the arguments are assigned properly. This can happen when dealing
6583 with a nested class inside a partial specialization of a class
6584 template, as in variadic92.C, or when deducing a template parameter pack
6585 from a sub-declarator, as in variadic114.C. */
6586 if (!post_variadic_parms)
6587 inner_args = expand_template_argument_pack (inner_args);
6588
6589 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6590 if ((nargs > nparms && !variadic_p)
6591 || (nargs < nparms - variadic_p
6592 && require_all_args
6593 && !any_pack_expanson_args_p (inner_args)
6594 && (!use_default_args
6595 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6596 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6597 {
6598 if (complain & tf_error)
6599 {
6600 if (variadic_p)
6601 {
6602 nparms -= variadic_p;
6603 error ("wrong number of template arguments "
6604 "(%d, should be %d or more)", nargs, nparms);
6605 }
6606 else
6607 error ("wrong number of template arguments "
6608 "(%d, should be %d)", nargs, nparms);
6609
6610 if (in_decl)
6611 error ("provided for %q+D", in_decl);
6612 }
6613
6614 return error_mark_node;
6615 }
6616
6617 /* We need to evaluate the template arguments, even though this
6618 template-id may be nested within a "sizeof". */
6619 saved_unevaluated_operand = cp_unevaluated_operand;
6620 cp_unevaluated_operand = 0;
6621 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6622 c_inhibit_evaluation_warnings = 0;
6623 new_inner_args = make_tree_vec (nparms);
6624 new_args = add_outermost_template_args (args, new_inner_args);
6625 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6626 {
6627 tree arg;
6628 tree parm;
6629
6630 /* Get the Ith template parameter. */
6631 parm = TREE_VEC_ELT (parms, parm_idx);
6632
6633 if (parm == error_mark_node)
6634 {
6635 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6636 continue;
6637 }
6638
6639 /* Calculate the next argument. */
6640 if (arg_idx < nargs)
6641 arg = TREE_VEC_ELT (inner_args, arg_idx);
6642 else
6643 arg = NULL_TREE;
6644
6645 if (template_parameter_pack_p (TREE_VALUE (parm))
6646 && !(arg && ARGUMENT_PACK_P (arg)))
6647 {
6648 /* All remaining arguments will be placed in the
6649 template parameter pack PARM. */
6650 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6651 inner_args, arg_idx,
6652 new_args, &lost,
6653 in_decl, complain);
6654
6655 /* Store this argument. */
6656 if (arg == error_mark_node)
6657 lost++;
6658 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6659
6660 /* We are done with all of the arguments. */
6661 arg_idx = nargs;
6662
6663 continue;
6664 }
6665 else if (arg)
6666 {
6667 if (PACK_EXPANSION_P (arg))
6668 {
6669 /* We don't know how many args we have yet, just
6670 use the unconverted ones for now. */
6671 new_inner_args = inner_args;
6672 break;
6673 }
6674 }
6675 else if (require_all_args)
6676 {
6677 /* There must be a default arg in this case. */
6678 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6679 complain, in_decl);
6680 /* The position of the first default template argument,
6681 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6682 Record that. */
6683 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6684 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6685 }
6686 else
6687 break;
6688
6689 if (arg == error_mark_node)
6690 {
6691 if (complain & tf_error)
6692 error ("template argument %d is invalid", arg_idx + 1);
6693 }
6694 else if (!arg)
6695 /* This only occurs if there was an error in the template
6696 parameter list itself (which we would already have
6697 reported) that we are trying to recover from, e.g., a class
6698 template with a parameter list such as
6699 template<typename..., typename>. */
6700 ++lost;
6701 else
6702 arg = convert_template_argument (TREE_VALUE (parm),
6703 arg, new_args, complain,
6704 parm_idx, in_decl);
6705
6706 if (arg == error_mark_node)
6707 lost++;
6708 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6709 }
6710 cp_unevaluated_operand = saved_unevaluated_operand;
6711 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6712
6713 if (lost)
6714 return error_mark_node;
6715
6716 #ifdef ENABLE_CHECKING
6717 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6718 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6719 TREE_VEC_LENGTH (new_inner_args));
6720 #endif
6721
6722 return new_inner_args;
6723 }
6724
6725 /* Like coerce_template_parms. If PARMS represents all template
6726 parameters levels, this function returns a vector of vectors
6727 representing all the resulting argument levels. Note that in this
6728 case, only the innermost arguments are coerced because the
6729 outermost ones are supposed to have been coerced already.
6730
6731 Otherwise, if PARMS represents only (the innermost) vector of
6732 parameters, this function returns a vector containing just the
6733 innermost resulting arguments. */
6734
6735 static tree
6736 coerce_innermost_template_parms (tree parms,
6737 tree args,
6738 tree in_decl,
6739 tsubst_flags_t complain,
6740 bool require_all_args,
6741 bool use_default_args)
6742 {
6743 int parms_depth = TMPL_PARMS_DEPTH (parms);
6744 int args_depth = TMPL_ARGS_DEPTH (args);
6745 tree coerced_args;
6746
6747 if (parms_depth > 1)
6748 {
6749 coerced_args = make_tree_vec (parms_depth);
6750 tree level;
6751 int cur_depth;
6752
6753 for (level = parms, cur_depth = parms_depth;
6754 parms_depth > 0 && level != NULL_TREE;
6755 level = TREE_CHAIN (level), --cur_depth)
6756 {
6757 tree l;
6758 if (cur_depth == args_depth)
6759 l = coerce_template_parms (TREE_VALUE (level),
6760 args, in_decl, complain,
6761 require_all_args,
6762 use_default_args);
6763 else
6764 l = TMPL_ARGS_LEVEL (args, cur_depth);
6765
6766 if (l == error_mark_node)
6767 return error_mark_node;
6768
6769 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6770 }
6771 }
6772 else
6773 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6774 args, in_decl, complain,
6775 require_all_args,
6776 use_default_args);
6777 return coerced_args;
6778 }
6779
6780 /* Returns 1 if template args OT and NT are equivalent. */
6781
6782 static int
6783 template_args_equal (tree ot, tree nt)
6784 {
6785 if (nt == ot)
6786 return 1;
6787 if (nt == NULL_TREE || ot == NULL_TREE)
6788 return false;
6789
6790 if (TREE_CODE (nt) == TREE_VEC)
6791 /* For member templates */
6792 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6793 else if (PACK_EXPANSION_P (ot))
6794 return (PACK_EXPANSION_P (nt)
6795 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6796 PACK_EXPANSION_PATTERN (nt))
6797 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6798 PACK_EXPANSION_EXTRA_ARGS (nt)));
6799 else if (ARGUMENT_PACK_P (ot))
6800 {
6801 int i, len;
6802 tree opack, npack;
6803
6804 if (!ARGUMENT_PACK_P (nt))
6805 return 0;
6806
6807 opack = ARGUMENT_PACK_ARGS (ot);
6808 npack = ARGUMENT_PACK_ARGS (nt);
6809 len = TREE_VEC_LENGTH (opack);
6810 if (TREE_VEC_LENGTH (npack) != len)
6811 return 0;
6812 for (i = 0; i < len; ++i)
6813 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6814 TREE_VEC_ELT (npack, i)))
6815 return 0;
6816 return 1;
6817 }
6818 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6819 {
6820 /* We get here probably because we are in the middle of substituting
6821 into the pattern of a pack expansion. In that case the
6822 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6823 interested in. So we want to use the initial pack argument for
6824 the comparison. */
6825 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6826 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6827 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6828 return template_args_equal (ot, nt);
6829 }
6830 else if (TYPE_P (nt))
6831 return TYPE_P (ot) && same_type_p (ot, nt);
6832 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6833 return 0;
6834 else
6835 return cp_tree_equal (ot, nt);
6836 }
6837
6838 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6839 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6840 NEWARG_PTR with the offending arguments if they are non-NULL. */
6841
6842 static int
6843 comp_template_args_with_info (tree oldargs, tree newargs,
6844 tree *oldarg_ptr, tree *newarg_ptr)
6845 {
6846 int i;
6847
6848 if (oldargs == newargs)
6849 return 1;
6850
6851 if (!oldargs || !newargs)
6852 return 0;
6853
6854 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6855 return 0;
6856
6857 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6858 {
6859 tree nt = TREE_VEC_ELT (newargs, i);
6860 tree ot = TREE_VEC_ELT (oldargs, i);
6861
6862 if (! template_args_equal (ot, nt))
6863 {
6864 if (oldarg_ptr != NULL)
6865 *oldarg_ptr = ot;
6866 if (newarg_ptr != NULL)
6867 *newarg_ptr = nt;
6868 return 0;
6869 }
6870 }
6871 return 1;
6872 }
6873
6874 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6875 of template arguments. Returns 0 otherwise. */
6876
6877 int
6878 comp_template_args (tree oldargs, tree newargs)
6879 {
6880 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6881 }
6882
6883 static void
6884 add_pending_template (tree d)
6885 {
6886 tree ti = (TYPE_P (d)
6887 ? CLASSTYPE_TEMPLATE_INFO (d)
6888 : DECL_TEMPLATE_INFO (d));
6889 struct pending_template *pt;
6890 int level;
6891
6892 if (TI_PENDING_TEMPLATE_FLAG (ti))
6893 return;
6894
6895 /* We are called both from instantiate_decl, where we've already had a
6896 tinst_level pushed, and instantiate_template, where we haven't.
6897 Compensate. */
6898 level = !current_tinst_level || current_tinst_level->decl != d;
6899
6900 if (level)
6901 push_tinst_level (d);
6902
6903 pt = ggc_alloc_pending_template ();
6904 pt->next = NULL;
6905 pt->tinst = current_tinst_level;
6906 if (last_pending_template)
6907 last_pending_template->next = pt;
6908 else
6909 pending_templates = pt;
6910
6911 last_pending_template = pt;
6912
6913 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6914
6915 if (level)
6916 pop_tinst_level ();
6917 }
6918
6919
6920 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6921 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6922 documentation for TEMPLATE_ID_EXPR. */
6923
6924 tree
6925 lookup_template_function (tree fns, tree arglist)
6926 {
6927 tree type;
6928
6929 if (fns == error_mark_node || arglist == error_mark_node)
6930 return error_mark_node;
6931
6932 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6933
6934 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6935 {
6936 error ("%q#D is not a function template", fns);
6937 return error_mark_node;
6938 }
6939
6940 if (BASELINK_P (fns))
6941 {
6942 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6943 unknown_type_node,
6944 BASELINK_FUNCTIONS (fns),
6945 arglist);
6946 return fns;
6947 }
6948
6949 type = TREE_TYPE (fns);
6950 if (TREE_CODE (fns) == OVERLOAD || !type)
6951 type = unknown_type_node;
6952
6953 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6954 }
6955
6956 /* Within the scope of a template class S<T>, the name S gets bound
6957 (in build_self_reference) to a TYPE_DECL for the class, not a
6958 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6959 or one of its enclosing classes, and that type is a template,
6960 return the associated TEMPLATE_DECL. Otherwise, the original
6961 DECL is returned.
6962
6963 Also handle the case when DECL is a TREE_LIST of ambiguous
6964 injected-class-names from different bases. */
6965
6966 tree
6967 maybe_get_template_decl_from_type_decl (tree decl)
6968 {
6969 if (decl == NULL_TREE)
6970 return decl;
6971
6972 /* DR 176: A lookup that finds an injected-class-name (10.2
6973 [class.member.lookup]) can result in an ambiguity in certain cases
6974 (for example, if it is found in more than one base class). If all of
6975 the injected-class-names that are found refer to specializations of
6976 the same class template, and if the name is followed by a
6977 template-argument-list, the reference refers to the class template
6978 itself and not a specialization thereof, and is not ambiguous. */
6979 if (TREE_CODE (decl) == TREE_LIST)
6980 {
6981 tree t, tmpl = NULL_TREE;
6982 for (t = decl; t; t = TREE_CHAIN (t))
6983 {
6984 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6985 if (!tmpl)
6986 tmpl = elt;
6987 else if (tmpl != elt)
6988 break;
6989 }
6990 if (tmpl && t == NULL_TREE)
6991 return tmpl;
6992 else
6993 return decl;
6994 }
6995
6996 return (decl != NULL_TREE
6997 && DECL_SELF_REFERENCE_P (decl)
6998 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6999 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7000 }
7001
7002 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7003 parameters, find the desired type.
7004
7005 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7006
7007 IN_DECL, if non-NULL, is the template declaration we are trying to
7008 instantiate.
7009
7010 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7011 the class we are looking up.
7012
7013 Issue error and warning messages under control of COMPLAIN.
7014
7015 If the template class is really a local class in a template
7016 function, then the FUNCTION_CONTEXT is the function in which it is
7017 being instantiated.
7018
7019 ??? Note that this function is currently called *twice* for each
7020 template-id: the first time from the parser, while creating the
7021 incomplete type (finish_template_type), and the second type during the
7022 real instantiation (instantiate_template_class). This is surely something
7023 that we want to avoid. It also causes some problems with argument
7024 coercion (see convert_nontype_argument for more information on this). */
7025
7026 static tree
7027 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7028 int entering_scope, tsubst_flags_t complain)
7029 {
7030 tree templ = NULL_TREE, parmlist;
7031 tree t;
7032 void **slot;
7033 spec_entry *entry;
7034 spec_entry elt;
7035 hashval_t hash;
7036
7037 if (TREE_CODE (d1) == IDENTIFIER_NODE)
7038 {
7039 tree value = innermost_non_namespace_value (d1);
7040 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7041 templ = value;
7042 else
7043 {
7044 if (context)
7045 push_decl_namespace (context);
7046 templ = lookup_name (d1);
7047 templ = maybe_get_template_decl_from_type_decl (templ);
7048 if (context)
7049 pop_decl_namespace ();
7050 }
7051 if (templ)
7052 context = DECL_CONTEXT (templ);
7053 }
7054 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7055 {
7056 tree type = TREE_TYPE (d1);
7057
7058 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7059 an implicit typename for the second A. Deal with it. */
7060 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7061 type = TREE_TYPE (type);
7062
7063 if (CLASSTYPE_TEMPLATE_INFO (type))
7064 {
7065 templ = CLASSTYPE_TI_TEMPLATE (type);
7066 d1 = DECL_NAME (templ);
7067 }
7068 }
7069 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7070 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7071 {
7072 templ = TYPE_TI_TEMPLATE (d1);
7073 d1 = DECL_NAME (templ);
7074 }
7075 else if (TREE_CODE (d1) == TEMPLATE_DECL
7076 && DECL_TEMPLATE_RESULT (d1)
7077 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7078 {
7079 templ = d1;
7080 d1 = DECL_NAME (templ);
7081 context = DECL_CONTEXT (templ);
7082 }
7083
7084 /* Issue an error message if we didn't find a template. */
7085 if (! templ)
7086 {
7087 if (complain & tf_error)
7088 error ("%qT is not a template", d1);
7089 return error_mark_node;
7090 }
7091
7092 if (TREE_CODE (templ) != TEMPLATE_DECL
7093 /* Make sure it's a user visible template, if it was named by
7094 the user. */
7095 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7096 && !PRIMARY_TEMPLATE_P (templ)))
7097 {
7098 if (complain & tf_error)
7099 {
7100 error ("non-template type %qT used as a template", d1);
7101 if (in_decl)
7102 error ("for template declaration %q+D", in_decl);
7103 }
7104 return error_mark_node;
7105 }
7106
7107 complain &= ~tf_user;
7108
7109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7110 {
7111 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7112 template arguments */
7113
7114 tree parm;
7115 tree arglist2;
7116 tree outer;
7117
7118 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7119
7120 /* Consider an example where a template template parameter declared as
7121
7122 template <class T, class U = std::allocator<T> > class TT
7123
7124 The template parameter level of T and U are one level larger than
7125 of TT. To proper process the default argument of U, say when an
7126 instantiation `TT<int>' is seen, we need to build the full
7127 arguments containing {int} as the innermost level. Outer levels,
7128 available when not appearing as default template argument, can be
7129 obtained from the arguments of the enclosing template.
7130
7131 Suppose that TT is later substituted with std::vector. The above
7132 instantiation is `TT<int, std::allocator<T> >' with TT at
7133 level 1, and T at level 2, while the template arguments at level 1
7134 becomes {std::vector} and the inner level 2 is {int}. */
7135
7136 outer = DECL_CONTEXT (templ);
7137 if (outer)
7138 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7139 else if (current_template_parms)
7140 /* This is an argument of the current template, so we haven't set
7141 DECL_CONTEXT yet. */
7142 outer = current_template_args ();
7143
7144 if (outer)
7145 arglist = add_to_template_args (outer, arglist);
7146
7147 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7148 complain,
7149 /*require_all_args=*/true,
7150 /*use_default_args=*/true);
7151 if (arglist2 == error_mark_node
7152 || (!uses_template_parms (arglist2)
7153 && check_instantiated_args (templ, arglist2, complain)))
7154 return error_mark_node;
7155
7156 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7157 return parm;
7158 }
7159 else
7160 {
7161 tree template_type = TREE_TYPE (templ);
7162 tree gen_tmpl;
7163 tree type_decl;
7164 tree found = NULL_TREE;
7165 int arg_depth;
7166 int parm_depth;
7167 int is_dependent_type;
7168 int use_partial_inst_tmpl = false;
7169
7170 if (template_type == error_mark_node)
7171 /* An error occured while building the template TEMPL, and a
7172 diagnostic has most certainly been emitted for that
7173 already. Let's propagate that error. */
7174 return error_mark_node;
7175
7176 gen_tmpl = most_general_template (templ);
7177 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7178 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7179 arg_depth = TMPL_ARGS_DEPTH (arglist);
7180
7181 if (arg_depth == 1 && parm_depth > 1)
7182 {
7183 /* We've been given an incomplete set of template arguments.
7184 For example, given:
7185
7186 template <class T> struct S1 {
7187 template <class U> struct S2 {};
7188 template <class U> struct S2<U*> {};
7189 };
7190
7191 we will be called with an ARGLIST of `U*', but the
7192 TEMPLATE will be `template <class T> template
7193 <class U> struct S1<T>::S2'. We must fill in the missing
7194 arguments. */
7195 arglist
7196 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7197 arglist);
7198 arg_depth = TMPL_ARGS_DEPTH (arglist);
7199 }
7200
7201 /* Now we should have enough arguments. */
7202 gcc_assert (parm_depth == arg_depth);
7203
7204 /* From here on, we're only interested in the most general
7205 template. */
7206
7207 /* Calculate the BOUND_ARGS. These will be the args that are
7208 actually tsubst'd into the definition to create the
7209 instantiation. */
7210 if (parm_depth > 1)
7211 {
7212 /* We have multiple levels of arguments to coerce, at once. */
7213 int i;
7214 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7215
7216 tree bound_args = make_tree_vec (parm_depth);
7217
7218 for (i = saved_depth,
7219 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7220 i > 0 && t != NULL_TREE;
7221 --i, t = TREE_CHAIN (t))
7222 {
7223 tree a;
7224 if (i == saved_depth)
7225 a = coerce_template_parms (TREE_VALUE (t),
7226 arglist, gen_tmpl,
7227 complain,
7228 /*require_all_args=*/true,
7229 /*use_default_args=*/true);
7230 else
7231 /* Outer levels should have already been coerced. */
7232 a = TMPL_ARGS_LEVEL (arglist, i);
7233
7234 /* Don't process further if one of the levels fails. */
7235 if (a == error_mark_node)
7236 {
7237 /* Restore the ARGLIST to its full size. */
7238 TREE_VEC_LENGTH (arglist) = saved_depth;
7239 return error_mark_node;
7240 }
7241
7242 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7243
7244 /* We temporarily reduce the length of the ARGLIST so
7245 that coerce_template_parms will see only the arguments
7246 corresponding to the template parameters it is
7247 examining. */
7248 TREE_VEC_LENGTH (arglist)--;
7249 }
7250
7251 /* Restore the ARGLIST to its full size. */
7252 TREE_VEC_LENGTH (arglist) = saved_depth;
7253
7254 arglist = bound_args;
7255 }
7256 else
7257 arglist
7258 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7259 INNERMOST_TEMPLATE_ARGS (arglist),
7260 gen_tmpl,
7261 complain,
7262 /*require_all_args=*/true,
7263 /*use_default_args=*/true);
7264
7265 if (arglist == error_mark_node)
7266 /* We were unable to bind the arguments. */
7267 return error_mark_node;
7268
7269 /* In the scope of a template class, explicit references to the
7270 template class refer to the type of the template, not any
7271 instantiation of it. For example, in:
7272
7273 template <class T> class C { void f(C<T>); }
7274
7275 the `C<T>' is just the same as `C'. Outside of the
7276 class, however, such a reference is an instantiation. */
7277 if ((entering_scope
7278 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7279 || currently_open_class (template_type))
7280 /* comp_template_args is expensive, check it last. */
7281 && comp_template_args (TYPE_TI_ARGS (template_type),
7282 arglist))
7283 return template_type;
7284
7285 /* If we already have this specialization, return it. */
7286 elt.tmpl = gen_tmpl;
7287 elt.args = arglist;
7288 hash = hash_specialization (&elt);
7289 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7290 &elt, hash);
7291
7292 if (entry)
7293 return entry->spec;
7294
7295 is_dependent_type = uses_template_parms (arglist);
7296
7297 /* If the deduced arguments are invalid, then the binding
7298 failed. */
7299 if (!is_dependent_type
7300 && check_instantiated_args (gen_tmpl,
7301 INNERMOST_TEMPLATE_ARGS (arglist),
7302 complain))
7303 return error_mark_node;
7304
7305 if (!is_dependent_type
7306 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7307 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7308 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7309 {
7310 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7311 DECL_NAME (gen_tmpl),
7312 /*tag_scope=*/ts_global);
7313 return found;
7314 }
7315
7316 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7317 complain, in_decl);
7318 if (context == error_mark_node)
7319 return error_mark_node;
7320
7321 if (!context)
7322 context = global_namespace;
7323
7324 /* Create the type. */
7325 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7326 {
7327 if (!is_dependent_type)
7328 {
7329 set_current_access_from_decl (TYPE_NAME (template_type));
7330 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7331 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7332 arglist, complain, in_decl),
7333 SCOPED_ENUM_P (template_type), NULL);
7334 }
7335 else
7336 {
7337 /* We don't want to call start_enum for this type, since
7338 the values for the enumeration constants may involve
7339 template parameters. And, no one should be interested
7340 in the enumeration constants for such a type. */
7341 t = cxx_make_type (ENUMERAL_TYPE);
7342 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7343 }
7344 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7345 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7346 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7347 }
7348 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7349 {
7350 /* The user referred to a specialization of an alias
7351 template represented by GEN_TMPL.
7352
7353 [temp.alias]/2 says:
7354
7355 When a template-id refers to the specialization of an
7356 alias template, it is equivalent to the associated
7357 type obtained by substitution of its
7358 template-arguments for the template-parameters in the
7359 type-id of the alias template. */
7360
7361 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7362 /* Note that the call above (by indirectly calling
7363 register_specialization in tsubst_decl) registers the
7364 TYPE_DECL representing the specialization of the alias
7365 template. So next time someone substitutes ARGLIST for
7366 the template parms into the alias template (GEN_TMPL),
7367 she'll get that TYPE_DECL back. */
7368
7369 if (t == error_mark_node)
7370 return t;
7371 }
7372 else if (CLASS_TYPE_P (template_type))
7373 {
7374 t = make_class_type (TREE_CODE (template_type));
7375 CLASSTYPE_DECLARED_CLASS (t)
7376 = CLASSTYPE_DECLARED_CLASS (template_type);
7377 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7378 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7379
7380 /* A local class. Make sure the decl gets registered properly. */
7381 if (context == current_function_decl)
7382 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7383
7384 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7385 /* This instantiation is another name for the primary
7386 template type. Set the TYPE_CANONICAL field
7387 appropriately. */
7388 TYPE_CANONICAL (t) = template_type;
7389 else if (any_template_arguments_need_structural_equality_p (arglist))
7390 /* Some of the template arguments require structural
7391 equality testing, so this template class requires
7392 structural equality testing. */
7393 SET_TYPE_STRUCTURAL_EQUALITY (t);
7394 }
7395 else
7396 gcc_unreachable ();
7397
7398 /* If we called start_enum or pushtag above, this information
7399 will already be set up. */
7400 if (!TYPE_NAME (t))
7401 {
7402 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7403
7404 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7405 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7406 DECL_SOURCE_LOCATION (type_decl)
7407 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7408 }
7409 else
7410 type_decl = TYPE_NAME (t);
7411
7412 if (CLASS_TYPE_P (template_type))
7413 {
7414 TREE_PRIVATE (type_decl)
7415 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7416 TREE_PROTECTED (type_decl)
7417 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7418 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7419 {
7420 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7421 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7422 }
7423 }
7424
7425 /* Let's consider the explicit specialization of a member
7426 of a class template specialization that is implicitely instantiated,
7427 e.g.:
7428 template<class T>
7429 struct S
7430 {
7431 template<class U> struct M {}; //#0
7432 };
7433
7434 template<>
7435 template<>
7436 struct S<int>::M<char> //#1
7437 {
7438 int i;
7439 };
7440 [temp.expl.spec]/4 says this is valid.
7441
7442 In this case, when we write:
7443 S<int>::M<char> m;
7444
7445 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7446 the one of #0.
7447
7448 When we encounter #1, we want to store the partial instantiation
7449 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7450
7451 For all cases other than this "explicit specialization of member of a
7452 class template", we just want to store the most general template into
7453 the CLASSTYPE_TI_TEMPLATE of M.
7454
7455 This case of "explicit specialization of member of a class template"
7456 only happens when:
7457 1/ the enclosing class is an instantiation of, and therefore not
7458 the same as, the context of the most general template, and
7459 2/ we aren't looking at the partial instantiation itself, i.e.
7460 the innermost arguments are not the same as the innermost parms of
7461 the most general template.
7462
7463 So it's only when 1/ and 2/ happens that we want to use the partial
7464 instantiation of the member template in lieu of its most general
7465 template. */
7466
7467 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7468 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7469 /* the enclosing class must be an instantiation... */
7470 && CLASS_TYPE_P (context)
7471 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7472 {
7473 tree partial_inst_args;
7474 TREE_VEC_LENGTH (arglist)--;
7475 ++processing_template_decl;
7476 partial_inst_args =
7477 tsubst (INNERMOST_TEMPLATE_ARGS
7478 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7479 arglist, complain, NULL_TREE);
7480 --processing_template_decl;
7481 TREE_VEC_LENGTH (arglist)++;
7482 use_partial_inst_tmpl =
7483 /*...and we must not be looking at the partial instantiation
7484 itself. */
7485 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7486 partial_inst_args);
7487 }
7488
7489 if (!use_partial_inst_tmpl)
7490 /* This case is easy; there are no member templates involved. */
7491 found = gen_tmpl;
7492 else
7493 {
7494 /* This is a full instantiation of a member template. Find
7495 the partial instantiation of which this is an instance. */
7496
7497 /* Temporarily reduce by one the number of levels in the ARGLIST
7498 so as to avoid comparing the last set of arguments. */
7499 TREE_VEC_LENGTH (arglist)--;
7500 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7501 TREE_VEC_LENGTH (arglist)++;
7502 /* FOUND is either a proper class type, or an alias
7503 template specialization. In the later case, it's a
7504 TYPE_DECL, resulting from the substituting of arguments
7505 for parameters in the TYPE_DECL of the alias template
7506 done earlier. So be careful while getting the template
7507 of FOUND. */
7508 found = TREE_CODE (found) == TYPE_DECL
7509 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7510 : CLASSTYPE_TI_TEMPLATE (found);
7511 }
7512
7513 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7514
7515 elt.spec = t;
7516 slot = htab_find_slot_with_hash (type_specializations,
7517 &elt, hash, INSERT);
7518 entry = ggc_alloc_spec_entry ();
7519 *entry = elt;
7520 *slot = entry;
7521
7522 /* Note this use of the partial instantiation so we can check it
7523 later in maybe_process_partial_specialization. */
7524 DECL_TEMPLATE_INSTANTIATIONS (templ)
7525 = tree_cons (arglist, t,
7526 DECL_TEMPLATE_INSTANTIATIONS (templ));
7527
7528 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7529 /* Now that the type has been registered on the instantiations
7530 list, we set up the enumerators. Because the enumeration
7531 constants may involve the enumeration type itself, we make
7532 sure to register the type first, and then create the
7533 constants. That way, doing tsubst_expr for the enumeration
7534 constants won't result in recursive calls here; we'll find
7535 the instantiation and exit above. */
7536 tsubst_enum (template_type, t, arglist);
7537
7538 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7539 /* If the type makes use of template parameters, the
7540 code that generates debugging information will crash. */
7541 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7542
7543 /* Possibly limit visibility based on template args. */
7544 TREE_PUBLIC (type_decl) = 1;
7545 determine_visibility (type_decl);
7546
7547 return t;
7548 }
7549 }
7550
7551 /* Wrapper for lookup_template_class_1. */
7552
7553 tree
7554 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7555 int entering_scope, tsubst_flags_t complain)
7556 {
7557 tree ret;
7558 timevar_push (TV_TEMPLATE_INST);
7559 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7560 entering_scope, complain);
7561 timevar_pop (TV_TEMPLATE_INST);
7562 return ret;
7563 }
7564 \f
7565 struct pair_fn_data
7566 {
7567 tree_fn_t fn;
7568 void *data;
7569 /* True when we should also visit template parameters that occur in
7570 non-deduced contexts. */
7571 bool include_nondeduced_p;
7572 struct pointer_set_t *visited;
7573 };
7574
7575 /* Called from for_each_template_parm via walk_tree. */
7576
7577 static tree
7578 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7579 {
7580 tree t = *tp;
7581 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7582 tree_fn_t fn = pfd->fn;
7583 void *data = pfd->data;
7584
7585 if (TYPE_P (t)
7586 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7587 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7588 pfd->include_nondeduced_p))
7589 return error_mark_node;
7590
7591 switch (TREE_CODE (t))
7592 {
7593 case RECORD_TYPE:
7594 if (TYPE_PTRMEMFUNC_P (t))
7595 break;
7596 /* Fall through. */
7597
7598 case UNION_TYPE:
7599 case ENUMERAL_TYPE:
7600 if (!TYPE_TEMPLATE_INFO (t))
7601 *walk_subtrees = 0;
7602 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7603 fn, data, pfd->visited,
7604 pfd->include_nondeduced_p))
7605 return error_mark_node;
7606 break;
7607
7608 case INTEGER_TYPE:
7609 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7610 fn, data, pfd->visited,
7611 pfd->include_nondeduced_p)
7612 || for_each_template_parm (TYPE_MAX_VALUE (t),
7613 fn, data, pfd->visited,
7614 pfd->include_nondeduced_p))
7615 return error_mark_node;
7616 break;
7617
7618 case METHOD_TYPE:
7619 /* Since we're not going to walk subtrees, we have to do this
7620 explicitly here. */
7621 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7622 pfd->visited, pfd->include_nondeduced_p))
7623 return error_mark_node;
7624 /* Fall through. */
7625
7626 case FUNCTION_TYPE:
7627 /* Check the return type. */
7628 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7629 pfd->include_nondeduced_p))
7630 return error_mark_node;
7631
7632 /* Check the parameter types. Since default arguments are not
7633 instantiated until they are needed, the TYPE_ARG_TYPES may
7634 contain expressions that involve template parameters. But,
7635 no-one should be looking at them yet. And, once they're
7636 instantiated, they don't contain template parameters, so
7637 there's no point in looking at them then, either. */
7638 {
7639 tree parm;
7640
7641 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7642 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7643 pfd->visited, pfd->include_nondeduced_p))
7644 return error_mark_node;
7645
7646 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7647 want walk_tree walking into them itself. */
7648 *walk_subtrees = 0;
7649 }
7650 break;
7651
7652 case TYPEOF_TYPE:
7653 case UNDERLYING_TYPE:
7654 if (pfd->include_nondeduced_p
7655 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7656 pfd->visited,
7657 pfd->include_nondeduced_p))
7658 return error_mark_node;
7659 break;
7660
7661 case FUNCTION_DECL:
7662 case VAR_DECL:
7663 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7664 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7665 pfd->visited, pfd->include_nondeduced_p))
7666 return error_mark_node;
7667 /* Fall through. */
7668
7669 case PARM_DECL:
7670 case CONST_DECL:
7671 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7672 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7673 pfd->visited, pfd->include_nondeduced_p))
7674 return error_mark_node;
7675 if (DECL_CONTEXT (t)
7676 && pfd->include_nondeduced_p
7677 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7678 pfd->visited, pfd->include_nondeduced_p))
7679 return error_mark_node;
7680 break;
7681
7682 case BOUND_TEMPLATE_TEMPLATE_PARM:
7683 /* Record template parameters such as `T' inside `TT<T>'. */
7684 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7685 pfd->include_nondeduced_p))
7686 return error_mark_node;
7687 /* Fall through. */
7688
7689 case TEMPLATE_TEMPLATE_PARM:
7690 case TEMPLATE_TYPE_PARM:
7691 case TEMPLATE_PARM_INDEX:
7692 if (fn && (*fn)(t, data))
7693 return error_mark_node;
7694 else if (!fn)
7695 return error_mark_node;
7696 break;
7697
7698 case TEMPLATE_DECL:
7699 /* A template template parameter is encountered. */
7700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7701 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7702 pfd->include_nondeduced_p))
7703 return error_mark_node;
7704
7705 /* Already substituted template template parameter */
7706 *walk_subtrees = 0;
7707 break;
7708
7709 case TYPENAME_TYPE:
7710 if (!fn
7711 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7712 data, pfd->visited,
7713 pfd->include_nondeduced_p))
7714 return error_mark_node;
7715 break;
7716
7717 case CONSTRUCTOR:
7718 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7719 && pfd->include_nondeduced_p
7720 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7721 (TREE_TYPE (t)), fn, data,
7722 pfd->visited, pfd->include_nondeduced_p))
7723 return error_mark_node;
7724 break;
7725
7726 case INDIRECT_REF:
7727 case COMPONENT_REF:
7728 /* If there's no type, then this thing must be some expression
7729 involving template parameters. */
7730 if (!fn && !TREE_TYPE (t))
7731 return error_mark_node;
7732 break;
7733
7734 case MODOP_EXPR:
7735 case CAST_EXPR:
7736 case IMPLICIT_CONV_EXPR:
7737 case REINTERPRET_CAST_EXPR:
7738 case CONST_CAST_EXPR:
7739 case STATIC_CAST_EXPR:
7740 case DYNAMIC_CAST_EXPR:
7741 case ARROW_EXPR:
7742 case DOTSTAR_EXPR:
7743 case TYPEID_EXPR:
7744 case PSEUDO_DTOR_EXPR:
7745 if (!fn)
7746 return error_mark_node;
7747 break;
7748
7749 default:
7750 break;
7751 }
7752
7753 /* We didn't find any template parameters we liked. */
7754 return NULL_TREE;
7755 }
7756
7757 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7758 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7759 call FN with the parameter and the DATA.
7760 If FN returns nonzero, the iteration is terminated, and
7761 for_each_template_parm returns 1. Otherwise, the iteration
7762 continues. If FN never returns a nonzero value, the value
7763 returned by for_each_template_parm is 0. If FN is NULL, it is
7764 considered to be the function which always returns 1.
7765
7766 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7767 parameters that occur in non-deduced contexts. When false, only
7768 visits those template parameters that can be deduced. */
7769
7770 static int
7771 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7772 struct pointer_set_t *visited,
7773 bool include_nondeduced_p)
7774 {
7775 struct pair_fn_data pfd;
7776 int result;
7777
7778 /* Set up. */
7779 pfd.fn = fn;
7780 pfd.data = data;
7781 pfd.include_nondeduced_p = include_nondeduced_p;
7782
7783 /* Walk the tree. (Conceptually, we would like to walk without
7784 duplicates, but for_each_template_parm_r recursively calls
7785 for_each_template_parm, so we would need to reorganize a fair
7786 bit to use walk_tree_without_duplicates, so we keep our own
7787 visited list.) */
7788 if (visited)
7789 pfd.visited = visited;
7790 else
7791 pfd.visited = pointer_set_create ();
7792 result = cp_walk_tree (&t,
7793 for_each_template_parm_r,
7794 &pfd,
7795 pfd.visited) != NULL_TREE;
7796
7797 /* Clean up. */
7798 if (!visited)
7799 {
7800 pointer_set_destroy (pfd.visited);
7801 pfd.visited = 0;
7802 }
7803
7804 return result;
7805 }
7806
7807 /* Returns true if T depends on any template parameter. */
7808
7809 int
7810 uses_template_parms (tree t)
7811 {
7812 bool dependent_p;
7813 int saved_processing_template_decl;
7814
7815 saved_processing_template_decl = processing_template_decl;
7816 if (!saved_processing_template_decl)
7817 processing_template_decl = 1;
7818 if (TYPE_P (t))
7819 dependent_p = dependent_type_p (t);
7820 else if (TREE_CODE (t) == TREE_VEC)
7821 dependent_p = any_dependent_template_arguments_p (t);
7822 else if (TREE_CODE (t) == TREE_LIST)
7823 dependent_p = (uses_template_parms (TREE_VALUE (t))
7824 || uses_template_parms (TREE_CHAIN (t)));
7825 else if (TREE_CODE (t) == TYPE_DECL)
7826 dependent_p = dependent_type_p (TREE_TYPE (t));
7827 else if (DECL_P (t)
7828 || EXPR_P (t)
7829 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7830 || TREE_CODE (t) == OVERLOAD
7831 || BASELINK_P (t)
7832 || TREE_CODE (t) == IDENTIFIER_NODE
7833 || TREE_CODE (t) == TRAIT_EXPR
7834 || TREE_CODE (t) == CONSTRUCTOR
7835 || CONSTANT_CLASS_P (t))
7836 dependent_p = (type_dependent_expression_p (t)
7837 || value_dependent_expression_p (t));
7838 else
7839 {
7840 gcc_assert (t == error_mark_node);
7841 dependent_p = false;
7842 }
7843
7844 processing_template_decl = saved_processing_template_decl;
7845
7846 return dependent_p;
7847 }
7848
7849 /* Returns true iff current_function_decl is an incompletely instantiated
7850 template. Useful instead of processing_template_decl because the latter
7851 is set to 0 during fold_non_dependent_expr. */
7852
7853 bool
7854 in_template_function (void)
7855 {
7856 tree fn = current_function_decl;
7857 bool ret;
7858 ++processing_template_decl;
7859 ret = (fn && DECL_LANG_SPECIFIC (fn)
7860 && DECL_TEMPLATE_INFO (fn)
7861 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7862 --processing_template_decl;
7863 return ret;
7864 }
7865
7866 /* Returns true if T depends on any template parameter with level LEVEL. */
7867
7868 int
7869 uses_template_parms_level (tree t, int level)
7870 {
7871 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7872 /*include_nondeduced_p=*/true);
7873 }
7874
7875 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7876 ill-formed translation unit, i.e. a variable or function that isn't
7877 usable in a constant expression. */
7878
7879 static inline bool
7880 neglectable_inst_p (tree d)
7881 {
7882 return (DECL_P (d)
7883 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7884 : decl_maybe_constant_var_p (d)));
7885 }
7886
7887 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7888 neglectable and instantiated from within an erroneous instantiation. */
7889
7890 static bool
7891 limit_bad_template_recursion (tree decl)
7892 {
7893 struct tinst_level *lev = current_tinst_level;
7894 int errs = errorcount + sorrycount;
7895 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7896 return false;
7897
7898 for (; lev; lev = lev->next)
7899 if (neglectable_inst_p (lev->decl))
7900 break;
7901
7902 return (lev && errs > lev->errors);
7903 }
7904
7905 static int tinst_depth;
7906 extern int max_tinst_depth;
7907 int depth_reached;
7908
7909 static GTY(()) struct tinst_level *last_error_tinst_level;
7910
7911 /* We're starting to instantiate D; record the template instantiation context
7912 for diagnostics and to restore it later. */
7913
7914 int
7915 push_tinst_level (tree d)
7916 {
7917 struct tinst_level *new_level;
7918
7919 if (tinst_depth >= max_tinst_depth)
7920 {
7921 last_error_tinst_level = current_tinst_level;
7922 if (TREE_CODE (d) == TREE_LIST)
7923 error ("template instantiation depth exceeds maximum of %d (use "
7924 "-ftemplate-depth= to increase the maximum) substituting %qS",
7925 max_tinst_depth, d);
7926 else
7927 error ("template instantiation depth exceeds maximum of %d (use "
7928 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7929 max_tinst_depth, d);
7930
7931 print_instantiation_context ();
7932
7933 return 0;
7934 }
7935
7936 /* If the current instantiation caused problems, don't let it instantiate
7937 anything else. Do allow deduction substitution and decls usable in
7938 constant expressions. */
7939 if (limit_bad_template_recursion (d))
7940 return 0;
7941
7942 new_level = ggc_alloc_tinst_level ();
7943 new_level->decl = d;
7944 new_level->locus = input_location;
7945 new_level->errors = errorcount+sorrycount;
7946 new_level->in_system_header_p = in_system_header;
7947 new_level->next = current_tinst_level;
7948 current_tinst_level = new_level;
7949
7950 ++tinst_depth;
7951 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
7952 depth_reached = tinst_depth;
7953
7954 return 1;
7955 }
7956
7957 /* We're done instantiating this template; return to the instantiation
7958 context. */
7959
7960 void
7961 pop_tinst_level (void)
7962 {
7963 /* Restore the filename and line number stashed away when we started
7964 this instantiation. */
7965 input_location = current_tinst_level->locus;
7966 current_tinst_level = current_tinst_level->next;
7967 --tinst_depth;
7968 }
7969
7970 /* We're instantiating a deferred template; restore the template
7971 instantiation context in which the instantiation was requested, which
7972 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7973
7974 static tree
7975 reopen_tinst_level (struct tinst_level *level)
7976 {
7977 struct tinst_level *t;
7978
7979 tinst_depth = 0;
7980 for (t = level; t; t = t->next)
7981 ++tinst_depth;
7982
7983 current_tinst_level = level;
7984 pop_tinst_level ();
7985 if (current_tinst_level)
7986 current_tinst_level->errors = errorcount+sorrycount;
7987 return level->decl;
7988 }
7989
7990 /* Returns the TINST_LEVEL which gives the original instantiation
7991 context. */
7992
7993 struct tinst_level *
7994 outermost_tinst_level (void)
7995 {
7996 struct tinst_level *level = current_tinst_level;
7997 if (level)
7998 while (level->next)
7999 level = level->next;
8000 return level;
8001 }
8002
8003 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8004 vector of template arguments, as for tsubst.
8005
8006 Returns an appropriate tsubst'd friend declaration. */
8007
8008 static tree
8009 tsubst_friend_function (tree decl, tree args)
8010 {
8011 tree new_friend;
8012
8013 if (TREE_CODE (decl) == FUNCTION_DECL
8014 && DECL_TEMPLATE_INSTANTIATION (decl)
8015 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8016 /* This was a friend declared with an explicit template
8017 argument list, e.g.:
8018
8019 friend void f<>(T);
8020
8021 to indicate that f was a template instantiation, not a new
8022 function declaration. Now, we have to figure out what
8023 instantiation of what template. */
8024 {
8025 tree template_id, arglist, fns;
8026 tree new_args;
8027 tree tmpl;
8028 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8029
8030 /* Friend functions are looked up in the containing namespace scope.
8031 We must enter that scope, to avoid finding member functions of the
8032 current class with same name. */
8033 push_nested_namespace (ns);
8034 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8035 tf_warning_or_error, NULL_TREE,
8036 /*integral_constant_expression_p=*/false);
8037 pop_nested_namespace (ns);
8038 arglist = tsubst (DECL_TI_ARGS (decl), args,
8039 tf_warning_or_error, NULL_TREE);
8040 template_id = lookup_template_function (fns, arglist);
8041
8042 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8043 tmpl = determine_specialization (template_id, new_friend,
8044 &new_args,
8045 /*need_member_template=*/0,
8046 TREE_VEC_LENGTH (args),
8047 tsk_none);
8048 return instantiate_template (tmpl, new_args, tf_error);
8049 }
8050
8051 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8052
8053 /* The NEW_FRIEND will look like an instantiation, to the
8054 compiler, but is not an instantiation from the point of view of
8055 the language. For example, we might have had:
8056
8057 template <class T> struct S {
8058 template <class U> friend void f(T, U);
8059 };
8060
8061 Then, in S<int>, template <class U> void f(int, U) is not an
8062 instantiation of anything. */
8063 if (new_friend == error_mark_node)
8064 return error_mark_node;
8065
8066 DECL_USE_TEMPLATE (new_friend) = 0;
8067 if (TREE_CODE (decl) == TEMPLATE_DECL)
8068 {
8069 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8070 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8071 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8072 }
8073
8074 /* The mangled name for the NEW_FRIEND is incorrect. The function
8075 is not a template instantiation and should not be mangled like
8076 one. Therefore, we forget the mangling here; we'll recompute it
8077 later if we need it. */
8078 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8079 {
8080 SET_DECL_RTL (new_friend, NULL);
8081 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8082 }
8083
8084 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8085 {
8086 tree old_decl;
8087 tree new_friend_template_info;
8088 tree new_friend_result_template_info;
8089 tree ns;
8090 int new_friend_is_defn;
8091
8092 /* We must save some information from NEW_FRIEND before calling
8093 duplicate decls since that function will free NEW_FRIEND if
8094 possible. */
8095 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8096 new_friend_is_defn =
8097 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8098 (template_for_substitution (new_friend)))
8099 != NULL_TREE);
8100 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8101 {
8102 /* This declaration is a `primary' template. */
8103 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8104
8105 new_friend_result_template_info
8106 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8107 }
8108 else
8109 new_friend_result_template_info = NULL_TREE;
8110
8111 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8112 if (new_friend_is_defn)
8113 DECL_INITIAL (new_friend) = error_mark_node;
8114
8115 /* Inside pushdecl_namespace_level, we will push into the
8116 current namespace. However, the friend function should go
8117 into the namespace of the template. */
8118 ns = decl_namespace_context (new_friend);
8119 push_nested_namespace (ns);
8120 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8121 pop_nested_namespace (ns);
8122
8123 if (old_decl == error_mark_node)
8124 return error_mark_node;
8125
8126 if (old_decl != new_friend)
8127 {
8128 /* This new friend declaration matched an existing
8129 declaration. For example, given:
8130
8131 template <class T> void f(T);
8132 template <class U> class C {
8133 template <class T> friend void f(T) {}
8134 };
8135
8136 the friend declaration actually provides the definition
8137 of `f', once C has been instantiated for some type. So,
8138 old_decl will be the out-of-class template declaration,
8139 while new_friend is the in-class definition.
8140
8141 But, if `f' was called before this point, the
8142 instantiation of `f' will have DECL_TI_ARGS corresponding
8143 to `T' but not to `U', references to which might appear
8144 in the definition of `f'. Previously, the most general
8145 template for an instantiation of `f' was the out-of-class
8146 version; now it is the in-class version. Therefore, we
8147 run through all specialization of `f', adding to their
8148 DECL_TI_ARGS appropriately. In particular, they need a
8149 new set of outer arguments, corresponding to the
8150 arguments for this class instantiation.
8151
8152 The same situation can arise with something like this:
8153
8154 friend void f(int);
8155 template <class T> class C {
8156 friend void f(T) {}
8157 };
8158
8159 when `C<int>' is instantiated. Now, `f(int)' is defined
8160 in the class. */
8161
8162 if (!new_friend_is_defn)
8163 /* On the other hand, if the in-class declaration does
8164 *not* provide a definition, then we don't want to alter
8165 existing definitions. We can just leave everything
8166 alone. */
8167 ;
8168 else
8169 {
8170 tree new_template = TI_TEMPLATE (new_friend_template_info);
8171 tree new_args = TI_ARGS (new_friend_template_info);
8172
8173 /* Overwrite whatever template info was there before, if
8174 any, with the new template information pertaining to
8175 the declaration. */
8176 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8177
8178 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8179 {
8180 /* We should have called reregister_specialization in
8181 duplicate_decls. */
8182 gcc_assert (retrieve_specialization (new_template,
8183 new_args, 0)
8184 == old_decl);
8185
8186 /* Instantiate it if the global has already been used. */
8187 if (DECL_ODR_USED (old_decl))
8188 instantiate_decl (old_decl, /*defer_ok=*/true,
8189 /*expl_inst_class_mem_p=*/false);
8190 }
8191 else
8192 {
8193 tree t;
8194
8195 /* Indicate that the old function template is a partial
8196 instantiation. */
8197 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8198 = new_friend_result_template_info;
8199
8200 gcc_assert (new_template
8201 == most_general_template (new_template));
8202 gcc_assert (new_template != old_decl);
8203
8204 /* Reassign any specializations already in the hash table
8205 to the new more general template, and add the
8206 additional template args. */
8207 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8208 t != NULL_TREE;
8209 t = TREE_CHAIN (t))
8210 {
8211 tree spec = TREE_VALUE (t);
8212 spec_entry elt;
8213
8214 elt.tmpl = old_decl;
8215 elt.args = DECL_TI_ARGS (spec);
8216 elt.spec = NULL_TREE;
8217
8218 htab_remove_elt (decl_specializations, &elt);
8219
8220 DECL_TI_ARGS (spec)
8221 = add_outermost_template_args (new_args,
8222 DECL_TI_ARGS (spec));
8223
8224 register_specialization
8225 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8226
8227 }
8228 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8229 }
8230 }
8231
8232 /* The information from NEW_FRIEND has been merged into OLD_DECL
8233 by duplicate_decls. */
8234 new_friend = old_decl;
8235 }
8236 }
8237 else
8238 {
8239 tree context = DECL_CONTEXT (new_friend);
8240 bool dependent_p;
8241
8242 /* In the code
8243 template <class T> class C {
8244 template <class U> friend void C1<U>::f (); // case 1
8245 friend void C2<T>::f (); // case 2
8246 };
8247 we only need to make sure CONTEXT is a complete type for
8248 case 2. To distinguish between the two cases, we note that
8249 CONTEXT of case 1 remains dependent type after tsubst while
8250 this isn't true for case 2. */
8251 ++processing_template_decl;
8252 dependent_p = dependent_type_p (context);
8253 --processing_template_decl;
8254
8255 if (!dependent_p
8256 && !complete_type_or_else (context, NULL_TREE))
8257 return error_mark_node;
8258
8259 if (COMPLETE_TYPE_P (context))
8260 {
8261 /* Check to see that the declaration is really present, and,
8262 possibly obtain an improved declaration. */
8263 tree fn = check_classfn (context,
8264 new_friend, NULL_TREE);
8265
8266 if (fn)
8267 new_friend = fn;
8268 }
8269 }
8270
8271 return new_friend;
8272 }
8273
8274 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8275 template arguments, as for tsubst.
8276
8277 Returns an appropriate tsubst'd friend type or error_mark_node on
8278 failure. */
8279
8280 static tree
8281 tsubst_friend_class (tree friend_tmpl, tree args)
8282 {
8283 tree friend_type;
8284 tree tmpl;
8285 tree context;
8286
8287 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8288 {
8289 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8290 return TREE_TYPE (t);
8291 }
8292
8293 context = CP_DECL_CONTEXT (friend_tmpl);
8294
8295 if (context != global_namespace)
8296 {
8297 if (TREE_CODE (context) == NAMESPACE_DECL)
8298 push_nested_namespace (context);
8299 else
8300 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8301 }
8302
8303 /* Look for a class template declaration. We look for hidden names
8304 because two friend declarations of the same template are the
8305 same. For example, in:
8306
8307 struct A {
8308 template <typename> friend class F;
8309 };
8310 template <typename> struct B {
8311 template <typename> friend class F;
8312 };
8313
8314 both F templates are the same. */
8315 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8316 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8317
8318 /* But, if we don't find one, it might be because we're in a
8319 situation like this:
8320
8321 template <class T>
8322 struct S {
8323 template <class U>
8324 friend struct S;
8325 };
8326
8327 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8328 for `S<int>', not the TEMPLATE_DECL. */
8329 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8330 {
8331 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8332 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8333 }
8334
8335 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8336 {
8337 /* The friend template has already been declared. Just
8338 check to see that the declarations match, and install any new
8339 default parameters. We must tsubst the default parameters,
8340 of course. We only need the innermost template parameters
8341 because that is all that redeclare_class_template will look
8342 at. */
8343 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8344 > TMPL_ARGS_DEPTH (args))
8345 {
8346 tree parms;
8347 location_t saved_input_location;
8348 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8349 args, tf_warning_or_error);
8350
8351 saved_input_location = input_location;
8352 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8353 redeclare_class_template (TREE_TYPE (tmpl), parms);
8354 input_location = saved_input_location;
8355
8356 }
8357
8358 friend_type = TREE_TYPE (tmpl);
8359 }
8360 else
8361 {
8362 /* The friend template has not already been declared. In this
8363 case, the instantiation of the template class will cause the
8364 injection of this template into the global scope. */
8365 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8366 if (tmpl == error_mark_node)
8367 return error_mark_node;
8368
8369 /* The new TMPL is not an instantiation of anything, so we
8370 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8371 the new type because that is supposed to be the corresponding
8372 template decl, i.e., TMPL. */
8373 DECL_USE_TEMPLATE (tmpl) = 0;
8374 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8375 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8376 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8377 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8378
8379 /* Inject this template into the global scope. */
8380 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8381 }
8382
8383 if (context != global_namespace)
8384 {
8385 if (TREE_CODE (context) == NAMESPACE_DECL)
8386 pop_nested_namespace (context);
8387 else
8388 pop_nested_class ();
8389 }
8390
8391 return friend_type;
8392 }
8393
8394 /* Returns zero if TYPE cannot be completed later due to circularity.
8395 Otherwise returns one. */
8396
8397 static int
8398 can_complete_type_without_circularity (tree type)
8399 {
8400 if (type == NULL_TREE || type == error_mark_node)
8401 return 0;
8402 else if (COMPLETE_TYPE_P (type))
8403 return 1;
8404 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8405 return can_complete_type_without_circularity (TREE_TYPE (type));
8406 else if (CLASS_TYPE_P (type)
8407 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8408 return 0;
8409 else
8410 return 1;
8411 }
8412
8413 /* Apply any attributes which had to be deferred until instantiation
8414 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8415 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8416
8417 static void
8418 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8419 tree args, tsubst_flags_t complain, tree in_decl)
8420 {
8421 tree last_dep = NULL_TREE;
8422 tree t;
8423 tree *p;
8424
8425 for (t = attributes; t; t = TREE_CHAIN (t))
8426 if (ATTR_IS_DEPENDENT (t))
8427 {
8428 last_dep = t;
8429 attributes = copy_list (attributes);
8430 break;
8431 }
8432
8433 if (DECL_P (*decl_p))
8434 {
8435 if (TREE_TYPE (*decl_p) == error_mark_node)
8436 return;
8437 p = &DECL_ATTRIBUTES (*decl_p);
8438 }
8439 else
8440 p = &TYPE_ATTRIBUTES (*decl_p);
8441
8442 if (last_dep)
8443 {
8444 tree late_attrs = NULL_TREE;
8445 tree *q = &late_attrs;
8446
8447 for (*p = attributes; *p; )
8448 {
8449 t = *p;
8450 if (ATTR_IS_DEPENDENT (t))
8451 {
8452 *p = TREE_CHAIN (t);
8453 TREE_CHAIN (t) = NULL_TREE;
8454 /* If the first attribute argument is an identifier, don't
8455 pass it through tsubst. Attributes like mode, format,
8456 cleanup and several target specific attributes expect it
8457 unmodified. */
8458 if (TREE_VALUE (t)
8459 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8460 && TREE_VALUE (TREE_VALUE (t))
8461 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8462 == IDENTIFIER_NODE))
8463 {
8464 tree chain
8465 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8466 in_decl,
8467 /*integral_constant_expression_p=*/false);
8468 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8469 TREE_VALUE (t)
8470 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8471 chain);
8472 }
8473 else
8474 TREE_VALUE (t)
8475 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8476 /*integral_constant_expression_p=*/false);
8477 *q = t;
8478 q = &TREE_CHAIN (t);
8479 }
8480 else
8481 p = &TREE_CHAIN (t);
8482 }
8483
8484 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8485 }
8486 }
8487
8488 /* Perform (or defer) access check for typedefs that were referenced
8489 from within the template TMPL code.
8490 This is a subroutine of instantiate_decl and instantiate_class_template.
8491 TMPL is the template to consider and TARGS is the list of arguments of
8492 that template. */
8493
8494 static void
8495 perform_typedefs_access_check (tree tmpl, tree targs)
8496 {
8497 location_t saved_location;
8498 unsigned i;
8499 qualified_typedef_usage_t *iter;
8500
8501 if (!tmpl
8502 || (!CLASS_TYPE_P (tmpl)
8503 && TREE_CODE (tmpl) != FUNCTION_DECL))
8504 return;
8505
8506 saved_location = input_location;
8507 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8508 {
8509 tree type_decl = iter->typedef_decl;
8510 tree type_scope = iter->context;
8511
8512 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8513 continue;
8514
8515 if (uses_template_parms (type_decl))
8516 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8517 if (uses_template_parms (type_scope))
8518 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8519
8520 /* Make access check error messages point to the location
8521 of the use of the typedef. */
8522 input_location = iter->locus;
8523 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8524 type_decl, type_decl,
8525 tf_warning_or_error);
8526 }
8527 input_location = saved_location;
8528 }
8529
8530 static tree
8531 instantiate_class_template_1 (tree type)
8532 {
8533 tree templ, args, pattern, t, member;
8534 tree typedecl;
8535 tree pbinfo;
8536 tree base_list;
8537 unsigned int saved_maximum_field_alignment;
8538 tree fn_context;
8539
8540 if (type == error_mark_node)
8541 return error_mark_node;
8542
8543 if (COMPLETE_OR_OPEN_TYPE_P (type)
8544 || uses_template_parms (type))
8545 return type;
8546
8547 /* Figure out which template is being instantiated. */
8548 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8549 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8550
8551 /* Determine what specialization of the original template to
8552 instantiate. */
8553 t = most_specialized_class (type, templ, tf_warning_or_error);
8554 if (t == error_mark_node)
8555 {
8556 TYPE_BEING_DEFINED (type) = 1;
8557 return error_mark_node;
8558 }
8559 else if (t)
8560 {
8561 /* This TYPE is actually an instantiation of a partial
8562 specialization. We replace the innermost set of ARGS with
8563 the arguments appropriate for substitution. For example,
8564 given:
8565
8566 template <class T> struct S {};
8567 template <class T> struct S<T*> {};
8568
8569 and supposing that we are instantiating S<int*>, ARGS will
8570 presently be {int*} -- but we need {int}. */
8571 pattern = TREE_TYPE (t);
8572 args = TREE_PURPOSE (t);
8573 }
8574 else
8575 {
8576 pattern = TREE_TYPE (templ);
8577 args = CLASSTYPE_TI_ARGS (type);
8578 }
8579
8580 /* If the template we're instantiating is incomplete, then clearly
8581 there's nothing we can do. */
8582 if (!COMPLETE_TYPE_P (pattern))
8583 return type;
8584
8585 /* If we've recursively instantiated too many templates, stop. */
8586 if (! push_tinst_level (type))
8587 return type;
8588
8589 /* Now we're really doing the instantiation. Mark the type as in
8590 the process of being defined. */
8591 TYPE_BEING_DEFINED (type) = 1;
8592
8593 /* We may be in the middle of deferred access check. Disable
8594 it now. */
8595 push_deferring_access_checks (dk_no_deferred);
8596
8597 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8598 if (!fn_context)
8599 push_to_top_level ();
8600 /* Use #pragma pack from the template context. */
8601 saved_maximum_field_alignment = maximum_field_alignment;
8602 maximum_field_alignment = TYPE_PRECISION (pattern);
8603
8604 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8605
8606 /* Set the input location to the most specialized template definition.
8607 This is needed if tsubsting causes an error. */
8608 typedecl = TYPE_MAIN_DECL (pattern);
8609 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8610 DECL_SOURCE_LOCATION (typedecl);
8611
8612 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8613 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8614 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8615 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8616 if (ANON_AGGR_TYPE_P (pattern))
8617 SET_ANON_AGGR_TYPE_P (type);
8618 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8619 {
8620 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8621 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8622 /* Adjust visibility for template arguments. */
8623 determine_visibility (TYPE_MAIN_DECL (type));
8624 }
8625 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8626
8627 pbinfo = TYPE_BINFO (pattern);
8628
8629 /* We should never instantiate a nested class before its enclosing
8630 class; we need to look up the nested class by name before we can
8631 instantiate it, and that lookup should instantiate the enclosing
8632 class. */
8633 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8634 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8635
8636 base_list = NULL_TREE;
8637 if (BINFO_N_BASE_BINFOS (pbinfo))
8638 {
8639 tree pbase_binfo;
8640 tree pushed_scope;
8641 int i;
8642
8643 /* We must enter the scope containing the type, as that is where
8644 the accessibility of types named in dependent bases are
8645 looked up from. */
8646 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8647
8648 /* Substitute into each of the bases to determine the actual
8649 basetypes. */
8650 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8651 {
8652 tree base;
8653 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8654 tree expanded_bases = NULL_TREE;
8655 int idx, len = 1;
8656
8657 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8658 {
8659 expanded_bases =
8660 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8661 args, tf_error, NULL_TREE);
8662 if (expanded_bases == error_mark_node)
8663 continue;
8664
8665 len = TREE_VEC_LENGTH (expanded_bases);
8666 }
8667
8668 for (idx = 0; idx < len; idx++)
8669 {
8670 if (expanded_bases)
8671 /* Extract the already-expanded base class. */
8672 base = TREE_VEC_ELT (expanded_bases, idx);
8673 else
8674 /* Substitute to figure out the base class. */
8675 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8676 NULL_TREE);
8677
8678 if (base == error_mark_node)
8679 continue;
8680
8681 base_list = tree_cons (access, base, base_list);
8682 if (BINFO_VIRTUAL_P (pbase_binfo))
8683 TREE_TYPE (base_list) = integer_type_node;
8684 }
8685 }
8686
8687 /* The list is now in reverse order; correct that. */
8688 base_list = nreverse (base_list);
8689
8690 if (pushed_scope)
8691 pop_scope (pushed_scope);
8692 }
8693 /* Now call xref_basetypes to set up all the base-class
8694 information. */
8695 xref_basetypes (type, base_list);
8696
8697 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8698 (int) ATTR_FLAG_TYPE_IN_PLACE,
8699 args, tf_error, NULL_TREE);
8700 fixup_attribute_variants (type);
8701
8702 /* Now that our base classes are set up, enter the scope of the
8703 class, so that name lookups into base classes, etc. will work
8704 correctly. This is precisely analogous to what we do in
8705 begin_class_definition when defining an ordinary non-template
8706 class, except we also need to push the enclosing classes. */
8707 push_nested_class (type);
8708
8709 /* Now members are processed in the order of declaration. */
8710 for (member = CLASSTYPE_DECL_LIST (pattern);
8711 member; member = TREE_CHAIN (member))
8712 {
8713 tree t = TREE_VALUE (member);
8714
8715 if (TREE_PURPOSE (member))
8716 {
8717 if (TYPE_P (t))
8718 {
8719 /* Build new CLASSTYPE_NESTED_UTDS. */
8720
8721 tree newtag;
8722 bool class_template_p;
8723
8724 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8725 && TYPE_LANG_SPECIFIC (t)
8726 && CLASSTYPE_IS_TEMPLATE (t));
8727 /* If the member is a class template, then -- even after
8728 substitution -- there may be dependent types in the
8729 template argument list for the class. We increment
8730 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8731 that function will assume that no types are dependent
8732 when outside of a template. */
8733 if (class_template_p)
8734 ++processing_template_decl;
8735 newtag = tsubst (t, args, tf_error, NULL_TREE);
8736 if (class_template_p)
8737 --processing_template_decl;
8738 if (newtag == error_mark_node)
8739 continue;
8740
8741 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8742 {
8743 tree name = TYPE_IDENTIFIER (t);
8744
8745 if (class_template_p)
8746 /* Unfortunately, lookup_template_class sets
8747 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8748 instantiation (i.e., for the type of a member
8749 template class nested within a template class.)
8750 This behavior is required for
8751 maybe_process_partial_specialization to work
8752 correctly, but is not accurate in this case;
8753 the TAG is not an instantiation of anything.
8754 (The corresponding TEMPLATE_DECL is an
8755 instantiation, but the TYPE is not.) */
8756 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8757
8758 /* Now, we call pushtag to put this NEWTAG into the scope of
8759 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8760 pushtag calling push_template_decl. We don't have to do
8761 this for enums because it will already have been done in
8762 tsubst_enum. */
8763 if (name)
8764 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8765 pushtag (name, newtag, /*tag_scope=*/ts_current);
8766 }
8767 }
8768 else if (TREE_CODE (t) == FUNCTION_DECL
8769 || DECL_FUNCTION_TEMPLATE_P (t))
8770 {
8771 /* Build new TYPE_METHODS. */
8772 tree r;
8773
8774 if (TREE_CODE (t) == TEMPLATE_DECL)
8775 ++processing_template_decl;
8776 r = tsubst (t, args, tf_error, NULL_TREE);
8777 if (TREE_CODE (t) == TEMPLATE_DECL)
8778 --processing_template_decl;
8779 set_current_access_from_decl (r);
8780 finish_member_declaration (r);
8781 /* Instantiate members marked with attribute used. */
8782 if (r != error_mark_node && DECL_PRESERVE_P (r))
8783 mark_used (r);
8784 }
8785 else
8786 {
8787 /* Build new TYPE_FIELDS. */
8788 if (TREE_CODE (t) == STATIC_ASSERT)
8789 {
8790 tree condition;
8791
8792 ++c_inhibit_evaluation_warnings;
8793 condition =
8794 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8795 tf_warning_or_error, NULL_TREE,
8796 /*integral_constant_expression_p=*/true);
8797 --c_inhibit_evaluation_warnings;
8798
8799 finish_static_assert (condition,
8800 STATIC_ASSERT_MESSAGE (t),
8801 STATIC_ASSERT_SOURCE_LOCATION (t),
8802 /*member_p=*/true);
8803 }
8804 else if (TREE_CODE (t) != CONST_DECL)
8805 {
8806 tree r;
8807
8808 /* The file and line for this declaration, to
8809 assist in error message reporting. Since we
8810 called push_tinst_level above, we don't need to
8811 restore these. */
8812 input_location = DECL_SOURCE_LOCATION (t);
8813
8814 if (TREE_CODE (t) == TEMPLATE_DECL)
8815 ++processing_template_decl;
8816 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8817 if (TREE_CODE (t) == TEMPLATE_DECL)
8818 --processing_template_decl;
8819 if (TREE_CODE (r) == VAR_DECL)
8820 {
8821 /* In [temp.inst]:
8822
8823 [t]he initialization (and any associated
8824 side-effects) of a static data member does
8825 not occur unless the static data member is
8826 itself used in a way that requires the
8827 definition of the static data member to
8828 exist.
8829
8830 Therefore, we do not substitute into the
8831 initialized for the static data member here. */
8832 finish_static_data_member_decl
8833 (r,
8834 /*init=*/NULL_TREE,
8835 /*init_const_expr_p=*/false,
8836 /*asmspec_tree=*/NULL_TREE,
8837 /*flags=*/0);
8838 /* Instantiate members marked with attribute used. */
8839 if (r != error_mark_node && DECL_PRESERVE_P (r))
8840 mark_used (r);
8841 }
8842 else if (TREE_CODE (r) == FIELD_DECL)
8843 {
8844 /* Determine whether R has a valid type and can be
8845 completed later. If R is invalid, then it is
8846 replaced by error_mark_node so that it will not be
8847 added to TYPE_FIELDS. */
8848 tree rtype = TREE_TYPE (r);
8849 if (can_complete_type_without_circularity (rtype))
8850 complete_type (rtype);
8851
8852 if (!COMPLETE_TYPE_P (rtype))
8853 {
8854 cxx_incomplete_type_error (r, rtype);
8855 r = error_mark_node;
8856 }
8857 }
8858
8859 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8860 such a thing will already have been added to the field
8861 list by tsubst_enum in finish_member_declaration in the
8862 CLASSTYPE_NESTED_UTDS case above. */
8863 if (!(TREE_CODE (r) == TYPE_DECL
8864 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8865 && DECL_ARTIFICIAL (r)))
8866 {
8867 set_current_access_from_decl (r);
8868 finish_member_declaration (r);
8869 }
8870 }
8871 }
8872 }
8873 else
8874 {
8875 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
8876 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8877 {
8878 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8879
8880 tree friend_type = t;
8881 bool adjust_processing_template_decl = false;
8882
8883 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8884 {
8885 /* template <class T> friend class C; */
8886 friend_type = tsubst_friend_class (friend_type, args);
8887 adjust_processing_template_decl = true;
8888 }
8889 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8890 {
8891 /* template <class T> friend class C::D; */
8892 friend_type = tsubst (friend_type, args,
8893 tf_warning_or_error, NULL_TREE);
8894 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8895 friend_type = TREE_TYPE (friend_type);
8896 adjust_processing_template_decl = true;
8897 }
8898 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8899 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8900 {
8901 /* This could be either
8902
8903 friend class T::C;
8904
8905 when dependent_type_p is false or
8906
8907 template <class U> friend class T::C;
8908
8909 otherwise. */
8910 friend_type = tsubst (friend_type, args,
8911 tf_warning_or_error, NULL_TREE);
8912 /* Bump processing_template_decl for correct
8913 dependent_type_p calculation. */
8914 ++processing_template_decl;
8915 if (dependent_type_p (friend_type))
8916 adjust_processing_template_decl = true;
8917 --processing_template_decl;
8918 }
8919 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8920 && hidden_name_p (TYPE_NAME (friend_type)))
8921 {
8922 /* friend class C;
8923
8924 where C hasn't been declared yet. Let's lookup name
8925 from namespace scope directly, bypassing any name that
8926 come from dependent base class. */
8927 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8928
8929 /* The call to xref_tag_from_type does injection for friend
8930 classes. */
8931 push_nested_namespace (ns);
8932 friend_type =
8933 xref_tag_from_type (friend_type, NULL_TREE,
8934 /*tag_scope=*/ts_current);
8935 pop_nested_namespace (ns);
8936 }
8937 else if (uses_template_parms (friend_type))
8938 /* friend class C<T>; */
8939 friend_type = tsubst (friend_type, args,
8940 tf_warning_or_error, NULL_TREE);
8941 /* Otherwise it's
8942
8943 friend class C;
8944
8945 where C is already declared or
8946
8947 friend class C<int>;
8948
8949 We don't have to do anything in these cases. */
8950
8951 if (adjust_processing_template_decl)
8952 /* Trick make_friend_class into realizing that the friend
8953 we're adding is a template, not an ordinary class. It's
8954 important that we use make_friend_class since it will
8955 perform some error-checking and output cross-reference
8956 information. */
8957 ++processing_template_decl;
8958
8959 if (friend_type != error_mark_node)
8960 make_friend_class (type, friend_type, /*complain=*/false);
8961
8962 if (adjust_processing_template_decl)
8963 --processing_template_decl;
8964 }
8965 else
8966 {
8967 /* Build new DECL_FRIENDLIST. */
8968 tree r;
8969
8970 /* The file and line for this declaration, to
8971 assist in error message reporting. Since we
8972 called push_tinst_level above, we don't need to
8973 restore these. */
8974 input_location = DECL_SOURCE_LOCATION (t);
8975
8976 if (TREE_CODE (t) == TEMPLATE_DECL)
8977 {
8978 ++processing_template_decl;
8979 push_deferring_access_checks (dk_no_check);
8980 }
8981
8982 r = tsubst_friend_function (t, args);
8983 add_friend (type, r, /*complain=*/false);
8984 if (TREE_CODE (t) == TEMPLATE_DECL)
8985 {
8986 pop_deferring_access_checks ();
8987 --processing_template_decl;
8988 }
8989 }
8990 }
8991 }
8992
8993 if (CLASSTYPE_LAMBDA_EXPR (type))
8994 {
8995 tree decl = lambda_function (type);
8996 if (decl)
8997 {
8998 instantiate_decl (decl, false, false);
8999 maybe_add_lambda_conv_op (type);
9000 }
9001 else
9002 gcc_assert (errorcount);
9003 }
9004
9005 /* Set the file and line number information to whatever is given for
9006 the class itself. This puts error messages involving generated
9007 implicit functions at a predictable point, and the same point
9008 that would be used for non-template classes. */
9009 input_location = DECL_SOURCE_LOCATION (typedecl);
9010
9011 unreverse_member_declarations (type);
9012 finish_struct_1 (type);
9013 TYPE_BEING_DEFINED (type) = 0;
9014
9015 /* We don't instantiate default arguments for member functions. 14.7.1:
9016
9017 The implicit instantiation of a class template specialization causes
9018 the implicit instantiation of the declarations, but not of the
9019 definitions or default arguments, of the class member functions,
9020 member classes, static data members and member templates.... */
9021
9022 /* Some typedefs referenced from within the template code need to be access
9023 checked at template instantiation time, i.e now. These types were
9024 added to the template at parsing time. Let's get those and perform
9025 the access checks then. */
9026 perform_typedefs_access_check (pattern, args);
9027 perform_deferred_access_checks (tf_warning_or_error);
9028 pop_nested_class ();
9029 maximum_field_alignment = saved_maximum_field_alignment;
9030 if (!fn_context)
9031 pop_from_top_level ();
9032 pop_deferring_access_checks ();
9033 pop_tinst_level ();
9034
9035 /* The vtable for a template class can be emitted in any translation
9036 unit in which the class is instantiated. When there is no key
9037 method, however, finish_struct_1 will already have added TYPE to
9038 the keyed_classes list. */
9039 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9040 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9041
9042 return type;
9043 }
9044
9045 /* Wrapper for instantiate_class_template_1. */
9046
9047 tree
9048 instantiate_class_template (tree type)
9049 {
9050 tree ret;
9051 timevar_push (TV_TEMPLATE_INST);
9052 ret = instantiate_class_template_1 (type);
9053 timevar_pop (TV_TEMPLATE_INST);
9054 return ret;
9055 }
9056
9057 static tree
9058 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9059 {
9060 tree r;
9061
9062 if (!t)
9063 r = t;
9064 else if (TYPE_P (t))
9065 r = tsubst (t, args, complain, in_decl);
9066 else
9067 {
9068 if (!(complain & tf_warning))
9069 ++c_inhibit_evaluation_warnings;
9070 r = tsubst_expr (t, args, complain, in_decl,
9071 /*integral_constant_expression_p=*/true);
9072 if (!(complain & tf_warning))
9073 --c_inhibit_evaluation_warnings;
9074 /* Preserve the raw-reference nature of T. */
9075 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9076 && REFERENCE_REF_P (r))
9077 r = TREE_OPERAND (r, 0);
9078 }
9079 return r;
9080 }
9081
9082 /* Given a function parameter pack TMPL_PARM and some function parameters
9083 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9084 and set *SPEC_P to point at the next point in the list. */
9085
9086 static tree
9087 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9088 {
9089 /* Collect all of the extra "packed" parameters into an
9090 argument pack. */
9091 tree parmvec;
9092 tree parmtypevec;
9093 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9094 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9095 tree spec_parm = *spec_p;
9096 int i, len;
9097
9098 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9099 if (tmpl_parm
9100 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9101 break;
9102
9103 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9104 parmvec = make_tree_vec (len);
9105 parmtypevec = make_tree_vec (len);
9106 spec_parm = *spec_p;
9107 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9108 {
9109 TREE_VEC_ELT (parmvec, i) = spec_parm;
9110 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9111 }
9112
9113 /* Build the argument packs. */
9114 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9115 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9116 TREE_TYPE (argpack) = argtypepack;
9117 *spec_p = spec_parm;
9118
9119 return argpack;
9120 }
9121
9122 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9123 NONTYPE_ARGUMENT_PACK. */
9124
9125 static tree
9126 make_fnparm_pack (tree spec_parm)
9127 {
9128 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9129 }
9130
9131 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9132 pack expansion. */
9133
9134 static bool
9135 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9136 {
9137 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9138 if (i >= TREE_VEC_LENGTH (vec))
9139 return false;
9140 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9141 }
9142
9143
9144 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9145
9146 static tree
9147 make_argument_pack_select (tree arg_pack, unsigned index)
9148 {
9149 tree aps = make_node (ARGUMENT_PACK_SELECT);
9150
9151 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9152 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9153
9154 return aps;
9155 }
9156
9157 /* This is a subroutine of tsubst_pack_expansion.
9158
9159 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9160 mechanism to store the (non complete list of) arguments of the
9161 substitution and return a non substituted pack expansion, in order
9162 to wait for when we have enough arguments to really perform the
9163 substitution. */
9164
9165 static bool
9166 use_pack_expansion_extra_args_p (tree parm_packs,
9167 int arg_pack_len,
9168 bool has_empty_arg)
9169 {
9170 if (parm_packs == NULL_TREE)
9171 return false;
9172
9173 bool has_expansion_arg = false;
9174 for (int i = 0 ; i < arg_pack_len; ++i)
9175 {
9176 bool has_non_expansion_arg = false;
9177 for (tree parm_pack = parm_packs;
9178 parm_pack;
9179 parm_pack = TREE_CHAIN (parm_pack))
9180 {
9181 tree arg = TREE_VALUE (parm_pack);
9182
9183 if (argument_pack_element_is_expansion_p (arg, i))
9184 has_expansion_arg = true;
9185 else
9186 has_non_expansion_arg = true;
9187 }
9188
9189 /* If one pack has an expansion and another pack has a normal
9190 argument or if one pack has an empty argument another one
9191 hasn't then tsubst_pack_expansion cannot perform the
9192 substitution and need to fall back on the
9193 PACK_EXPANSION_EXTRA mechanism. */
9194 if ((has_expansion_arg && has_non_expansion_arg)
9195 || (has_empty_arg && (has_expansion_arg || has_non_expansion_arg)))
9196 return true;
9197 }
9198 return false;
9199 }
9200
9201 /* [temp.variadic]/6 says that:
9202
9203 The instantiation of a pack expansion [...]
9204 produces a list E1,E2, ..., En, where N is the number of elements
9205 in the pack expansion parameters.
9206
9207 This subroutine of tsubst_pack_expansion produces one of these Ei.
9208
9209 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9210 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9211 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9212 INDEX is the index 'i' of the element Ei to produce. ARGS,
9213 COMPLAIN, and IN_DECL are the same parameters as for the
9214 tsubst_pack_expansion function.
9215
9216 The function returns the resulting Ei upon successful completion,
9217 or error_mark_node.
9218
9219 Note that this function possibly modifies the ARGS parameter, so
9220 it's the responsibility of the caller to restore it. */
9221
9222 static tree
9223 gen_elem_of_pack_expansion_instantiation (tree pattern,
9224 tree parm_packs,
9225 unsigned index,
9226 tree args /* This parm gets
9227 modified. */,
9228 tsubst_flags_t complain,
9229 tree in_decl)
9230 {
9231 tree t;
9232 bool ith_elem_is_expansion = false;
9233
9234 /* For each parameter pack, change the substitution of the parameter
9235 pack to the ith argument in its argument pack, then expand the
9236 pattern. */
9237 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9238 {
9239 tree parm = TREE_PURPOSE (pack);
9240 tree arg_pack = TREE_VALUE (pack);
9241 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9242
9243 ith_elem_is_expansion |=
9244 argument_pack_element_is_expansion_p (arg_pack, index);
9245
9246 /* Select the Ith argument from the pack. */
9247 if (TREE_CODE (parm) == PARM_DECL)
9248 {
9249 if (index == 0)
9250 {
9251 aps = make_argument_pack_select (arg_pack, index);
9252 mark_used (parm);
9253 register_local_specialization (aps, parm);
9254 }
9255 else
9256 aps = retrieve_local_specialization (parm);
9257 }
9258 else
9259 {
9260 int idx, level;
9261 template_parm_level_and_index (parm, &level, &idx);
9262
9263 if (index == 0)
9264 {
9265 aps = make_argument_pack_select (arg_pack, index);
9266 /* Update the corresponding argument. */
9267 TMPL_ARG (args, level, idx) = aps;
9268 }
9269 else
9270 /* Re-use the ARGUMENT_PACK_SELECT. */
9271 aps = TMPL_ARG (args, level, idx);
9272 }
9273 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9274 }
9275
9276 /* Substitute into the PATTERN with the (possibly altered)
9277 arguments. */
9278 if (!TYPE_P (pattern))
9279 t = tsubst_expr (pattern, args, complain, in_decl,
9280 /*integral_constant_expression_p=*/false);
9281 else
9282 t = tsubst (pattern, args, complain, in_decl);
9283
9284 /* If the Ith argument pack element is a pack expansion, then
9285 the Ith element resulting from the substituting is going to
9286 be a pack expansion as well. */
9287 if (ith_elem_is_expansion)
9288 t = make_pack_expansion (t);
9289
9290 return t;
9291 }
9292
9293 /* Substitute ARGS into T, which is an pack expansion
9294 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9295 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9296 (if only a partial substitution could be performed) or
9297 ERROR_MARK_NODE if there was an error. */
9298 tree
9299 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9300 tree in_decl)
9301 {
9302 tree pattern;
9303 tree pack, packs = NULL_TREE;
9304 bool unsubstituted_packs = false;
9305 int i, len = -1;
9306 tree result;
9307 struct pointer_map_t *saved_local_specializations = NULL;
9308 bool need_local_specializations = false;
9309 int levels;
9310
9311 gcc_assert (PACK_EXPANSION_P (t));
9312 pattern = PACK_EXPANSION_PATTERN (t);
9313
9314 /* Add in any args remembered from an earlier partial instantiation. */
9315 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9316
9317 levels = TMPL_ARGS_DEPTH (args);
9318
9319 /* Determine the argument packs that will instantiate the parameter
9320 packs used in the expansion expression. While we're at it,
9321 compute the number of arguments to be expanded and make sure it
9322 is consistent. */
9323 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9324 pack = TREE_CHAIN (pack))
9325 {
9326 tree parm_pack = TREE_VALUE (pack);
9327 tree arg_pack = NULL_TREE;
9328 tree orig_arg = NULL_TREE;
9329 int level = 0;
9330
9331 if (TREE_CODE (parm_pack) == BASES)
9332 {
9333 if (BASES_DIRECT (parm_pack))
9334 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9335 args, complain, in_decl, false));
9336 else
9337 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9338 args, complain, in_decl, false));
9339 }
9340 if (TREE_CODE (parm_pack) == PARM_DECL)
9341 {
9342 if (PACK_EXPANSION_LOCAL_P (t))
9343 arg_pack = retrieve_local_specialization (parm_pack);
9344 else
9345 {
9346 /* We can't rely on local_specializations for a parameter
9347 name used later in a function declaration (such as in a
9348 late-specified return type). Even if it exists, it might
9349 have the wrong value for a recursive call. Just make a
9350 dummy decl, since it's only used for its type. */
9351 arg_pack = tsubst_decl (parm_pack, args, complain);
9352 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9353 /* Partial instantiation of the parm_pack, we can't build
9354 up an argument pack yet. */
9355 arg_pack = NULL_TREE;
9356 else
9357 arg_pack = make_fnparm_pack (arg_pack);
9358 need_local_specializations = true;
9359 }
9360 }
9361 else
9362 {
9363 int idx;
9364 template_parm_level_and_index (parm_pack, &level, &idx);
9365
9366 if (level <= levels)
9367 arg_pack = TMPL_ARG (args, level, idx);
9368 }
9369
9370 orig_arg = arg_pack;
9371 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9372 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9373
9374 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9375 /* This can only happen if we forget to expand an argument
9376 pack somewhere else. Just return an error, silently. */
9377 {
9378 result = make_tree_vec (1);
9379 TREE_VEC_ELT (result, 0) = error_mark_node;
9380 return result;
9381 }
9382
9383 if (arg_pack)
9384 {
9385 int my_len =
9386 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9387
9388 /* Don't bother trying to do a partial substitution with
9389 incomplete packs; we'll try again after deduction. */
9390 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9391 return t;
9392
9393 if (len < 0)
9394 len = my_len;
9395 else if (len != my_len)
9396 {
9397 if (!(complain & tf_error))
9398 /* Fail quietly. */;
9399 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9400 error ("mismatched argument pack lengths while expanding "
9401 "%<%T%>",
9402 pattern);
9403 else
9404 error ("mismatched argument pack lengths while expanding "
9405 "%<%E%>",
9406 pattern);
9407 return error_mark_node;
9408 }
9409
9410 /* Keep track of the parameter packs and their corresponding
9411 argument packs. */
9412 packs = tree_cons (parm_pack, arg_pack, packs);
9413 TREE_TYPE (packs) = orig_arg;
9414 }
9415 else
9416 {
9417 /* We can't substitute for this parameter pack. We use a flag as
9418 well as the missing_level counter because function parameter
9419 packs don't have a level. */
9420 unsubstituted_packs = true;
9421 }
9422 }
9423
9424 /* We cannot expand this expansion expression, because we don't have
9425 all of the argument packs we need. */
9426 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9427 {
9428 /* We got some full packs, but we can't substitute them in until we
9429 have values for all the packs. So remember these until then. */
9430
9431 t = make_pack_expansion (pattern);
9432 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9433 return t;
9434 }
9435 else if (unsubstituted_packs)
9436 {
9437 /* There were no real arguments, we're just replacing a parameter
9438 pack with another version of itself. Substitute into the
9439 pattern and return a PACK_EXPANSION_*. The caller will need to
9440 deal with that. */
9441 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9442 t = tsubst_expr (pattern, args, complain, in_decl,
9443 /*integral_constant_expression_p=*/false);
9444 else
9445 t = tsubst (pattern, args, complain, in_decl);
9446 t = make_pack_expansion (t);
9447 return t;
9448 }
9449
9450 gcc_assert (len >= 0);
9451
9452 if (need_local_specializations)
9453 {
9454 /* We're in a late-specified return type, so create our own local
9455 specializations map; the current map is either NULL or (in the
9456 case of recursive unification) might have bindings that we don't
9457 want to use or alter. */
9458 saved_local_specializations = local_specializations;
9459 local_specializations = pointer_map_create ();
9460 }
9461
9462 /* For each argument in each argument pack, substitute into the
9463 pattern. */
9464 result = make_tree_vec (len);
9465 for (i = 0; i < len; ++i)
9466 {
9467 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9468 i,
9469 args, complain,
9470 in_decl);
9471 TREE_VEC_ELT (result, i) = t;
9472 if (t == error_mark_node)
9473 {
9474 result = error_mark_node;
9475 break;
9476 }
9477 }
9478
9479 /* Update ARGS to restore the substitution from parameter packs to
9480 their argument packs. */
9481 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9482 {
9483 tree parm = TREE_PURPOSE (pack);
9484
9485 if (TREE_CODE (parm) == PARM_DECL)
9486 register_local_specialization (TREE_TYPE (pack), parm);
9487 else
9488 {
9489 int idx, level;
9490
9491 if (TREE_VALUE (pack) == NULL_TREE)
9492 continue;
9493
9494 template_parm_level_and_index (parm, &level, &idx);
9495
9496 /* Update the corresponding argument. */
9497 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9498 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9499 TREE_TYPE (pack);
9500 else
9501 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9502 }
9503 }
9504
9505 if (need_local_specializations)
9506 {
9507 pointer_map_destroy (local_specializations);
9508 local_specializations = saved_local_specializations;
9509 }
9510
9511 return result;
9512 }
9513
9514 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9515 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9516 parameter packs; all parms generated from a function parameter pack will
9517 have the same DECL_PARM_INDEX. */
9518
9519 tree
9520 get_pattern_parm (tree parm, tree tmpl)
9521 {
9522 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9523 tree patparm;
9524
9525 if (DECL_ARTIFICIAL (parm))
9526 {
9527 for (patparm = DECL_ARGUMENTS (pattern);
9528 patparm; patparm = DECL_CHAIN (patparm))
9529 if (DECL_ARTIFICIAL (patparm)
9530 && DECL_NAME (parm) == DECL_NAME (patparm))
9531 break;
9532 }
9533 else
9534 {
9535 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9536 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9537 gcc_assert (DECL_PARM_INDEX (patparm)
9538 == DECL_PARM_INDEX (parm));
9539 }
9540
9541 return patparm;
9542 }
9543
9544 /* Substitute ARGS into the vector or list of template arguments T. */
9545
9546 static tree
9547 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9548 {
9549 tree orig_t = t;
9550 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9551 tree *elts;
9552
9553 if (t == error_mark_node)
9554 return error_mark_node;
9555
9556 len = TREE_VEC_LENGTH (t);
9557 elts = XALLOCAVEC (tree, len);
9558
9559 for (i = 0; i < len; i++)
9560 {
9561 tree orig_arg = TREE_VEC_ELT (t, i);
9562 tree new_arg;
9563
9564 if (TREE_CODE (orig_arg) == TREE_VEC)
9565 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9566 else if (PACK_EXPANSION_P (orig_arg))
9567 {
9568 /* Substitute into an expansion expression. */
9569 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9570
9571 if (TREE_CODE (new_arg) == TREE_VEC)
9572 /* Add to the expanded length adjustment the number of
9573 expanded arguments. We subtract one from this
9574 measurement, because the argument pack expression
9575 itself is already counted as 1 in
9576 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9577 the argument pack is empty. */
9578 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9579 }
9580 else if (ARGUMENT_PACK_P (orig_arg))
9581 {
9582 /* Substitute into each of the arguments. */
9583 new_arg = TYPE_P (orig_arg)
9584 ? cxx_make_type (TREE_CODE (orig_arg))
9585 : make_node (TREE_CODE (orig_arg));
9586
9587 SET_ARGUMENT_PACK_ARGS (
9588 new_arg,
9589 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9590 args, complain, in_decl));
9591
9592 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9593 new_arg = error_mark_node;
9594
9595 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9596 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9597 complain, in_decl);
9598 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9599
9600 if (TREE_TYPE (new_arg) == error_mark_node)
9601 new_arg = error_mark_node;
9602 }
9603 }
9604 else
9605 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9606
9607 if (new_arg == error_mark_node)
9608 return error_mark_node;
9609
9610 elts[i] = new_arg;
9611 if (new_arg != orig_arg)
9612 need_new = 1;
9613 }
9614
9615 if (!need_new)
9616 return t;
9617
9618 /* Make space for the expanded arguments coming from template
9619 argument packs. */
9620 t = make_tree_vec (len + expanded_len_adjust);
9621 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9622 arguments for a member template.
9623 In that case each TREE_VEC in ORIG_T represents a level of template
9624 arguments, and ORIG_T won't carry any non defaulted argument count.
9625 It will rather be the nested TREE_VECs that will carry one.
9626 In other words, ORIG_T carries a non defaulted argument count only
9627 if it doesn't contain any nested TREE_VEC. */
9628 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9629 {
9630 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9631 count += expanded_len_adjust;
9632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9633 }
9634 for (i = 0, out = 0; i < len; i++)
9635 {
9636 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9637 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9638 && TREE_CODE (elts[i]) == TREE_VEC)
9639 {
9640 int idx;
9641
9642 /* Now expand the template argument pack "in place". */
9643 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9644 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9645 }
9646 else
9647 {
9648 TREE_VEC_ELT (t, out) = elts[i];
9649 out++;
9650 }
9651 }
9652
9653 return t;
9654 }
9655
9656 /* Return the result of substituting ARGS into the template parameters
9657 given by PARMS. If there are m levels of ARGS and m + n levels of
9658 PARMS, then the result will contain n levels of PARMS. For
9659 example, if PARMS is `template <class T> template <class U>
9660 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9661 result will be `template <int*, double, class V>'. */
9662
9663 static tree
9664 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9665 {
9666 tree r = NULL_TREE;
9667 tree* new_parms;
9668
9669 /* When substituting into a template, we must set
9670 PROCESSING_TEMPLATE_DECL as the template parameters may be
9671 dependent if they are based on one-another, and the dependency
9672 predicates are short-circuit outside of templates. */
9673 ++processing_template_decl;
9674
9675 for (new_parms = &r;
9676 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9677 new_parms = &(TREE_CHAIN (*new_parms)),
9678 parms = TREE_CHAIN (parms))
9679 {
9680 tree new_vec =
9681 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9682 int i;
9683
9684 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9685 {
9686 tree tuple;
9687
9688 if (parms == error_mark_node)
9689 continue;
9690
9691 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9692
9693 if (tuple == error_mark_node)
9694 continue;
9695
9696 TREE_VEC_ELT (new_vec, i) =
9697 tsubst_template_parm (tuple, args, complain);
9698 }
9699
9700 *new_parms =
9701 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9702 - TMPL_ARGS_DEPTH (args)),
9703 new_vec, NULL_TREE);
9704 }
9705
9706 --processing_template_decl;
9707
9708 return r;
9709 }
9710
9711 /* Return the result of substituting ARGS into one template parameter
9712 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9713 parameter and which TREE_PURPOSE is the default argument of the
9714 template parameter. */
9715
9716 static tree
9717 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9718 {
9719 tree default_value, parm_decl;
9720
9721 if (args == NULL_TREE
9722 || t == NULL_TREE
9723 || t == error_mark_node)
9724 return t;
9725
9726 gcc_assert (TREE_CODE (t) == TREE_LIST);
9727
9728 default_value = TREE_PURPOSE (t);
9729 parm_decl = TREE_VALUE (t);
9730
9731 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9732 if (TREE_CODE (parm_decl) == PARM_DECL
9733 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9734 parm_decl = error_mark_node;
9735 default_value = tsubst_template_arg (default_value, args,
9736 complain, NULL_TREE);
9737
9738 return build_tree_list (default_value, parm_decl);
9739 }
9740
9741 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9742 type T. If T is not an aggregate or enumeration type, it is
9743 handled as if by tsubst. IN_DECL is as for tsubst. If
9744 ENTERING_SCOPE is nonzero, T is the context for a template which
9745 we are presently tsubst'ing. Return the substituted value. */
9746
9747 static tree
9748 tsubst_aggr_type (tree t,
9749 tree args,
9750 tsubst_flags_t complain,
9751 tree in_decl,
9752 int entering_scope)
9753 {
9754 if (t == NULL_TREE)
9755 return NULL_TREE;
9756
9757 switch (TREE_CODE (t))
9758 {
9759 case RECORD_TYPE:
9760 if (TYPE_PTRMEMFUNC_P (t))
9761 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9762
9763 /* Else fall through. */
9764 case ENUMERAL_TYPE:
9765 case UNION_TYPE:
9766 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9767 {
9768 tree argvec;
9769 tree context;
9770 tree r;
9771 int saved_unevaluated_operand;
9772 int saved_inhibit_evaluation_warnings;
9773
9774 /* In "sizeof(X<I>)" we need to evaluate "I". */
9775 saved_unevaluated_operand = cp_unevaluated_operand;
9776 cp_unevaluated_operand = 0;
9777 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9778 c_inhibit_evaluation_warnings = 0;
9779
9780 /* First, determine the context for the type we are looking
9781 up. */
9782 context = TYPE_CONTEXT (t);
9783 if (context && TYPE_P (context))
9784 {
9785 context = tsubst_aggr_type (context, args, complain,
9786 in_decl, /*entering_scope=*/1);
9787 /* If context is a nested class inside a class template,
9788 it may still need to be instantiated (c++/33959). */
9789 context = complete_type (context);
9790 }
9791
9792 /* Then, figure out what arguments are appropriate for the
9793 type we are trying to find. For example, given:
9794
9795 template <class T> struct S;
9796 template <class T, class U> void f(T, U) { S<U> su; }
9797
9798 and supposing that we are instantiating f<int, double>,
9799 then our ARGS will be {int, double}, but, when looking up
9800 S we only want {double}. */
9801 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9802 complain, in_decl);
9803 if (argvec == error_mark_node)
9804 r = error_mark_node;
9805 else
9806 {
9807 r = lookup_template_class (t, argvec, in_decl, context,
9808 entering_scope, complain);
9809 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9810 }
9811
9812 cp_unevaluated_operand = saved_unevaluated_operand;
9813 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9814
9815 return r;
9816 }
9817 else
9818 /* This is not a template type, so there's nothing to do. */
9819 return t;
9820
9821 default:
9822 return tsubst (t, args, complain, in_decl);
9823 }
9824 }
9825
9826 /* Substitute into the default argument ARG (a default argument for
9827 FN), which has the indicated TYPE. */
9828
9829 tree
9830 tsubst_default_argument (tree fn, tree type, tree arg)
9831 {
9832 tree saved_class_ptr = NULL_TREE;
9833 tree saved_class_ref = NULL_TREE;
9834 int errs = errorcount + sorrycount;
9835
9836 /* This can happen in invalid code. */
9837 if (TREE_CODE (arg) == DEFAULT_ARG)
9838 return arg;
9839
9840 /* This default argument came from a template. Instantiate the
9841 default argument here, not in tsubst. In the case of
9842 something like:
9843
9844 template <class T>
9845 struct S {
9846 static T t();
9847 void f(T = t());
9848 };
9849
9850 we must be careful to do name lookup in the scope of S<T>,
9851 rather than in the current class. */
9852 push_access_scope (fn);
9853 /* The "this" pointer is not valid in a default argument. */
9854 if (cfun)
9855 {
9856 saved_class_ptr = current_class_ptr;
9857 cp_function_chain->x_current_class_ptr = NULL_TREE;
9858 saved_class_ref = current_class_ref;
9859 cp_function_chain->x_current_class_ref = NULL_TREE;
9860 }
9861
9862 push_deferring_access_checks(dk_no_deferred);
9863 /* The default argument expression may cause implicitly defined
9864 member functions to be synthesized, which will result in garbage
9865 collection. We must treat this situation as if we were within
9866 the body of function so as to avoid collecting live data on the
9867 stack. */
9868 ++function_depth;
9869 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9870 tf_warning_or_error, NULL_TREE,
9871 /*integral_constant_expression_p=*/false);
9872 --function_depth;
9873 pop_deferring_access_checks();
9874
9875 /* Restore the "this" pointer. */
9876 if (cfun)
9877 {
9878 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9879 cp_function_chain->x_current_class_ref = saved_class_ref;
9880 }
9881
9882 if (errorcount+sorrycount > errs)
9883 inform (input_location,
9884 " when instantiating default argument for call to %D", fn);
9885
9886 /* Make sure the default argument is reasonable. */
9887 arg = check_default_argument (type, arg);
9888
9889 pop_access_scope (fn);
9890
9891 return arg;
9892 }
9893
9894 /* Substitute into all the default arguments for FN. */
9895
9896 static void
9897 tsubst_default_arguments (tree fn)
9898 {
9899 tree arg;
9900 tree tmpl_args;
9901
9902 tmpl_args = DECL_TI_ARGS (fn);
9903
9904 /* If this function is not yet instantiated, we certainly don't need
9905 its default arguments. */
9906 if (uses_template_parms (tmpl_args))
9907 return;
9908 /* Don't do this again for clones. */
9909 if (DECL_CLONED_FUNCTION_P (fn))
9910 return;
9911
9912 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9913 arg;
9914 arg = TREE_CHAIN (arg))
9915 if (TREE_PURPOSE (arg))
9916 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9917 TREE_VALUE (arg),
9918 TREE_PURPOSE (arg));
9919 }
9920
9921 /* Substitute the ARGS into the T, which is a _DECL. Return the
9922 result of the substitution. Issue error and warning messages under
9923 control of COMPLAIN. */
9924
9925 static tree
9926 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9927 {
9928 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9929 location_t saved_loc;
9930 tree r = NULL_TREE;
9931 tree in_decl = t;
9932 hashval_t hash = 0;
9933
9934 /* Set the filename and linenumber to improve error-reporting. */
9935 saved_loc = input_location;
9936 input_location = DECL_SOURCE_LOCATION (t);
9937
9938 switch (TREE_CODE (t))
9939 {
9940 case TEMPLATE_DECL:
9941 {
9942 /* We can get here when processing a member function template,
9943 member class template, or template template parameter. */
9944 tree decl = DECL_TEMPLATE_RESULT (t);
9945 tree spec;
9946 tree tmpl_args;
9947 tree full_args;
9948
9949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9950 {
9951 /* Template template parameter is treated here. */
9952 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9953 if (new_type == error_mark_node)
9954 RETURN (error_mark_node);
9955 /* If we get a real template back, return it. This can happen in
9956 the context of most_specialized_class. */
9957 if (TREE_CODE (new_type) == TEMPLATE_DECL)
9958 return new_type;
9959
9960 r = copy_decl (t);
9961 DECL_CHAIN (r) = NULL_TREE;
9962 TREE_TYPE (r) = new_type;
9963 DECL_TEMPLATE_RESULT (r)
9964 = build_decl (DECL_SOURCE_LOCATION (decl),
9965 TYPE_DECL, DECL_NAME (decl), new_type);
9966 DECL_TEMPLATE_PARMS (r)
9967 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9968 complain);
9969 TYPE_NAME (new_type) = r;
9970 break;
9971 }
9972
9973 /* We might already have an instance of this template.
9974 The ARGS are for the surrounding class type, so the
9975 full args contain the tsubst'd args for the context,
9976 plus the innermost args from the template decl. */
9977 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9978 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9979 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9980 /* Because this is a template, the arguments will still be
9981 dependent, even after substitution. If
9982 PROCESSING_TEMPLATE_DECL is not set, the dependency
9983 predicates will short-circuit. */
9984 ++processing_template_decl;
9985 full_args = tsubst_template_args (tmpl_args, args,
9986 complain, in_decl);
9987 --processing_template_decl;
9988 if (full_args == error_mark_node)
9989 RETURN (error_mark_node);
9990
9991 /* If this is a default template template argument,
9992 tsubst might not have changed anything. */
9993 if (full_args == tmpl_args)
9994 RETURN (t);
9995
9996 hash = hash_tmpl_and_args (t, full_args);
9997 spec = retrieve_specialization (t, full_args, hash);
9998 if (spec != NULL_TREE)
9999 {
10000 r = spec;
10001 break;
10002 }
10003
10004 /* Make a new template decl. It will be similar to the
10005 original, but will record the current template arguments.
10006 We also create a new function declaration, which is just
10007 like the old one, but points to this new template, rather
10008 than the old one. */
10009 r = copy_decl (t);
10010 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10011 DECL_CHAIN (r) = NULL_TREE;
10012
10013 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10014
10015 if (TREE_CODE (decl) == TYPE_DECL
10016 && !TYPE_DECL_ALIAS_P (decl))
10017 {
10018 tree new_type;
10019 ++processing_template_decl;
10020 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10021 --processing_template_decl;
10022 if (new_type == error_mark_node)
10023 RETURN (error_mark_node);
10024
10025 TREE_TYPE (r) = new_type;
10026 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10027 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10028 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10029 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10030 }
10031 else
10032 {
10033 tree new_decl;
10034 ++processing_template_decl;
10035 new_decl = tsubst (decl, args, complain, in_decl);
10036 --processing_template_decl;
10037 if (new_decl == error_mark_node)
10038 RETURN (error_mark_node);
10039
10040 DECL_TEMPLATE_RESULT (r) = new_decl;
10041 DECL_TI_TEMPLATE (new_decl) = r;
10042 TREE_TYPE (r) = TREE_TYPE (new_decl);
10043 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10044 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10045 }
10046
10047 SET_DECL_IMPLICIT_INSTANTIATION (r);
10048 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10049 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10050
10051 /* The template parameters for this new template are all the
10052 template parameters for the old template, except the
10053 outermost level of parameters. */
10054 DECL_TEMPLATE_PARMS (r)
10055 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10056 complain);
10057
10058 if (PRIMARY_TEMPLATE_P (t))
10059 DECL_PRIMARY_TEMPLATE (r) = r;
10060
10061 if (TREE_CODE (decl) != TYPE_DECL)
10062 /* Record this non-type partial instantiation. */
10063 register_specialization (r, t,
10064 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10065 false, hash);
10066 }
10067 break;
10068
10069 case FUNCTION_DECL:
10070 {
10071 tree ctx;
10072 tree argvec = NULL_TREE;
10073 tree *friends;
10074 tree gen_tmpl;
10075 tree type;
10076 int member;
10077 int args_depth;
10078 int parms_depth;
10079
10080 /* Nobody should be tsubst'ing into non-template functions. */
10081 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10082
10083 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10084 {
10085 tree spec;
10086 bool dependent_p;
10087
10088 /* If T is not dependent, just return it. We have to
10089 increment PROCESSING_TEMPLATE_DECL because
10090 value_dependent_expression_p assumes that nothing is
10091 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10092 ++processing_template_decl;
10093 dependent_p = value_dependent_expression_p (t);
10094 --processing_template_decl;
10095 if (!dependent_p)
10096 RETURN (t);
10097
10098 /* Calculate the most general template of which R is a
10099 specialization, and the complete set of arguments used to
10100 specialize R. */
10101 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10102 argvec = tsubst_template_args (DECL_TI_ARGS
10103 (DECL_TEMPLATE_RESULT
10104 (DECL_TI_TEMPLATE (t))),
10105 args, complain, in_decl);
10106 if (argvec == error_mark_node)
10107 RETURN (error_mark_node);
10108
10109 /* Check to see if we already have this specialization. */
10110 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10111 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10112
10113 if (spec)
10114 {
10115 r = spec;
10116 break;
10117 }
10118
10119 /* We can see more levels of arguments than parameters if
10120 there was a specialization of a member template, like
10121 this:
10122
10123 template <class T> struct S { template <class U> void f(); }
10124 template <> template <class U> void S<int>::f(U);
10125
10126 Here, we'll be substituting into the specialization,
10127 because that's where we can find the code we actually
10128 want to generate, but we'll have enough arguments for
10129 the most general template.
10130
10131 We also deal with the peculiar case:
10132
10133 template <class T> struct S {
10134 template <class U> friend void f();
10135 };
10136 template <class U> void f() {}
10137 template S<int>;
10138 template void f<double>();
10139
10140 Here, the ARGS for the instantiation of will be {int,
10141 double}. But, we only need as many ARGS as there are
10142 levels of template parameters in CODE_PATTERN. We are
10143 careful not to get fooled into reducing the ARGS in
10144 situations like:
10145
10146 template <class T> struct S { template <class U> void f(U); }
10147 template <class T> template <> void S<T>::f(int) {}
10148
10149 which we can spot because the pattern will be a
10150 specialization in this case. */
10151 args_depth = TMPL_ARGS_DEPTH (args);
10152 parms_depth =
10153 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10154 if (args_depth > parms_depth
10155 && !DECL_TEMPLATE_SPECIALIZATION (t))
10156 args = get_innermost_template_args (args, parms_depth);
10157 }
10158 else
10159 {
10160 /* This special case arises when we have something like this:
10161
10162 template <class T> struct S {
10163 friend void f<int>(int, double);
10164 };
10165
10166 Here, the DECL_TI_TEMPLATE for the friend declaration
10167 will be an IDENTIFIER_NODE. We are being called from
10168 tsubst_friend_function, and we want only to create a
10169 new decl (R) with appropriate types so that we can call
10170 determine_specialization. */
10171 gen_tmpl = NULL_TREE;
10172 }
10173
10174 if (DECL_CLASS_SCOPE_P (t))
10175 {
10176 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10177 member = 2;
10178 else
10179 member = 1;
10180 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10181 complain, t, /*entering_scope=*/1);
10182 }
10183 else
10184 {
10185 member = 0;
10186 ctx = DECL_CONTEXT (t);
10187 }
10188 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10189 if (type == error_mark_node)
10190 RETURN (error_mark_node);
10191
10192 /* If we hit excessive deduction depth, the type is bogus even if
10193 it isn't error_mark_node, so don't build a decl. */
10194 if (excessive_deduction_depth)
10195 RETURN (error_mark_node);
10196
10197 /* We do NOT check for matching decls pushed separately at this
10198 point, as they may not represent instantiations of this
10199 template, and in any case are considered separate under the
10200 discrete model. */
10201 r = copy_decl (t);
10202 DECL_USE_TEMPLATE (r) = 0;
10203 TREE_TYPE (r) = type;
10204 /* Clear out the mangled name and RTL for the instantiation. */
10205 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10206 SET_DECL_RTL (r, NULL);
10207 /* Leave DECL_INITIAL set on deleted instantiations. */
10208 if (!DECL_DELETED_FN (r))
10209 DECL_INITIAL (r) = NULL_TREE;
10210 DECL_CONTEXT (r) = ctx;
10211
10212 if (member && DECL_CONV_FN_P (r))
10213 /* Type-conversion operator. Reconstruct the name, in
10214 case it's the name of one of the template's parameters. */
10215 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10216
10217 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10218 complain, t);
10219 DECL_RESULT (r) = NULL_TREE;
10220
10221 TREE_STATIC (r) = 0;
10222 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10223 DECL_EXTERNAL (r) = 1;
10224 /* If this is an instantiation of a function with internal
10225 linkage, we already know what object file linkage will be
10226 assigned to the instantiation. */
10227 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10228 DECL_DEFER_OUTPUT (r) = 0;
10229 DECL_CHAIN (r) = NULL_TREE;
10230 DECL_PENDING_INLINE_INFO (r) = 0;
10231 DECL_PENDING_INLINE_P (r) = 0;
10232 DECL_SAVED_TREE (r) = NULL_TREE;
10233 DECL_STRUCT_FUNCTION (r) = NULL;
10234 TREE_USED (r) = 0;
10235 /* We'll re-clone as appropriate in instantiate_template. */
10236 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10237
10238 /* If we aren't complaining now, return on error before we register
10239 the specialization so that we'll complain eventually. */
10240 if ((complain & tf_error) == 0
10241 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10242 && !grok_op_properties (r, /*complain=*/false))
10243 RETURN (error_mark_node);
10244
10245 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10246 this in the special friend case mentioned above where
10247 GEN_TMPL is NULL. */
10248 if (gen_tmpl)
10249 {
10250 DECL_TEMPLATE_INFO (r)
10251 = build_template_info (gen_tmpl, argvec);
10252 SET_DECL_IMPLICIT_INSTANTIATION (r);
10253 register_specialization (r, gen_tmpl, argvec, false, hash);
10254
10255 /* We're not supposed to instantiate default arguments
10256 until they are called, for a template. But, for a
10257 declaration like:
10258
10259 template <class T> void f ()
10260 { extern void g(int i = T()); }
10261
10262 we should do the substitution when the template is
10263 instantiated. We handle the member function case in
10264 instantiate_class_template since the default arguments
10265 might refer to other members of the class. */
10266 if (!member
10267 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10268 && !uses_template_parms (argvec))
10269 tsubst_default_arguments (r);
10270 }
10271 else
10272 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10273
10274 /* Copy the list of befriending classes. */
10275 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10276 *friends;
10277 friends = &TREE_CHAIN (*friends))
10278 {
10279 *friends = copy_node (*friends);
10280 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10281 args, complain,
10282 in_decl);
10283 }
10284
10285 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10286 {
10287 maybe_retrofit_in_chrg (r);
10288 if (DECL_CONSTRUCTOR_P (r))
10289 grok_ctor_properties (ctx, r);
10290 if (DECL_INHERITED_CTOR_BASE (r))
10291 deduce_inheriting_ctor (r);
10292 /* If this is an instantiation of a member template, clone it.
10293 If it isn't, that'll be handled by
10294 clone_constructors_and_destructors. */
10295 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10296 clone_function_decl (r, /*update_method_vec_p=*/0);
10297 }
10298 else if ((complain & tf_error) != 0
10299 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10300 && !grok_op_properties (r, /*complain=*/true))
10301 RETURN (error_mark_node);
10302
10303 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10304 SET_DECL_FRIEND_CONTEXT (r,
10305 tsubst (DECL_FRIEND_CONTEXT (t),
10306 args, complain, in_decl));
10307
10308 /* Possibly limit visibility based on template args. */
10309 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10310 if (DECL_VISIBILITY_SPECIFIED (t))
10311 {
10312 DECL_VISIBILITY_SPECIFIED (r) = 0;
10313 DECL_ATTRIBUTES (r)
10314 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10315 }
10316 determine_visibility (r);
10317 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10318 && !processing_template_decl)
10319 defaulted_late_check (r);
10320
10321 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10322 args, complain, in_decl);
10323 }
10324 break;
10325
10326 case PARM_DECL:
10327 {
10328 tree type = NULL_TREE;
10329 int i, len = 1;
10330 tree expanded_types = NULL_TREE;
10331 tree prev_r = NULL_TREE;
10332 tree first_r = NULL_TREE;
10333
10334 if (FUNCTION_PARAMETER_PACK_P (t))
10335 {
10336 /* If there is a local specialization that isn't a
10337 parameter pack, it means that we're doing a "simple"
10338 substitution from inside tsubst_pack_expansion. Just
10339 return the local specialization (which will be a single
10340 parm). */
10341 tree spec = retrieve_local_specialization (t);
10342 if (spec
10343 && TREE_CODE (spec) == PARM_DECL
10344 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10345 RETURN (spec);
10346
10347 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10348 the parameters in this function parameter pack. */
10349 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10350 complain, in_decl);
10351 if (TREE_CODE (expanded_types) == TREE_VEC)
10352 {
10353 len = TREE_VEC_LENGTH (expanded_types);
10354
10355 /* Zero-length parameter packs are boring. Just substitute
10356 into the chain. */
10357 if (len == 0)
10358 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10359 TREE_CHAIN (t)));
10360 }
10361 else
10362 {
10363 /* All we did was update the type. Make a note of that. */
10364 type = expanded_types;
10365 expanded_types = NULL_TREE;
10366 }
10367 }
10368
10369 /* Loop through all of the parameter's we'll build. When T is
10370 a function parameter pack, LEN is the number of expanded
10371 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10372 r = NULL_TREE;
10373 for (i = 0; i < len; ++i)
10374 {
10375 prev_r = r;
10376 r = copy_node (t);
10377 if (DECL_TEMPLATE_PARM_P (t))
10378 SET_DECL_TEMPLATE_PARM_P (r);
10379
10380 if (expanded_types)
10381 /* We're on the Ith parameter of the function parameter
10382 pack. */
10383 {
10384 /* An argument of a function parameter pack is not a parameter
10385 pack. */
10386 FUNCTION_PARAMETER_PACK_P (r) = false;
10387
10388 /* Get the Ith type. */
10389 type = TREE_VEC_ELT (expanded_types, i);
10390
10391 /* Rename the parameter to include the index. */
10392 DECL_NAME (r)
10393 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10394 }
10395 else if (!type)
10396 /* We're dealing with a normal parameter. */
10397 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10398
10399 type = type_decays_to (type);
10400 TREE_TYPE (r) = type;
10401 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10402
10403 if (DECL_INITIAL (r))
10404 {
10405 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10406 DECL_INITIAL (r) = TREE_TYPE (r);
10407 else
10408 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10409 complain, in_decl);
10410 }
10411
10412 DECL_CONTEXT (r) = NULL_TREE;
10413
10414 if (!DECL_TEMPLATE_PARM_P (r))
10415 DECL_ARG_TYPE (r) = type_passed_as (type);
10416
10417 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10418 args, complain, in_decl);
10419
10420 /* Keep track of the first new parameter we
10421 generate. That's what will be returned to the
10422 caller. */
10423 if (!first_r)
10424 first_r = r;
10425
10426 /* Build a proper chain of parameters when substituting
10427 into a function parameter pack. */
10428 if (prev_r)
10429 DECL_CHAIN (prev_r) = r;
10430 }
10431
10432 /* If cp_unevaluated_operand is set, we're just looking for a
10433 single dummy parameter, so don't keep going. */
10434 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10435 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10436 complain, DECL_CHAIN (t));
10437
10438 /* FIRST_R contains the start of the chain we've built. */
10439 r = first_r;
10440 }
10441 break;
10442
10443 case FIELD_DECL:
10444 {
10445 tree type;
10446
10447 r = copy_decl (t);
10448 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10449 if (type == error_mark_node)
10450 RETURN (error_mark_node);
10451 TREE_TYPE (r) = type;
10452 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10453
10454 if (DECL_C_BIT_FIELD (r))
10455 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10456 non-bit-fields DECL_INITIAL is a non-static data member
10457 initializer, which gets deferred instantiation. */
10458 DECL_INITIAL (r)
10459 = tsubst_expr (DECL_INITIAL (t), args,
10460 complain, in_decl,
10461 /*integral_constant_expression_p=*/true);
10462 else if (DECL_INITIAL (t))
10463 {
10464 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10465 NSDMI in perform_member_init. Still set DECL_INITIAL
10466 so that we know there is one. */
10467 DECL_INITIAL (r) = void_zero_node;
10468 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10469 retrofit_lang_decl (r);
10470 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10471 }
10472 /* We don't have to set DECL_CONTEXT here; it is set by
10473 finish_member_declaration. */
10474 DECL_CHAIN (r) = NULL_TREE;
10475 if (VOID_TYPE_P (type))
10476 error ("instantiation of %q+D as type %qT", r, type);
10477
10478 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10479 args, complain, in_decl);
10480 }
10481 break;
10482
10483 case USING_DECL:
10484 /* We reach here only for member using decls. We also need to check
10485 uses_template_parms because DECL_DEPENDENT_P is not set for a
10486 using-declaration that designates a member of the current
10487 instantiation (c++/53549). */
10488 if (DECL_DEPENDENT_P (t)
10489 || uses_template_parms (USING_DECL_SCOPE (t)))
10490 {
10491 tree scope = USING_DECL_SCOPE (t);
10492 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10493 complain, in_decl);
10494 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10495 /* Handle 'using T::T'. */
10496 if (TYPE_NAME (scope)
10497 && name == TYPE_IDENTIFIER (scope))
10498 name = TYPE_IDENTIFIER (inst_scope);
10499 r = do_class_using_decl (inst_scope, name);
10500 if (!r)
10501 r = error_mark_node;
10502 else
10503 {
10504 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10505 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10506 }
10507 }
10508 else
10509 {
10510 r = copy_node (t);
10511 DECL_CHAIN (r) = NULL_TREE;
10512 }
10513 break;
10514
10515 case TYPE_DECL:
10516 case VAR_DECL:
10517 {
10518 tree argvec = NULL_TREE;
10519 tree gen_tmpl = NULL_TREE;
10520 tree spec;
10521 tree tmpl = NULL_TREE;
10522 tree ctx;
10523 tree type = NULL_TREE;
10524 bool local_p;
10525
10526 if (TREE_CODE (t) == TYPE_DECL
10527 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10528 {
10529 /* If this is the canonical decl, we don't have to
10530 mess with instantiations, and often we can't (for
10531 typename, template type parms and such). Note that
10532 TYPE_NAME is not correct for the above test if
10533 we've copied the type for a typedef. */
10534 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10535 if (type == error_mark_node)
10536 RETURN (error_mark_node);
10537 r = TYPE_NAME (type);
10538 break;
10539 }
10540
10541 /* Check to see if we already have the specialization we
10542 need. */
10543 spec = NULL_TREE;
10544 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10545 {
10546 /* T is a static data member or namespace-scope entity.
10547 We have to substitute into namespace-scope variables
10548 (even though such entities are never templates) because
10549 of cases like:
10550
10551 template <class T> void f() { extern T t; }
10552
10553 where the entity referenced is not known until
10554 instantiation time. */
10555 local_p = false;
10556 ctx = DECL_CONTEXT (t);
10557 if (DECL_CLASS_SCOPE_P (t))
10558 {
10559 ctx = tsubst_aggr_type (ctx, args,
10560 complain,
10561 in_decl, /*entering_scope=*/1);
10562 /* If CTX is unchanged, then T is in fact the
10563 specialization we want. That situation occurs when
10564 referencing a static data member within in its own
10565 class. We can use pointer equality, rather than
10566 same_type_p, because DECL_CONTEXT is always
10567 canonical... */
10568 if (ctx == DECL_CONTEXT (t)
10569 && (TREE_CODE (t) != TYPE_DECL
10570 /* ... unless T is a member template; in which
10571 case our caller can be willing to create a
10572 specialization of that template represented
10573 by T. */
10574 || !(DECL_TI_TEMPLATE (t)
10575 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10576 spec = t;
10577 }
10578
10579 if (!spec)
10580 {
10581 tmpl = DECL_TI_TEMPLATE (t);
10582 gen_tmpl = most_general_template (tmpl);
10583 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10584 if (argvec == error_mark_node)
10585 RETURN (error_mark_node);
10586 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10587 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10588 }
10589 }
10590 else
10591 {
10592 /* A local variable. */
10593 local_p = true;
10594 /* Subsequent calls to pushdecl will fill this in. */
10595 ctx = NULL_TREE;
10596 spec = retrieve_local_specialization (t);
10597 }
10598 /* If we already have the specialization we need, there is
10599 nothing more to do. */
10600 if (spec)
10601 {
10602 r = spec;
10603 break;
10604 }
10605
10606 if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t))
10607 {
10608 /* Just use name lookup to find a member alias for an anonymous
10609 union, but then add it to the hash table. */
10610 r = lookup_name (DECL_NAME (t));
10611 gcc_assert (DECL_ANON_UNION_VAR_P (r));
10612 register_local_specialization (r, t);
10613 break;
10614 }
10615
10616 /* Create a new node for the specialization we need. */
10617 r = copy_decl (t);
10618 if (type == NULL_TREE)
10619 {
10620 if (is_typedef_decl (t))
10621 type = DECL_ORIGINAL_TYPE (t);
10622 else
10623 type = TREE_TYPE (t);
10624 if (TREE_CODE (t) == VAR_DECL
10625 && VAR_HAD_UNKNOWN_BOUND (t)
10626 && type != error_mark_node)
10627 type = strip_array_domain (type);
10628 type = tsubst (type, args, complain, in_decl);
10629 }
10630 if (TREE_CODE (r) == VAR_DECL)
10631 {
10632 /* Even if the original location is out of scope, the
10633 newly substituted one is not. */
10634 DECL_DEAD_FOR_LOCAL (r) = 0;
10635 DECL_INITIALIZED_P (r) = 0;
10636 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10637 if (type == error_mark_node)
10638 RETURN (error_mark_node);
10639 if (TREE_CODE (type) == FUNCTION_TYPE)
10640 {
10641 /* It may seem that this case cannot occur, since:
10642
10643 typedef void f();
10644 void g() { f x; }
10645
10646 declares a function, not a variable. However:
10647
10648 typedef void f();
10649 template <typename T> void g() { T t; }
10650 template void g<f>();
10651
10652 is an attempt to declare a variable with function
10653 type. */
10654 error ("variable %qD has function type",
10655 /* R is not yet sufficiently initialized, so we
10656 just use its name. */
10657 DECL_NAME (r));
10658 RETURN (error_mark_node);
10659 }
10660 type = complete_type (type);
10661 /* Wait until cp_finish_decl to set this again, to handle
10662 circular dependency (template/instantiate6.C). */
10663 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10664 type = check_var_type (DECL_NAME (r), type);
10665
10666 if (DECL_HAS_VALUE_EXPR_P (t))
10667 {
10668 tree ve = DECL_VALUE_EXPR (t);
10669 ve = tsubst_expr (ve, args, complain, in_decl,
10670 /*constant_expression_p=*/false);
10671 if (REFERENCE_REF_P (ve))
10672 {
10673 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10674 ve = TREE_OPERAND (ve, 0);
10675 }
10676 SET_DECL_VALUE_EXPR (r, ve);
10677 }
10678 }
10679 else if (DECL_SELF_REFERENCE_P (t))
10680 SET_DECL_SELF_REFERENCE_P (r);
10681 TREE_TYPE (r) = type;
10682 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10683 DECL_CONTEXT (r) = ctx;
10684 /* Clear out the mangled name and RTL for the instantiation. */
10685 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10686 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10687 SET_DECL_RTL (r, NULL);
10688 /* The initializer must not be expanded until it is required;
10689 see [temp.inst]. */
10690 DECL_INITIAL (r) = NULL_TREE;
10691 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10692 SET_DECL_RTL (r, NULL);
10693 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10694 if (TREE_CODE (r) == VAR_DECL)
10695 {
10696 /* Possibly limit visibility based on template args. */
10697 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10698 if (DECL_VISIBILITY_SPECIFIED (t))
10699 {
10700 DECL_VISIBILITY_SPECIFIED (r) = 0;
10701 DECL_ATTRIBUTES (r)
10702 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10703 }
10704 determine_visibility (r);
10705 }
10706
10707 if (!local_p)
10708 {
10709 /* A static data member declaration is always marked
10710 external when it is declared in-class, even if an
10711 initializer is present. We mimic the non-template
10712 processing here. */
10713 DECL_EXTERNAL (r) = 1;
10714
10715 register_specialization (r, gen_tmpl, argvec, false, hash);
10716 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10717 SET_DECL_IMPLICIT_INSTANTIATION (r);
10718 }
10719 else if (cp_unevaluated_operand)
10720 {
10721 /* We're substituting this var in a decltype outside of its
10722 scope, such as for a lambda return type. Don't add it to
10723 local_specializations, do perform auto deduction. */
10724 tree auto_node = type_uses_auto (type);
10725 if (auto_node)
10726 {
10727 tree init
10728 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10729 /*constant_expression_p=*/false);
10730 init = resolve_nondeduced_context (init);
10731 TREE_TYPE (r) = type
10732 = do_auto_deduction (type, init, auto_node);
10733 }
10734 }
10735 else
10736 register_local_specialization (r, t);
10737
10738 DECL_CHAIN (r) = NULL_TREE;
10739
10740 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10741 /*flags=*/0,
10742 args, complain, in_decl);
10743
10744 /* Preserve a typedef that names a type. */
10745 if (is_typedef_decl (r))
10746 {
10747 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10748 set_underlying_type (r);
10749 }
10750
10751 layout_decl (r, 0);
10752 }
10753 break;
10754
10755 default:
10756 gcc_unreachable ();
10757 }
10758 #undef RETURN
10759
10760 out:
10761 /* Restore the file and line information. */
10762 input_location = saved_loc;
10763
10764 return r;
10765 }
10766
10767 /* Substitute into the ARG_TYPES of a function type.
10768 If END is a TREE_CHAIN, leave it and any following types
10769 un-substituted. */
10770
10771 static tree
10772 tsubst_arg_types (tree arg_types,
10773 tree args,
10774 tree end,
10775 tsubst_flags_t complain,
10776 tree in_decl)
10777 {
10778 tree remaining_arg_types;
10779 tree type = NULL_TREE;
10780 int i = 1;
10781 tree expanded_args = NULL_TREE;
10782 tree default_arg;
10783
10784 if (!arg_types || arg_types == void_list_node || arg_types == end)
10785 return arg_types;
10786
10787 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10788 args, end, complain, in_decl);
10789 if (remaining_arg_types == error_mark_node)
10790 return error_mark_node;
10791
10792 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10793 {
10794 /* For a pack expansion, perform substitution on the
10795 entire expression. Later on, we'll handle the arguments
10796 one-by-one. */
10797 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10798 args, complain, in_decl);
10799
10800 if (TREE_CODE (expanded_args) == TREE_VEC)
10801 /* So that we'll spin through the parameters, one by one. */
10802 i = TREE_VEC_LENGTH (expanded_args);
10803 else
10804 {
10805 /* We only partially substituted into the parameter
10806 pack. Our type is TYPE_PACK_EXPANSION. */
10807 type = expanded_args;
10808 expanded_args = NULL_TREE;
10809 }
10810 }
10811
10812 while (i > 0) {
10813 --i;
10814
10815 if (expanded_args)
10816 type = TREE_VEC_ELT (expanded_args, i);
10817 else if (!type)
10818 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10819
10820 if (type == error_mark_node)
10821 return error_mark_node;
10822 if (VOID_TYPE_P (type))
10823 {
10824 if (complain & tf_error)
10825 {
10826 error ("invalid parameter type %qT", type);
10827 if (in_decl)
10828 error ("in declaration %q+D", in_decl);
10829 }
10830 return error_mark_node;
10831 }
10832
10833 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10834 top-level qualifiers as required. */
10835 type = cv_unqualified (type_decays_to (type));
10836
10837 /* We do not substitute into default arguments here. The standard
10838 mandates that they be instantiated only when needed, which is
10839 done in build_over_call. */
10840 default_arg = TREE_PURPOSE (arg_types);
10841
10842 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10843 {
10844 /* We've instantiated a template before its default arguments
10845 have been parsed. This can happen for a nested template
10846 class, and is not an error unless we require the default
10847 argument in a call of this function. */
10848 remaining_arg_types =
10849 tree_cons (default_arg, type, remaining_arg_types);
10850 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
10851 }
10852 else
10853 remaining_arg_types =
10854 hash_tree_cons (default_arg, type, remaining_arg_types);
10855 }
10856
10857 return remaining_arg_types;
10858 }
10859
10860 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10861 *not* handle the exception-specification for FNTYPE, because the
10862 initial substitution of explicitly provided template parameters
10863 during argument deduction forbids substitution into the
10864 exception-specification:
10865
10866 [temp.deduct]
10867
10868 All references in the function type of the function template to the
10869 corresponding template parameters are replaced by the specified tem-
10870 plate argument values. If a substitution in a template parameter or
10871 in the function type of the function template results in an invalid
10872 type, type deduction fails. [Note: The equivalent substitution in
10873 exception specifications is done only when the function is instanti-
10874 ated, at which point a program is ill-formed if the substitution
10875 results in an invalid type.] */
10876
10877 static tree
10878 tsubst_function_type (tree t,
10879 tree args,
10880 tsubst_flags_t complain,
10881 tree in_decl)
10882 {
10883 tree return_type;
10884 tree arg_types;
10885 tree fntype;
10886
10887 /* The TYPE_CONTEXT is not used for function/method types. */
10888 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10889
10890 /* Substitute the return type. */
10891 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10892 if (return_type == error_mark_node)
10893 return error_mark_node;
10894 /* The standard does not presently indicate that creation of a
10895 function type with an invalid return type is a deduction failure.
10896 However, that is clearly analogous to creating an array of "void"
10897 or a reference to a reference. This is core issue #486. */
10898 if (TREE_CODE (return_type) == ARRAY_TYPE
10899 || TREE_CODE (return_type) == FUNCTION_TYPE)
10900 {
10901 if (complain & tf_error)
10902 {
10903 if (TREE_CODE (return_type) == ARRAY_TYPE)
10904 error ("function returning an array");
10905 else
10906 error ("function returning a function");
10907 }
10908 return error_mark_node;
10909 }
10910
10911 /* Substitute the argument types. */
10912 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10913 complain, in_decl);
10914 if (arg_types == error_mark_node)
10915 return error_mark_node;
10916
10917 /* Construct a new type node and return it. */
10918 if (TREE_CODE (t) == FUNCTION_TYPE)
10919 {
10920 fntype = build_function_type (return_type, arg_types);
10921 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10922 }
10923 else
10924 {
10925 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10926 if (! MAYBE_CLASS_TYPE_P (r))
10927 {
10928 /* [temp.deduct]
10929
10930 Type deduction may fail for any of the following
10931 reasons:
10932
10933 -- Attempting to create "pointer to member of T" when T
10934 is not a class type. */
10935 if (complain & tf_error)
10936 error ("creating pointer to member function of non-class type %qT",
10937 r);
10938 return error_mark_node;
10939 }
10940
10941 fntype = build_method_type_directly (r, return_type,
10942 TREE_CHAIN (arg_types));
10943 }
10944 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10945
10946 return fntype;
10947 }
10948
10949 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10950 ARGS into that specification, and return the substituted
10951 specification. If there is no specification, return NULL_TREE. */
10952
10953 static tree
10954 tsubst_exception_specification (tree fntype,
10955 tree args,
10956 tsubst_flags_t complain,
10957 tree in_decl,
10958 bool defer_ok)
10959 {
10960 tree specs;
10961 tree new_specs;
10962
10963 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10964 new_specs = NULL_TREE;
10965 if (specs && TREE_PURPOSE (specs))
10966 {
10967 /* A noexcept-specifier. */
10968 tree expr = TREE_PURPOSE (specs);
10969 if (TREE_CODE (expr) == INTEGER_CST)
10970 new_specs = expr;
10971 else if (defer_ok)
10972 {
10973 /* Defer instantiation of noexcept-specifiers to avoid
10974 excessive instantiations (c++/49107). */
10975 new_specs = make_node (DEFERRED_NOEXCEPT);
10976 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10977 {
10978 /* We already partially instantiated this member template,
10979 so combine the new args with the old. */
10980 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10981 = DEFERRED_NOEXCEPT_PATTERN (expr);
10982 DEFERRED_NOEXCEPT_ARGS (new_specs)
10983 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10984 }
10985 else
10986 {
10987 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10988 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10989 }
10990 }
10991 else
10992 new_specs = tsubst_copy_and_build
10993 (expr, args, complain, in_decl, /*function_p=*/false,
10994 /*integral_constant_expression_p=*/true);
10995 new_specs = build_noexcept_spec (new_specs, complain);
10996 }
10997 else if (specs)
10998 {
10999 if (! TREE_VALUE (specs))
11000 new_specs = specs;
11001 else
11002 while (specs)
11003 {
11004 tree spec;
11005 int i, len = 1;
11006 tree expanded_specs = NULL_TREE;
11007
11008 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11009 {
11010 /* Expand the pack expansion type. */
11011 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11012 args, complain,
11013 in_decl);
11014
11015 if (expanded_specs == error_mark_node)
11016 return error_mark_node;
11017 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11018 len = TREE_VEC_LENGTH (expanded_specs);
11019 else
11020 {
11021 /* We're substituting into a member template, so
11022 we got a TYPE_PACK_EXPANSION back. Add that
11023 expansion and move on. */
11024 gcc_assert (TREE_CODE (expanded_specs)
11025 == TYPE_PACK_EXPANSION);
11026 new_specs = add_exception_specifier (new_specs,
11027 expanded_specs,
11028 complain);
11029 specs = TREE_CHAIN (specs);
11030 continue;
11031 }
11032 }
11033
11034 for (i = 0; i < len; ++i)
11035 {
11036 if (expanded_specs)
11037 spec = TREE_VEC_ELT (expanded_specs, i);
11038 else
11039 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11040 if (spec == error_mark_node)
11041 return spec;
11042 new_specs = add_exception_specifier (new_specs, spec,
11043 complain);
11044 }
11045
11046 specs = TREE_CHAIN (specs);
11047 }
11048 }
11049 return new_specs;
11050 }
11051
11052 /* Take the tree structure T and replace template parameters used
11053 therein with the argument vector ARGS. IN_DECL is an associated
11054 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11055 Issue error and warning messages under control of COMPLAIN. Note
11056 that we must be relatively non-tolerant of extensions here, in
11057 order to preserve conformance; if we allow substitutions that
11058 should not be allowed, we may allow argument deductions that should
11059 not succeed, and therefore report ambiguous overload situations
11060 where there are none. In theory, we could allow the substitution,
11061 but indicate that it should have failed, and allow our caller to
11062 make sure that the right thing happens, but we don't try to do this
11063 yet.
11064
11065 This function is used for dealing with types, decls and the like;
11066 for expressions, use tsubst_expr or tsubst_copy. */
11067
11068 tree
11069 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11070 {
11071 enum tree_code code;
11072 tree type, r = NULL_TREE;
11073
11074 if (t == NULL_TREE || t == error_mark_node
11075 || t == integer_type_node
11076 || t == void_type_node
11077 || t == char_type_node
11078 || t == unknown_type_node
11079 || TREE_CODE (t) == NAMESPACE_DECL
11080 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11081 return t;
11082
11083 if (DECL_P (t))
11084 return tsubst_decl (t, args, complain);
11085
11086 if (args == NULL_TREE)
11087 return t;
11088
11089 code = TREE_CODE (t);
11090
11091 if (code == IDENTIFIER_NODE)
11092 type = IDENTIFIER_TYPE_VALUE (t);
11093 else
11094 type = TREE_TYPE (t);
11095
11096 gcc_assert (type != unknown_type_node);
11097
11098 /* Reuse typedefs. We need to do this to handle dependent attributes,
11099 such as attribute aligned. */
11100 if (TYPE_P (t)
11101 && typedef_variant_p (t))
11102 {
11103 tree decl = TYPE_NAME (t);
11104
11105 if (alias_template_specialization_p (t))
11106 {
11107 /* DECL represents an alias template and we want to
11108 instantiate it. */
11109 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11110 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11111 r = instantiate_alias_template (tmpl, gen_args, complain);
11112 }
11113 else if (DECL_CLASS_SCOPE_P (decl)
11114 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11115 && uses_template_parms (DECL_CONTEXT (decl)))
11116 {
11117 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11118 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11119 r = retrieve_specialization (tmpl, gen_args, 0);
11120 }
11121 else if (DECL_FUNCTION_SCOPE_P (decl)
11122 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11123 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11124 r = retrieve_local_specialization (decl);
11125 else
11126 /* The typedef is from a non-template context. */
11127 return t;
11128
11129 if (r)
11130 {
11131 r = TREE_TYPE (r);
11132 r = cp_build_qualified_type_real
11133 (r, cp_type_quals (t) | cp_type_quals (r),
11134 complain | tf_ignore_bad_quals);
11135 return r;
11136 }
11137 else
11138 {
11139 /* We don't have an instantiation yet, so drop the typedef. */
11140 int quals = cp_type_quals (t);
11141 t = DECL_ORIGINAL_TYPE (decl);
11142 t = cp_build_qualified_type_real (t, quals,
11143 complain | tf_ignore_bad_quals);
11144 }
11145 }
11146
11147 if (type
11148 && code != TYPENAME_TYPE
11149 && code != TEMPLATE_TYPE_PARM
11150 && code != IDENTIFIER_NODE
11151 && code != FUNCTION_TYPE
11152 && code != METHOD_TYPE)
11153 type = tsubst (type, args, complain, in_decl);
11154 if (type == error_mark_node)
11155 return error_mark_node;
11156
11157 switch (code)
11158 {
11159 case RECORD_TYPE:
11160 case UNION_TYPE:
11161 case ENUMERAL_TYPE:
11162 return tsubst_aggr_type (t, args, complain, in_decl,
11163 /*entering_scope=*/0);
11164
11165 case ERROR_MARK:
11166 case IDENTIFIER_NODE:
11167 case VOID_TYPE:
11168 case REAL_TYPE:
11169 case COMPLEX_TYPE:
11170 case VECTOR_TYPE:
11171 case BOOLEAN_TYPE:
11172 case NULLPTR_TYPE:
11173 case LANG_TYPE:
11174 return t;
11175
11176 case INTEGER_TYPE:
11177 if (t == integer_type_node)
11178 return t;
11179
11180 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11181 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11182 return t;
11183
11184 {
11185 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11186
11187 max = tsubst_expr (omax, args, complain, in_decl,
11188 /*integral_constant_expression_p=*/false);
11189
11190 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11191 needed. */
11192 if (TREE_CODE (max) == NOP_EXPR
11193 && TREE_SIDE_EFFECTS (omax)
11194 && !TREE_TYPE (max))
11195 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11196
11197 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11198 with TREE_SIDE_EFFECTS that indicates this is not an integral
11199 constant expression. */
11200 if (processing_template_decl
11201 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11202 {
11203 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11204 TREE_SIDE_EFFECTS (max) = 1;
11205 }
11206
11207 return compute_array_index_type (NULL_TREE, max, complain);
11208 }
11209
11210 case TEMPLATE_TYPE_PARM:
11211 case TEMPLATE_TEMPLATE_PARM:
11212 case BOUND_TEMPLATE_TEMPLATE_PARM:
11213 case TEMPLATE_PARM_INDEX:
11214 {
11215 int idx;
11216 int level;
11217 int levels;
11218 tree arg = NULL_TREE;
11219
11220 r = NULL_TREE;
11221
11222 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11223 template_parm_level_and_index (t, &level, &idx);
11224
11225 levels = TMPL_ARGS_DEPTH (args);
11226 if (level <= levels)
11227 {
11228 arg = TMPL_ARG (args, level, idx);
11229
11230 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11231 {
11232 /* See through ARGUMENT_PACK_SELECT arguments. */
11233 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11234 /* If the selected argument is an expansion E, that most
11235 likely means we were called from
11236 gen_elem_of_pack_expansion_instantiation during the
11237 substituting of pack an argument pack (which Ith
11238 element is a pack expansion, where I is
11239 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11240 In this case, the Ith element resulting from this
11241 substituting is going to be a pack expansion, which
11242 pattern is the pattern of E. Let's return the
11243 pattern of E, and
11244 gen_elem_of_pack_expansion_instantiation will
11245 build the resulting pack expansion from it. */
11246 if (PACK_EXPANSION_P (arg))
11247 arg = PACK_EXPANSION_PATTERN (arg);
11248 }
11249 }
11250
11251 if (arg == error_mark_node)
11252 return error_mark_node;
11253 else if (arg != NULL_TREE)
11254 {
11255 if (ARGUMENT_PACK_P (arg))
11256 /* If ARG is an argument pack, we don't actually want to
11257 perform a substitution here, because substitutions
11258 for argument packs are only done
11259 element-by-element. We can get to this point when
11260 substituting the type of a non-type template
11261 parameter pack, when that type actually contains
11262 template parameter packs from an outer template, e.g.,
11263
11264 template<typename... Types> struct A {
11265 template<Types... Values> struct B { };
11266 }; */
11267 return t;
11268
11269 if (code == TEMPLATE_TYPE_PARM)
11270 {
11271 int quals;
11272 gcc_assert (TYPE_P (arg));
11273
11274 quals = cp_type_quals (arg) | cp_type_quals (t);
11275
11276 return cp_build_qualified_type_real
11277 (arg, quals, complain | tf_ignore_bad_quals);
11278 }
11279 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11280 {
11281 /* We are processing a type constructed from a
11282 template template parameter. */
11283 tree argvec = tsubst (TYPE_TI_ARGS (t),
11284 args, complain, in_decl);
11285 if (argvec == error_mark_node)
11286 return error_mark_node;
11287
11288 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11289 || TREE_CODE (arg) == TEMPLATE_DECL
11290 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11291
11292 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11293 /* Consider this code:
11294
11295 template <template <class> class Template>
11296 struct Internal {
11297 template <class Arg> using Bind = Template<Arg>;
11298 };
11299
11300 template <template <class> class Template, class Arg>
11301 using Instantiate = Template<Arg>; //#0
11302
11303 template <template <class> class Template,
11304 class Argument>
11305 using Bind =
11306 Instantiate<Internal<Template>::template Bind,
11307 Argument>; //#1
11308
11309 When #1 is parsed, the
11310 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11311 parameter `Template' in #0 matches the
11312 UNBOUND_CLASS_TEMPLATE representing the argument
11313 `Internal<Template>::template Bind'; We then want
11314 to assemble the type `Bind<Argument>' that can't
11315 be fully created right now, because
11316 `Internal<Template>' not being complete, the Bind
11317 template cannot be looked up in that context. So
11318 we need to "store" `Bind<Argument>' for later
11319 when the context of Bind becomes complete. Let's
11320 store that in a TYPENAME_TYPE. */
11321 return make_typename_type (TYPE_CONTEXT (arg),
11322 build_nt (TEMPLATE_ID_EXPR,
11323 TYPE_IDENTIFIER (arg),
11324 argvec),
11325 typename_type,
11326 complain);
11327
11328 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11329 are resolving nested-types in the signature of a
11330 member function templates. Otherwise ARG is a
11331 TEMPLATE_DECL and is the real template to be
11332 instantiated. */
11333 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11334 arg = TYPE_NAME (arg);
11335
11336 r = lookup_template_class (arg,
11337 argvec, in_decl,
11338 DECL_CONTEXT (arg),
11339 /*entering_scope=*/0,
11340 complain);
11341 return cp_build_qualified_type_real
11342 (r, cp_type_quals (t), complain);
11343 }
11344 else
11345 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11346 return convert_from_reference (unshare_expr (arg));
11347 }
11348
11349 if (level == 1)
11350 /* This can happen during the attempted tsubst'ing in
11351 unify. This means that we don't yet have any information
11352 about the template parameter in question. */
11353 return t;
11354
11355 /* Early in template argument deduction substitution, we don't
11356 want to reduce the level of 'auto', or it will be confused
11357 with a normal template parm in subsequent deduction. */
11358 if (is_auto (t) && (complain & tf_partial))
11359 return t;
11360
11361 /* If we get here, we must have been looking at a parm for a
11362 more deeply nested template. Make a new version of this
11363 template parameter, but with a lower level. */
11364 switch (code)
11365 {
11366 case TEMPLATE_TYPE_PARM:
11367 case TEMPLATE_TEMPLATE_PARM:
11368 case BOUND_TEMPLATE_TEMPLATE_PARM:
11369 if (cp_type_quals (t))
11370 {
11371 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11372 r = cp_build_qualified_type_real
11373 (r, cp_type_quals (t),
11374 complain | (code == TEMPLATE_TYPE_PARM
11375 ? tf_ignore_bad_quals : 0));
11376 }
11377 else
11378 {
11379 r = copy_type (t);
11380 TEMPLATE_TYPE_PARM_INDEX (r)
11381 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11382 r, levels, args, complain);
11383 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11384 TYPE_MAIN_VARIANT (r) = r;
11385 TYPE_POINTER_TO (r) = NULL_TREE;
11386 TYPE_REFERENCE_TO (r) = NULL_TREE;
11387
11388 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11389 /* We have reduced the level of the template
11390 template parameter, but not the levels of its
11391 template parameters, so canonical_type_parameter
11392 will not be able to find the canonical template
11393 template parameter for this level. Thus, we
11394 require structural equality checking to compare
11395 TEMPLATE_TEMPLATE_PARMs. */
11396 SET_TYPE_STRUCTURAL_EQUALITY (r);
11397 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11398 SET_TYPE_STRUCTURAL_EQUALITY (r);
11399 else
11400 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11401
11402 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11403 {
11404 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11405 complain, in_decl);
11406 if (argvec == error_mark_node)
11407 return error_mark_node;
11408
11409 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11410 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11411 }
11412 }
11413 break;
11414
11415 case TEMPLATE_PARM_INDEX:
11416 r = reduce_template_parm_level (t, type, levels, args, complain);
11417 break;
11418
11419 default:
11420 gcc_unreachable ();
11421 }
11422
11423 return r;
11424 }
11425
11426 case TREE_LIST:
11427 {
11428 tree purpose, value, chain;
11429
11430 if (t == void_list_node)
11431 return t;
11432
11433 purpose = TREE_PURPOSE (t);
11434 if (purpose)
11435 {
11436 purpose = tsubst (purpose, args, complain, in_decl);
11437 if (purpose == error_mark_node)
11438 return error_mark_node;
11439 }
11440 value = TREE_VALUE (t);
11441 if (value)
11442 {
11443 value = tsubst (value, args, complain, in_decl);
11444 if (value == error_mark_node)
11445 return error_mark_node;
11446 }
11447 chain = TREE_CHAIN (t);
11448 if (chain && chain != void_type_node)
11449 {
11450 chain = tsubst (chain, args, complain, in_decl);
11451 if (chain == error_mark_node)
11452 return error_mark_node;
11453 }
11454 if (purpose == TREE_PURPOSE (t)
11455 && value == TREE_VALUE (t)
11456 && chain == TREE_CHAIN (t))
11457 return t;
11458 return hash_tree_cons (purpose, value, chain);
11459 }
11460
11461 case TREE_BINFO:
11462 /* We should never be tsubsting a binfo. */
11463 gcc_unreachable ();
11464
11465 case TREE_VEC:
11466 /* A vector of template arguments. */
11467 gcc_assert (!type);
11468 return tsubst_template_args (t, args, complain, in_decl);
11469
11470 case POINTER_TYPE:
11471 case REFERENCE_TYPE:
11472 {
11473 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11474 return t;
11475
11476 /* [temp.deduct]
11477
11478 Type deduction may fail for any of the following
11479 reasons:
11480
11481 -- Attempting to create a pointer to reference type.
11482 -- Attempting to create a reference to a reference type or
11483 a reference to void.
11484
11485 Core issue 106 says that creating a reference to a reference
11486 during instantiation is no longer a cause for failure. We
11487 only enforce this check in strict C++98 mode. */
11488 if ((TREE_CODE (type) == REFERENCE_TYPE
11489 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11490 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11491 {
11492 static location_t last_loc;
11493
11494 /* We keep track of the last time we issued this error
11495 message to avoid spewing a ton of messages during a
11496 single bad template instantiation. */
11497 if (complain & tf_error
11498 && last_loc != input_location)
11499 {
11500 if (TREE_CODE (type) == VOID_TYPE)
11501 error ("forming reference to void");
11502 else if (code == POINTER_TYPE)
11503 error ("forming pointer to reference type %qT", type);
11504 else
11505 error ("forming reference to reference type %qT", type);
11506 last_loc = input_location;
11507 }
11508
11509 return error_mark_node;
11510 }
11511 else if (code == POINTER_TYPE)
11512 {
11513 r = build_pointer_type (type);
11514 if (TREE_CODE (type) == METHOD_TYPE)
11515 r = build_ptrmemfunc_type (r);
11516 }
11517 else if (TREE_CODE (type) == REFERENCE_TYPE)
11518 /* In C++0x, during template argument substitution, when there is an
11519 attempt to create a reference to a reference type, reference
11520 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11521
11522 "If a template-argument for a template-parameter T names a type
11523 that is a reference to a type A, an attempt to create the type
11524 'lvalue reference to cv T' creates the type 'lvalue reference to
11525 A,' while an attempt to create the type type rvalue reference to
11526 cv T' creates the type T"
11527 */
11528 r = cp_build_reference_type
11529 (TREE_TYPE (type),
11530 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11531 else
11532 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11533 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11534
11535 if (r != error_mark_node)
11536 /* Will this ever be needed for TYPE_..._TO values? */
11537 layout_type (r);
11538
11539 return r;
11540 }
11541 case OFFSET_TYPE:
11542 {
11543 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11544 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11545 {
11546 /* [temp.deduct]
11547
11548 Type deduction may fail for any of the following
11549 reasons:
11550
11551 -- Attempting to create "pointer to member of T" when T
11552 is not a class type. */
11553 if (complain & tf_error)
11554 error ("creating pointer to member of non-class type %qT", r);
11555 return error_mark_node;
11556 }
11557 if (TREE_CODE (type) == REFERENCE_TYPE)
11558 {
11559 if (complain & tf_error)
11560 error ("creating pointer to member reference type %qT", type);
11561 return error_mark_node;
11562 }
11563 if (TREE_CODE (type) == VOID_TYPE)
11564 {
11565 if (complain & tf_error)
11566 error ("creating pointer to member of type void");
11567 return error_mark_node;
11568 }
11569 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11570 if (TREE_CODE (type) == FUNCTION_TYPE)
11571 {
11572 /* The type of the implicit object parameter gets its
11573 cv-qualifiers from the FUNCTION_TYPE. */
11574 tree memptr;
11575 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11576 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11577 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11578 complain);
11579 }
11580 else
11581 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11582 cp_type_quals (t),
11583 complain);
11584 }
11585 case FUNCTION_TYPE:
11586 case METHOD_TYPE:
11587 {
11588 tree fntype;
11589 tree specs;
11590 fntype = tsubst_function_type (t, args, complain, in_decl);
11591 if (fntype == error_mark_node)
11592 return error_mark_node;
11593
11594 /* Substitute the exception specification. */
11595 specs = tsubst_exception_specification (t, args, complain,
11596 in_decl, /*defer_ok*/true);
11597 if (specs == error_mark_node)
11598 return error_mark_node;
11599 if (specs)
11600 fntype = build_exception_variant (fntype, specs);
11601 return fntype;
11602 }
11603 case ARRAY_TYPE:
11604 {
11605 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11606 if (domain == error_mark_node)
11607 return error_mark_node;
11608
11609 /* As an optimization, we avoid regenerating the array type if
11610 it will obviously be the same as T. */
11611 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11612 return t;
11613
11614 /* These checks should match the ones in grokdeclarator.
11615
11616 [temp.deduct]
11617
11618 The deduction may fail for any of the following reasons:
11619
11620 -- Attempting to create an array with an element type that
11621 is void, a function type, or a reference type, or [DR337]
11622 an abstract class type. */
11623 if (TREE_CODE (type) == VOID_TYPE
11624 || TREE_CODE (type) == FUNCTION_TYPE
11625 || TREE_CODE (type) == REFERENCE_TYPE)
11626 {
11627 if (complain & tf_error)
11628 error ("creating array of %qT", type);
11629 return error_mark_node;
11630 }
11631 if (ABSTRACT_CLASS_TYPE_P (type))
11632 {
11633 if (complain & tf_error)
11634 error ("creating array of %qT, which is an abstract class type",
11635 type);
11636 return error_mark_node;
11637 }
11638
11639 r = build_cplus_array_type (type, domain);
11640
11641 if (TYPE_USER_ALIGN (t))
11642 {
11643 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11644 TYPE_USER_ALIGN (r) = 1;
11645 }
11646
11647 return r;
11648 }
11649
11650 case TYPENAME_TYPE:
11651 {
11652 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11653 in_decl, /*entering_scope=*/1);
11654 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11655 complain, in_decl);
11656
11657 if (ctx == error_mark_node || f == error_mark_node)
11658 return error_mark_node;
11659
11660 if (!MAYBE_CLASS_TYPE_P (ctx))
11661 {
11662 if (complain & tf_error)
11663 error ("%qT is not a class, struct, or union type", ctx);
11664 return error_mark_node;
11665 }
11666 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11667 {
11668 /* Normally, make_typename_type does not require that the CTX
11669 have complete type in order to allow things like:
11670
11671 template <class T> struct S { typename S<T>::X Y; };
11672
11673 But, such constructs have already been resolved by this
11674 point, so here CTX really should have complete type, unless
11675 it's a partial instantiation. */
11676 ctx = complete_type (ctx);
11677 if (!COMPLETE_TYPE_P (ctx))
11678 {
11679 if (complain & tf_error)
11680 cxx_incomplete_type_error (NULL_TREE, ctx);
11681 return error_mark_node;
11682 }
11683 }
11684
11685 f = make_typename_type (ctx, f, typename_type,
11686 complain | tf_keep_type_decl);
11687 if (f == error_mark_node)
11688 return f;
11689 if (TREE_CODE (f) == TYPE_DECL)
11690 {
11691 complain |= tf_ignore_bad_quals;
11692 f = TREE_TYPE (f);
11693 }
11694
11695 if (TREE_CODE (f) != TYPENAME_TYPE)
11696 {
11697 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11698 {
11699 if (complain & tf_error)
11700 error ("%qT resolves to %qT, which is not an enumeration type",
11701 t, f);
11702 else
11703 return error_mark_node;
11704 }
11705 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11706 {
11707 if (complain & tf_error)
11708 error ("%qT resolves to %qT, which is is not a class type",
11709 t, f);
11710 else
11711 return error_mark_node;
11712 }
11713 }
11714
11715 return cp_build_qualified_type_real
11716 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11717 }
11718
11719 case UNBOUND_CLASS_TEMPLATE:
11720 {
11721 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11722 in_decl, /*entering_scope=*/1);
11723 tree name = TYPE_IDENTIFIER (t);
11724 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11725
11726 if (ctx == error_mark_node || name == error_mark_node)
11727 return error_mark_node;
11728
11729 if (parm_list)
11730 parm_list = tsubst_template_parms (parm_list, args, complain);
11731 return make_unbound_class_template (ctx, name, parm_list, complain);
11732 }
11733
11734 case TYPEOF_TYPE:
11735 {
11736 tree type;
11737
11738 ++cp_unevaluated_operand;
11739 ++c_inhibit_evaluation_warnings;
11740
11741 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11742 complain, in_decl,
11743 /*integral_constant_expression_p=*/false);
11744
11745 --cp_unevaluated_operand;
11746 --c_inhibit_evaluation_warnings;
11747
11748 type = finish_typeof (type);
11749 return cp_build_qualified_type_real (type,
11750 cp_type_quals (t)
11751 | cp_type_quals (type),
11752 complain);
11753 }
11754
11755 case DECLTYPE_TYPE:
11756 {
11757 tree type;
11758
11759 ++cp_unevaluated_operand;
11760 ++c_inhibit_evaluation_warnings;
11761
11762 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11763 complain, in_decl,
11764 /*integral_constant_expression_p=*/false);
11765
11766 --cp_unevaluated_operand;
11767 --c_inhibit_evaluation_warnings;
11768
11769 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11770 type = lambda_capture_field_type (type);
11771 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11772 type = lambda_proxy_type (type);
11773 else
11774 type = finish_decltype_type
11775 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11776 return cp_build_qualified_type_real (type,
11777 cp_type_quals (t)
11778 | cp_type_quals (type),
11779 complain);
11780 }
11781
11782 case UNDERLYING_TYPE:
11783 {
11784 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11785 complain, in_decl);
11786 return finish_underlying_type (type);
11787 }
11788
11789 case TYPE_ARGUMENT_PACK:
11790 case NONTYPE_ARGUMENT_PACK:
11791 {
11792 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11793 tree packed_out =
11794 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11795 args,
11796 complain,
11797 in_decl);
11798 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11799
11800 /* For template nontype argument packs, also substitute into
11801 the type. */
11802 if (code == NONTYPE_ARGUMENT_PACK)
11803 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11804
11805 return r;
11806 }
11807 break;
11808
11809 case INTEGER_CST:
11810 case REAL_CST:
11811 case STRING_CST:
11812 case PLUS_EXPR:
11813 case MINUS_EXPR:
11814 case NEGATE_EXPR:
11815 case NOP_EXPR:
11816 case INDIRECT_REF:
11817 case ADDR_EXPR:
11818 case CALL_EXPR:
11819 case ARRAY_REF:
11820 case SCOPE_REF:
11821 /* We should use one of the expression tsubsts for these codes. */
11822 gcc_unreachable ();
11823
11824 default:
11825 sorry ("use of %qs in template", tree_code_name [(int) code]);
11826 return error_mark_node;
11827 }
11828 }
11829
11830 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11831 type of the expression on the left-hand side of the "." or "->"
11832 operator. */
11833
11834 static tree
11835 tsubst_baselink (tree baselink, tree object_type,
11836 tree args, tsubst_flags_t complain, tree in_decl)
11837 {
11838 tree name;
11839 tree qualifying_scope;
11840 tree fns;
11841 tree optype;
11842 tree template_args = 0;
11843 bool template_id_p = false;
11844 bool qualified = BASELINK_QUALIFIED_P (baselink);
11845
11846 /* A baselink indicates a function from a base class. Both the
11847 BASELINK_ACCESS_BINFO and the base class referenced may
11848 indicate bases of the template class, rather than the
11849 instantiated class. In addition, lookups that were not
11850 ambiguous before may be ambiguous now. Therefore, we perform
11851 the lookup again. */
11852 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11853 qualifying_scope = tsubst (qualifying_scope, args,
11854 complain, in_decl);
11855 fns = BASELINK_FUNCTIONS (baselink);
11856 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11857 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11858 {
11859 template_id_p = true;
11860 template_args = TREE_OPERAND (fns, 1);
11861 fns = TREE_OPERAND (fns, 0);
11862 if (template_args)
11863 template_args = tsubst_template_args (template_args, args,
11864 complain, in_decl);
11865 }
11866 name = DECL_NAME (get_first_fn (fns));
11867 if (IDENTIFIER_TYPENAME_P (name))
11868 name = mangle_conv_op_name_for_type (optype);
11869 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11870 if (!baselink)
11871 return error_mark_node;
11872
11873 /* If lookup found a single function, mark it as used at this
11874 point. (If it lookup found multiple functions the one selected
11875 later by overload resolution will be marked as used at that
11876 point.) */
11877 if (BASELINK_P (baselink))
11878 fns = BASELINK_FUNCTIONS (baselink);
11879 if (!template_id_p && !really_overloaded_fn (fns))
11880 mark_used (OVL_CURRENT (fns));
11881
11882 /* Add back the template arguments, if present. */
11883 if (BASELINK_P (baselink) && template_id_p)
11884 BASELINK_FUNCTIONS (baselink)
11885 = build_nt (TEMPLATE_ID_EXPR,
11886 BASELINK_FUNCTIONS (baselink),
11887 template_args);
11888 /* Update the conversion operator type. */
11889 BASELINK_OPTYPE (baselink) = optype;
11890
11891 if (!object_type)
11892 object_type = current_class_type;
11893
11894 if (qualified)
11895 baselink = adjust_result_of_qualified_name_lookup (baselink,
11896 qualifying_scope,
11897 object_type);
11898 return baselink;
11899 }
11900
11901 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11902 true if the qualified-id will be a postfix-expression in-and-of
11903 itself; false if more of the postfix-expression follows the
11904 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11905 of "&". */
11906
11907 static tree
11908 tsubst_qualified_id (tree qualified_id, tree args,
11909 tsubst_flags_t complain, tree in_decl,
11910 bool done, bool address_p)
11911 {
11912 tree expr;
11913 tree scope;
11914 tree name;
11915 bool is_template;
11916 tree template_args;
11917 location_t loc = UNKNOWN_LOCATION;
11918
11919 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11920
11921 /* Figure out what name to look up. */
11922 name = TREE_OPERAND (qualified_id, 1);
11923 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11924 {
11925 is_template = true;
11926 loc = EXPR_LOCATION (name);
11927 template_args = TREE_OPERAND (name, 1);
11928 if (template_args)
11929 template_args = tsubst_template_args (template_args, args,
11930 complain, in_decl);
11931 name = TREE_OPERAND (name, 0);
11932 }
11933 else
11934 {
11935 is_template = false;
11936 template_args = NULL_TREE;
11937 }
11938
11939 /* Substitute into the qualifying scope. When there are no ARGS, we
11940 are just trying to simplify a non-dependent expression. In that
11941 case the qualifying scope may be dependent, and, in any case,
11942 substituting will not help. */
11943 scope = TREE_OPERAND (qualified_id, 0);
11944 if (args)
11945 {
11946 scope = tsubst (scope, args, complain, in_decl);
11947 expr = tsubst_copy (name, args, complain, in_decl);
11948 }
11949 else
11950 expr = name;
11951
11952 if (dependent_scope_p (scope))
11953 {
11954 if (is_template)
11955 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11956 return build_qualified_name (NULL_TREE, scope, expr,
11957 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11958 }
11959
11960 if (!BASELINK_P (name) && !DECL_P (expr))
11961 {
11962 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11963 {
11964 /* A BIT_NOT_EXPR is used to represent a destructor. */
11965 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11966 {
11967 error ("qualifying type %qT does not match destructor name ~%qT",
11968 scope, TREE_OPERAND (expr, 0));
11969 expr = error_mark_node;
11970 }
11971 else
11972 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11973 /*is_type_p=*/0, false);
11974 }
11975 else
11976 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11977 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11978 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11979 {
11980 if (complain & tf_error)
11981 {
11982 error ("dependent-name %qE is parsed as a non-type, but "
11983 "instantiation yields a type", qualified_id);
11984 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11985 }
11986 return error_mark_node;
11987 }
11988 }
11989
11990 if (DECL_P (expr))
11991 {
11992 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11993 scope);
11994 /* Remember that there was a reference to this entity. */
11995 mark_used (expr);
11996 }
11997
11998 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11999 {
12000 if (complain & tf_error)
12001 qualified_name_lookup_error (scope,
12002 TREE_OPERAND (qualified_id, 1),
12003 expr, input_location);
12004 return error_mark_node;
12005 }
12006
12007 if (is_template)
12008 expr = lookup_template_function (expr, template_args);
12009
12010 if (expr == error_mark_node && complain & tf_error)
12011 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12012 expr, input_location);
12013 else if (TYPE_P (scope))
12014 {
12015 expr = (adjust_result_of_qualified_name_lookup
12016 (expr, scope, current_class_type));
12017 expr = (finish_qualified_id_expr
12018 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12019 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12020 /*template_arg_p=*/false));
12021 }
12022
12023 /* Expressions do not generally have reference type. */
12024 if (TREE_CODE (expr) != SCOPE_REF
12025 /* However, if we're about to form a pointer-to-member, we just
12026 want the referenced member referenced. */
12027 && TREE_CODE (expr) != OFFSET_REF)
12028 expr = convert_from_reference (expr);
12029
12030 return expr;
12031 }
12032
12033 /* Like tsubst, but deals with expressions. This function just replaces
12034 template parms; to finish processing the resultant expression, use
12035 tsubst_copy_and_build or tsubst_expr. */
12036
12037 static tree
12038 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12039 {
12040 enum tree_code code;
12041 tree r;
12042
12043 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12044 return t;
12045
12046 code = TREE_CODE (t);
12047
12048 switch (code)
12049 {
12050 case PARM_DECL:
12051 r = retrieve_local_specialization (t);
12052
12053 if (r == NULL_TREE)
12054 {
12055 /* We get here for a use of 'this' in an NSDMI. */
12056 if (DECL_NAME (t) == this_identifier
12057 && at_function_scope_p ()
12058 && DECL_CONSTRUCTOR_P (current_function_decl))
12059 return current_class_ptr;
12060
12061 /* This can happen for a parameter name used later in a function
12062 declaration (such as in a late-specified return type). Just
12063 make a dummy decl, since it's only used for its type. */
12064 gcc_assert (cp_unevaluated_operand != 0);
12065 r = tsubst_decl (t, args, complain);
12066 /* Give it the template pattern as its context; its true context
12067 hasn't been instantiated yet and this is good enough for
12068 mangling. */
12069 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12070 }
12071
12072 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12073 r = ARGUMENT_PACK_SELECT_ARG (r);
12074 mark_used (r);
12075 return r;
12076
12077 case CONST_DECL:
12078 {
12079 tree enum_type;
12080 tree v;
12081
12082 if (DECL_TEMPLATE_PARM_P (t))
12083 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12084 /* There is no need to substitute into namespace-scope
12085 enumerators. */
12086 if (DECL_NAMESPACE_SCOPE_P (t))
12087 return t;
12088 /* If ARGS is NULL, then T is known to be non-dependent. */
12089 if (args == NULL_TREE)
12090 return integral_constant_value (t);
12091
12092 /* Unfortunately, we cannot just call lookup_name here.
12093 Consider:
12094
12095 template <int I> int f() {
12096 enum E { a = I };
12097 struct S { void g() { E e = a; } };
12098 };
12099
12100 When we instantiate f<7>::S::g(), say, lookup_name is not
12101 clever enough to find f<7>::a. */
12102 enum_type
12103 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12104 /*entering_scope=*/0);
12105
12106 for (v = TYPE_VALUES (enum_type);
12107 v != NULL_TREE;
12108 v = TREE_CHAIN (v))
12109 if (TREE_PURPOSE (v) == DECL_NAME (t))
12110 return TREE_VALUE (v);
12111
12112 /* We didn't find the name. That should never happen; if
12113 name-lookup found it during preliminary parsing, we
12114 should find it again here during instantiation. */
12115 gcc_unreachable ();
12116 }
12117 return t;
12118
12119 case FIELD_DECL:
12120 if (DECL_CONTEXT (t))
12121 {
12122 tree ctx;
12123
12124 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12125 /*entering_scope=*/1);
12126 if (ctx != DECL_CONTEXT (t))
12127 {
12128 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12129 if (!r)
12130 {
12131 if (complain & tf_error)
12132 error ("using invalid field %qD", t);
12133 return error_mark_node;
12134 }
12135 return r;
12136 }
12137 }
12138
12139 return t;
12140
12141 case VAR_DECL:
12142 case FUNCTION_DECL:
12143 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12144 || local_variable_p (t))
12145 t = tsubst (t, args, complain, in_decl);
12146 mark_used (t);
12147 return t;
12148
12149 case NAMESPACE_DECL:
12150 return t;
12151
12152 case OVERLOAD:
12153 /* An OVERLOAD will always be a non-dependent overload set; an
12154 overload set from function scope will just be represented with an
12155 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12156 gcc_assert (!uses_template_parms (t));
12157 return t;
12158
12159 case BASELINK:
12160 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
12161
12162 case TEMPLATE_DECL:
12163 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12164 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12165 args, complain, in_decl);
12166 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12167 return tsubst (t, args, complain, in_decl);
12168 else if (DECL_CLASS_SCOPE_P (t)
12169 && uses_template_parms (DECL_CONTEXT (t)))
12170 {
12171 /* Template template argument like the following example need
12172 special treatment:
12173
12174 template <template <class> class TT> struct C {};
12175 template <class T> struct D {
12176 template <class U> struct E {};
12177 C<E> c; // #1
12178 };
12179 D<int> d; // #2
12180
12181 We are processing the template argument `E' in #1 for
12182 the template instantiation #2. Originally, `E' is a
12183 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12184 have to substitute this with one having context `D<int>'. */
12185
12186 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12187 return lookup_field (context, DECL_NAME(t), 0, false);
12188 }
12189 else
12190 /* Ordinary template template argument. */
12191 return t;
12192
12193 case CAST_EXPR:
12194 case REINTERPRET_CAST_EXPR:
12195 case CONST_CAST_EXPR:
12196 case STATIC_CAST_EXPR:
12197 case DYNAMIC_CAST_EXPR:
12198 case IMPLICIT_CONV_EXPR:
12199 case CONVERT_EXPR:
12200 case NOP_EXPR:
12201 return build1
12202 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12203 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12204
12205 case SIZEOF_EXPR:
12206 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12207 {
12208
12209 tree expanded, op = TREE_OPERAND (t, 0);
12210 int len = 0;
12211
12212 if (SIZEOF_EXPR_TYPE_P (t))
12213 op = TREE_TYPE (op);
12214
12215 ++cp_unevaluated_operand;
12216 ++c_inhibit_evaluation_warnings;
12217 /* We only want to compute the number of arguments. */
12218 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12219 --cp_unevaluated_operand;
12220 --c_inhibit_evaluation_warnings;
12221
12222 if (TREE_CODE (expanded) == TREE_VEC)
12223 len = TREE_VEC_LENGTH (expanded);
12224
12225 if (expanded == error_mark_node)
12226 return error_mark_node;
12227 else if (PACK_EXPANSION_P (expanded)
12228 || (TREE_CODE (expanded) == TREE_VEC
12229 && len > 0
12230 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12231 {
12232 if (TREE_CODE (expanded) == TREE_VEC)
12233 expanded = TREE_VEC_ELT (expanded, len - 1);
12234
12235 if (TYPE_P (expanded))
12236 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12237 complain & tf_error);
12238 else
12239 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12240 complain & tf_error);
12241 }
12242 else
12243 return build_int_cst (size_type_node, len);
12244 }
12245 if (SIZEOF_EXPR_TYPE_P (t))
12246 {
12247 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12248 args, complain, in_decl);
12249 r = build1 (NOP_EXPR, r, error_mark_node);
12250 r = build1 (SIZEOF_EXPR,
12251 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12252 SIZEOF_EXPR_TYPE_P (r) = 1;
12253 return r;
12254 }
12255 /* Fall through */
12256
12257 case INDIRECT_REF:
12258 case NEGATE_EXPR:
12259 case TRUTH_NOT_EXPR:
12260 case BIT_NOT_EXPR:
12261 case ADDR_EXPR:
12262 case UNARY_PLUS_EXPR: /* Unary + */
12263 case ALIGNOF_EXPR:
12264 case AT_ENCODE_EXPR:
12265 case ARROW_EXPR:
12266 case THROW_EXPR:
12267 case TYPEID_EXPR:
12268 case REALPART_EXPR:
12269 case IMAGPART_EXPR:
12270 return build1
12271 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12272 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12273
12274 case COMPONENT_REF:
12275 {
12276 tree object;
12277 tree name;
12278
12279 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12280 name = TREE_OPERAND (t, 1);
12281 if (TREE_CODE (name) == BIT_NOT_EXPR)
12282 {
12283 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12284 complain, in_decl);
12285 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12286 }
12287 else if (TREE_CODE (name) == SCOPE_REF
12288 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12289 {
12290 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12291 complain, in_decl);
12292 name = TREE_OPERAND (name, 1);
12293 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12294 complain, in_decl);
12295 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12296 name = build_qualified_name (/*type=*/NULL_TREE,
12297 base, name,
12298 /*template_p=*/false);
12299 }
12300 else if (BASELINK_P (name))
12301 name = tsubst_baselink (name,
12302 non_reference (TREE_TYPE (object)),
12303 args, complain,
12304 in_decl);
12305 else
12306 name = tsubst_copy (name, args, complain, in_decl);
12307 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12308 }
12309
12310 case PLUS_EXPR:
12311 case MINUS_EXPR:
12312 case MULT_EXPR:
12313 case TRUNC_DIV_EXPR:
12314 case CEIL_DIV_EXPR:
12315 case FLOOR_DIV_EXPR:
12316 case ROUND_DIV_EXPR:
12317 case EXACT_DIV_EXPR:
12318 case BIT_AND_EXPR:
12319 case BIT_IOR_EXPR:
12320 case BIT_XOR_EXPR:
12321 case TRUNC_MOD_EXPR:
12322 case FLOOR_MOD_EXPR:
12323 case TRUTH_ANDIF_EXPR:
12324 case TRUTH_ORIF_EXPR:
12325 case TRUTH_AND_EXPR:
12326 case TRUTH_OR_EXPR:
12327 case RSHIFT_EXPR:
12328 case LSHIFT_EXPR:
12329 case RROTATE_EXPR:
12330 case LROTATE_EXPR:
12331 case EQ_EXPR:
12332 case NE_EXPR:
12333 case MAX_EXPR:
12334 case MIN_EXPR:
12335 case LE_EXPR:
12336 case GE_EXPR:
12337 case LT_EXPR:
12338 case GT_EXPR:
12339 case COMPOUND_EXPR:
12340 case DOTSTAR_EXPR:
12341 case MEMBER_REF:
12342 case PREDECREMENT_EXPR:
12343 case PREINCREMENT_EXPR:
12344 case POSTDECREMENT_EXPR:
12345 case POSTINCREMENT_EXPR:
12346 return build_nt
12347 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12348 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12349
12350 case SCOPE_REF:
12351 return build_qualified_name (/*type=*/NULL_TREE,
12352 tsubst_copy (TREE_OPERAND (t, 0),
12353 args, complain, in_decl),
12354 tsubst_copy (TREE_OPERAND (t, 1),
12355 args, complain, in_decl),
12356 QUALIFIED_NAME_IS_TEMPLATE (t));
12357
12358 case ARRAY_REF:
12359 return build_nt
12360 (ARRAY_REF,
12361 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12362 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12363 NULL_TREE, NULL_TREE);
12364
12365 case CALL_EXPR:
12366 {
12367 int n = VL_EXP_OPERAND_LENGTH (t);
12368 tree result = build_vl_exp (CALL_EXPR, n);
12369 int i;
12370 for (i = 0; i < n; i++)
12371 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12372 complain, in_decl);
12373 return result;
12374 }
12375
12376 case COND_EXPR:
12377 case MODOP_EXPR:
12378 case PSEUDO_DTOR_EXPR:
12379 {
12380 r = build_nt
12381 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12382 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12383 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12384 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12385 return r;
12386 }
12387
12388 case NEW_EXPR:
12389 {
12390 r = build_nt
12391 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12392 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12393 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12394 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12395 return r;
12396 }
12397
12398 case DELETE_EXPR:
12399 {
12400 r = build_nt
12401 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12402 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12403 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12404 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12405 return r;
12406 }
12407
12408 case TEMPLATE_ID_EXPR:
12409 {
12410 /* Substituted template arguments */
12411 tree fn = TREE_OPERAND (t, 0);
12412 tree targs = TREE_OPERAND (t, 1);
12413
12414 fn = tsubst_copy (fn, args, complain, in_decl);
12415 if (targs)
12416 targs = tsubst_template_args (targs, args, complain, in_decl);
12417
12418 return lookup_template_function (fn, targs);
12419 }
12420
12421 case TREE_LIST:
12422 {
12423 tree purpose, value, chain;
12424
12425 if (t == void_list_node)
12426 return t;
12427
12428 purpose = TREE_PURPOSE (t);
12429 if (purpose)
12430 purpose = tsubst_copy (purpose, args, complain, in_decl);
12431 value = TREE_VALUE (t);
12432 if (value)
12433 value = tsubst_copy (value, args, complain, in_decl);
12434 chain = TREE_CHAIN (t);
12435 if (chain && chain != void_type_node)
12436 chain = tsubst_copy (chain, args, complain, in_decl);
12437 if (purpose == TREE_PURPOSE (t)
12438 && value == TREE_VALUE (t)
12439 && chain == TREE_CHAIN (t))
12440 return t;
12441 return tree_cons (purpose, value, chain);
12442 }
12443
12444 case RECORD_TYPE:
12445 case UNION_TYPE:
12446 case ENUMERAL_TYPE:
12447 case INTEGER_TYPE:
12448 case TEMPLATE_TYPE_PARM:
12449 case TEMPLATE_TEMPLATE_PARM:
12450 case BOUND_TEMPLATE_TEMPLATE_PARM:
12451 case TEMPLATE_PARM_INDEX:
12452 case POINTER_TYPE:
12453 case REFERENCE_TYPE:
12454 case OFFSET_TYPE:
12455 case FUNCTION_TYPE:
12456 case METHOD_TYPE:
12457 case ARRAY_TYPE:
12458 case TYPENAME_TYPE:
12459 case UNBOUND_CLASS_TEMPLATE:
12460 case TYPEOF_TYPE:
12461 case DECLTYPE_TYPE:
12462 case TYPE_DECL:
12463 return tsubst (t, args, complain, in_decl);
12464
12465 case IDENTIFIER_NODE:
12466 if (IDENTIFIER_TYPENAME_P (t))
12467 {
12468 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12469 return mangle_conv_op_name_for_type (new_type);
12470 }
12471 else
12472 return t;
12473
12474 case CONSTRUCTOR:
12475 /* This is handled by tsubst_copy_and_build. */
12476 gcc_unreachable ();
12477
12478 case VA_ARG_EXPR:
12479 return build_x_va_arg (EXPR_LOCATION (t),
12480 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12481 in_decl),
12482 tsubst (TREE_TYPE (t), args, complain, in_decl));
12483
12484 case CLEANUP_POINT_EXPR:
12485 /* We shouldn't have built any of these during initial template
12486 generation. Instead, they should be built during instantiation
12487 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12488 gcc_unreachable ();
12489
12490 case OFFSET_REF:
12491 r = build2
12492 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12493 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12494 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12495 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12496 mark_used (TREE_OPERAND (r, 1));
12497 return r;
12498
12499 case EXPR_PACK_EXPANSION:
12500 error ("invalid use of pack expansion expression");
12501 return error_mark_node;
12502
12503 case NONTYPE_ARGUMENT_PACK:
12504 error ("use %<...%> to expand argument pack");
12505 return error_mark_node;
12506
12507 case INTEGER_CST:
12508 case REAL_CST:
12509 case STRING_CST:
12510 case COMPLEX_CST:
12511 {
12512 /* Instantiate any typedefs in the type. */
12513 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12514 r = fold_convert (type, t);
12515 gcc_assert (TREE_CODE (r) == code);
12516 return r;
12517 }
12518
12519 case PTRMEM_CST:
12520 /* These can sometimes show up in a partial instantiation, but never
12521 involve template parms. */
12522 gcc_assert (!uses_template_parms (t));
12523 return t;
12524
12525 default:
12526 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12527 gcc_checking_assert (false);
12528 return t;
12529 }
12530 }
12531
12532 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12533
12534 static tree
12535 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12536 tree in_decl)
12537 {
12538 tree new_clauses = NULL, nc, oc;
12539
12540 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12541 {
12542 nc = copy_node (oc);
12543 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12544 new_clauses = nc;
12545
12546 switch (OMP_CLAUSE_CODE (nc))
12547 {
12548 case OMP_CLAUSE_LASTPRIVATE:
12549 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12550 {
12551 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12552 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12553 in_decl, /*integral_constant_expression_p=*/false);
12554 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12555 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12556 }
12557 /* FALLTHRU */
12558 case OMP_CLAUSE_PRIVATE:
12559 case OMP_CLAUSE_SHARED:
12560 case OMP_CLAUSE_FIRSTPRIVATE:
12561 case OMP_CLAUSE_REDUCTION:
12562 case OMP_CLAUSE_COPYIN:
12563 case OMP_CLAUSE_COPYPRIVATE:
12564 case OMP_CLAUSE_IF:
12565 case OMP_CLAUSE_NUM_THREADS:
12566 case OMP_CLAUSE_SCHEDULE:
12567 case OMP_CLAUSE_COLLAPSE:
12568 case OMP_CLAUSE_FINAL:
12569 OMP_CLAUSE_OPERAND (nc, 0)
12570 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12571 in_decl, /*integral_constant_expression_p=*/false);
12572 break;
12573 case OMP_CLAUSE_NOWAIT:
12574 case OMP_CLAUSE_ORDERED:
12575 case OMP_CLAUSE_DEFAULT:
12576 case OMP_CLAUSE_UNTIED:
12577 case OMP_CLAUSE_MERGEABLE:
12578 break;
12579 default:
12580 gcc_unreachable ();
12581 }
12582 }
12583
12584 return finish_omp_clauses (nreverse (new_clauses));
12585 }
12586
12587 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12588
12589 static tree
12590 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12591 tree in_decl)
12592 {
12593 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12594
12595 tree purpose, value, chain;
12596
12597 if (t == NULL)
12598 return t;
12599
12600 if (TREE_CODE (t) != TREE_LIST)
12601 return tsubst_copy_and_build (t, args, complain, in_decl,
12602 /*function_p=*/false,
12603 /*integral_constant_expression_p=*/false);
12604
12605 if (t == void_list_node)
12606 return t;
12607
12608 purpose = TREE_PURPOSE (t);
12609 if (purpose)
12610 purpose = RECUR (purpose);
12611 value = TREE_VALUE (t);
12612 if (value)
12613 {
12614 if (TREE_CODE (value) != LABEL_DECL)
12615 value = RECUR (value);
12616 else
12617 {
12618 value = lookup_label (DECL_NAME (value));
12619 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12620 TREE_USED (value) = 1;
12621 }
12622 }
12623 chain = TREE_CHAIN (t);
12624 if (chain && chain != void_type_node)
12625 chain = RECUR (chain);
12626 return tree_cons (purpose, value, chain);
12627 #undef RECUR
12628 }
12629
12630 /* Substitute one OMP_FOR iterator. */
12631
12632 static void
12633 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12634 tree condv, tree incrv, tree *clauses,
12635 tree args, tsubst_flags_t complain, tree in_decl,
12636 bool integral_constant_expression_p)
12637 {
12638 #define RECUR(NODE) \
12639 tsubst_expr ((NODE), args, complain, in_decl, \
12640 integral_constant_expression_p)
12641 tree decl, init, cond, incr;
12642 bool init_decl;
12643
12644 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12645 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12646 decl = TREE_OPERAND (init, 0);
12647 init = TREE_OPERAND (init, 1);
12648 /* Do this before substituting into decl to handle 'auto'. */
12649 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12650 init = RECUR (init);
12651 decl = RECUR (decl);
12652 if (init_decl)
12653 {
12654 gcc_assert (!processing_template_decl);
12655 init = DECL_INITIAL (decl);
12656 DECL_INITIAL (decl) = NULL_TREE;
12657 }
12658
12659 gcc_assert (!type_dependent_expression_p (decl));
12660
12661 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12662 {
12663 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12664 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12665 if (TREE_CODE (incr) == MODIFY_EXPR)
12666 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12667 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12668 RECUR (TREE_OPERAND (incr, 1)),
12669 complain);
12670 else
12671 incr = RECUR (incr);
12672 TREE_VEC_ELT (declv, i) = decl;
12673 TREE_VEC_ELT (initv, i) = init;
12674 TREE_VEC_ELT (condv, i) = cond;
12675 TREE_VEC_ELT (incrv, i) = incr;
12676 return;
12677 }
12678
12679 if (init && !init_decl)
12680 {
12681 tree c;
12682 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12683 {
12684 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12685 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12686 && OMP_CLAUSE_DECL (c) == decl)
12687 break;
12688 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12689 && OMP_CLAUSE_DECL (c) == decl)
12690 error ("iteration variable %qD should not be firstprivate", decl);
12691 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12692 && OMP_CLAUSE_DECL (c) == decl)
12693 error ("iteration variable %qD should not be reduction", decl);
12694 }
12695 if (c == NULL)
12696 {
12697 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12698 OMP_CLAUSE_DECL (c) = decl;
12699 c = finish_omp_clauses (c);
12700 if (c)
12701 {
12702 OMP_CLAUSE_CHAIN (c) = *clauses;
12703 *clauses = c;
12704 }
12705 }
12706 }
12707 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12708 if (COMPARISON_CLASS_P (cond))
12709 cond = build2 (TREE_CODE (cond), boolean_type_node,
12710 RECUR (TREE_OPERAND (cond, 0)),
12711 RECUR (TREE_OPERAND (cond, 1)));
12712 else
12713 cond = RECUR (cond);
12714 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12715 switch (TREE_CODE (incr))
12716 {
12717 case PREINCREMENT_EXPR:
12718 case PREDECREMENT_EXPR:
12719 case POSTINCREMENT_EXPR:
12720 case POSTDECREMENT_EXPR:
12721 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12722 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12723 break;
12724 case MODIFY_EXPR:
12725 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12726 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12727 {
12728 tree rhs = TREE_OPERAND (incr, 1);
12729 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12730 RECUR (TREE_OPERAND (incr, 0)),
12731 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12732 RECUR (TREE_OPERAND (rhs, 0)),
12733 RECUR (TREE_OPERAND (rhs, 1))));
12734 }
12735 else
12736 incr = RECUR (incr);
12737 break;
12738 case MODOP_EXPR:
12739 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12740 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12741 {
12742 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12743 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12744 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12745 TREE_TYPE (decl), lhs,
12746 RECUR (TREE_OPERAND (incr, 2))));
12747 }
12748 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12749 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12750 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12751 {
12752 tree rhs = TREE_OPERAND (incr, 2);
12753 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12754 RECUR (TREE_OPERAND (incr, 0)),
12755 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12756 RECUR (TREE_OPERAND (rhs, 0)),
12757 RECUR (TREE_OPERAND (rhs, 1))));
12758 }
12759 else
12760 incr = RECUR (incr);
12761 break;
12762 default:
12763 incr = RECUR (incr);
12764 break;
12765 }
12766
12767 TREE_VEC_ELT (declv, i) = decl;
12768 TREE_VEC_ELT (initv, i) = init;
12769 TREE_VEC_ELT (condv, i) = cond;
12770 TREE_VEC_ELT (incrv, i) = incr;
12771 #undef RECUR
12772 }
12773
12774 /* Like tsubst_copy for expressions, etc. but also does semantic
12775 processing. */
12776
12777 static tree
12778 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12779 bool integral_constant_expression_p)
12780 {
12781 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12782 #define RECUR(NODE) \
12783 tsubst_expr ((NODE), args, complain, in_decl, \
12784 integral_constant_expression_p)
12785
12786 tree stmt, tmp;
12787 tree r;
12788 location_t loc;
12789
12790 if (t == NULL_TREE || t == error_mark_node)
12791 return t;
12792
12793 loc = input_location;
12794 if (EXPR_HAS_LOCATION (t))
12795 input_location = EXPR_LOCATION (t);
12796 if (STATEMENT_CODE_P (TREE_CODE (t)))
12797 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12798
12799 switch (TREE_CODE (t))
12800 {
12801 case STATEMENT_LIST:
12802 {
12803 tree_stmt_iterator i;
12804 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12805 RECUR (tsi_stmt (i));
12806 break;
12807 }
12808
12809 case CTOR_INITIALIZER:
12810 finish_mem_initializers (tsubst_initializer_list
12811 (TREE_OPERAND (t, 0), args));
12812 break;
12813
12814 case RETURN_EXPR:
12815 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12816 break;
12817
12818 case EXPR_STMT:
12819 tmp = RECUR (EXPR_STMT_EXPR (t));
12820 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12821 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12822 else
12823 finish_expr_stmt (tmp);
12824 break;
12825
12826 case USING_STMT:
12827 do_using_directive (USING_STMT_NAMESPACE (t));
12828 break;
12829
12830 case DECL_EXPR:
12831 {
12832 tree decl, pattern_decl;
12833 tree init;
12834
12835 pattern_decl = decl = DECL_EXPR_DECL (t);
12836 if (TREE_CODE (decl) == LABEL_DECL)
12837 finish_label_decl (DECL_NAME (decl));
12838 else if (TREE_CODE (decl) == USING_DECL)
12839 {
12840 tree scope = USING_DECL_SCOPE (decl);
12841 tree name = DECL_NAME (decl);
12842 tree decl;
12843
12844 scope = tsubst (scope, args, complain, in_decl);
12845 decl = lookup_qualified_name (scope, name,
12846 /*is_type_p=*/false,
12847 /*complain=*/false);
12848 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12849 qualified_name_lookup_error (scope, name, decl, input_location);
12850 else
12851 do_local_using_decl (decl, scope, name);
12852 }
12853 else
12854 {
12855 init = DECL_INITIAL (decl);
12856 decl = tsubst (decl, args, complain, in_decl);
12857 if (decl != error_mark_node)
12858 {
12859 /* By marking the declaration as instantiated, we avoid
12860 trying to instantiate it. Since instantiate_decl can't
12861 handle local variables, and since we've already done
12862 all that needs to be done, that's the right thing to
12863 do. */
12864 if (TREE_CODE (decl) == VAR_DECL)
12865 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12866 if (TREE_CODE (decl) == VAR_DECL
12867 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12868 /* Anonymous aggregates are a special case. */
12869 finish_anon_union (decl);
12870 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12871 {
12872 DECL_CONTEXT (decl) = current_function_decl;
12873 if (DECL_NAME (decl) == this_identifier)
12874 {
12875 tree lam = DECL_CONTEXT (current_function_decl);
12876 lam = CLASSTYPE_LAMBDA_EXPR (lam);
12877 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
12878 }
12879 insert_capture_proxy (decl);
12880 }
12881 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12882 /* We already did a pushtag. */;
12883 else
12884 {
12885 int const_init = false;
12886 maybe_push_decl (decl);
12887 if (TREE_CODE (decl) == VAR_DECL
12888 && DECL_PRETTY_FUNCTION_P (decl))
12889 {
12890 /* For __PRETTY_FUNCTION__ we have to adjust the
12891 initializer. */
12892 const char *const name
12893 = cxx_printable_name (current_function_decl, 2);
12894 init = cp_fname_init (name, &TREE_TYPE (decl));
12895 }
12896 else
12897 {
12898 tree t = RECUR (init);
12899
12900 if (init && !t)
12901 {
12902 /* If we had an initializer but it
12903 instantiated to nothing,
12904 value-initialize the object. This will
12905 only occur when the initializer was a
12906 pack expansion where the parameter packs
12907 used in that expansion were of length
12908 zero. */
12909 init = build_value_init (TREE_TYPE (decl),
12910 complain);
12911 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12912 init = get_target_expr_sfinae (init, complain);
12913 }
12914 else
12915 init = t;
12916 }
12917
12918 if (TREE_CODE (decl) == VAR_DECL)
12919 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12920 (pattern_decl));
12921 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12922 }
12923 }
12924 }
12925
12926 break;
12927 }
12928
12929 case FOR_STMT:
12930 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12931 RECUR (FOR_INIT_STMT (t));
12932 finish_for_init_stmt (stmt);
12933 tmp = RECUR (FOR_COND (t));
12934 finish_for_cond (tmp, stmt);
12935 tmp = RECUR (FOR_EXPR (t));
12936 finish_for_expr (tmp, stmt);
12937 RECUR (FOR_BODY (t));
12938 finish_for_stmt (stmt);
12939 break;
12940
12941 case RANGE_FOR_STMT:
12942 {
12943 tree decl, expr;
12944 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12945 decl = RANGE_FOR_DECL (t);
12946 decl = tsubst (decl, args, complain, in_decl);
12947 maybe_push_decl (decl);
12948 expr = RECUR (RANGE_FOR_EXPR (t));
12949 stmt = cp_convert_range_for (stmt, decl, expr);
12950 RECUR (RANGE_FOR_BODY (t));
12951 finish_for_stmt (stmt);
12952 }
12953 break;
12954
12955 case WHILE_STMT:
12956 stmt = begin_while_stmt ();
12957 tmp = RECUR (WHILE_COND (t));
12958 finish_while_stmt_cond (tmp, stmt);
12959 RECUR (WHILE_BODY (t));
12960 finish_while_stmt (stmt);
12961 break;
12962
12963 case DO_STMT:
12964 stmt = begin_do_stmt ();
12965 RECUR (DO_BODY (t));
12966 finish_do_body (stmt);
12967 tmp = RECUR (DO_COND (t));
12968 finish_do_stmt (tmp, stmt);
12969 break;
12970
12971 case IF_STMT:
12972 stmt = begin_if_stmt ();
12973 tmp = RECUR (IF_COND (t));
12974 finish_if_stmt_cond (tmp, stmt);
12975 RECUR (THEN_CLAUSE (t));
12976 finish_then_clause (stmt);
12977
12978 if (ELSE_CLAUSE (t))
12979 {
12980 begin_else_clause (stmt);
12981 RECUR (ELSE_CLAUSE (t));
12982 finish_else_clause (stmt);
12983 }
12984
12985 finish_if_stmt (stmt);
12986 break;
12987
12988 case BIND_EXPR:
12989 if (BIND_EXPR_BODY_BLOCK (t))
12990 stmt = begin_function_body ();
12991 else
12992 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12993 ? BCS_TRY_BLOCK : 0);
12994
12995 RECUR (BIND_EXPR_BODY (t));
12996
12997 if (BIND_EXPR_BODY_BLOCK (t))
12998 finish_function_body (stmt);
12999 else
13000 finish_compound_stmt (stmt);
13001 break;
13002
13003 case BREAK_STMT:
13004 finish_break_stmt ();
13005 break;
13006
13007 case CONTINUE_STMT:
13008 finish_continue_stmt ();
13009 break;
13010
13011 case SWITCH_STMT:
13012 stmt = begin_switch_stmt ();
13013 tmp = RECUR (SWITCH_STMT_COND (t));
13014 finish_switch_cond (tmp, stmt);
13015 RECUR (SWITCH_STMT_BODY (t));
13016 finish_switch_stmt (stmt);
13017 break;
13018
13019 case CASE_LABEL_EXPR:
13020 finish_case_label (EXPR_LOCATION (t),
13021 RECUR (CASE_LOW (t)),
13022 RECUR (CASE_HIGH (t)));
13023 break;
13024
13025 case LABEL_EXPR:
13026 {
13027 tree decl = LABEL_EXPR_LABEL (t);
13028 tree label;
13029
13030 label = finish_label_stmt (DECL_NAME (decl));
13031 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13032 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13033 }
13034 break;
13035
13036 case GOTO_EXPR:
13037 tmp = GOTO_DESTINATION (t);
13038 if (TREE_CODE (tmp) != LABEL_DECL)
13039 /* Computed goto's must be tsubst'd into. On the other hand,
13040 non-computed gotos must not be; the identifier in question
13041 will have no binding. */
13042 tmp = RECUR (tmp);
13043 else
13044 tmp = DECL_NAME (tmp);
13045 finish_goto_stmt (tmp);
13046 break;
13047
13048 case ASM_EXPR:
13049 tmp = finish_asm_stmt
13050 (ASM_VOLATILE_P (t),
13051 RECUR (ASM_STRING (t)),
13052 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13053 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13054 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13055 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13056 {
13057 tree asm_expr = tmp;
13058 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13059 asm_expr = TREE_OPERAND (asm_expr, 0);
13060 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13061 }
13062 break;
13063
13064 case TRY_BLOCK:
13065 if (CLEANUP_P (t))
13066 {
13067 stmt = begin_try_block ();
13068 RECUR (TRY_STMTS (t));
13069 finish_cleanup_try_block (stmt);
13070 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13071 }
13072 else
13073 {
13074 tree compound_stmt = NULL_TREE;
13075
13076 if (FN_TRY_BLOCK_P (t))
13077 stmt = begin_function_try_block (&compound_stmt);
13078 else
13079 stmt = begin_try_block ();
13080
13081 RECUR (TRY_STMTS (t));
13082
13083 if (FN_TRY_BLOCK_P (t))
13084 finish_function_try_block (stmt);
13085 else
13086 finish_try_block (stmt);
13087
13088 RECUR (TRY_HANDLERS (t));
13089 if (FN_TRY_BLOCK_P (t))
13090 finish_function_handler_sequence (stmt, compound_stmt);
13091 else
13092 finish_handler_sequence (stmt);
13093 }
13094 break;
13095
13096 case HANDLER:
13097 {
13098 tree decl = HANDLER_PARMS (t);
13099
13100 if (decl)
13101 {
13102 decl = tsubst (decl, args, complain, in_decl);
13103 /* Prevent instantiate_decl from trying to instantiate
13104 this variable. We've already done all that needs to be
13105 done. */
13106 if (decl != error_mark_node)
13107 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13108 }
13109 stmt = begin_handler ();
13110 finish_handler_parms (decl, stmt);
13111 RECUR (HANDLER_BODY (t));
13112 finish_handler (stmt);
13113 }
13114 break;
13115
13116 case TAG_DEFN:
13117 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13118 if (CLASS_TYPE_P (tmp))
13119 {
13120 /* Local classes are not independent templates; they are
13121 instantiated along with their containing function. And this
13122 way we don't have to deal with pushing out of one local class
13123 to instantiate a member of another local class. */
13124 tree fn;
13125 /* Closures are handled by the LAMBDA_EXPR. */
13126 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13127 complete_type (tmp);
13128 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13129 if (!DECL_ARTIFICIAL (fn))
13130 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13131 }
13132 break;
13133
13134 case STATIC_ASSERT:
13135 {
13136 tree condition;
13137
13138 ++c_inhibit_evaluation_warnings;
13139 condition =
13140 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13141 args,
13142 complain, in_decl,
13143 /*integral_constant_expression_p=*/true);
13144 --c_inhibit_evaluation_warnings;
13145
13146 finish_static_assert (condition,
13147 STATIC_ASSERT_MESSAGE (t),
13148 STATIC_ASSERT_SOURCE_LOCATION (t),
13149 /*member_p=*/false);
13150 }
13151 break;
13152
13153 case OMP_PARALLEL:
13154 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
13155 args, complain, in_decl);
13156 stmt = begin_omp_parallel ();
13157 RECUR (OMP_PARALLEL_BODY (t));
13158 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13159 = OMP_PARALLEL_COMBINED (t);
13160 break;
13161
13162 case OMP_TASK:
13163 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
13164 args, complain, in_decl);
13165 stmt = begin_omp_task ();
13166 RECUR (OMP_TASK_BODY (t));
13167 finish_omp_task (tmp, stmt);
13168 break;
13169
13170 case OMP_FOR:
13171 {
13172 tree clauses, body, pre_body;
13173 tree declv, initv, condv, incrv;
13174 int i;
13175
13176 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
13177 args, complain, in_decl);
13178 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13179 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13180 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13181 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13182
13183 stmt = begin_omp_structured_block ();
13184
13185 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13186 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13187 &clauses, args, complain, in_decl,
13188 integral_constant_expression_p);
13189
13190 pre_body = push_stmt_list ();
13191 RECUR (OMP_FOR_PRE_BODY (t));
13192 pre_body = pop_stmt_list (pre_body);
13193
13194 body = push_stmt_list ();
13195 RECUR (OMP_FOR_BODY (t));
13196 body = pop_stmt_list (body);
13197
13198 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
13199 body, pre_body, clauses);
13200
13201 add_stmt (finish_omp_structured_block (stmt));
13202 }
13203 break;
13204
13205 case OMP_SECTIONS:
13206 case OMP_SINGLE:
13207 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
13208 stmt = push_stmt_list ();
13209 RECUR (OMP_BODY (t));
13210 stmt = pop_stmt_list (stmt);
13211
13212 t = copy_node (t);
13213 OMP_BODY (t) = stmt;
13214 OMP_CLAUSES (t) = tmp;
13215 add_stmt (t);
13216 break;
13217
13218 case OMP_SECTION:
13219 case OMP_CRITICAL:
13220 case OMP_MASTER:
13221 case OMP_ORDERED:
13222 stmt = push_stmt_list ();
13223 RECUR (OMP_BODY (t));
13224 stmt = pop_stmt_list (stmt);
13225
13226 t = copy_node (t);
13227 OMP_BODY (t) = stmt;
13228 add_stmt (t);
13229 break;
13230
13231 case OMP_ATOMIC:
13232 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13233 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13234 {
13235 tree op1 = TREE_OPERAND (t, 1);
13236 tree rhs1 = NULL_TREE;
13237 tree lhs, rhs;
13238 if (TREE_CODE (op1) == COMPOUND_EXPR)
13239 {
13240 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13241 op1 = TREE_OPERAND (op1, 1);
13242 }
13243 lhs = RECUR (TREE_OPERAND (op1, 0));
13244 rhs = RECUR (TREE_OPERAND (op1, 1));
13245 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13246 NULL_TREE, NULL_TREE, rhs1);
13247 }
13248 else
13249 {
13250 tree op1 = TREE_OPERAND (t, 1);
13251 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13252 tree rhs1 = NULL_TREE;
13253 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13254 enum tree_code opcode = NOP_EXPR;
13255 if (code == OMP_ATOMIC_READ)
13256 {
13257 v = RECUR (TREE_OPERAND (op1, 0));
13258 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13259 }
13260 else if (code == OMP_ATOMIC_CAPTURE_OLD
13261 || code == OMP_ATOMIC_CAPTURE_NEW)
13262 {
13263 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13264 v = RECUR (TREE_OPERAND (op1, 0));
13265 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13266 if (TREE_CODE (op11) == COMPOUND_EXPR)
13267 {
13268 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13269 op11 = TREE_OPERAND (op11, 1);
13270 }
13271 lhs = RECUR (TREE_OPERAND (op11, 0));
13272 rhs = RECUR (TREE_OPERAND (op11, 1));
13273 opcode = TREE_CODE (op11);
13274 }
13275 else
13276 {
13277 code = OMP_ATOMIC;
13278 lhs = RECUR (TREE_OPERAND (op1, 0));
13279 rhs = RECUR (TREE_OPERAND (op1, 1));
13280 }
13281 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13282 }
13283 break;
13284
13285 case TRANSACTION_EXPR:
13286 {
13287 int flags = 0;
13288 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13289 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13290
13291 if (TRANSACTION_EXPR_IS_STMT (t))
13292 {
13293 tree body = TRANSACTION_EXPR_BODY (t);
13294 tree noex = NULL_TREE;
13295 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13296 {
13297 noex = MUST_NOT_THROW_COND (body);
13298 if (noex == NULL_TREE)
13299 noex = boolean_true_node;
13300 body = TREE_OPERAND (body, 0);
13301 }
13302 stmt = begin_transaction_stmt (input_location, NULL, flags);
13303 RECUR (body);
13304 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13305 }
13306 else
13307 {
13308 stmt = build_transaction_expr (EXPR_LOCATION (t),
13309 RECUR (TRANSACTION_EXPR_BODY (t)),
13310 flags, NULL_TREE);
13311 RETURN (stmt);
13312 }
13313 }
13314 break;
13315
13316 case MUST_NOT_THROW_EXPR:
13317 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13318 RECUR (MUST_NOT_THROW_COND (t))));
13319
13320 case EXPR_PACK_EXPANSION:
13321 error ("invalid use of pack expansion expression");
13322 RETURN (error_mark_node);
13323
13324 case NONTYPE_ARGUMENT_PACK:
13325 error ("use %<...%> to expand argument pack");
13326 RETURN (error_mark_node);
13327
13328 case COMPOUND_EXPR:
13329 tmp = RECUR (TREE_OPERAND (t, 0));
13330 if (tmp == NULL_TREE)
13331 /* If the first operand was a statement, we're done with it. */
13332 RETURN (RECUR (TREE_OPERAND (t, 1)));
13333 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13334 RECUR (TREE_OPERAND (t, 1)),
13335 complain));
13336
13337 default:
13338 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13339
13340 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13341 /*function_p=*/false,
13342 integral_constant_expression_p));
13343 }
13344
13345 RETURN (NULL_TREE);
13346 out:
13347 input_location = loc;
13348 return r;
13349 #undef RECUR
13350 #undef RETURN
13351 }
13352
13353 /* T is a postfix-expression that is not being used in a function
13354 call. Return the substituted version of T. */
13355
13356 static tree
13357 tsubst_non_call_postfix_expression (tree t, tree args,
13358 tsubst_flags_t complain,
13359 tree in_decl)
13360 {
13361 if (TREE_CODE (t) == SCOPE_REF)
13362 t = tsubst_qualified_id (t, args, complain, in_decl,
13363 /*done=*/false, /*address_p=*/false);
13364 else
13365 t = tsubst_copy_and_build (t, args, complain, in_decl,
13366 /*function_p=*/false,
13367 /*integral_constant_expression_p=*/false);
13368
13369 return t;
13370 }
13371
13372 /* Like tsubst but deals with expressions and performs semantic
13373 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13374
13375 tree
13376 tsubst_copy_and_build (tree t,
13377 tree args,
13378 tsubst_flags_t complain,
13379 tree in_decl,
13380 bool function_p,
13381 bool integral_constant_expression_p)
13382 {
13383 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13384 #define RECUR(NODE) \
13385 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13386 /*function_p=*/false, \
13387 integral_constant_expression_p)
13388
13389 tree retval, op1;
13390 location_t loc;
13391
13392 if (t == NULL_TREE || t == error_mark_node)
13393 return t;
13394
13395 loc = input_location;
13396 if (EXPR_HAS_LOCATION (t))
13397 input_location = EXPR_LOCATION (t);
13398
13399 switch (TREE_CODE (t))
13400 {
13401 case USING_DECL:
13402 t = DECL_NAME (t);
13403 /* Fall through. */
13404 case IDENTIFIER_NODE:
13405 {
13406 tree decl;
13407 cp_id_kind idk;
13408 bool non_integral_constant_expression_p;
13409 const char *error_msg;
13410
13411 if (IDENTIFIER_TYPENAME_P (t))
13412 {
13413 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13414 t = mangle_conv_op_name_for_type (new_type);
13415 }
13416
13417 /* Look up the name. */
13418 decl = lookup_name (t);
13419
13420 /* By convention, expressions use ERROR_MARK_NODE to indicate
13421 failure, not NULL_TREE. */
13422 if (decl == NULL_TREE)
13423 decl = error_mark_node;
13424
13425 decl = finish_id_expression (t, decl, NULL_TREE,
13426 &idk,
13427 integral_constant_expression_p,
13428 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13429 &non_integral_constant_expression_p,
13430 /*template_p=*/false,
13431 /*done=*/true,
13432 /*address_p=*/false,
13433 /*template_arg_p=*/false,
13434 &error_msg,
13435 input_location);
13436 if (error_msg)
13437 error (error_msg);
13438 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13439 {
13440 if (complain & tf_error)
13441 unqualified_name_lookup_error (decl);
13442 decl = error_mark_node;
13443 }
13444 RETURN (decl);
13445 }
13446
13447 case TEMPLATE_ID_EXPR:
13448 {
13449 tree object;
13450 tree templ = RECUR (TREE_OPERAND (t, 0));
13451 tree targs = TREE_OPERAND (t, 1);
13452
13453 if (targs)
13454 targs = tsubst_template_args (targs, args, complain, in_decl);
13455
13456 if (TREE_CODE (templ) == COMPONENT_REF)
13457 {
13458 object = TREE_OPERAND (templ, 0);
13459 templ = TREE_OPERAND (templ, 1);
13460 }
13461 else
13462 object = NULL_TREE;
13463 templ = lookup_template_function (templ, targs);
13464
13465 if (object)
13466 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13467 object, templ, NULL_TREE));
13468 else
13469 RETURN (baselink_for_fns (templ));
13470 }
13471
13472 case INDIRECT_REF:
13473 {
13474 tree r = RECUR (TREE_OPERAND (t, 0));
13475
13476 if (REFERENCE_REF_P (t))
13477 {
13478 /* A type conversion to reference type will be enclosed in
13479 such an indirect ref, but the substitution of the cast
13480 will have also added such an indirect ref. */
13481 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13482 r = convert_from_reference (r);
13483 }
13484 else
13485 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13486 RETURN (r);
13487 }
13488
13489 case NOP_EXPR:
13490 RETURN (build_nop
13491 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13492 RECUR (TREE_OPERAND (t, 0))));
13493
13494 case IMPLICIT_CONV_EXPR:
13495 {
13496 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13497 tree expr = RECUR (TREE_OPERAND (t, 0));
13498 int flags = LOOKUP_IMPLICIT;
13499 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13500 flags = LOOKUP_NORMAL;
13501 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13502 flags));
13503 }
13504
13505 case CONVERT_EXPR:
13506 RETURN (build1
13507 (CONVERT_EXPR,
13508 tsubst (TREE_TYPE (t), args, complain, in_decl),
13509 RECUR (TREE_OPERAND (t, 0))));
13510
13511 case CAST_EXPR:
13512 case REINTERPRET_CAST_EXPR:
13513 case CONST_CAST_EXPR:
13514 case DYNAMIC_CAST_EXPR:
13515 case STATIC_CAST_EXPR:
13516 {
13517 tree type;
13518 tree op, r = NULL_TREE;
13519
13520 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13521 if (integral_constant_expression_p
13522 && !cast_valid_in_integral_constant_expression_p (type))
13523 {
13524 if (complain & tf_error)
13525 error ("a cast to a type other than an integral or "
13526 "enumeration type cannot appear in a constant-expression");
13527 RETURN (error_mark_node);
13528 }
13529
13530 op = RECUR (TREE_OPERAND (t, 0));
13531
13532 ++c_inhibit_evaluation_warnings;
13533 switch (TREE_CODE (t))
13534 {
13535 case CAST_EXPR:
13536 r = build_functional_cast (type, op, complain);
13537 break;
13538 case REINTERPRET_CAST_EXPR:
13539 r = build_reinterpret_cast (type, op, complain);
13540 break;
13541 case CONST_CAST_EXPR:
13542 r = build_const_cast (type, op, complain);
13543 break;
13544 case DYNAMIC_CAST_EXPR:
13545 r = build_dynamic_cast (type, op, complain);
13546 break;
13547 case STATIC_CAST_EXPR:
13548 r = build_static_cast (type, op, complain);
13549 break;
13550 default:
13551 gcc_unreachable ();
13552 }
13553 --c_inhibit_evaluation_warnings;
13554
13555 RETURN (r);
13556 }
13557
13558 case POSTDECREMENT_EXPR:
13559 case POSTINCREMENT_EXPR:
13560 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13561 args, complain, in_decl);
13562 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
13563
13564 case PREDECREMENT_EXPR:
13565 case PREINCREMENT_EXPR:
13566 case NEGATE_EXPR:
13567 case BIT_NOT_EXPR:
13568 case ABS_EXPR:
13569 case TRUTH_NOT_EXPR:
13570 case UNARY_PLUS_EXPR: /* Unary + */
13571 case REALPART_EXPR:
13572 case IMAGPART_EXPR:
13573 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
13574 RECUR (TREE_OPERAND (t, 0)), complain));
13575
13576 case FIX_TRUNC_EXPR:
13577 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13578 0, complain));
13579
13580 case ADDR_EXPR:
13581 op1 = TREE_OPERAND (t, 0);
13582 if (TREE_CODE (op1) == LABEL_DECL)
13583 RETURN (finish_label_address_expr (DECL_NAME (op1),
13584 EXPR_LOCATION (op1)));
13585 if (TREE_CODE (op1) == SCOPE_REF)
13586 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13587 /*done=*/true, /*address_p=*/true);
13588 else
13589 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13590 in_decl);
13591 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
13592
13593 case PLUS_EXPR:
13594 case MINUS_EXPR:
13595 case MULT_EXPR:
13596 case TRUNC_DIV_EXPR:
13597 case CEIL_DIV_EXPR:
13598 case FLOOR_DIV_EXPR:
13599 case ROUND_DIV_EXPR:
13600 case EXACT_DIV_EXPR:
13601 case BIT_AND_EXPR:
13602 case BIT_IOR_EXPR:
13603 case BIT_XOR_EXPR:
13604 case TRUNC_MOD_EXPR:
13605 case FLOOR_MOD_EXPR:
13606 case TRUTH_ANDIF_EXPR:
13607 case TRUTH_ORIF_EXPR:
13608 case TRUTH_AND_EXPR:
13609 case TRUTH_OR_EXPR:
13610 case RSHIFT_EXPR:
13611 case LSHIFT_EXPR:
13612 case RROTATE_EXPR:
13613 case LROTATE_EXPR:
13614 case EQ_EXPR:
13615 case NE_EXPR:
13616 case MAX_EXPR:
13617 case MIN_EXPR:
13618 case LE_EXPR:
13619 case GE_EXPR:
13620 case LT_EXPR:
13621 case GT_EXPR:
13622 case MEMBER_REF:
13623 case DOTSTAR_EXPR:
13624 {
13625 tree r;
13626
13627 ++c_inhibit_evaluation_warnings;
13628
13629 r = build_x_binary_op
13630 (input_location, TREE_CODE (t),
13631 RECUR (TREE_OPERAND (t, 0)),
13632 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13633 ? ERROR_MARK
13634 : TREE_CODE (TREE_OPERAND (t, 0))),
13635 RECUR (TREE_OPERAND (t, 1)),
13636 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13637 ? ERROR_MARK
13638 : TREE_CODE (TREE_OPERAND (t, 1))),
13639 /*overload=*/NULL,
13640 complain);
13641 if (EXPR_P (r) && TREE_NO_WARNING (t))
13642 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13643
13644 --c_inhibit_evaluation_warnings;
13645
13646 RETURN (r);
13647 }
13648
13649 case SCOPE_REF:
13650 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13651 /*address_p=*/false));
13652 case ARRAY_REF:
13653 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13654 args, complain, in_decl);
13655 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
13656 RECUR (TREE_OPERAND (t, 1)), complain));
13657
13658 case SIZEOF_EXPR:
13659 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13660 RETURN (tsubst_copy (t, args, complain, in_decl));
13661 /* Fall through */
13662
13663 case ALIGNOF_EXPR:
13664 {
13665 tree r;
13666
13667 op1 = TREE_OPERAND (t, 0);
13668 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
13669 op1 = TREE_TYPE (op1);
13670 if (!args)
13671 {
13672 /* When there are no ARGS, we are trying to evaluate a
13673 non-dependent expression from the parser. Trying to do
13674 the substitutions may not work. */
13675 if (!TYPE_P (op1))
13676 op1 = TREE_TYPE (op1);
13677 }
13678 else
13679 {
13680 ++cp_unevaluated_operand;
13681 ++c_inhibit_evaluation_warnings;
13682 if (TYPE_P (op1))
13683 op1 = tsubst (op1, args, complain, in_decl);
13684 else
13685 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13686 /*function_p=*/false,
13687 /*integral_constant_expression_p=*/
13688 false);
13689 --cp_unevaluated_operand;
13690 --c_inhibit_evaluation_warnings;
13691 }
13692 if (TYPE_P (op1))
13693 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13694 complain & tf_error);
13695 else
13696 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13697 complain & tf_error);
13698 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
13699 {
13700 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
13701 {
13702 if (!processing_template_decl && TYPE_P (op1))
13703 {
13704 r = build_min (SIZEOF_EXPR, size_type_node,
13705 build1 (NOP_EXPR, op1, error_mark_node));
13706 SIZEOF_EXPR_TYPE_P (r) = 1;
13707 }
13708 else
13709 r = build_min (SIZEOF_EXPR, size_type_node, op1);
13710 TREE_SIDE_EFFECTS (r) = 0;
13711 TREE_READONLY (r) = 1;
13712 }
13713 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
13714 }
13715 RETURN (r);
13716 }
13717
13718 case AT_ENCODE_EXPR:
13719 {
13720 op1 = TREE_OPERAND (t, 0);
13721 ++cp_unevaluated_operand;
13722 ++c_inhibit_evaluation_warnings;
13723 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13724 /*function_p=*/false,
13725 /*integral_constant_expression_p=*/false);
13726 --cp_unevaluated_operand;
13727 --c_inhibit_evaluation_warnings;
13728 RETURN (objc_build_encode_expr (op1));
13729 }
13730
13731 case NOEXCEPT_EXPR:
13732 op1 = TREE_OPERAND (t, 0);
13733 ++cp_unevaluated_operand;
13734 ++c_inhibit_evaluation_warnings;
13735 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13736 /*function_p=*/false,
13737 /*integral_constant_expression_p=*/false);
13738 --cp_unevaluated_operand;
13739 --c_inhibit_evaluation_warnings;
13740 RETURN (finish_noexcept_expr (op1, complain));
13741
13742 case MODOP_EXPR:
13743 {
13744 tree r = build_x_modify_expr
13745 (EXPR_LOCATION (t),
13746 RECUR (TREE_OPERAND (t, 0)),
13747 TREE_CODE (TREE_OPERAND (t, 1)),
13748 RECUR (TREE_OPERAND (t, 2)),
13749 complain);
13750 /* TREE_NO_WARNING must be set if either the expression was
13751 parenthesized or it uses an operator such as >>= rather
13752 than plain assignment. In the former case, it was already
13753 set and must be copied. In the latter case,
13754 build_x_modify_expr sets it and it must not be reset
13755 here. */
13756 if (TREE_NO_WARNING (t))
13757 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13758 RETURN (r);
13759 }
13760
13761 case ARROW_EXPR:
13762 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13763 args, complain, in_decl);
13764 /* Remember that there was a reference to this entity. */
13765 if (DECL_P (op1))
13766 mark_used (op1);
13767 RETURN (build_x_arrow (input_location, op1, complain));
13768
13769 case NEW_EXPR:
13770 {
13771 tree placement = RECUR (TREE_OPERAND (t, 0));
13772 tree init = RECUR (TREE_OPERAND (t, 3));
13773 vec<tree, va_gc> *placement_vec;
13774 vec<tree, va_gc> *init_vec;
13775 tree ret;
13776
13777 if (placement == NULL_TREE)
13778 placement_vec = NULL;
13779 else
13780 {
13781 placement_vec = make_tree_vector ();
13782 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13783 vec_safe_push (placement_vec, TREE_VALUE (placement));
13784 }
13785
13786 /* If there was an initializer in the original tree, but it
13787 instantiated to an empty list, then we should pass a
13788 non-NULL empty vector to tell build_new that it was an
13789 empty initializer() rather than no initializer. This can
13790 only happen when the initializer is a pack expansion whose
13791 parameter packs are of length zero. */
13792 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13793 init_vec = NULL;
13794 else
13795 {
13796 init_vec = make_tree_vector ();
13797 if (init == void_zero_node)
13798 gcc_assert (init_vec != NULL);
13799 else
13800 {
13801 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13802 vec_safe_push (init_vec, TREE_VALUE (init));
13803 }
13804 }
13805
13806 ret = build_new (&placement_vec,
13807 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13808 RECUR (TREE_OPERAND (t, 2)),
13809 &init_vec,
13810 NEW_EXPR_USE_GLOBAL (t),
13811 complain);
13812
13813 if (placement_vec != NULL)
13814 release_tree_vector (placement_vec);
13815 if (init_vec != NULL)
13816 release_tree_vector (init_vec);
13817
13818 RETURN (ret);
13819 }
13820
13821 case DELETE_EXPR:
13822 RETURN (delete_sanity
13823 (RECUR (TREE_OPERAND (t, 0)),
13824 RECUR (TREE_OPERAND (t, 1)),
13825 DELETE_EXPR_USE_VEC (t),
13826 DELETE_EXPR_USE_GLOBAL (t),
13827 complain));
13828
13829 case COMPOUND_EXPR:
13830 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
13831 RECUR (TREE_OPERAND (t, 0)),
13832 RECUR (TREE_OPERAND (t, 1)),
13833 complain));
13834
13835 case CALL_EXPR:
13836 {
13837 tree function;
13838 vec<tree, va_gc> *call_args;
13839 unsigned int nargs, i;
13840 bool qualified_p;
13841 bool koenig_p;
13842 tree ret;
13843
13844 function = CALL_EXPR_FN (t);
13845 /* When we parsed the expression, we determined whether or
13846 not Koenig lookup should be performed. */
13847 koenig_p = KOENIG_LOOKUP_P (t);
13848 if (TREE_CODE (function) == SCOPE_REF)
13849 {
13850 qualified_p = true;
13851 function = tsubst_qualified_id (function, args, complain, in_decl,
13852 /*done=*/false,
13853 /*address_p=*/false);
13854 }
13855 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13856 {
13857 /* Do nothing; calling tsubst_copy_and_build on an identifier
13858 would incorrectly perform unqualified lookup again.
13859
13860 Note that we can also have an IDENTIFIER_NODE if the earlier
13861 unqualified lookup found a member function; in that case
13862 koenig_p will be false and we do want to do the lookup
13863 again to find the instantiated member function.
13864
13865 FIXME but doing that causes c++/15272, so we need to stop
13866 using IDENTIFIER_NODE in that situation. */
13867 qualified_p = false;
13868 }
13869 else
13870 {
13871 if (TREE_CODE (function) == COMPONENT_REF)
13872 {
13873 tree op = TREE_OPERAND (function, 1);
13874
13875 qualified_p = (TREE_CODE (op) == SCOPE_REF
13876 || (BASELINK_P (op)
13877 && BASELINK_QUALIFIED_P (op)));
13878 }
13879 else
13880 qualified_p = false;
13881
13882 if (TREE_CODE (function) == ADDR_EXPR
13883 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
13884 /* Avoid error about taking the address of a constructor. */
13885 function = TREE_OPERAND (function, 0);
13886
13887 function = tsubst_copy_and_build (function, args, complain,
13888 in_decl,
13889 !qualified_p,
13890 integral_constant_expression_p);
13891
13892 if (BASELINK_P (function))
13893 qualified_p = true;
13894 }
13895
13896 nargs = call_expr_nargs (t);
13897 call_args = make_tree_vector ();
13898 for (i = 0; i < nargs; ++i)
13899 {
13900 tree arg = CALL_EXPR_ARG (t, i);
13901
13902 if (!PACK_EXPANSION_P (arg))
13903 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
13904 else
13905 {
13906 /* Expand the pack expansion and push each entry onto
13907 CALL_ARGS. */
13908 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13909 if (TREE_CODE (arg) == TREE_VEC)
13910 {
13911 unsigned int len, j;
13912
13913 len = TREE_VEC_LENGTH (arg);
13914 for (j = 0; j < len; ++j)
13915 {
13916 tree value = TREE_VEC_ELT (arg, j);
13917 if (value != NULL_TREE)
13918 value = convert_from_reference (value);
13919 vec_safe_push (call_args, value);
13920 }
13921 }
13922 else
13923 {
13924 /* A partial substitution. Add one entry. */
13925 vec_safe_push (call_args, arg);
13926 }
13927 }
13928 }
13929
13930 /* We do not perform argument-dependent lookup if normal
13931 lookup finds a non-function, in accordance with the
13932 expected resolution of DR 218. */
13933 if (koenig_p
13934 && ((is_overloaded_fn (function)
13935 /* If lookup found a member function, the Koenig lookup is
13936 not appropriate, even if an unqualified-name was used
13937 to denote the function. */
13938 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13939 || TREE_CODE (function) == IDENTIFIER_NODE)
13940 /* Only do this when substitution turns a dependent call
13941 into a non-dependent call. */
13942 && type_dependent_expression_p_push (t)
13943 && !any_type_dependent_arguments_p (call_args))
13944 function = perform_koenig_lookup (function, call_args, false,
13945 tf_none);
13946
13947 if (TREE_CODE (function) == IDENTIFIER_NODE
13948 && !any_type_dependent_arguments_p (call_args))
13949 {
13950 if (koenig_p && (complain & tf_warning_or_error))
13951 {
13952 /* For backwards compatibility and good diagnostics, try
13953 the unqualified lookup again if we aren't in SFINAE
13954 context. */
13955 tree unq = (tsubst_copy_and_build
13956 (function, args, complain, in_decl, true,
13957 integral_constant_expression_p));
13958 if (unq == error_mark_node)
13959 RETURN (error_mark_node);
13960
13961 if (unq != function)
13962 {
13963 tree fn = unq;
13964 if (TREE_CODE (fn) == INDIRECT_REF)
13965 fn = TREE_OPERAND (fn, 0);
13966 if (TREE_CODE (fn) == COMPONENT_REF)
13967 fn = TREE_OPERAND (fn, 1);
13968 if (is_overloaded_fn (fn))
13969 fn = get_first_fn (fn);
13970 permerror (EXPR_LOC_OR_HERE (t),
13971 "%qD was not declared in this scope, "
13972 "and no declarations were found by "
13973 "argument-dependent lookup at the point "
13974 "of instantiation", function);
13975 if (!DECL_P (fn))
13976 /* Can't say anything more. */;
13977 else if (DECL_CLASS_SCOPE_P (fn))
13978 {
13979 inform (EXPR_LOC_OR_HERE (t),
13980 "declarations in dependent base %qT are "
13981 "not found by unqualified lookup",
13982 DECL_CLASS_CONTEXT (fn));
13983 if (current_class_ptr)
13984 inform (EXPR_LOC_OR_HERE (t),
13985 "use %<this->%D%> instead", function);
13986 else
13987 inform (EXPR_LOC_OR_HERE (t),
13988 "use %<%T::%D%> instead",
13989 current_class_name, function);
13990 }
13991 else
13992 inform (0, "%q+D declared here, later in the "
13993 "translation unit", fn);
13994 function = unq;
13995 }
13996 }
13997 if (TREE_CODE (function) == IDENTIFIER_NODE)
13998 {
13999 if (complain & tf_error)
14000 unqualified_name_lookup_error (function);
14001 release_tree_vector (call_args);
14002 RETURN (error_mark_node);
14003 }
14004 }
14005
14006 /* Remember that there was a reference to this entity. */
14007 if (DECL_P (function))
14008 mark_used (function);
14009
14010 if (TREE_CODE (function) == OFFSET_REF)
14011 ret = build_offset_ref_call_from_tree (function, &call_args,
14012 complain);
14013 else if (TREE_CODE (function) == COMPONENT_REF)
14014 {
14015 tree instance = TREE_OPERAND (function, 0);
14016 tree fn = TREE_OPERAND (function, 1);
14017
14018 if (processing_template_decl
14019 && (type_dependent_expression_p (instance)
14020 || (!BASELINK_P (fn)
14021 && TREE_CODE (fn) != FIELD_DECL)
14022 || type_dependent_expression_p (fn)
14023 || any_type_dependent_arguments_p (call_args)))
14024 ret = build_nt_call_vec (function, call_args);
14025 else if (!BASELINK_P (fn))
14026 ret = finish_call_expr (function, &call_args,
14027 /*disallow_virtual=*/false,
14028 /*koenig_p=*/false,
14029 complain);
14030 else
14031 ret = (build_new_method_call
14032 (instance, fn,
14033 &call_args, NULL_TREE,
14034 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14035 /*fn_p=*/NULL,
14036 complain));
14037 }
14038 else
14039 ret = finish_call_expr (function, &call_args,
14040 /*disallow_virtual=*/qualified_p,
14041 koenig_p,
14042 complain);
14043
14044 release_tree_vector (call_args);
14045
14046 RETURN (ret);
14047 }
14048
14049 case COND_EXPR:
14050 {
14051 tree cond = RECUR (TREE_OPERAND (t, 0));
14052 tree exp1, exp2;
14053
14054 if (TREE_CODE (cond) == INTEGER_CST)
14055 {
14056 if (integer_zerop (cond))
14057 {
14058 ++c_inhibit_evaluation_warnings;
14059 exp1 = RECUR (TREE_OPERAND (t, 1));
14060 --c_inhibit_evaluation_warnings;
14061 exp2 = RECUR (TREE_OPERAND (t, 2));
14062 }
14063 else
14064 {
14065 exp1 = RECUR (TREE_OPERAND (t, 1));
14066 ++c_inhibit_evaluation_warnings;
14067 exp2 = RECUR (TREE_OPERAND (t, 2));
14068 --c_inhibit_evaluation_warnings;
14069 }
14070 }
14071 else
14072 {
14073 exp1 = RECUR (TREE_OPERAND (t, 1));
14074 exp2 = RECUR (TREE_OPERAND (t, 2));
14075 }
14076
14077 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14078 cond, exp1, exp2, complain));
14079 }
14080
14081 case PSEUDO_DTOR_EXPR:
14082 RETURN (finish_pseudo_destructor_expr
14083 (RECUR (TREE_OPERAND (t, 0)),
14084 RECUR (TREE_OPERAND (t, 1)),
14085 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)));
14086
14087 case TREE_LIST:
14088 {
14089 tree purpose, value, chain;
14090
14091 if (t == void_list_node)
14092 RETURN (t);
14093
14094 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14095 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14096 {
14097 /* We have pack expansions, so expand those and
14098 create a new list out of it. */
14099 tree purposevec = NULL_TREE;
14100 tree valuevec = NULL_TREE;
14101 tree chain;
14102 int i, len = -1;
14103
14104 /* Expand the argument expressions. */
14105 if (TREE_PURPOSE (t))
14106 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14107 complain, in_decl);
14108 if (TREE_VALUE (t))
14109 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14110 complain, in_decl);
14111
14112 /* Build the rest of the list. */
14113 chain = TREE_CHAIN (t);
14114 if (chain && chain != void_type_node)
14115 chain = RECUR (chain);
14116
14117 /* Determine the number of arguments. */
14118 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14119 {
14120 len = TREE_VEC_LENGTH (purposevec);
14121 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14122 }
14123 else if (TREE_CODE (valuevec) == TREE_VEC)
14124 len = TREE_VEC_LENGTH (valuevec);
14125 else
14126 {
14127 /* Since we only performed a partial substitution into
14128 the argument pack, we only RETURN (a single list
14129 node. */
14130 if (purposevec == TREE_PURPOSE (t)
14131 && valuevec == TREE_VALUE (t)
14132 && chain == TREE_CHAIN (t))
14133 RETURN (t);
14134
14135 RETURN (tree_cons (purposevec, valuevec, chain));
14136 }
14137
14138 /* Convert the argument vectors into a TREE_LIST */
14139 i = len;
14140 while (i > 0)
14141 {
14142 /* Grab the Ith values. */
14143 i--;
14144 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14145 : NULL_TREE;
14146 value
14147 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14148 : NULL_TREE;
14149
14150 /* Build the list (backwards). */
14151 chain = tree_cons (purpose, value, chain);
14152 }
14153
14154 RETURN (chain);
14155 }
14156
14157 purpose = TREE_PURPOSE (t);
14158 if (purpose)
14159 purpose = RECUR (purpose);
14160 value = TREE_VALUE (t);
14161 if (value)
14162 value = RECUR (value);
14163 chain = TREE_CHAIN (t);
14164 if (chain && chain != void_type_node)
14165 chain = RECUR (chain);
14166 if (purpose == TREE_PURPOSE (t)
14167 && value == TREE_VALUE (t)
14168 && chain == TREE_CHAIN (t))
14169 RETURN (t);
14170 RETURN (tree_cons (purpose, value, chain));
14171 }
14172
14173 case COMPONENT_REF:
14174 {
14175 tree object;
14176 tree object_type;
14177 tree member;
14178
14179 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14180 args, complain, in_decl);
14181 /* Remember that there was a reference to this entity. */
14182 if (DECL_P (object))
14183 mark_used (object);
14184 object_type = TREE_TYPE (object);
14185
14186 member = TREE_OPERAND (t, 1);
14187 if (BASELINK_P (member))
14188 member = tsubst_baselink (member,
14189 non_reference (TREE_TYPE (object)),
14190 args, complain, in_decl);
14191 else
14192 member = tsubst_copy (member, args, complain, in_decl);
14193 if (member == error_mark_node)
14194 RETURN (error_mark_node);
14195
14196 if (type_dependent_expression_p (object))
14197 /* We can't do much here. */;
14198 else if (!CLASS_TYPE_P (object_type))
14199 {
14200 if (scalarish_type_p (object_type))
14201 {
14202 tree s = NULL_TREE;
14203 tree dtor = member;
14204
14205 if (TREE_CODE (dtor) == SCOPE_REF)
14206 {
14207 s = TREE_OPERAND (dtor, 0);
14208 dtor = TREE_OPERAND (dtor, 1);
14209 }
14210 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14211 {
14212 dtor = TREE_OPERAND (dtor, 0);
14213 if (TYPE_P (dtor))
14214 RETURN (finish_pseudo_destructor_expr (object, s, dtor));
14215 }
14216 }
14217 }
14218 else if (TREE_CODE (member) == SCOPE_REF
14219 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14220 {
14221 /* Lookup the template functions now that we know what the
14222 scope is. */
14223 tree scope = TREE_OPERAND (member, 0);
14224 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14225 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14226 member = lookup_qualified_name (scope, tmpl,
14227 /*is_type_p=*/false,
14228 /*complain=*/false);
14229 if (BASELINK_P (member))
14230 {
14231 BASELINK_FUNCTIONS (member)
14232 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14233 args);
14234 member = (adjust_result_of_qualified_name_lookup
14235 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14236 object_type));
14237 }
14238 else
14239 {
14240 qualified_name_lookup_error (scope, tmpl, member,
14241 input_location);
14242 RETURN (error_mark_node);
14243 }
14244 }
14245 else if (TREE_CODE (member) == SCOPE_REF
14246 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14247 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14248 {
14249 if (complain & tf_error)
14250 {
14251 if (TYPE_P (TREE_OPERAND (member, 0)))
14252 error ("%qT is not a class or namespace",
14253 TREE_OPERAND (member, 0));
14254 else
14255 error ("%qD is not a class or namespace",
14256 TREE_OPERAND (member, 0));
14257 }
14258 RETURN (error_mark_node);
14259 }
14260 else if (TREE_CODE (member) == FIELD_DECL)
14261 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14262
14263 RETURN (finish_class_member_access_expr (object, member,
14264 /*template_p=*/false,
14265 complain));
14266 }
14267
14268 case THROW_EXPR:
14269 RETURN (build_throw
14270 (RECUR (TREE_OPERAND (t, 0))));
14271
14272 case CONSTRUCTOR:
14273 {
14274 vec<constructor_elt, va_gc> *n;
14275 constructor_elt *ce;
14276 unsigned HOST_WIDE_INT idx;
14277 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14278 bool process_index_p;
14279 int newlen;
14280 bool need_copy_p = false;
14281 tree r;
14282
14283 if (type == error_mark_node)
14284 RETURN (error_mark_node);
14285
14286 /* digest_init will do the wrong thing if we let it. */
14287 if (type && TYPE_PTRMEMFUNC_P (type))
14288 RETURN (t);
14289
14290 /* We do not want to process the index of aggregate
14291 initializers as they are identifier nodes which will be
14292 looked up by digest_init. */
14293 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14294
14295 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14296 newlen = vec_safe_length (n);
14297 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14298 {
14299 if (ce->index && process_index_p)
14300 ce->index = RECUR (ce->index);
14301
14302 if (PACK_EXPANSION_P (ce->value))
14303 {
14304 /* Substitute into the pack expansion. */
14305 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14306 in_decl);
14307
14308 if (ce->value == error_mark_node
14309 || PACK_EXPANSION_P (ce->value))
14310 ;
14311 else if (TREE_VEC_LENGTH (ce->value) == 1)
14312 /* Just move the argument into place. */
14313 ce->value = TREE_VEC_ELT (ce->value, 0);
14314 else
14315 {
14316 /* Update the length of the final CONSTRUCTOR
14317 arguments vector, and note that we will need to
14318 copy.*/
14319 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14320 need_copy_p = true;
14321 }
14322 }
14323 else
14324 ce->value = RECUR (ce->value);
14325 }
14326
14327 if (need_copy_p)
14328 {
14329 vec<constructor_elt, va_gc> *old_n = n;
14330
14331 vec_alloc (n, newlen);
14332 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14333 {
14334 if (TREE_CODE (ce->value) == TREE_VEC)
14335 {
14336 int i, len = TREE_VEC_LENGTH (ce->value);
14337 for (i = 0; i < len; ++i)
14338 CONSTRUCTOR_APPEND_ELT (n, 0,
14339 TREE_VEC_ELT (ce->value, i));
14340 }
14341 else
14342 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14343 }
14344 }
14345
14346 r = build_constructor (init_list_type_node, n);
14347 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14348
14349 if (TREE_HAS_CONSTRUCTOR (t))
14350 RETURN (finish_compound_literal (type, r, complain));
14351
14352 TREE_TYPE (r) = type;
14353 RETURN (r);
14354 }
14355
14356 case TYPEID_EXPR:
14357 {
14358 tree operand_0 = TREE_OPERAND (t, 0);
14359 if (TYPE_P (operand_0))
14360 {
14361 operand_0 = tsubst (operand_0, args, complain, in_decl);
14362 RETURN (get_typeid (operand_0, complain));
14363 }
14364 else
14365 {
14366 operand_0 = RECUR (operand_0);
14367 RETURN (build_typeid (operand_0, complain));
14368 }
14369 }
14370
14371 case VAR_DECL:
14372 if (!args)
14373 RETURN (t);
14374 /* Fall through */
14375
14376 case PARM_DECL:
14377 {
14378 tree r = tsubst_copy (t, args, complain, in_decl);
14379
14380 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14381 /* If the original type was a reference, we'll be wrapped in
14382 the appropriate INDIRECT_REF. */
14383 r = convert_from_reference (r);
14384 RETURN (r);
14385 }
14386
14387 case VA_ARG_EXPR:
14388 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14389 RECUR (TREE_OPERAND (t, 0)),
14390 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14391
14392 case OFFSETOF_EXPR:
14393 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14394
14395 case TRAIT_EXPR:
14396 {
14397 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14398 complain, in_decl);
14399
14400 tree type2 = TRAIT_EXPR_TYPE2 (t);
14401 if (type2)
14402 type2 = tsubst_copy (type2, args, complain, in_decl);
14403
14404 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14405 }
14406
14407 case STMT_EXPR:
14408 {
14409 tree old_stmt_expr = cur_stmt_expr;
14410 tree stmt_expr = begin_stmt_expr ();
14411
14412 cur_stmt_expr = stmt_expr;
14413 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14414 integral_constant_expression_p);
14415 stmt_expr = finish_stmt_expr (stmt_expr, false);
14416 cur_stmt_expr = old_stmt_expr;
14417
14418 /* If the resulting list of expression statement is empty,
14419 fold it further into void_zero_node. */
14420 if (empty_expr_stmt_p (stmt_expr))
14421 stmt_expr = void_zero_node;
14422
14423 RETURN (stmt_expr);
14424 }
14425
14426 case LAMBDA_EXPR:
14427 {
14428 tree r = build_lambda_expr ();
14429
14430 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14431 LAMBDA_EXPR_CLOSURE (r) = type;
14432 CLASSTYPE_LAMBDA_EXPR (type) = r;
14433
14434 LAMBDA_EXPR_LOCATION (r)
14435 = LAMBDA_EXPR_LOCATION (t);
14436 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14437 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14438 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14439 LAMBDA_EXPR_DISCRIMINATOR (r)
14440 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14441 /* For a function scope, we want to use tsubst so that we don't
14442 complain about referring to an auto function before its return
14443 type has been deduced. Otherwise, we want to use tsubst_copy so
14444 that we look up the existing field/parameter/variable rather
14445 than build a new one. */
14446 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14447 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14448 scope = tsubst (scope, args, complain, in_decl);
14449 else if (scope && TREE_CODE (scope) == PARM_DECL)
14450 {
14451 /* Look up the parameter we want directly, as tsubst_copy
14452 doesn't do what we need. */
14453 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14454 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14455 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14456 parm = DECL_CHAIN (parm);
14457 scope = parm;
14458 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14459 if (DECL_CONTEXT (scope) == NULL_TREE)
14460 DECL_CONTEXT (scope) = fn;
14461 }
14462 else
14463 scope = RECUR (scope);
14464 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
14465 LAMBDA_EXPR_RETURN_TYPE (r)
14466 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14467
14468 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14469 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14470
14471 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14472 determine_visibility (TYPE_NAME (type));
14473 /* Now that we know visibility, instantiate the type so we have a
14474 declaration of the op() for later calls to lambda_function. */
14475 complete_type (type);
14476
14477 /* The capture list refers to closure members, so this needs to
14478 wait until after we finish instantiating the type. Also keep
14479 any captures that may have been added during instantiation. */
14480 LAMBDA_EXPR_CAPTURE_LIST (r)
14481 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)),
14482 LAMBDA_EXPR_CAPTURE_LIST (r));
14483 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
14484
14485 RETURN (build_lambda_object (r));
14486 }
14487
14488 case TARGET_EXPR:
14489 /* We can get here for a constant initializer of non-dependent type.
14490 FIXME stop folding in cp_parser_initializer_clause. */
14491 {
14492 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
14493 complain);
14494 RETURN (r);
14495 }
14496
14497 case TRANSACTION_EXPR:
14498 RETURN (tsubst_expr(t, args, complain, in_decl,
14499 integral_constant_expression_p));
14500
14501 default:
14502 /* Handle Objective-C++ constructs, if appropriate. */
14503 {
14504 tree subst
14505 = objcp_tsubst_copy_and_build (t, args, complain,
14506 in_decl, /*function_p=*/false);
14507 if (subst)
14508 RETURN (subst);
14509 }
14510 RETURN (tsubst_copy (t, args, complain, in_decl));
14511 }
14512
14513 #undef RECUR
14514 #undef RETURN
14515 out:
14516 input_location = loc;
14517 return retval;
14518 }
14519
14520 /* Verify that the instantiated ARGS are valid. For type arguments,
14521 make sure that the type's linkage is ok. For non-type arguments,
14522 make sure they are constants if they are integral or enumerations.
14523 Emit an error under control of COMPLAIN, and return TRUE on error. */
14524
14525 static bool
14526 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14527 {
14528 if (dependent_template_arg_p (t))
14529 return false;
14530 if (ARGUMENT_PACK_P (t))
14531 {
14532 tree vec = ARGUMENT_PACK_ARGS (t);
14533 int len = TREE_VEC_LENGTH (vec);
14534 bool result = false;
14535 int i;
14536
14537 for (i = 0; i < len; ++i)
14538 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14539 result = true;
14540 return result;
14541 }
14542 else if (TYPE_P (t))
14543 {
14544 /* [basic.link]: A name with no linkage (notably, the name
14545 of a class or enumeration declared in a local scope)
14546 shall not be used to declare an entity with linkage.
14547 This implies that names with no linkage cannot be used as
14548 template arguments
14549
14550 DR 757 relaxes this restriction for C++0x. */
14551 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14552 : no_linkage_check (t, /*relaxed_p=*/false));
14553
14554 if (nt)
14555 {
14556 /* DR 488 makes use of a type with no linkage cause
14557 type deduction to fail. */
14558 if (complain & tf_error)
14559 {
14560 if (TYPE_ANONYMOUS_P (nt))
14561 error ("%qT is/uses anonymous type", t);
14562 else
14563 error ("template argument for %qD uses local type %qT",
14564 tmpl, t);
14565 }
14566 return true;
14567 }
14568 /* In order to avoid all sorts of complications, we do not
14569 allow variably-modified types as template arguments. */
14570 else if (variably_modified_type_p (t, NULL_TREE))
14571 {
14572 if (complain & tf_error)
14573 error ("%qT is a variably modified type", t);
14574 return true;
14575 }
14576 }
14577 /* Class template and alias template arguments should be OK. */
14578 else if (DECL_TYPE_TEMPLATE_P (t))
14579 ;
14580 /* A non-type argument of integral or enumerated type must be a
14581 constant. */
14582 else if (TREE_TYPE (t)
14583 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14584 && !TREE_CONSTANT (t))
14585 {
14586 if (complain & tf_error)
14587 error ("integral expression %qE is not constant", t);
14588 return true;
14589 }
14590 return false;
14591 }
14592
14593 static bool
14594 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14595 {
14596 int ix, len = DECL_NTPARMS (tmpl);
14597 bool result = false;
14598
14599 for (ix = 0; ix != len; ix++)
14600 {
14601 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14602 result = true;
14603 }
14604 if (result && (complain & tf_error))
14605 error (" trying to instantiate %qD", tmpl);
14606 return result;
14607 }
14608
14609 /* We're out of SFINAE context now, so generate diagnostics for the access
14610 errors we saw earlier when instantiating D from TMPL and ARGS. */
14611
14612 static void
14613 recheck_decl_substitution (tree d, tree tmpl, tree args)
14614 {
14615 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
14616 tree type = TREE_TYPE (pattern);
14617 location_t loc = input_location;
14618
14619 push_access_scope (d);
14620 push_deferring_access_checks (dk_no_deferred);
14621 input_location = DECL_SOURCE_LOCATION (pattern);
14622 tsubst (type, args, tf_warning_or_error, d);
14623 input_location = loc;
14624 pop_deferring_access_checks ();
14625 pop_access_scope (d);
14626 }
14627
14628 /* Instantiate the indicated variable, function, or alias template TMPL with
14629 the template arguments in TARG_PTR. */
14630
14631 static tree
14632 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14633 {
14634 tree targ_ptr = orig_args;
14635 tree fndecl;
14636 tree gen_tmpl;
14637 tree spec;
14638 bool access_ok = true;
14639
14640 if (tmpl == error_mark_node)
14641 return error_mark_node;
14642
14643 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14644
14645 /* If this function is a clone, handle it specially. */
14646 if (DECL_CLONED_FUNCTION_P (tmpl))
14647 {
14648 tree spec;
14649 tree clone;
14650
14651 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14652 DECL_CLONED_FUNCTION. */
14653 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14654 targ_ptr, complain);
14655 if (spec == error_mark_node)
14656 return error_mark_node;
14657
14658 /* Look for the clone. */
14659 FOR_EACH_CLONE (clone, spec)
14660 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14661 return clone;
14662 /* We should always have found the clone by now. */
14663 gcc_unreachable ();
14664 return NULL_TREE;
14665 }
14666
14667 /* Check to see if we already have this specialization. */
14668 gen_tmpl = most_general_template (tmpl);
14669 if (tmpl != gen_tmpl)
14670 /* The TMPL is a partial instantiation. To get a full set of
14671 arguments we must add the arguments used to perform the
14672 partial instantiation. */
14673 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14674 targ_ptr);
14675
14676 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14677 but it doesn't seem to be on the hot path. */
14678 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14679
14680 gcc_assert (tmpl == gen_tmpl
14681 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14682 == spec)
14683 || fndecl == NULL_TREE);
14684
14685 if (spec != NULL_TREE)
14686 {
14687 if (FNDECL_HAS_ACCESS_ERRORS (spec))
14688 {
14689 if (complain & tf_error)
14690 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
14691 return error_mark_node;
14692 }
14693 return spec;
14694 }
14695
14696 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14697 complain))
14698 return error_mark_node;
14699
14700 /* We are building a FUNCTION_DECL, during which the access of its
14701 parameters and return types have to be checked. However this
14702 FUNCTION_DECL which is the desired context for access checking
14703 is not built yet. We solve this chicken-and-egg problem by
14704 deferring all checks until we have the FUNCTION_DECL. */
14705 push_deferring_access_checks (dk_deferred);
14706
14707 /* Instantiation of the function happens in the context of the function
14708 template, not the context of the overload resolution we're doing. */
14709 push_to_top_level ();
14710 /* If there are dependent arguments, e.g. because we're doing partial
14711 ordering, make sure processing_template_decl stays set. */
14712 if (uses_template_parms (targ_ptr))
14713 ++processing_template_decl;
14714 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14715 {
14716 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14717 complain, gen_tmpl);
14718 push_nested_class (ctx);
14719 }
14720 /* Substitute template parameters to obtain the specialization. */
14721 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14722 targ_ptr, complain, gen_tmpl);
14723 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14724 pop_nested_class ();
14725 pop_from_top_level ();
14726
14727 if (fndecl == error_mark_node)
14728 {
14729 pop_deferring_access_checks ();
14730 return error_mark_node;
14731 }
14732
14733 /* The DECL_TI_TEMPLATE should always be the immediate parent
14734 template, not the most general template. */
14735 DECL_TI_TEMPLATE (fndecl) = tmpl;
14736
14737 /* Now we know the specialization, compute access previously
14738 deferred. */
14739 push_access_scope (fndecl);
14740 if (!perform_deferred_access_checks (complain))
14741 access_ok = false;
14742 pop_access_scope (fndecl);
14743 pop_deferring_access_checks ();
14744
14745 /* If we've just instantiated the main entry point for a function,
14746 instantiate all the alternate entry points as well. We do this
14747 by cloning the instantiation of the main entry point, not by
14748 instantiating the template clones. */
14749 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14750 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14751
14752 if (!access_ok)
14753 {
14754 if (!(complain & tf_error))
14755 {
14756 /* Remember to reinstantiate when we're out of SFINAE so the user
14757 can see the errors. */
14758 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
14759 }
14760 return error_mark_node;
14761 }
14762 return fndecl;
14763 }
14764
14765 /* Wrapper for instantiate_template_1. */
14766
14767 tree
14768 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14769 {
14770 tree ret;
14771 timevar_push (TV_TEMPLATE_INST);
14772 ret = instantiate_template_1 (tmpl, orig_args, complain);
14773 timevar_pop (TV_TEMPLATE_INST);
14774 return ret;
14775 }
14776
14777 /* Instantiate the alias template TMPL with ARGS. Also push a template
14778 instantiation level, which instantiate_template doesn't do because
14779 functions and variables have sufficient context established by the
14780 callers. */
14781
14782 static tree
14783 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
14784 {
14785 struct pending_template *old_last_pend = last_pending_template;
14786 struct tinst_level *old_error_tinst = last_error_tinst_level;
14787 if (tmpl == error_mark_node || args == error_mark_node)
14788 return error_mark_node;
14789 tree tinst = build_tree_list (tmpl, args);
14790 if (!push_tinst_level (tinst))
14791 {
14792 ggc_free (tinst);
14793 return error_mark_node;
14794 }
14795
14796 args =
14797 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
14798 args, tmpl, complain,
14799 /*require_all_args=*/true,
14800 /*use_default_args=*/true);
14801
14802 tree r = instantiate_template (tmpl, args, complain);
14803 pop_tinst_level ();
14804 /* We can't free this if a pending_template entry or last_error_tinst_level
14805 is pointing at it. */
14806 if (last_pending_template == old_last_pend
14807 && last_error_tinst_level == old_error_tinst)
14808 ggc_free (tinst);
14809
14810 return r;
14811 }
14812
14813 /* PARM is a template parameter pack for FN. Returns true iff
14814 PARM is used in a deducible way in the argument list of FN. */
14815
14816 static bool
14817 pack_deducible_p (tree parm, tree fn)
14818 {
14819 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14820 for (; t; t = TREE_CHAIN (t))
14821 {
14822 tree type = TREE_VALUE (t);
14823 tree packs;
14824 if (!PACK_EXPANSION_P (type))
14825 continue;
14826 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14827 packs; packs = TREE_CHAIN (packs))
14828 if (TREE_VALUE (packs) == parm)
14829 {
14830 /* The template parameter pack is used in a function parameter
14831 pack. If this is the end of the parameter list, the
14832 template parameter pack is deducible. */
14833 if (TREE_CHAIN (t) == void_list_node)
14834 return true;
14835 else
14836 /* Otherwise, not. Well, it could be deduced from
14837 a non-pack parameter, but doing so would end up with
14838 a deduction mismatch, so don't bother. */
14839 return false;
14840 }
14841 }
14842 /* The template parameter pack isn't used in any function parameter
14843 packs, but it might be used deeper, e.g. tuple<Args...>. */
14844 return true;
14845 }
14846
14847 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14848 NARGS elements of the arguments that are being used when calling
14849 it. TARGS is a vector into which the deduced template arguments
14850 are placed.
14851
14852 Return zero for success, 2 for an incomplete match that doesn't resolve
14853 all the types, and 1 for complete failure. An error message will be
14854 printed only for an incomplete match.
14855
14856 If FN is a conversion operator, or we are trying to produce a specific
14857 specialization, RETURN_TYPE is the return type desired.
14858
14859 The EXPLICIT_TARGS are explicit template arguments provided via a
14860 template-id.
14861
14862 The parameter STRICT is one of:
14863
14864 DEDUCE_CALL:
14865 We are deducing arguments for a function call, as in
14866 [temp.deduct.call].
14867
14868 DEDUCE_CONV:
14869 We are deducing arguments for a conversion function, as in
14870 [temp.deduct.conv].
14871
14872 DEDUCE_EXACT:
14873 We are deducing arguments when doing an explicit instantiation
14874 as in [temp.explicit], when determining an explicit specialization
14875 as in [temp.expl.spec], or when taking the address of a function
14876 template, as in [temp.deduct.funcaddr]. */
14877
14878 tree
14879 fn_type_unification (tree fn,
14880 tree explicit_targs,
14881 tree targs,
14882 const tree *args,
14883 unsigned int nargs,
14884 tree return_type,
14885 unification_kind_t strict,
14886 int flags,
14887 bool explain_p)
14888 {
14889 tree parms;
14890 tree fntype;
14891 tree decl = NULL_TREE;
14892 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
14893 bool ok;
14894 static int deduction_depth;
14895 struct pending_template *old_last_pend = last_pending_template;
14896 struct tinst_level *old_error_tinst = last_error_tinst_level;
14897 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14898 tree tinst;
14899 tree r = error_mark_node;
14900
14901 /* Adjust any explicit template arguments before entering the
14902 substitution context. */
14903 if (explicit_targs)
14904 {
14905 explicit_targs
14906 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14907 complain,
14908 /*require_all_args=*/false,
14909 /*use_default_args=*/false));
14910 if (explicit_targs == error_mark_node)
14911 return error_mark_node;
14912 }
14913
14914 /* In C++0x, it's possible to have a function template whose type depends
14915 on itself recursively. This is most obvious with decltype, but can also
14916 occur with enumeration scope (c++/48969). So we need to catch infinite
14917 recursion and reject the substitution at deduction time; this function
14918 will return error_mark_node for any repeated substitution.
14919
14920 This also catches excessive recursion such as when f<N> depends on
14921 f<N-1> across all integers, and returns error_mark_node for all the
14922 substitutions back up to the initial one.
14923
14924 This is, of course, not reentrant. */
14925 if (excessive_deduction_depth)
14926 return error_mark_node;
14927 tinst = build_tree_list (fn, targs);
14928 if (!push_tinst_level (tinst))
14929 {
14930 excessive_deduction_depth = true;
14931 ggc_free (tinst);
14932 return error_mark_node;
14933 }
14934 ++deduction_depth;
14935 push_deferring_access_checks (dk_deferred);
14936
14937 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14938
14939 fntype = TREE_TYPE (fn);
14940 if (explicit_targs)
14941 {
14942 /* [temp.deduct]
14943
14944 The specified template arguments must match the template
14945 parameters in kind (i.e., type, nontype, template), and there
14946 must not be more arguments than there are parameters;
14947 otherwise type deduction fails.
14948
14949 Nontype arguments must match the types of the corresponding
14950 nontype template parameters, or must be convertible to the
14951 types of the corresponding nontype parameters as specified in
14952 _temp.arg.nontype_, otherwise type deduction fails.
14953
14954 All references in the function type of the function template
14955 to the corresponding template parameters are replaced by the
14956 specified template argument values. If a substitution in a
14957 template parameter or in the function type of the function
14958 template results in an invalid type, type deduction fails. */
14959 int i, len = TREE_VEC_LENGTH (tparms);
14960 location_t loc = input_location;
14961 bool incomplete = false;
14962
14963 /* Substitute the explicit args into the function type. This is
14964 necessary so that, for instance, explicitly declared function
14965 arguments can match null pointed constants. If we were given
14966 an incomplete set of explicit args, we must not do semantic
14967 processing during substitution as we could create partial
14968 instantiations. */
14969 for (i = 0; i < len; i++)
14970 {
14971 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14972 bool parameter_pack = false;
14973 tree targ = TREE_VEC_ELT (explicit_targs, i);
14974
14975 /* Dig out the actual parm. */
14976 if (TREE_CODE (parm) == TYPE_DECL
14977 || TREE_CODE (parm) == TEMPLATE_DECL)
14978 {
14979 parm = TREE_TYPE (parm);
14980 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14981 }
14982 else if (TREE_CODE (parm) == PARM_DECL)
14983 {
14984 parm = DECL_INITIAL (parm);
14985 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14986 }
14987
14988 if (!parameter_pack && targ == NULL_TREE)
14989 /* No explicit argument for this template parameter. */
14990 incomplete = true;
14991
14992 if (parameter_pack && pack_deducible_p (parm, fn))
14993 {
14994 /* Mark the argument pack as "incomplete". We could
14995 still deduce more arguments during unification.
14996 We remove this mark in type_unification_real. */
14997 if (targ)
14998 {
14999 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15000 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15001 = ARGUMENT_PACK_ARGS (targ);
15002 }
15003
15004 /* We have some incomplete argument packs. */
15005 incomplete = true;
15006 }
15007 }
15008
15009 processing_template_decl += incomplete;
15010 input_location = DECL_SOURCE_LOCATION (fn);
15011 TREE_VALUE (tinst) = explicit_targs;
15012 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15013 complain | tf_partial, NULL_TREE);
15014 TREE_VALUE (tinst) = targs;
15015 input_location = loc;
15016 processing_template_decl -= incomplete;
15017
15018 if (fntype == error_mark_node)
15019 goto fail;
15020
15021 /* Throw away these access checks; we'll see them again in
15022 instantiate_template and they might have the wrong
15023 access path at this point. */
15024 pop_deferring_access_checks ();
15025 push_deferring_access_checks (dk_deferred);
15026
15027 /* Place the explicitly specified arguments in TARGS. */
15028 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15029 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15030 }
15031
15032 /* Never do unification on the 'this' parameter. */
15033 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15034
15035 if (return_type)
15036 {
15037 tree *new_args;
15038
15039 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15040 new_args = XALLOCAVEC (tree, nargs + 1);
15041 new_args[0] = return_type;
15042 memcpy (new_args + 1, args, nargs * sizeof (tree));
15043 args = new_args;
15044 ++nargs;
15045 }
15046
15047 /* We allow incomplete unification without an error message here
15048 because the standard doesn't seem to explicitly prohibit it. Our
15049 callers must be ready to deal with unification failures in any
15050 event. */
15051
15052 pop_tinst_level ();
15053 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15054 targs, parms, args, nargs, /*subr=*/0,
15055 strict, flags, explain_p);
15056 push_tinst_level (tinst);
15057 if (!ok)
15058 goto fail;
15059
15060 /* Now that we have bindings for all of the template arguments,
15061 ensure that the arguments deduced for the template template
15062 parameters have compatible template parameter lists. We cannot
15063 check this property before we have deduced all template
15064 arguments, because the template parameter types of a template
15065 template parameter might depend on prior template parameters
15066 deduced after the template template parameter. The following
15067 ill-formed example illustrates this issue:
15068
15069 template<typename T, template<T> class C> void f(C<5>, T);
15070
15071 template<int N> struct X {};
15072
15073 void g() {
15074 f(X<5>(), 5l); // error: template argument deduction fails
15075 }
15076
15077 The template parameter list of 'C' depends on the template type
15078 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15079 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15080 time that we deduce 'C'. */
15081 if (!template_template_parm_bindings_ok_p
15082 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15083 {
15084 unify_inconsistent_template_template_parameters (explain_p);
15085 goto fail;
15086 }
15087
15088 /* All is well so far. Now, check:
15089
15090 [temp.deduct]
15091
15092 When all template arguments have been deduced, all uses of
15093 template parameters in nondeduced contexts are replaced with
15094 the corresponding deduced argument values. If the
15095 substitution results in an invalid type, as described above,
15096 type deduction fails. */
15097 decl = instantiate_template (fn, targs, complain);
15098 if (decl == error_mark_node)
15099 goto fail;
15100
15101 /* Now perform any access checks encountered during deduction, such as
15102 for default template arguments. */
15103 push_access_scope (decl);
15104 ok = perform_deferred_access_checks (complain);
15105 pop_access_scope (decl);
15106 if (!ok)
15107 goto fail;
15108
15109 /* If we're looking for an exact match, check that what we got
15110 is indeed an exact match. It might not be if some template
15111 parameters are used in non-deduced contexts. */
15112 if (strict == DEDUCE_EXACT)
15113 {
15114 tree substed = TREE_TYPE (decl);
15115 unsigned int i;
15116
15117 tree sarg
15118 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15119 if (return_type)
15120 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15121 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15122 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15123 {
15124 unify_type_mismatch (explain_p, args[i],
15125 TREE_VALUE (sarg));
15126 goto fail;
15127 }
15128 }
15129
15130 r = decl;
15131
15132 fail:
15133 pop_deferring_access_checks ();
15134 --deduction_depth;
15135 if (excessive_deduction_depth)
15136 {
15137 if (deduction_depth == 0)
15138 /* Reset once we're all the way out. */
15139 excessive_deduction_depth = false;
15140 }
15141
15142 pop_tinst_level ();
15143 /* We can't free this if a pending_template entry or last_error_tinst_level
15144 is pointing at it. */
15145 if (last_pending_template == old_last_pend
15146 && last_error_tinst_level == old_error_tinst)
15147 ggc_free (tinst);
15148
15149 return r;
15150 }
15151
15152 /* Adjust types before performing type deduction, as described in
15153 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15154 sections are symmetric. PARM is the type of a function parameter
15155 or the return type of the conversion function. ARG is the type of
15156 the argument passed to the call, or the type of the value
15157 initialized with the result of the conversion function.
15158 ARG_EXPR is the original argument expression, which may be null. */
15159
15160 static int
15161 maybe_adjust_types_for_deduction (unification_kind_t strict,
15162 tree* parm,
15163 tree* arg,
15164 tree arg_expr)
15165 {
15166 int result = 0;
15167
15168 switch (strict)
15169 {
15170 case DEDUCE_CALL:
15171 break;
15172
15173 case DEDUCE_CONV:
15174 {
15175 /* Swap PARM and ARG throughout the remainder of this
15176 function; the handling is precisely symmetric since PARM
15177 will initialize ARG rather than vice versa. */
15178 tree* temp = parm;
15179 parm = arg;
15180 arg = temp;
15181 break;
15182 }
15183
15184 case DEDUCE_EXACT:
15185 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15186 too, but here handle it by stripping the reference from PARM
15187 rather than by adding it to ARG. */
15188 if (TREE_CODE (*parm) == REFERENCE_TYPE
15189 && TYPE_REF_IS_RVALUE (*parm)
15190 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15191 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15192 && TREE_CODE (*arg) == REFERENCE_TYPE
15193 && !TYPE_REF_IS_RVALUE (*arg))
15194 *parm = TREE_TYPE (*parm);
15195 /* Nothing else to do in this case. */
15196 return 0;
15197
15198 default:
15199 gcc_unreachable ();
15200 }
15201
15202 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15203 {
15204 /* [temp.deduct.call]
15205
15206 If P is not a reference type:
15207
15208 --If A is an array type, the pointer type produced by the
15209 array-to-pointer standard conversion (_conv.array_) is
15210 used in place of A for type deduction; otherwise,
15211
15212 --If A is a function type, the pointer type produced by
15213 the function-to-pointer standard conversion
15214 (_conv.func_) is used in place of A for type deduction;
15215 otherwise,
15216
15217 --If A is a cv-qualified type, the top level
15218 cv-qualifiers of A's type are ignored for type
15219 deduction. */
15220 if (TREE_CODE (*arg) == ARRAY_TYPE)
15221 *arg = build_pointer_type (TREE_TYPE (*arg));
15222 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15223 *arg = build_pointer_type (*arg);
15224 else
15225 *arg = TYPE_MAIN_VARIANT (*arg);
15226 }
15227
15228 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15229 of the form T&&, where T is a template parameter, and the argument
15230 is an lvalue, T is deduced as A& */
15231 if (TREE_CODE (*parm) == REFERENCE_TYPE
15232 && TYPE_REF_IS_RVALUE (*parm)
15233 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15234 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15235 && (arg_expr ? real_lvalue_p (arg_expr)
15236 /* try_one_overload doesn't provide an arg_expr, but
15237 functions are always lvalues. */
15238 : TREE_CODE (*arg) == FUNCTION_TYPE))
15239 *arg = build_reference_type (*arg);
15240
15241 /* [temp.deduct.call]
15242
15243 If P is a cv-qualified type, the top level cv-qualifiers
15244 of P's type are ignored for type deduction. If P is a
15245 reference type, the type referred to by P is used for
15246 type deduction. */
15247 *parm = TYPE_MAIN_VARIANT (*parm);
15248 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15249 {
15250 *parm = TREE_TYPE (*parm);
15251 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15252 }
15253
15254 /* DR 322. For conversion deduction, remove a reference type on parm
15255 too (which has been swapped into ARG). */
15256 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15257 *arg = TREE_TYPE (*arg);
15258
15259 return result;
15260 }
15261
15262 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15263 template which does contain any deducible template parameters; check if
15264 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15265 unify_one_argument. */
15266
15267 static int
15268 check_non_deducible_conversion (tree parm, tree arg, int strict,
15269 int flags, bool explain_p)
15270 {
15271 tree type;
15272
15273 if (!TYPE_P (arg))
15274 type = TREE_TYPE (arg);
15275 else
15276 type = arg;
15277
15278 if (same_type_p (parm, type))
15279 return unify_success (explain_p);
15280
15281 if (strict == DEDUCE_CONV)
15282 {
15283 if (can_convert_arg (type, parm, NULL_TREE, flags,
15284 explain_p ? tf_warning_or_error : tf_none))
15285 return unify_success (explain_p);
15286 }
15287 else if (strict != DEDUCE_EXACT)
15288 {
15289 if (can_convert_arg (parm, type,
15290 TYPE_P (arg) ? NULL_TREE : arg,
15291 flags, explain_p ? tf_warning_or_error : tf_none))
15292 return unify_success (explain_p);
15293 }
15294
15295 if (strict == DEDUCE_EXACT)
15296 return unify_type_mismatch (explain_p, parm, arg);
15297 else
15298 return unify_arg_conversion (explain_p, parm, type, arg);
15299 }
15300
15301 /* Subroutine of type_unification_real and unify_pack_expansion to
15302 handle unification of a single P/A pair. Parameters are as
15303 for those functions. */
15304
15305 static int
15306 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
15307 int subr, unification_kind_t strict, int flags,
15308 bool explain_p)
15309 {
15310 tree arg_expr = NULL_TREE;
15311 int arg_strict;
15312
15313 if (arg == error_mark_node || parm == error_mark_node)
15314 return unify_invalid (explain_p);
15315 if (arg == unknown_type_node)
15316 /* We can't deduce anything from this, but we might get all the
15317 template args from other function args. */
15318 return unify_success (explain_p);
15319
15320 /* FIXME uses_deducible_template_parms */
15321 if (TYPE_P (parm) && !uses_template_parms (parm))
15322 return check_non_deducible_conversion (parm, arg, strict, flags,
15323 explain_p);
15324
15325 switch (strict)
15326 {
15327 case DEDUCE_CALL:
15328 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
15329 | UNIFY_ALLOW_MORE_CV_QUAL
15330 | UNIFY_ALLOW_DERIVED);
15331 break;
15332
15333 case DEDUCE_CONV:
15334 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15335 break;
15336
15337 case DEDUCE_EXACT:
15338 arg_strict = UNIFY_ALLOW_NONE;
15339 break;
15340
15341 default:
15342 gcc_unreachable ();
15343 }
15344
15345 /* We only do these transformations if this is the top-level
15346 parameter_type_list in a call or declaration matching; in other
15347 situations (nested function declarators, template argument lists) we
15348 won't be comparing a type to an expression, and we don't do any type
15349 adjustments. */
15350 if (!subr)
15351 {
15352 if (!TYPE_P (arg))
15353 {
15354 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15355 if (type_unknown_p (arg))
15356 {
15357 /* [temp.deduct.type] A template-argument can be
15358 deduced from a pointer to function or pointer
15359 to member function argument if the set of
15360 overloaded functions does not contain function
15361 templates and at most one of a set of
15362 overloaded functions provides a unique
15363 match. */
15364
15365 if (resolve_overloaded_unification
15366 (tparms, targs, parm, arg, strict,
15367 arg_strict, explain_p))
15368 return unify_success (explain_p);
15369 return unify_overload_resolution_failure (explain_p, arg);
15370 }
15371
15372 arg_expr = arg;
15373 arg = unlowered_expr_type (arg);
15374 if (arg == error_mark_node)
15375 return unify_invalid (explain_p);
15376 }
15377
15378 arg_strict |=
15379 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
15380 }
15381 else
15382 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
15383 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
15384
15385 /* For deduction from an init-list we need the actual list. */
15386 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15387 arg = arg_expr;
15388 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
15389 }
15390
15391 /* Most parms like fn_type_unification.
15392
15393 If SUBR is 1, we're being called recursively (to unify the
15394 arguments of a function or method parameter of a function
15395 template). */
15396
15397 static int
15398 type_unification_real (tree tparms,
15399 tree targs,
15400 tree xparms,
15401 const tree *xargs,
15402 unsigned int xnargs,
15403 int subr,
15404 unification_kind_t strict,
15405 int flags,
15406 bool explain_p)
15407 {
15408 tree parm, arg;
15409 int i;
15410 int ntparms = TREE_VEC_LENGTH (tparms);
15411 int saw_undeduced = 0;
15412 tree parms;
15413 const tree *args;
15414 unsigned int nargs;
15415 unsigned int ia;
15416
15417 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15418 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15419 gcc_assert (ntparms > 0);
15420
15421 /* Reset the number of non-defaulted template arguments contained
15422 in TARGS. */
15423 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15424
15425 again:
15426 parms = xparms;
15427 args = xargs;
15428 nargs = xnargs;
15429
15430 ia = 0;
15431 while (parms && parms != void_list_node
15432 && ia < nargs)
15433 {
15434 parm = TREE_VALUE (parms);
15435
15436 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15437 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15438 /* For a function parameter pack that occurs at the end of the
15439 parameter-declaration-list, the type A of each remaining
15440 argument of the call is compared with the type P of the
15441 declarator-id of the function parameter pack. */
15442 break;
15443
15444 parms = TREE_CHAIN (parms);
15445
15446 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15447 /* For a function parameter pack that does not occur at the
15448 end of the parameter-declaration-list, the type of the
15449 parameter pack is a non-deduced context. */
15450 continue;
15451
15452 arg = args[ia];
15453 ++ia;
15454
15455 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15456 flags, explain_p))
15457 return 1;
15458 }
15459
15460 if (parms
15461 && parms != void_list_node
15462 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15463 {
15464 /* Unify the remaining arguments with the pack expansion type. */
15465 tree argvec;
15466 tree parmvec = make_tree_vec (1);
15467
15468 /* Allocate a TREE_VEC and copy in all of the arguments */
15469 argvec = make_tree_vec (nargs - ia);
15470 for (i = 0; ia < nargs; ++ia, ++i)
15471 TREE_VEC_ELT (argvec, i) = args[ia];
15472
15473 /* Copy the parameter into parmvec. */
15474 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15475 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15476 /*subr=*/subr, explain_p))
15477 return 1;
15478
15479 /* Advance to the end of the list of parameters. */
15480 parms = TREE_CHAIN (parms);
15481 }
15482
15483 /* Fail if we've reached the end of the parm list, and more args
15484 are present, and the parm list isn't variadic. */
15485 if (ia < nargs && parms == void_list_node)
15486 return unify_too_many_arguments (explain_p, nargs, ia);
15487 /* Fail if parms are left and they don't have default values. */
15488 if (parms && parms != void_list_node
15489 && TREE_PURPOSE (parms) == NULL_TREE)
15490 {
15491 unsigned int count = nargs;
15492 tree p = parms;
15493 while (p && p != void_list_node)
15494 {
15495 count++;
15496 p = TREE_CHAIN (p);
15497 }
15498 return unify_too_few_arguments (explain_p, ia, count);
15499 }
15500
15501 if (!subr)
15502 {
15503 tsubst_flags_t complain = (explain_p
15504 ? tf_warning_or_error
15505 : tf_none);
15506
15507 for (i = 0; i < ntparms; i++)
15508 {
15509 tree targ = TREE_VEC_ELT (targs, i);
15510 tree tparm = TREE_VEC_ELT (tparms, i);
15511
15512 /* Clear the "incomplete" flags on all argument packs now so that
15513 substituting them into later default arguments works. */
15514 if (targ && ARGUMENT_PACK_P (targ))
15515 {
15516 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15517 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15518 }
15519
15520 if (targ || tparm == error_mark_node)
15521 continue;
15522 tparm = TREE_VALUE (tparm);
15523
15524 /* If this is an undeduced nontype parameter that depends on
15525 a type parameter, try another pass; its type may have been
15526 deduced from a later argument than the one from which
15527 this parameter can be deduced. */
15528 if (TREE_CODE (tparm) == PARM_DECL
15529 && uses_template_parms (TREE_TYPE (tparm))
15530 && !saw_undeduced++)
15531 goto again;
15532
15533 /* Core issue #226 (C++0x) [temp.deduct]:
15534
15535 If a template argument has not been deduced, its
15536 default template argument, if any, is used.
15537
15538 When we are in C++98 mode, TREE_PURPOSE will either
15539 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15540 to explicitly check cxx_dialect here. */
15541 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15542 {
15543 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15544 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15545 location_t save_loc = input_location;
15546 if (DECL_P (parm))
15547 input_location = DECL_SOURCE_LOCATION (parm);
15548 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15549 arg = convert_template_argument (parm, arg, targs, complain,
15550 i, NULL_TREE);
15551 input_location = save_loc;
15552 if (arg == error_mark_node)
15553 return 1;
15554 else
15555 {
15556 TREE_VEC_ELT (targs, i) = arg;
15557 /* The position of the first default template argument,
15558 is also the number of non-defaulted arguments in TARGS.
15559 Record that. */
15560 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15561 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15562 continue;
15563 }
15564 }
15565
15566 /* If the type parameter is a parameter pack, then it will
15567 be deduced to an empty parameter pack. */
15568 if (template_parameter_pack_p (tparm))
15569 {
15570 tree arg;
15571
15572 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15573 {
15574 arg = make_node (NONTYPE_ARGUMENT_PACK);
15575 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15576 TREE_CONSTANT (arg) = 1;
15577 }
15578 else
15579 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15580
15581 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15582
15583 TREE_VEC_ELT (targs, i) = arg;
15584 continue;
15585 }
15586
15587 return unify_parameter_deduction_failure (explain_p, tparm);
15588 }
15589 }
15590 #ifdef ENABLE_CHECKING
15591 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15592 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15593 #endif
15594
15595 return unify_success (explain_p);
15596 }
15597
15598 /* Subroutine of type_unification_real. Args are like the variables
15599 at the call site. ARG is an overloaded function (or template-id);
15600 we try deducing template args from each of the overloads, and if
15601 only one succeeds, we go with that. Modifies TARGS and returns
15602 true on success. */
15603
15604 static bool
15605 resolve_overloaded_unification (tree tparms,
15606 tree targs,
15607 tree parm,
15608 tree arg,
15609 unification_kind_t strict,
15610 int sub_strict,
15611 bool explain_p)
15612 {
15613 tree tempargs = copy_node (targs);
15614 int good = 0;
15615 tree goodfn = NULL_TREE;
15616 bool addr_p;
15617
15618 if (TREE_CODE (arg) == ADDR_EXPR)
15619 {
15620 arg = TREE_OPERAND (arg, 0);
15621 addr_p = true;
15622 }
15623 else
15624 addr_p = false;
15625
15626 if (TREE_CODE (arg) == COMPONENT_REF)
15627 /* Handle `&x' where `x' is some static or non-static member
15628 function name. */
15629 arg = TREE_OPERAND (arg, 1);
15630
15631 if (TREE_CODE (arg) == OFFSET_REF)
15632 arg = TREE_OPERAND (arg, 1);
15633
15634 /* Strip baselink information. */
15635 if (BASELINK_P (arg))
15636 arg = BASELINK_FUNCTIONS (arg);
15637
15638 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15639 {
15640 /* If we got some explicit template args, we need to plug them into
15641 the affected templates before we try to unify, in case the
15642 explicit args will completely resolve the templates in question. */
15643
15644 int ok = 0;
15645 tree expl_subargs = TREE_OPERAND (arg, 1);
15646 arg = TREE_OPERAND (arg, 0);
15647
15648 for (; arg; arg = OVL_NEXT (arg))
15649 {
15650 tree fn = OVL_CURRENT (arg);
15651 tree subargs, elem;
15652
15653 if (TREE_CODE (fn) != TEMPLATE_DECL)
15654 continue;
15655
15656 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15657 expl_subargs, NULL_TREE, tf_none,
15658 /*require_all_args=*/true,
15659 /*use_default_args=*/true);
15660 if (subargs != error_mark_node
15661 && !any_dependent_template_arguments_p (subargs))
15662 {
15663 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15664 if (try_one_overload (tparms, targs, tempargs, parm,
15665 elem, strict, sub_strict, addr_p, explain_p)
15666 && (!goodfn || !same_type_p (goodfn, elem)))
15667 {
15668 goodfn = elem;
15669 ++good;
15670 }
15671 }
15672 else if (subargs)
15673 ++ok;
15674 }
15675 /* If no templates (or more than one) are fully resolved by the
15676 explicit arguments, this template-id is a non-deduced context; it
15677 could still be OK if we deduce all template arguments for the
15678 enclosing call through other arguments. */
15679 if (good != 1)
15680 good = ok;
15681 }
15682 else if (TREE_CODE (arg) != OVERLOAD
15683 && TREE_CODE (arg) != FUNCTION_DECL)
15684 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15685 -- but the deduction does not succeed because the expression is
15686 not just the function on its own. */
15687 return false;
15688 else
15689 for (; arg; arg = OVL_NEXT (arg))
15690 if (try_one_overload (tparms, targs, tempargs, parm,
15691 TREE_TYPE (OVL_CURRENT (arg)),
15692 strict, sub_strict, addr_p, explain_p)
15693 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15694 {
15695 goodfn = OVL_CURRENT (arg);
15696 ++good;
15697 }
15698
15699 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15700 to function or pointer to member function argument if the set of
15701 overloaded functions does not contain function templates and at most
15702 one of a set of overloaded functions provides a unique match.
15703
15704 So if we found multiple possibilities, we return success but don't
15705 deduce anything. */
15706
15707 if (good == 1)
15708 {
15709 int i = TREE_VEC_LENGTH (targs);
15710 for (; i--; )
15711 if (TREE_VEC_ELT (tempargs, i))
15712 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15713 }
15714 if (good)
15715 return true;
15716
15717 return false;
15718 }
15719
15720 /* Core DR 115: In contexts where deduction is done and fails, or in
15721 contexts where deduction is not done, if a template argument list is
15722 specified and it, along with any default template arguments, identifies
15723 a single function template specialization, then the template-id is an
15724 lvalue for the function template specialization. */
15725
15726 tree
15727 resolve_nondeduced_context (tree orig_expr)
15728 {
15729 tree expr, offset, baselink;
15730 bool addr;
15731
15732 if (!type_unknown_p (orig_expr))
15733 return orig_expr;
15734
15735 expr = orig_expr;
15736 addr = false;
15737 offset = NULL_TREE;
15738 baselink = NULL_TREE;
15739
15740 if (TREE_CODE (expr) == ADDR_EXPR)
15741 {
15742 expr = TREE_OPERAND (expr, 0);
15743 addr = true;
15744 }
15745 if (TREE_CODE (expr) == OFFSET_REF)
15746 {
15747 offset = expr;
15748 expr = TREE_OPERAND (expr, 1);
15749 }
15750 if (BASELINK_P (expr))
15751 {
15752 baselink = expr;
15753 expr = BASELINK_FUNCTIONS (expr);
15754 }
15755
15756 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15757 {
15758 int good = 0;
15759 tree goodfn = NULL_TREE;
15760
15761 /* If we got some explicit template args, we need to plug them into
15762 the affected templates before we try to unify, in case the
15763 explicit args will completely resolve the templates in question. */
15764
15765 tree expl_subargs = TREE_OPERAND (expr, 1);
15766 tree arg = TREE_OPERAND (expr, 0);
15767 tree badfn = NULL_TREE;
15768 tree badargs = NULL_TREE;
15769
15770 for (; arg; arg = OVL_NEXT (arg))
15771 {
15772 tree fn = OVL_CURRENT (arg);
15773 tree subargs, elem;
15774
15775 if (TREE_CODE (fn) != TEMPLATE_DECL)
15776 continue;
15777
15778 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15779 expl_subargs, NULL_TREE, tf_none,
15780 /*require_all_args=*/true,
15781 /*use_default_args=*/true);
15782 if (subargs != error_mark_node
15783 && !any_dependent_template_arguments_p (subargs))
15784 {
15785 elem = instantiate_template (fn, subargs, tf_none);
15786 if (elem == error_mark_node)
15787 {
15788 badfn = fn;
15789 badargs = subargs;
15790 }
15791 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15792 {
15793 goodfn = elem;
15794 ++good;
15795 }
15796 }
15797 }
15798 if (good == 1)
15799 {
15800 mark_used (goodfn);
15801 expr = goodfn;
15802 if (baselink)
15803 expr = build_baselink (BASELINK_BINFO (baselink),
15804 BASELINK_ACCESS_BINFO (baselink),
15805 expr, BASELINK_OPTYPE (baselink));
15806 if (offset)
15807 {
15808 tree base
15809 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15810 expr = build_offset_ref (base, expr, addr);
15811 }
15812 if (addr)
15813 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15814 return expr;
15815 }
15816 else if (good == 0 && badargs)
15817 /* There were no good options and at least one bad one, so let the
15818 user know what the problem is. */
15819 instantiate_template (badfn, badargs, tf_warning_or_error);
15820 }
15821 return orig_expr;
15822 }
15823
15824 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15825 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15826 different overloads deduce different arguments for a given parm.
15827 ADDR_P is true if the expression for which deduction is being
15828 performed was of the form "& fn" rather than simply "fn".
15829
15830 Returns 1 on success. */
15831
15832 static int
15833 try_one_overload (tree tparms,
15834 tree orig_targs,
15835 tree targs,
15836 tree parm,
15837 tree arg,
15838 unification_kind_t strict,
15839 int sub_strict,
15840 bool addr_p,
15841 bool explain_p)
15842 {
15843 int nargs;
15844 tree tempargs;
15845 int i;
15846
15847 if (arg == error_mark_node)
15848 return 0;
15849
15850 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15851 to function or pointer to member function argument if the set of
15852 overloaded functions does not contain function templates and at most
15853 one of a set of overloaded functions provides a unique match.
15854
15855 So if this is a template, just return success. */
15856
15857 if (uses_template_parms (arg))
15858 return 1;
15859
15860 if (TREE_CODE (arg) == METHOD_TYPE)
15861 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15862 else if (addr_p)
15863 arg = build_pointer_type (arg);
15864
15865 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15866
15867 /* We don't copy orig_targs for this because if we have already deduced
15868 some template args from previous args, unify would complain when we
15869 try to deduce a template parameter for the same argument, even though
15870 there isn't really a conflict. */
15871 nargs = TREE_VEC_LENGTH (targs);
15872 tempargs = make_tree_vec (nargs);
15873
15874 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15875 return 0;
15876
15877 /* First make sure we didn't deduce anything that conflicts with
15878 explicitly specified args. */
15879 for (i = nargs; i--; )
15880 {
15881 tree elt = TREE_VEC_ELT (tempargs, i);
15882 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15883
15884 if (!elt)
15885 /*NOP*/;
15886 else if (uses_template_parms (elt))
15887 /* Since we're unifying against ourselves, we will fill in
15888 template args used in the function parm list with our own
15889 template parms. Discard them. */
15890 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15891 else if (oldelt && !template_args_equal (oldelt, elt))
15892 return 0;
15893 }
15894
15895 for (i = nargs; i--; )
15896 {
15897 tree elt = TREE_VEC_ELT (tempargs, i);
15898
15899 if (elt)
15900 TREE_VEC_ELT (targs, i) = elt;
15901 }
15902
15903 return 1;
15904 }
15905
15906 /* PARM is a template class (perhaps with unbound template
15907 parameters). ARG is a fully instantiated type. If ARG can be
15908 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15909 TARGS are as for unify. */
15910
15911 static tree
15912 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15913 bool explain_p)
15914 {
15915 tree copy_of_targs;
15916
15917 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15918 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15919 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15920 return NULL_TREE;
15921
15922 /* We need to make a new template argument vector for the call to
15923 unify. If we used TARGS, we'd clutter it up with the result of
15924 the attempted unification, even if this class didn't work out.
15925 We also don't want to commit ourselves to all the unifications
15926 we've already done, since unification is supposed to be done on
15927 an argument-by-argument basis. In other words, consider the
15928 following pathological case:
15929
15930 template <int I, int J, int K>
15931 struct S {};
15932
15933 template <int I, int J>
15934 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15935
15936 template <int I, int J, int K>
15937 void f(S<I, J, K>, S<I, I, I>);
15938
15939 void g() {
15940 S<0, 0, 0> s0;
15941 S<0, 1, 2> s2;
15942
15943 f(s0, s2);
15944 }
15945
15946 Now, by the time we consider the unification involving `s2', we
15947 already know that we must have `f<0, 0, 0>'. But, even though
15948 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15949 because there are two ways to unify base classes of S<0, 1, 2>
15950 with S<I, I, I>. If we kept the already deduced knowledge, we
15951 would reject the possibility I=1. */
15952 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15953
15954 /* If unification failed, we're done. */
15955 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15956 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15957 return NULL_TREE;
15958
15959 return arg;
15960 }
15961
15962 /* Given a template type PARM and a class type ARG, find the unique
15963 base type in ARG that is an instance of PARM. We do not examine
15964 ARG itself; only its base-classes. If there is not exactly one
15965 appropriate base class, return NULL_TREE. PARM may be the type of
15966 a partial specialization, as well as a plain template type. Used
15967 by unify. */
15968
15969 static enum template_base_result
15970 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15971 bool explain_p, tree *result)
15972 {
15973 tree rval = NULL_TREE;
15974 tree binfo;
15975
15976 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15977
15978 binfo = TYPE_BINFO (complete_type (arg));
15979 if (!binfo)
15980 {
15981 /* The type could not be completed. */
15982 *result = NULL_TREE;
15983 return tbr_incomplete_type;
15984 }
15985
15986 /* Walk in inheritance graph order. The search order is not
15987 important, and this avoids multiple walks of virtual bases. */
15988 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15989 {
15990 tree r = try_class_unification (tparms, targs, parm,
15991 BINFO_TYPE (binfo), explain_p);
15992
15993 if (r)
15994 {
15995 /* If there is more than one satisfactory baseclass, then:
15996
15997 [temp.deduct.call]
15998
15999 If they yield more than one possible deduced A, the type
16000 deduction fails.
16001
16002 applies. */
16003 if (rval && !same_type_p (r, rval))
16004 {
16005 *result = NULL_TREE;
16006 return tbr_ambiguous_baseclass;
16007 }
16008
16009 rval = r;
16010 }
16011 }
16012
16013 *result = rval;
16014 return tbr_success;
16015 }
16016
16017 /* Returns the level of DECL, which declares a template parameter. */
16018
16019 static int
16020 template_decl_level (tree decl)
16021 {
16022 switch (TREE_CODE (decl))
16023 {
16024 case TYPE_DECL:
16025 case TEMPLATE_DECL:
16026 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16027
16028 case PARM_DECL:
16029 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16030
16031 default:
16032 gcc_unreachable ();
16033 }
16034 return 0;
16035 }
16036
16037 /* Decide whether ARG can be unified with PARM, considering only the
16038 cv-qualifiers of each type, given STRICT as documented for unify.
16039 Returns nonzero iff the unification is OK on that basis. */
16040
16041 static int
16042 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16043 {
16044 int arg_quals = cp_type_quals (arg);
16045 int parm_quals = cp_type_quals (parm);
16046
16047 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16048 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16049 {
16050 /* Although a CVR qualifier is ignored when being applied to a
16051 substituted template parameter ([8.3.2]/1 for example), that
16052 does not allow us to unify "const T" with "int&" because both
16053 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16054 It is ok when we're allowing additional CV qualifiers
16055 at the outer level [14.8.2.1]/3,1st bullet. */
16056 if ((TREE_CODE (arg) == REFERENCE_TYPE
16057 || TREE_CODE (arg) == FUNCTION_TYPE
16058 || TREE_CODE (arg) == METHOD_TYPE)
16059 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16060 return 0;
16061
16062 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16063 && (parm_quals & TYPE_QUAL_RESTRICT))
16064 return 0;
16065 }
16066
16067 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16068 && (arg_quals & parm_quals) != parm_quals)
16069 return 0;
16070
16071 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16072 && (parm_quals & arg_quals) != arg_quals)
16073 return 0;
16074
16075 return 1;
16076 }
16077
16078 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16079 void
16080 template_parm_level_and_index (tree parm, int* level, int* index)
16081 {
16082 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16083 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16084 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16085 {
16086 *index = TEMPLATE_TYPE_IDX (parm);
16087 *level = TEMPLATE_TYPE_LEVEL (parm);
16088 }
16089 else
16090 {
16091 *index = TEMPLATE_PARM_IDX (parm);
16092 *level = TEMPLATE_PARM_LEVEL (parm);
16093 }
16094 }
16095
16096 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16097 do { \
16098 if (unify (TP, TA, P, A, S, EP)) \
16099 return 1; \
16100 } while (0);
16101
16102 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16103 expansion at the end of PACKED_PARMS. Returns 0 if the type
16104 deduction succeeds, 1 otherwise. STRICT is the same as in
16105 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16106 call argument list. We'll need to adjust the arguments to make them
16107 types. SUBR tells us if this is from a recursive call to
16108 type_unification_real, or for comparing two template argument
16109 lists. */
16110
16111 static int
16112 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16113 tree packed_args, unification_kind_t strict,
16114 bool subr, bool explain_p)
16115 {
16116 tree parm
16117 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16118 tree pattern = PACK_EXPANSION_PATTERN (parm);
16119 tree pack, packs = NULL_TREE;
16120 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16121 int len = TREE_VEC_LENGTH (packed_args);
16122
16123 /* Determine the parameter packs we will be deducing from the
16124 pattern, and record their current deductions. */
16125 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16126 pack; pack = TREE_CHAIN (pack))
16127 {
16128 tree parm_pack = TREE_VALUE (pack);
16129 int idx, level;
16130
16131 /* Determine the index and level of this parameter pack. */
16132 template_parm_level_and_index (parm_pack, &level, &idx);
16133
16134 /* Keep track of the parameter packs and their corresponding
16135 argument packs. */
16136 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16137 TREE_TYPE (packs) = make_tree_vec (len - start);
16138 }
16139
16140 /* Loop through all of the arguments that have not yet been
16141 unified and unify each with the pattern. */
16142 for (i = start; i < len; i++)
16143 {
16144 tree parm;
16145 bool any_explicit = false;
16146 tree arg = TREE_VEC_ELT (packed_args, i);
16147
16148 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16149 or the element of its argument pack at the current index if
16150 this argument was explicitly specified. */
16151 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16152 {
16153 int idx, level;
16154 tree arg, pargs;
16155 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16156
16157 arg = NULL_TREE;
16158 if (TREE_VALUE (pack)
16159 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16160 && (i < TREE_VEC_LENGTH (pargs)))
16161 {
16162 any_explicit = true;
16163 arg = TREE_VEC_ELT (pargs, i);
16164 }
16165 TMPL_ARG (targs, level, idx) = arg;
16166 }
16167
16168 /* If we had explicit template arguments, substitute them into the
16169 pattern before deduction. */
16170 if (any_explicit)
16171 {
16172 /* Some arguments might still be unspecified or dependent. */
16173 bool dependent;
16174 ++processing_template_decl;
16175 dependent = any_dependent_template_arguments_p (targs);
16176 if (!dependent)
16177 --processing_template_decl;
16178 parm = tsubst (pattern, targs,
16179 explain_p ? tf_warning_or_error : tf_none,
16180 NULL_TREE);
16181 if (dependent)
16182 --processing_template_decl;
16183 if (parm == error_mark_node)
16184 return 1;
16185 }
16186 else
16187 parm = pattern;
16188
16189 /* Unify the pattern with the current argument. */
16190 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16191 LOOKUP_IMPLICIT, explain_p))
16192 return 1;
16193
16194 /* For each parameter pack, collect the deduced value. */
16195 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16196 {
16197 int idx, level;
16198 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16199
16200 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16201 TMPL_ARG (targs, level, idx);
16202 }
16203 }
16204
16205 /* Verify that the results of unification with the parameter packs
16206 produce results consistent with what we've seen before, and make
16207 the deduced argument packs available. */
16208 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16209 {
16210 tree old_pack = TREE_VALUE (pack);
16211 tree new_args = TREE_TYPE (pack);
16212 int i, len = TREE_VEC_LENGTH (new_args);
16213 int idx, level;
16214 bool nondeduced_p = false;
16215
16216 /* By default keep the original deduced argument pack.
16217 If necessary, more specific code is going to update the
16218 resulting deduced argument later down in this function. */
16219 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16220 TMPL_ARG (targs, level, idx) = old_pack;
16221
16222 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16223 actually deduce anything. */
16224 for (i = 0; i < len && !nondeduced_p; ++i)
16225 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16226 nondeduced_p = true;
16227 if (nondeduced_p)
16228 continue;
16229
16230 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16231 {
16232 /* If we had fewer function args than explicit template args,
16233 just use the explicits. */
16234 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16235 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16236 if (len < explicit_len)
16237 new_args = explicit_args;
16238 }
16239
16240 if (!old_pack)
16241 {
16242 tree result;
16243 /* Build the deduced *_ARGUMENT_PACK. */
16244 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16245 {
16246 result = make_node (NONTYPE_ARGUMENT_PACK);
16247 TREE_TYPE (result) =
16248 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16249 TREE_CONSTANT (result) = 1;
16250 }
16251 else
16252 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16253
16254 SET_ARGUMENT_PACK_ARGS (result, new_args);
16255
16256 /* Note the deduced argument packs for this parameter
16257 pack. */
16258 TMPL_ARG (targs, level, idx) = result;
16259 }
16260 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16261 && (ARGUMENT_PACK_ARGS (old_pack)
16262 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16263 {
16264 /* We only had the explicitly-provided arguments before, but
16265 now we have a complete set of arguments. */
16266 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16267
16268 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16269 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16270 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16271 }
16272 else
16273 {
16274 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16275 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16276
16277 if (!comp_template_args_with_info (old_args, new_args,
16278 &bad_old_arg, &bad_new_arg))
16279 /* Inconsistent unification of this parameter pack. */
16280 return unify_parameter_pack_inconsistent (explain_p,
16281 bad_old_arg,
16282 bad_new_arg);
16283 }
16284 }
16285
16286 return unify_success (explain_p);
16287 }
16288
16289 /* Deduce the value of template parameters. TPARMS is the (innermost)
16290 set of template parameters to a template. TARGS is the bindings
16291 for those template parameters, as determined thus far; TARGS may
16292 include template arguments for outer levels of template parameters
16293 as well. PARM is a parameter to a template function, or a
16294 subcomponent of that parameter; ARG is the corresponding argument.
16295 This function attempts to match PARM with ARG in a manner
16296 consistent with the existing assignments in TARGS. If more values
16297 are deduced, then TARGS is updated.
16298
16299 Returns 0 if the type deduction succeeds, 1 otherwise. The
16300 parameter STRICT is a bitwise or of the following flags:
16301
16302 UNIFY_ALLOW_NONE:
16303 Require an exact match between PARM and ARG.
16304 UNIFY_ALLOW_MORE_CV_QUAL:
16305 Allow the deduced ARG to be more cv-qualified (by qualification
16306 conversion) than ARG.
16307 UNIFY_ALLOW_LESS_CV_QUAL:
16308 Allow the deduced ARG to be less cv-qualified than ARG.
16309 UNIFY_ALLOW_DERIVED:
16310 Allow the deduced ARG to be a template base class of ARG,
16311 or a pointer to a template base class of the type pointed to by
16312 ARG.
16313 UNIFY_ALLOW_INTEGER:
16314 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16315 case for more information.
16316 UNIFY_ALLOW_OUTER_LEVEL:
16317 This is the outermost level of a deduction. Used to determine validity
16318 of qualification conversions. A valid qualification conversion must
16319 have const qualified pointers leading up to the inner type which
16320 requires additional CV quals, except at the outer level, where const
16321 is not required [conv.qual]. It would be normal to set this flag in
16322 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16323 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16324 This is the outermost level of a deduction, and PARM can be more CV
16325 qualified at this point.
16326 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16327 This is the outermost level of a deduction, and PARM can be less CV
16328 qualified at this point. */
16329
16330 static int
16331 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
16332 bool explain_p)
16333 {
16334 int idx;
16335 tree targ;
16336 tree tparm;
16337 int strict_in = strict;
16338
16339 /* I don't think this will do the right thing with respect to types.
16340 But the only case I've seen it in so far has been array bounds, where
16341 signedness is the only information lost, and I think that will be
16342 okay. */
16343 while (TREE_CODE (parm) == NOP_EXPR)
16344 parm = TREE_OPERAND (parm, 0);
16345
16346 if (arg == error_mark_node)
16347 return unify_invalid (explain_p);
16348 if (arg == unknown_type_node
16349 || arg == init_list_type_node)
16350 /* We can't deduce anything from this, but we might get all the
16351 template args from other function args. */
16352 return unify_success (explain_p);
16353
16354 /* If PARM uses template parameters, then we can't bail out here,
16355 even if ARG == PARM, since we won't record unifications for the
16356 template parameters. We might need them if we're trying to
16357 figure out which of two things is more specialized. */
16358 if (arg == parm && !uses_template_parms (parm))
16359 return unify_success (explain_p);
16360
16361 /* Handle init lists early, so the rest of the function can assume
16362 we're dealing with a type. */
16363 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
16364 {
16365 tree elt, elttype;
16366 unsigned i;
16367 tree orig_parm = parm;
16368
16369 /* Replace T with std::initializer_list<T> for deduction. */
16370 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16371 && flag_deduce_init_list)
16372 parm = listify (parm);
16373
16374 if (!is_std_init_list (parm))
16375 /* We can only deduce from an initializer list argument if the
16376 parameter is std::initializer_list; otherwise this is a
16377 non-deduced context. */
16378 return unify_success (explain_p);
16379
16380 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
16381
16382 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
16383 {
16384 int elt_strict = strict;
16385
16386 if (elt == error_mark_node)
16387 return unify_invalid (explain_p);
16388
16389 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
16390 {
16391 tree type = TREE_TYPE (elt);
16392 /* It should only be possible to get here for a call. */
16393 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
16394 elt_strict |= maybe_adjust_types_for_deduction
16395 (DEDUCE_CALL, &elttype, &type, elt);
16396 elt = type;
16397 }
16398
16399 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
16400 explain_p);
16401 }
16402
16403 /* If the std::initializer_list<T> deduction worked, replace the
16404 deduced A with std::initializer_list<A>. */
16405 if (orig_parm != parm)
16406 {
16407 idx = TEMPLATE_TYPE_IDX (orig_parm);
16408 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16409 targ = listify (targ);
16410 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
16411 }
16412 return unify_success (explain_p);
16413 }
16414
16415 /* Immediately reject some pairs that won't unify because of
16416 cv-qualification mismatches. */
16417 if (TREE_CODE (arg) == TREE_CODE (parm)
16418 && TYPE_P (arg)
16419 /* It is the elements of the array which hold the cv quals of an array
16420 type, and the elements might be template type parms. We'll check
16421 when we recurse. */
16422 && TREE_CODE (arg) != ARRAY_TYPE
16423 /* We check the cv-qualifiers when unifying with template type
16424 parameters below. We want to allow ARG `const T' to unify with
16425 PARM `T' for example, when computing which of two templates
16426 is more specialized, for example. */
16427 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16428 && !check_cv_quals_for_unify (strict_in, arg, parm))
16429 return unify_cv_qual_mismatch (explain_p, parm, arg);
16430
16431 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16432 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16433 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16434 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16435 strict &= ~UNIFY_ALLOW_DERIVED;
16436 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16437 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16438
16439 switch (TREE_CODE (parm))
16440 {
16441 case TYPENAME_TYPE:
16442 case SCOPE_REF:
16443 case UNBOUND_CLASS_TEMPLATE:
16444 /* In a type which contains a nested-name-specifier, template
16445 argument values cannot be deduced for template parameters used
16446 within the nested-name-specifier. */
16447 return unify_success (explain_p);
16448
16449 case TEMPLATE_TYPE_PARM:
16450 case TEMPLATE_TEMPLATE_PARM:
16451 case BOUND_TEMPLATE_TEMPLATE_PARM:
16452 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16453 if (tparm == error_mark_node)
16454 return unify_invalid (explain_p);
16455
16456 if (TEMPLATE_TYPE_LEVEL (parm)
16457 != template_decl_level (tparm))
16458 /* The PARM is not one we're trying to unify. Just check
16459 to see if it matches ARG. */
16460 {
16461 if (TREE_CODE (arg) == TREE_CODE (parm)
16462 && (is_auto (parm) ? is_auto (arg)
16463 : same_type_p (parm, arg)))
16464 return unify_success (explain_p);
16465 else
16466 return unify_type_mismatch (explain_p, parm, arg);
16467 }
16468 idx = TEMPLATE_TYPE_IDX (parm);
16469 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16470 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16471 if (tparm == error_mark_node)
16472 return unify_invalid (explain_p);
16473
16474 /* Check for mixed types and values. */
16475 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16476 && TREE_CODE (tparm) != TYPE_DECL)
16477 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16478 && TREE_CODE (tparm) != TEMPLATE_DECL))
16479 gcc_unreachable ();
16480
16481 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16482 {
16483 /* ARG must be constructed from a template class or a template
16484 template parameter. */
16485 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16486 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16487 return unify_template_deduction_failure (explain_p, parm, arg);
16488
16489 {
16490 tree parmvec = TYPE_TI_ARGS (parm);
16491 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16492 tree full_argvec = add_to_template_args (targs, argvec);
16493 tree parm_parms
16494 = DECL_INNERMOST_TEMPLATE_PARMS
16495 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16496 int i, len;
16497 int parm_variadic_p = 0;
16498
16499 /* The resolution to DR150 makes clear that default
16500 arguments for an N-argument may not be used to bind T
16501 to a template template parameter with fewer than N
16502 parameters. It is not safe to permit the binding of
16503 default arguments as an extension, as that may change
16504 the meaning of a conforming program. Consider:
16505
16506 struct Dense { static const unsigned int dim = 1; };
16507
16508 template <template <typename> class View,
16509 typename Block>
16510 void operator+(float, View<Block> const&);
16511
16512 template <typename Block,
16513 unsigned int Dim = Block::dim>
16514 struct Lvalue_proxy { operator float() const; };
16515
16516 void
16517 test_1d (void) {
16518 Lvalue_proxy<Dense> p;
16519 float b;
16520 b + p;
16521 }
16522
16523 Here, if Lvalue_proxy is permitted to bind to View, then
16524 the global operator+ will be used; if they are not, the
16525 Lvalue_proxy will be converted to float. */
16526 if (coerce_template_parms (parm_parms,
16527 full_argvec,
16528 TYPE_TI_TEMPLATE (parm),
16529 (explain_p
16530 ? tf_warning_or_error
16531 : tf_none),
16532 /*require_all_args=*/true,
16533 /*use_default_args=*/false)
16534 == error_mark_node)
16535 return 1;
16536
16537 /* Deduce arguments T, i from TT<T> or TT<i>.
16538 We check each element of PARMVEC and ARGVEC individually
16539 rather than the whole TREE_VEC since they can have
16540 different number of elements. */
16541
16542 parmvec = expand_template_argument_pack (parmvec);
16543 argvec = expand_template_argument_pack (argvec);
16544
16545 len = TREE_VEC_LENGTH (parmvec);
16546
16547 /* Check if the parameters end in a pack, making them
16548 variadic. */
16549 if (len > 0
16550 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16551 parm_variadic_p = 1;
16552
16553 for (i = 0; i < len - parm_variadic_p; ++i)
16554 /* If the template argument list of P contains a pack
16555 expansion that is not the last template argument, the
16556 entire template argument list is a non-deduced
16557 context. */
16558 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
16559 return unify_success (explain_p);
16560
16561 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16562 return unify_too_few_arguments (explain_p,
16563 TREE_VEC_LENGTH (argvec), len);
16564
16565 for (i = 0; i < len - parm_variadic_p; ++i)
16566 {
16567 RECUR_AND_CHECK_FAILURE (tparms, targs,
16568 TREE_VEC_ELT (parmvec, i),
16569 TREE_VEC_ELT (argvec, i),
16570 UNIFY_ALLOW_NONE, explain_p);
16571 }
16572
16573 if (parm_variadic_p
16574 && unify_pack_expansion (tparms, targs,
16575 parmvec, argvec,
16576 DEDUCE_EXACT,
16577 /*subr=*/true, explain_p))
16578 return 1;
16579 }
16580 arg = TYPE_TI_TEMPLATE (arg);
16581
16582 /* Fall through to deduce template name. */
16583 }
16584
16585 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16586 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16587 {
16588 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16589
16590 /* Simple cases: Value already set, does match or doesn't. */
16591 if (targ != NULL_TREE && template_args_equal (targ, arg))
16592 return unify_success (explain_p);
16593 else if (targ)
16594 return unify_inconsistency (explain_p, parm, targ, arg);
16595 }
16596 else
16597 {
16598 /* If PARM is `const T' and ARG is only `int', we don't have
16599 a match unless we are allowing additional qualification.
16600 If ARG is `const int' and PARM is just `T' that's OK;
16601 that binds `const int' to `T'. */
16602 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16603 arg, parm))
16604 return unify_cv_qual_mismatch (explain_p, parm, arg);
16605
16606 /* Consider the case where ARG is `const volatile int' and
16607 PARM is `const T'. Then, T should be `volatile int'. */
16608 arg = cp_build_qualified_type_real
16609 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16610 if (arg == error_mark_node)
16611 return unify_invalid (explain_p);
16612
16613 /* Simple cases: Value already set, does match or doesn't. */
16614 if (targ != NULL_TREE && same_type_p (targ, arg))
16615 return unify_success (explain_p);
16616 else if (targ)
16617 return unify_inconsistency (explain_p, parm, targ, arg);
16618
16619 /* Make sure that ARG is not a variable-sized array. (Note
16620 that were talking about variable-sized arrays (like
16621 `int[n]'), rather than arrays of unknown size (like
16622 `int[]').) We'll get very confused by such a type since
16623 the bound of the array is not constant, and therefore
16624 not mangleable. Besides, such types are not allowed in
16625 ISO C++, so we can do as we please here. We do allow
16626 them for 'auto' deduction, since that isn't ABI-exposed. */
16627 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16628 return unify_vla_arg (explain_p, arg);
16629
16630 /* Strip typedefs as in convert_template_argument. */
16631 arg = canonicalize_type_argument (arg, tf_none);
16632 }
16633
16634 /* If ARG is a parameter pack or an expansion, we cannot unify
16635 against it unless PARM is also a parameter pack. */
16636 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16637 && !template_parameter_pack_p (parm))
16638 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16639
16640 /* If the argument deduction results is a METHOD_TYPE,
16641 then there is a problem.
16642 METHOD_TYPE doesn't map to any real C++ type the result of
16643 the deduction can not be of that type. */
16644 if (TREE_CODE (arg) == METHOD_TYPE)
16645 return unify_method_type_error (explain_p, arg);
16646
16647 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16648 return unify_success (explain_p);
16649
16650 case TEMPLATE_PARM_INDEX:
16651 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16652 if (tparm == error_mark_node)
16653 return unify_invalid (explain_p);
16654
16655 if (TEMPLATE_PARM_LEVEL (parm)
16656 != template_decl_level (tparm))
16657 {
16658 /* The PARM is not one we're trying to unify. Just check
16659 to see if it matches ARG. */
16660 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16661 && cp_tree_equal (parm, arg));
16662 if (result)
16663 unify_expression_unequal (explain_p, parm, arg);
16664 return result;
16665 }
16666
16667 idx = TEMPLATE_PARM_IDX (parm);
16668 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16669
16670 if (targ)
16671 {
16672 int x = !cp_tree_equal (targ, arg);
16673 if (x)
16674 unify_inconsistency (explain_p, parm, targ, arg);
16675 return x;
16676 }
16677
16678 /* [temp.deduct.type] If, in the declaration of a function template
16679 with a non-type template-parameter, the non-type
16680 template-parameter is used in an expression in the function
16681 parameter-list and, if the corresponding template-argument is
16682 deduced, the template-argument type shall match the type of the
16683 template-parameter exactly, except that a template-argument
16684 deduced from an array bound may be of any integral type.
16685 The non-type parameter might use already deduced type parameters. */
16686 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16687 if (!TREE_TYPE (arg))
16688 /* Template-parameter dependent expression. Just accept it for now.
16689 It will later be processed in convert_template_argument. */
16690 ;
16691 else if (same_type_p (TREE_TYPE (arg), tparm))
16692 /* OK */;
16693 else if ((strict & UNIFY_ALLOW_INTEGER)
16694 && (TREE_CODE (tparm) == INTEGER_TYPE
16695 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16696 /* Convert the ARG to the type of PARM; the deduced non-type
16697 template argument must exactly match the types of the
16698 corresponding parameter. */
16699 arg = fold (build_nop (tparm, arg));
16700 else if (uses_template_parms (tparm))
16701 /* We haven't deduced the type of this parameter yet. Try again
16702 later. */
16703 return unify_success (explain_p);
16704 else
16705 return unify_type_mismatch (explain_p, tparm, arg);
16706
16707 /* If ARG is a parameter pack or an expansion, we cannot unify
16708 against it unless PARM is also a parameter pack. */
16709 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16710 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16711 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16712
16713 arg = strip_typedefs_expr (arg);
16714 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16715 return unify_success (explain_p);
16716
16717 case PTRMEM_CST:
16718 {
16719 /* A pointer-to-member constant can be unified only with
16720 another constant. */
16721 if (TREE_CODE (arg) != PTRMEM_CST)
16722 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16723
16724 /* Just unify the class member. It would be useless (and possibly
16725 wrong, depending on the strict flags) to unify also
16726 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16727 arg refer to the same variable, even if through different
16728 classes. For instance:
16729
16730 struct A { int x; };
16731 struct B : A { };
16732
16733 Unification of &A::x and &B::x must succeed. */
16734 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16735 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16736 }
16737
16738 case POINTER_TYPE:
16739 {
16740 if (TREE_CODE (arg) != POINTER_TYPE)
16741 return unify_type_mismatch (explain_p, parm, arg);
16742
16743 /* [temp.deduct.call]
16744
16745 A can be another pointer or pointer to member type that can
16746 be converted to the deduced A via a qualification
16747 conversion (_conv.qual_).
16748
16749 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16750 This will allow for additional cv-qualification of the
16751 pointed-to types if appropriate. */
16752
16753 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16754 /* The derived-to-base conversion only persists through one
16755 level of pointers. */
16756 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16757
16758 return unify (tparms, targs, TREE_TYPE (parm),
16759 TREE_TYPE (arg), strict, explain_p);
16760 }
16761
16762 case REFERENCE_TYPE:
16763 if (TREE_CODE (arg) != REFERENCE_TYPE)
16764 return unify_type_mismatch (explain_p, parm, arg);
16765 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16766 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16767
16768 case ARRAY_TYPE:
16769 if (TREE_CODE (arg) != ARRAY_TYPE)
16770 return unify_type_mismatch (explain_p, parm, arg);
16771 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16772 != (TYPE_DOMAIN (arg) == NULL_TREE))
16773 return unify_type_mismatch (explain_p, parm, arg);
16774 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16775 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16776 if (TYPE_DOMAIN (parm) != NULL_TREE)
16777 {
16778 tree parm_max;
16779 tree arg_max;
16780 bool parm_cst;
16781 bool arg_cst;
16782
16783 /* Our representation of array types uses "N - 1" as the
16784 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16785 not an integer constant. We cannot unify arbitrarily
16786 complex expressions, so we eliminate the MINUS_EXPRs
16787 here. */
16788 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16789 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16790 if (!parm_cst)
16791 {
16792 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16793 parm_max = TREE_OPERAND (parm_max, 0);
16794 }
16795 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16796 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16797 if (!arg_cst)
16798 {
16799 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16800 trying to unify the type of a variable with the type
16801 of a template parameter. For example:
16802
16803 template <unsigned int N>
16804 void f (char (&) [N]);
16805 int g();
16806 void h(int i) {
16807 char a[g(i)];
16808 f(a);
16809 }
16810
16811 Here, the type of the ARG will be "int [g(i)]", and
16812 may be a SAVE_EXPR, etc. */
16813 if (TREE_CODE (arg_max) != MINUS_EXPR)
16814 return unify_vla_arg (explain_p, arg);
16815 arg_max = TREE_OPERAND (arg_max, 0);
16816 }
16817
16818 /* If only one of the bounds used a MINUS_EXPR, compensate
16819 by adding one to the other bound. */
16820 if (parm_cst && !arg_cst)
16821 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16822 integer_type_node,
16823 parm_max,
16824 integer_one_node);
16825 else if (arg_cst && !parm_cst)
16826 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16827 integer_type_node,
16828 arg_max,
16829 integer_one_node);
16830
16831 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16832 UNIFY_ALLOW_INTEGER, explain_p);
16833 }
16834 return unify_success (explain_p);
16835
16836 case REAL_TYPE:
16837 case COMPLEX_TYPE:
16838 case VECTOR_TYPE:
16839 case INTEGER_TYPE:
16840 case BOOLEAN_TYPE:
16841 case ENUMERAL_TYPE:
16842 case VOID_TYPE:
16843 case NULLPTR_TYPE:
16844 if (TREE_CODE (arg) != TREE_CODE (parm))
16845 return unify_type_mismatch (explain_p, parm, arg);
16846
16847 /* We have already checked cv-qualification at the top of the
16848 function. */
16849 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16850 return unify_type_mismatch (explain_p, parm, arg);
16851
16852 /* As far as unification is concerned, this wins. Later checks
16853 will invalidate it if necessary. */
16854 return unify_success (explain_p);
16855
16856 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16857 /* Type INTEGER_CST can come from ordinary constant template args. */
16858 case INTEGER_CST:
16859 while (TREE_CODE (arg) == NOP_EXPR)
16860 arg = TREE_OPERAND (arg, 0);
16861
16862 if (TREE_CODE (arg) != INTEGER_CST)
16863 return unify_template_argument_mismatch (explain_p, parm, arg);
16864 return (tree_int_cst_equal (parm, arg)
16865 ? unify_success (explain_p)
16866 : unify_template_argument_mismatch (explain_p, parm, arg));
16867
16868 case TREE_VEC:
16869 {
16870 int i, len, argslen;
16871 int parm_variadic_p = 0;
16872
16873 if (TREE_CODE (arg) != TREE_VEC)
16874 return unify_template_argument_mismatch (explain_p, parm, arg);
16875
16876 len = TREE_VEC_LENGTH (parm);
16877 argslen = TREE_VEC_LENGTH (arg);
16878
16879 /* Check for pack expansions in the parameters. */
16880 for (i = 0; i < len; ++i)
16881 {
16882 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16883 {
16884 if (i == len - 1)
16885 /* We can unify against something with a trailing
16886 parameter pack. */
16887 parm_variadic_p = 1;
16888 else
16889 /* [temp.deduct.type]/9: If the template argument list of
16890 P contains a pack expansion that is not the last
16891 template argument, the entire template argument list
16892 is a non-deduced context. */
16893 return unify_success (explain_p);
16894 }
16895 }
16896
16897 /* If we don't have enough arguments to satisfy the parameters
16898 (not counting the pack expression at the end), or we have
16899 too many arguments for a parameter list that doesn't end in
16900 a pack expression, we can't unify. */
16901 if (parm_variadic_p
16902 ? argslen < len - parm_variadic_p
16903 : argslen != len)
16904 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16905
16906 /* Unify all of the parameters that precede the (optional)
16907 pack expression. */
16908 for (i = 0; i < len - parm_variadic_p; ++i)
16909 {
16910 RECUR_AND_CHECK_FAILURE (tparms, targs,
16911 TREE_VEC_ELT (parm, i),
16912 TREE_VEC_ELT (arg, i),
16913 UNIFY_ALLOW_NONE, explain_p);
16914 }
16915 if (parm_variadic_p)
16916 return unify_pack_expansion (tparms, targs, parm, arg,
16917 DEDUCE_EXACT,
16918 /*subr=*/true, explain_p);
16919 return unify_success (explain_p);
16920 }
16921
16922 case RECORD_TYPE:
16923 case UNION_TYPE:
16924 if (TREE_CODE (arg) != TREE_CODE (parm))
16925 return unify_type_mismatch (explain_p, parm, arg);
16926
16927 if (TYPE_PTRMEMFUNC_P (parm))
16928 {
16929 if (!TYPE_PTRMEMFUNC_P (arg))
16930 return unify_type_mismatch (explain_p, parm, arg);
16931
16932 return unify (tparms, targs,
16933 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16934 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16935 strict, explain_p);
16936 }
16937
16938 if (CLASSTYPE_TEMPLATE_INFO (parm))
16939 {
16940 tree t = NULL_TREE;
16941
16942 if (strict_in & UNIFY_ALLOW_DERIVED)
16943 {
16944 /* First, we try to unify the PARM and ARG directly. */
16945 t = try_class_unification (tparms, targs,
16946 parm, arg, explain_p);
16947
16948 if (!t)
16949 {
16950 /* Fallback to the special case allowed in
16951 [temp.deduct.call]:
16952
16953 If P is a class, and P has the form
16954 template-id, then A can be a derived class of
16955 the deduced A. Likewise, if P is a pointer to
16956 a class of the form template-id, A can be a
16957 pointer to a derived class pointed to by the
16958 deduced A. */
16959 enum template_base_result r;
16960 r = get_template_base (tparms, targs, parm, arg,
16961 explain_p, &t);
16962
16963 if (!t)
16964 return unify_no_common_base (explain_p, r, parm, arg);
16965 }
16966 }
16967 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16968 && (CLASSTYPE_TI_TEMPLATE (parm)
16969 == CLASSTYPE_TI_TEMPLATE (arg)))
16970 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16971 Then, we should unify `int' and `U'. */
16972 t = arg;
16973 else
16974 /* There's no chance of unification succeeding. */
16975 return unify_type_mismatch (explain_p, parm, arg);
16976
16977 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16978 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16979 }
16980 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16981 return unify_type_mismatch (explain_p, parm, arg);
16982 return unify_success (explain_p);
16983
16984 case METHOD_TYPE:
16985 case FUNCTION_TYPE:
16986 {
16987 unsigned int nargs;
16988 tree *args;
16989 tree a;
16990 unsigned int i;
16991
16992 if (TREE_CODE (arg) != TREE_CODE (parm))
16993 return unify_type_mismatch (explain_p, parm, arg);
16994
16995 /* CV qualifications for methods can never be deduced, they must
16996 match exactly. We need to check them explicitly here,
16997 because type_unification_real treats them as any other
16998 cv-qualified parameter. */
16999 if (TREE_CODE (parm) == METHOD_TYPE
17000 && (!check_cv_quals_for_unify
17001 (UNIFY_ALLOW_NONE,
17002 class_of_this_parm (arg),
17003 class_of_this_parm (parm))))
17004 return unify_cv_qual_mismatch (explain_p, parm, arg);
17005
17006 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17007 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17008
17009 nargs = list_length (TYPE_ARG_TYPES (arg));
17010 args = XALLOCAVEC (tree, nargs);
17011 for (a = TYPE_ARG_TYPES (arg), i = 0;
17012 a != NULL_TREE && a != void_list_node;
17013 a = TREE_CHAIN (a), ++i)
17014 args[i] = TREE_VALUE (a);
17015 nargs = i;
17016
17017 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17018 args, nargs, 1, DEDUCE_EXACT,
17019 LOOKUP_NORMAL, explain_p);
17020 }
17021
17022 case OFFSET_TYPE:
17023 /* Unify a pointer to member with a pointer to member function, which
17024 deduces the type of the member as a function type. */
17025 if (TYPE_PTRMEMFUNC_P (arg))
17026 {
17027 tree method_type;
17028 tree fntype;
17029
17030 /* Check top-level cv qualifiers */
17031 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17032 return unify_cv_qual_mismatch (explain_p, parm, arg);
17033
17034 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17035 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17036 UNIFY_ALLOW_NONE, explain_p);
17037
17038 /* Determine the type of the function we are unifying against. */
17039 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
17040 fntype =
17041 build_function_type (TREE_TYPE (method_type),
17042 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
17043
17044 /* Extract the cv-qualifiers of the member function from the
17045 implicit object parameter and place them on the function
17046 type to be restored later. */
17047 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
17048 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17049 }
17050
17051 if (TREE_CODE (arg) != OFFSET_TYPE)
17052 return unify_type_mismatch (explain_p, parm, arg);
17053 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17054 TYPE_OFFSET_BASETYPE (arg),
17055 UNIFY_ALLOW_NONE, explain_p);
17056 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17057 strict, explain_p);
17058
17059 case CONST_DECL:
17060 if (DECL_TEMPLATE_PARM_P (parm))
17061 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17062 if (arg != integral_constant_value (parm))
17063 return unify_template_argument_mismatch (explain_p, parm, arg);
17064 return unify_success (explain_p);
17065
17066 case FIELD_DECL:
17067 case TEMPLATE_DECL:
17068 /* Matched cases are handled by the ARG == PARM test above. */
17069 return unify_template_argument_mismatch (explain_p, parm, arg);
17070
17071 case VAR_DECL:
17072 /* A non-type template parameter that is a variable should be a
17073 an integral constant, in which case, it whould have been
17074 folded into its (constant) value. So we should not be getting
17075 a variable here. */
17076 gcc_unreachable ();
17077
17078 case TYPE_ARGUMENT_PACK:
17079 case NONTYPE_ARGUMENT_PACK:
17080 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17081 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17082
17083 case TYPEOF_TYPE:
17084 case DECLTYPE_TYPE:
17085 case UNDERLYING_TYPE:
17086 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17087 or UNDERLYING_TYPE nodes. */
17088 return unify_success (explain_p);
17089
17090 case ERROR_MARK:
17091 /* Unification fails if we hit an error node. */
17092 return unify_invalid (explain_p);
17093
17094 default:
17095 /* An unresolved overload is a nondeduced context. */
17096 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17097 return unify_success (explain_p);
17098 gcc_assert (EXPR_P (parm));
17099
17100 /* We must be looking at an expression. This can happen with
17101 something like:
17102
17103 template <int I>
17104 void foo(S<I>, S<I + 2>);
17105
17106 This is a "nondeduced context":
17107
17108 [deduct.type]
17109
17110 The nondeduced contexts are:
17111
17112 --A type that is a template-id in which one or more of
17113 the template-arguments is an expression that references
17114 a template-parameter.
17115
17116 In these cases, we assume deduction succeeded, but don't
17117 actually infer any unifications. */
17118
17119 if (!uses_template_parms (parm)
17120 && !template_args_equal (parm, arg))
17121 return unify_expression_unequal (explain_p, parm, arg);
17122 else
17123 return unify_success (explain_p);
17124 }
17125 }
17126 #undef RECUR_AND_CHECK_FAILURE
17127 \f
17128 /* Note that DECL can be defined in this translation unit, if
17129 required. */
17130
17131 static void
17132 mark_definable (tree decl)
17133 {
17134 tree clone;
17135 DECL_NOT_REALLY_EXTERN (decl) = 1;
17136 FOR_EACH_CLONE (clone, decl)
17137 DECL_NOT_REALLY_EXTERN (clone) = 1;
17138 }
17139
17140 /* Called if RESULT is explicitly instantiated, or is a member of an
17141 explicitly instantiated class. */
17142
17143 void
17144 mark_decl_instantiated (tree result, int extern_p)
17145 {
17146 SET_DECL_EXPLICIT_INSTANTIATION (result);
17147
17148 /* If this entity has already been written out, it's too late to
17149 make any modifications. */
17150 if (TREE_ASM_WRITTEN (result))
17151 return;
17152
17153 if (TREE_CODE (result) != FUNCTION_DECL)
17154 /* The TREE_PUBLIC flag for function declarations will have been
17155 set correctly by tsubst. */
17156 TREE_PUBLIC (result) = 1;
17157
17158 /* This might have been set by an earlier implicit instantiation. */
17159 DECL_COMDAT (result) = 0;
17160
17161 if (extern_p)
17162 DECL_NOT_REALLY_EXTERN (result) = 0;
17163 else
17164 {
17165 mark_definable (result);
17166 /* Always make artificials weak. */
17167 if (DECL_ARTIFICIAL (result) && flag_weak)
17168 comdat_linkage (result);
17169 /* For WIN32 we also want to put explicit instantiations in
17170 linkonce sections. */
17171 else if (TREE_PUBLIC (result))
17172 maybe_make_one_only (result);
17173 }
17174
17175 /* If EXTERN_P, then this function will not be emitted -- unless
17176 followed by an explicit instantiation, at which point its linkage
17177 will be adjusted. If !EXTERN_P, then this function will be
17178 emitted here. In neither circumstance do we want
17179 import_export_decl to adjust the linkage. */
17180 DECL_INTERFACE_KNOWN (result) = 1;
17181 }
17182
17183 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17184 important template arguments. If any are missing, we check whether
17185 they're important by using error_mark_node for substituting into any
17186 args that were used for partial ordering (the ones between ARGS and END)
17187 and seeing if it bubbles up. */
17188
17189 static bool
17190 check_undeduced_parms (tree targs, tree args, tree end)
17191 {
17192 bool found = false;
17193 int i;
17194 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17195 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17196 {
17197 found = true;
17198 TREE_VEC_ELT (targs, i) = error_mark_node;
17199 }
17200 if (found)
17201 {
17202 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17203 if (substed == error_mark_node)
17204 return true;
17205 }
17206 return false;
17207 }
17208
17209 /* Given two function templates PAT1 and PAT2, return:
17210
17211 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17212 -1 if PAT2 is more specialized than PAT1.
17213 0 if neither is more specialized.
17214
17215 LEN indicates the number of parameters we should consider
17216 (defaulted parameters should not be considered).
17217
17218 The 1998 std underspecified function template partial ordering, and
17219 DR214 addresses the issue. We take pairs of arguments, one from
17220 each of the templates, and deduce them against each other. One of
17221 the templates will be more specialized if all the *other*
17222 template's arguments deduce against its arguments and at least one
17223 of its arguments *does* *not* deduce against the other template's
17224 corresponding argument. Deduction is done as for class templates.
17225 The arguments used in deduction have reference and top level cv
17226 qualifiers removed. Iff both arguments were originally reference
17227 types *and* deduction succeeds in both directions, the template
17228 with the more cv-qualified argument wins for that pairing (if
17229 neither is more cv-qualified, they both are equal). Unlike regular
17230 deduction, after all the arguments have been deduced in this way,
17231 we do *not* verify the deduced template argument values can be
17232 substituted into non-deduced contexts.
17233
17234 The logic can be a bit confusing here, because we look at deduce1 and
17235 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17236 can find template arguments for pat1 to make arg1 look like arg2, that
17237 means that arg2 is at least as specialized as arg1. */
17238
17239 int
17240 more_specialized_fn (tree pat1, tree pat2, int len)
17241 {
17242 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17243 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17244 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17245 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17246 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17247 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17248 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17249 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17250 tree origs1, origs2;
17251 bool lose1 = false;
17252 bool lose2 = false;
17253
17254 /* Remove the this parameter from non-static member functions. If
17255 one is a non-static member function and the other is not a static
17256 member function, remove the first parameter from that function
17257 also. This situation occurs for operator functions where we
17258 locate both a member function (with this pointer) and non-member
17259 operator (with explicit first operand). */
17260 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
17261 {
17262 len--; /* LEN is the number of significant arguments for DECL1 */
17263 args1 = TREE_CHAIN (args1);
17264 if (!DECL_STATIC_FUNCTION_P (decl2))
17265 args2 = TREE_CHAIN (args2);
17266 }
17267 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
17268 {
17269 args2 = TREE_CHAIN (args2);
17270 if (!DECL_STATIC_FUNCTION_P (decl1))
17271 {
17272 len--;
17273 args1 = TREE_CHAIN (args1);
17274 }
17275 }
17276
17277 /* If only one is a conversion operator, they are unordered. */
17278 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
17279 return 0;
17280
17281 /* Consider the return type for a conversion function */
17282 if (DECL_CONV_FN_P (decl1))
17283 {
17284 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
17285 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
17286 len++;
17287 }
17288
17289 processing_template_decl++;
17290
17291 origs1 = args1;
17292 origs2 = args2;
17293
17294 while (len--
17295 /* Stop when an ellipsis is seen. */
17296 && args1 != NULL_TREE && args2 != NULL_TREE)
17297 {
17298 tree arg1 = TREE_VALUE (args1);
17299 tree arg2 = TREE_VALUE (args2);
17300 int deduce1, deduce2;
17301 int quals1 = -1;
17302 int quals2 = -1;
17303
17304 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17305 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17306 {
17307 /* When both arguments are pack expansions, we need only
17308 unify the patterns themselves. */
17309 arg1 = PACK_EXPANSION_PATTERN (arg1);
17310 arg2 = PACK_EXPANSION_PATTERN (arg2);
17311
17312 /* This is the last comparison we need to do. */
17313 len = 0;
17314 }
17315
17316 if (TREE_CODE (arg1) == REFERENCE_TYPE)
17317 {
17318 arg1 = TREE_TYPE (arg1);
17319 quals1 = cp_type_quals (arg1);
17320 }
17321
17322 if (TREE_CODE (arg2) == REFERENCE_TYPE)
17323 {
17324 arg2 = TREE_TYPE (arg2);
17325 quals2 = cp_type_quals (arg2);
17326 }
17327
17328 arg1 = TYPE_MAIN_VARIANT (arg1);
17329 arg2 = TYPE_MAIN_VARIANT (arg2);
17330
17331 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
17332 {
17333 int i, len2 = list_length (args2);
17334 tree parmvec = make_tree_vec (1);
17335 tree argvec = make_tree_vec (len2);
17336 tree ta = args2;
17337
17338 /* Setup the parameter vector, which contains only ARG1. */
17339 TREE_VEC_ELT (parmvec, 0) = arg1;
17340
17341 /* Setup the argument vector, which contains the remaining
17342 arguments. */
17343 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
17344 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17345
17346 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
17347 argvec, DEDUCE_EXACT,
17348 /*subr=*/true, /*explain_p=*/false)
17349 == 0);
17350
17351 /* We cannot deduce in the other direction, because ARG1 is
17352 a pack expansion but ARG2 is not. */
17353 deduce2 = 0;
17354 }
17355 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17356 {
17357 int i, len1 = list_length (args1);
17358 tree parmvec = make_tree_vec (1);
17359 tree argvec = make_tree_vec (len1);
17360 tree ta = args1;
17361
17362 /* Setup the parameter vector, which contains only ARG1. */
17363 TREE_VEC_ELT (parmvec, 0) = arg2;
17364
17365 /* Setup the argument vector, which contains the remaining
17366 arguments. */
17367 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
17368 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17369
17370 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
17371 argvec, DEDUCE_EXACT,
17372 /*subr=*/true, /*explain_p=*/false)
17373 == 0);
17374
17375 /* We cannot deduce in the other direction, because ARG2 is
17376 a pack expansion but ARG1 is not.*/
17377 deduce1 = 0;
17378 }
17379
17380 else
17381 {
17382 /* The normal case, where neither argument is a pack
17383 expansion. */
17384 deduce1 = (unify (tparms1, targs1, arg1, arg2,
17385 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17386 == 0);
17387 deduce2 = (unify (tparms2, targs2, arg2, arg1,
17388 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17389 == 0);
17390 }
17391
17392 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17393 arg2, then arg2 is not as specialized as arg1. */
17394 if (!deduce1)
17395 lose2 = true;
17396 if (!deduce2)
17397 lose1 = true;
17398
17399 /* "If, for a given type, deduction succeeds in both directions
17400 (i.e., the types are identical after the transformations above)
17401 and if the type from the argument template is more cv-qualified
17402 than the type from the parameter template (as described above)
17403 that type is considered to be more specialized than the other. If
17404 neither type is more cv-qualified than the other then neither type
17405 is more specialized than the other." */
17406
17407 if (deduce1 && deduce2
17408 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17409 {
17410 if ((quals1 & quals2) == quals2)
17411 lose2 = true;
17412 if ((quals1 & quals2) == quals1)
17413 lose1 = true;
17414 }
17415
17416 if (lose1 && lose2)
17417 /* We've failed to deduce something in either direction.
17418 These must be unordered. */
17419 break;
17420
17421 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17422 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17423 /* We have already processed all of the arguments in our
17424 handing of the pack expansion type. */
17425 len = 0;
17426
17427 args1 = TREE_CHAIN (args1);
17428 args2 = TREE_CHAIN (args2);
17429 }
17430
17431 /* "In most cases, all template parameters must have values in order for
17432 deduction to succeed, but for partial ordering purposes a template
17433 parameter may remain without a value provided it is not used in the
17434 types being used for partial ordering."
17435
17436 Thus, if we are missing any of the targs1 we need to substitute into
17437 origs1, then pat2 is not as specialized as pat1. This can happen when
17438 there is a nondeduced context. */
17439 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17440 lose2 = true;
17441 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17442 lose1 = true;
17443
17444 processing_template_decl--;
17445
17446 /* All things being equal, if the next argument is a pack expansion
17447 for one function but not for the other, prefer the
17448 non-variadic function. FIXME this is bogus; see c++/41958. */
17449 if (lose1 == lose2
17450 && args1 && TREE_VALUE (args1)
17451 && args2 && TREE_VALUE (args2))
17452 {
17453 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17454 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17455 }
17456
17457 if (lose1 == lose2)
17458 return 0;
17459 else if (!lose1)
17460 return 1;
17461 else
17462 return -1;
17463 }
17464
17465 /* Determine which of two partial specializations of MAIN_TMPL is more
17466 specialized.
17467
17468 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17469 to the first partial specialization. The TREE_VALUE is the
17470 innermost set of template parameters for the partial
17471 specialization. PAT2 is similar, but for the second template.
17472
17473 Return 1 if the first partial specialization is more specialized;
17474 -1 if the second is more specialized; 0 if neither is more
17475 specialized.
17476
17477 See [temp.class.order] for information about determining which of
17478 two templates is more specialized. */
17479
17480 static int
17481 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
17482 {
17483 tree targs;
17484 tree tmpl1, tmpl2;
17485 int winner = 0;
17486 bool any_deductions = false;
17487
17488 tmpl1 = TREE_TYPE (pat1);
17489 tmpl2 = TREE_TYPE (pat2);
17490
17491 /* Just like what happens for functions, if we are ordering between
17492 different class template specializations, we may encounter dependent
17493 types in the arguments, and we need our dependency check functions
17494 to behave correctly. */
17495 ++processing_template_decl;
17496 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
17497 CLASSTYPE_TI_ARGS (tmpl1),
17498 CLASSTYPE_TI_ARGS (tmpl2));
17499 if (targs)
17500 {
17501 --winner;
17502 any_deductions = true;
17503 }
17504
17505 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
17506 CLASSTYPE_TI_ARGS (tmpl2),
17507 CLASSTYPE_TI_ARGS (tmpl1));
17508 if (targs)
17509 {
17510 ++winner;
17511 any_deductions = true;
17512 }
17513 --processing_template_decl;
17514
17515 /* In the case of a tie where at least one of the class templates
17516 has a parameter pack at the end, the template with the most
17517 non-packed parameters wins. */
17518 if (winner == 0
17519 && any_deductions
17520 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17521 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17522 {
17523 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17524 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17525 int len1 = TREE_VEC_LENGTH (args1);
17526 int len2 = TREE_VEC_LENGTH (args2);
17527
17528 /* We don't count the pack expansion at the end. */
17529 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17530 --len1;
17531 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17532 --len2;
17533
17534 if (len1 > len2)
17535 return 1;
17536 else if (len1 < len2)
17537 return -1;
17538 }
17539
17540 return winner;
17541 }
17542
17543 /* Return the template arguments that will produce the function signature
17544 DECL from the function template FN, with the explicit template
17545 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17546 also match. Return NULL_TREE if no satisfactory arguments could be
17547 found. */
17548
17549 static tree
17550 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17551 {
17552 int ntparms = DECL_NTPARMS (fn);
17553 tree targs = make_tree_vec (ntparms);
17554 tree decl_type = TREE_TYPE (decl);
17555 tree decl_arg_types;
17556 tree *args;
17557 unsigned int nargs, ix;
17558 tree arg;
17559
17560 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
17561
17562 /* Never do unification on the 'this' parameter. */
17563 decl_arg_types = skip_artificial_parms_for (decl,
17564 TYPE_ARG_TYPES (decl_type));
17565
17566 nargs = list_length (decl_arg_types);
17567 args = XALLOCAVEC (tree, nargs);
17568 for (arg = decl_arg_types, ix = 0;
17569 arg != NULL_TREE && arg != void_list_node;
17570 arg = TREE_CHAIN (arg), ++ix)
17571 args[ix] = TREE_VALUE (arg);
17572
17573 if (fn_type_unification (fn, explicit_args, targs,
17574 args, ix,
17575 (check_rettype || DECL_CONV_FN_P (fn)
17576 ? TREE_TYPE (decl_type) : NULL_TREE),
17577 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)
17578 == error_mark_node)
17579 return NULL_TREE;
17580
17581 return targs;
17582 }
17583
17584 /* Return the innermost template arguments that, when applied to a partial
17585 specialization of MAIN_TMPL whose innermost template parameters are
17586 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17587 ARGS.
17588
17589 For example, suppose we have:
17590
17591 template <class T, class U> struct S {};
17592 template <class T> struct S<T*, int> {};
17593
17594 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17595 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17596 int}. The resulting vector will be {double}, indicating that `T'
17597 is bound to `double'. */
17598
17599 static tree
17600 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
17601 {
17602 int i, ntparms = TREE_VEC_LENGTH (tparms);
17603 tree deduced_args;
17604 tree innermost_deduced_args;
17605
17606 innermost_deduced_args = make_tree_vec (ntparms);
17607 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17608 {
17609 deduced_args = copy_node (args);
17610 SET_TMPL_ARGS_LEVEL (deduced_args,
17611 TMPL_ARGS_DEPTH (deduced_args),
17612 innermost_deduced_args);
17613 }
17614 else
17615 deduced_args = innermost_deduced_args;
17616
17617 if (unify (tparms, deduced_args,
17618 INNERMOST_TEMPLATE_ARGS (spec_args),
17619 INNERMOST_TEMPLATE_ARGS (args),
17620 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17621 return NULL_TREE;
17622
17623 for (i = 0; i < ntparms; ++i)
17624 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17625 return NULL_TREE;
17626
17627 /* Verify that nondeduced template arguments agree with the type
17628 obtained from argument deduction.
17629
17630 For example:
17631
17632 struct A { typedef int X; };
17633 template <class T, class U> struct C {};
17634 template <class T> struct C<T, typename T::X> {};
17635
17636 Then with the instantiation `C<A, int>', we can deduce that
17637 `T' is `A' but unify () does not check whether `typename T::X'
17638 is `int'. */
17639 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17640 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
17641 spec_args, main_tmpl,
17642 tf_none, false, false);
17643 if (spec_args == error_mark_node
17644 /* We only need to check the innermost arguments; the other
17645 arguments will always agree. */
17646 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17647 INNERMOST_TEMPLATE_ARGS (args)))
17648 return NULL_TREE;
17649
17650 /* Now that we have bindings for all of the template arguments,
17651 ensure that the arguments deduced for the template template
17652 parameters have compatible template parameter lists. See the use
17653 of template_template_parm_bindings_ok_p in fn_type_unification
17654 for more information. */
17655 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17656 return NULL_TREE;
17657
17658 return deduced_args;
17659 }
17660
17661 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17662 Return the TREE_LIST node with the most specialized template, if
17663 any. If there is no most specialized template, the error_mark_node
17664 is returned.
17665
17666 Note that this function does not look at, or modify, the
17667 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17668 returned is one of the elements of INSTANTIATIONS, callers may
17669 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17670 and retrieve it from the value returned. */
17671
17672 tree
17673 most_specialized_instantiation (tree templates)
17674 {
17675 tree fn, champ;
17676
17677 ++processing_template_decl;
17678
17679 champ = templates;
17680 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17681 {
17682 int fate = 0;
17683
17684 if (get_bindings (TREE_VALUE (champ),
17685 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17686 NULL_TREE, /*check_ret=*/true))
17687 fate--;
17688
17689 if (get_bindings (TREE_VALUE (fn),
17690 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17691 NULL_TREE, /*check_ret=*/true))
17692 fate++;
17693
17694 if (fate == -1)
17695 champ = fn;
17696 else if (!fate)
17697 {
17698 /* Equally specialized, move to next function. If there
17699 is no next function, nothing's most specialized. */
17700 fn = TREE_CHAIN (fn);
17701 champ = fn;
17702 if (!fn)
17703 break;
17704 }
17705 }
17706
17707 if (champ)
17708 /* Now verify that champ is better than everything earlier in the
17709 instantiation list. */
17710 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17711 if (get_bindings (TREE_VALUE (champ),
17712 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17713 NULL_TREE, /*check_ret=*/true)
17714 || !get_bindings (TREE_VALUE (fn),
17715 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17716 NULL_TREE, /*check_ret=*/true))
17717 {
17718 champ = NULL_TREE;
17719 break;
17720 }
17721
17722 processing_template_decl--;
17723
17724 if (!champ)
17725 return error_mark_node;
17726
17727 return champ;
17728 }
17729
17730 /* If DECL is a specialization of some template, return the most
17731 general such template. Otherwise, returns NULL_TREE.
17732
17733 For example, given:
17734
17735 template <class T> struct S { template <class U> void f(U); };
17736
17737 if TMPL is `template <class U> void S<int>::f(U)' this will return
17738 the full template. This function will not trace past partial
17739 specializations, however. For example, given in addition:
17740
17741 template <class T> struct S<T*> { template <class U> void f(U); };
17742
17743 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17744 `template <class T> template <class U> S<T*>::f(U)'. */
17745
17746 tree
17747 most_general_template (tree decl)
17748 {
17749 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17750 an immediate specialization. */
17751 if (TREE_CODE (decl) == FUNCTION_DECL)
17752 {
17753 if (DECL_TEMPLATE_INFO (decl)) {
17754 decl = DECL_TI_TEMPLATE (decl);
17755
17756 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17757 template friend. */
17758 if (TREE_CODE (decl) != TEMPLATE_DECL)
17759 return NULL_TREE;
17760 } else
17761 return NULL_TREE;
17762 }
17763
17764 /* Look for more and more general templates. */
17765 while (DECL_TEMPLATE_INFO (decl))
17766 {
17767 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17768 (See cp-tree.h for details.) */
17769 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17770 break;
17771
17772 if (CLASS_TYPE_P (TREE_TYPE (decl))
17773 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17774 break;
17775
17776 /* Stop if we run into an explicitly specialized class template. */
17777 if (!DECL_NAMESPACE_SCOPE_P (decl)
17778 && DECL_CONTEXT (decl)
17779 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17780 break;
17781
17782 decl = DECL_TI_TEMPLATE (decl);
17783 }
17784
17785 return decl;
17786 }
17787
17788 /* Return the most specialized of the class template partial
17789 specializations of TMPL which can produce TYPE, a specialization of
17790 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17791 a _TYPE node corresponding to the partial specialization, while the
17792 TREE_PURPOSE is the set of template arguments that must be
17793 substituted into the TREE_TYPE in order to generate TYPE.
17794
17795 If the choice of partial specialization is ambiguous, a diagnostic
17796 is issued, and the error_mark_node is returned. If there are no
17797 partial specializations of TMPL matching TYPE, then NULL_TREE is
17798 returned. */
17799
17800 static tree
17801 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17802 {
17803 tree list = NULL_TREE;
17804 tree t;
17805 tree champ;
17806 int fate;
17807 bool ambiguous_p;
17808 tree args;
17809 tree outer_args = NULL_TREE;
17810
17811 tmpl = most_general_template (tmpl);
17812 args = CLASSTYPE_TI_ARGS (type);
17813
17814 /* For determining which partial specialization to use, only the
17815 innermost args are interesting. */
17816 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17817 {
17818 outer_args = strip_innermost_template_args (args, 1);
17819 args = INNERMOST_TEMPLATE_ARGS (args);
17820 }
17821
17822 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17823 {
17824 tree partial_spec_args;
17825 tree spec_args;
17826 tree parms = TREE_VALUE (t);
17827
17828 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17829
17830 ++processing_template_decl;
17831
17832 if (outer_args)
17833 {
17834 int i;
17835
17836 /* Discard the outer levels of args, and then substitute in the
17837 template args from the enclosing class. */
17838 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17839 partial_spec_args = tsubst_template_args
17840 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17841
17842 /* PARMS already refers to just the innermost parms, but the
17843 template parms in partial_spec_args had their levels lowered
17844 by tsubst, so we need to do the same for the parm list. We
17845 can't just tsubst the TREE_VEC itself, as tsubst wants to
17846 treat a TREE_VEC as an argument vector. */
17847 parms = copy_node (parms);
17848 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17849 TREE_VEC_ELT (parms, i) =
17850 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17851
17852 }
17853
17854 partial_spec_args =
17855 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17856 add_to_template_args (outer_args,
17857 partial_spec_args),
17858 tmpl, tf_none,
17859 /*require_all_args=*/true,
17860 /*use_default_args=*/true);
17861
17862 --processing_template_decl;
17863
17864 if (partial_spec_args == error_mark_node)
17865 return error_mark_node;
17866
17867 spec_args = get_class_bindings (tmpl, parms,
17868 partial_spec_args,
17869 args);
17870 if (spec_args)
17871 {
17872 if (outer_args)
17873 spec_args = add_to_template_args (outer_args, spec_args);
17874 list = tree_cons (spec_args, TREE_VALUE (t), list);
17875 TREE_TYPE (list) = TREE_TYPE (t);
17876 }
17877 }
17878
17879 if (! list)
17880 return NULL_TREE;
17881
17882 ambiguous_p = false;
17883 t = list;
17884 champ = t;
17885 t = TREE_CHAIN (t);
17886 for (; t; t = TREE_CHAIN (t))
17887 {
17888 fate = more_specialized_class (tmpl, champ, t);
17889 if (fate == 1)
17890 ;
17891 else
17892 {
17893 if (fate == 0)
17894 {
17895 t = TREE_CHAIN (t);
17896 if (! t)
17897 {
17898 ambiguous_p = true;
17899 break;
17900 }
17901 }
17902 champ = t;
17903 }
17904 }
17905
17906 if (!ambiguous_p)
17907 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17908 {
17909 fate = more_specialized_class (tmpl, champ, t);
17910 if (fate != 1)
17911 {
17912 ambiguous_p = true;
17913 break;
17914 }
17915 }
17916
17917 if (ambiguous_p)
17918 {
17919 const char *str;
17920 char *spaces = NULL;
17921 if (!(complain & tf_error))
17922 return error_mark_node;
17923 error ("ambiguous class template instantiation for %q#T", type);
17924 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17925 for (t = list; t; t = TREE_CHAIN (t))
17926 {
17927 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17928 spaces = spaces ? spaces : get_spaces (str);
17929 }
17930 free (spaces);
17931 return error_mark_node;
17932 }
17933
17934 return champ;
17935 }
17936
17937 /* Explicitly instantiate DECL. */
17938
17939 void
17940 do_decl_instantiation (tree decl, tree storage)
17941 {
17942 tree result = NULL_TREE;
17943 int extern_p = 0;
17944
17945 if (!decl || decl == error_mark_node)
17946 /* An error occurred, for which grokdeclarator has already issued
17947 an appropriate message. */
17948 return;
17949 else if (! DECL_LANG_SPECIFIC (decl))
17950 {
17951 error ("explicit instantiation of non-template %q#D", decl);
17952 return;
17953 }
17954 else if (TREE_CODE (decl) == VAR_DECL)
17955 {
17956 /* There is an asymmetry here in the way VAR_DECLs and
17957 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17958 the latter, the DECL we get back will be marked as a
17959 template instantiation, and the appropriate
17960 DECL_TEMPLATE_INFO will be set up. This does not happen for
17961 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17962 should handle VAR_DECLs as it currently handles
17963 FUNCTION_DECLs. */
17964 if (!DECL_CLASS_SCOPE_P (decl))
17965 {
17966 error ("%qD is not a static data member of a class template", decl);
17967 return;
17968 }
17969 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17970 if (!result || TREE_CODE (result) != VAR_DECL)
17971 {
17972 error ("no matching template for %qD found", decl);
17973 return;
17974 }
17975 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17976 {
17977 error ("type %qT for explicit instantiation %qD does not match "
17978 "declared type %qT", TREE_TYPE (result), decl,
17979 TREE_TYPE (decl));
17980 return;
17981 }
17982 }
17983 else if (TREE_CODE (decl) != FUNCTION_DECL)
17984 {
17985 error ("explicit instantiation of %q#D", decl);
17986 return;
17987 }
17988 else
17989 result = decl;
17990
17991 /* Check for various error cases. Note that if the explicit
17992 instantiation is valid the RESULT will currently be marked as an
17993 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17994 until we get here. */
17995
17996 if (DECL_TEMPLATE_SPECIALIZATION (result))
17997 {
17998 /* DR 259 [temp.spec].
17999
18000 Both an explicit instantiation and a declaration of an explicit
18001 specialization shall not appear in a program unless the explicit
18002 instantiation follows a declaration of the explicit specialization.
18003
18004 For a given set of template parameters, if an explicit
18005 instantiation of a template appears after a declaration of an
18006 explicit specialization for that template, the explicit
18007 instantiation has no effect. */
18008 return;
18009 }
18010 else if (DECL_EXPLICIT_INSTANTIATION (result))
18011 {
18012 /* [temp.spec]
18013
18014 No program shall explicitly instantiate any template more
18015 than once.
18016
18017 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18018 the first instantiation was `extern' and the second is not,
18019 and EXTERN_P for the opposite case. */
18020 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18021 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18022 /* If an "extern" explicit instantiation follows an ordinary
18023 explicit instantiation, the template is instantiated. */
18024 if (extern_p)
18025 return;
18026 }
18027 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18028 {
18029 error ("no matching template for %qD found", result);
18030 return;
18031 }
18032 else if (!DECL_TEMPLATE_INFO (result))
18033 {
18034 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18035 return;
18036 }
18037
18038 if (storage == NULL_TREE)
18039 ;
18040 else if (storage == ridpointers[(int) RID_EXTERN])
18041 {
18042 if (!in_system_header && (cxx_dialect == cxx98))
18043 pedwarn (input_location, OPT_Wpedantic,
18044 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18045 "instantiations");
18046 extern_p = 1;
18047 }
18048 else
18049 error ("storage class %qD applied to template instantiation", storage);
18050
18051 check_explicit_instantiation_namespace (result);
18052 mark_decl_instantiated (result, extern_p);
18053 if (! extern_p)
18054 instantiate_decl (result, /*defer_ok=*/1,
18055 /*expl_inst_class_mem_p=*/false);
18056 }
18057
18058 static void
18059 mark_class_instantiated (tree t, int extern_p)
18060 {
18061 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18062 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18063 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18064 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18065 if (! extern_p)
18066 {
18067 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18068 rest_of_type_compilation (t, 1);
18069 }
18070 }
18071
18072 /* Called from do_type_instantiation through binding_table_foreach to
18073 do recursive instantiation for the type bound in ENTRY. */
18074 static void
18075 bt_instantiate_type_proc (binding_entry entry, void *data)
18076 {
18077 tree storage = *(tree *) data;
18078
18079 if (MAYBE_CLASS_TYPE_P (entry->type)
18080 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18081 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18082 }
18083
18084 /* Called from do_type_instantiation to instantiate a member
18085 (a member function or a static member variable) of an
18086 explicitly instantiated class template. */
18087 static void
18088 instantiate_class_member (tree decl, int extern_p)
18089 {
18090 mark_decl_instantiated (decl, extern_p);
18091 if (! extern_p)
18092 instantiate_decl (decl, /*defer_ok=*/1,
18093 /*expl_inst_class_mem_p=*/true);
18094 }
18095
18096 /* Perform an explicit instantiation of template class T. STORAGE, if
18097 non-null, is the RID for extern, inline or static. COMPLAIN is
18098 nonzero if this is called from the parser, zero if called recursively,
18099 since the standard is unclear (as detailed below). */
18100
18101 void
18102 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18103 {
18104 int extern_p = 0;
18105 int nomem_p = 0;
18106 int static_p = 0;
18107 int previous_instantiation_extern_p = 0;
18108
18109 if (TREE_CODE (t) == TYPE_DECL)
18110 t = TREE_TYPE (t);
18111
18112 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18113 {
18114 tree tmpl =
18115 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18116 if (tmpl)
18117 error ("explicit instantiation of non-class template %qD", tmpl);
18118 else
18119 error ("explicit instantiation of non-template type %qT", t);
18120 return;
18121 }
18122
18123 complete_type (t);
18124
18125 if (!COMPLETE_TYPE_P (t))
18126 {
18127 if (complain & tf_error)
18128 error ("explicit instantiation of %q#T before definition of template",
18129 t);
18130 return;
18131 }
18132
18133 if (storage != NULL_TREE)
18134 {
18135 if (!in_system_header)
18136 {
18137 if (storage == ridpointers[(int) RID_EXTERN])
18138 {
18139 if (cxx_dialect == cxx98)
18140 pedwarn (input_location, OPT_Wpedantic,
18141 "ISO C++ 1998 forbids the use of %<extern%> on "
18142 "explicit instantiations");
18143 }
18144 else
18145 pedwarn (input_location, OPT_Wpedantic,
18146 "ISO C++ forbids the use of %qE"
18147 " on explicit instantiations", storage);
18148 }
18149
18150 if (storage == ridpointers[(int) RID_INLINE])
18151 nomem_p = 1;
18152 else if (storage == ridpointers[(int) RID_EXTERN])
18153 extern_p = 1;
18154 else if (storage == ridpointers[(int) RID_STATIC])
18155 static_p = 1;
18156 else
18157 {
18158 error ("storage class %qD applied to template instantiation",
18159 storage);
18160 extern_p = 0;
18161 }
18162 }
18163
18164 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18165 {
18166 /* DR 259 [temp.spec].
18167
18168 Both an explicit instantiation and a declaration of an explicit
18169 specialization shall not appear in a program unless the explicit
18170 instantiation follows a declaration of the explicit specialization.
18171
18172 For a given set of template parameters, if an explicit
18173 instantiation of a template appears after a declaration of an
18174 explicit specialization for that template, the explicit
18175 instantiation has no effect. */
18176 return;
18177 }
18178 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18179 {
18180 /* [temp.spec]
18181
18182 No program shall explicitly instantiate any template more
18183 than once.
18184
18185 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18186 instantiation was `extern'. If EXTERN_P then the second is.
18187 These cases are OK. */
18188 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18189
18190 if (!previous_instantiation_extern_p && !extern_p
18191 && (complain & tf_error))
18192 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18193
18194 /* If we've already instantiated the template, just return now. */
18195 if (!CLASSTYPE_INTERFACE_ONLY (t))
18196 return;
18197 }
18198
18199 check_explicit_instantiation_namespace (TYPE_NAME (t));
18200 mark_class_instantiated (t, extern_p);
18201
18202 if (nomem_p)
18203 return;
18204
18205 {
18206 tree tmp;
18207
18208 /* In contrast to implicit instantiation, where only the
18209 declarations, and not the definitions, of members are
18210 instantiated, we have here:
18211
18212 [temp.explicit]
18213
18214 The explicit instantiation of a class template specialization
18215 implies the instantiation of all of its members not
18216 previously explicitly specialized in the translation unit
18217 containing the explicit instantiation.
18218
18219 Of course, we can't instantiate member template classes, since
18220 we don't have any arguments for them. Note that the standard
18221 is unclear on whether the instantiation of the members are
18222 *explicit* instantiations or not. However, the most natural
18223 interpretation is that it should be an explicit instantiation. */
18224
18225 if (! static_p)
18226 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18227 if (TREE_CODE (tmp) == FUNCTION_DECL
18228 && DECL_TEMPLATE_INSTANTIATION (tmp))
18229 instantiate_class_member (tmp, extern_p);
18230
18231 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18232 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
18233 instantiate_class_member (tmp, extern_p);
18234
18235 if (CLASSTYPE_NESTED_UTDS (t))
18236 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18237 bt_instantiate_type_proc, &storage);
18238 }
18239 }
18240
18241 /* Given a function DECL, which is a specialization of TMPL, modify
18242 DECL to be a re-instantiation of TMPL with the same template
18243 arguments. TMPL should be the template into which tsubst'ing
18244 should occur for DECL, not the most general template.
18245
18246 One reason for doing this is a scenario like this:
18247
18248 template <class T>
18249 void f(const T&, int i);
18250
18251 void g() { f(3, 7); }
18252
18253 template <class T>
18254 void f(const T& t, const int i) { }
18255
18256 Note that when the template is first instantiated, with
18257 instantiate_template, the resulting DECL will have no name for the
18258 first parameter, and the wrong type for the second. So, when we go
18259 to instantiate the DECL, we regenerate it. */
18260
18261 static void
18262 regenerate_decl_from_template (tree decl, tree tmpl)
18263 {
18264 /* The arguments used to instantiate DECL, from the most general
18265 template. */
18266 tree args;
18267 tree code_pattern;
18268
18269 args = DECL_TI_ARGS (decl);
18270 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
18271
18272 /* Make sure that we can see identifiers, and compute access
18273 correctly. */
18274 push_access_scope (decl);
18275
18276 if (TREE_CODE (decl) == FUNCTION_DECL)
18277 {
18278 tree decl_parm;
18279 tree pattern_parm;
18280 tree specs;
18281 int args_depth;
18282 int parms_depth;
18283
18284 args_depth = TMPL_ARGS_DEPTH (args);
18285 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
18286 if (args_depth > parms_depth)
18287 args = get_innermost_template_args (args, parms_depth);
18288
18289 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
18290 args, tf_error, NULL_TREE,
18291 /*defer_ok*/false);
18292 if (specs && specs != error_mark_node)
18293 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
18294 specs);
18295
18296 /* Merge parameter declarations. */
18297 decl_parm = skip_artificial_parms_for (decl,
18298 DECL_ARGUMENTS (decl));
18299 pattern_parm
18300 = skip_artificial_parms_for (code_pattern,
18301 DECL_ARGUMENTS (code_pattern));
18302 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
18303 {
18304 tree parm_type;
18305 tree attributes;
18306
18307 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18308 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
18309 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
18310 NULL_TREE);
18311 parm_type = type_decays_to (parm_type);
18312 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18313 TREE_TYPE (decl_parm) = parm_type;
18314 attributes = DECL_ATTRIBUTES (pattern_parm);
18315 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18316 {
18317 DECL_ATTRIBUTES (decl_parm) = attributes;
18318 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18319 }
18320 decl_parm = DECL_CHAIN (decl_parm);
18321 pattern_parm = DECL_CHAIN (pattern_parm);
18322 }
18323 /* Merge any parameters that match with the function parameter
18324 pack. */
18325 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
18326 {
18327 int i, len;
18328 tree expanded_types;
18329 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18330 the parameters in this function parameter pack. */
18331 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
18332 args, tf_error, NULL_TREE);
18333 len = TREE_VEC_LENGTH (expanded_types);
18334 for (i = 0; i < len; i++)
18335 {
18336 tree parm_type;
18337 tree attributes;
18338
18339 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18340 /* Rename the parameter to include the index. */
18341 DECL_NAME (decl_parm) =
18342 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
18343 parm_type = TREE_VEC_ELT (expanded_types, i);
18344 parm_type = type_decays_to (parm_type);
18345 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18346 TREE_TYPE (decl_parm) = parm_type;
18347 attributes = DECL_ATTRIBUTES (pattern_parm);
18348 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18349 {
18350 DECL_ATTRIBUTES (decl_parm) = attributes;
18351 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18352 }
18353 decl_parm = DECL_CHAIN (decl_parm);
18354 }
18355 }
18356 /* Merge additional specifiers from the CODE_PATTERN. */
18357 if (DECL_DECLARED_INLINE_P (code_pattern)
18358 && !DECL_DECLARED_INLINE_P (decl))
18359 DECL_DECLARED_INLINE_P (decl) = 1;
18360 }
18361 else if (TREE_CODE (decl) == VAR_DECL)
18362 {
18363 DECL_INITIAL (decl) =
18364 tsubst_expr (DECL_INITIAL (code_pattern), args,
18365 tf_error, DECL_TI_TEMPLATE (decl),
18366 /*integral_constant_expression_p=*/false);
18367 if (VAR_HAD_UNKNOWN_BOUND (decl))
18368 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18369 tf_error, DECL_TI_TEMPLATE (decl));
18370 }
18371 else
18372 gcc_unreachable ();
18373
18374 pop_access_scope (decl);
18375 }
18376
18377 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18378 substituted to get DECL. */
18379
18380 tree
18381 template_for_substitution (tree decl)
18382 {
18383 tree tmpl = DECL_TI_TEMPLATE (decl);
18384
18385 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18386 for the instantiation. This is not always the most general
18387 template. Consider, for example:
18388
18389 template <class T>
18390 struct S { template <class U> void f();
18391 template <> void f<int>(); };
18392
18393 and an instantiation of S<double>::f<int>. We want TD to be the
18394 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18395 while (/* An instantiation cannot have a definition, so we need a
18396 more general template. */
18397 DECL_TEMPLATE_INSTANTIATION (tmpl)
18398 /* We must also deal with friend templates. Given:
18399
18400 template <class T> struct S {
18401 template <class U> friend void f() {};
18402 };
18403
18404 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18405 so far as the language is concerned, but that's still
18406 where we get the pattern for the instantiation from. On
18407 other hand, if the definition comes outside the class, say:
18408
18409 template <class T> struct S {
18410 template <class U> friend void f();
18411 };
18412 template <class U> friend void f() {}
18413
18414 we don't need to look any further. That's what the check for
18415 DECL_INITIAL is for. */
18416 || (TREE_CODE (decl) == FUNCTION_DECL
18417 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18418 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18419 {
18420 /* The present template, TD, should not be a definition. If it
18421 were a definition, we should be using it! Note that we
18422 cannot restructure the loop to just keep going until we find
18423 a template with a definition, since that might go too far if
18424 a specialization was declared, but not defined. */
18425 gcc_assert (TREE_CODE (decl) != VAR_DECL
18426 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18427
18428 /* Fetch the more general template. */
18429 tmpl = DECL_TI_TEMPLATE (tmpl);
18430 }
18431
18432 return tmpl;
18433 }
18434
18435 /* Returns true if we need to instantiate this template instance even if we
18436 know we aren't going to emit it.. */
18437
18438 bool
18439 always_instantiate_p (tree decl)
18440 {
18441 /* We always instantiate inline functions so that we can inline them. An
18442 explicit instantiation declaration prohibits implicit instantiation of
18443 non-inline functions. With high levels of optimization, we would
18444 normally inline non-inline functions -- but we're not allowed to do
18445 that for "extern template" functions. Therefore, we check
18446 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18447 return ((TREE_CODE (decl) == FUNCTION_DECL
18448 && (DECL_DECLARED_INLINE_P (decl)
18449 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
18450 /* And we need to instantiate static data members so that
18451 their initializers are available in integral constant
18452 expressions. */
18453 || (TREE_CODE (decl) == VAR_DECL
18454 && decl_maybe_constant_var_p (decl)));
18455 }
18456
18457 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18458 instantiate it now, modifying TREE_TYPE (fn). */
18459
18460 void
18461 maybe_instantiate_noexcept (tree fn)
18462 {
18463 tree fntype, spec, noex, clone;
18464
18465 if (DECL_CLONED_FUNCTION_P (fn))
18466 fn = DECL_CLONED_FUNCTION (fn);
18467 fntype = TREE_TYPE (fn);
18468 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18469
18470 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18471 return;
18472
18473 noex = TREE_PURPOSE (spec);
18474
18475 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18476 {
18477 if (push_tinst_level (fn))
18478 {
18479 push_access_scope (fn);
18480 push_deferring_access_checks (dk_no_deferred);
18481 input_location = DECL_SOURCE_LOCATION (fn);
18482 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18483 DEFERRED_NOEXCEPT_ARGS (noex),
18484 tf_warning_or_error, fn,
18485 /*function_p=*/false,
18486 /*integral_constant_expression_p=*/true);
18487 pop_deferring_access_checks ();
18488 pop_access_scope (fn);
18489 pop_tinst_level ();
18490 spec = build_noexcept_spec (noex, tf_warning_or_error);
18491 if (spec == error_mark_node)
18492 spec = noexcept_false_spec;
18493 }
18494 else
18495 spec = noexcept_false_spec;
18496 }
18497 else
18498 {
18499 /* This is an implicitly declared function, so NOEX is a list of
18500 other functions to evaluate and merge. */
18501 tree elt;
18502 spec = noexcept_true_spec;
18503 for (elt = noex; elt; elt = OVL_NEXT (elt))
18504 {
18505 tree fn = OVL_CURRENT (elt);
18506 tree subspec;
18507 maybe_instantiate_noexcept (fn);
18508 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18509 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18510 }
18511 }
18512
18513 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18514
18515 FOR_EACH_CLONE (clone, fn)
18516 {
18517 if (TREE_TYPE (clone) == fntype)
18518 TREE_TYPE (clone) = TREE_TYPE (fn);
18519 else
18520 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18521 }
18522 }
18523
18524 /* Produce the definition of D, a _DECL generated from a template. If
18525 DEFER_OK is nonzero, then we don't have to actually do the
18526 instantiation now; we just have to do it sometime. Normally it is
18527 an error if this is an explicit instantiation but D is undefined.
18528 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18529 explicitly instantiated class template. */
18530
18531 tree
18532 instantiate_decl (tree d, int defer_ok,
18533 bool expl_inst_class_mem_p)
18534 {
18535 tree tmpl = DECL_TI_TEMPLATE (d);
18536 tree gen_args;
18537 tree args;
18538 tree td;
18539 tree code_pattern;
18540 tree spec;
18541 tree gen_tmpl;
18542 bool pattern_defined;
18543 location_t saved_loc = input_location;
18544 bool external_p;
18545 tree fn_context;
18546 bool nested;
18547
18548 /* This function should only be used to instantiate templates for
18549 functions and static member variables. */
18550 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18551 || TREE_CODE (d) == VAR_DECL);
18552
18553 /* Variables are never deferred; if instantiation is required, they
18554 are instantiated right away. That allows for better code in the
18555 case that an expression refers to the value of the variable --
18556 if the variable has a constant value the referring expression can
18557 take advantage of that fact. */
18558 if (TREE_CODE (d) == VAR_DECL
18559 || DECL_DECLARED_CONSTEXPR_P (d))
18560 defer_ok = 0;
18561
18562 /* Don't instantiate cloned functions. Instead, instantiate the
18563 functions they cloned. */
18564 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18565 d = DECL_CLONED_FUNCTION (d);
18566
18567 if (DECL_TEMPLATE_INSTANTIATED (d)
18568 || (TREE_CODE (d) == FUNCTION_DECL
18569 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18570 || DECL_TEMPLATE_SPECIALIZATION (d))
18571 /* D has already been instantiated or explicitly specialized, so
18572 there's nothing for us to do here.
18573
18574 It might seem reasonable to check whether or not D is an explicit
18575 instantiation, and, if so, stop here. But when an explicit
18576 instantiation is deferred until the end of the compilation,
18577 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18578 the instantiation. */
18579 return d;
18580
18581 /* Check to see whether we know that this template will be
18582 instantiated in some other file, as with "extern template"
18583 extension. */
18584 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18585
18586 /* In general, we do not instantiate such templates. */
18587 if (external_p && !always_instantiate_p (d))
18588 return d;
18589
18590 gen_tmpl = most_general_template (tmpl);
18591 gen_args = DECL_TI_ARGS (d);
18592
18593 if (tmpl != gen_tmpl)
18594 /* We should already have the extra args. */
18595 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18596 == TMPL_ARGS_DEPTH (gen_args));
18597 /* And what's in the hash table should match D. */
18598 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18599 || spec == NULL_TREE);
18600
18601 /* This needs to happen before any tsubsting. */
18602 if (! push_tinst_level (d))
18603 return d;
18604
18605 timevar_push (TV_TEMPLATE_INST);
18606
18607 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18608 for the instantiation. */
18609 td = template_for_substitution (d);
18610 code_pattern = DECL_TEMPLATE_RESULT (td);
18611
18612 /* We should never be trying to instantiate a member of a class
18613 template or partial specialization. */
18614 gcc_assert (d != code_pattern);
18615
18616 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18617 || DECL_TEMPLATE_SPECIALIZATION (td))
18618 /* In the case of a friend template whose definition is provided
18619 outside the class, we may have too many arguments. Drop the
18620 ones we don't need. The same is true for specializations. */
18621 args = get_innermost_template_args
18622 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18623 else
18624 args = gen_args;
18625
18626 if (TREE_CODE (d) == FUNCTION_DECL)
18627 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18628 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18629 else
18630 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18631
18632 /* We may be in the middle of deferred access check. Disable it now. */
18633 push_deferring_access_checks (dk_no_deferred);
18634
18635 /* Unless an explicit instantiation directive has already determined
18636 the linkage of D, remember that a definition is available for
18637 this entity. */
18638 if (pattern_defined
18639 && !DECL_INTERFACE_KNOWN (d)
18640 && !DECL_NOT_REALLY_EXTERN (d))
18641 mark_definable (d);
18642
18643 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18644 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18645 input_location = DECL_SOURCE_LOCATION (d);
18646
18647 /* If D is a member of an explicitly instantiated class template,
18648 and no definition is available, treat it like an implicit
18649 instantiation. */
18650 if (!pattern_defined && expl_inst_class_mem_p
18651 && DECL_EXPLICIT_INSTANTIATION (d))
18652 {
18653 /* Leave linkage flags alone on instantiations with anonymous
18654 visibility. */
18655 if (TREE_PUBLIC (d))
18656 {
18657 DECL_NOT_REALLY_EXTERN (d) = 0;
18658 DECL_INTERFACE_KNOWN (d) = 0;
18659 }
18660 SET_DECL_IMPLICIT_INSTANTIATION (d);
18661 }
18662
18663 if (TREE_CODE (d) == FUNCTION_DECL)
18664 maybe_instantiate_noexcept (d);
18665
18666 /* Defer all other templates, unless we have been explicitly
18667 forbidden from doing so. */
18668 if (/* If there is no definition, we cannot instantiate the
18669 template. */
18670 ! pattern_defined
18671 /* If it's OK to postpone instantiation, do so. */
18672 || defer_ok
18673 /* If this is a static data member that will be defined
18674 elsewhere, we don't want to instantiate the entire data
18675 member, but we do want to instantiate the initializer so that
18676 we can substitute that elsewhere. */
18677 || (external_p && TREE_CODE (d) == VAR_DECL))
18678 {
18679 /* The definition of the static data member is now required so
18680 we must substitute the initializer. */
18681 if (TREE_CODE (d) == VAR_DECL
18682 && !DECL_INITIAL (d)
18683 && DECL_INITIAL (code_pattern))
18684 {
18685 tree ns;
18686 tree init;
18687 bool const_init = false;
18688
18689 ns = decl_namespace_context (d);
18690 push_nested_namespace (ns);
18691 push_nested_class (DECL_CONTEXT (d));
18692 init = tsubst_expr (DECL_INITIAL (code_pattern),
18693 args,
18694 tf_warning_or_error, NULL_TREE,
18695 /*integral_constant_expression_p=*/false);
18696 /* Make sure the initializer is still constant, in case of
18697 circular dependency (template/instantiate6.C). */
18698 const_init
18699 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18700 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18701 /*asmspec_tree=*/NULL_TREE,
18702 LOOKUP_ONLYCONVERTING);
18703 pop_nested_class ();
18704 pop_nested_namespace (ns);
18705 }
18706
18707 /* We restore the source position here because it's used by
18708 add_pending_template. */
18709 input_location = saved_loc;
18710
18711 if (at_eof && !pattern_defined
18712 && DECL_EXPLICIT_INSTANTIATION (d)
18713 && DECL_NOT_REALLY_EXTERN (d))
18714 /* [temp.explicit]
18715
18716 The definition of a non-exported function template, a
18717 non-exported member function template, or a non-exported
18718 member function or static data member of a class template
18719 shall be present in every translation unit in which it is
18720 explicitly instantiated. */
18721 permerror (input_location, "explicit instantiation of %qD "
18722 "but no definition available", d);
18723
18724 /* If we're in unevaluated context, we just wanted to get the
18725 constant value; this isn't an odr use, so don't queue
18726 a full instantiation. */
18727 if (cp_unevaluated_operand != 0)
18728 goto out;
18729 /* ??? Historically, we have instantiated inline functions, even
18730 when marked as "extern template". */
18731 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18732 add_pending_template (d);
18733 goto out;
18734 }
18735 /* Tell the repository that D is available in this translation unit
18736 -- and see if it is supposed to be instantiated here. */
18737 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18738 {
18739 /* In a PCH file, despite the fact that the repository hasn't
18740 requested instantiation in the PCH it is still possible that
18741 an instantiation will be required in a file that includes the
18742 PCH. */
18743 if (pch_file)
18744 add_pending_template (d);
18745 /* Instantiate inline functions so that the inliner can do its
18746 job, even though we'll not be emitting a copy of this
18747 function. */
18748 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18749 goto out;
18750 }
18751
18752 fn_context = decl_function_context (d);
18753 nested = (current_function_decl != NULL_TREE);
18754 if (!fn_context)
18755 push_to_top_level ();
18756 else if (nested)
18757 push_function_context ();
18758
18759 /* Mark D as instantiated so that recursive calls to
18760 instantiate_decl do not try to instantiate it again. */
18761 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18762
18763 /* Regenerate the declaration in case the template has been modified
18764 by a subsequent redeclaration. */
18765 regenerate_decl_from_template (d, td);
18766
18767 /* We already set the file and line above. Reset them now in case
18768 they changed as a result of calling regenerate_decl_from_template. */
18769 input_location = DECL_SOURCE_LOCATION (d);
18770
18771 if (TREE_CODE (d) == VAR_DECL)
18772 {
18773 tree init;
18774 bool const_init = false;
18775
18776 /* Clear out DECL_RTL; whatever was there before may not be right
18777 since we've reset the type of the declaration. */
18778 SET_DECL_RTL (d, NULL);
18779 DECL_IN_AGGR_P (d) = 0;
18780
18781 /* The initializer is placed in DECL_INITIAL by
18782 regenerate_decl_from_template so we don't need to
18783 push/pop_access_scope again here. Pull it out so that
18784 cp_finish_decl can process it. */
18785 init = DECL_INITIAL (d);
18786 DECL_INITIAL (d) = NULL_TREE;
18787 DECL_INITIALIZED_P (d) = 0;
18788
18789 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18790 initializer. That function will defer actual emission until
18791 we have a chance to determine linkage. */
18792 DECL_EXTERNAL (d) = 0;
18793
18794 /* Enter the scope of D so that access-checking works correctly. */
18795 push_nested_class (DECL_CONTEXT (d));
18796 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18797 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18798 pop_nested_class ();
18799 }
18800 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18801 synthesize_method (d);
18802 else if (TREE_CODE (d) == FUNCTION_DECL)
18803 {
18804 struct pointer_map_t *saved_local_specializations;
18805 tree subst_decl;
18806 tree tmpl_parm;
18807 tree spec_parm;
18808
18809 /* Save away the current list, in case we are instantiating one
18810 template from within the body of another. */
18811 saved_local_specializations = local_specializations;
18812
18813 /* Set up the list of local specializations. */
18814 local_specializations = pointer_map_create ();
18815
18816 /* Set up context. */
18817 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18818
18819 /* Some typedefs referenced from within the template code need to be
18820 access checked at template instantiation time, i.e now. These
18821 types were added to the template at parsing time. Let's get those
18822 and perform the access checks then. */
18823 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18824 gen_args);
18825
18826 /* Create substitution entries for the parameters. */
18827 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18828 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18829 spec_parm = DECL_ARGUMENTS (d);
18830 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18831 {
18832 register_local_specialization (spec_parm, tmpl_parm);
18833 spec_parm = skip_artificial_parms_for (d, spec_parm);
18834 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18835 }
18836 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18837 {
18838 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18839 {
18840 register_local_specialization (spec_parm, tmpl_parm);
18841 spec_parm = DECL_CHAIN (spec_parm);
18842 }
18843 else
18844 {
18845 /* Register the (value) argument pack as a specialization of
18846 TMPL_PARM, then move on. */
18847 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18848 register_local_specialization (argpack, tmpl_parm);
18849 }
18850 }
18851 gcc_assert (!spec_parm);
18852
18853 /* Substitute into the body of the function. */
18854 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18855 tf_warning_or_error, tmpl,
18856 /*integral_constant_expression_p=*/false);
18857
18858 /* Set the current input_location to the end of the function
18859 so that finish_function knows where we are. */
18860 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18861
18862 /* We don't need the local specializations any more. */
18863 pointer_map_destroy (local_specializations);
18864 local_specializations = saved_local_specializations;
18865
18866 /* Finish the function. */
18867 d = finish_function (0);
18868 expand_or_defer_fn (d);
18869 }
18870
18871 /* We're not deferring instantiation any more. */
18872 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18873
18874 if (!fn_context)
18875 pop_from_top_level ();
18876 else if (nested)
18877 pop_function_context ();
18878
18879 out:
18880 input_location = saved_loc;
18881 pop_deferring_access_checks ();
18882 pop_tinst_level ();
18883
18884 timevar_pop (TV_TEMPLATE_INST);
18885
18886 return d;
18887 }
18888
18889 /* Run through the list of templates that we wish we could
18890 instantiate, and instantiate any we can. RETRIES is the
18891 number of times we retry pending template instantiation. */
18892
18893 void
18894 instantiate_pending_templates (int retries)
18895 {
18896 int reconsider;
18897 location_t saved_loc = input_location;
18898
18899 /* Instantiating templates may trigger vtable generation. This in turn
18900 may require further template instantiations. We place a limit here
18901 to avoid infinite loop. */
18902 if (pending_templates && retries >= max_tinst_depth)
18903 {
18904 tree decl = pending_templates->tinst->decl;
18905
18906 error ("template instantiation depth exceeds maximum of %d"
18907 " instantiating %q+D, possibly from virtual table generation"
18908 " (use -ftemplate-depth= to increase the maximum)",
18909 max_tinst_depth, decl);
18910 if (TREE_CODE (decl) == FUNCTION_DECL)
18911 /* Pretend that we defined it. */
18912 DECL_INITIAL (decl) = error_mark_node;
18913 return;
18914 }
18915
18916 do
18917 {
18918 struct pending_template **t = &pending_templates;
18919 struct pending_template *last = NULL;
18920 reconsider = 0;
18921 while (*t)
18922 {
18923 tree instantiation = reopen_tinst_level ((*t)->tinst);
18924 bool complete = false;
18925
18926 if (TYPE_P (instantiation))
18927 {
18928 tree fn;
18929
18930 if (!COMPLETE_TYPE_P (instantiation))
18931 {
18932 instantiate_class_template (instantiation);
18933 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18934 for (fn = TYPE_METHODS (instantiation);
18935 fn;
18936 fn = TREE_CHAIN (fn))
18937 if (! DECL_ARTIFICIAL (fn))
18938 instantiate_decl (fn,
18939 /*defer_ok=*/0,
18940 /*expl_inst_class_mem_p=*/false);
18941 if (COMPLETE_TYPE_P (instantiation))
18942 reconsider = 1;
18943 }
18944
18945 complete = COMPLETE_TYPE_P (instantiation);
18946 }
18947 else
18948 {
18949 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18950 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18951 {
18952 instantiation
18953 = instantiate_decl (instantiation,
18954 /*defer_ok=*/0,
18955 /*expl_inst_class_mem_p=*/false);
18956 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18957 reconsider = 1;
18958 }
18959
18960 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18961 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18962 }
18963
18964 if (complete)
18965 /* If INSTANTIATION has been instantiated, then we don't
18966 need to consider it again in the future. */
18967 *t = (*t)->next;
18968 else
18969 {
18970 last = *t;
18971 t = &(*t)->next;
18972 }
18973 tinst_depth = 0;
18974 current_tinst_level = NULL;
18975 }
18976 last_pending_template = last;
18977 }
18978 while (reconsider);
18979
18980 input_location = saved_loc;
18981 }
18982
18983 /* Substitute ARGVEC into T, which is a list of initializers for
18984 either base class or a non-static data member. The TREE_PURPOSEs
18985 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18986 instantiate_decl. */
18987
18988 static tree
18989 tsubst_initializer_list (tree t, tree argvec)
18990 {
18991 tree inits = NULL_TREE;
18992
18993 for (; t; t = TREE_CHAIN (t))
18994 {
18995 tree decl;
18996 tree init;
18997 tree expanded_bases = NULL_TREE;
18998 tree expanded_arguments = NULL_TREE;
18999 int i, len = 1;
19000
19001 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19002 {
19003 tree expr;
19004 tree arg;
19005
19006 /* Expand the base class expansion type into separate base
19007 classes. */
19008 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19009 tf_warning_or_error,
19010 NULL_TREE);
19011 if (expanded_bases == error_mark_node)
19012 continue;
19013
19014 /* We'll be building separate TREE_LISTs of arguments for
19015 each base. */
19016 len = TREE_VEC_LENGTH (expanded_bases);
19017 expanded_arguments = make_tree_vec (len);
19018 for (i = 0; i < len; i++)
19019 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19020
19021 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19022 expand each argument in the TREE_VALUE of t. */
19023 expr = make_node (EXPR_PACK_EXPANSION);
19024 PACK_EXPANSION_LOCAL_P (expr) = true;
19025 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19026 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19027
19028 if (TREE_VALUE (t) == void_type_node)
19029 /* VOID_TYPE_NODE is used to indicate
19030 value-initialization. */
19031 {
19032 for (i = 0; i < len; i++)
19033 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19034 }
19035 else
19036 {
19037 /* Substitute parameter packs into each argument in the
19038 TREE_LIST. */
19039 in_base_initializer = 1;
19040 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19041 {
19042 tree expanded_exprs;
19043
19044 /* Expand the argument. */
19045 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19046 expanded_exprs
19047 = tsubst_pack_expansion (expr, argvec,
19048 tf_warning_or_error,
19049 NULL_TREE);
19050 if (expanded_exprs == error_mark_node)
19051 continue;
19052
19053 /* Prepend each of the expanded expressions to the
19054 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19055 for (i = 0; i < len; i++)
19056 {
19057 TREE_VEC_ELT (expanded_arguments, i) =
19058 tree_cons (NULL_TREE,
19059 TREE_VEC_ELT (expanded_exprs, i),
19060 TREE_VEC_ELT (expanded_arguments, i));
19061 }
19062 }
19063 in_base_initializer = 0;
19064
19065 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19066 since we built them backwards. */
19067 for (i = 0; i < len; i++)
19068 {
19069 TREE_VEC_ELT (expanded_arguments, i) =
19070 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19071 }
19072 }
19073 }
19074
19075 for (i = 0; i < len; ++i)
19076 {
19077 if (expanded_bases)
19078 {
19079 decl = TREE_VEC_ELT (expanded_bases, i);
19080 decl = expand_member_init (decl);
19081 init = TREE_VEC_ELT (expanded_arguments, i);
19082 }
19083 else
19084 {
19085 tree tmp;
19086 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19087 tf_warning_or_error, NULL_TREE);
19088
19089 decl = expand_member_init (decl);
19090 if (decl && !DECL_P (decl))
19091 in_base_initializer = 1;
19092
19093 init = TREE_VALUE (t);
19094 tmp = init;
19095 if (init != void_type_node)
19096 init = tsubst_expr (init, argvec,
19097 tf_warning_or_error, NULL_TREE,
19098 /*integral_constant_expression_p=*/false);
19099 if (init == NULL_TREE && tmp != NULL_TREE)
19100 /* If we had an initializer but it instantiated to nothing,
19101 value-initialize the object. This will only occur when
19102 the initializer was a pack expansion where the parameter
19103 packs used in that expansion were of length zero. */
19104 init = void_type_node;
19105 in_base_initializer = 0;
19106 }
19107
19108 if (decl)
19109 {
19110 init = build_tree_list (decl, init);
19111 TREE_CHAIN (init) = inits;
19112 inits = init;
19113 }
19114 }
19115 }
19116 return inits;
19117 }
19118
19119 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19120
19121 static void
19122 set_current_access_from_decl (tree decl)
19123 {
19124 if (TREE_PRIVATE (decl))
19125 current_access_specifier = access_private_node;
19126 else if (TREE_PROTECTED (decl))
19127 current_access_specifier = access_protected_node;
19128 else
19129 current_access_specifier = access_public_node;
19130 }
19131
19132 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19133 is the instantiation (which should have been created with
19134 start_enum) and ARGS are the template arguments to use. */
19135
19136 static void
19137 tsubst_enum (tree tag, tree newtag, tree args)
19138 {
19139 tree e;
19140
19141 if (SCOPED_ENUM_P (newtag))
19142 begin_scope (sk_scoped_enum, newtag);
19143
19144 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19145 {
19146 tree value;
19147 tree decl;
19148
19149 decl = TREE_VALUE (e);
19150 /* Note that in a template enum, the TREE_VALUE is the
19151 CONST_DECL, not the corresponding INTEGER_CST. */
19152 value = tsubst_expr (DECL_INITIAL (decl),
19153 args, tf_warning_or_error, NULL_TREE,
19154 /*integral_constant_expression_p=*/true);
19155
19156 /* Give this enumeration constant the correct access. */
19157 set_current_access_from_decl (decl);
19158
19159 /* Actually build the enumerator itself. */
19160 build_enumerator
19161 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19162 }
19163
19164 if (SCOPED_ENUM_P (newtag))
19165 finish_scope ();
19166
19167 finish_enum_value_list (newtag);
19168 finish_enum (newtag);
19169
19170 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19171 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19172 }
19173
19174 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19175 its type -- but without substituting the innermost set of template
19176 arguments. So, innermost set of template parameters will appear in
19177 the type. */
19178
19179 tree
19180 get_mostly_instantiated_function_type (tree decl)
19181 {
19182 tree fn_type;
19183 tree tmpl;
19184 tree targs;
19185 tree tparms;
19186 int parm_depth;
19187
19188 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19189 targs = DECL_TI_ARGS (decl);
19190 tparms = DECL_TEMPLATE_PARMS (tmpl);
19191 parm_depth = TMPL_PARMS_DEPTH (tparms);
19192
19193 /* There should be as many levels of arguments as there are levels
19194 of parameters. */
19195 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19196
19197 fn_type = TREE_TYPE (tmpl);
19198
19199 if (parm_depth == 1)
19200 /* No substitution is necessary. */
19201 ;
19202 else
19203 {
19204 int i;
19205 tree partial_args;
19206
19207 /* Replace the innermost level of the TARGS with NULL_TREEs to
19208 let tsubst know not to substitute for those parameters. */
19209 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19210 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19211 SET_TMPL_ARGS_LEVEL (partial_args, i,
19212 TMPL_ARGS_LEVEL (targs, i));
19213 SET_TMPL_ARGS_LEVEL (partial_args,
19214 TMPL_ARGS_DEPTH (targs),
19215 make_tree_vec (DECL_NTPARMS (tmpl)));
19216
19217 /* Make sure that we can see identifiers, and compute access
19218 correctly. */
19219 push_access_scope (decl);
19220
19221 ++processing_template_decl;
19222 /* Now, do the (partial) substitution to figure out the
19223 appropriate function type. */
19224 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
19225 --processing_template_decl;
19226
19227 /* Substitute into the template parameters to obtain the real
19228 innermost set of parameters. This step is important if the
19229 innermost set of template parameters contains value
19230 parameters whose types depend on outer template parameters. */
19231 TREE_VEC_LENGTH (partial_args)--;
19232 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
19233
19234 pop_access_scope (decl);
19235 }
19236
19237 return fn_type;
19238 }
19239
19240 /* Return truthvalue if we're processing a template different from
19241 the last one involved in diagnostics. */
19242 int
19243 problematic_instantiation_changed (void)
19244 {
19245 return current_tinst_level != last_error_tinst_level;
19246 }
19247
19248 /* Remember current template involved in diagnostics. */
19249 void
19250 record_last_problematic_instantiation (void)
19251 {
19252 last_error_tinst_level = current_tinst_level;
19253 }
19254
19255 struct tinst_level *
19256 current_instantiation (void)
19257 {
19258 return current_tinst_level;
19259 }
19260
19261 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19262 type. Return zero for ok, nonzero for disallowed. Issue error and
19263 warning messages under control of COMPLAIN. */
19264
19265 static int
19266 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
19267 {
19268 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
19269 return 0;
19270 else if (POINTER_TYPE_P (type))
19271 return 0;
19272 else if (TYPE_PTRMEM_P (type))
19273 return 0;
19274 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
19275 return 0;
19276 else if (TREE_CODE (type) == TYPENAME_TYPE)
19277 return 0;
19278 else if (TREE_CODE (type) == DECLTYPE_TYPE)
19279 return 0;
19280 else if (TREE_CODE (type) == NULLPTR_TYPE)
19281 return 0;
19282
19283 if (complain & tf_error)
19284 {
19285 if (type == error_mark_node)
19286 inform (input_location, "invalid template non-type parameter");
19287 else
19288 error ("%q#T is not a valid type for a template non-type parameter",
19289 type);
19290 }
19291 return 1;
19292 }
19293
19294 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19295 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19296
19297 static bool
19298 dependent_type_p_r (tree type)
19299 {
19300 tree scope;
19301
19302 /* [temp.dep.type]
19303
19304 A type is dependent if it is:
19305
19306 -- a template parameter. Template template parameters are types
19307 for us (since TYPE_P holds true for them) so we handle
19308 them here. */
19309 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19310 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
19311 return true;
19312 /* -- a qualified-id with a nested-name-specifier which contains a
19313 class-name that names a dependent type or whose unqualified-id
19314 names a dependent type. */
19315 if (TREE_CODE (type) == TYPENAME_TYPE)
19316 return true;
19317 /* -- a cv-qualified type where the cv-unqualified type is
19318 dependent. */
19319 type = TYPE_MAIN_VARIANT (type);
19320 /* -- a compound type constructed from any dependent type. */
19321 if (TYPE_PTRMEM_P (type))
19322 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
19323 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19324 (type)));
19325 else if (TREE_CODE (type) == POINTER_TYPE
19326 || TREE_CODE (type) == REFERENCE_TYPE)
19327 return dependent_type_p (TREE_TYPE (type));
19328 else if (TREE_CODE (type) == FUNCTION_TYPE
19329 || TREE_CODE (type) == METHOD_TYPE)
19330 {
19331 tree arg_type;
19332
19333 if (dependent_type_p (TREE_TYPE (type)))
19334 return true;
19335 for (arg_type = TYPE_ARG_TYPES (type);
19336 arg_type;
19337 arg_type = TREE_CHAIN (arg_type))
19338 if (dependent_type_p (TREE_VALUE (arg_type)))
19339 return true;
19340 return false;
19341 }
19342 /* -- an array type constructed from any dependent type or whose
19343 size is specified by a constant expression that is
19344 value-dependent.
19345
19346 We checked for type- and value-dependence of the bounds in
19347 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19348 if (TREE_CODE (type) == ARRAY_TYPE)
19349 {
19350 if (TYPE_DOMAIN (type)
19351 && dependent_type_p (TYPE_DOMAIN (type)))
19352 return true;
19353 return dependent_type_p (TREE_TYPE (type));
19354 }
19355
19356 /* -- a template-id in which either the template name is a template
19357 parameter ... */
19358 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19359 return true;
19360 /* ... or any of the template arguments is a dependent type or
19361 an expression that is type-dependent or value-dependent. */
19362 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19363 && (any_dependent_template_arguments_p
19364 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19365 return true;
19366
19367 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19368 dependent; if the argument of the `typeof' expression is not
19369 type-dependent, then it should already been have resolved. */
19370 if (TREE_CODE (type) == TYPEOF_TYPE
19371 || TREE_CODE (type) == DECLTYPE_TYPE
19372 || TREE_CODE (type) == UNDERLYING_TYPE)
19373 return true;
19374
19375 /* A template argument pack is dependent if any of its packed
19376 arguments are. */
19377 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19378 {
19379 tree args = ARGUMENT_PACK_ARGS (type);
19380 int i, len = TREE_VEC_LENGTH (args);
19381 for (i = 0; i < len; ++i)
19382 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19383 return true;
19384 }
19385
19386 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19387 be template parameters. */
19388 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19389 return true;
19390
19391 /* The standard does not specifically mention types that are local
19392 to template functions or local classes, but they should be
19393 considered dependent too. For example:
19394
19395 template <int I> void f() {
19396 enum E { a = I };
19397 S<sizeof (E)> s;
19398 }
19399
19400 The size of `E' cannot be known until the value of `I' has been
19401 determined. Therefore, `E' must be considered dependent. */
19402 scope = TYPE_CONTEXT (type);
19403 if (scope && TYPE_P (scope))
19404 return dependent_type_p (scope);
19405 /* Don't use type_dependent_expression_p here, as it can lead
19406 to infinite recursion trying to determine whether a lambda
19407 nested in a lambda is dependent (c++/47687). */
19408 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19409 && DECL_LANG_SPECIFIC (scope)
19410 && DECL_TEMPLATE_INFO (scope)
19411 && (any_dependent_template_arguments_p
19412 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19413 return true;
19414
19415 /* Other types are non-dependent. */
19416 return false;
19417 }
19418
19419 /* Returns TRUE if TYPE is dependent, in the sense of
19420 [temp.dep.type]. Note that a NULL type is considered dependent. */
19421
19422 bool
19423 dependent_type_p (tree type)
19424 {
19425 /* If there are no template parameters in scope, then there can't be
19426 any dependent types. */
19427 if (!processing_template_decl)
19428 {
19429 /* If we are not processing a template, then nobody should be
19430 providing us with a dependent type. */
19431 gcc_assert (type);
19432 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19433 return false;
19434 }
19435
19436 /* If the type is NULL, we have not computed a type for the entity
19437 in question; in that case, the type is dependent. */
19438 if (!type)
19439 return true;
19440
19441 /* Erroneous types can be considered non-dependent. */
19442 if (type == error_mark_node)
19443 return false;
19444
19445 /* If we have not already computed the appropriate value for TYPE,
19446 do so now. */
19447 if (!TYPE_DEPENDENT_P_VALID (type))
19448 {
19449 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19450 TYPE_DEPENDENT_P_VALID (type) = 1;
19451 }
19452
19453 return TYPE_DEPENDENT_P (type);
19454 }
19455
19456 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19457 lookup. In other words, a dependent type that is not the current
19458 instantiation. */
19459
19460 bool
19461 dependent_scope_p (tree scope)
19462 {
19463 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19464 && !currently_open_class (scope));
19465 }
19466
19467 /* T is a SCOPE_REF; return whether we need to consider it
19468 instantiation-dependent so that we can check access at instantiation
19469 time even though we know which member it resolves to. */
19470
19471 static bool
19472 instantiation_dependent_scope_ref_p (tree t)
19473 {
19474 if (DECL_P (TREE_OPERAND (t, 1))
19475 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
19476 && accessible_in_template_p (TREE_OPERAND (t, 0),
19477 TREE_OPERAND (t, 1)))
19478 return false;
19479 else
19480 return true;
19481 }
19482
19483 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19484 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19485 expression. */
19486
19487 /* Note that this predicate is not appropriate for general expressions;
19488 only constant expressions (that satisfy potential_constant_expression)
19489 can be tested for value dependence. */
19490
19491 bool
19492 value_dependent_expression_p (tree expression)
19493 {
19494 if (!processing_template_decl)
19495 return false;
19496
19497 /* A name declared with a dependent type. */
19498 if (DECL_P (expression) && type_dependent_expression_p (expression))
19499 return true;
19500
19501 switch (TREE_CODE (expression))
19502 {
19503 case IDENTIFIER_NODE:
19504 /* A name that has not been looked up -- must be dependent. */
19505 return true;
19506
19507 case TEMPLATE_PARM_INDEX:
19508 /* A non-type template parm. */
19509 return true;
19510
19511 case CONST_DECL:
19512 /* A non-type template parm. */
19513 if (DECL_TEMPLATE_PARM_P (expression))
19514 return true;
19515 return value_dependent_expression_p (DECL_INITIAL (expression));
19516
19517 case VAR_DECL:
19518 /* A constant with literal type and is initialized
19519 with an expression that is value-dependent.
19520
19521 Note that a non-dependent parenthesized initializer will have
19522 already been replaced with its constant value, so if we see
19523 a TREE_LIST it must be dependent. */
19524 if (DECL_INITIAL (expression)
19525 && decl_constant_var_p (expression)
19526 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
19527 || value_dependent_expression_p (DECL_INITIAL (expression))))
19528 return true;
19529 return false;
19530
19531 case DYNAMIC_CAST_EXPR:
19532 case STATIC_CAST_EXPR:
19533 case CONST_CAST_EXPR:
19534 case REINTERPRET_CAST_EXPR:
19535 case CAST_EXPR:
19536 /* These expressions are value-dependent if the type to which
19537 the cast occurs is dependent or the expression being casted
19538 is value-dependent. */
19539 {
19540 tree type = TREE_TYPE (expression);
19541
19542 if (dependent_type_p (type))
19543 return true;
19544
19545 /* A functional cast has a list of operands. */
19546 expression = TREE_OPERAND (expression, 0);
19547 if (!expression)
19548 {
19549 /* If there are no operands, it must be an expression such
19550 as "int()". This should not happen for aggregate types
19551 because it would form non-constant expressions. */
19552 gcc_assert (cxx_dialect >= cxx0x
19553 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19554
19555 return false;
19556 }
19557
19558 if (TREE_CODE (expression) == TREE_LIST)
19559 return any_value_dependent_elements_p (expression);
19560
19561 return value_dependent_expression_p (expression);
19562 }
19563
19564 case SIZEOF_EXPR:
19565 if (SIZEOF_EXPR_TYPE_P (expression))
19566 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
19567 /* FALLTHRU */
19568 case ALIGNOF_EXPR:
19569 case TYPEID_EXPR:
19570 /* A `sizeof' expression is value-dependent if the operand is
19571 type-dependent or is a pack expansion. */
19572 expression = TREE_OPERAND (expression, 0);
19573 if (PACK_EXPANSION_P (expression))
19574 return true;
19575 else if (TYPE_P (expression))
19576 return dependent_type_p (expression);
19577 return instantiation_dependent_expression_p (expression);
19578
19579 case AT_ENCODE_EXPR:
19580 /* An 'encode' expression is value-dependent if the operand is
19581 type-dependent. */
19582 expression = TREE_OPERAND (expression, 0);
19583 return dependent_type_p (expression);
19584
19585 case NOEXCEPT_EXPR:
19586 expression = TREE_OPERAND (expression, 0);
19587 return instantiation_dependent_expression_p (expression);
19588
19589 case SCOPE_REF:
19590 /* All instantiation-dependent expressions should also be considered
19591 value-dependent. */
19592 return instantiation_dependent_scope_ref_p (expression);
19593
19594 case COMPONENT_REF:
19595 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19596 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19597
19598 case NONTYPE_ARGUMENT_PACK:
19599 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19600 is value-dependent. */
19601 {
19602 tree values = ARGUMENT_PACK_ARGS (expression);
19603 int i, len = TREE_VEC_LENGTH (values);
19604
19605 for (i = 0; i < len; ++i)
19606 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19607 return true;
19608
19609 return false;
19610 }
19611
19612 case TRAIT_EXPR:
19613 {
19614 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19615 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19616 || (type2 ? dependent_type_p (type2) : false));
19617 }
19618
19619 case MODOP_EXPR:
19620 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19621 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19622
19623 case ARRAY_REF:
19624 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19625 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19626
19627 case ADDR_EXPR:
19628 {
19629 tree op = TREE_OPERAND (expression, 0);
19630 return (value_dependent_expression_p (op)
19631 || has_value_dependent_address (op));
19632 }
19633
19634 case CALL_EXPR:
19635 {
19636 tree fn = get_callee_fndecl (expression);
19637 int i, nargs;
19638 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19639 return true;
19640 nargs = call_expr_nargs (expression);
19641 for (i = 0; i < nargs; ++i)
19642 {
19643 tree op = CALL_EXPR_ARG (expression, i);
19644 /* In a call to a constexpr member function, look through the
19645 implicit ADDR_EXPR on the object argument so that it doesn't
19646 cause the call to be considered value-dependent. We also
19647 look through it in potential_constant_expression. */
19648 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19649 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19650 && TREE_CODE (op) == ADDR_EXPR)
19651 op = TREE_OPERAND (op, 0);
19652 if (value_dependent_expression_p (op))
19653 return true;
19654 }
19655 return false;
19656 }
19657
19658 case TEMPLATE_ID_EXPR:
19659 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19660 type-dependent. */
19661 return type_dependent_expression_p (expression);
19662
19663 case CONSTRUCTOR:
19664 {
19665 unsigned ix;
19666 tree val;
19667 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19668 if (value_dependent_expression_p (val))
19669 return true;
19670 return false;
19671 }
19672
19673 case STMT_EXPR:
19674 /* Treat a GNU statement expression as dependent to avoid crashing
19675 under fold_non_dependent_expr; it can't be constant. */
19676 return true;
19677
19678 default:
19679 /* A constant expression is value-dependent if any subexpression is
19680 value-dependent. */
19681 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19682 {
19683 case tcc_reference:
19684 case tcc_unary:
19685 case tcc_comparison:
19686 case tcc_binary:
19687 case tcc_expression:
19688 case tcc_vl_exp:
19689 {
19690 int i, len = cp_tree_operand_length (expression);
19691
19692 for (i = 0; i < len; i++)
19693 {
19694 tree t = TREE_OPERAND (expression, i);
19695
19696 /* In some cases, some of the operands may be missing.l
19697 (For example, in the case of PREDECREMENT_EXPR, the
19698 amount to increment by may be missing.) That doesn't
19699 make the expression dependent. */
19700 if (t && value_dependent_expression_p (t))
19701 return true;
19702 }
19703 }
19704 break;
19705 default:
19706 break;
19707 }
19708 break;
19709 }
19710
19711 /* The expression is not value-dependent. */
19712 return false;
19713 }
19714
19715 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19716 [temp.dep.expr]. Note that an expression with no type is
19717 considered dependent. Other parts of the compiler arrange for an
19718 expression with type-dependent subexpressions to have no type, so
19719 this function doesn't have to be fully recursive. */
19720
19721 bool
19722 type_dependent_expression_p (tree expression)
19723 {
19724 if (!processing_template_decl)
19725 return false;
19726
19727 if (expression == error_mark_node)
19728 return false;
19729
19730 /* An unresolved name is always dependent. */
19731 if (TREE_CODE (expression) == IDENTIFIER_NODE
19732 || TREE_CODE (expression) == USING_DECL)
19733 return true;
19734
19735 /* Some expression forms are never type-dependent. */
19736 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19737 || TREE_CODE (expression) == SIZEOF_EXPR
19738 || TREE_CODE (expression) == ALIGNOF_EXPR
19739 || TREE_CODE (expression) == AT_ENCODE_EXPR
19740 || TREE_CODE (expression) == NOEXCEPT_EXPR
19741 || TREE_CODE (expression) == TRAIT_EXPR
19742 || TREE_CODE (expression) == TYPEID_EXPR
19743 || TREE_CODE (expression) == DELETE_EXPR
19744 || TREE_CODE (expression) == VEC_DELETE_EXPR
19745 || TREE_CODE (expression) == THROW_EXPR)
19746 return false;
19747
19748 /* The types of these expressions depends only on the type to which
19749 the cast occurs. */
19750 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19751 || TREE_CODE (expression) == STATIC_CAST_EXPR
19752 || TREE_CODE (expression) == CONST_CAST_EXPR
19753 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19754 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19755 || TREE_CODE (expression) == CAST_EXPR)
19756 return dependent_type_p (TREE_TYPE (expression));
19757
19758 /* The types of these expressions depends only on the type created
19759 by the expression. */
19760 if (TREE_CODE (expression) == NEW_EXPR
19761 || TREE_CODE (expression) == VEC_NEW_EXPR)
19762 {
19763 /* For NEW_EXPR tree nodes created inside a template, either
19764 the object type itself or a TREE_LIST may appear as the
19765 operand 1. */
19766 tree type = TREE_OPERAND (expression, 1);
19767 if (TREE_CODE (type) == TREE_LIST)
19768 /* This is an array type. We need to check array dimensions
19769 as well. */
19770 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19771 || value_dependent_expression_p
19772 (TREE_OPERAND (TREE_VALUE (type), 1));
19773 else
19774 return dependent_type_p (type);
19775 }
19776
19777 if (TREE_CODE (expression) == SCOPE_REF)
19778 {
19779 tree scope = TREE_OPERAND (expression, 0);
19780 tree name = TREE_OPERAND (expression, 1);
19781
19782 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19783 contains an identifier associated by name lookup with one or more
19784 declarations declared with a dependent type, or...a
19785 nested-name-specifier or qualified-id that names a member of an
19786 unknown specialization. */
19787 return (type_dependent_expression_p (name)
19788 || dependent_scope_p (scope));
19789 }
19790
19791 if (TREE_CODE (expression) == FUNCTION_DECL
19792 && DECL_LANG_SPECIFIC (expression)
19793 && DECL_TEMPLATE_INFO (expression)
19794 && (any_dependent_template_arguments_p
19795 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19796 return true;
19797
19798 if (TREE_CODE (expression) == TEMPLATE_DECL
19799 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19800 return false;
19801
19802 if (TREE_CODE (expression) == STMT_EXPR)
19803 expression = stmt_expr_value_expr (expression);
19804
19805 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19806 {
19807 tree elt;
19808 unsigned i;
19809
19810 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19811 {
19812 if (type_dependent_expression_p (elt))
19813 return true;
19814 }
19815 return false;
19816 }
19817
19818 /* A static data member of the current instantiation with incomplete
19819 array type is type-dependent, as the definition and specializations
19820 can have different bounds. */
19821 if (TREE_CODE (expression) == VAR_DECL
19822 && DECL_CLASS_SCOPE_P (expression)
19823 && dependent_type_p (DECL_CONTEXT (expression))
19824 && VAR_HAD_UNKNOWN_BOUND (expression))
19825 return true;
19826
19827 if (TREE_TYPE (expression) == unknown_type_node)
19828 {
19829 if (TREE_CODE (expression) == ADDR_EXPR)
19830 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19831 if (TREE_CODE (expression) == COMPONENT_REF
19832 || TREE_CODE (expression) == OFFSET_REF)
19833 {
19834 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19835 return true;
19836 expression = TREE_OPERAND (expression, 1);
19837 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19838 return false;
19839 }
19840 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19841 if (TREE_CODE (expression) == SCOPE_REF)
19842 return false;
19843
19844 if (BASELINK_P (expression))
19845 expression = BASELINK_FUNCTIONS (expression);
19846
19847 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19848 {
19849 if (any_dependent_template_arguments_p
19850 (TREE_OPERAND (expression, 1)))
19851 return true;
19852 expression = TREE_OPERAND (expression, 0);
19853 }
19854 gcc_assert (TREE_CODE (expression) == OVERLOAD
19855 || TREE_CODE (expression) == FUNCTION_DECL);
19856
19857 while (expression)
19858 {
19859 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19860 return true;
19861 expression = OVL_NEXT (expression);
19862 }
19863 return false;
19864 }
19865
19866 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19867
19868 return (dependent_type_p (TREE_TYPE (expression)));
19869 }
19870
19871 /* walk_tree callback function for instantiation_dependent_expression_p,
19872 below. Returns non-zero if a dependent subexpression is found. */
19873
19874 static tree
19875 instantiation_dependent_r (tree *tp, int *walk_subtrees,
19876 void * /*data*/)
19877 {
19878 if (TYPE_P (*tp))
19879 {
19880 /* We don't have to worry about decltype currently because decltype
19881 of an instantiation-dependent expr is a dependent type. This
19882 might change depending on the resolution of DR 1172. */
19883 *walk_subtrees = false;
19884 return NULL_TREE;
19885 }
19886 enum tree_code code = TREE_CODE (*tp);
19887 switch (code)
19888 {
19889 /* Don't treat an argument list as dependent just because it has no
19890 TREE_TYPE. */
19891 case TREE_LIST:
19892 case TREE_VEC:
19893 return NULL_TREE;
19894
19895 case TEMPLATE_PARM_INDEX:
19896 return *tp;
19897
19898 /* Handle expressions with type operands. */
19899 case SIZEOF_EXPR:
19900 case ALIGNOF_EXPR:
19901 case TYPEID_EXPR:
19902 case AT_ENCODE_EXPR:
19903 {
19904 tree op = TREE_OPERAND (*tp, 0);
19905 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
19906 op = TREE_TYPE (op);
19907 if (TYPE_P (op))
19908 {
19909 if (dependent_type_p (op))
19910 return *tp;
19911 else
19912 {
19913 *walk_subtrees = false;
19914 return NULL_TREE;
19915 }
19916 }
19917 break;
19918 }
19919
19920 case TRAIT_EXPR:
19921 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
19922 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp)))
19923 return *tp;
19924 *walk_subtrees = false;
19925 return NULL_TREE;
19926
19927 case COMPONENT_REF:
19928 if (TREE_CODE (TREE_OPERAND (*tp, 1)) == IDENTIFIER_NODE)
19929 /* In a template, finish_class_member_access_expr creates a
19930 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19931 type-dependent, so that we can check access control at
19932 instantiation time (PR 42277). See also Core issue 1273. */
19933 return *tp;
19934 break;
19935
19936 case SCOPE_REF:
19937 if (instantiation_dependent_scope_ref_p (*tp))
19938 return *tp;
19939 else
19940 break;
19941
19942 /* Treat statement-expressions as dependent. */
19943 case BIND_EXPR:
19944 return *tp;
19945
19946 default:
19947 break;
19948 }
19949
19950 if (type_dependent_expression_p (*tp))
19951 return *tp;
19952 else
19953 return NULL_TREE;
19954 }
19955
19956 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19957 sense defined by the ABI:
19958
19959 "An expression is instantiation-dependent if it is type-dependent
19960 or value-dependent, or it has a subexpression that is type-dependent
19961 or value-dependent." */
19962
19963 bool
19964 instantiation_dependent_expression_p (tree expression)
19965 {
19966 tree result;
19967
19968 if (!processing_template_decl)
19969 return false;
19970
19971 if (expression == error_mark_node)
19972 return false;
19973
19974 result = cp_walk_tree_without_duplicates (&expression,
19975 instantiation_dependent_r, NULL);
19976 return result != NULL_TREE;
19977 }
19978
19979 /* Like type_dependent_expression_p, but it also works while not processing
19980 a template definition, i.e. during substitution or mangling. */
19981
19982 bool
19983 type_dependent_expression_p_push (tree expr)
19984 {
19985 bool b;
19986 ++processing_template_decl;
19987 b = type_dependent_expression_p (expr);
19988 --processing_template_decl;
19989 return b;
19990 }
19991
19992 /* Returns TRUE if ARGS contains a type-dependent expression. */
19993
19994 bool
19995 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
19996 {
19997 unsigned int i;
19998 tree arg;
19999
20000 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20001 {
20002 if (type_dependent_expression_p (arg))
20003 return true;
20004 }
20005 return false;
20006 }
20007
20008 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20009 expressions) contains any type-dependent expressions. */
20010
20011 bool
20012 any_type_dependent_elements_p (const_tree list)
20013 {
20014 for (; list; list = TREE_CHAIN (list))
20015 if (value_dependent_expression_p (TREE_VALUE (list)))
20016 return true;
20017
20018 return false;
20019 }
20020
20021 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20022 expressions) contains any value-dependent expressions. */
20023
20024 bool
20025 any_value_dependent_elements_p (const_tree list)
20026 {
20027 for (; list; list = TREE_CHAIN (list))
20028 if (value_dependent_expression_p (TREE_VALUE (list)))
20029 return true;
20030
20031 return false;
20032 }
20033
20034 /* Returns TRUE if the ARG (a template argument) is dependent. */
20035
20036 bool
20037 dependent_template_arg_p (tree arg)
20038 {
20039 if (!processing_template_decl)
20040 return false;
20041
20042 /* Assume a template argument that was wrongly written by the user
20043 is dependent. This is consistent with what
20044 any_dependent_template_arguments_p [that calls this function]
20045 does. */
20046 if (!arg || arg == error_mark_node)
20047 return true;
20048
20049 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20050 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20051
20052 if (TREE_CODE (arg) == TEMPLATE_DECL
20053 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20054 return dependent_template_p (arg);
20055 else if (ARGUMENT_PACK_P (arg))
20056 {
20057 tree args = ARGUMENT_PACK_ARGS (arg);
20058 int i, len = TREE_VEC_LENGTH (args);
20059 for (i = 0; i < len; ++i)
20060 {
20061 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20062 return true;
20063 }
20064
20065 return false;
20066 }
20067 else if (TYPE_P (arg))
20068 return dependent_type_p (arg);
20069 else
20070 return (type_dependent_expression_p (arg)
20071 || value_dependent_expression_p (arg));
20072 }
20073
20074 /* Returns true if ARGS (a collection of template arguments) contains
20075 any types that require structural equality testing. */
20076
20077 bool
20078 any_template_arguments_need_structural_equality_p (tree args)
20079 {
20080 int i;
20081 int j;
20082
20083 if (!args)
20084 return false;
20085 if (args == error_mark_node)
20086 return true;
20087
20088 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20089 {
20090 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20091 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20092 {
20093 tree arg = TREE_VEC_ELT (level, j);
20094 tree packed_args = NULL_TREE;
20095 int k, len = 1;
20096
20097 if (ARGUMENT_PACK_P (arg))
20098 {
20099 /* Look inside the argument pack. */
20100 packed_args = ARGUMENT_PACK_ARGS (arg);
20101 len = TREE_VEC_LENGTH (packed_args);
20102 }
20103
20104 for (k = 0; k < len; ++k)
20105 {
20106 if (packed_args)
20107 arg = TREE_VEC_ELT (packed_args, k);
20108
20109 if (error_operand_p (arg))
20110 return true;
20111 else if (TREE_CODE (arg) == TEMPLATE_DECL
20112 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20113 continue;
20114 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20115 return true;
20116 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20117 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20118 return true;
20119 }
20120 }
20121 }
20122
20123 return false;
20124 }
20125
20126 /* Returns true if ARGS (a collection of template arguments) contains
20127 any dependent arguments. */
20128
20129 bool
20130 any_dependent_template_arguments_p (const_tree args)
20131 {
20132 int i;
20133 int j;
20134
20135 if (!args)
20136 return false;
20137 if (args == error_mark_node)
20138 return true;
20139
20140 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20141 {
20142 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20143 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20144 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20145 return true;
20146 }
20147
20148 return false;
20149 }
20150
20151 /* Returns TRUE if the template TMPL is dependent. */
20152
20153 bool
20154 dependent_template_p (tree tmpl)
20155 {
20156 if (TREE_CODE (tmpl) == OVERLOAD)
20157 {
20158 while (tmpl)
20159 {
20160 if (dependent_template_p (OVL_CURRENT (tmpl)))
20161 return true;
20162 tmpl = OVL_NEXT (tmpl);
20163 }
20164 return false;
20165 }
20166
20167 /* Template template parameters are dependent. */
20168 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20169 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20170 return true;
20171 /* So are names that have not been looked up. */
20172 if (TREE_CODE (tmpl) == SCOPE_REF
20173 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
20174 return true;
20175 /* So are member templates of dependent classes. */
20176 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20177 return dependent_type_p (DECL_CONTEXT (tmpl));
20178 return false;
20179 }
20180
20181 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20182
20183 bool
20184 dependent_template_id_p (tree tmpl, tree args)
20185 {
20186 return (dependent_template_p (tmpl)
20187 || any_dependent_template_arguments_p (args));
20188 }
20189
20190 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20191 is dependent. */
20192
20193 bool
20194 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
20195 {
20196 int i;
20197
20198 if (!processing_template_decl)
20199 return false;
20200
20201 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
20202 {
20203 tree decl = TREE_VEC_ELT (declv, i);
20204 tree init = TREE_VEC_ELT (initv, i);
20205 tree cond = TREE_VEC_ELT (condv, i);
20206 tree incr = TREE_VEC_ELT (incrv, i);
20207
20208 if (type_dependent_expression_p (decl))
20209 return true;
20210
20211 if (init && type_dependent_expression_p (init))
20212 return true;
20213
20214 if (type_dependent_expression_p (cond))
20215 return true;
20216
20217 if (COMPARISON_CLASS_P (cond)
20218 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
20219 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
20220 return true;
20221
20222 if (TREE_CODE (incr) == MODOP_EXPR)
20223 {
20224 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
20225 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
20226 return true;
20227 }
20228 else if (type_dependent_expression_p (incr))
20229 return true;
20230 else if (TREE_CODE (incr) == MODIFY_EXPR)
20231 {
20232 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
20233 return true;
20234 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
20235 {
20236 tree t = TREE_OPERAND (incr, 1);
20237 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
20238 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
20239 return true;
20240 }
20241 }
20242 }
20243
20244 return false;
20245 }
20246
20247 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20248 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20249 no such TYPE can be found. Note that this function peers inside
20250 uninstantiated templates and therefore should be used only in
20251 extremely limited situations. ONLY_CURRENT_P restricts this
20252 peering to the currently open classes hierarchy (which is required
20253 when comparing types). */
20254
20255 tree
20256 resolve_typename_type (tree type, bool only_current_p)
20257 {
20258 tree scope;
20259 tree name;
20260 tree decl;
20261 int quals;
20262 tree pushed_scope;
20263 tree result;
20264
20265 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
20266
20267 scope = TYPE_CONTEXT (type);
20268 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20269 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20270 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20271 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20272 identifier of the TYPENAME_TYPE anymore.
20273 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20274 TYPENAME_TYPE instead, we avoid messing up with a possible
20275 typedef variant case. */
20276 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
20277
20278 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20279 it first before we can figure out what NAME refers to. */
20280 if (TREE_CODE (scope) == TYPENAME_TYPE)
20281 {
20282 if (TYPENAME_IS_RESOLVING_P (scope))
20283 /* Given a class template A with a dependent base with nested type C,
20284 typedef typename A::C::C C will land us here, as trying to resolve
20285 the initial A::C leads to the local C typedef, which leads back to
20286 A::C::C. So we break the recursion now. */
20287 return type;
20288 else
20289 scope = resolve_typename_type (scope, only_current_p);
20290 }
20291 /* If we don't know what SCOPE refers to, then we cannot resolve the
20292 TYPENAME_TYPE. */
20293 if (TREE_CODE (scope) == TYPENAME_TYPE)
20294 return type;
20295 /* If the SCOPE is a template type parameter, we have no way of
20296 resolving the name. */
20297 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
20298 return type;
20299 /* If the SCOPE is not the current instantiation, there's no reason
20300 to look inside it. */
20301 if (only_current_p && !currently_open_class (scope))
20302 return type;
20303 /* If this is a typedef, we don't want to look inside (c++/11987). */
20304 if (typedef_variant_p (type))
20305 return type;
20306 /* If SCOPE isn't the template itself, it will not have a valid
20307 TYPE_FIELDS list. */
20308 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
20309 /* scope is either the template itself or a compatible instantiation
20310 like X<T>, so look up the name in the original template. */
20311 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
20312 else
20313 /* scope is a partial instantiation, so we can't do the lookup or we
20314 will lose the template arguments. */
20315 return type;
20316 /* Enter the SCOPE so that name lookup will be resolved as if we
20317 were in the class definition. In particular, SCOPE will no
20318 longer be considered a dependent type. */
20319 pushed_scope = push_scope (scope);
20320 /* Look up the declaration. */
20321 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
20322 tf_warning_or_error);
20323
20324 result = NULL_TREE;
20325
20326 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20327 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20328 if (!decl)
20329 /*nop*/;
20330 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
20331 && TREE_CODE (decl) == TYPE_DECL)
20332 {
20333 result = TREE_TYPE (decl);
20334 if (result == error_mark_node)
20335 result = NULL_TREE;
20336 }
20337 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
20338 && DECL_CLASS_TEMPLATE_P (decl))
20339 {
20340 tree tmpl;
20341 tree args;
20342 /* Obtain the template and the arguments. */
20343 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
20344 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
20345 /* Instantiate the template. */
20346 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
20347 /*entering_scope=*/0,
20348 tf_error | tf_user);
20349 if (result == error_mark_node)
20350 result = NULL_TREE;
20351 }
20352
20353 /* Leave the SCOPE. */
20354 if (pushed_scope)
20355 pop_scope (pushed_scope);
20356
20357 /* If we failed to resolve it, return the original typename. */
20358 if (!result)
20359 return type;
20360
20361 /* If lookup found a typename type, resolve that too. */
20362 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
20363 {
20364 /* Ill-formed programs can cause infinite recursion here, so we
20365 must catch that. */
20366 TYPENAME_IS_RESOLVING_P (type) = 1;
20367 result = resolve_typename_type (result, only_current_p);
20368 TYPENAME_IS_RESOLVING_P (type) = 0;
20369 }
20370
20371 /* Qualify the resulting type. */
20372 quals = cp_type_quals (type);
20373 if (quals)
20374 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
20375
20376 return result;
20377 }
20378
20379 /* EXPR is an expression which is not type-dependent. Return a proxy
20380 for EXPR that can be used to compute the types of larger
20381 expressions containing EXPR. */
20382
20383 tree
20384 build_non_dependent_expr (tree expr)
20385 {
20386 tree inner_expr;
20387
20388 #ifdef ENABLE_CHECKING
20389 /* Try to get a constant value for all non-dependent expressions in
20390 order to expose bugs in *_dependent_expression_p and constexpr. */
20391 if (cxx_dialect >= cxx0x
20392 && !instantiation_dependent_expression_p (expr))
20393 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
20394 #endif
20395
20396 /* Preserve OVERLOADs; the functions must be available to resolve
20397 types. */
20398 inner_expr = expr;
20399 if (TREE_CODE (inner_expr) == STMT_EXPR)
20400 inner_expr = stmt_expr_value_expr (inner_expr);
20401 if (TREE_CODE (inner_expr) == ADDR_EXPR)
20402 inner_expr = TREE_OPERAND (inner_expr, 0);
20403 if (TREE_CODE (inner_expr) == COMPONENT_REF)
20404 inner_expr = TREE_OPERAND (inner_expr, 1);
20405 if (is_overloaded_fn (inner_expr)
20406 || TREE_CODE (inner_expr) == OFFSET_REF)
20407 return expr;
20408 /* There is no need to return a proxy for a variable. */
20409 if (TREE_CODE (expr) == VAR_DECL)
20410 return expr;
20411 /* Preserve string constants; conversions from string constants to
20412 "char *" are allowed, even though normally a "const char *"
20413 cannot be used to initialize a "char *". */
20414 if (TREE_CODE (expr) == STRING_CST)
20415 return expr;
20416 /* Preserve arithmetic constants, as an optimization -- there is no
20417 reason to create a new node. */
20418 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
20419 return expr;
20420 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20421 There is at least one place where we want to know that a
20422 particular expression is a throw-expression: when checking a ?:
20423 expression, there are special rules if the second or third
20424 argument is a throw-expression. */
20425 if (TREE_CODE (expr) == THROW_EXPR)
20426 return expr;
20427
20428 /* Don't wrap an initializer list, we need to be able to look inside. */
20429 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
20430 return expr;
20431
20432 /* Don't wrap a dummy object, we need to be able to test for it. */
20433 if (is_dummy_object (expr))
20434 return expr;
20435
20436 if (TREE_CODE (expr) == COND_EXPR)
20437 return build3 (COND_EXPR,
20438 TREE_TYPE (expr),
20439 TREE_OPERAND (expr, 0),
20440 (TREE_OPERAND (expr, 1)
20441 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
20442 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
20443 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
20444 if (TREE_CODE (expr) == COMPOUND_EXPR
20445 && !COMPOUND_EXPR_OVERLOADED (expr))
20446 return build2 (COMPOUND_EXPR,
20447 TREE_TYPE (expr),
20448 TREE_OPERAND (expr, 0),
20449 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
20450
20451 /* If the type is unknown, it can't really be non-dependent */
20452 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
20453
20454 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20455 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20456 }
20457
20458 /* ARGS is a vector of expressions as arguments to a function call.
20459 Replace the arguments with equivalent non-dependent expressions.
20460 This modifies ARGS in place. */
20461
20462 void
20463 make_args_non_dependent (vec<tree, va_gc> *args)
20464 {
20465 unsigned int ix;
20466 tree arg;
20467
20468 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
20469 {
20470 tree newarg = build_non_dependent_expr (arg);
20471 if (newarg != arg)
20472 (*args)[ix] = newarg;
20473 }
20474 }
20475
20476 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20477 with a level one deeper than the actual template parms. */
20478
20479 tree
20480 make_auto (void)
20481 {
20482 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20483 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20484 TYPE_DECL, get_identifier ("auto"), au);
20485 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20486 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20487 (0, processing_template_decl + 1, processing_template_decl + 1,
20488 TYPE_NAME (au), NULL_TREE);
20489 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20490 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20491 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20492
20493 return au;
20494 }
20495
20496 /* Given type ARG, return std::initializer_list<ARG>. */
20497
20498 static tree
20499 listify (tree arg)
20500 {
20501 tree std_init_list = namespace_binding
20502 (get_identifier ("initializer_list"), std_node);
20503 tree argvec;
20504 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20505 {
20506 error ("deducing from brace-enclosed initializer list requires "
20507 "#include <initializer_list>");
20508 return error_mark_node;
20509 }
20510 argvec = make_tree_vec (1);
20511 TREE_VEC_ELT (argvec, 0) = arg;
20512 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20513 NULL_TREE, 0, tf_warning_or_error);
20514 }
20515
20516 /* Replace auto in TYPE with std::initializer_list<auto>. */
20517
20518 static tree
20519 listify_autos (tree type, tree auto_node)
20520 {
20521 tree init_auto = listify (auto_node);
20522 tree argvec = make_tree_vec (1);
20523 TREE_VEC_ELT (argvec, 0) = init_auto;
20524 if (processing_template_decl)
20525 argvec = add_to_template_args (current_template_args (), argvec);
20526 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20527 }
20528
20529 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20530 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20531
20532 tree
20533 do_auto_deduction (tree type, tree init, tree auto_node)
20534 {
20535 tree parms, tparms, targs;
20536 tree args[1];
20537 int val;
20538
20539 if (init == error_mark_node)
20540 return error_mark_node;
20541
20542 if (type_dependent_expression_p (init))
20543 /* Defining a subset of type-dependent expressions that we can deduce
20544 from ahead of time isn't worth the trouble. */
20545 return type;
20546
20547 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20548 with either a new invented type template parameter U or, if the
20549 initializer is a braced-init-list (8.5.4), with
20550 std::initializer_list<U>. */
20551 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20552 type = listify_autos (type, auto_node);
20553
20554 init = resolve_nondeduced_context (init);
20555
20556 parms = build_tree_list (NULL_TREE, type);
20557 args[0] = init;
20558 tparms = make_tree_vec (1);
20559 targs = make_tree_vec (1);
20560 TREE_VEC_ELT (tparms, 0)
20561 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20562 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20563 DEDUCE_CALL, LOOKUP_NORMAL,
20564 /*explain_p=*/false);
20565 if (val > 0)
20566 {
20567 if (processing_template_decl)
20568 /* Try again at instantiation time. */
20569 return type;
20570 if (type && type != error_mark_node)
20571 /* If type is error_mark_node a diagnostic must have been
20572 emitted by now. Also, having a mention to '<type error>'
20573 in the diagnostic is not really useful to the user. */
20574 {
20575 if (cfun && auto_node == current_function_auto_return_pattern
20576 && LAMBDA_FUNCTION_P (current_function_decl))
20577 error ("unable to deduce lambda return type from %qE", init);
20578 else
20579 error ("unable to deduce %qT from %qE", type, init);
20580 }
20581 return error_mark_node;
20582 }
20583
20584 /* If the list of declarators contains more than one declarator, the type
20585 of each declared variable is determined as described above. If the
20586 type deduced for the template parameter U is not the same in each
20587 deduction, the program is ill-formed. */
20588 if (TREE_TYPE (auto_node)
20589 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20590 {
20591 if (cfun && auto_node == current_function_auto_return_pattern
20592 && LAMBDA_FUNCTION_P (current_function_decl))
20593 error ("inconsistent types %qT and %qT deduced for "
20594 "lambda return type", TREE_TYPE (auto_node),
20595 TREE_VEC_ELT (targs, 0));
20596 else
20597 error ("inconsistent deduction for %qT: %qT and then %qT",
20598 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20599 return error_mark_node;
20600 }
20601 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20602
20603 if (processing_template_decl)
20604 targs = add_to_template_args (current_template_args (), targs);
20605 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20606 }
20607
20608 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20609 result. */
20610
20611 tree
20612 splice_late_return_type (tree type, tree late_return_type)
20613 {
20614 tree argvec;
20615
20616 if (late_return_type == NULL_TREE)
20617 return type;
20618 argvec = make_tree_vec (1);
20619 TREE_VEC_ELT (argvec, 0) = late_return_type;
20620 if (processing_template_parmlist)
20621 /* For a late-specified return type in a template type-parameter, we
20622 need to add a dummy argument level for its parmlist. */
20623 argvec = add_to_template_args
20624 (make_tree_vec (processing_template_parmlist), argvec);
20625 if (current_template_parms)
20626 argvec = add_to_template_args (current_template_args (), argvec);
20627 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20628 }
20629
20630 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20631
20632 bool
20633 is_auto (const_tree type)
20634 {
20635 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20636 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20637 return true;
20638 else
20639 return false;
20640 }
20641
20642 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20643 appear as a type-specifier for the declaration in question, we don't
20644 have to look through the whole type. */
20645
20646 tree
20647 type_uses_auto (tree type)
20648 {
20649 enum tree_code code;
20650 if (is_auto (type))
20651 return type;
20652
20653 code = TREE_CODE (type);
20654
20655 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20656 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20657 || code == METHOD_TYPE || code == ARRAY_TYPE)
20658 return type_uses_auto (TREE_TYPE (type));
20659
20660 if (TYPE_PTRMEMFUNC_P (type))
20661 return type_uses_auto (TREE_TYPE (TREE_TYPE
20662 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20663
20664 return NULL_TREE;
20665 }
20666
20667 /* For a given template T, return the vector of typedefs referenced
20668 in T for which access check is needed at T instantiation time.
20669 T is either a FUNCTION_DECL or a RECORD_TYPE.
20670 Those typedefs were added to T by the function
20671 append_type_to_template_for_access_check. */
20672
20673 vec<qualified_typedef_usage_t, va_gc> *
20674 get_types_needing_access_check (tree t)
20675 {
20676 tree ti;
20677 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
20678
20679 if (!t || t == error_mark_node)
20680 return NULL;
20681
20682 if (!(ti = get_template_info (t)))
20683 return NULL;
20684
20685 if (CLASS_TYPE_P (t)
20686 || TREE_CODE (t) == FUNCTION_DECL)
20687 {
20688 if (!TI_TEMPLATE (ti))
20689 return NULL;
20690
20691 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20692 }
20693
20694 return result;
20695 }
20696
20697 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20698 tied to T. That list of typedefs will be access checked at
20699 T instantiation time.
20700 T is either a FUNCTION_DECL or a RECORD_TYPE.
20701 TYPE_DECL is a TYPE_DECL node representing a typedef.
20702 SCOPE is the scope through which TYPE_DECL is accessed.
20703 LOCATION is the location of the usage point of TYPE_DECL.
20704
20705 This function is a subroutine of
20706 append_type_to_template_for_access_check. */
20707
20708 static void
20709 append_type_to_template_for_access_check_1 (tree t,
20710 tree type_decl,
20711 tree scope,
20712 location_t location)
20713 {
20714 qualified_typedef_usage_t typedef_usage;
20715 tree ti;
20716
20717 if (!t || t == error_mark_node)
20718 return;
20719
20720 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20721 || CLASS_TYPE_P (t))
20722 && type_decl
20723 && TREE_CODE (type_decl) == TYPE_DECL
20724 && scope);
20725
20726 if (!(ti = get_template_info (t)))
20727 return;
20728
20729 gcc_assert (TI_TEMPLATE (ti));
20730
20731 typedef_usage.typedef_decl = type_decl;
20732 typedef_usage.context = scope;
20733 typedef_usage.locus = location;
20734
20735 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
20736 }
20737
20738 /* Append TYPE_DECL to the template TEMPL.
20739 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20740 At TEMPL instanciation time, TYPE_DECL will be checked to see
20741 if it can be accessed through SCOPE.
20742 LOCATION is the location of the usage point of TYPE_DECL.
20743
20744 e.g. consider the following code snippet:
20745
20746 class C
20747 {
20748 typedef int myint;
20749 };
20750
20751 template<class U> struct S
20752 {
20753 C::myint mi; // <-- usage point of the typedef C::myint
20754 };
20755
20756 S<char> s;
20757
20758 At S<char> instantiation time, we need to check the access of C::myint
20759 In other words, we need to check the access of the myint typedef through
20760 the C scope. For that purpose, this function will add the myint typedef
20761 and the scope C through which its being accessed to a list of typedefs
20762 tied to the template S. That list will be walked at template instantiation
20763 time and access check performed on each typedefs it contains.
20764 Note that this particular code snippet should yield an error because
20765 myint is private to C. */
20766
20767 void
20768 append_type_to_template_for_access_check (tree templ,
20769 tree type_decl,
20770 tree scope,
20771 location_t location)
20772 {
20773 qualified_typedef_usage_t *iter;
20774 unsigned i;
20775
20776 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20777
20778 /* Make sure we don't append the type to the template twice. */
20779 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
20780 if (iter->typedef_decl == type_decl && scope == iter->context)
20781 return;
20782
20783 append_type_to_template_for_access_check_1 (templ, type_decl,
20784 scope, location);
20785 }
20786
20787 /* Set up the hash tables for template instantiations. */
20788
20789 void
20790 init_template_processing (void)
20791 {
20792 decl_specializations = htab_create_ggc (37,
20793 hash_specialization,
20794 eq_specializations,
20795 ggc_free);
20796 type_specializations = htab_create_ggc (37,
20797 hash_specialization,
20798 eq_specializations,
20799 ggc_free);
20800 }
20801
20802 /* Print stats about the template hash tables for -fstats. */
20803
20804 void
20805 print_template_statistics (void)
20806 {
20807 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20808 "%f collisions\n", (long) htab_size (decl_specializations),
20809 (long) htab_elements (decl_specializations),
20810 htab_collisions (decl_specializations));
20811 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20812 "%f collisions\n", (long) htab_size (type_specializations),
20813 (long) htab_elements (type_specializations),
20814 htab_collisions (type_specializations));
20815 }
20816
20817 #include "gt-cp-pt.h"