]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Factor unrelated declarations out of tree.h.
[thirdparty/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 "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimple.h"
50 #include "gimplify.h"
51
52 /* The type of functions taking a tree, and some additional data, and
53 returning an int. */
54 typedef int (*tree_fn_t) (tree, void*);
55
56 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
57 instantiations have been deferred, either because their definitions
58 were not yet available, or because we were putting off doing the work. */
59 struct GTY ((chain_next ("%h.next"))) pending_template {
60 struct pending_template *next;
61 struct tinst_level *tinst;
62 };
63
64 static GTY(()) struct pending_template *pending_templates;
65 static GTY(()) struct pending_template *last_pending_template;
66
67 int processing_template_parmlist;
68 static int template_header_count;
69
70 static GTY(()) tree saved_trees;
71 static vec<int> inline_parm_levels;
72
73 static GTY(()) struct tinst_level *current_tinst_level;
74
75 static GTY(()) tree saved_access_scope;
76
77 /* Live only within one (recursive) call to tsubst_expr. We use
78 this to pass the statement expression node from the STMT_EXPR
79 to the EXPR_STMT that is its result. */
80 static tree cur_stmt_expr;
81
82 /* A map from local variable declarations in the body of the template
83 presently being instantiated to the corresponding instantiated
84 local variables. */
85 static struct pointer_map_t *local_specializations;
86
87 /* True if we've recursed into fn_type_unification too many times. */
88 static bool excessive_deduction_depth;
89
90 typedef struct GTY(()) spec_entry
91 {
92 tree tmpl;
93 tree args;
94 tree spec;
95 } spec_entry;
96
97 static GTY ((param_is (spec_entry)))
98 htab_t decl_specializations;
99
100 static GTY ((param_is (spec_entry)))
101 htab_t type_specializations;
102
103 /* Contains canonical template parameter types. The vector is indexed by
104 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
105 TREE_LIST, whose TREE_VALUEs contain the canonical template
106 parameters of various types and levels. */
107 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
108
109 #define UNIFY_ALLOW_NONE 0
110 #define UNIFY_ALLOW_MORE_CV_QUAL 1
111 #define UNIFY_ALLOW_LESS_CV_QUAL 2
112 #define UNIFY_ALLOW_DERIVED 4
113 #define UNIFY_ALLOW_INTEGER 8
114 #define UNIFY_ALLOW_OUTER_LEVEL 16
115 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
116 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
117
118 enum template_base_result {
119 tbr_incomplete_type,
120 tbr_ambiguous_baseclass,
121 tbr_success
122 };
123
124 static void push_access_scope (tree);
125 static void pop_access_scope (tree);
126 static bool resolve_overloaded_unification (tree, tree, tree, tree,
127 unification_kind_t, int,
128 bool);
129 static int try_one_overload (tree, tree, tree, tree, tree,
130 unification_kind_t, int, bool, bool);
131 static int unify (tree, tree, tree, tree, int, bool);
132 static void add_pending_template (tree);
133 static tree reopen_tinst_level (struct tinst_level *);
134 static tree tsubst_initializer_list (tree, tree);
135 static tree get_class_bindings (tree, tree, tree, tree);
136 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
137 bool, bool);
138 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
139 bool, bool);
140 static void tsubst_enum (tree, tree, tree);
141 static tree add_to_template_args (tree, tree);
142 static tree add_outermost_template_args (tree, tree);
143 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
144 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
145 tree);
146 static int type_unification_real (tree, tree, tree, const tree *,
147 unsigned int, int, unification_kind_t, int,
148 vec<deferred_access_check, va_gc> **,
149 bool);
150 static void note_template_header (int);
151 static tree convert_nontype_argument_function (tree, tree);
152 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
153 static tree convert_template_argument (tree, tree, tree,
154 tsubst_flags_t, int, tree);
155 static int for_each_template_parm (tree, tree_fn_t, void*,
156 struct pointer_set_t*, bool);
157 static tree expand_template_argument_pack (tree);
158 static tree build_template_parm_index (int, int, int, tree, tree);
159 static bool inline_needs_template_parms (tree, bool);
160 static void push_inline_template_parms_recursive (tree, int);
161 static tree retrieve_local_specialization (tree);
162 static void register_local_specialization (tree, tree);
163 static hashval_t hash_specialization (const void *p);
164 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
165 static int mark_template_parm (tree, void *);
166 static int template_parm_this_level_p (tree, void *);
167 static tree tsubst_friend_function (tree, tree);
168 static tree tsubst_friend_class (tree, tree);
169 static int can_complete_type_without_circularity (tree);
170 static tree get_bindings (tree, tree, tree, bool);
171 static int template_decl_level (tree);
172 static int check_cv_quals_for_unify (int, tree, tree);
173 static void template_parm_level_and_index (tree, int*, int*);
174 static int unify_pack_expansion (tree, tree, tree,
175 tree, unification_kind_t, bool, bool);
176 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
178 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
179 static void regenerate_decl_from_template (tree, tree);
180 static tree most_specialized_class (tree, tree, tsubst_flags_t);
181 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
182 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
184 static bool check_specialization_scope (void);
185 static tree process_partial_specialization (tree);
186 static void set_current_access_from_decl (tree);
187 static enum template_base_result get_template_base (tree, tree, tree, tree,
188 bool , tree *);
189 static tree try_class_unification (tree, tree, tree, tree, bool);
190 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
191 tree, tree);
192 static bool template_template_parm_bindings_ok_p (tree, tree);
193 static int template_args_equal (tree, tree);
194 static void tsubst_default_arguments (tree, tsubst_flags_t);
195 static tree for_each_template_parm_r (tree *, int *, void *);
196 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
197 static void copy_default_args_to_explicit_spec (tree);
198 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
199 static bool dependent_template_arg_p (tree);
200 static bool any_template_arguments_need_structural_equality_p (tree);
201 static bool dependent_type_p_r (tree);
202 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
203 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
204 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
205 static tree tsubst_decl (tree, tree, tsubst_flags_t);
206 static void perform_typedefs_access_check (tree tmpl, tree targs);
207 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
208 location_t);
209 static tree listify (tree);
210 static tree listify_autos (tree, tree);
211 static tree template_parm_to_arg (tree t);
212 static tree current_template_args (void);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215
216 /* Make the current scope suitable for access checking when we are
217 processing T. T can be FUNCTION_DECL for instantiated function
218 template, VAR_DECL for static member variable, or TYPE_DECL for
219 alias template (needed by instantiate_decl). */
220
221 static void
222 push_access_scope (tree t)
223 {
224 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
225 || TREE_CODE (t) == TYPE_DECL);
226
227 if (DECL_FRIEND_CONTEXT (t))
228 push_nested_class (DECL_FRIEND_CONTEXT (t));
229 else if (DECL_CLASS_SCOPE_P (t))
230 push_nested_class (DECL_CONTEXT (t));
231 else
232 push_to_top_level ();
233
234 if (TREE_CODE (t) == FUNCTION_DECL)
235 {
236 saved_access_scope = tree_cons
237 (NULL_TREE, current_function_decl, saved_access_scope);
238 current_function_decl = t;
239 }
240 }
241
242 /* Restore the scope set up by push_access_scope. T is the node we
243 are processing. */
244
245 static void
246 pop_access_scope (tree t)
247 {
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 current_function_decl = TREE_VALUE (saved_access_scope);
251 saved_access_scope = TREE_CHAIN (saved_access_scope);
252 }
253
254 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
255 pop_nested_class ();
256 else
257 pop_from_top_level ();
258 }
259
260 /* Do any processing required when DECL (a member template
261 declaration) is finished. Returns the TEMPLATE_DECL corresponding
262 to DECL, unless it is a specialization, in which case the DECL
263 itself is returned. */
264
265 tree
266 finish_member_template_decl (tree decl)
267 {
268 if (decl == error_mark_node)
269 return error_mark_node;
270
271 gcc_assert (DECL_P (decl));
272
273 if (TREE_CODE (decl) == TYPE_DECL)
274 {
275 tree type;
276
277 type = TREE_TYPE (decl);
278 if (type == error_mark_node)
279 return error_mark_node;
280 if (MAYBE_CLASS_TYPE_P (type)
281 && CLASSTYPE_TEMPLATE_INFO (type)
282 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
283 {
284 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
285 check_member_template (tmpl);
286 return tmpl;
287 }
288 return NULL_TREE;
289 }
290 else if (TREE_CODE (decl) == FIELD_DECL)
291 error ("data member %qD cannot be a member template", decl);
292 else if (DECL_TEMPLATE_INFO (decl))
293 {
294 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
295 {
296 check_member_template (DECL_TI_TEMPLATE (decl));
297 return DECL_TI_TEMPLATE (decl);
298 }
299 else
300 return decl;
301 }
302 else
303 error ("invalid member template declaration %qD", decl);
304
305 return error_mark_node;
306 }
307
308 /* Create a template info node. */
309
310 tree
311 build_template_info (tree template_decl, tree template_args)
312 {
313 tree result = make_node (TEMPLATE_INFO);
314 TI_TEMPLATE (result) = template_decl;
315 TI_ARGS (result) = template_args;
316 return result;
317 }
318
319 /* Return the template info node corresponding to T, whatever T is. */
320
321 tree
322 get_template_info (const_tree t)
323 {
324 tree tinfo = NULL_TREE;
325
326 if (!t || t == error_mark_node)
327 return NULL;
328
329 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
330 tinfo = DECL_TEMPLATE_INFO (t);
331
332 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
333 t = TREE_TYPE (t);
334
335 if (OVERLOAD_TYPE_P (t))
336 tinfo = TYPE_TEMPLATE_INFO (t);
337 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
338 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
339
340 return tinfo;
341 }
342
343 /* Returns the template nesting level of the indicated class TYPE.
344
345 For example, in:
346 template <class T>
347 struct A
348 {
349 template <class U>
350 struct B {};
351 };
352
353 A<T>::B<U> has depth two, while A<T> has depth one.
354 Both A<T>::B<int> and A<int>::B<U> have depth one, if
355 they are instantiations, not specializations.
356
357 This function is guaranteed to return 0 if passed NULL_TREE so
358 that, for example, `template_class_depth (current_class_type)' is
359 always safe. */
360
361 int
362 template_class_depth (tree type)
363 {
364 int depth;
365
366 for (depth = 0;
367 type && TREE_CODE (type) != NAMESPACE_DECL;
368 type = (TREE_CODE (type) == FUNCTION_DECL)
369 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
370 {
371 tree tinfo = get_template_info (type);
372
373 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
374 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
375 ++depth;
376 }
377
378 return depth;
379 }
380
381 /* Subroutine of maybe_begin_member_template_processing.
382 Returns true if processing DECL needs us to push template parms. */
383
384 static bool
385 inline_needs_template_parms (tree decl, bool nsdmi)
386 {
387 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
388 return false;
389
390 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
391 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
392 }
393
394 /* Subroutine of maybe_begin_member_template_processing.
395 Push the template parms in PARMS, starting from LEVELS steps into the
396 chain, and ending at the beginning, since template parms are listed
397 innermost first. */
398
399 static void
400 push_inline_template_parms_recursive (tree parmlist, int levels)
401 {
402 tree parms = TREE_VALUE (parmlist);
403 int i;
404
405 if (levels > 1)
406 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
407
408 ++processing_template_decl;
409 current_template_parms
410 = tree_cons (size_int (processing_template_decl),
411 parms, current_template_parms);
412 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
413
414 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
415 NULL);
416 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
417 {
418 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
419
420 if (parm == error_mark_node)
421 continue;
422
423 gcc_assert (DECL_P (parm));
424
425 switch (TREE_CODE (parm))
426 {
427 case TYPE_DECL:
428 case TEMPLATE_DECL:
429 pushdecl (parm);
430 break;
431
432 case PARM_DECL:
433 {
434 /* Make a CONST_DECL as is done in process_template_parm.
435 It is ugly that we recreate this here; the original
436 version built in process_template_parm is no longer
437 available. */
438 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
439 CONST_DECL, DECL_NAME (parm),
440 TREE_TYPE (parm));
441 DECL_ARTIFICIAL (decl) = 1;
442 TREE_CONSTANT (decl) = 1;
443 TREE_READONLY (decl) = 1;
444 DECL_INITIAL (decl) = DECL_INITIAL (parm);
445 SET_DECL_TEMPLATE_PARM_P (decl);
446 pushdecl (decl);
447 }
448 break;
449
450 default:
451 gcc_unreachable ();
452 }
453 }
454 }
455
456 /* Restore the template parameter context for a member template, a
457 friend template defined in a class definition, or a non-template
458 member of template class. */
459
460 void
461 maybe_begin_member_template_processing (tree decl)
462 {
463 tree parms;
464 int levels = 0;
465 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
466
467 if (nsdmi)
468 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
469 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
470 : NULL_TREE);
471
472 if (inline_needs_template_parms (decl, nsdmi))
473 {
474 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
475 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
476
477 if (DECL_TEMPLATE_SPECIALIZATION (decl))
478 {
479 --levels;
480 parms = TREE_CHAIN (parms);
481 }
482
483 push_inline_template_parms_recursive (parms, levels);
484 }
485
486 /* Remember how many levels of template parameters we pushed so that
487 we can pop them later. */
488 inline_parm_levels.safe_push (levels);
489 }
490
491 /* Undo the effects of maybe_begin_member_template_processing. */
492
493 void
494 maybe_end_member_template_processing (void)
495 {
496 int i;
497 int last;
498
499 if (inline_parm_levels.length () == 0)
500 return;
501
502 last = inline_parm_levels.pop ();
503 for (i = 0; i < last; ++i)
504 {
505 --processing_template_decl;
506 current_template_parms = TREE_CHAIN (current_template_parms);
507 poplevel (0, 0, 0);
508 }
509 }
510
511 /* Return a new template argument vector which contains all of ARGS,
512 but has as its innermost set of arguments the EXTRA_ARGS. */
513
514 static tree
515 add_to_template_args (tree args, tree extra_args)
516 {
517 tree new_args;
518 int extra_depth;
519 int i;
520 int j;
521
522 if (args == NULL_TREE || extra_args == error_mark_node)
523 return extra_args;
524
525 extra_depth = TMPL_ARGS_DEPTH (extra_args);
526 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
527
528 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
529 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
530
531 for (j = 1; j <= extra_depth; ++j, ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
533
534 return new_args;
535 }
536
537 /* Like add_to_template_args, but only the outermost ARGS are added to
538 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
539 (EXTRA_ARGS) levels are added. This function is used to combine
540 the template arguments from a partial instantiation with the
541 template arguments used to attain the full instantiation from the
542 partial instantiation. */
543
544 static tree
545 add_outermost_template_args (tree args, tree extra_args)
546 {
547 tree new_args;
548
549 /* If there are more levels of EXTRA_ARGS than there are ARGS,
550 something very fishy is going on. */
551 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
552
553 /* If *all* the new arguments will be the EXTRA_ARGS, just return
554 them. */
555 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
556 return extra_args;
557
558 /* For the moment, we make ARGS look like it contains fewer levels. */
559 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
560
561 new_args = add_to_template_args (args, extra_args);
562
563 /* Now, we restore ARGS to its full dimensions. */
564 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
565
566 return new_args;
567 }
568
569 /* Return the N levels of innermost template arguments from the ARGS. */
570
571 tree
572 get_innermost_template_args (tree args, int n)
573 {
574 tree new_args;
575 int extra_levels;
576 int i;
577
578 gcc_assert (n >= 0);
579
580 /* If N is 1, just return the innermost set of template arguments. */
581 if (n == 1)
582 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
583
584 /* If we're not removing anything, just return the arguments we were
585 given. */
586 extra_levels = TMPL_ARGS_DEPTH (args) - n;
587 gcc_assert (extra_levels >= 0);
588 if (extra_levels == 0)
589 return args;
590
591 /* Make a new set of arguments, not containing the outer arguments. */
592 new_args = make_tree_vec (n);
593 for (i = 1; i <= n; ++i)
594 SET_TMPL_ARGS_LEVEL (new_args, i,
595 TMPL_ARGS_LEVEL (args, i + extra_levels));
596
597 return new_args;
598 }
599
600 /* The inverse of get_innermost_template_args: Return all but the innermost
601 EXTRA_LEVELS levels of template arguments from the ARGS. */
602
603 static tree
604 strip_innermost_template_args (tree args, int extra_levels)
605 {
606 tree new_args;
607 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
608 int i;
609
610 gcc_assert (n >= 0);
611
612 /* If N is 1, just return the outermost set of template arguments. */
613 if (n == 1)
614 return TMPL_ARGS_LEVEL (args, 1);
615
616 /* If we're not removing anything, just return the arguments we were
617 given. */
618 gcc_assert (extra_levels >= 0);
619 if (extra_levels == 0)
620 return args;
621
622 /* Make a new set of arguments, not containing the inner arguments. */
623 new_args = make_tree_vec (n);
624 for (i = 1; i <= n; ++i)
625 SET_TMPL_ARGS_LEVEL (new_args, i,
626 TMPL_ARGS_LEVEL (args, i));
627
628 return new_args;
629 }
630
631 /* We've got a template header coming up; push to a new level for storing
632 the parms. */
633
634 void
635 begin_template_parm_list (void)
636 {
637 /* We use a non-tag-transparent scope here, which causes pushtag to
638 put tags in this scope, rather than in the enclosing class or
639 namespace scope. This is the right thing, since we want
640 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
641 global template class, push_template_decl handles putting the
642 TEMPLATE_DECL into top-level scope. For a nested template class,
643 e.g.:
644
645 template <class T> struct S1 {
646 template <class T> struct S2 {};
647 };
648
649 pushtag contains special code to call pushdecl_with_scope on the
650 TEMPLATE_DECL for S2. */
651 begin_scope (sk_template_parms, NULL);
652 ++processing_template_decl;
653 ++processing_template_parmlist;
654 note_template_header (0);
655 }
656
657 /* This routine is called when a specialization is declared. If it is
658 invalid to declare a specialization here, an error is reported and
659 false is returned, otherwise this routine will return true. */
660
661 static bool
662 check_specialization_scope (void)
663 {
664 tree scope = current_scope ();
665
666 /* [temp.expl.spec]
667
668 An explicit specialization shall be declared in the namespace of
669 which the template is a member, or, for member templates, in the
670 namespace of which the enclosing class or enclosing class
671 template is a member. An explicit specialization of a member
672 function, member class or static data member of a class template
673 shall be declared in the namespace of which the class template
674 is a member. */
675 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
676 {
677 error ("explicit specialization in non-namespace scope %qD", scope);
678 return false;
679 }
680
681 /* [temp.expl.spec]
682
683 In an explicit specialization declaration for a member of a class
684 template or a member template that appears in namespace scope,
685 the member template and some of its enclosing class templates may
686 remain unspecialized, except that the declaration shall not
687 explicitly specialize a class member template if its enclosing
688 class templates are not explicitly specialized as well. */
689 if (current_template_parms)
690 {
691 error ("enclosing class templates are not explicitly specialized");
692 return false;
693 }
694
695 return true;
696 }
697
698 /* We've just seen template <>. */
699
700 bool
701 begin_specialization (void)
702 {
703 begin_scope (sk_template_spec, NULL);
704 note_template_header (1);
705 return check_specialization_scope ();
706 }
707
708 /* Called at then end of processing a declaration preceded by
709 template<>. */
710
711 void
712 end_specialization (void)
713 {
714 finish_scope ();
715 reset_specialization ();
716 }
717
718 /* Any template <>'s that we have seen thus far are not referring to a
719 function specialization. */
720
721 void
722 reset_specialization (void)
723 {
724 processing_specialization = 0;
725 template_header_count = 0;
726 }
727
728 /* We've just seen a template header. If SPECIALIZATION is nonzero,
729 it was of the form template <>. */
730
731 static void
732 note_template_header (int specialization)
733 {
734 processing_specialization = specialization;
735 template_header_count++;
736 }
737
738 /* We're beginning an explicit instantiation. */
739
740 void
741 begin_explicit_instantiation (void)
742 {
743 gcc_assert (!processing_explicit_instantiation);
744 processing_explicit_instantiation = true;
745 }
746
747
748 void
749 end_explicit_instantiation (void)
750 {
751 gcc_assert (processing_explicit_instantiation);
752 processing_explicit_instantiation = false;
753 }
754
755 /* An explicit specialization or partial specialization of TMPL is being
756 declared. Check that the namespace in which the specialization is
757 occurring is permissible. Returns false iff it is invalid to
758 specialize TMPL in the current namespace. */
759
760 static bool
761 check_specialization_namespace (tree tmpl)
762 {
763 tree tpl_ns = decl_namespace_context (tmpl);
764
765 /* [tmpl.expl.spec]
766
767 An explicit specialization shall be declared in the namespace of
768 which the template is a member, or, for member templates, in the
769 namespace of which the enclosing class or enclosing class
770 template is a member. An explicit specialization of a member
771 function, member class or static data member of a class template
772 shall be declared in the namespace of which the class template is
773 a member. */
774 if (current_scope() != DECL_CONTEXT (tmpl)
775 && !at_namespace_scope_p ())
776 {
777 error ("specialization of %qD must appear at namespace scope", tmpl);
778 return false;
779 }
780 if (is_associated_namespace (current_namespace, tpl_ns))
781 /* Same or super-using namespace. */
782 return true;
783 else
784 {
785 permerror (input_location, "specialization of %qD in different namespace", tmpl);
786 permerror (input_location, " from definition of %q+#D", tmpl);
787 return false;
788 }
789 }
790
791 /* SPEC is an explicit instantiation. Check that it is valid to
792 perform this explicit instantiation in the current namespace. */
793
794 static void
795 check_explicit_instantiation_namespace (tree spec)
796 {
797 tree ns;
798
799 /* DR 275: An explicit instantiation shall appear in an enclosing
800 namespace of its template. */
801 ns = decl_namespace_context (spec);
802 if (!is_ancestor (current_namespace, ns))
803 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
804 "(which does not enclose namespace %qD)",
805 spec, current_namespace, ns);
806 }
807
808 /* The TYPE is being declared. If it is a template type, that means it
809 is a partial specialization. Do appropriate error-checking. */
810
811 tree
812 maybe_process_partial_specialization (tree type)
813 {
814 tree context;
815
816 if (type == error_mark_node)
817 return error_mark_node;
818
819 /* A lambda that appears in specialization context is not itself a
820 specialization. */
821 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
822 return type;
823
824 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
825 {
826 error ("name of class shadows template template parameter %qD",
827 TYPE_NAME (type));
828 return error_mark_node;
829 }
830
831 context = TYPE_CONTEXT (type);
832
833 if (TYPE_ALIAS_P (type))
834 {
835 if (TYPE_TEMPLATE_INFO (type)
836 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
837 error ("specialization of alias template %qD",
838 TYPE_TI_TEMPLATE (type));
839 else
840 error ("explicit specialization of non-template %qT", type);
841 return error_mark_node;
842 }
843 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
844 {
845 /* This is for ordinary explicit specialization and partial
846 specialization of a template class such as:
847
848 template <> class C<int>;
849
850 or:
851
852 template <class T> class C<T*>;
853
854 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
855
856 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
857 && !COMPLETE_TYPE_P (type))
858 {
859 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
860 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
861 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
862 if (processing_template_decl)
863 {
864 if (push_template_decl (TYPE_MAIN_DECL (type))
865 == error_mark_node)
866 return error_mark_node;
867 }
868 }
869 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
870 error ("specialization of %qT after instantiation", type);
871 else if (errorcount && !processing_specialization
872 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
873 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
874 /* Trying to define a specialization either without a template<> header
875 or in an inappropriate place. We've already given an error, so just
876 bail now so we don't actually define the specialization. */
877 return error_mark_node;
878 }
879 else if (CLASS_TYPE_P (type)
880 && !CLASSTYPE_USE_TEMPLATE (type)
881 && CLASSTYPE_TEMPLATE_INFO (type)
882 && context && CLASS_TYPE_P (context)
883 && CLASSTYPE_TEMPLATE_INFO (context))
884 {
885 /* This is for an explicit specialization of member class
886 template according to [temp.expl.spec/18]:
887
888 template <> template <class U> class C<int>::D;
889
890 The context `C<int>' must be an implicit instantiation.
891 Otherwise this is just a member class template declared
892 earlier like:
893
894 template <> class C<int> { template <class U> class D; };
895 template <> template <class U> class C<int>::D;
896
897 In the first case, `C<int>::D' is a specialization of `C<T>::D'
898 while in the second case, `C<int>::D' is a primary template
899 and `C<T>::D' may not exist. */
900
901 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
902 && !COMPLETE_TYPE_P (type))
903 {
904 tree t;
905 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
906
907 if (current_namespace
908 != decl_namespace_context (tmpl))
909 {
910 permerror (input_location, "specializing %q#T in different namespace", type);
911 permerror (input_location, " from definition of %q+#D", tmpl);
912 }
913
914 /* Check for invalid specialization after instantiation:
915
916 template <> template <> class C<int>::D<int>;
917 template <> template <class U> class C<int>::D; */
918
919 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
920 t; t = TREE_CHAIN (t))
921 {
922 tree inst = TREE_VALUE (t);
923 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
924 {
925 /* We already have a full specialization of this partial
926 instantiation. Reassign it to the new member
927 specialization template. */
928 spec_entry elt;
929 spec_entry *entry;
930 void **slot;
931
932 elt.tmpl = most_general_template (tmpl);
933 elt.args = CLASSTYPE_TI_ARGS (inst);
934 elt.spec = inst;
935
936 htab_remove_elt (type_specializations, &elt);
937
938 elt.tmpl = tmpl;
939 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
940
941 slot = htab_find_slot (type_specializations, &elt, INSERT);
942 entry = ggc_alloc_spec_entry ();
943 *entry = elt;
944 *slot = entry;
945 }
946 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
947 /* But if we've had an implicit instantiation, that's a
948 problem ([temp.expl.spec]/6). */
949 error ("specialization %qT after instantiation %qT",
950 type, inst);
951 }
952
953 /* Mark TYPE as a specialization. And as a result, we only
954 have one level of template argument for the innermost
955 class template. */
956 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
957 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
958 CLASSTYPE_TI_ARGS (type)
959 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
960 }
961 }
962 else if (processing_specialization)
963 {
964 /* Someday C++0x may allow for enum template specialization. */
965 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
966 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
967 pedwarn (input_location, OPT_Wpedantic, "template specialization "
968 "of %qD not allowed by ISO C++", type);
969 else
970 {
971 error ("explicit specialization of non-template %qT", type);
972 return error_mark_node;
973 }
974 }
975
976 return type;
977 }
978
979 /* Returns nonzero if we can optimize the retrieval of specializations
980 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
981 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
982
983 static inline bool
984 optimize_specialization_lookup_p (tree tmpl)
985 {
986 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
987 && DECL_CLASS_SCOPE_P (tmpl)
988 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
989 parameter. */
990 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
991 /* The optimized lookup depends on the fact that the
992 template arguments for the member function template apply
993 purely to the containing class, which is not true if the
994 containing class is an explicit or partial
995 specialization. */
996 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
997 && !DECL_MEMBER_TEMPLATE_P (tmpl)
998 && !DECL_CONV_FN_P (tmpl)
999 /* It is possible to have a template that is not a member
1000 template and is not a member of a template class:
1001
1002 template <typename T>
1003 struct S { friend A::f(); };
1004
1005 Here, the friend function is a template, but the context does
1006 not have template information. The optimized lookup relies
1007 on having ARGS be the template arguments for both the class
1008 and the function template. */
1009 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1010 }
1011
1012 /* Retrieve the specialization (in the sense of [temp.spec] - a
1013 specialization is either an instantiation or an explicit
1014 specialization) of TMPL for the given template ARGS. If there is
1015 no such specialization, return NULL_TREE. The ARGS are a vector of
1016 arguments, or a vector of vectors of arguments, in the case of
1017 templates with more than one level of parameters.
1018
1019 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1020 then we search for a partial specialization matching ARGS. This
1021 parameter is ignored if TMPL is not a class template.
1022
1023 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1024 result is a NONTYPE_ARGUMENT_PACK. */
1025
1026 static tree
1027 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1028 {
1029 if (tmpl == NULL_TREE)
1030 return NULL_TREE;
1031
1032 if (args == error_mark_node)
1033 return NULL_TREE;
1034
1035 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1036 || TREE_CODE (tmpl) == FIELD_DECL);
1037
1038 /* There should be as many levels of arguments as there are
1039 levels of parameters. */
1040 gcc_assert (TMPL_ARGS_DEPTH (args)
1041 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1042 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1043 : template_class_depth (DECL_CONTEXT (tmpl))));
1044
1045 if (optimize_specialization_lookup_p (tmpl))
1046 {
1047 tree class_template;
1048 tree class_specialization;
1049 vec<tree, va_gc> *methods;
1050 tree fns;
1051 int idx;
1052
1053 /* The template arguments actually apply to the containing
1054 class. Find the class specialization with those
1055 arguments. */
1056 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1057 class_specialization
1058 = retrieve_specialization (class_template, args, 0);
1059 if (!class_specialization)
1060 return NULL_TREE;
1061 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1062 for the specialization. */
1063 idx = class_method_index_for_fn (class_specialization, tmpl);
1064 if (idx == -1)
1065 return NULL_TREE;
1066 /* Iterate through the methods with the indicated name, looking
1067 for the one that has an instance of TMPL. */
1068 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1069 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1070 {
1071 tree fn = OVL_CURRENT (fns);
1072 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1073 /* using-declarations can add base methods to the method vec,
1074 and we don't want those here. */
1075 && DECL_CONTEXT (fn) == class_specialization)
1076 return fn;
1077 }
1078 return NULL_TREE;
1079 }
1080 else
1081 {
1082 spec_entry *found;
1083 spec_entry elt;
1084 htab_t specializations;
1085
1086 elt.tmpl = tmpl;
1087 elt.args = args;
1088 elt.spec = NULL_TREE;
1089
1090 if (DECL_CLASS_TEMPLATE_P (tmpl))
1091 specializations = type_specializations;
1092 else
1093 specializations = decl_specializations;
1094
1095 if (hash == 0)
1096 hash = hash_specialization (&elt);
1097 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1098 if (found)
1099 return found->spec;
1100 }
1101
1102 return NULL_TREE;
1103 }
1104
1105 /* Like retrieve_specialization, but for local declarations. */
1106
1107 static tree
1108 retrieve_local_specialization (tree tmpl)
1109 {
1110 void **slot;
1111
1112 if (local_specializations == NULL)
1113 return NULL_TREE;
1114
1115 slot = pointer_map_contains (local_specializations, tmpl);
1116 return slot ? (tree) *slot : NULL_TREE;
1117 }
1118
1119 /* Returns nonzero iff DECL is a specialization of TMPL. */
1120
1121 int
1122 is_specialization_of (tree decl, tree tmpl)
1123 {
1124 tree t;
1125
1126 if (TREE_CODE (decl) == FUNCTION_DECL)
1127 {
1128 for (t = decl;
1129 t != NULL_TREE;
1130 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1131 if (t == tmpl)
1132 return 1;
1133 }
1134 else
1135 {
1136 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1137
1138 for (t = TREE_TYPE (decl);
1139 t != NULL_TREE;
1140 t = CLASSTYPE_USE_TEMPLATE (t)
1141 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1142 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1143 return 1;
1144 }
1145
1146 return 0;
1147 }
1148
1149 /* Returns nonzero iff DECL is a specialization of friend declaration
1150 FRIEND_DECL according to [temp.friend]. */
1151
1152 bool
1153 is_specialization_of_friend (tree decl, tree friend_decl)
1154 {
1155 bool need_template = true;
1156 int template_depth;
1157
1158 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1159 || TREE_CODE (decl) == TYPE_DECL);
1160
1161 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1162 of a template class, we want to check if DECL is a specialization
1163 if this. */
1164 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1165 && DECL_TEMPLATE_INFO (friend_decl)
1166 && !DECL_USE_TEMPLATE (friend_decl))
1167 {
1168 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1169 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1170 need_template = false;
1171 }
1172 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1173 && !PRIMARY_TEMPLATE_P (friend_decl))
1174 need_template = false;
1175
1176 /* There is nothing to do if this is not a template friend. */
1177 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1178 return false;
1179
1180 if (is_specialization_of (decl, friend_decl))
1181 return true;
1182
1183 /* [temp.friend/6]
1184 A member of a class template may be declared to be a friend of a
1185 non-template class. In this case, the corresponding member of
1186 every specialization of the class template is a friend of the
1187 class granting friendship.
1188
1189 For example, given a template friend declaration
1190
1191 template <class T> friend void A<T>::f();
1192
1193 the member function below is considered a friend
1194
1195 template <> struct A<int> {
1196 void f();
1197 };
1198
1199 For this type of template friend, TEMPLATE_DEPTH below will be
1200 nonzero. To determine if DECL is a friend of FRIEND, we first
1201 check if the enclosing class is a specialization of another. */
1202
1203 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1204 if (template_depth
1205 && DECL_CLASS_SCOPE_P (decl)
1206 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1207 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1208 {
1209 /* Next, we check the members themselves. In order to handle
1210 a few tricky cases, such as when FRIEND_DECL's are
1211
1212 template <class T> friend void A<T>::g(T t);
1213 template <class T> template <T t> friend void A<T>::h();
1214
1215 and DECL's are
1216
1217 void A<int>::g(int);
1218 template <int> void A<int>::h();
1219
1220 we need to figure out ARGS, the template arguments from
1221 the context of DECL. This is required for template substitution
1222 of `T' in the function parameter of `g' and template parameter
1223 of `h' in the above examples. Here ARGS corresponds to `int'. */
1224
1225 tree context = DECL_CONTEXT (decl);
1226 tree args = NULL_TREE;
1227 int current_depth = 0;
1228
1229 while (current_depth < template_depth)
1230 {
1231 if (CLASSTYPE_TEMPLATE_INFO (context))
1232 {
1233 if (current_depth == 0)
1234 args = TYPE_TI_ARGS (context);
1235 else
1236 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1237 current_depth++;
1238 }
1239 context = TYPE_CONTEXT (context);
1240 }
1241
1242 if (TREE_CODE (decl) == FUNCTION_DECL)
1243 {
1244 bool is_template;
1245 tree friend_type;
1246 tree decl_type;
1247 tree friend_args_type;
1248 tree decl_args_type;
1249
1250 /* Make sure that both DECL and FRIEND_DECL are templates or
1251 non-templates. */
1252 is_template = DECL_TEMPLATE_INFO (decl)
1253 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1254 if (need_template ^ is_template)
1255 return false;
1256 else if (is_template)
1257 {
1258 /* If both are templates, check template parameter list. */
1259 tree friend_parms
1260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1261 args, tf_none);
1262 if (!comp_template_parms
1263 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1264 friend_parms))
1265 return false;
1266
1267 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1268 }
1269 else
1270 decl_type = TREE_TYPE (decl);
1271
1272 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1273 tf_none, NULL_TREE);
1274 if (friend_type == error_mark_node)
1275 return false;
1276
1277 /* Check if return types match. */
1278 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1279 return false;
1280
1281 /* Check if function parameter types match, ignoring the
1282 `this' parameter. */
1283 friend_args_type = TYPE_ARG_TYPES (friend_type);
1284 decl_args_type = TYPE_ARG_TYPES (decl_type);
1285 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1286 friend_args_type = TREE_CHAIN (friend_args_type);
1287 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1288 decl_args_type = TREE_CHAIN (decl_args_type);
1289
1290 return compparms (decl_args_type, friend_args_type);
1291 }
1292 else
1293 {
1294 /* DECL is a TYPE_DECL */
1295 bool is_template;
1296 tree decl_type = TREE_TYPE (decl);
1297
1298 /* Make sure that both DECL and FRIEND_DECL are templates or
1299 non-templates. */
1300 is_template
1301 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1302 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1303
1304 if (need_template ^ is_template)
1305 return false;
1306 else if (is_template)
1307 {
1308 tree friend_parms;
1309 /* If both are templates, check the name of the two
1310 TEMPLATE_DECL's first because is_friend didn't. */
1311 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1312 != DECL_NAME (friend_decl))
1313 return false;
1314
1315 /* Now check template parameter list. */
1316 friend_parms
1317 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1318 args, tf_none);
1319 return comp_template_parms
1320 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1321 friend_parms);
1322 }
1323 else
1324 return (DECL_NAME (decl)
1325 == DECL_NAME (friend_decl));
1326 }
1327 }
1328 return false;
1329 }
1330
1331 /* Register the specialization SPEC as a specialization of TMPL with
1332 the indicated ARGS. IS_FRIEND indicates whether the specialization
1333 is actually just a friend declaration. Returns SPEC, or an
1334 equivalent prior declaration, if available.
1335
1336 We also store instantiations of field packs in the hash table, even
1337 though they are not themselves templates, to make lookup easier. */
1338
1339 static tree
1340 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1341 hashval_t hash)
1342 {
1343 tree fn;
1344 void **slot = NULL;
1345 spec_entry elt;
1346
1347 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1348 || (TREE_CODE (tmpl) == FIELD_DECL
1349 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1350
1351 if (TREE_CODE (spec) == FUNCTION_DECL
1352 && uses_template_parms (DECL_TI_ARGS (spec)))
1353 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1354 register it; we want the corresponding TEMPLATE_DECL instead.
1355 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1356 the more obvious `uses_template_parms (spec)' to avoid problems
1357 with default function arguments. In particular, given
1358 something like this:
1359
1360 template <class T> void f(T t1, T t = T())
1361
1362 the default argument expression is not substituted for in an
1363 instantiation unless and until it is actually needed. */
1364 return spec;
1365
1366 if (optimize_specialization_lookup_p (tmpl))
1367 /* We don't put these specializations in the hash table, but we might
1368 want to give an error about a mismatch. */
1369 fn = retrieve_specialization (tmpl, args, 0);
1370 else
1371 {
1372 elt.tmpl = tmpl;
1373 elt.args = args;
1374 elt.spec = spec;
1375
1376 if (hash == 0)
1377 hash = hash_specialization (&elt);
1378
1379 slot =
1380 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1381 if (*slot)
1382 fn = ((spec_entry *) *slot)->spec;
1383 else
1384 fn = NULL_TREE;
1385 }
1386
1387 /* We can sometimes try to re-register a specialization that we've
1388 already got. In particular, regenerate_decl_from_template calls
1389 duplicate_decls which will update the specialization list. But,
1390 we'll still get called again here anyhow. It's more convenient
1391 to simply allow this than to try to prevent it. */
1392 if (fn == spec)
1393 return spec;
1394 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1395 {
1396 if (DECL_TEMPLATE_INSTANTIATION (fn))
1397 {
1398 if (DECL_ODR_USED (fn)
1399 || DECL_EXPLICIT_INSTANTIATION (fn))
1400 {
1401 error ("specialization of %qD after instantiation",
1402 fn);
1403 return error_mark_node;
1404 }
1405 else
1406 {
1407 tree clone;
1408 /* This situation should occur only if the first
1409 specialization is an implicit instantiation, the
1410 second is an explicit specialization, and the
1411 implicit instantiation has not yet been used. That
1412 situation can occur if we have implicitly
1413 instantiated a member function and then specialized
1414 it later.
1415
1416 We can also wind up here if a friend declaration that
1417 looked like an instantiation turns out to be a
1418 specialization:
1419
1420 template <class T> void foo(T);
1421 class S { friend void foo<>(int) };
1422 template <> void foo(int);
1423
1424 We transform the existing DECL in place so that any
1425 pointers to it become pointers to the updated
1426 declaration.
1427
1428 If there was a definition for the template, but not
1429 for the specialization, we want this to look as if
1430 there were no definition, and vice versa. */
1431 DECL_INITIAL (fn) = NULL_TREE;
1432 duplicate_decls (spec, fn, is_friend);
1433 /* The call to duplicate_decls will have applied
1434 [temp.expl.spec]:
1435
1436 An explicit specialization of a function template
1437 is inline only if it is explicitly declared to be,
1438 and independently of whether its function template
1439 is.
1440
1441 to the primary function; now copy the inline bits to
1442 the various clones. */
1443 FOR_EACH_CLONE (clone, fn)
1444 {
1445 DECL_DECLARED_INLINE_P (clone)
1446 = DECL_DECLARED_INLINE_P (fn);
1447 DECL_SOURCE_LOCATION (clone)
1448 = DECL_SOURCE_LOCATION (fn);
1449 }
1450 check_specialization_namespace (tmpl);
1451
1452 return fn;
1453 }
1454 }
1455 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1456 {
1457 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1458 /* Dup decl failed, but this is a new definition. Set the
1459 line number so any errors match this new
1460 definition. */
1461 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1462
1463 return fn;
1464 }
1465 }
1466 else if (fn)
1467 return duplicate_decls (spec, fn, is_friend);
1468
1469 /* A specialization must be declared in the same namespace as the
1470 template it is specializing. */
1471 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1472 && !check_specialization_namespace (tmpl))
1473 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1474
1475 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1476 {
1477 spec_entry *entry = ggc_alloc_spec_entry ();
1478 gcc_assert (tmpl && args && spec);
1479 *entry = elt;
1480 *slot = entry;
1481 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1482 && PRIMARY_TEMPLATE_P (tmpl)
1483 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1484 /* TMPL is a forward declaration of a template function; keep a list
1485 of all specializations in case we need to reassign them to a friend
1486 template later in tsubst_friend_function. */
1487 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1488 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1489 }
1490
1491 return spec;
1492 }
1493
1494 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1495 TMPL and ARGS members, ignores SPEC. */
1496
1497 int comparing_specializations;
1498
1499 static int
1500 eq_specializations (const void *p1, const void *p2)
1501 {
1502 const spec_entry *e1 = (const spec_entry *)p1;
1503 const spec_entry *e2 = (const spec_entry *)p2;
1504 int equal;
1505
1506 ++comparing_specializations;
1507 equal = (e1->tmpl == e2->tmpl
1508 && comp_template_args (e1->args, e2->args));
1509 --comparing_specializations;
1510
1511 return equal;
1512 }
1513
1514 /* Returns a hash for a template TMPL and template arguments ARGS. */
1515
1516 static hashval_t
1517 hash_tmpl_and_args (tree tmpl, tree args)
1518 {
1519 hashval_t val = DECL_UID (tmpl);
1520 return iterative_hash_template_arg (args, val);
1521 }
1522
1523 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1524 ignoring SPEC. */
1525
1526 static hashval_t
1527 hash_specialization (const void *p)
1528 {
1529 const spec_entry *e = (const spec_entry *)p;
1530 return hash_tmpl_and_args (e->tmpl, e->args);
1531 }
1532
1533 /* Recursively calculate a hash value for a template argument ARG, for use
1534 in the hash tables of template specializations. */
1535
1536 hashval_t
1537 iterative_hash_template_arg (tree arg, hashval_t val)
1538 {
1539 unsigned HOST_WIDE_INT i;
1540 enum tree_code code;
1541 char tclass;
1542
1543 if (arg == NULL_TREE)
1544 return iterative_hash_object (arg, val);
1545
1546 if (!TYPE_P (arg))
1547 STRIP_NOPS (arg);
1548
1549 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1550 /* We can get one of these when re-hashing a previous entry in the middle
1551 of substituting into a pack expansion. Just look through it. */
1552 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1553
1554 code = TREE_CODE (arg);
1555 tclass = TREE_CODE_CLASS (code);
1556
1557 val = iterative_hash_object (code, val);
1558
1559 switch (code)
1560 {
1561 case ERROR_MARK:
1562 return val;
1563
1564 case IDENTIFIER_NODE:
1565 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1566
1567 case TREE_VEC:
1568 {
1569 int i, len = TREE_VEC_LENGTH (arg);
1570 for (i = 0; i < len; ++i)
1571 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1572 return val;
1573 }
1574
1575 case TYPE_PACK_EXPANSION:
1576 case EXPR_PACK_EXPANSION:
1577 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1578 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1579
1580 case TYPE_ARGUMENT_PACK:
1581 case NONTYPE_ARGUMENT_PACK:
1582 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1583
1584 case TREE_LIST:
1585 for (; arg; arg = TREE_CHAIN (arg))
1586 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1587 return val;
1588
1589 case OVERLOAD:
1590 for (; arg; arg = OVL_NEXT (arg))
1591 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1592 return val;
1593
1594 case CONSTRUCTOR:
1595 {
1596 tree field, value;
1597 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1598 {
1599 val = iterative_hash_template_arg (field, val);
1600 val = iterative_hash_template_arg (value, val);
1601 }
1602 return val;
1603 }
1604
1605 case PARM_DECL:
1606 if (!DECL_ARTIFICIAL (arg))
1607 {
1608 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1609 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1610 }
1611 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1612
1613 case TARGET_EXPR:
1614 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1615
1616 case PTRMEM_CST:
1617 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1618 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1619
1620 case TEMPLATE_PARM_INDEX:
1621 val = iterative_hash_template_arg
1622 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1623 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1624 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1625
1626 case TRAIT_EXPR:
1627 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1628 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1629 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1630
1631 case BASELINK:
1632 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1633 val);
1634 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1635 val);
1636
1637 case MODOP_EXPR:
1638 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1639 code = TREE_CODE (TREE_OPERAND (arg, 1));
1640 val = iterative_hash_object (code, val);
1641 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1642
1643 case LAMBDA_EXPR:
1644 /* A lambda can't appear in a template arg, but don't crash on
1645 erroneous input. */
1646 gcc_assert (seen_error ());
1647 return val;
1648
1649 case CAST_EXPR:
1650 case IMPLICIT_CONV_EXPR:
1651 case STATIC_CAST_EXPR:
1652 case REINTERPRET_CAST_EXPR:
1653 case CONST_CAST_EXPR:
1654 case DYNAMIC_CAST_EXPR:
1655 case NEW_EXPR:
1656 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1657 /* Now hash operands as usual. */
1658 break;
1659
1660 default:
1661 break;
1662 }
1663
1664 switch (tclass)
1665 {
1666 case tcc_type:
1667 if (TYPE_CANONICAL (arg))
1668 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1669 val);
1670 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1671 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1672 /* Otherwise just compare the types during lookup. */
1673 return val;
1674
1675 case tcc_declaration:
1676 case tcc_constant:
1677 return iterative_hash_expr (arg, val);
1678
1679 default:
1680 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1681 {
1682 unsigned n = cp_tree_operand_length (arg);
1683 for (i = 0; i < n; ++i)
1684 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1685 return val;
1686 }
1687 }
1688 gcc_unreachable ();
1689 return 0;
1690 }
1691
1692 /* Unregister the specialization SPEC as a specialization of TMPL.
1693 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1694 if the SPEC was listed as a specialization of TMPL.
1695
1696 Note that SPEC has been ggc_freed, so we can't look inside it. */
1697
1698 bool
1699 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1700 {
1701 spec_entry *entry;
1702 spec_entry elt;
1703
1704 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1705 elt.args = TI_ARGS (tinfo);
1706 elt.spec = NULL_TREE;
1707
1708 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1709 if (entry != NULL)
1710 {
1711 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1712 gcc_assert (new_spec != NULL_TREE);
1713 entry->spec = new_spec;
1714 return 1;
1715 }
1716
1717 return 0;
1718 }
1719
1720 /* Like register_specialization, but for local declarations. We are
1721 registering SPEC, an instantiation of TMPL. */
1722
1723 static void
1724 register_local_specialization (tree spec, tree tmpl)
1725 {
1726 void **slot;
1727
1728 slot = pointer_map_insert (local_specializations, tmpl);
1729 *slot = spec;
1730 }
1731
1732 /* TYPE is a class type. Returns true if TYPE is an explicitly
1733 specialized class. */
1734
1735 bool
1736 explicit_class_specialization_p (tree type)
1737 {
1738 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1739 return false;
1740 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1741 }
1742
1743 /* Print the list of functions at FNS, going through all the overloads
1744 for each element of the list. Alternatively, FNS can not be a
1745 TREE_LIST, in which case it will be printed together with all the
1746 overloads.
1747
1748 MORE and *STR should respectively be FALSE and NULL when the function
1749 is called from the outside. They are used internally on recursive
1750 calls. print_candidates manages the two parameters and leaves NULL
1751 in *STR when it ends. */
1752
1753 static void
1754 print_candidates_1 (tree fns, bool more, const char **str)
1755 {
1756 tree fn, fn2;
1757 char *spaces = NULL;
1758
1759 for (fn = fns; fn; fn = OVL_NEXT (fn))
1760 if (TREE_CODE (fn) == TREE_LIST)
1761 {
1762 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1763 print_candidates_1 (TREE_VALUE (fn2),
1764 TREE_CHAIN (fn2) || more, str);
1765 }
1766 else
1767 {
1768 tree cand = OVL_CURRENT (fn);
1769 if (!*str)
1770 {
1771 /* Pick the prefix string. */
1772 if (!more && !OVL_NEXT (fns))
1773 {
1774 inform (DECL_SOURCE_LOCATION (cand),
1775 "candidate is: %#D", cand);
1776 continue;
1777 }
1778
1779 *str = _("candidates are:");
1780 spaces = get_spaces (*str);
1781 }
1782 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1783 *str = spaces ? spaces : *str;
1784 }
1785
1786 if (!more)
1787 {
1788 free (spaces);
1789 *str = NULL;
1790 }
1791 }
1792
1793 /* Print the list of candidate FNS in an error message. FNS can also
1794 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1795
1796 void
1797 print_candidates (tree fns)
1798 {
1799 const char *str = NULL;
1800 print_candidates_1 (fns, false, &str);
1801 gcc_assert (str == NULL);
1802 }
1803
1804 /* Returns the template (one of the functions given by TEMPLATE_ID)
1805 which can be specialized to match the indicated DECL with the
1806 explicit template args given in TEMPLATE_ID. The DECL may be
1807 NULL_TREE if none is available. In that case, the functions in
1808 TEMPLATE_ID are non-members.
1809
1810 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1811 specialization of a member template.
1812
1813 The TEMPLATE_COUNT is the number of references to qualifying
1814 template classes that appeared in the name of the function. See
1815 check_explicit_specialization for a more accurate description.
1816
1817 TSK indicates what kind of template declaration (if any) is being
1818 declared. TSK_TEMPLATE indicates that the declaration given by
1819 DECL, though a FUNCTION_DECL, has template parameters, and is
1820 therefore a template function.
1821
1822 The template args (those explicitly specified and those deduced)
1823 are output in a newly created vector *TARGS_OUT.
1824
1825 If it is impossible to determine the result, an error message is
1826 issued. The error_mark_node is returned to indicate failure. */
1827
1828 static tree
1829 determine_specialization (tree template_id,
1830 tree decl,
1831 tree* targs_out,
1832 int need_member_template,
1833 int template_count,
1834 tmpl_spec_kind tsk)
1835 {
1836 tree fns;
1837 tree targs;
1838 tree explicit_targs;
1839 tree candidates = NULL_TREE;
1840 /* A TREE_LIST of templates of which DECL may be a specialization.
1841 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1842 corresponding TREE_PURPOSE is the set of template arguments that,
1843 when used to instantiate the template, would produce a function
1844 with the signature of DECL. */
1845 tree templates = NULL_TREE;
1846 int header_count;
1847 cp_binding_level *b;
1848
1849 *targs_out = NULL_TREE;
1850
1851 if (template_id == error_mark_node || decl == error_mark_node)
1852 return error_mark_node;
1853
1854 /* We shouldn't be specializing a member template of an
1855 unspecialized class template; we already gave an error in
1856 check_specialization_scope, now avoid crashing. */
1857 if (template_count && DECL_CLASS_SCOPE_P (decl)
1858 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1859 {
1860 gcc_assert (errorcount);
1861 return error_mark_node;
1862 }
1863
1864 fns = TREE_OPERAND (template_id, 0);
1865 explicit_targs = TREE_OPERAND (template_id, 1);
1866
1867 if (fns == error_mark_node)
1868 return error_mark_node;
1869
1870 /* Check for baselinks. */
1871 if (BASELINK_P (fns))
1872 fns = BASELINK_FUNCTIONS (fns);
1873
1874 if (!is_overloaded_fn (fns))
1875 {
1876 error ("%qD is not a function template", fns);
1877 return error_mark_node;
1878 }
1879
1880 /* Count the number of template headers specified for this
1881 specialization. */
1882 header_count = 0;
1883 for (b = current_binding_level;
1884 b->kind == sk_template_parms;
1885 b = b->level_chain)
1886 ++header_count;
1887
1888 for (; fns; fns = OVL_NEXT (fns))
1889 {
1890 tree fn = OVL_CURRENT (fns);
1891
1892 if (TREE_CODE (fn) == TEMPLATE_DECL)
1893 {
1894 tree decl_arg_types;
1895 tree fn_arg_types;
1896 tree insttype;
1897
1898 /* In case of explicit specialization, we need to check if
1899 the number of template headers appearing in the specialization
1900 is correct. This is usually done in check_explicit_specialization,
1901 but the check done there cannot be exhaustive when specializing
1902 member functions. Consider the following code:
1903
1904 template <> void A<int>::f(int);
1905 template <> template <> void A<int>::f(int);
1906
1907 Assuming that A<int> is not itself an explicit specialization
1908 already, the first line specializes "f" which is a non-template
1909 member function, whilst the second line specializes "f" which
1910 is a template member function. So both lines are syntactically
1911 correct, and check_explicit_specialization does not reject
1912 them.
1913
1914 Here, we can do better, as we are matching the specialization
1915 against the declarations. We count the number of template
1916 headers, and we check if they match TEMPLATE_COUNT + 1
1917 (TEMPLATE_COUNT is the number of qualifying template classes,
1918 plus there must be another header for the member template
1919 itself).
1920
1921 Notice that if header_count is zero, this is not a
1922 specialization but rather a template instantiation, so there
1923 is no check we can perform here. */
1924 if (header_count && header_count != template_count + 1)
1925 continue;
1926
1927 /* Check that the number of template arguments at the
1928 innermost level for DECL is the same as for FN. */
1929 if (current_binding_level->kind == sk_template_parms
1930 && !current_binding_level->explicit_spec_p
1931 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1932 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1933 (current_template_parms))))
1934 continue;
1935
1936 /* DECL might be a specialization of FN. */
1937 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1938 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1939
1940 /* For a non-static member function, we need to make sure
1941 that the const qualification is the same. Since
1942 get_bindings does not try to merge the "this" parameter,
1943 we must do the comparison explicitly. */
1944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1945 && !same_type_p (TREE_VALUE (fn_arg_types),
1946 TREE_VALUE (decl_arg_types)))
1947 continue;
1948
1949 /* Skip the "this" parameter and, for constructors of
1950 classes with virtual bases, the VTT parameter. A
1951 full specialization of a constructor will have a VTT
1952 parameter, but a template never will. */
1953 decl_arg_types
1954 = skip_artificial_parms_for (decl, decl_arg_types);
1955 fn_arg_types
1956 = skip_artificial_parms_for (fn, fn_arg_types);
1957
1958 /* Function templates cannot be specializations; there are
1959 no partial specializations of functions. Therefore, if
1960 the type of DECL does not match FN, there is no
1961 match. */
1962 if (tsk == tsk_template)
1963 {
1964 if (compparms (fn_arg_types, decl_arg_types))
1965 candidates = tree_cons (NULL_TREE, fn, candidates);
1966 continue;
1967 }
1968
1969 /* See whether this function might be a specialization of this
1970 template. Suppress access control because we might be trying
1971 to make this specialization a friend, and we have already done
1972 access control for the declaration of the specialization. */
1973 push_deferring_access_checks (dk_no_check);
1974 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1975 pop_deferring_access_checks ();
1976
1977 if (!targs)
1978 /* We cannot deduce template arguments that when used to
1979 specialize TMPL will produce DECL. */
1980 continue;
1981
1982 /* Make sure that the deduced arguments actually work. */
1983 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1984 if (insttype == error_mark_node)
1985 continue;
1986 fn_arg_types
1987 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1988 if (!compparms (fn_arg_types, decl_arg_types))
1989 continue;
1990
1991 /* Save this template, and the arguments deduced. */
1992 templates = tree_cons (targs, fn, templates);
1993 }
1994 else if (need_member_template)
1995 /* FN is an ordinary member function, and we need a
1996 specialization of a member template. */
1997 ;
1998 else if (TREE_CODE (fn) != FUNCTION_DECL)
1999 /* We can get IDENTIFIER_NODEs here in certain erroneous
2000 cases. */
2001 ;
2002 else if (!DECL_FUNCTION_MEMBER_P (fn))
2003 /* This is just an ordinary non-member function. Nothing can
2004 be a specialization of that. */
2005 ;
2006 else if (DECL_ARTIFICIAL (fn))
2007 /* Cannot specialize functions that are created implicitly. */
2008 ;
2009 else
2010 {
2011 tree decl_arg_types;
2012
2013 /* This is an ordinary member function. However, since
2014 we're here, we can assume its enclosing class is a
2015 template class. For example,
2016
2017 template <typename T> struct S { void f(); };
2018 template <> void S<int>::f() {}
2019
2020 Here, S<int>::f is a non-template, but S<int> is a
2021 template class. If FN has the same type as DECL, we
2022 might be in business. */
2023
2024 if (!DECL_TEMPLATE_INFO (fn))
2025 /* Its enclosing class is an explicit specialization
2026 of a template class. This is not a candidate. */
2027 continue;
2028
2029 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2030 TREE_TYPE (TREE_TYPE (fn))))
2031 /* The return types differ. */
2032 continue;
2033
2034 /* Adjust the type of DECL in case FN is a static member. */
2035 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2036 if (DECL_STATIC_FUNCTION_P (fn)
2037 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2038 decl_arg_types = TREE_CHAIN (decl_arg_types);
2039
2040 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2041 decl_arg_types))
2042 /* They match! */
2043 candidates = tree_cons (NULL_TREE, fn, candidates);
2044 }
2045 }
2046
2047 if (templates && TREE_CHAIN (templates))
2048 {
2049 /* We have:
2050
2051 [temp.expl.spec]
2052
2053 It is possible for a specialization with a given function
2054 signature to be instantiated from more than one function
2055 template. In such cases, explicit specification of the
2056 template arguments must be used to uniquely identify the
2057 function template specialization being specialized.
2058
2059 Note that here, there's no suggestion that we're supposed to
2060 determine which of the candidate templates is most
2061 specialized. However, we, also have:
2062
2063 [temp.func.order]
2064
2065 Partial ordering of overloaded function template
2066 declarations is used in the following contexts to select
2067 the function template to which a function template
2068 specialization refers:
2069
2070 -- when an explicit specialization refers to a function
2071 template.
2072
2073 So, we do use the partial ordering rules, at least for now.
2074 This extension can only serve to make invalid programs valid,
2075 so it's safe. And, there is strong anecdotal evidence that
2076 the committee intended the partial ordering rules to apply;
2077 the EDG front end has that behavior, and John Spicer claims
2078 that the committee simply forgot to delete the wording in
2079 [temp.expl.spec]. */
2080 tree tmpl = most_specialized_instantiation (templates);
2081 if (tmpl != error_mark_node)
2082 {
2083 templates = tmpl;
2084 TREE_CHAIN (templates) = NULL_TREE;
2085 }
2086 }
2087
2088 if (templates == NULL_TREE && candidates == NULL_TREE)
2089 {
2090 error ("template-id %qD for %q+D does not match any template "
2091 "declaration", template_id, decl);
2092 if (header_count && header_count != template_count + 1)
2093 inform (input_location, "saw %d %<template<>%>, need %d for "
2094 "specializing a member function template",
2095 header_count, template_count + 1);
2096 return error_mark_node;
2097 }
2098 else if ((templates && TREE_CHAIN (templates))
2099 || (candidates && TREE_CHAIN (candidates))
2100 || (templates && candidates))
2101 {
2102 error ("ambiguous template specialization %qD for %q+D",
2103 template_id, decl);
2104 candidates = chainon (candidates, templates);
2105 print_candidates (candidates);
2106 return error_mark_node;
2107 }
2108
2109 /* We have one, and exactly one, match. */
2110 if (candidates)
2111 {
2112 tree fn = TREE_VALUE (candidates);
2113 *targs_out = copy_node (DECL_TI_ARGS (fn));
2114 /* DECL is a re-declaration or partial instantiation of a template
2115 function. */
2116 if (TREE_CODE (fn) == TEMPLATE_DECL)
2117 return fn;
2118 /* It was a specialization of an ordinary member function in a
2119 template class. */
2120 return DECL_TI_TEMPLATE (fn);
2121 }
2122
2123 /* It was a specialization of a template. */
2124 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2125 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2126 {
2127 *targs_out = copy_node (targs);
2128 SET_TMPL_ARGS_LEVEL (*targs_out,
2129 TMPL_ARGS_DEPTH (*targs_out),
2130 TREE_PURPOSE (templates));
2131 }
2132 else
2133 *targs_out = TREE_PURPOSE (templates);
2134 return TREE_VALUE (templates);
2135 }
2136
2137 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2138 but with the default argument values filled in from those in the
2139 TMPL_TYPES. */
2140
2141 static tree
2142 copy_default_args_to_explicit_spec_1 (tree spec_types,
2143 tree tmpl_types)
2144 {
2145 tree new_spec_types;
2146
2147 if (!spec_types)
2148 return NULL_TREE;
2149
2150 if (spec_types == void_list_node)
2151 return void_list_node;
2152
2153 /* Substitute into the rest of the list. */
2154 new_spec_types =
2155 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2156 TREE_CHAIN (tmpl_types));
2157
2158 /* Add the default argument for this parameter. */
2159 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2160 TREE_VALUE (spec_types),
2161 new_spec_types);
2162 }
2163
2164 /* DECL is an explicit specialization. Replicate default arguments
2165 from the template it specializes. (That way, code like:
2166
2167 template <class T> void f(T = 3);
2168 template <> void f(double);
2169 void g () { f (); }
2170
2171 works, as required.) An alternative approach would be to look up
2172 the correct default arguments at the call-site, but this approach
2173 is consistent with how implicit instantiations are handled. */
2174
2175 static void
2176 copy_default_args_to_explicit_spec (tree decl)
2177 {
2178 tree tmpl;
2179 tree spec_types;
2180 tree tmpl_types;
2181 tree new_spec_types;
2182 tree old_type;
2183 tree new_type;
2184 tree t;
2185 tree object_type = NULL_TREE;
2186 tree in_charge = NULL_TREE;
2187 tree vtt = NULL_TREE;
2188
2189 /* See if there's anything we need to do. */
2190 tmpl = DECL_TI_TEMPLATE (decl);
2191 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2192 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2193 if (TREE_PURPOSE (t))
2194 break;
2195 if (!t)
2196 return;
2197
2198 old_type = TREE_TYPE (decl);
2199 spec_types = TYPE_ARG_TYPES (old_type);
2200
2201 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2202 {
2203 /* Remove the this pointer, but remember the object's type for
2204 CV quals. */
2205 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2206 spec_types = TREE_CHAIN (spec_types);
2207 tmpl_types = TREE_CHAIN (tmpl_types);
2208
2209 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2210 {
2211 /* DECL may contain more parameters than TMPL due to the extra
2212 in-charge parameter in constructors and destructors. */
2213 in_charge = spec_types;
2214 spec_types = TREE_CHAIN (spec_types);
2215 }
2216 if (DECL_HAS_VTT_PARM_P (decl))
2217 {
2218 vtt = spec_types;
2219 spec_types = TREE_CHAIN (spec_types);
2220 }
2221 }
2222
2223 /* Compute the merged default arguments. */
2224 new_spec_types =
2225 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2226
2227 /* Compute the new FUNCTION_TYPE. */
2228 if (object_type)
2229 {
2230 if (vtt)
2231 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2232 TREE_VALUE (vtt),
2233 new_spec_types);
2234
2235 if (in_charge)
2236 /* Put the in-charge parameter back. */
2237 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2238 TREE_VALUE (in_charge),
2239 new_spec_types);
2240
2241 new_type = build_method_type_directly (object_type,
2242 TREE_TYPE (old_type),
2243 new_spec_types);
2244 }
2245 else
2246 new_type = build_function_type (TREE_TYPE (old_type),
2247 new_spec_types);
2248 new_type = cp_build_type_attribute_variant (new_type,
2249 TYPE_ATTRIBUTES (old_type));
2250 new_type = build_exception_variant (new_type,
2251 TYPE_RAISES_EXCEPTIONS (old_type));
2252 TREE_TYPE (decl) = new_type;
2253 }
2254
2255 /* Return the number of template headers we expect to see for a definition
2256 or specialization of CTYPE or one of its non-template members. */
2257
2258 int
2259 num_template_headers_for_class (tree ctype)
2260 {
2261 int num_templates = 0;
2262
2263 while (ctype && CLASS_TYPE_P (ctype))
2264 {
2265 /* You're supposed to have one `template <...>' for every
2266 template class, but you don't need one for a full
2267 specialization. For example:
2268
2269 template <class T> struct S{};
2270 template <> struct S<int> { void f(); };
2271 void S<int>::f () {}
2272
2273 is correct; there shouldn't be a `template <>' for the
2274 definition of `S<int>::f'. */
2275 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2276 /* If CTYPE does not have template information of any
2277 kind, then it is not a template, nor is it nested
2278 within a template. */
2279 break;
2280 if (explicit_class_specialization_p (ctype))
2281 break;
2282 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2283 ++num_templates;
2284
2285 ctype = TYPE_CONTEXT (ctype);
2286 }
2287
2288 return num_templates;
2289 }
2290
2291 /* Do a simple sanity check on the template headers that precede the
2292 variable declaration DECL. */
2293
2294 void
2295 check_template_variable (tree decl)
2296 {
2297 tree ctx = CP_DECL_CONTEXT (decl);
2298 int wanted = num_template_headers_for_class (ctx);
2299 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2300 permerror (DECL_SOURCE_LOCATION (decl),
2301 "%qD is not a static data member of a class template", decl);
2302 else if (template_header_count > wanted)
2303 {
2304 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2305 "too many template headers for %D (should be %d)",
2306 decl, wanted);
2307 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2308 inform (DECL_SOURCE_LOCATION (decl),
2309 "members of an explicitly specialized class are defined "
2310 "without a template header");
2311 }
2312 }
2313
2314 /* Check to see if the function just declared, as indicated in
2315 DECLARATOR, and in DECL, is a specialization of a function
2316 template. We may also discover that the declaration is an explicit
2317 instantiation at this point.
2318
2319 Returns DECL, or an equivalent declaration that should be used
2320 instead if all goes well. Issues an error message if something is
2321 amiss. Returns error_mark_node if the error is not easily
2322 recoverable.
2323
2324 FLAGS is a bitmask consisting of the following flags:
2325
2326 2: The function has a definition.
2327 4: The function is a friend.
2328
2329 The TEMPLATE_COUNT is the number of references to qualifying
2330 template classes that appeared in the name of the function. For
2331 example, in
2332
2333 template <class T> struct S { void f(); };
2334 void S<int>::f();
2335
2336 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2337 classes are not counted in the TEMPLATE_COUNT, so that in
2338
2339 template <class T> struct S {};
2340 template <> struct S<int> { void f(); }
2341 template <> void S<int>::f();
2342
2343 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2344 invalid; there should be no template <>.)
2345
2346 If the function is a specialization, it is marked as such via
2347 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2348 is set up correctly, and it is added to the list of specializations
2349 for that template. */
2350
2351 tree
2352 check_explicit_specialization (tree declarator,
2353 tree decl,
2354 int template_count,
2355 int flags)
2356 {
2357 int have_def = flags & 2;
2358 int is_friend = flags & 4;
2359 int specialization = 0;
2360 int explicit_instantiation = 0;
2361 int member_specialization = 0;
2362 tree ctype = DECL_CLASS_CONTEXT (decl);
2363 tree dname = DECL_NAME (decl);
2364 tmpl_spec_kind tsk;
2365
2366 if (is_friend)
2367 {
2368 if (!processing_specialization)
2369 tsk = tsk_none;
2370 else
2371 tsk = tsk_excessive_parms;
2372 }
2373 else
2374 tsk = current_tmpl_spec_kind (template_count);
2375
2376 switch (tsk)
2377 {
2378 case tsk_none:
2379 if (processing_specialization)
2380 {
2381 specialization = 1;
2382 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2383 }
2384 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2385 {
2386 if (is_friend)
2387 /* This could be something like:
2388
2389 template <class T> void f(T);
2390 class S { friend void f<>(int); } */
2391 specialization = 1;
2392 else
2393 {
2394 /* This case handles bogus declarations like template <>
2395 template <class T> void f<int>(); */
2396
2397 error ("template-id %qD in declaration of primary template",
2398 declarator);
2399 return decl;
2400 }
2401 }
2402 break;
2403
2404 case tsk_invalid_member_spec:
2405 /* The error has already been reported in
2406 check_specialization_scope. */
2407 return error_mark_node;
2408
2409 case tsk_invalid_expl_inst:
2410 error ("template parameter list used in explicit instantiation");
2411
2412 /* Fall through. */
2413
2414 case tsk_expl_inst:
2415 if (have_def)
2416 error ("definition provided for explicit instantiation");
2417
2418 explicit_instantiation = 1;
2419 break;
2420
2421 case tsk_excessive_parms:
2422 case tsk_insufficient_parms:
2423 if (tsk == tsk_excessive_parms)
2424 error ("too many template parameter lists in declaration of %qD",
2425 decl);
2426 else if (template_header_count)
2427 error("too few template parameter lists in declaration of %qD", decl);
2428 else
2429 error("explicit specialization of %qD must be introduced by "
2430 "%<template <>%>", decl);
2431
2432 /* Fall through. */
2433 case tsk_expl_spec:
2434 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2435 if (ctype)
2436 member_specialization = 1;
2437 else
2438 specialization = 1;
2439 break;
2440
2441 case tsk_template:
2442 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2443 {
2444 /* This case handles bogus declarations like template <>
2445 template <class T> void f<int>(); */
2446
2447 if (uses_template_parms (declarator))
2448 error ("function template partial specialization %qD "
2449 "is not allowed", declarator);
2450 else
2451 error ("template-id %qD in declaration of primary template",
2452 declarator);
2453 return decl;
2454 }
2455
2456 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2457 /* This is a specialization of a member template, without
2458 specialization the containing class. Something like:
2459
2460 template <class T> struct S {
2461 template <class U> void f (U);
2462 };
2463 template <> template <class U> void S<int>::f(U) {}
2464
2465 That's a specialization -- but of the entire template. */
2466 specialization = 1;
2467 break;
2468
2469 default:
2470 gcc_unreachable ();
2471 }
2472
2473 if (specialization || member_specialization)
2474 {
2475 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2476 for (; t; t = TREE_CHAIN (t))
2477 if (TREE_PURPOSE (t))
2478 {
2479 permerror (input_location,
2480 "default argument specified in explicit specialization");
2481 break;
2482 }
2483 }
2484
2485 if (specialization || member_specialization || explicit_instantiation)
2486 {
2487 tree tmpl = NULL_TREE;
2488 tree targs = NULL_TREE;
2489
2490 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2491 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2492 {
2493 tree fns;
2494
2495 gcc_assert (identifier_p (declarator));
2496 if (ctype)
2497 fns = dname;
2498 else
2499 {
2500 /* If there is no class context, the explicit instantiation
2501 must be at namespace scope. */
2502 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2503
2504 /* Find the namespace binding, using the declaration
2505 context. */
2506 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2507 false, true);
2508 if (fns == error_mark_node || !is_overloaded_fn (fns))
2509 {
2510 error ("%qD is not a template function", dname);
2511 fns = error_mark_node;
2512 }
2513 else
2514 {
2515 tree fn = OVL_CURRENT (fns);
2516 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2517 CP_DECL_CONTEXT (fn)))
2518 error ("%qD is not declared in %qD",
2519 decl, current_namespace);
2520 }
2521 }
2522
2523 declarator = lookup_template_function (fns, NULL_TREE);
2524 }
2525
2526 if (declarator == error_mark_node)
2527 return error_mark_node;
2528
2529 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2530 {
2531 if (!explicit_instantiation)
2532 /* A specialization in class scope. This is invalid,
2533 but the error will already have been flagged by
2534 check_specialization_scope. */
2535 return error_mark_node;
2536 else
2537 {
2538 /* It's not valid to write an explicit instantiation in
2539 class scope, e.g.:
2540
2541 class C { template void f(); }
2542
2543 This case is caught by the parser. However, on
2544 something like:
2545
2546 template class C { void f(); };
2547
2548 (which is invalid) we can get here. The error will be
2549 issued later. */
2550 ;
2551 }
2552
2553 return decl;
2554 }
2555 else if (ctype != NULL_TREE
2556 && (identifier_p (TREE_OPERAND (declarator, 0))))
2557 {
2558 /* Find the list of functions in ctype that have the same
2559 name as the declared function. */
2560 tree name = TREE_OPERAND (declarator, 0);
2561 tree fns = NULL_TREE;
2562 int idx;
2563
2564 if (constructor_name_p (name, ctype))
2565 {
2566 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2567
2568 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2569 : !CLASSTYPE_DESTRUCTORS (ctype))
2570 {
2571 /* From [temp.expl.spec]:
2572
2573 If such an explicit specialization for the member
2574 of a class template names an implicitly-declared
2575 special member function (clause _special_), the
2576 program is ill-formed.
2577
2578 Similar language is found in [temp.explicit]. */
2579 error ("specialization of implicitly-declared special member function");
2580 return error_mark_node;
2581 }
2582
2583 name = is_constructor ? ctor_identifier : dtor_identifier;
2584 }
2585
2586 if (!DECL_CONV_FN_P (decl))
2587 {
2588 idx = lookup_fnfields_1 (ctype, name);
2589 if (idx >= 0)
2590 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2591 }
2592 else
2593 {
2594 vec<tree, va_gc> *methods;
2595 tree ovl;
2596
2597 /* For a type-conversion operator, we cannot do a
2598 name-based lookup. We might be looking for `operator
2599 int' which will be a specialization of `operator T'.
2600 So, we find *all* the conversion operators, and then
2601 select from them. */
2602 fns = NULL_TREE;
2603
2604 methods = CLASSTYPE_METHOD_VEC (ctype);
2605 if (methods)
2606 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2607 methods->iterate (idx, &ovl);
2608 ++idx)
2609 {
2610 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2611 /* There are no more conversion functions. */
2612 break;
2613
2614 /* Glue all these conversion functions together
2615 with those we already have. */
2616 for (; ovl; ovl = OVL_NEXT (ovl))
2617 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2618 }
2619 }
2620
2621 if (fns == NULL_TREE)
2622 {
2623 error ("no member function %qD declared in %qT", name, ctype);
2624 return error_mark_node;
2625 }
2626 else
2627 TREE_OPERAND (declarator, 0) = fns;
2628 }
2629
2630 /* Figure out what exactly is being specialized at this point.
2631 Note that for an explicit instantiation, even one for a
2632 member function, we cannot tell apriori whether the
2633 instantiation is for a member template, or just a member
2634 function of a template class. Even if a member template is
2635 being instantiated, the member template arguments may be
2636 elided if they can be deduced from the rest of the
2637 declaration. */
2638 tmpl = determine_specialization (declarator, decl,
2639 &targs,
2640 member_specialization,
2641 template_count,
2642 tsk);
2643
2644 if (!tmpl || tmpl == error_mark_node)
2645 /* We couldn't figure out what this declaration was
2646 specializing. */
2647 return error_mark_node;
2648 else
2649 {
2650 tree gen_tmpl = most_general_template (tmpl);
2651
2652 if (explicit_instantiation)
2653 {
2654 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2655 is done by do_decl_instantiation later. */
2656
2657 int arg_depth = TMPL_ARGS_DEPTH (targs);
2658 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2659
2660 if (arg_depth > parm_depth)
2661 {
2662 /* If TMPL is not the most general template (for
2663 example, if TMPL is a friend template that is
2664 injected into namespace scope), then there will
2665 be too many levels of TARGS. Remove some of them
2666 here. */
2667 int i;
2668 tree new_targs;
2669
2670 new_targs = make_tree_vec (parm_depth);
2671 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2672 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2673 = TREE_VEC_ELT (targs, i);
2674 targs = new_targs;
2675 }
2676
2677 return instantiate_template (tmpl, targs, tf_error);
2678 }
2679
2680 /* If we thought that the DECL was a member function, but it
2681 turns out to be specializing a static member function,
2682 make DECL a static member function as well. */
2683 if (DECL_STATIC_FUNCTION_P (tmpl)
2684 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2685 revert_static_member_fn (decl);
2686
2687 /* If this is a specialization of a member template of a
2688 template class, we want to return the TEMPLATE_DECL, not
2689 the specialization of it. */
2690 if (tsk == tsk_template)
2691 {
2692 tree result = DECL_TEMPLATE_RESULT (tmpl);
2693 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2694 DECL_INITIAL (result) = NULL_TREE;
2695 if (have_def)
2696 {
2697 tree parm;
2698 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2699 DECL_SOURCE_LOCATION (result)
2700 = DECL_SOURCE_LOCATION (decl);
2701 /* We want to use the argument list specified in the
2702 definition, not in the original declaration. */
2703 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2704 for (parm = DECL_ARGUMENTS (result); parm;
2705 parm = DECL_CHAIN (parm))
2706 DECL_CONTEXT (parm) = result;
2707 }
2708 return register_specialization (tmpl, gen_tmpl, targs,
2709 is_friend, 0);
2710 }
2711
2712 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2713 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2714
2715 /* Inherit default function arguments from the template
2716 DECL is specializing. */
2717 copy_default_args_to_explicit_spec (decl);
2718
2719 /* This specialization has the same protection as the
2720 template it specializes. */
2721 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2722 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2723
2724 /* 7.1.1-1 [dcl.stc]
2725
2726 A storage-class-specifier shall not be specified in an
2727 explicit specialization...
2728
2729 The parser rejects these, so unless action is taken here,
2730 explicit function specializations will always appear with
2731 global linkage.
2732
2733 The action recommended by the C++ CWG in response to C++
2734 defect report 605 is to make the storage class and linkage
2735 of the explicit specialization match the templated function:
2736
2737 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2738 */
2739 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2740 {
2741 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2742 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2743
2744 /* This specialization has the same linkage and visibility as
2745 the function template it specializes. */
2746 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2747 if (! TREE_PUBLIC (decl))
2748 {
2749 DECL_INTERFACE_KNOWN (decl) = 1;
2750 DECL_NOT_REALLY_EXTERN (decl) = 1;
2751 }
2752 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2753 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2754 {
2755 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2756 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2757 }
2758 }
2759
2760 /* If DECL is a friend declaration, declared using an
2761 unqualified name, the namespace associated with DECL may
2762 have been set incorrectly. For example, in:
2763
2764 template <typename T> void f(T);
2765 namespace N {
2766 struct S { friend void f<int>(int); }
2767 }
2768
2769 we will have set the DECL_CONTEXT for the friend
2770 declaration to N, rather than to the global namespace. */
2771 if (DECL_NAMESPACE_SCOPE_P (decl))
2772 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2773
2774 if (is_friend && !have_def)
2775 /* This is not really a declaration of a specialization.
2776 It's just the name of an instantiation. But, it's not
2777 a request for an instantiation, either. */
2778 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2779 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2780 /* This is indeed a specialization. In case of constructors
2781 and destructors, we need in-charge and not-in-charge
2782 versions in V3 ABI. */
2783 clone_function_decl (decl, /*update_method_vec_p=*/0);
2784
2785 /* Register this specialization so that we can find it
2786 again. */
2787 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2788 }
2789 }
2790
2791 return decl;
2792 }
2793
2794 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2795 parameters. These are represented in the same format used for
2796 DECL_TEMPLATE_PARMS. */
2797
2798 int
2799 comp_template_parms (const_tree parms1, const_tree parms2)
2800 {
2801 const_tree p1;
2802 const_tree p2;
2803
2804 if (parms1 == parms2)
2805 return 1;
2806
2807 for (p1 = parms1, p2 = parms2;
2808 p1 != NULL_TREE && p2 != NULL_TREE;
2809 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2810 {
2811 tree t1 = TREE_VALUE (p1);
2812 tree t2 = TREE_VALUE (p2);
2813 int i;
2814
2815 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2816 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2817
2818 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2819 return 0;
2820
2821 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2822 {
2823 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2824 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2825
2826 /* If either of the template parameters are invalid, assume
2827 they match for the sake of error recovery. */
2828 if (parm1 == error_mark_node || parm2 == error_mark_node)
2829 return 1;
2830
2831 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2832 return 0;
2833
2834 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2835 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2836 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2837 continue;
2838 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2839 return 0;
2840 }
2841 }
2842
2843 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2844 /* One set of parameters has more parameters lists than the
2845 other. */
2846 return 0;
2847
2848 return 1;
2849 }
2850
2851 /* Determine whether PARM is a parameter pack. */
2852
2853 bool
2854 template_parameter_pack_p (const_tree parm)
2855 {
2856 /* Determine if we have a non-type template parameter pack. */
2857 if (TREE_CODE (parm) == PARM_DECL)
2858 return (DECL_TEMPLATE_PARM_P (parm)
2859 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2860 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2861 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2862
2863 /* If this is a list of template parameters, we could get a
2864 TYPE_DECL or a TEMPLATE_DECL. */
2865 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2866 parm = TREE_TYPE (parm);
2867
2868 /* Otherwise it must be a type template parameter. */
2869 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2870 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2871 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2872 }
2873
2874 /* Determine if T is a function parameter pack. */
2875
2876 bool
2877 function_parameter_pack_p (const_tree t)
2878 {
2879 if (t && TREE_CODE (t) == PARM_DECL)
2880 return DECL_PACK_P (t);
2881 return false;
2882 }
2883
2884 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2885 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2886
2887 tree
2888 get_function_template_decl (const_tree primary_func_tmpl_inst)
2889 {
2890 if (! primary_func_tmpl_inst
2891 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2892 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2893 return NULL;
2894
2895 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2896 }
2897
2898 /* Return true iff the function parameter PARAM_DECL was expanded
2899 from the function parameter pack PACK. */
2900
2901 bool
2902 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2903 {
2904 if (DECL_ARTIFICIAL (param_decl)
2905 || !function_parameter_pack_p (pack))
2906 return false;
2907
2908 /* The parameter pack and its pack arguments have the same
2909 DECL_PARM_INDEX. */
2910 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2911 }
2912
2913 /* Determine whether ARGS describes a variadic template args list,
2914 i.e., one that is terminated by a template argument pack. */
2915
2916 static bool
2917 template_args_variadic_p (tree args)
2918 {
2919 int nargs;
2920 tree last_parm;
2921
2922 if (args == NULL_TREE)
2923 return false;
2924
2925 args = INNERMOST_TEMPLATE_ARGS (args);
2926 nargs = TREE_VEC_LENGTH (args);
2927
2928 if (nargs == 0)
2929 return false;
2930
2931 last_parm = TREE_VEC_ELT (args, nargs - 1);
2932
2933 return ARGUMENT_PACK_P (last_parm);
2934 }
2935
2936 /* Generate a new name for the parameter pack name NAME (an
2937 IDENTIFIER_NODE) that incorporates its */
2938
2939 static tree
2940 make_ith_pack_parameter_name (tree name, int i)
2941 {
2942 /* Munge the name to include the parameter index. */
2943 #define NUMBUF_LEN 128
2944 char numbuf[NUMBUF_LEN];
2945 char* newname;
2946 int newname_len;
2947
2948 if (name == NULL_TREE)
2949 return name;
2950 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2951 newname_len = IDENTIFIER_LENGTH (name)
2952 + strlen (numbuf) + 2;
2953 newname = (char*)alloca (newname_len);
2954 snprintf (newname, newname_len,
2955 "%s#%i", IDENTIFIER_POINTER (name), i);
2956 return get_identifier (newname);
2957 }
2958
2959 /* Return true if T is a primary function, class or alias template
2960 instantiation. */
2961
2962 bool
2963 primary_template_instantiation_p (const_tree t)
2964 {
2965 if (!t)
2966 return false;
2967
2968 if (TREE_CODE (t) == FUNCTION_DECL)
2969 return DECL_LANG_SPECIFIC (t)
2970 && DECL_TEMPLATE_INSTANTIATION (t)
2971 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2972 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2973 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2974 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2975 else if (alias_template_specialization_p (t))
2976 return true;
2977 return false;
2978 }
2979
2980 /* Return true if PARM is a template template parameter. */
2981
2982 bool
2983 template_template_parameter_p (const_tree parm)
2984 {
2985 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2986 }
2987
2988 /* Return true iff PARM is a DECL representing a type template
2989 parameter. */
2990
2991 bool
2992 template_type_parameter_p (const_tree parm)
2993 {
2994 return (parm
2995 && (TREE_CODE (parm) == TYPE_DECL
2996 || TREE_CODE (parm) == TEMPLATE_DECL)
2997 && DECL_TEMPLATE_PARM_P (parm));
2998 }
2999
3000 /* Return the template parameters of T if T is a
3001 primary template instantiation, NULL otherwise. */
3002
3003 tree
3004 get_primary_template_innermost_parameters (const_tree t)
3005 {
3006 tree parms = NULL, template_info = NULL;
3007
3008 if ((template_info = get_template_info (t))
3009 && primary_template_instantiation_p (t))
3010 parms = INNERMOST_TEMPLATE_PARMS
3011 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3012
3013 return parms;
3014 }
3015
3016 /* Return the template parameters of the LEVELth level from the full list
3017 of template parameters PARMS. */
3018
3019 tree
3020 get_template_parms_at_level (tree parms, int level)
3021 {
3022 tree p;
3023 if (!parms
3024 || TREE_CODE (parms) != TREE_LIST
3025 || level > TMPL_PARMS_DEPTH (parms))
3026 return NULL_TREE;
3027
3028 for (p = parms; p; p = TREE_CHAIN (p))
3029 if (TMPL_PARMS_DEPTH (p) == level)
3030 return p;
3031
3032 return NULL_TREE;
3033 }
3034
3035 /* Returns the template arguments of T if T is a template instantiation,
3036 NULL otherwise. */
3037
3038 tree
3039 get_template_innermost_arguments (const_tree t)
3040 {
3041 tree args = NULL, template_info = NULL;
3042
3043 if ((template_info = get_template_info (t))
3044 && TI_ARGS (template_info))
3045 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3046
3047 return args;
3048 }
3049
3050 /* Return the argument pack elements of T if T is a template argument pack,
3051 NULL otherwise. */
3052
3053 tree
3054 get_template_argument_pack_elems (const_tree t)
3055 {
3056 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3057 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3058 return NULL;
3059
3060 return ARGUMENT_PACK_ARGS (t);
3061 }
3062
3063 /* Structure used to track the progress of find_parameter_packs_r. */
3064 struct find_parameter_pack_data
3065 {
3066 /* TREE_LIST that will contain all of the parameter packs found by
3067 the traversal. */
3068 tree* parameter_packs;
3069
3070 /* Set of AST nodes that have been visited by the traversal. */
3071 struct pointer_set_t *visited;
3072 };
3073
3074 /* Identifies all of the argument packs that occur in a template
3075 argument and appends them to the TREE_LIST inside DATA, which is a
3076 find_parameter_pack_data structure. This is a subroutine of
3077 make_pack_expansion and uses_parameter_packs. */
3078 static tree
3079 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3080 {
3081 tree t = *tp;
3082 struct find_parameter_pack_data* ppd =
3083 (struct find_parameter_pack_data*)data;
3084 bool parameter_pack_p = false;
3085
3086 /* Handle type aliases/typedefs. */
3087 if (TYPE_ALIAS_P (t))
3088 {
3089 if (TYPE_TEMPLATE_INFO (t))
3090 cp_walk_tree (&TYPE_TI_ARGS (t),
3091 &find_parameter_packs_r,
3092 ppd, ppd->visited);
3093 *walk_subtrees = 0;
3094 return NULL_TREE;
3095 }
3096
3097 /* Identify whether this is a parameter pack or not. */
3098 switch (TREE_CODE (t))
3099 {
3100 case TEMPLATE_PARM_INDEX:
3101 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3102 parameter_pack_p = true;
3103 break;
3104
3105 case TEMPLATE_TYPE_PARM:
3106 t = TYPE_MAIN_VARIANT (t);
3107 case TEMPLATE_TEMPLATE_PARM:
3108 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3109 parameter_pack_p = true;
3110 break;
3111
3112 case FIELD_DECL:
3113 case PARM_DECL:
3114 if (DECL_PACK_P (t))
3115 {
3116 /* We don't want to walk into the type of a PARM_DECL,
3117 because we don't want to see the type parameter pack. */
3118 *walk_subtrees = 0;
3119 parameter_pack_p = true;
3120 }
3121 break;
3122
3123 /* Look through a lambda capture proxy to the field pack. */
3124 case VAR_DECL:
3125 if (DECL_HAS_VALUE_EXPR_P (t))
3126 {
3127 tree v = DECL_VALUE_EXPR (t);
3128 cp_walk_tree (&v,
3129 &find_parameter_packs_r,
3130 ppd, ppd->visited);
3131 *walk_subtrees = 0;
3132 }
3133 break;
3134
3135 case BASES:
3136 parameter_pack_p = true;
3137 break;
3138 default:
3139 /* Not a parameter pack. */
3140 break;
3141 }
3142
3143 if (parameter_pack_p)
3144 {
3145 /* Add this parameter pack to the list. */
3146 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3147 }
3148
3149 if (TYPE_P (t))
3150 cp_walk_tree (&TYPE_CONTEXT (t),
3151 &find_parameter_packs_r, ppd, ppd->visited);
3152
3153 /* This switch statement will return immediately if we don't find a
3154 parameter pack. */
3155 switch (TREE_CODE (t))
3156 {
3157 case TEMPLATE_PARM_INDEX:
3158 return NULL_TREE;
3159
3160 case BOUND_TEMPLATE_TEMPLATE_PARM:
3161 /* Check the template itself. */
3162 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3163 &find_parameter_packs_r, ppd, ppd->visited);
3164 /* Check the template arguments. */
3165 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3166 ppd->visited);
3167 *walk_subtrees = 0;
3168 return NULL_TREE;
3169
3170 case TEMPLATE_TYPE_PARM:
3171 case TEMPLATE_TEMPLATE_PARM:
3172 return NULL_TREE;
3173
3174 case PARM_DECL:
3175 return NULL_TREE;
3176
3177 case RECORD_TYPE:
3178 if (TYPE_PTRMEMFUNC_P (t))
3179 return NULL_TREE;
3180 /* Fall through. */
3181
3182 case UNION_TYPE:
3183 case ENUMERAL_TYPE:
3184 if (TYPE_TEMPLATE_INFO (t))
3185 cp_walk_tree (&TYPE_TI_ARGS (t),
3186 &find_parameter_packs_r, ppd, ppd->visited);
3187
3188 *walk_subtrees = 0;
3189 return NULL_TREE;
3190
3191 case CONSTRUCTOR:
3192 case TEMPLATE_DECL:
3193 cp_walk_tree (&TREE_TYPE (t),
3194 &find_parameter_packs_r, ppd, ppd->visited);
3195 return NULL_TREE;
3196
3197 case TYPENAME_TYPE:
3198 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3199 ppd, ppd->visited);
3200 *walk_subtrees = 0;
3201 return NULL_TREE;
3202
3203 case TYPE_PACK_EXPANSION:
3204 case EXPR_PACK_EXPANSION:
3205 *walk_subtrees = 0;
3206 return NULL_TREE;
3207
3208 case INTEGER_TYPE:
3209 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3210 ppd, ppd->visited);
3211 *walk_subtrees = 0;
3212 return NULL_TREE;
3213
3214 case IDENTIFIER_NODE:
3215 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3216 ppd->visited);
3217 *walk_subtrees = 0;
3218 return NULL_TREE;
3219
3220 default:
3221 return NULL_TREE;
3222 }
3223
3224 return NULL_TREE;
3225 }
3226
3227 /* Determines if the expression or type T uses any parameter packs. */
3228 bool
3229 uses_parameter_packs (tree t)
3230 {
3231 tree parameter_packs = NULL_TREE;
3232 struct find_parameter_pack_data ppd;
3233 ppd.parameter_packs = &parameter_packs;
3234 ppd.visited = pointer_set_create ();
3235 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3236 pointer_set_destroy (ppd.visited);
3237 return parameter_packs != NULL_TREE;
3238 }
3239
3240 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3241 representation a base-class initializer into a parameter pack
3242 expansion. If all goes well, the resulting node will be an
3243 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3244 respectively. */
3245 tree
3246 make_pack_expansion (tree arg)
3247 {
3248 tree result;
3249 tree parameter_packs = NULL_TREE;
3250 bool for_types = false;
3251 struct find_parameter_pack_data ppd;
3252
3253 if (!arg || arg == error_mark_node)
3254 return arg;
3255
3256 if (TREE_CODE (arg) == TREE_LIST)
3257 {
3258 /* The only time we will see a TREE_LIST here is for a base
3259 class initializer. In this case, the TREE_PURPOSE will be a
3260 _TYPE node (representing the base class expansion we're
3261 initializing) and the TREE_VALUE will be a TREE_LIST
3262 containing the initialization arguments.
3263
3264 The resulting expansion looks somewhat different from most
3265 expansions. Rather than returning just one _EXPANSION, we
3266 return a TREE_LIST whose TREE_PURPOSE is a
3267 TYPE_PACK_EXPANSION containing the bases that will be
3268 initialized. The TREE_VALUE will be identical to the
3269 original TREE_VALUE, which is a list of arguments that will
3270 be passed to each base. We do not introduce any new pack
3271 expansion nodes into the TREE_VALUE (although it is possible
3272 that some already exist), because the TREE_PURPOSE and
3273 TREE_VALUE all need to be expanded together with the same
3274 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3275 resulting TREE_PURPOSE will mention the parameter packs in
3276 both the bases and the arguments to the bases. */
3277 tree purpose;
3278 tree value;
3279 tree parameter_packs = NULL_TREE;
3280
3281 /* Determine which parameter packs will be used by the base
3282 class expansion. */
3283 ppd.visited = pointer_set_create ();
3284 ppd.parameter_packs = &parameter_packs;
3285 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3286 &ppd, ppd.visited);
3287
3288 if (parameter_packs == NULL_TREE)
3289 {
3290 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3291 pointer_set_destroy (ppd.visited);
3292 return error_mark_node;
3293 }
3294
3295 if (TREE_VALUE (arg) != void_type_node)
3296 {
3297 /* Collect the sets of parameter packs used in each of the
3298 initialization arguments. */
3299 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3300 {
3301 /* Determine which parameter packs will be expanded in this
3302 argument. */
3303 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3304 &ppd, ppd.visited);
3305 }
3306 }
3307
3308 pointer_set_destroy (ppd.visited);
3309
3310 /* Create the pack expansion type for the base type. */
3311 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3312 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3313 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3314
3315 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3316 they will rarely be compared to anything. */
3317 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3318
3319 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3320 }
3321
3322 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3323 for_types = true;
3324
3325 /* Build the PACK_EXPANSION_* node. */
3326 result = for_types
3327 ? cxx_make_type (TYPE_PACK_EXPANSION)
3328 : make_node (EXPR_PACK_EXPANSION);
3329 SET_PACK_EXPANSION_PATTERN (result, arg);
3330 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3331 {
3332 /* Propagate type and const-expression information. */
3333 TREE_TYPE (result) = TREE_TYPE (arg);
3334 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3335 }
3336 else
3337 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3338 they will rarely be compared to anything. */
3339 SET_TYPE_STRUCTURAL_EQUALITY (result);
3340
3341 /* Determine which parameter packs will be expanded. */
3342 ppd.parameter_packs = &parameter_packs;
3343 ppd.visited = pointer_set_create ();
3344 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3345 pointer_set_destroy (ppd.visited);
3346
3347 /* Make sure we found some parameter packs. */
3348 if (parameter_packs == NULL_TREE)
3349 {
3350 if (TYPE_P (arg))
3351 error ("expansion pattern %<%T%> contains no argument packs", arg);
3352 else
3353 error ("expansion pattern %<%E%> contains no argument packs", arg);
3354 return error_mark_node;
3355 }
3356 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3357
3358 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3359
3360 return result;
3361 }
3362
3363 /* Checks T for any "bare" parameter packs, which have not yet been
3364 expanded, and issues an error if any are found. This operation can
3365 only be done on full expressions or types (e.g., an expression
3366 statement, "if" condition, etc.), because we could have expressions like:
3367
3368 foo(f(g(h(args)))...)
3369
3370 where "args" is a parameter pack. check_for_bare_parameter_packs
3371 should not be called for the subexpressions args, h(args),
3372 g(h(args)), or f(g(h(args))), because we would produce erroneous
3373 error messages.
3374
3375 Returns TRUE and emits an error if there were bare parameter packs,
3376 returns FALSE otherwise. */
3377 bool
3378 check_for_bare_parameter_packs (tree t)
3379 {
3380 tree parameter_packs = NULL_TREE;
3381 struct find_parameter_pack_data ppd;
3382
3383 if (!processing_template_decl || !t || t == error_mark_node)
3384 return false;
3385
3386 if (TREE_CODE (t) == TYPE_DECL)
3387 t = TREE_TYPE (t);
3388
3389 ppd.parameter_packs = &parameter_packs;
3390 ppd.visited = pointer_set_create ();
3391 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3392 pointer_set_destroy (ppd.visited);
3393
3394 if (parameter_packs)
3395 {
3396 error ("parameter packs not expanded with %<...%>:");
3397 while (parameter_packs)
3398 {
3399 tree pack = TREE_VALUE (parameter_packs);
3400 tree name = NULL_TREE;
3401
3402 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3403 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3404 name = TYPE_NAME (pack);
3405 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3406 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3407 else
3408 name = DECL_NAME (pack);
3409
3410 if (name)
3411 inform (input_location, " %qD", name);
3412 else
3413 inform (input_location, " <anonymous>");
3414
3415 parameter_packs = TREE_CHAIN (parameter_packs);
3416 }
3417
3418 return true;
3419 }
3420
3421 return false;
3422 }
3423
3424 /* Expand any parameter packs that occur in the template arguments in
3425 ARGS. */
3426 tree
3427 expand_template_argument_pack (tree args)
3428 {
3429 tree result_args = NULL_TREE;
3430 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3431 int num_result_args = -1;
3432 int non_default_args_count = -1;
3433
3434 /* First, determine if we need to expand anything, and the number of
3435 slots we'll need. */
3436 for (in_arg = 0; in_arg < nargs; ++in_arg)
3437 {
3438 tree arg = TREE_VEC_ELT (args, in_arg);
3439 if (arg == NULL_TREE)
3440 return args;
3441 if (ARGUMENT_PACK_P (arg))
3442 {
3443 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3444 if (num_result_args < 0)
3445 num_result_args = in_arg + num_packed;
3446 else
3447 num_result_args += num_packed;
3448 }
3449 else
3450 {
3451 if (num_result_args >= 0)
3452 num_result_args++;
3453 }
3454 }
3455
3456 /* If no expansion is necessary, we're done. */
3457 if (num_result_args < 0)
3458 return args;
3459
3460 /* Expand arguments. */
3461 result_args = make_tree_vec (num_result_args);
3462 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3463 non_default_args_count =
3464 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3465 for (in_arg = 0; in_arg < nargs; ++in_arg)
3466 {
3467 tree arg = TREE_VEC_ELT (args, in_arg);
3468 if (ARGUMENT_PACK_P (arg))
3469 {
3470 tree packed = ARGUMENT_PACK_ARGS (arg);
3471 int i, num_packed = TREE_VEC_LENGTH (packed);
3472 for (i = 0; i < num_packed; ++i, ++out_arg)
3473 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3474 if (non_default_args_count > 0)
3475 non_default_args_count += num_packed;
3476 }
3477 else
3478 {
3479 TREE_VEC_ELT (result_args, out_arg) = arg;
3480 ++out_arg;
3481 }
3482 }
3483 if (non_default_args_count >= 0)
3484 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3485 return result_args;
3486 }
3487
3488 /* Checks if DECL shadows a template parameter.
3489
3490 [temp.local]: A template-parameter shall not be redeclared within its
3491 scope (including nested scopes).
3492
3493 Emits an error and returns TRUE if the DECL shadows a parameter,
3494 returns FALSE otherwise. */
3495
3496 bool
3497 check_template_shadow (tree decl)
3498 {
3499 tree olddecl;
3500
3501 /* If we're not in a template, we can't possibly shadow a template
3502 parameter. */
3503 if (!current_template_parms)
3504 return true;
3505
3506 /* Figure out what we're shadowing. */
3507 if (TREE_CODE (decl) == OVERLOAD)
3508 decl = OVL_CURRENT (decl);
3509 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3510
3511 /* If there's no previous binding for this name, we're not shadowing
3512 anything, let alone a template parameter. */
3513 if (!olddecl)
3514 return true;
3515
3516 /* If we're not shadowing a template parameter, we're done. Note
3517 that OLDDECL might be an OVERLOAD (or perhaps even an
3518 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3519 node. */
3520 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3521 return true;
3522
3523 /* We check for decl != olddecl to avoid bogus errors for using a
3524 name inside a class. We check TPFI to avoid duplicate errors for
3525 inline member templates. */
3526 if (decl == olddecl
3527 || (DECL_TEMPLATE_PARM_P (decl)
3528 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3529 return true;
3530
3531 error ("declaration of %q+#D", decl);
3532 error (" shadows template parm %q+#D", olddecl);
3533 return false;
3534 }
3535
3536 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3537 ORIG_LEVEL, DECL, and TYPE. */
3538
3539 static tree
3540 build_template_parm_index (int index,
3541 int level,
3542 int orig_level,
3543 tree decl,
3544 tree type)
3545 {
3546 tree t = make_node (TEMPLATE_PARM_INDEX);
3547 TEMPLATE_PARM_IDX (t) = index;
3548 TEMPLATE_PARM_LEVEL (t) = level;
3549 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3550 TEMPLATE_PARM_DECL (t) = decl;
3551 TREE_TYPE (t) = type;
3552 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3553 TREE_READONLY (t) = TREE_READONLY (decl);
3554
3555 return t;
3556 }
3557
3558 /* Find the canonical type parameter for the given template type
3559 parameter. Returns the canonical type parameter, which may be TYPE
3560 if no such parameter existed. */
3561
3562 static tree
3563 canonical_type_parameter (tree type)
3564 {
3565 tree list;
3566 int idx = TEMPLATE_TYPE_IDX (type);
3567 if (!canonical_template_parms)
3568 vec_alloc (canonical_template_parms, idx+1);
3569
3570 while (canonical_template_parms->length () <= (unsigned)idx)
3571 vec_safe_push (canonical_template_parms, NULL_TREE);
3572
3573 list = (*canonical_template_parms)[idx];
3574 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3575 list = TREE_CHAIN (list);
3576
3577 if (list)
3578 return TREE_VALUE (list);
3579 else
3580 {
3581 (*canonical_template_parms)[idx]
3582 = tree_cons (NULL_TREE, type,
3583 (*canonical_template_parms)[idx]);
3584 return type;
3585 }
3586 }
3587
3588 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3589 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3590 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3591 new one is created. */
3592
3593 static tree
3594 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3595 tsubst_flags_t complain)
3596 {
3597 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3598 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3599 != TEMPLATE_PARM_LEVEL (index) - levels)
3600 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3601 {
3602 tree orig_decl = TEMPLATE_PARM_DECL (index);
3603 tree decl, t;
3604
3605 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3606 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3607 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3608 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3609 DECL_ARTIFICIAL (decl) = 1;
3610 SET_DECL_TEMPLATE_PARM_P (decl);
3611
3612 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3613 TEMPLATE_PARM_LEVEL (index) - levels,
3614 TEMPLATE_PARM_ORIG_LEVEL (index),
3615 decl, type);
3616 TEMPLATE_PARM_DESCENDANTS (index) = t;
3617 TEMPLATE_PARM_PARAMETER_PACK (t)
3618 = TEMPLATE_PARM_PARAMETER_PACK (index);
3619
3620 /* Template template parameters need this. */
3621 if (TREE_CODE (decl) == TEMPLATE_DECL)
3622 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3623 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3624 args, complain);
3625 }
3626
3627 return TEMPLATE_PARM_DESCENDANTS (index);
3628 }
3629
3630 /* Process information from new template parameter PARM and append it
3631 to the LIST being built. This new parameter is a non-type
3632 parameter iff IS_NON_TYPE is true. This new parameter is a
3633 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3634 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3635 parameter list PARM belongs to. This is used used to create a
3636 proper canonical type for the type of PARM that is to be created,
3637 iff PARM is a type. If the size is not known, this parameter shall
3638 be set to 0. */
3639
3640 tree
3641 process_template_parm (tree list, location_t parm_loc, tree parm,
3642 bool is_non_type, bool is_parameter_pack)
3643 {
3644 tree decl = 0;
3645 tree defval;
3646 tree err_parm_list;
3647 int idx = 0;
3648
3649 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3650 defval = TREE_PURPOSE (parm);
3651
3652 if (list)
3653 {
3654 tree p = tree_last (list);
3655
3656 if (p && TREE_VALUE (p) != error_mark_node)
3657 {
3658 p = TREE_VALUE (p);
3659 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3660 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3661 else
3662 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3663 }
3664
3665 ++idx;
3666 }
3667 else
3668 idx = 0;
3669
3670 if (is_non_type)
3671 {
3672 parm = TREE_VALUE (parm);
3673
3674 SET_DECL_TEMPLATE_PARM_P (parm);
3675
3676 if (TREE_TYPE (parm) == error_mark_node)
3677 {
3678 err_parm_list = build_tree_list (defval, parm);
3679 TREE_VALUE (err_parm_list) = error_mark_node;
3680 return chainon (list, err_parm_list);
3681 }
3682 else
3683 {
3684 /* [temp.param]
3685
3686 The top-level cv-qualifiers on the template-parameter are
3687 ignored when determining its type. */
3688 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3689 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3690 {
3691 err_parm_list = build_tree_list (defval, parm);
3692 TREE_VALUE (err_parm_list) = error_mark_node;
3693 return chainon (list, err_parm_list);
3694 }
3695
3696 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3697 {
3698 /* This template parameter is not a parameter pack, but it
3699 should be. Complain about "bare" parameter packs. */
3700 check_for_bare_parameter_packs (TREE_TYPE (parm));
3701
3702 /* Recover by calling this a parameter pack. */
3703 is_parameter_pack = true;
3704 }
3705 }
3706
3707 /* A template parameter is not modifiable. */
3708 TREE_CONSTANT (parm) = 1;
3709 TREE_READONLY (parm) = 1;
3710 decl = build_decl (parm_loc,
3711 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3712 TREE_CONSTANT (decl) = 1;
3713 TREE_READONLY (decl) = 1;
3714 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3715 = build_template_parm_index (idx, processing_template_decl,
3716 processing_template_decl,
3717 decl, TREE_TYPE (parm));
3718
3719 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3720 = is_parameter_pack;
3721 }
3722 else
3723 {
3724 tree t;
3725 parm = TREE_VALUE (TREE_VALUE (parm));
3726
3727 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3728 {
3729 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3730 /* This is for distinguishing between real templates and template
3731 template parameters */
3732 TREE_TYPE (parm) = t;
3733 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3734 decl = parm;
3735 }
3736 else
3737 {
3738 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3739 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3740 decl = build_decl (parm_loc,
3741 TYPE_DECL, parm, t);
3742 }
3743
3744 TYPE_NAME (t) = decl;
3745 TYPE_STUB_DECL (t) = decl;
3746 parm = decl;
3747 TEMPLATE_TYPE_PARM_INDEX (t)
3748 = build_template_parm_index (idx, processing_template_decl,
3749 processing_template_decl,
3750 decl, TREE_TYPE (parm));
3751 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3752 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3753 }
3754 DECL_ARTIFICIAL (decl) = 1;
3755 SET_DECL_TEMPLATE_PARM_P (decl);
3756 pushdecl (decl);
3757 parm = build_tree_list (defval, parm);
3758 return chainon (list, parm);
3759 }
3760
3761 /* The end of a template parameter list has been reached. Process the
3762 tree list into a parameter vector, converting each parameter into a more
3763 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3764 as PARM_DECLs. */
3765
3766 tree
3767 end_template_parm_list (tree parms)
3768 {
3769 int nparms;
3770 tree parm, next;
3771 tree saved_parmlist = make_tree_vec (list_length (parms));
3772
3773 current_template_parms
3774 = tree_cons (size_int (processing_template_decl),
3775 saved_parmlist, current_template_parms);
3776
3777 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3778 {
3779 next = TREE_CHAIN (parm);
3780 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3781 TREE_CHAIN (parm) = NULL_TREE;
3782 }
3783
3784 --processing_template_parmlist;
3785
3786 return saved_parmlist;
3787 }
3788
3789 /* end_template_decl is called after a template declaration is seen. */
3790
3791 void
3792 end_template_decl (void)
3793 {
3794 reset_specialization ();
3795
3796 if (! processing_template_decl)
3797 return;
3798
3799 /* This matches the pushlevel in begin_template_parm_list. */
3800 finish_scope ();
3801
3802 --processing_template_decl;
3803 current_template_parms = TREE_CHAIN (current_template_parms);
3804 }
3805
3806 /* Takes a TREE_LIST representing a template parameter and convert it
3807 into an argument suitable to be passed to the type substitution
3808 functions. Note that If the TREE_LIST contains an error_mark
3809 node, the returned argument is error_mark_node. */
3810
3811 static tree
3812 template_parm_to_arg (tree t)
3813 {
3814
3815 if (t == NULL_TREE
3816 || TREE_CODE (t) != TREE_LIST)
3817 return t;
3818
3819 if (error_operand_p (TREE_VALUE (t)))
3820 return error_mark_node;
3821
3822 t = TREE_VALUE (t);
3823
3824 if (TREE_CODE (t) == TYPE_DECL
3825 || TREE_CODE (t) == TEMPLATE_DECL)
3826 {
3827 t = TREE_TYPE (t);
3828
3829 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3830 {
3831 /* Turn this argument into a TYPE_ARGUMENT_PACK
3832 with a single element, which expands T. */
3833 tree vec = make_tree_vec (1);
3834 #ifdef ENABLE_CHECKING
3835 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3836 (vec, TREE_VEC_LENGTH (vec));
3837 #endif
3838 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3839
3840 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3841 SET_ARGUMENT_PACK_ARGS (t, vec);
3842 }
3843 }
3844 else
3845 {
3846 t = DECL_INITIAL (t);
3847
3848 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3849 {
3850 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3851 with a single element, which expands T. */
3852 tree vec = make_tree_vec (1);
3853 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3854 #ifdef ENABLE_CHECKING
3855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3856 (vec, TREE_VEC_LENGTH (vec));
3857 #endif
3858 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3859
3860 t = make_node (NONTYPE_ARGUMENT_PACK);
3861 SET_ARGUMENT_PACK_ARGS (t, vec);
3862 TREE_TYPE (t) = type;
3863 }
3864 }
3865 return t;
3866 }
3867
3868 /* Given a set of template parameters, return them as a set of template
3869 arguments. The template parameters are represented as a TREE_VEC, in
3870 the form documented in cp-tree.h for template arguments. */
3871
3872 static tree
3873 template_parms_to_args (tree parms)
3874 {
3875 tree header;
3876 tree args = NULL_TREE;
3877 int length = TMPL_PARMS_DEPTH (parms);
3878 int l = length;
3879
3880 /* If there is only one level of template parameters, we do not
3881 create a TREE_VEC of TREE_VECs. Instead, we return a single
3882 TREE_VEC containing the arguments. */
3883 if (length > 1)
3884 args = make_tree_vec (length);
3885
3886 for (header = parms; header; header = TREE_CHAIN (header))
3887 {
3888 tree a = copy_node (TREE_VALUE (header));
3889 int i;
3890
3891 TREE_TYPE (a) = NULL_TREE;
3892 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3893 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3894
3895 #ifdef ENABLE_CHECKING
3896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3897 #endif
3898
3899 if (length > 1)
3900 TREE_VEC_ELT (args, --l) = a;
3901 else
3902 args = a;
3903 }
3904
3905 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3906 /* This can happen for template parms of a template template
3907 parameter, e.g:
3908
3909 template<template<class T, class U> class TT> struct S;
3910
3911 Consider the level of the parms of TT; T and U both have
3912 level 2; TT has no template parm of level 1. So in this case
3913 the first element of full_template_args is NULL_TREE. If we
3914 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3915 of 2. This will make tsubst wrongly consider that T and U
3916 have level 1. Instead, let's create a dummy vector as the
3917 first element of full_template_args so that TMPL_ARGS_DEPTH
3918 returns the correct depth for args. */
3919 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3920 return args;
3921 }
3922
3923 /* Within the declaration of a template, return the currently active
3924 template parameters as an argument TREE_VEC. */
3925
3926 static tree
3927 current_template_args (void)
3928 {
3929 return template_parms_to_args (current_template_parms);
3930 }
3931
3932 /* Update the declared TYPE by doing any lookups which were thought to be
3933 dependent, but are not now that we know the SCOPE of the declarator. */
3934
3935 tree
3936 maybe_update_decl_type (tree orig_type, tree scope)
3937 {
3938 tree type = orig_type;
3939
3940 if (type == NULL_TREE)
3941 return type;
3942
3943 if (TREE_CODE (orig_type) == TYPE_DECL)
3944 type = TREE_TYPE (type);
3945
3946 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3947 && dependent_type_p (type)
3948 /* Don't bother building up the args in this case. */
3949 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3950 {
3951 /* tsubst in the args corresponding to the template parameters,
3952 including auto if present. Most things will be unchanged, but
3953 make_typename_type and tsubst_qualified_id will resolve
3954 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3955 tree args = current_template_args ();
3956 tree auto_node = type_uses_auto (type);
3957 tree pushed;
3958 if (auto_node)
3959 {
3960 tree auto_vec = make_tree_vec (1);
3961 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3962 args = add_to_template_args (args, auto_vec);
3963 }
3964 pushed = push_scope (scope);
3965 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3966 if (pushed)
3967 pop_scope (scope);
3968 }
3969
3970 if (type == error_mark_node)
3971 return orig_type;
3972
3973 if (TREE_CODE (orig_type) == TYPE_DECL)
3974 {
3975 if (same_type_p (type, TREE_TYPE (orig_type)))
3976 type = orig_type;
3977 else
3978 type = TYPE_NAME (type);
3979 }
3980 return type;
3981 }
3982
3983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3984 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3985 a member template. Used by push_template_decl below. */
3986
3987 static tree
3988 build_template_decl (tree decl, tree parms, bool member_template_p)
3989 {
3990 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3991 DECL_TEMPLATE_PARMS (tmpl) = parms;
3992 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3993 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3994 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3995
3996 return tmpl;
3997 }
3998
3999 struct template_parm_data
4000 {
4001 /* The level of the template parameters we are currently
4002 processing. */
4003 int level;
4004
4005 /* The index of the specialization argument we are currently
4006 processing. */
4007 int current_arg;
4008
4009 /* An array whose size is the number of template parameters. The
4010 elements are nonzero if the parameter has been used in any one
4011 of the arguments processed so far. */
4012 int* parms;
4013
4014 /* An array whose size is the number of template arguments. The
4015 elements are nonzero if the argument makes use of template
4016 parameters of this level. */
4017 int* arg_uses_template_parms;
4018 };
4019
4020 /* Subroutine of push_template_decl used to see if each template
4021 parameter in a partial specialization is used in the explicit
4022 argument list. If T is of the LEVEL given in DATA (which is
4023 treated as a template_parm_data*), then DATA->PARMS is marked
4024 appropriately. */
4025
4026 static int
4027 mark_template_parm (tree t, void* data)
4028 {
4029 int level;
4030 int idx;
4031 struct template_parm_data* tpd = (struct template_parm_data*) data;
4032
4033 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4034 {
4035 level = TEMPLATE_PARM_LEVEL (t);
4036 idx = TEMPLATE_PARM_IDX (t);
4037 }
4038 else
4039 {
4040 level = TEMPLATE_TYPE_LEVEL (t);
4041 idx = TEMPLATE_TYPE_IDX (t);
4042 }
4043
4044 if (level == tpd->level)
4045 {
4046 tpd->parms[idx] = 1;
4047 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4048 }
4049
4050 /* Return zero so that for_each_template_parm will continue the
4051 traversal of the tree; we want to mark *every* template parm. */
4052 return 0;
4053 }
4054
4055 /* Process the partial specialization DECL. */
4056
4057 static tree
4058 process_partial_specialization (tree decl)
4059 {
4060 tree type = TREE_TYPE (decl);
4061 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4062 tree specargs = CLASSTYPE_TI_ARGS (type);
4063 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4064 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4065 tree inner_parms;
4066 tree inst;
4067 int nargs = TREE_VEC_LENGTH (inner_args);
4068 int ntparms;
4069 int i;
4070 bool did_error_intro = false;
4071 struct template_parm_data tpd;
4072 struct template_parm_data tpd2;
4073
4074 gcc_assert (current_template_parms);
4075
4076 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4077 ntparms = TREE_VEC_LENGTH (inner_parms);
4078
4079 /* We check that each of the template parameters given in the
4080 partial specialization is used in the argument list to the
4081 specialization. For example:
4082
4083 template <class T> struct S;
4084 template <class T> struct S<T*>;
4085
4086 The second declaration is OK because `T*' uses the template
4087 parameter T, whereas
4088
4089 template <class T> struct S<int>;
4090
4091 is no good. Even trickier is:
4092
4093 template <class T>
4094 struct S1
4095 {
4096 template <class U>
4097 struct S2;
4098 template <class U>
4099 struct S2<T>;
4100 };
4101
4102 The S2<T> declaration is actually invalid; it is a
4103 full-specialization. Of course,
4104
4105 template <class U>
4106 struct S2<T (*)(U)>;
4107
4108 or some such would have been OK. */
4109 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4110 tpd.parms = XALLOCAVEC (int, ntparms);
4111 memset (tpd.parms, 0, sizeof (int) * ntparms);
4112
4113 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4114 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4115 for (i = 0; i < nargs; ++i)
4116 {
4117 tpd.current_arg = i;
4118 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4119 &mark_template_parm,
4120 &tpd,
4121 NULL,
4122 /*include_nondeduced_p=*/false);
4123 }
4124 for (i = 0; i < ntparms; ++i)
4125 if (tpd.parms[i] == 0)
4126 {
4127 /* One of the template parms was not used in the
4128 specialization. */
4129 if (!did_error_intro)
4130 {
4131 error ("template parameters not used in partial specialization:");
4132 did_error_intro = true;
4133 }
4134
4135 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4136 }
4137
4138 if (did_error_intro)
4139 return error_mark_node;
4140
4141 /* [temp.class.spec]
4142
4143 The argument list of the specialization shall not be identical to
4144 the implicit argument list of the primary template. */
4145 if (comp_template_args
4146 (inner_args,
4147 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4148 (maintmpl)))))
4149 error ("partial specialization %qT does not specialize any template arguments", type);
4150
4151 /* A partial specialization that replaces multiple parameters of the
4152 primary template with a pack expansion is less specialized for those
4153 parameters. */
4154 if (nargs < DECL_NTPARMS (maintmpl))
4155 {
4156 error ("partial specialization is not more specialized than the "
4157 "primary template because it replaces multiple parameters "
4158 "with a pack expansion");
4159 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4160 return decl;
4161 }
4162
4163 /* [temp.class.spec]
4164
4165 A partially specialized non-type argument expression shall not
4166 involve template parameters of the partial specialization except
4167 when the argument expression is a simple identifier.
4168
4169 The type of a template parameter corresponding to a specialized
4170 non-type argument shall not be dependent on a parameter of the
4171 specialization.
4172
4173 Also, we verify that pack expansions only occur at the
4174 end of the argument list. */
4175 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4176 tpd2.parms = 0;
4177 for (i = 0; i < nargs; ++i)
4178 {
4179 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4180 tree arg = TREE_VEC_ELT (inner_args, i);
4181 tree packed_args = NULL_TREE;
4182 int j, len = 1;
4183
4184 if (ARGUMENT_PACK_P (arg))
4185 {
4186 /* Extract the arguments from the argument pack. We'll be
4187 iterating over these in the following loop. */
4188 packed_args = ARGUMENT_PACK_ARGS (arg);
4189 len = TREE_VEC_LENGTH (packed_args);
4190 }
4191
4192 for (j = 0; j < len; j++)
4193 {
4194 if (packed_args)
4195 /* Get the Jth argument in the parameter pack. */
4196 arg = TREE_VEC_ELT (packed_args, j);
4197
4198 if (PACK_EXPANSION_P (arg))
4199 {
4200 /* Pack expansions must come at the end of the
4201 argument list. */
4202 if ((packed_args && j < len - 1)
4203 || (!packed_args && i < nargs - 1))
4204 {
4205 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4206 error ("parameter pack argument %qE must be at the "
4207 "end of the template argument list", arg);
4208 else
4209 error ("parameter pack argument %qT must be at the "
4210 "end of the template argument list", arg);
4211 }
4212 }
4213
4214 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4215 /* We only care about the pattern. */
4216 arg = PACK_EXPANSION_PATTERN (arg);
4217
4218 if (/* These first two lines are the `non-type' bit. */
4219 !TYPE_P (arg)
4220 && TREE_CODE (arg) != TEMPLATE_DECL
4221 /* This next line is the `argument expression is not just a
4222 simple identifier' condition and also the `specialized
4223 non-type argument' bit. */
4224 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4225 {
4226 if ((!packed_args && tpd.arg_uses_template_parms[i])
4227 || (packed_args && uses_template_parms (arg)))
4228 error ("template argument %qE involves template parameter(s)",
4229 arg);
4230 else
4231 {
4232 /* Look at the corresponding template parameter,
4233 marking which template parameters its type depends
4234 upon. */
4235 tree type = TREE_TYPE (parm);
4236
4237 if (!tpd2.parms)
4238 {
4239 /* We haven't yet initialized TPD2. Do so now. */
4240 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4241 /* The number of parameters here is the number in the
4242 main template, which, as checked in the assertion
4243 above, is NARGS. */
4244 tpd2.parms = XALLOCAVEC (int, nargs);
4245 tpd2.level =
4246 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4247 }
4248
4249 /* Mark the template parameters. But this time, we're
4250 looking for the template parameters of the main
4251 template, not in the specialization. */
4252 tpd2.current_arg = i;
4253 tpd2.arg_uses_template_parms[i] = 0;
4254 memset (tpd2.parms, 0, sizeof (int) * nargs);
4255 for_each_template_parm (type,
4256 &mark_template_parm,
4257 &tpd2,
4258 NULL,
4259 /*include_nondeduced_p=*/false);
4260
4261 if (tpd2.arg_uses_template_parms [i])
4262 {
4263 /* The type depended on some template parameters.
4264 If they are fully specialized in the
4265 specialization, that's OK. */
4266 int j;
4267 int count = 0;
4268 for (j = 0; j < nargs; ++j)
4269 if (tpd2.parms[j] != 0
4270 && tpd.arg_uses_template_parms [j])
4271 ++count;
4272 if (count != 0)
4273 error_n (input_location, count,
4274 "type %qT of template argument %qE depends "
4275 "on a template parameter",
4276 "type %qT of template argument %qE depends "
4277 "on template parameters",
4278 type,
4279 arg);
4280 }
4281 }
4282 }
4283 }
4284 }
4285
4286 /* We should only get here once. */
4287 gcc_assert (!COMPLETE_TYPE_P (type));
4288
4289 tree tmpl = build_template_decl (decl, current_template_parms,
4290 DECL_MEMBER_TEMPLATE_P (maintmpl));
4291 TREE_TYPE (tmpl) = type;
4292 DECL_TEMPLATE_RESULT (tmpl) = decl;
4293 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4294 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4295 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4296
4297 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4298 = tree_cons (specargs, tmpl,
4299 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4300 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4301
4302 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4303 inst = TREE_CHAIN (inst))
4304 {
4305 tree inst_type = TREE_VALUE (inst);
4306 if (COMPLETE_TYPE_P (inst_type)
4307 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4308 {
4309 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4310 if (spec && TREE_TYPE (spec) == type)
4311 permerror (input_location,
4312 "partial specialization of %qT after instantiation "
4313 "of %qT", type, inst_type);
4314 }
4315 }
4316
4317 return decl;
4318 }
4319
4320 /* Check that a template declaration's use of default arguments and
4321 parameter packs is not invalid. Here, PARMS are the template
4322 parameters. IS_PRIMARY is true if DECL is the thing declared by
4323 a primary template. IS_PARTIAL is true if DECL is a partial
4324 specialization.
4325
4326 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4327 declaration (but not a definition); 1 indicates a declaration, 2
4328 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4329 emitted for extraneous default arguments.
4330
4331 Returns TRUE if there were no errors found, FALSE otherwise. */
4332
4333 bool
4334 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4335 bool is_partial, int is_friend_decl)
4336 {
4337 const char *msg;
4338 int last_level_to_check;
4339 tree parm_level;
4340 bool no_errors = true;
4341
4342 /* [temp.param]
4343
4344 A default template-argument shall not be specified in a
4345 function template declaration or a function template definition, nor
4346 in the template-parameter-list of the definition of a member of a
4347 class template. */
4348
4349 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4350 /* You can't have a function template declaration in a local
4351 scope, nor you can you define a member of a class template in a
4352 local scope. */
4353 return true;
4354
4355 if (TREE_CODE (decl) == TYPE_DECL
4356 && TREE_TYPE (decl)
4357 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4358 /* A lambda doesn't have an explicit declaration; don't complain
4359 about the parms of the enclosing class. */
4360 return true;
4361
4362 if (current_class_type
4363 && !TYPE_BEING_DEFINED (current_class_type)
4364 && DECL_LANG_SPECIFIC (decl)
4365 && DECL_DECLARES_FUNCTION_P (decl)
4366 /* If this is either a friend defined in the scope of the class
4367 or a member function. */
4368 && (DECL_FUNCTION_MEMBER_P (decl)
4369 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4370 : DECL_FRIEND_CONTEXT (decl)
4371 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4372 : false)
4373 /* And, if it was a member function, it really was defined in
4374 the scope of the class. */
4375 && (!DECL_FUNCTION_MEMBER_P (decl)
4376 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4377 /* We already checked these parameters when the template was
4378 declared, so there's no need to do it again now. This function
4379 was defined in class scope, but we're processing its body now
4380 that the class is complete. */
4381 return true;
4382
4383 /* Core issue 226 (C++0x only): the following only applies to class
4384 templates. */
4385 if (is_primary
4386 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4387 {
4388 /* [temp.param]
4389
4390 If a template-parameter has a default template-argument, all
4391 subsequent template-parameters shall have a default
4392 template-argument supplied. */
4393 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4394 {
4395 tree inner_parms = TREE_VALUE (parm_level);
4396 int ntparms = TREE_VEC_LENGTH (inner_parms);
4397 int seen_def_arg_p = 0;
4398 int i;
4399
4400 for (i = 0; i < ntparms; ++i)
4401 {
4402 tree parm = TREE_VEC_ELT (inner_parms, i);
4403
4404 if (parm == error_mark_node)
4405 continue;
4406
4407 if (TREE_PURPOSE (parm))
4408 seen_def_arg_p = 1;
4409 else if (seen_def_arg_p
4410 && !template_parameter_pack_p (TREE_VALUE (parm)))
4411 {
4412 error ("no default argument for %qD", TREE_VALUE (parm));
4413 /* For better subsequent error-recovery, we indicate that
4414 there should have been a default argument. */
4415 TREE_PURPOSE (parm) = error_mark_node;
4416 no_errors = false;
4417 }
4418 else if (!is_partial
4419 && !is_friend_decl
4420 /* Don't complain about an enclosing partial
4421 specialization. */
4422 && parm_level == parms
4423 && TREE_CODE (decl) == TYPE_DECL
4424 && i < ntparms - 1
4425 && template_parameter_pack_p (TREE_VALUE (parm)))
4426 {
4427 /* A primary class template can only have one
4428 parameter pack, at the end of the template
4429 parameter list. */
4430
4431 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4432 error ("parameter pack %qE must be at the end of the"
4433 " template parameter list", TREE_VALUE (parm));
4434 else
4435 error ("parameter pack %qT must be at the end of the"
4436 " template parameter list",
4437 TREE_TYPE (TREE_VALUE (parm)));
4438
4439 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4440 = error_mark_node;
4441 no_errors = false;
4442 }
4443 }
4444 }
4445 }
4446
4447 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4448 || is_partial
4449 || !is_primary
4450 || is_friend_decl)
4451 /* For an ordinary class template, default template arguments are
4452 allowed at the innermost level, e.g.:
4453 template <class T = int>
4454 struct S {};
4455 but, in a partial specialization, they're not allowed even
4456 there, as we have in [temp.class.spec]:
4457
4458 The template parameter list of a specialization shall not
4459 contain default template argument values.
4460
4461 So, for a partial specialization, or for a function template
4462 (in C++98/C++03), we look at all of them. */
4463 ;
4464 else
4465 /* But, for a primary class template that is not a partial
4466 specialization we look at all template parameters except the
4467 innermost ones. */
4468 parms = TREE_CHAIN (parms);
4469
4470 /* Figure out what error message to issue. */
4471 if (is_friend_decl == 2)
4472 msg = G_("default template arguments may not be used in function template "
4473 "friend re-declaration");
4474 else if (is_friend_decl)
4475 msg = G_("default template arguments may not be used in function template "
4476 "friend declarations");
4477 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4478 msg = G_("default template arguments may not be used in function templates "
4479 "without -std=c++11 or -std=gnu++11");
4480 else if (is_partial)
4481 msg = G_("default template arguments may not be used in "
4482 "partial specializations");
4483 else
4484 msg = G_("default argument for template parameter for class enclosing %qD");
4485
4486 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4487 /* If we're inside a class definition, there's no need to
4488 examine the parameters to the class itself. On the one
4489 hand, they will be checked when the class is defined, and,
4490 on the other, default arguments are valid in things like:
4491 template <class T = double>
4492 struct S { template <class U> void f(U); };
4493 Here the default argument for `S' has no bearing on the
4494 declaration of `f'. */
4495 last_level_to_check = template_class_depth (current_class_type) + 1;
4496 else
4497 /* Check everything. */
4498 last_level_to_check = 0;
4499
4500 for (parm_level = parms;
4501 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4502 parm_level = TREE_CHAIN (parm_level))
4503 {
4504 tree inner_parms = TREE_VALUE (parm_level);
4505 int i;
4506 int ntparms;
4507
4508 ntparms = TREE_VEC_LENGTH (inner_parms);
4509 for (i = 0; i < ntparms; ++i)
4510 {
4511 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4512 continue;
4513
4514 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4515 {
4516 if (msg)
4517 {
4518 no_errors = false;
4519 if (is_friend_decl == 2)
4520 return no_errors;
4521
4522 error (msg, decl);
4523 msg = 0;
4524 }
4525
4526 /* Clear out the default argument so that we are not
4527 confused later. */
4528 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4529 }
4530 }
4531
4532 /* At this point, if we're still interested in issuing messages,
4533 they must apply to classes surrounding the object declared. */
4534 if (msg)
4535 msg = G_("default argument for template parameter for class "
4536 "enclosing %qD");
4537 }
4538
4539 return no_errors;
4540 }
4541
4542 /* Worker for push_template_decl_real, called via
4543 for_each_template_parm. DATA is really an int, indicating the
4544 level of the parameters we are interested in. If T is a template
4545 parameter of that level, return nonzero. */
4546
4547 static int
4548 template_parm_this_level_p (tree t, void* data)
4549 {
4550 int this_level = *(int *)data;
4551 int level;
4552
4553 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4554 level = TEMPLATE_PARM_LEVEL (t);
4555 else
4556 level = TEMPLATE_TYPE_LEVEL (t);
4557 return level == this_level;
4558 }
4559
4560 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4561 parameters given by current_template_args, or reuses a
4562 previously existing one, if appropriate. Returns the DECL, or an
4563 equivalent one, if it is replaced via a call to duplicate_decls.
4564
4565 If IS_FRIEND is true, DECL is a friend declaration. */
4566
4567 tree
4568 push_template_decl_real (tree decl, bool is_friend)
4569 {
4570 tree tmpl;
4571 tree args;
4572 tree info;
4573 tree ctx;
4574 bool is_primary;
4575 bool is_partial;
4576 int new_template_p = 0;
4577 /* True if the template is a member template, in the sense of
4578 [temp.mem]. */
4579 bool member_template_p = false;
4580
4581 if (decl == error_mark_node || !current_template_parms)
4582 return error_mark_node;
4583
4584 /* See if this is a partial specialization. */
4585 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4586 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4587 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4588
4589 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4590 is_friend = true;
4591
4592 if (is_friend)
4593 /* For a friend, we want the context of the friend function, not
4594 the type of which it is a friend. */
4595 ctx = CP_DECL_CONTEXT (decl);
4596 else if (CP_DECL_CONTEXT (decl)
4597 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4598 /* In the case of a virtual function, we want the class in which
4599 it is defined. */
4600 ctx = CP_DECL_CONTEXT (decl);
4601 else
4602 /* Otherwise, if we're currently defining some class, the DECL
4603 is assumed to be a member of the class. */
4604 ctx = current_scope ();
4605
4606 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4607 ctx = NULL_TREE;
4608
4609 if (!DECL_CONTEXT (decl))
4610 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4611
4612 /* See if this is a primary template. */
4613 if (is_friend && ctx)
4614 /* A friend template that specifies a class context, i.e.
4615 template <typename T> friend void A<T>::f();
4616 is not primary. */
4617 is_primary = false;
4618 else
4619 is_primary = template_parm_scope_p ();
4620
4621 if (is_primary)
4622 {
4623 if (DECL_CLASS_SCOPE_P (decl))
4624 member_template_p = true;
4625 if (TREE_CODE (decl) == TYPE_DECL
4626 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4627 {
4628 error ("template class without a name");
4629 return error_mark_node;
4630 }
4631 else if (TREE_CODE (decl) == FUNCTION_DECL)
4632 {
4633 if (DECL_DESTRUCTOR_P (decl))
4634 {
4635 /* [temp.mem]
4636
4637 A destructor shall not be a member template. */
4638 error ("destructor %qD declared as member template", decl);
4639 return error_mark_node;
4640 }
4641 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4642 && (!prototype_p (TREE_TYPE (decl))
4643 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4644 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4645 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4646 == void_list_node)))
4647 {
4648 /* [basic.stc.dynamic.allocation]
4649
4650 An allocation function can be a function
4651 template. ... Template allocation functions shall
4652 have two or more parameters. */
4653 error ("invalid template declaration of %qD", decl);
4654 return error_mark_node;
4655 }
4656 }
4657 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4658 && CLASS_TYPE_P (TREE_TYPE (decl)))
4659 /* OK */;
4660 else if (TREE_CODE (decl) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl))
4662 /* alias-declaration */
4663 gcc_assert (!DECL_ARTIFICIAL (decl));
4664 else
4665 {
4666 error ("template declaration of %q#D", decl);
4667 return error_mark_node;
4668 }
4669 }
4670
4671 /* Check to see that the rules regarding the use of default
4672 arguments are not being violated. */
4673 check_default_tmpl_args (decl, current_template_parms,
4674 is_primary, is_partial, /*is_friend_decl=*/0);
4675
4676 /* Ensure that there are no parameter packs in the type of this
4677 declaration that have not been expanded. */
4678 if (TREE_CODE (decl) == FUNCTION_DECL)
4679 {
4680 /* Check each of the arguments individually to see if there are
4681 any bare parameter packs. */
4682 tree type = TREE_TYPE (decl);
4683 tree arg = DECL_ARGUMENTS (decl);
4684 tree argtype = TYPE_ARG_TYPES (type);
4685
4686 while (arg && argtype)
4687 {
4688 if (!DECL_PACK_P (arg)
4689 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4690 {
4691 /* This is a PARM_DECL that contains unexpanded parameter
4692 packs. We have already complained about this in the
4693 check_for_bare_parameter_packs call, so just replace
4694 these types with ERROR_MARK_NODE. */
4695 TREE_TYPE (arg) = error_mark_node;
4696 TREE_VALUE (argtype) = error_mark_node;
4697 }
4698
4699 arg = DECL_CHAIN (arg);
4700 argtype = TREE_CHAIN (argtype);
4701 }
4702
4703 /* Check for bare parameter packs in the return type and the
4704 exception specifiers. */
4705 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4706 /* Errors were already issued, set return type to int
4707 as the frontend doesn't expect error_mark_node as
4708 the return type. */
4709 TREE_TYPE (type) = integer_type_node;
4710 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4711 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4712 }
4713 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4714 && TYPE_DECL_ALIAS_P (decl))
4715 ? DECL_ORIGINAL_TYPE (decl)
4716 : TREE_TYPE (decl)))
4717 {
4718 TREE_TYPE (decl) = error_mark_node;
4719 return error_mark_node;
4720 }
4721
4722 if (is_partial)
4723 return process_partial_specialization (decl);
4724
4725 args = current_template_args ();
4726
4727 if (!ctx
4728 || TREE_CODE (ctx) == FUNCTION_DECL
4729 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4730 || (TREE_CODE (decl) == TYPE_DECL
4731 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4732 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4733 {
4734 if (DECL_LANG_SPECIFIC (decl)
4735 && DECL_TEMPLATE_INFO (decl)
4736 && DECL_TI_TEMPLATE (decl))
4737 tmpl = DECL_TI_TEMPLATE (decl);
4738 /* If DECL is a TYPE_DECL for a class-template, then there won't
4739 be DECL_LANG_SPECIFIC. The information equivalent to
4740 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4741 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4742 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4743 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4744 {
4745 /* Since a template declaration already existed for this
4746 class-type, we must be redeclaring it here. Make sure
4747 that the redeclaration is valid. */
4748 redeclare_class_template (TREE_TYPE (decl),
4749 current_template_parms);
4750 /* We don't need to create a new TEMPLATE_DECL; just use the
4751 one we already had. */
4752 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4753 }
4754 else
4755 {
4756 tmpl = build_template_decl (decl, current_template_parms,
4757 member_template_p);
4758 new_template_p = 1;
4759
4760 if (DECL_LANG_SPECIFIC (decl)
4761 && DECL_TEMPLATE_SPECIALIZATION (decl))
4762 {
4763 /* A specialization of a member template of a template
4764 class. */
4765 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4766 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4767 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4768 }
4769 }
4770 }
4771 else
4772 {
4773 tree a, t, current, parms;
4774 int i;
4775 tree tinfo = get_template_info (decl);
4776
4777 if (!tinfo)
4778 {
4779 error ("template definition of non-template %q#D", decl);
4780 return error_mark_node;
4781 }
4782
4783 tmpl = TI_TEMPLATE (tinfo);
4784
4785 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4786 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4787 && DECL_TEMPLATE_SPECIALIZATION (decl)
4788 && DECL_MEMBER_TEMPLATE_P (tmpl))
4789 {
4790 tree new_tmpl;
4791
4792 /* The declaration is a specialization of a member
4793 template, declared outside the class. Therefore, the
4794 innermost template arguments will be NULL, so we
4795 replace them with the arguments determined by the
4796 earlier call to check_explicit_specialization. */
4797 args = DECL_TI_ARGS (decl);
4798
4799 new_tmpl
4800 = build_template_decl (decl, current_template_parms,
4801 member_template_p);
4802 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4803 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4804 DECL_TI_TEMPLATE (decl) = new_tmpl;
4805 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4806 DECL_TEMPLATE_INFO (new_tmpl)
4807 = build_template_info (tmpl, args);
4808
4809 register_specialization (new_tmpl,
4810 most_general_template (tmpl),
4811 args,
4812 is_friend, 0);
4813 return decl;
4814 }
4815
4816 /* Make sure the template headers we got make sense. */
4817
4818 parms = DECL_TEMPLATE_PARMS (tmpl);
4819 i = TMPL_PARMS_DEPTH (parms);
4820 if (TMPL_ARGS_DEPTH (args) != i)
4821 {
4822 error ("expected %d levels of template parms for %q#D, got %d",
4823 i, decl, TMPL_ARGS_DEPTH (args));
4824 }
4825 else
4826 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4827 {
4828 a = TMPL_ARGS_LEVEL (args, i);
4829 t = INNERMOST_TEMPLATE_PARMS (parms);
4830
4831 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4832 {
4833 if (current == decl)
4834 error ("got %d template parameters for %q#D",
4835 TREE_VEC_LENGTH (a), decl);
4836 else
4837 error ("got %d template parameters for %q#T",
4838 TREE_VEC_LENGTH (a), current);
4839 error (" but %d required", TREE_VEC_LENGTH (t));
4840 /* Avoid crash in import_export_decl. */
4841 DECL_INTERFACE_KNOWN (decl) = 1;
4842 return error_mark_node;
4843 }
4844
4845 if (current == decl)
4846 current = ctx;
4847 else if (current == NULL_TREE)
4848 /* Can happen in erroneous input. */
4849 break;
4850 else
4851 current = get_containing_scope (current);
4852 }
4853
4854 /* Check that the parms are used in the appropriate qualifying scopes
4855 in the declarator. */
4856 if (!comp_template_args
4857 (TI_ARGS (tinfo),
4858 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4859 {
4860 error ("\
4861 template arguments to %qD do not match original template %qD",
4862 decl, DECL_TEMPLATE_RESULT (tmpl));
4863 if (!uses_template_parms (TI_ARGS (tinfo)))
4864 inform (input_location, "use template<> for an explicit specialization");
4865 /* Avoid crash in import_export_decl. */
4866 DECL_INTERFACE_KNOWN (decl) = 1;
4867 return error_mark_node;
4868 }
4869 }
4870
4871 DECL_TEMPLATE_RESULT (tmpl) = decl;
4872 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4873
4874 /* Push template declarations for global functions and types. Note
4875 that we do not try to push a global template friend declared in a
4876 template class; such a thing may well depend on the template
4877 parameters of the class. */
4878 if (new_template_p && !ctx
4879 && !(is_friend && template_class_depth (current_class_type) > 0))
4880 {
4881 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4882 if (tmpl == error_mark_node)
4883 return error_mark_node;
4884
4885 /* Hide template friend classes that haven't been declared yet. */
4886 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4887 {
4888 DECL_ANTICIPATED (tmpl) = 1;
4889 DECL_FRIEND_P (tmpl) = 1;
4890 }
4891 }
4892
4893 if (is_primary)
4894 {
4895 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4896 int i;
4897
4898 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4899 if (DECL_CONV_FN_P (tmpl))
4900 {
4901 int depth = TMPL_PARMS_DEPTH (parms);
4902
4903 /* It is a conversion operator. See if the type converted to
4904 depends on innermost template operands. */
4905
4906 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4907 depth))
4908 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4909 }
4910
4911 /* Give template template parms a DECL_CONTEXT of the template
4912 for which they are a parameter. */
4913 parms = INNERMOST_TEMPLATE_PARMS (parms);
4914 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4915 {
4916 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4917 if (TREE_CODE (parm) == TEMPLATE_DECL)
4918 DECL_CONTEXT (parm) = tmpl;
4919 }
4920 }
4921
4922 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4923 back to its most general template. If TMPL is a specialization,
4924 ARGS may only have the innermost set of arguments. Add the missing
4925 argument levels if necessary. */
4926 if (DECL_TEMPLATE_INFO (tmpl))
4927 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4928
4929 info = build_template_info (tmpl, args);
4930
4931 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4932 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4933 else
4934 {
4935 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4936 retrofit_lang_decl (decl);
4937 if (DECL_LANG_SPECIFIC (decl))
4938 DECL_TEMPLATE_INFO (decl) = info;
4939 }
4940
4941 return DECL_TEMPLATE_RESULT (tmpl);
4942 }
4943
4944 tree
4945 push_template_decl (tree decl)
4946 {
4947 return push_template_decl_real (decl, false);
4948 }
4949
4950 /* FN is an inheriting constructor that inherits from the constructor
4951 template INHERITED; turn FN into a constructor template with a matching
4952 template header. */
4953
4954 tree
4955 add_inherited_template_parms (tree fn, tree inherited)
4956 {
4957 tree inner_parms
4958 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4959 inner_parms = copy_node (inner_parms);
4960 tree parms
4961 = tree_cons (size_int (processing_template_decl + 1),
4962 inner_parms, current_template_parms);
4963 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4964 tree args = template_parms_to_args (parms);
4965 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4966 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4967 DECL_TEMPLATE_RESULT (tmpl) = fn;
4968 DECL_ARTIFICIAL (tmpl) = true;
4969 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4970 return tmpl;
4971 }
4972
4973 /* Called when a class template TYPE is redeclared with the indicated
4974 template PARMS, e.g.:
4975
4976 template <class T> struct S;
4977 template <class T> struct S {}; */
4978
4979 bool
4980 redeclare_class_template (tree type, tree parms)
4981 {
4982 tree tmpl;
4983 tree tmpl_parms;
4984 int i;
4985
4986 if (!TYPE_TEMPLATE_INFO (type))
4987 {
4988 error ("%qT is not a template type", type);
4989 return false;
4990 }
4991
4992 tmpl = TYPE_TI_TEMPLATE (type);
4993 if (!PRIMARY_TEMPLATE_P (tmpl))
4994 /* The type is nested in some template class. Nothing to worry
4995 about here; there are no new template parameters for the nested
4996 type. */
4997 return true;
4998
4999 if (!parms)
5000 {
5001 error ("template specifiers not specified in declaration of %qD",
5002 tmpl);
5003 return false;
5004 }
5005
5006 parms = INNERMOST_TEMPLATE_PARMS (parms);
5007 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5008
5009 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5010 {
5011 error_n (input_location, TREE_VEC_LENGTH (parms),
5012 "redeclared with %d template parameter",
5013 "redeclared with %d template parameters",
5014 TREE_VEC_LENGTH (parms));
5015 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5016 "previous declaration %q+D used %d template parameter",
5017 "previous declaration %q+D used %d template parameters",
5018 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5019 return false;
5020 }
5021
5022 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5023 {
5024 tree tmpl_parm;
5025 tree parm;
5026 tree tmpl_default;
5027 tree parm_default;
5028
5029 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5030 || TREE_VEC_ELT (parms, i) == error_mark_node)
5031 continue;
5032
5033 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5034 if (tmpl_parm == error_mark_node)
5035 return false;
5036
5037 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5038 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5039 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5040
5041 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5042 TEMPLATE_DECL. */
5043 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5044 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5045 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5046 || (TREE_CODE (tmpl_parm) != PARM_DECL
5047 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5048 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5049 || (TREE_CODE (tmpl_parm) == PARM_DECL
5050 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5051 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5052 {
5053 error ("template parameter %q+#D", tmpl_parm);
5054 error ("redeclared here as %q#D", parm);
5055 return false;
5056 }
5057
5058 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5059 {
5060 /* We have in [temp.param]:
5061
5062 A template-parameter may not be given default arguments
5063 by two different declarations in the same scope. */
5064 error_at (input_location, "redefinition of default argument for %q#D", parm);
5065 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5066 "original definition appeared here");
5067 return false;
5068 }
5069
5070 if (parm_default != NULL_TREE)
5071 /* Update the previous template parameters (which are the ones
5072 that will really count) with the new default value. */
5073 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5074 else if (tmpl_default != NULL_TREE)
5075 /* Update the new parameters, too; they'll be used as the
5076 parameters for any members. */
5077 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5078 }
5079
5080 return true;
5081 }
5082
5083 /* Simplify EXPR if it is a non-dependent expression. Returns the
5084 (possibly simplified) expression. */
5085
5086 tree
5087 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5088 {
5089 if (expr == NULL_TREE)
5090 return NULL_TREE;
5091
5092 /* If we're in a template, but EXPR isn't value dependent, simplify
5093 it. We're supposed to treat:
5094
5095 template <typename T> void f(T[1 + 1]);
5096 template <typename T> void f(T[2]);
5097
5098 as two declarations of the same function, for example. */
5099 if (processing_template_decl
5100 && !instantiation_dependent_expression_p (expr)
5101 && potential_constant_expression (expr))
5102 {
5103 HOST_WIDE_INT saved_processing_template_decl;
5104
5105 saved_processing_template_decl = processing_template_decl;
5106 processing_template_decl = 0;
5107 expr = tsubst_copy_and_build (expr,
5108 /*args=*/NULL_TREE,
5109 complain,
5110 /*in_decl=*/NULL_TREE,
5111 /*function_p=*/false,
5112 /*integral_constant_expression_p=*/true);
5113 processing_template_decl = saved_processing_template_decl;
5114 }
5115 return expr;
5116 }
5117
5118 tree
5119 fold_non_dependent_expr (tree expr)
5120 {
5121 return fold_non_dependent_expr_sfinae (expr, tf_error);
5122 }
5123
5124 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5125 template declaration, or a TYPE_DECL for an alias declaration. */
5126
5127 bool
5128 alias_type_or_template_p (tree t)
5129 {
5130 if (t == NULL_TREE)
5131 return false;
5132 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5133 || (TYPE_P (t)
5134 && TYPE_NAME (t)
5135 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5136 || DECL_ALIAS_TEMPLATE_P (t));
5137 }
5138
5139 /* Return TRUE iff is a specialization of an alias template. */
5140
5141 bool
5142 alias_template_specialization_p (const_tree t)
5143 {
5144 if (t == NULL_TREE)
5145 return false;
5146
5147 return (TYPE_P (t)
5148 && TYPE_TEMPLATE_INFO (t)
5149 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5150 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5151 }
5152
5153 /* Return either TMPL or another template that it is equivalent to under DR
5154 1286: An alias that just changes the name of a template is equivalent to
5155 the other template. */
5156
5157 static tree
5158 get_underlying_template (tree tmpl)
5159 {
5160 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5161 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5162 {
5163 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5164 if (TYPE_TEMPLATE_INFO (result))
5165 {
5166 tree sub = TYPE_TI_TEMPLATE (result);
5167 if (PRIMARY_TEMPLATE_P (sub)
5168 && same_type_p (result, TREE_TYPE (sub)))
5169 {
5170 /* The alias type is equivalent to the pattern of the
5171 underlying template, so strip the alias. */
5172 tmpl = sub;
5173 continue;
5174 }
5175 }
5176 break;
5177 }
5178 return tmpl;
5179 }
5180
5181 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5182 must be a function or a pointer-to-function type, as specified
5183 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5184 and check that the resulting function has external linkage. */
5185
5186 static tree
5187 convert_nontype_argument_function (tree type, tree expr)
5188 {
5189 tree fns = expr;
5190 tree fn, fn_no_ptr;
5191 linkage_kind linkage;
5192
5193 fn = instantiate_type (type, fns, tf_none);
5194 if (fn == error_mark_node)
5195 return error_mark_node;
5196
5197 fn_no_ptr = fn;
5198 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5199 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5200 if (BASELINK_P (fn_no_ptr))
5201 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5202
5203 /* [temp.arg.nontype]/1
5204
5205 A template-argument for a non-type, non-template template-parameter
5206 shall be one of:
5207 [...]
5208 -- the address of an object or function with external [C++11: or
5209 internal] linkage. */
5210
5211 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5212 {
5213 error ("%qE is not a valid template argument for type %qT", expr, type);
5214 if (TYPE_PTR_P (type))
5215 error ("it must be the address of a function with external linkage");
5216 else
5217 error ("it must be the name of a function with external linkage");
5218 return NULL_TREE;
5219 }
5220
5221 linkage = decl_linkage (fn_no_ptr);
5222 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5223 {
5224 if (cxx_dialect >= cxx11)
5225 error ("%qE is not a valid template argument for type %qT "
5226 "because %qD has no linkage",
5227 expr, type, fn_no_ptr);
5228 else
5229 error ("%qE is not a valid template argument for type %qT "
5230 "because %qD does not have external linkage",
5231 expr, type, fn_no_ptr);
5232 return NULL_TREE;
5233 }
5234
5235 return fn;
5236 }
5237
5238 /* Subroutine of convert_nontype_argument.
5239 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5240 Emit an error otherwise. */
5241
5242 static bool
5243 check_valid_ptrmem_cst_expr (tree type, tree expr,
5244 tsubst_flags_t complain)
5245 {
5246 STRIP_NOPS (expr);
5247 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5248 return true;
5249 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5250 return true;
5251 if (complain & tf_error)
5252 {
5253 error ("%qE is not a valid template argument for type %qT",
5254 expr, type);
5255 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5256 }
5257 return false;
5258 }
5259
5260 /* Returns TRUE iff the address of OP is value-dependent.
5261
5262 14.6.2.4 [temp.dep.temp]:
5263 A non-integral non-type template-argument is dependent if its type is
5264 dependent or it has either of the following forms
5265 qualified-id
5266 & qualified-id
5267 and contains a nested-name-specifier which specifies a class-name that
5268 names a dependent type.
5269
5270 We generalize this to just say that the address of a member of a
5271 dependent class is value-dependent; the above doesn't cover the
5272 address of a static data member named with an unqualified-id. */
5273
5274 static bool
5275 has_value_dependent_address (tree op)
5276 {
5277 /* We could use get_inner_reference here, but there's no need;
5278 this is only relevant for template non-type arguments, which
5279 can only be expressed as &id-expression. */
5280 if (DECL_P (op))
5281 {
5282 tree ctx = CP_DECL_CONTEXT (op);
5283 if (TYPE_P (ctx) && dependent_type_p (ctx))
5284 return true;
5285 }
5286
5287 return false;
5288 }
5289
5290 /* The next set of functions are used for providing helpful explanatory
5291 diagnostics for failed overload resolution. Their messages should be
5292 indented by two spaces for consistency with the messages in
5293 call.c */
5294
5295 static int
5296 unify_success (bool /*explain_p*/)
5297 {
5298 return 0;
5299 }
5300
5301 static int
5302 unify_parameter_deduction_failure (bool explain_p, tree parm)
5303 {
5304 if (explain_p)
5305 inform (input_location,
5306 " couldn't deduce template parameter %qD", parm);
5307 return 1;
5308 }
5309
5310 static int
5311 unify_invalid (bool /*explain_p*/)
5312 {
5313 return 1;
5314 }
5315
5316 static int
5317 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5318 {
5319 if (explain_p)
5320 inform (input_location,
5321 " types %qT and %qT have incompatible cv-qualifiers",
5322 parm, arg);
5323 return 1;
5324 }
5325
5326 static int
5327 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5328 {
5329 if (explain_p)
5330 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5331 return 1;
5332 }
5333
5334 static int
5335 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5336 {
5337 if (explain_p)
5338 inform (input_location,
5339 " template parameter %qD is not a parameter pack, but "
5340 "argument %qD is",
5341 parm, arg);
5342 return 1;
5343 }
5344
5345 static int
5346 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5347 {
5348 if (explain_p)
5349 inform (input_location,
5350 " template argument %qE does not match "
5351 "pointer-to-member constant %qE",
5352 arg, parm);
5353 return 1;
5354 }
5355
5356 static int
5357 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5358 {
5359 if (explain_p)
5360 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5361 return 1;
5362 }
5363
5364 static int
5365 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5366 {
5367 if (explain_p)
5368 inform (input_location,
5369 " inconsistent parameter pack deduction with %qT and %qT",
5370 old_arg, new_arg);
5371 return 1;
5372 }
5373
5374 static int
5375 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5376 {
5377 if (explain_p)
5378 {
5379 if (TYPE_P (parm))
5380 inform (input_location,
5381 " deduced conflicting types for parameter %qT (%qT and %qT)",
5382 parm, first, second);
5383 else
5384 inform (input_location,
5385 " deduced conflicting values for non-type parameter "
5386 "%qE (%qE and %qE)", parm, first, second);
5387 }
5388 return 1;
5389 }
5390
5391 static int
5392 unify_vla_arg (bool explain_p, tree arg)
5393 {
5394 if (explain_p)
5395 inform (input_location,
5396 " variable-sized array type %qT is not "
5397 "a valid template argument",
5398 arg);
5399 return 1;
5400 }
5401
5402 static int
5403 unify_method_type_error (bool explain_p, tree arg)
5404 {
5405 if (explain_p)
5406 inform (input_location,
5407 " member function type %qT is not a valid template argument",
5408 arg);
5409 return 1;
5410 }
5411
5412 static int
5413 unify_arity (bool explain_p, int have, int wanted)
5414 {
5415 if (explain_p)
5416 inform_n (input_location, wanted,
5417 " candidate expects %d argument, %d provided",
5418 " candidate expects %d arguments, %d provided",
5419 wanted, have);
5420 return 1;
5421 }
5422
5423 static int
5424 unify_too_many_arguments (bool explain_p, int have, int wanted)
5425 {
5426 return unify_arity (explain_p, have, wanted);
5427 }
5428
5429 static int
5430 unify_too_few_arguments (bool explain_p, int have, int wanted)
5431 {
5432 return unify_arity (explain_p, have, wanted);
5433 }
5434
5435 static int
5436 unify_arg_conversion (bool explain_p, tree to_type,
5437 tree from_type, tree arg)
5438 {
5439 if (explain_p)
5440 inform (EXPR_LOC_OR_HERE (arg),
5441 " cannot convert %qE (type %qT) to type %qT",
5442 arg, from_type, to_type);
5443 return 1;
5444 }
5445
5446 static int
5447 unify_no_common_base (bool explain_p, enum template_base_result r,
5448 tree parm, tree arg)
5449 {
5450 if (explain_p)
5451 switch (r)
5452 {
5453 case tbr_ambiguous_baseclass:
5454 inform (input_location, " %qT is an ambiguous base class of %qT",
5455 parm, arg);
5456 break;
5457 default:
5458 inform (input_location, " %qT is not derived from %qT", arg, parm);
5459 break;
5460 }
5461 return 1;
5462 }
5463
5464 static int
5465 unify_inconsistent_template_template_parameters (bool explain_p)
5466 {
5467 if (explain_p)
5468 inform (input_location,
5469 " template parameters of a template template argument are "
5470 "inconsistent with other deduced template arguments");
5471 return 1;
5472 }
5473
5474 static int
5475 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5476 {
5477 if (explain_p)
5478 inform (input_location,
5479 " can't deduce a template for %qT from non-template type %qT",
5480 parm, arg);
5481 return 1;
5482 }
5483
5484 static int
5485 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5486 {
5487 if (explain_p)
5488 inform (input_location,
5489 " template argument %qE does not match %qD", arg, parm);
5490 return 1;
5491 }
5492
5493 static int
5494 unify_overload_resolution_failure (bool explain_p, tree arg)
5495 {
5496 if (explain_p)
5497 inform (input_location,
5498 " could not resolve address from overloaded function %qE",
5499 arg);
5500 return 1;
5501 }
5502
5503 /* Attempt to convert the non-type template parameter EXPR to the
5504 indicated TYPE. If the conversion is successful, return the
5505 converted value. If the conversion is unsuccessful, return
5506 NULL_TREE if we issued an error message, or error_mark_node if we
5507 did not. We issue error messages for out-and-out bad template
5508 parameters, but not simply because the conversion failed, since we
5509 might be just trying to do argument deduction. Both TYPE and EXPR
5510 must be non-dependent.
5511
5512 The conversion follows the special rules described in
5513 [temp.arg.nontype], and it is much more strict than an implicit
5514 conversion.
5515
5516 This function is called twice for each template argument (see
5517 lookup_template_class for a more accurate description of this
5518 problem). This means that we need to handle expressions which
5519 are not valid in a C++ source, but can be created from the
5520 first call (for instance, casts to perform conversions). These
5521 hacks can go away after we fix the double coercion problem. */
5522
5523 static tree
5524 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5525 {
5526 tree expr_type;
5527
5528 /* Detect immediately string literals as invalid non-type argument.
5529 This special-case is not needed for correctness (we would easily
5530 catch this later), but only to provide better diagnostic for this
5531 common user mistake. As suggested by DR 100, we do not mention
5532 linkage issues in the diagnostic as this is not the point. */
5533 /* FIXME we're making this OK. */
5534 if (TREE_CODE (expr) == STRING_CST)
5535 {
5536 if (complain & tf_error)
5537 error ("%qE is not a valid template argument for type %qT "
5538 "because string literals can never be used in this context",
5539 expr, type);
5540 return NULL_TREE;
5541 }
5542
5543 /* Add the ADDR_EXPR now for the benefit of
5544 value_dependent_expression_p. */
5545 if (TYPE_PTROBV_P (type)
5546 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5547 {
5548 expr = decay_conversion (expr, complain);
5549 if (expr == error_mark_node)
5550 return error_mark_node;
5551 }
5552
5553 /* If we are in a template, EXPR may be non-dependent, but still
5554 have a syntactic, rather than semantic, form. For example, EXPR
5555 might be a SCOPE_REF, rather than the VAR_DECL to which the
5556 SCOPE_REF refers. Preserving the qualifying scope is necessary
5557 so that access checking can be performed when the template is
5558 instantiated -- but here we need the resolved form so that we can
5559 convert the argument. */
5560 if (TYPE_REF_OBJ_P (type)
5561 && has_value_dependent_address (expr))
5562 /* If we want the address and it's value-dependent, don't fold. */;
5563 else if (!type_unknown_p (expr))
5564 expr = fold_non_dependent_expr_sfinae (expr, complain);
5565 if (error_operand_p (expr))
5566 return error_mark_node;
5567 expr_type = TREE_TYPE (expr);
5568 if (TREE_CODE (type) == REFERENCE_TYPE)
5569 expr = mark_lvalue_use (expr);
5570 else
5571 expr = mark_rvalue_use (expr);
5572
5573 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5574 to a non-type argument of "nullptr". */
5575 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5576 expr = convert (type, expr);
5577
5578 /* In C++11, integral or enumeration non-type template arguments can be
5579 arbitrary constant expressions. Pointer and pointer to
5580 member arguments can be general constant expressions that evaluate
5581 to a null value, but otherwise still need to be of a specific form. */
5582 if (cxx_dialect >= cxx11)
5583 {
5584 if (TREE_CODE (expr) == PTRMEM_CST)
5585 /* A PTRMEM_CST is already constant, and a valid template
5586 argument for a parameter of pointer to member type, we just want
5587 to leave it in that form rather than lower it to a
5588 CONSTRUCTOR. */;
5589 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5590 expr = maybe_constant_value (expr);
5591 else if (TYPE_PTR_OR_PTRMEM_P (type))
5592 {
5593 tree folded = maybe_constant_value (expr);
5594 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5595 : null_member_pointer_value_p (folded))
5596 expr = folded;
5597 }
5598 }
5599
5600 /* HACK: Due to double coercion, we can get a
5601 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5602 which is the tree that we built on the first call (see
5603 below when coercing to reference to object or to reference to
5604 function). We just strip everything and get to the arg.
5605 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5606 for examples. */
5607 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5608 {
5609 tree probe_type, probe = expr;
5610 if (REFERENCE_REF_P (probe))
5611 probe = TREE_OPERAND (probe, 0);
5612 probe_type = TREE_TYPE (probe);
5613 if (TREE_CODE (probe) == NOP_EXPR)
5614 {
5615 /* ??? Maybe we could use convert_from_reference here, but we
5616 would need to relax its constraints because the NOP_EXPR
5617 could actually change the type to something more cv-qualified,
5618 and this is not folded by convert_from_reference. */
5619 tree addr = TREE_OPERAND (probe, 0);
5620 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5621 && TREE_CODE (addr) == ADDR_EXPR
5622 && TYPE_PTR_P (TREE_TYPE (addr))
5623 && (same_type_ignoring_top_level_qualifiers_p
5624 (TREE_TYPE (probe_type),
5625 TREE_TYPE (TREE_TYPE (addr)))))
5626 {
5627 expr = TREE_OPERAND (addr, 0);
5628 expr_type = TREE_TYPE (probe_type);
5629 }
5630 }
5631 }
5632
5633 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5634 parameter is a pointer to object, through decay and
5635 qualification conversion. Let's strip everything. */
5636 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5637 {
5638 tree probe = expr;
5639 STRIP_NOPS (probe);
5640 if (TREE_CODE (probe) == ADDR_EXPR
5641 && TYPE_PTR_P (TREE_TYPE (probe)))
5642 {
5643 /* Skip the ADDR_EXPR only if it is part of the decay for
5644 an array. Otherwise, it is part of the original argument
5645 in the source code. */
5646 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5647 probe = TREE_OPERAND (probe, 0);
5648 expr = probe;
5649 expr_type = TREE_TYPE (expr);
5650 }
5651 }
5652
5653 /* [temp.arg.nontype]/5, bullet 1
5654
5655 For a non-type template-parameter of integral or enumeration type,
5656 integral promotions (_conv.prom_) and integral conversions
5657 (_conv.integral_) are applied. */
5658 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5659 {
5660 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5661 t = maybe_constant_value (t);
5662 if (t != error_mark_node)
5663 expr = t;
5664
5665 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5666 return error_mark_node;
5667
5668 /* Notice that there are constant expressions like '4 % 0' which
5669 do not fold into integer constants. */
5670 if (TREE_CODE (expr) != INTEGER_CST)
5671 {
5672 if (complain & tf_error)
5673 {
5674 int errs = errorcount, warns = warningcount + werrorcount;
5675 if (processing_template_decl
5676 && !require_potential_constant_expression (expr))
5677 return NULL_TREE;
5678 expr = cxx_constant_value (expr);
5679 if (errorcount > errs || warningcount + werrorcount > warns)
5680 inform (EXPR_LOC_OR_HERE (expr),
5681 "in template argument for type %qT ", type);
5682 if (expr == error_mark_node)
5683 return NULL_TREE;
5684 /* else cxx_constant_value complained but gave us
5685 a real constant, so go ahead. */
5686 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5687 }
5688 else
5689 return NULL_TREE;
5690 }
5691
5692 /* Avoid typedef problems. */
5693 if (TREE_TYPE (expr) != type)
5694 expr = fold_convert (type, expr);
5695 }
5696 /* [temp.arg.nontype]/5, bullet 2
5697
5698 For a non-type template-parameter of type pointer to object,
5699 qualification conversions (_conv.qual_) and the array-to-pointer
5700 conversion (_conv.array_) are applied. */
5701 else if (TYPE_PTROBV_P (type))
5702 {
5703 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5704
5705 A template-argument for a non-type, non-template template-parameter
5706 shall be one of: [...]
5707
5708 -- the name of a non-type template-parameter;
5709 -- the address of an object or function with external linkage, [...]
5710 expressed as "& id-expression" where the & is optional if the name
5711 refers to a function or array, or if the corresponding
5712 template-parameter is a reference.
5713
5714 Here, we do not care about functions, as they are invalid anyway
5715 for a parameter of type pointer-to-object. */
5716
5717 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5718 /* Non-type template parameters are OK. */
5719 ;
5720 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5721 /* Null pointer values are OK in C++11. */;
5722 else if (TREE_CODE (expr) != ADDR_EXPR
5723 && TREE_CODE (expr_type) != ARRAY_TYPE)
5724 {
5725 if (VAR_P (expr))
5726 {
5727 error ("%qD is not a valid template argument "
5728 "because %qD is a variable, not the address of "
5729 "a variable",
5730 expr, expr);
5731 return NULL_TREE;
5732 }
5733 if (POINTER_TYPE_P (expr_type))
5734 {
5735 error ("%qE is not a valid template argument for %qT "
5736 "because it is not the address of a variable",
5737 expr, type);
5738 return NULL_TREE;
5739 }
5740 /* Other values, like integer constants, might be valid
5741 non-type arguments of some other type. */
5742 return error_mark_node;
5743 }
5744 else
5745 {
5746 tree decl;
5747
5748 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5749 ? TREE_OPERAND (expr, 0) : expr);
5750 if (!VAR_P (decl))
5751 {
5752 error ("%qE is not a valid template argument of type %qT "
5753 "because %qE is not a variable",
5754 expr, type, decl);
5755 return NULL_TREE;
5756 }
5757 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5758 {
5759 error ("%qE is not a valid template argument of type %qT "
5760 "because %qD does not have external linkage",
5761 expr, type, decl);
5762 return NULL_TREE;
5763 }
5764 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5765 {
5766 error ("%qE is not a valid template argument of type %qT "
5767 "because %qD has no linkage",
5768 expr, type, decl);
5769 return NULL_TREE;
5770 }
5771 }
5772
5773 expr = decay_conversion (expr, complain);
5774 if (expr == error_mark_node)
5775 return error_mark_node;
5776
5777 expr = perform_qualification_conversions (type, expr);
5778 if (expr == error_mark_node)
5779 return error_mark_node;
5780 }
5781 /* [temp.arg.nontype]/5, bullet 3
5782
5783 For a non-type template-parameter of type reference to object, no
5784 conversions apply. The type referred to by the reference may be more
5785 cv-qualified than the (otherwise identical) type of the
5786 template-argument. The template-parameter is bound directly to the
5787 template-argument, which must be an lvalue. */
5788 else if (TYPE_REF_OBJ_P (type))
5789 {
5790 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5791 expr_type))
5792 return error_mark_node;
5793
5794 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5795 {
5796 error ("%qE is not a valid template argument for type %qT "
5797 "because of conflicts in cv-qualification", expr, type);
5798 return NULL_TREE;
5799 }
5800
5801 if (!real_lvalue_p (expr))
5802 {
5803 error ("%qE is not a valid template argument for type %qT "
5804 "because it is not an lvalue", expr, type);
5805 return NULL_TREE;
5806 }
5807
5808 /* [temp.arg.nontype]/1
5809
5810 A template-argument for a non-type, non-template template-parameter
5811 shall be one of: [...]
5812
5813 -- the address of an object or function with external linkage. */
5814 if (INDIRECT_REF_P (expr)
5815 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5816 {
5817 expr = TREE_OPERAND (expr, 0);
5818 if (DECL_P (expr))
5819 {
5820 error ("%q#D is not a valid template argument for type %qT "
5821 "because a reference variable does not have a constant "
5822 "address", expr, type);
5823 return NULL_TREE;
5824 }
5825 }
5826
5827 if (!DECL_P (expr))
5828 {
5829 error ("%qE is not a valid template argument for type %qT "
5830 "because it is not an object with external linkage",
5831 expr, type);
5832 return NULL_TREE;
5833 }
5834
5835 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5836 {
5837 error ("%qE is not a valid template argument for type %qT "
5838 "because object %qD has not external linkage",
5839 expr, type, expr);
5840 return NULL_TREE;
5841 }
5842
5843 expr = build_nop (type, build_address (expr));
5844 }
5845 /* [temp.arg.nontype]/5, bullet 4
5846
5847 For a non-type template-parameter of type pointer to function, only
5848 the function-to-pointer conversion (_conv.func_) is applied. If the
5849 template-argument represents a set of overloaded functions (or a
5850 pointer to such), the matching function is selected from the set
5851 (_over.over_). */
5852 else if (TYPE_PTRFN_P (type))
5853 {
5854 /* If the argument is a template-id, we might not have enough
5855 context information to decay the pointer. */
5856 if (!type_unknown_p (expr_type))
5857 {
5858 expr = decay_conversion (expr, complain);
5859 if (expr == error_mark_node)
5860 return error_mark_node;
5861 }
5862
5863 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5864 /* Null pointer values are OK in C++11. */
5865 return perform_qualification_conversions (type, expr);
5866
5867 expr = convert_nontype_argument_function (type, expr);
5868 if (!expr || expr == error_mark_node)
5869 return expr;
5870 }
5871 /* [temp.arg.nontype]/5, bullet 5
5872
5873 For a non-type template-parameter of type reference to function, no
5874 conversions apply. If the template-argument represents a set of
5875 overloaded functions, the matching function is selected from the set
5876 (_over.over_). */
5877 else if (TYPE_REFFN_P (type))
5878 {
5879 if (TREE_CODE (expr) == ADDR_EXPR)
5880 {
5881 error ("%qE is not a valid template argument for type %qT "
5882 "because it is a pointer", expr, type);
5883 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5884 return NULL_TREE;
5885 }
5886
5887 expr = convert_nontype_argument_function (type, expr);
5888 if (!expr || expr == error_mark_node)
5889 return expr;
5890
5891 expr = build_nop (type, build_address (expr));
5892 }
5893 /* [temp.arg.nontype]/5, bullet 6
5894
5895 For a non-type template-parameter of type pointer to member function,
5896 no conversions apply. If the template-argument represents a set of
5897 overloaded member functions, the matching member function is selected
5898 from the set (_over.over_). */
5899 else if (TYPE_PTRMEMFUNC_P (type))
5900 {
5901 expr = instantiate_type (type, expr, tf_none);
5902 if (expr == error_mark_node)
5903 return error_mark_node;
5904
5905 /* [temp.arg.nontype] bullet 1 says the pointer to member
5906 expression must be a pointer-to-member constant. */
5907 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5908 return error_mark_node;
5909
5910 /* There is no way to disable standard conversions in
5911 resolve_address_of_overloaded_function (called by
5912 instantiate_type). It is possible that the call succeeded by
5913 converting &B::I to &D::I (where B is a base of D), so we need
5914 to reject this conversion here.
5915
5916 Actually, even if there was a way to disable standard conversions,
5917 it would still be better to reject them here so that we can
5918 provide a superior diagnostic. */
5919 if (!same_type_p (TREE_TYPE (expr), type))
5920 {
5921 error ("%qE is not a valid template argument for type %qT "
5922 "because it is of type %qT", expr, type,
5923 TREE_TYPE (expr));
5924 /* If we are just one standard conversion off, explain. */
5925 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5926 inform (input_location,
5927 "standard conversions are not allowed in this context");
5928 return NULL_TREE;
5929 }
5930 }
5931 /* [temp.arg.nontype]/5, bullet 7
5932
5933 For a non-type template-parameter of type pointer to data member,
5934 qualification conversions (_conv.qual_) are applied. */
5935 else if (TYPE_PTRDATAMEM_P (type))
5936 {
5937 /* [temp.arg.nontype] bullet 1 says the pointer to member
5938 expression must be a pointer-to-member constant. */
5939 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5940 return error_mark_node;
5941
5942 expr = perform_qualification_conversions (type, expr);
5943 if (expr == error_mark_node)
5944 return expr;
5945 }
5946 else if (NULLPTR_TYPE_P (type))
5947 {
5948 if (expr != nullptr_node)
5949 {
5950 error ("%qE is not a valid template argument for type %qT "
5951 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5952 return NULL_TREE;
5953 }
5954 return expr;
5955 }
5956 /* A template non-type parameter must be one of the above. */
5957 else
5958 gcc_unreachable ();
5959
5960 /* Sanity check: did we actually convert the argument to the
5961 right type? */
5962 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5963 (type, TREE_TYPE (expr)));
5964 return expr;
5965 }
5966
5967 /* Subroutine of coerce_template_template_parms, which returns 1 if
5968 PARM_PARM and ARG_PARM match using the rule for the template
5969 parameters of template template parameters. Both PARM and ARG are
5970 template parameters; the rest of the arguments are the same as for
5971 coerce_template_template_parms.
5972 */
5973 static int
5974 coerce_template_template_parm (tree parm,
5975 tree arg,
5976 tsubst_flags_t complain,
5977 tree in_decl,
5978 tree outer_args)
5979 {
5980 if (arg == NULL_TREE || arg == error_mark_node
5981 || parm == NULL_TREE || parm == error_mark_node)
5982 return 0;
5983
5984 if (TREE_CODE (arg) != TREE_CODE (parm))
5985 return 0;
5986
5987 switch (TREE_CODE (parm))
5988 {
5989 case TEMPLATE_DECL:
5990 /* We encounter instantiations of templates like
5991 template <template <template <class> class> class TT>
5992 class C; */
5993 {
5994 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5995 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5996
5997 if (!coerce_template_template_parms
5998 (parmparm, argparm, complain, in_decl, outer_args))
5999 return 0;
6000 }
6001 /* Fall through. */
6002
6003 case TYPE_DECL:
6004 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6005 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6006 /* Argument is a parameter pack but parameter is not. */
6007 return 0;
6008 break;
6009
6010 case PARM_DECL:
6011 /* The tsubst call is used to handle cases such as
6012
6013 template <int> class C {};
6014 template <class T, template <T> class TT> class D {};
6015 D<int, C> d;
6016
6017 i.e. the parameter list of TT depends on earlier parameters. */
6018 if (!uses_template_parms (TREE_TYPE (arg))
6019 && !same_type_p
6020 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6021 TREE_TYPE (arg)))
6022 return 0;
6023
6024 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6025 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6026 /* Argument is a parameter pack but parameter is not. */
6027 return 0;
6028
6029 break;
6030
6031 default:
6032 gcc_unreachable ();
6033 }
6034
6035 return 1;
6036 }
6037
6038
6039 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6040 template template parameters. Both PARM_PARMS and ARG_PARMS are
6041 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6042 or PARM_DECL.
6043
6044 Consider the example:
6045 template <class T> class A;
6046 template<template <class U> class TT> class B;
6047
6048 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6049 the parameters to A, and OUTER_ARGS contains A. */
6050
6051 static int
6052 coerce_template_template_parms (tree parm_parms,
6053 tree arg_parms,
6054 tsubst_flags_t complain,
6055 tree in_decl,
6056 tree outer_args)
6057 {
6058 int nparms, nargs, i;
6059 tree parm, arg;
6060 int variadic_p = 0;
6061
6062 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6063 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6064
6065 nparms = TREE_VEC_LENGTH (parm_parms);
6066 nargs = TREE_VEC_LENGTH (arg_parms);
6067
6068 /* Determine whether we have a parameter pack at the end of the
6069 template template parameter's template parameter list. */
6070 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6071 {
6072 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6073
6074 if (parm == error_mark_node)
6075 return 0;
6076
6077 switch (TREE_CODE (parm))
6078 {
6079 case TEMPLATE_DECL:
6080 case TYPE_DECL:
6081 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6082 variadic_p = 1;
6083 break;
6084
6085 case PARM_DECL:
6086 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6087 variadic_p = 1;
6088 break;
6089
6090 default:
6091 gcc_unreachable ();
6092 }
6093 }
6094
6095 if (nargs != nparms
6096 && !(variadic_p && nargs >= nparms - 1))
6097 return 0;
6098
6099 /* Check all of the template parameters except the parameter pack at
6100 the end (if any). */
6101 for (i = 0; i < nparms - variadic_p; ++i)
6102 {
6103 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6104 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6105 continue;
6106
6107 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6108 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6109
6110 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6111 outer_args))
6112 return 0;
6113
6114 }
6115
6116 if (variadic_p)
6117 {
6118 /* Check each of the template parameters in the template
6119 argument against the template parameter pack at the end of
6120 the template template parameter. */
6121 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6122 return 0;
6123
6124 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6125
6126 for (; i < nargs; ++i)
6127 {
6128 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6129 continue;
6130
6131 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6132
6133 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6134 outer_args))
6135 return 0;
6136 }
6137 }
6138
6139 return 1;
6140 }
6141
6142 /* Verifies that the deduced template arguments (in TARGS) for the
6143 template template parameters (in TPARMS) represent valid bindings,
6144 by comparing the template parameter list of each template argument
6145 to the template parameter list of its corresponding template
6146 template parameter, in accordance with DR150. This
6147 routine can only be called after all template arguments have been
6148 deduced. It will return TRUE if all of the template template
6149 parameter bindings are okay, FALSE otherwise. */
6150 bool
6151 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6152 {
6153 int i, ntparms = TREE_VEC_LENGTH (tparms);
6154 bool ret = true;
6155
6156 /* We're dealing with template parms in this process. */
6157 ++processing_template_decl;
6158
6159 targs = INNERMOST_TEMPLATE_ARGS (targs);
6160
6161 for (i = 0; i < ntparms; ++i)
6162 {
6163 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6164 tree targ = TREE_VEC_ELT (targs, i);
6165
6166 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6167 {
6168 tree packed_args = NULL_TREE;
6169 int idx, len = 1;
6170
6171 if (ARGUMENT_PACK_P (targ))
6172 {
6173 /* Look inside the argument pack. */
6174 packed_args = ARGUMENT_PACK_ARGS (targ);
6175 len = TREE_VEC_LENGTH (packed_args);
6176 }
6177
6178 for (idx = 0; idx < len; ++idx)
6179 {
6180 tree targ_parms = NULL_TREE;
6181
6182 if (packed_args)
6183 /* Extract the next argument from the argument
6184 pack. */
6185 targ = TREE_VEC_ELT (packed_args, idx);
6186
6187 if (PACK_EXPANSION_P (targ))
6188 /* Look at the pattern of the pack expansion. */
6189 targ = PACK_EXPANSION_PATTERN (targ);
6190
6191 /* Extract the template parameters from the template
6192 argument. */
6193 if (TREE_CODE (targ) == TEMPLATE_DECL)
6194 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6195 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6196 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6197
6198 /* Verify that we can coerce the template template
6199 parameters from the template argument to the template
6200 parameter. This requires an exact match. */
6201 if (targ_parms
6202 && !coerce_template_template_parms
6203 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6204 targ_parms,
6205 tf_none,
6206 tparm,
6207 targs))
6208 {
6209 ret = false;
6210 goto out;
6211 }
6212 }
6213 }
6214 }
6215
6216 out:
6217
6218 --processing_template_decl;
6219 return ret;
6220 }
6221
6222 /* Since type attributes aren't mangled, we need to strip them from
6223 template type arguments. */
6224
6225 static tree
6226 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6227 {
6228 tree mv;
6229 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6230 return arg;
6231 mv = TYPE_MAIN_VARIANT (arg);
6232 arg = strip_typedefs (arg);
6233 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6234 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6235 {
6236 if (complain & tf_warning)
6237 warning (0, "ignoring attributes on template argument %qT", arg);
6238 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6239 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6240 }
6241 return arg;
6242 }
6243
6244 /* Convert the indicated template ARG as necessary to match the
6245 indicated template PARM. Returns the converted ARG, or
6246 error_mark_node if the conversion was unsuccessful. Error and
6247 warning messages are issued under control of COMPLAIN. This
6248 conversion is for the Ith parameter in the parameter list. ARGS is
6249 the full set of template arguments deduced so far. */
6250
6251 static tree
6252 convert_template_argument (tree parm,
6253 tree arg,
6254 tree args,
6255 tsubst_flags_t complain,
6256 int i,
6257 tree in_decl)
6258 {
6259 tree orig_arg;
6260 tree val;
6261 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6262
6263 if (TREE_CODE (arg) == TREE_LIST
6264 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6265 {
6266 /* The template argument was the name of some
6267 member function. That's usually
6268 invalid, but static members are OK. In any
6269 case, grab the underlying fields/functions
6270 and issue an error later if required. */
6271 orig_arg = TREE_VALUE (arg);
6272 TREE_TYPE (arg) = unknown_type_node;
6273 }
6274
6275 orig_arg = arg;
6276
6277 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6278 requires_type = (TREE_CODE (parm) == TYPE_DECL
6279 || requires_tmpl_type);
6280
6281 /* When determining whether an argument pack expansion is a template,
6282 look at the pattern. */
6283 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6284 arg = PACK_EXPANSION_PATTERN (arg);
6285
6286 /* Deal with an injected-class-name used as a template template arg. */
6287 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6288 {
6289 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6290 if (TREE_CODE (t) == TEMPLATE_DECL)
6291 {
6292 if (cxx_dialect >= cxx11)
6293 /* OK under DR 1004. */;
6294 else if (complain & tf_warning_or_error)
6295 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6296 " used as template template argument", TYPE_NAME (arg));
6297 else if (flag_pedantic_errors)
6298 t = arg;
6299
6300 arg = t;
6301 }
6302 }
6303
6304 is_tmpl_type =
6305 ((TREE_CODE (arg) == TEMPLATE_DECL
6306 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6307 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6308 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6309 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6310
6311 if (is_tmpl_type
6312 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6313 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6314 arg = TYPE_STUB_DECL (arg);
6315
6316 is_type = TYPE_P (arg) || is_tmpl_type;
6317
6318 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6319 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6320 {
6321 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6322 {
6323 if (complain & tf_error)
6324 error ("invalid use of destructor %qE as a type", orig_arg);
6325 return error_mark_node;
6326 }
6327
6328 permerror (input_location,
6329 "to refer to a type member of a template parameter, "
6330 "use %<typename %E%>", orig_arg);
6331
6332 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6333 TREE_OPERAND (arg, 1),
6334 typename_type,
6335 complain);
6336 arg = orig_arg;
6337 is_type = 1;
6338 }
6339 if (is_type != requires_type)
6340 {
6341 if (in_decl)
6342 {
6343 if (complain & tf_error)
6344 {
6345 error ("type/value mismatch at argument %d in template "
6346 "parameter list for %qD",
6347 i + 1, in_decl);
6348 if (is_type)
6349 error (" expected a constant of type %qT, got %qT",
6350 TREE_TYPE (parm),
6351 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6352 else if (requires_tmpl_type)
6353 error (" expected a class template, got %qE", orig_arg);
6354 else
6355 error (" expected a type, got %qE", orig_arg);
6356 }
6357 }
6358 return error_mark_node;
6359 }
6360 if (is_tmpl_type ^ requires_tmpl_type)
6361 {
6362 if (in_decl && (complain & tf_error))
6363 {
6364 error ("type/value mismatch at argument %d in template "
6365 "parameter list for %qD",
6366 i + 1, in_decl);
6367 if (is_tmpl_type)
6368 error (" expected a type, got %qT", DECL_NAME (arg));
6369 else
6370 error (" expected a class template, got %qT", orig_arg);
6371 }
6372 return error_mark_node;
6373 }
6374
6375 if (is_type)
6376 {
6377 if (requires_tmpl_type)
6378 {
6379 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6380 val = orig_arg;
6381 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6382 /* The number of argument required is not known yet.
6383 Just accept it for now. */
6384 val = TREE_TYPE (arg);
6385 else
6386 {
6387 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6388 tree argparm;
6389
6390 /* Strip alias templates that are equivalent to another
6391 template. */
6392 arg = get_underlying_template (arg);
6393 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6394
6395 if (coerce_template_template_parms (parmparm, argparm,
6396 complain, in_decl,
6397 args))
6398 {
6399 val = arg;
6400
6401 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6402 TEMPLATE_DECL. */
6403 if (val != error_mark_node)
6404 {
6405 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6406 val = TREE_TYPE (val);
6407 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6408 val = make_pack_expansion (val);
6409 }
6410 }
6411 else
6412 {
6413 if (in_decl && (complain & tf_error))
6414 {
6415 error ("type/value mismatch at argument %d in "
6416 "template parameter list for %qD",
6417 i + 1, in_decl);
6418 error (" expected a template of type %qD, got %qT",
6419 parm, orig_arg);
6420 }
6421
6422 val = error_mark_node;
6423 }
6424 }
6425 }
6426 else
6427 val = orig_arg;
6428 /* We only form one instance of each template specialization.
6429 Therefore, if we use a non-canonical variant (i.e., a
6430 typedef), any future messages referring to the type will use
6431 the typedef, which is confusing if those future uses do not
6432 themselves also use the typedef. */
6433 if (TYPE_P (val))
6434 val = canonicalize_type_argument (val, complain);
6435 }
6436 else
6437 {
6438 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6439
6440 if (invalid_nontype_parm_type_p (t, complain))
6441 return error_mark_node;
6442
6443 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6444 {
6445 if (same_type_p (t, TREE_TYPE (orig_arg)))
6446 val = orig_arg;
6447 else
6448 {
6449 /* Not sure if this is reachable, but it doesn't hurt
6450 to be robust. */
6451 error ("type mismatch in nontype parameter pack");
6452 val = error_mark_node;
6453 }
6454 }
6455 else if (!dependent_template_arg_p (orig_arg)
6456 && !uses_template_parms (t))
6457 /* We used to call digest_init here. However, digest_init
6458 will report errors, which we don't want when complain
6459 is zero. More importantly, digest_init will try too
6460 hard to convert things: for example, `0' should not be
6461 converted to pointer type at this point according to
6462 the standard. Accepting this is not merely an
6463 extension, since deciding whether or not these
6464 conversions can occur is part of determining which
6465 function template to call, or whether a given explicit
6466 argument specification is valid. */
6467 val = convert_nontype_argument (t, orig_arg, complain);
6468 else
6469 val = strip_typedefs_expr (orig_arg);
6470
6471 if (val == NULL_TREE)
6472 val = error_mark_node;
6473 else if (val == error_mark_node && (complain & tf_error))
6474 error ("could not convert template argument %qE to %qT", orig_arg, t);
6475
6476 if (TREE_CODE (val) == SCOPE_REF)
6477 {
6478 /* Strip typedefs from the SCOPE_REF. */
6479 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6480 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6481 complain);
6482 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6483 QUALIFIED_NAME_IS_TEMPLATE (val));
6484 }
6485 }
6486
6487 return val;
6488 }
6489
6490 /* Coerces the remaining template arguments in INNER_ARGS (from
6491 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6492 Returns the coerced argument pack. PARM_IDX is the position of this
6493 parameter in the template parameter list. ARGS is the original
6494 template argument list. */
6495 static tree
6496 coerce_template_parameter_pack (tree parms,
6497 int parm_idx,
6498 tree args,
6499 tree inner_args,
6500 int arg_idx,
6501 tree new_args,
6502 int* lost,
6503 tree in_decl,
6504 tsubst_flags_t complain)
6505 {
6506 tree parm = TREE_VEC_ELT (parms, parm_idx);
6507 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6508 tree packed_args;
6509 tree argument_pack;
6510 tree packed_types = NULL_TREE;
6511
6512 if (arg_idx > nargs)
6513 arg_idx = nargs;
6514
6515 packed_args = make_tree_vec (nargs - arg_idx);
6516
6517 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6518 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6519 {
6520 /* When the template parameter is a non-type template
6521 parameter pack whose type uses parameter packs, we need
6522 to look at each of the template arguments
6523 separately. Build a vector of the types for these
6524 non-type template parameters in PACKED_TYPES. */
6525 tree expansion
6526 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6527 packed_types = tsubst_pack_expansion (expansion, args,
6528 complain, in_decl);
6529
6530 if (packed_types == error_mark_node)
6531 return error_mark_node;
6532
6533 /* Check that we have the right number of arguments. */
6534 if (arg_idx < nargs
6535 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6536 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6537 {
6538 int needed_parms
6539 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6540 error ("wrong number of template arguments (%d, should be %d)",
6541 nargs, needed_parms);
6542 return error_mark_node;
6543 }
6544
6545 /* If we aren't able to check the actual arguments now
6546 (because they haven't been expanded yet), we can at least
6547 verify that all of the types used for the non-type
6548 template parameter pack are, in fact, valid for non-type
6549 template parameters. */
6550 if (arg_idx < nargs
6551 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6552 {
6553 int j, len = TREE_VEC_LENGTH (packed_types);
6554 for (j = 0; j < len; ++j)
6555 {
6556 tree t = TREE_VEC_ELT (packed_types, j);
6557 if (invalid_nontype_parm_type_p (t, complain))
6558 return error_mark_node;
6559 }
6560 }
6561 }
6562
6563 /* Convert the remaining arguments, which will be a part of the
6564 parameter pack "parm". */
6565 for (; arg_idx < nargs; ++arg_idx)
6566 {
6567 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6568 tree actual_parm = TREE_VALUE (parm);
6569
6570 if (packed_types && !PACK_EXPANSION_P (arg))
6571 {
6572 /* When we have a vector of types (corresponding to the
6573 non-type template parameter pack that uses parameter
6574 packs in its type, as mention above), and the
6575 argument is not an expansion (which expands to a
6576 currently unknown number of arguments), clone the
6577 parm and give it the next type in PACKED_TYPES. */
6578 actual_parm = copy_node (actual_parm);
6579 TREE_TYPE (actual_parm) =
6580 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6581 }
6582
6583 if (arg == error_mark_node)
6584 {
6585 if (complain & tf_error)
6586 error ("template argument %d is invalid", arg_idx + 1);
6587 }
6588 else
6589 arg = convert_template_argument (actual_parm,
6590 arg, new_args, complain, parm_idx,
6591 in_decl);
6592 if (arg == error_mark_node)
6593 (*lost)++;
6594 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6595 }
6596
6597 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6598 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6599 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6600 else
6601 {
6602 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6603 TREE_TYPE (argument_pack)
6604 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6605 TREE_CONSTANT (argument_pack) = 1;
6606 }
6607
6608 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6609 #ifdef ENABLE_CHECKING
6610 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6611 TREE_VEC_LENGTH (packed_args));
6612 #endif
6613 return argument_pack;
6614 }
6615
6616 /* Returns the number of pack expansions in the template argument vector
6617 ARGS. */
6618
6619 static int
6620 pack_expansion_args_count (tree args)
6621 {
6622 int i;
6623 int count = 0;
6624 if (args)
6625 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6626 {
6627 tree elt = TREE_VEC_ELT (args, i);
6628 if (elt && PACK_EXPANSION_P (elt))
6629 ++count;
6630 }
6631 return count;
6632 }
6633
6634 /* Convert all template arguments to their appropriate types, and
6635 return a vector containing the innermost resulting template
6636 arguments. If any error occurs, return error_mark_node. Error and
6637 warning messages are issued under control of COMPLAIN.
6638
6639 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6640 for arguments not specified in ARGS. Otherwise, if
6641 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6642 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6643 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6644 ARGS. */
6645
6646 static tree
6647 coerce_template_parms (tree parms,
6648 tree args,
6649 tree in_decl,
6650 tsubst_flags_t complain,
6651 bool require_all_args,
6652 bool use_default_args)
6653 {
6654 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6655 tree inner_args;
6656 tree new_args;
6657 tree new_inner_args;
6658 int saved_unevaluated_operand;
6659 int saved_inhibit_evaluation_warnings;
6660
6661 /* When used as a boolean value, indicates whether this is a
6662 variadic template parameter list. Since it's an int, we can also
6663 subtract it from nparms to get the number of non-variadic
6664 parameters. */
6665 int variadic_p = 0;
6666 int variadic_args_p = 0;
6667 int post_variadic_parms = 0;
6668
6669 if (args == error_mark_node)
6670 return error_mark_node;
6671
6672 nparms = TREE_VEC_LENGTH (parms);
6673
6674 /* Determine if there are any parameter packs. */
6675 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6676 {
6677 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6678 if (variadic_p)
6679 ++post_variadic_parms;
6680 if (template_parameter_pack_p (tparm))
6681 ++variadic_p;
6682 }
6683
6684 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6685 /* If there are no parameters that follow a parameter pack, we need to
6686 expand any argument packs so that we can deduce a parameter pack from
6687 some non-packed args followed by an argument pack, as in variadic85.C.
6688 If there are such parameters, we need to leave argument packs intact
6689 so the arguments are assigned properly. This can happen when dealing
6690 with a nested class inside a partial specialization of a class
6691 template, as in variadic92.C, or when deducing a template parameter pack
6692 from a sub-declarator, as in variadic114.C. */
6693 if (!post_variadic_parms)
6694 inner_args = expand_template_argument_pack (inner_args);
6695
6696 /* Count any pack expansion args. */
6697 variadic_args_p = pack_expansion_args_count (inner_args);
6698
6699 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6700 if ((nargs > nparms && !variadic_p)
6701 || (nargs < nparms - variadic_p
6702 && require_all_args
6703 && !variadic_args_p
6704 && (!use_default_args
6705 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6706 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6707 {
6708 if (complain & tf_error)
6709 {
6710 if (variadic_p)
6711 {
6712 nparms -= variadic_p;
6713 error ("wrong number of template arguments "
6714 "(%d, should be %d or more)", nargs, nparms);
6715 }
6716 else
6717 error ("wrong number of template arguments "
6718 "(%d, should be %d)", nargs, nparms);
6719
6720 if (in_decl)
6721 error ("provided for %q+D", in_decl);
6722 }
6723
6724 return error_mark_node;
6725 }
6726 /* We can't pass a pack expansion to a non-pack parameter of an alias
6727 template (DR 1430). */
6728 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6729 && variadic_args_p
6730 && nargs - variadic_args_p < nparms - variadic_p)
6731 {
6732 if (complain & tf_error)
6733 {
6734 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6735 {
6736 tree arg = TREE_VEC_ELT (inner_args, i);
6737 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6738
6739 if (PACK_EXPANSION_P (arg)
6740 && !template_parameter_pack_p (parm))
6741 {
6742 error ("pack expansion argument for non-pack parameter "
6743 "%qD of alias template %qD", parm, in_decl);
6744 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6745 goto found;
6746 }
6747 }
6748 gcc_unreachable ();
6749 found:;
6750 }
6751 return error_mark_node;
6752 }
6753
6754 /* We need to evaluate the template arguments, even though this
6755 template-id may be nested within a "sizeof". */
6756 saved_unevaluated_operand = cp_unevaluated_operand;
6757 cp_unevaluated_operand = 0;
6758 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6759 c_inhibit_evaluation_warnings = 0;
6760 new_inner_args = make_tree_vec (nparms);
6761 new_args = add_outermost_template_args (args, new_inner_args);
6762 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6763 {
6764 tree arg;
6765 tree parm;
6766
6767 /* Get the Ith template parameter. */
6768 parm = TREE_VEC_ELT (parms, parm_idx);
6769
6770 if (parm == error_mark_node)
6771 {
6772 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6773 continue;
6774 }
6775
6776 /* Calculate the next argument. */
6777 if (arg_idx < nargs)
6778 arg = TREE_VEC_ELT (inner_args, arg_idx);
6779 else
6780 arg = NULL_TREE;
6781
6782 if (template_parameter_pack_p (TREE_VALUE (parm))
6783 && !(arg && ARGUMENT_PACK_P (arg)))
6784 {
6785 /* All remaining arguments will be placed in the
6786 template parameter pack PARM. */
6787 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6788 inner_args, arg_idx,
6789 new_args, &lost,
6790 in_decl, complain);
6791
6792 /* Store this argument. */
6793 if (arg == error_mark_node)
6794 lost++;
6795 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6796
6797 /* We are done with all of the arguments. */
6798 arg_idx = nargs;
6799
6800 continue;
6801 }
6802 else if (arg)
6803 {
6804 if (PACK_EXPANSION_P (arg))
6805 {
6806 /* We don't know how many args we have yet, just
6807 use the unconverted ones for now. */
6808 new_inner_args = inner_args;
6809 break;
6810 }
6811 }
6812 else if (require_all_args)
6813 {
6814 /* There must be a default arg in this case. */
6815 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6816 complain, in_decl);
6817 /* The position of the first default template argument,
6818 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6819 Record that. */
6820 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6822 }
6823 else
6824 break;
6825
6826 if (arg == error_mark_node)
6827 {
6828 if (complain & tf_error)
6829 error ("template argument %d is invalid", arg_idx + 1);
6830 }
6831 else if (!arg)
6832 /* This only occurs if there was an error in the template
6833 parameter list itself (which we would already have
6834 reported) that we are trying to recover from, e.g., a class
6835 template with a parameter list such as
6836 template<typename..., typename>. */
6837 ++lost;
6838 else
6839 arg = convert_template_argument (TREE_VALUE (parm),
6840 arg, new_args, complain,
6841 parm_idx, in_decl);
6842
6843 if (arg == error_mark_node)
6844 lost++;
6845 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6846 }
6847 cp_unevaluated_operand = saved_unevaluated_operand;
6848 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6849
6850 if (lost)
6851 return error_mark_node;
6852
6853 #ifdef ENABLE_CHECKING
6854 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6856 TREE_VEC_LENGTH (new_inner_args));
6857 #endif
6858
6859 return new_inner_args;
6860 }
6861
6862 /* Like coerce_template_parms. If PARMS represents all template
6863 parameters levels, this function returns a vector of vectors
6864 representing all the resulting argument levels. Note that in this
6865 case, only the innermost arguments are coerced because the
6866 outermost ones are supposed to have been coerced already.
6867
6868 Otherwise, if PARMS represents only (the innermost) vector of
6869 parameters, this function returns a vector containing just the
6870 innermost resulting arguments. */
6871
6872 static tree
6873 coerce_innermost_template_parms (tree parms,
6874 tree args,
6875 tree in_decl,
6876 tsubst_flags_t complain,
6877 bool require_all_args,
6878 bool use_default_args)
6879 {
6880 int parms_depth = TMPL_PARMS_DEPTH (parms);
6881 int args_depth = TMPL_ARGS_DEPTH (args);
6882 tree coerced_args;
6883
6884 if (parms_depth > 1)
6885 {
6886 coerced_args = make_tree_vec (parms_depth);
6887 tree level;
6888 int cur_depth;
6889
6890 for (level = parms, cur_depth = parms_depth;
6891 parms_depth > 0 && level != NULL_TREE;
6892 level = TREE_CHAIN (level), --cur_depth)
6893 {
6894 tree l;
6895 if (cur_depth == args_depth)
6896 l = coerce_template_parms (TREE_VALUE (level),
6897 args, in_decl, complain,
6898 require_all_args,
6899 use_default_args);
6900 else
6901 l = TMPL_ARGS_LEVEL (args, cur_depth);
6902
6903 if (l == error_mark_node)
6904 return error_mark_node;
6905
6906 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6907 }
6908 }
6909 else
6910 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6911 args, in_decl, complain,
6912 require_all_args,
6913 use_default_args);
6914 return coerced_args;
6915 }
6916
6917 /* Returns 1 if template args OT and NT are equivalent. */
6918
6919 static int
6920 template_args_equal (tree ot, tree nt)
6921 {
6922 if (nt == ot)
6923 return 1;
6924 if (nt == NULL_TREE || ot == NULL_TREE)
6925 return false;
6926
6927 if (TREE_CODE (nt) == TREE_VEC)
6928 /* For member templates */
6929 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6930 else if (PACK_EXPANSION_P (ot))
6931 return (PACK_EXPANSION_P (nt)
6932 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6933 PACK_EXPANSION_PATTERN (nt))
6934 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6935 PACK_EXPANSION_EXTRA_ARGS (nt)));
6936 else if (ARGUMENT_PACK_P (ot))
6937 {
6938 int i, len;
6939 tree opack, npack;
6940
6941 if (!ARGUMENT_PACK_P (nt))
6942 return 0;
6943
6944 opack = ARGUMENT_PACK_ARGS (ot);
6945 npack = ARGUMENT_PACK_ARGS (nt);
6946 len = TREE_VEC_LENGTH (opack);
6947 if (TREE_VEC_LENGTH (npack) != len)
6948 return 0;
6949 for (i = 0; i < len; ++i)
6950 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6951 TREE_VEC_ELT (npack, i)))
6952 return 0;
6953 return 1;
6954 }
6955 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6956 {
6957 /* We get here probably because we are in the middle of substituting
6958 into the pattern of a pack expansion. In that case the
6959 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6960 interested in. So we want to use the initial pack argument for
6961 the comparison. */
6962 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6963 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6964 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6965 return template_args_equal (ot, nt);
6966 }
6967 else if (TYPE_P (nt))
6968 return TYPE_P (ot) && same_type_p (ot, nt);
6969 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6970 return 0;
6971 else
6972 return cp_tree_equal (ot, nt);
6973 }
6974
6975 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6976 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6977 NEWARG_PTR with the offending arguments if they are non-NULL. */
6978
6979 static int
6980 comp_template_args_with_info (tree oldargs, tree newargs,
6981 tree *oldarg_ptr, tree *newarg_ptr)
6982 {
6983 int i;
6984
6985 if (oldargs == newargs)
6986 return 1;
6987
6988 if (!oldargs || !newargs)
6989 return 0;
6990
6991 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6992 return 0;
6993
6994 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6995 {
6996 tree nt = TREE_VEC_ELT (newargs, i);
6997 tree ot = TREE_VEC_ELT (oldargs, i);
6998
6999 if (! template_args_equal (ot, nt))
7000 {
7001 if (oldarg_ptr != NULL)
7002 *oldarg_ptr = ot;
7003 if (newarg_ptr != NULL)
7004 *newarg_ptr = nt;
7005 return 0;
7006 }
7007 }
7008 return 1;
7009 }
7010
7011 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7012 of template arguments. Returns 0 otherwise. */
7013
7014 int
7015 comp_template_args (tree oldargs, tree newargs)
7016 {
7017 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7018 }
7019
7020 static void
7021 add_pending_template (tree d)
7022 {
7023 tree ti = (TYPE_P (d)
7024 ? CLASSTYPE_TEMPLATE_INFO (d)
7025 : DECL_TEMPLATE_INFO (d));
7026 struct pending_template *pt;
7027 int level;
7028
7029 if (TI_PENDING_TEMPLATE_FLAG (ti))
7030 return;
7031
7032 /* We are called both from instantiate_decl, where we've already had a
7033 tinst_level pushed, and instantiate_template, where we haven't.
7034 Compensate. */
7035 level = !current_tinst_level || current_tinst_level->decl != d;
7036
7037 if (level)
7038 push_tinst_level (d);
7039
7040 pt = ggc_alloc_pending_template ();
7041 pt->next = NULL;
7042 pt->tinst = current_tinst_level;
7043 if (last_pending_template)
7044 last_pending_template->next = pt;
7045 else
7046 pending_templates = pt;
7047
7048 last_pending_template = pt;
7049
7050 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7051
7052 if (level)
7053 pop_tinst_level ();
7054 }
7055
7056
7057 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7058 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7059 documentation for TEMPLATE_ID_EXPR. */
7060
7061 tree
7062 lookup_template_function (tree fns, tree arglist)
7063 {
7064 tree type;
7065
7066 if (fns == error_mark_node || arglist == error_mark_node)
7067 return error_mark_node;
7068
7069 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7070
7071 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7072 {
7073 error ("%q#D is not a function template", fns);
7074 return error_mark_node;
7075 }
7076
7077 if (BASELINK_P (fns))
7078 {
7079 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7080 unknown_type_node,
7081 BASELINK_FUNCTIONS (fns),
7082 arglist);
7083 return fns;
7084 }
7085
7086 type = TREE_TYPE (fns);
7087 if (TREE_CODE (fns) == OVERLOAD || !type)
7088 type = unknown_type_node;
7089
7090 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7091 }
7092
7093 /* Within the scope of a template class S<T>, the name S gets bound
7094 (in build_self_reference) to a TYPE_DECL for the class, not a
7095 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7096 or one of its enclosing classes, and that type is a template,
7097 return the associated TEMPLATE_DECL. Otherwise, the original
7098 DECL is returned.
7099
7100 Also handle the case when DECL is a TREE_LIST of ambiguous
7101 injected-class-names from different bases. */
7102
7103 tree
7104 maybe_get_template_decl_from_type_decl (tree decl)
7105 {
7106 if (decl == NULL_TREE)
7107 return decl;
7108
7109 /* DR 176: A lookup that finds an injected-class-name (10.2
7110 [class.member.lookup]) can result in an ambiguity in certain cases
7111 (for example, if it is found in more than one base class). If all of
7112 the injected-class-names that are found refer to specializations of
7113 the same class template, and if the name is followed by a
7114 template-argument-list, the reference refers to the class template
7115 itself and not a specialization thereof, and is not ambiguous. */
7116 if (TREE_CODE (decl) == TREE_LIST)
7117 {
7118 tree t, tmpl = NULL_TREE;
7119 for (t = decl; t; t = TREE_CHAIN (t))
7120 {
7121 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7122 if (!tmpl)
7123 tmpl = elt;
7124 else if (tmpl != elt)
7125 break;
7126 }
7127 if (tmpl && t == NULL_TREE)
7128 return tmpl;
7129 else
7130 return decl;
7131 }
7132
7133 return (decl != NULL_TREE
7134 && DECL_SELF_REFERENCE_P (decl)
7135 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7136 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7137 }
7138
7139 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7140 parameters, find the desired type.
7141
7142 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7143
7144 IN_DECL, if non-NULL, is the template declaration we are trying to
7145 instantiate.
7146
7147 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7148 the class we are looking up.
7149
7150 Issue error and warning messages under control of COMPLAIN.
7151
7152 If the template class is really a local class in a template
7153 function, then the FUNCTION_CONTEXT is the function in which it is
7154 being instantiated.
7155
7156 ??? Note that this function is currently called *twice* for each
7157 template-id: the first time from the parser, while creating the
7158 incomplete type (finish_template_type), and the second type during the
7159 real instantiation (instantiate_template_class). This is surely something
7160 that we want to avoid. It also causes some problems with argument
7161 coercion (see convert_nontype_argument for more information on this). */
7162
7163 static tree
7164 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7165 int entering_scope, tsubst_flags_t complain)
7166 {
7167 tree templ = NULL_TREE, parmlist;
7168 tree t;
7169 void **slot;
7170 spec_entry *entry;
7171 spec_entry elt;
7172 hashval_t hash;
7173
7174 if (identifier_p (d1))
7175 {
7176 tree value = innermost_non_namespace_value (d1);
7177 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7178 templ = value;
7179 else
7180 {
7181 if (context)
7182 push_decl_namespace (context);
7183 templ = lookup_name (d1);
7184 templ = maybe_get_template_decl_from_type_decl (templ);
7185 if (context)
7186 pop_decl_namespace ();
7187 }
7188 if (templ)
7189 context = DECL_CONTEXT (templ);
7190 }
7191 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7192 {
7193 tree type = TREE_TYPE (d1);
7194
7195 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7196 an implicit typename for the second A. Deal with it. */
7197 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7198 type = TREE_TYPE (type);
7199
7200 if (CLASSTYPE_TEMPLATE_INFO (type))
7201 {
7202 templ = CLASSTYPE_TI_TEMPLATE (type);
7203 d1 = DECL_NAME (templ);
7204 }
7205 }
7206 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7207 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7208 {
7209 templ = TYPE_TI_TEMPLATE (d1);
7210 d1 = DECL_NAME (templ);
7211 }
7212 else if (TREE_CODE (d1) == TEMPLATE_DECL
7213 && DECL_TEMPLATE_RESULT (d1)
7214 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7215 {
7216 templ = d1;
7217 d1 = DECL_NAME (templ);
7218 context = DECL_CONTEXT (templ);
7219 }
7220 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7221 {
7222 templ = d1;
7223 d1 = DECL_NAME (templ);
7224 }
7225
7226 /* Issue an error message if we didn't find a template. */
7227 if (! templ)
7228 {
7229 if (complain & tf_error)
7230 error ("%qT is not a template", d1);
7231 return error_mark_node;
7232 }
7233
7234 if (TREE_CODE (templ) != TEMPLATE_DECL
7235 /* Make sure it's a user visible template, if it was named by
7236 the user. */
7237 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7238 && !PRIMARY_TEMPLATE_P (templ)))
7239 {
7240 if (complain & tf_error)
7241 {
7242 error ("non-template type %qT used as a template", d1);
7243 if (in_decl)
7244 error ("for template declaration %q+D", in_decl);
7245 }
7246 return error_mark_node;
7247 }
7248
7249 complain &= ~tf_user;
7250
7251 /* An alias that just changes the name of a template is equivalent to the
7252 other template, so if any of the arguments are pack expansions, strip
7253 the alias to avoid problems with a pack expansion passed to a non-pack
7254 alias template parameter (DR 1430). */
7255 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7256 templ = get_underlying_template (templ);
7257
7258 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7259 {
7260 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7261 template arguments */
7262
7263 tree parm;
7264 tree arglist2;
7265 tree outer;
7266
7267 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7268
7269 /* Consider an example where a template template parameter declared as
7270
7271 template <class T, class U = std::allocator<T> > class TT
7272
7273 The template parameter level of T and U are one level larger than
7274 of TT. To proper process the default argument of U, say when an
7275 instantiation `TT<int>' is seen, we need to build the full
7276 arguments containing {int} as the innermost level. Outer levels,
7277 available when not appearing as default template argument, can be
7278 obtained from the arguments of the enclosing template.
7279
7280 Suppose that TT is later substituted with std::vector. The above
7281 instantiation is `TT<int, std::allocator<T> >' with TT at
7282 level 1, and T at level 2, while the template arguments at level 1
7283 becomes {std::vector} and the inner level 2 is {int}. */
7284
7285 outer = DECL_CONTEXT (templ);
7286 if (outer)
7287 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7288 else if (current_template_parms)
7289 /* This is an argument of the current template, so we haven't set
7290 DECL_CONTEXT yet. */
7291 outer = current_template_args ();
7292
7293 if (outer)
7294 arglist = add_to_template_args (outer, arglist);
7295
7296 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7297 complain,
7298 /*require_all_args=*/true,
7299 /*use_default_args=*/true);
7300 if (arglist2 == error_mark_node
7301 || (!uses_template_parms (arglist2)
7302 && check_instantiated_args (templ, arglist2, complain)))
7303 return error_mark_node;
7304
7305 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7306 return parm;
7307 }
7308 else
7309 {
7310 tree template_type = TREE_TYPE (templ);
7311 tree gen_tmpl;
7312 tree type_decl;
7313 tree found = NULL_TREE;
7314 int arg_depth;
7315 int parm_depth;
7316 int is_dependent_type;
7317 int use_partial_inst_tmpl = false;
7318
7319 if (template_type == error_mark_node)
7320 /* An error occurred while building the template TEMPL, and a
7321 diagnostic has most certainly been emitted for that
7322 already. Let's propagate that error. */
7323 return error_mark_node;
7324
7325 gen_tmpl = most_general_template (templ);
7326 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7327 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7328 arg_depth = TMPL_ARGS_DEPTH (arglist);
7329
7330 if (arg_depth == 1 && parm_depth > 1)
7331 {
7332 /* We've been given an incomplete set of template arguments.
7333 For example, given:
7334
7335 template <class T> struct S1 {
7336 template <class U> struct S2 {};
7337 template <class U> struct S2<U*> {};
7338 };
7339
7340 we will be called with an ARGLIST of `U*', but the
7341 TEMPLATE will be `template <class T> template
7342 <class U> struct S1<T>::S2'. We must fill in the missing
7343 arguments. */
7344 arglist
7345 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7346 arglist);
7347 arg_depth = TMPL_ARGS_DEPTH (arglist);
7348 }
7349
7350 /* Now we should have enough arguments. */
7351 gcc_assert (parm_depth == arg_depth);
7352
7353 /* From here on, we're only interested in the most general
7354 template. */
7355
7356 /* Calculate the BOUND_ARGS. These will be the args that are
7357 actually tsubst'd into the definition to create the
7358 instantiation. */
7359 if (parm_depth > 1)
7360 {
7361 /* We have multiple levels of arguments to coerce, at once. */
7362 int i;
7363 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7364
7365 tree bound_args = make_tree_vec (parm_depth);
7366
7367 for (i = saved_depth,
7368 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7369 i > 0 && t != NULL_TREE;
7370 --i, t = TREE_CHAIN (t))
7371 {
7372 tree a;
7373 if (i == saved_depth)
7374 a = coerce_template_parms (TREE_VALUE (t),
7375 arglist, gen_tmpl,
7376 complain,
7377 /*require_all_args=*/true,
7378 /*use_default_args=*/true);
7379 else
7380 /* Outer levels should have already been coerced. */
7381 a = TMPL_ARGS_LEVEL (arglist, i);
7382
7383 /* Don't process further if one of the levels fails. */
7384 if (a == error_mark_node)
7385 {
7386 /* Restore the ARGLIST to its full size. */
7387 TREE_VEC_LENGTH (arglist) = saved_depth;
7388 return error_mark_node;
7389 }
7390
7391 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7392
7393 /* We temporarily reduce the length of the ARGLIST so
7394 that coerce_template_parms will see only the arguments
7395 corresponding to the template parameters it is
7396 examining. */
7397 TREE_VEC_LENGTH (arglist)--;
7398 }
7399
7400 /* Restore the ARGLIST to its full size. */
7401 TREE_VEC_LENGTH (arglist) = saved_depth;
7402
7403 arglist = bound_args;
7404 }
7405 else
7406 arglist
7407 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7408 INNERMOST_TEMPLATE_ARGS (arglist),
7409 gen_tmpl,
7410 complain,
7411 /*require_all_args=*/true,
7412 /*use_default_args=*/true);
7413
7414 if (arglist == error_mark_node)
7415 /* We were unable to bind the arguments. */
7416 return error_mark_node;
7417
7418 /* In the scope of a template class, explicit references to the
7419 template class refer to the type of the template, not any
7420 instantiation of it. For example, in:
7421
7422 template <class T> class C { void f(C<T>); }
7423
7424 the `C<T>' is just the same as `C'. Outside of the
7425 class, however, such a reference is an instantiation. */
7426 if ((entering_scope
7427 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7428 || currently_open_class (template_type))
7429 /* comp_template_args is expensive, check it last. */
7430 && comp_template_args (TYPE_TI_ARGS (template_type),
7431 arglist))
7432 return template_type;
7433
7434 /* If we already have this specialization, return it. */
7435 elt.tmpl = gen_tmpl;
7436 elt.args = arglist;
7437 hash = hash_specialization (&elt);
7438 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7439 &elt, hash);
7440
7441 if (entry)
7442 return entry->spec;
7443
7444 is_dependent_type = uses_template_parms (arglist);
7445
7446 /* If the deduced arguments are invalid, then the binding
7447 failed. */
7448 if (!is_dependent_type
7449 && check_instantiated_args (gen_tmpl,
7450 INNERMOST_TEMPLATE_ARGS (arglist),
7451 complain))
7452 return error_mark_node;
7453
7454 if (!is_dependent_type
7455 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7456 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7457 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7458 {
7459 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7460 DECL_NAME (gen_tmpl),
7461 /*tag_scope=*/ts_global);
7462 return found;
7463 }
7464
7465 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7466 complain, in_decl);
7467 if (context == error_mark_node)
7468 return error_mark_node;
7469
7470 if (!context)
7471 context = global_namespace;
7472
7473 /* Create the type. */
7474 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7475 {
7476 /* The user referred to a specialization of an alias
7477 template represented by GEN_TMPL.
7478
7479 [temp.alias]/2 says:
7480
7481 When a template-id refers to the specialization of an
7482 alias template, it is equivalent to the associated
7483 type obtained by substitution of its
7484 template-arguments for the template-parameters in the
7485 type-id of the alias template. */
7486
7487 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7488 /* Note that the call above (by indirectly calling
7489 register_specialization in tsubst_decl) registers the
7490 TYPE_DECL representing the specialization of the alias
7491 template. So next time someone substitutes ARGLIST for
7492 the template parms into the alias template (GEN_TMPL),
7493 she'll get that TYPE_DECL back. */
7494
7495 if (t == error_mark_node)
7496 return t;
7497 }
7498 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7499 {
7500 if (!is_dependent_type)
7501 {
7502 set_current_access_from_decl (TYPE_NAME (template_type));
7503 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7504 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7505 arglist, complain, in_decl),
7506 SCOPED_ENUM_P (template_type), NULL);
7507 }
7508 else
7509 {
7510 /* We don't want to call start_enum for this type, since
7511 the values for the enumeration constants may involve
7512 template parameters. And, no one should be interested
7513 in the enumeration constants for such a type. */
7514 t = cxx_make_type (ENUMERAL_TYPE);
7515 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7516 }
7517 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7518 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7519 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7520 }
7521 else if (CLASS_TYPE_P (template_type))
7522 {
7523 t = make_class_type (TREE_CODE (template_type));
7524 CLASSTYPE_DECLARED_CLASS (t)
7525 = CLASSTYPE_DECLARED_CLASS (template_type);
7526 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7527 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7528
7529 /* A local class. Make sure the decl gets registered properly. */
7530 if (context == current_function_decl)
7531 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7532
7533 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7534 /* This instantiation is another name for the primary
7535 template type. Set the TYPE_CANONICAL field
7536 appropriately. */
7537 TYPE_CANONICAL (t) = template_type;
7538 else if (any_template_arguments_need_structural_equality_p (arglist))
7539 /* Some of the template arguments require structural
7540 equality testing, so this template class requires
7541 structural equality testing. */
7542 SET_TYPE_STRUCTURAL_EQUALITY (t);
7543 }
7544 else
7545 gcc_unreachable ();
7546
7547 /* If we called start_enum or pushtag above, this information
7548 will already be set up. */
7549 if (!TYPE_NAME (t))
7550 {
7551 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7552
7553 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7554 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7555 DECL_SOURCE_LOCATION (type_decl)
7556 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7557 }
7558 else
7559 type_decl = TYPE_NAME (t);
7560
7561 if (CLASS_TYPE_P (template_type))
7562 {
7563 TREE_PRIVATE (type_decl)
7564 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7565 TREE_PROTECTED (type_decl)
7566 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7567 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7568 {
7569 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7570 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7571 }
7572 }
7573
7574 /* Let's consider the explicit specialization of a member
7575 of a class template specialization that is implicitely instantiated,
7576 e.g.:
7577 template<class T>
7578 struct S
7579 {
7580 template<class U> struct M {}; //#0
7581 };
7582
7583 template<>
7584 template<>
7585 struct S<int>::M<char> //#1
7586 {
7587 int i;
7588 };
7589 [temp.expl.spec]/4 says this is valid.
7590
7591 In this case, when we write:
7592 S<int>::M<char> m;
7593
7594 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7595 the one of #0.
7596
7597 When we encounter #1, we want to store the partial instantiation
7598 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7599
7600 For all cases other than this "explicit specialization of member of a
7601 class template", we just want to store the most general template into
7602 the CLASSTYPE_TI_TEMPLATE of M.
7603
7604 This case of "explicit specialization of member of a class template"
7605 only happens when:
7606 1/ the enclosing class is an instantiation of, and therefore not
7607 the same as, the context of the most general template, and
7608 2/ we aren't looking at the partial instantiation itself, i.e.
7609 the innermost arguments are not the same as the innermost parms of
7610 the most general template.
7611
7612 So it's only when 1/ and 2/ happens that we want to use the partial
7613 instantiation of the member template in lieu of its most general
7614 template. */
7615
7616 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7617 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7618 /* the enclosing class must be an instantiation... */
7619 && CLASS_TYPE_P (context)
7620 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7621 {
7622 tree partial_inst_args;
7623 TREE_VEC_LENGTH (arglist)--;
7624 ++processing_template_decl;
7625 partial_inst_args =
7626 tsubst (INNERMOST_TEMPLATE_ARGS
7627 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7628 arglist, complain, NULL_TREE);
7629 --processing_template_decl;
7630 TREE_VEC_LENGTH (arglist)++;
7631 use_partial_inst_tmpl =
7632 /*...and we must not be looking at the partial instantiation
7633 itself. */
7634 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7635 partial_inst_args);
7636 }
7637
7638 if (!use_partial_inst_tmpl)
7639 /* This case is easy; there are no member templates involved. */
7640 found = gen_tmpl;
7641 else
7642 {
7643 /* This is a full instantiation of a member template. Find
7644 the partial instantiation of which this is an instance. */
7645
7646 /* Temporarily reduce by one the number of levels in the ARGLIST
7647 so as to avoid comparing the last set of arguments. */
7648 TREE_VEC_LENGTH (arglist)--;
7649 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7650 TREE_VEC_LENGTH (arglist)++;
7651 /* FOUND is either a proper class type, or an alias
7652 template specialization. In the later case, it's a
7653 TYPE_DECL, resulting from the substituting of arguments
7654 for parameters in the TYPE_DECL of the alias template
7655 done earlier. So be careful while getting the template
7656 of FOUND. */
7657 found = TREE_CODE (found) == TYPE_DECL
7658 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7659 : CLASSTYPE_TI_TEMPLATE (found);
7660 }
7661
7662 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7663
7664 elt.spec = t;
7665 slot = htab_find_slot_with_hash (type_specializations,
7666 &elt, hash, INSERT);
7667 entry = ggc_alloc_spec_entry ();
7668 *entry = elt;
7669 *slot = entry;
7670
7671 /* Note this use of the partial instantiation so we can check it
7672 later in maybe_process_partial_specialization. */
7673 DECL_TEMPLATE_INSTANTIATIONS (templ)
7674 = tree_cons (arglist, t,
7675 DECL_TEMPLATE_INSTANTIATIONS (templ));
7676
7677 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7678 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7679 /* Now that the type has been registered on the instantiations
7680 list, we set up the enumerators. Because the enumeration
7681 constants may involve the enumeration type itself, we make
7682 sure to register the type first, and then create the
7683 constants. That way, doing tsubst_expr for the enumeration
7684 constants won't result in recursive calls here; we'll find
7685 the instantiation and exit above. */
7686 tsubst_enum (template_type, t, arglist);
7687
7688 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7689 /* If the type makes use of template parameters, the
7690 code that generates debugging information will crash. */
7691 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7692
7693 /* Possibly limit visibility based on template args. */
7694 TREE_PUBLIC (type_decl) = 1;
7695 determine_visibility (type_decl);
7696
7697 return t;
7698 }
7699 }
7700
7701 /* Wrapper for lookup_template_class_1. */
7702
7703 tree
7704 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7705 int entering_scope, tsubst_flags_t complain)
7706 {
7707 tree ret;
7708 timevar_push (TV_TEMPLATE_INST);
7709 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7710 entering_scope, complain);
7711 timevar_pop (TV_TEMPLATE_INST);
7712 return ret;
7713 }
7714 \f
7715 struct pair_fn_data
7716 {
7717 tree_fn_t fn;
7718 void *data;
7719 /* True when we should also visit template parameters that occur in
7720 non-deduced contexts. */
7721 bool include_nondeduced_p;
7722 struct pointer_set_t *visited;
7723 };
7724
7725 /* Called from for_each_template_parm via walk_tree. */
7726
7727 static tree
7728 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7729 {
7730 tree t = *tp;
7731 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7732 tree_fn_t fn = pfd->fn;
7733 void *data = pfd->data;
7734
7735 if (TYPE_P (t)
7736 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7737 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7738 pfd->include_nondeduced_p))
7739 return error_mark_node;
7740
7741 switch (TREE_CODE (t))
7742 {
7743 case RECORD_TYPE:
7744 if (TYPE_PTRMEMFUNC_P (t))
7745 break;
7746 /* Fall through. */
7747
7748 case UNION_TYPE:
7749 case ENUMERAL_TYPE:
7750 if (!TYPE_TEMPLATE_INFO (t))
7751 *walk_subtrees = 0;
7752 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7753 fn, data, pfd->visited,
7754 pfd->include_nondeduced_p))
7755 return error_mark_node;
7756 break;
7757
7758 case INTEGER_TYPE:
7759 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7760 fn, data, pfd->visited,
7761 pfd->include_nondeduced_p)
7762 || for_each_template_parm (TYPE_MAX_VALUE (t),
7763 fn, data, pfd->visited,
7764 pfd->include_nondeduced_p))
7765 return error_mark_node;
7766 break;
7767
7768 case METHOD_TYPE:
7769 /* Since we're not going to walk subtrees, we have to do this
7770 explicitly here. */
7771 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7772 pfd->visited, pfd->include_nondeduced_p))
7773 return error_mark_node;
7774 /* Fall through. */
7775
7776 case FUNCTION_TYPE:
7777 /* Check the return type. */
7778 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7779 pfd->include_nondeduced_p))
7780 return error_mark_node;
7781
7782 /* Check the parameter types. Since default arguments are not
7783 instantiated until they are needed, the TYPE_ARG_TYPES may
7784 contain expressions that involve template parameters. But,
7785 no-one should be looking at them yet. And, once they're
7786 instantiated, they don't contain template parameters, so
7787 there's no point in looking at them then, either. */
7788 {
7789 tree parm;
7790
7791 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7792 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7793 pfd->visited, pfd->include_nondeduced_p))
7794 return error_mark_node;
7795
7796 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7797 want walk_tree walking into them itself. */
7798 *walk_subtrees = 0;
7799 }
7800 break;
7801
7802 case TYPEOF_TYPE:
7803 case UNDERLYING_TYPE:
7804 if (pfd->include_nondeduced_p
7805 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7806 pfd->visited,
7807 pfd->include_nondeduced_p))
7808 return error_mark_node;
7809 break;
7810
7811 case FUNCTION_DECL:
7812 case VAR_DECL:
7813 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7814 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7815 pfd->visited, pfd->include_nondeduced_p))
7816 return error_mark_node;
7817 /* Fall through. */
7818
7819 case PARM_DECL:
7820 case CONST_DECL:
7821 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7822 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7823 pfd->visited, pfd->include_nondeduced_p))
7824 return error_mark_node;
7825 if (DECL_CONTEXT (t)
7826 && pfd->include_nondeduced_p
7827 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7828 pfd->visited, pfd->include_nondeduced_p))
7829 return error_mark_node;
7830 break;
7831
7832 case BOUND_TEMPLATE_TEMPLATE_PARM:
7833 /* Record template parameters such as `T' inside `TT<T>'. */
7834 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7835 pfd->include_nondeduced_p))
7836 return error_mark_node;
7837 /* Fall through. */
7838
7839 case TEMPLATE_TEMPLATE_PARM:
7840 case TEMPLATE_TYPE_PARM:
7841 case TEMPLATE_PARM_INDEX:
7842 if (fn && (*fn)(t, data))
7843 return error_mark_node;
7844 else if (!fn)
7845 return error_mark_node;
7846 break;
7847
7848 case TEMPLATE_DECL:
7849 /* A template template parameter is encountered. */
7850 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7851 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7852 pfd->include_nondeduced_p))
7853 return error_mark_node;
7854
7855 /* Already substituted template template parameter */
7856 *walk_subtrees = 0;
7857 break;
7858
7859 case TYPENAME_TYPE:
7860 if (!fn
7861 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7862 data, pfd->visited,
7863 pfd->include_nondeduced_p))
7864 return error_mark_node;
7865 break;
7866
7867 case CONSTRUCTOR:
7868 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7869 && pfd->include_nondeduced_p
7870 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7871 (TREE_TYPE (t)), fn, data,
7872 pfd->visited, pfd->include_nondeduced_p))
7873 return error_mark_node;
7874 break;
7875
7876 case INDIRECT_REF:
7877 case COMPONENT_REF:
7878 /* If there's no type, then this thing must be some expression
7879 involving template parameters. */
7880 if (!fn && !TREE_TYPE (t))
7881 return error_mark_node;
7882 break;
7883
7884 case MODOP_EXPR:
7885 case CAST_EXPR:
7886 case IMPLICIT_CONV_EXPR:
7887 case REINTERPRET_CAST_EXPR:
7888 case CONST_CAST_EXPR:
7889 case STATIC_CAST_EXPR:
7890 case DYNAMIC_CAST_EXPR:
7891 case ARROW_EXPR:
7892 case DOTSTAR_EXPR:
7893 case TYPEID_EXPR:
7894 case PSEUDO_DTOR_EXPR:
7895 if (!fn)
7896 return error_mark_node;
7897 break;
7898
7899 default:
7900 break;
7901 }
7902
7903 /* We didn't find any template parameters we liked. */
7904 return NULL_TREE;
7905 }
7906
7907 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7908 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7909 call FN with the parameter and the DATA.
7910 If FN returns nonzero, the iteration is terminated, and
7911 for_each_template_parm returns 1. Otherwise, the iteration
7912 continues. If FN never returns a nonzero value, the value
7913 returned by for_each_template_parm is 0. If FN is NULL, it is
7914 considered to be the function which always returns 1.
7915
7916 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7917 parameters that occur in non-deduced contexts. When false, only
7918 visits those template parameters that can be deduced. */
7919
7920 static int
7921 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7922 struct pointer_set_t *visited,
7923 bool include_nondeduced_p)
7924 {
7925 struct pair_fn_data pfd;
7926 int result;
7927
7928 /* Set up. */
7929 pfd.fn = fn;
7930 pfd.data = data;
7931 pfd.include_nondeduced_p = include_nondeduced_p;
7932
7933 /* Walk the tree. (Conceptually, we would like to walk without
7934 duplicates, but for_each_template_parm_r recursively calls
7935 for_each_template_parm, so we would need to reorganize a fair
7936 bit to use walk_tree_without_duplicates, so we keep our own
7937 visited list.) */
7938 if (visited)
7939 pfd.visited = visited;
7940 else
7941 pfd.visited = pointer_set_create ();
7942 result = cp_walk_tree (&t,
7943 for_each_template_parm_r,
7944 &pfd,
7945 pfd.visited) != NULL_TREE;
7946
7947 /* Clean up. */
7948 if (!visited)
7949 {
7950 pointer_set_destroy (pfd.visited);
7951 pfd.visited = 0;
7952 }
7953
7954 return result;
7955 }
7956
7957 /* Returns true if T depends on any template parameter. */
7958
7959 int
7960 uses_template_parms (tree t)
7961 {
7962 bool dependent_p;
7963 int saved_processing_template_decl;
7964
7965 saved_processing_template_decl = processing_template_decl;
7966 if (!saved_processing_template_decl)
7967 processing_template_decl = 1;
7968 if (TYPE_P (t))
7969 dependent_p = dependent_type_p (t);
7970 else if (TREE_CODE (t) == TREE_VEC)
7971 dependent_p = any_dependent_template_arguments_p (t);
7972 else if (TREE_CODE (t) == TREE_LIST)
7973 dependent_p = (uses_template_parms (TREE_VALUE (t))
7974 || uses_template_parms (TREE_CHAIN (t)));
7975 else if (TREE_CODE (t) == TYPE_DECL)
7976 dependent_p = dependent_type_p (TREE_TYPE (t));
7977 else if (DECL_P (t)
7978 || EXPR_P (t)
7979 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7980 || TREE_CODE (t) == OVERLOAD
7981 || BASELINK_P (t)
7982 || identifier_p (t)
7983 || TREE_CODE (t) == TRAIT_EXPR
7984 || TREE_CODE (t) == CONSTRUCTOR
7985 || CONSTANT_CLASS_P (t))
7986 dependent_p = (type_dependent_expression_p (t)
7987 || value_dependent_expression_p (t));
7988 else
7989 {
7990 gcc_assert (t == error_mark_node);
7991 dependent_p = false;
7992 }
7993
7994 processing_template_decl = saved_processing_template_decl;
7995
7996 return dependent_p;
7997 }
7998
7999 /* Returns true iff current_function_decl is an incompletely instantiated
8000 template. Useful instead of processing_template_decl because the latter
8001 is set to 0 during fold_non_dependent_expr. */
8002
8003 bool
8004 in_template_function (void)
8005 {
8006 tree fn = current_function_decl;
8007 bool ret;
8008 ++processing_template_decl;
8009 ret = (fn && DECL_LANG_SPECIFIC (fn)
8010 && DECL_TEMPLATE_INFO (fn)
8011 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8012 --processing_template_decl;
8013 return ret;
8014 }
8015
8016 /* Returns true if T depends on any template parameter with level LEVEL. */
8017
8018 int
8019 uses_template_parms_level (tree t, int level)
8020 {
8021 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8022 /*include_nondeduced_p=*/true);
8023 }
8024
8025 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8026 ill-formed translation unit, i.e. a variable or function that isn't
8027 usable in a constant expression. */
8028
8029 static inline bool
8030 neglectable_inst_p (tree d)
8031 {
8032 return (DECL_P (d)
8033 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8034 : decl_maybe_constant_var_p (d)));
8035 }
8036
8037 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8038 neglectable and instantiated from within an erroneous instantiation. */
8039
8040 static bool
8041 limit_bad_template_recursion (tree decl)
8042 {
8043 struct tinst_level *lev = current_tinst_level;
8044 int errs = errorcount + sorrycount;
8045 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8046 return false;
8047
8048 for (; lev; lev = lev->next)
8049 if (neglectable_inst_p (lev->decl))
8050 break;
8051
8052 return (lev && errs > lev->errors);
8053 }
8054
8055 static int tinst_depth;
8056 extern int max_tinst_depth;
8057 int depth_reached;
8058
8059 static GTY(()) struct tinst_level *last_error_tinst_level;
8060
8061 /* We're starting to instantiate D; record the template instantiation context
8062 for diagnostics and to restore it later. */
8063
8064 int
8065 push_tinst_level (tree d)
8066 {
8067 struct tinst_level *new_level;
8068
8069 if (tinst_depth >= max_tinst_depth)
8070 {
8071 last_error_tinst_level = current_tinst_level;
8072 if (TREE_CODE (d) == TREE_LIST)
8073 error ("template instantiation depth exceeds maximum of %d (use "
8074 "-ftemplate-depth= to increase the maximum) substituting %qS",
8075 max_tinst_depth, d);
8076 else
8077 error ("template instantiation depth exceeds maximum of %d (use "
8078 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8079 max_tinst_depth, d);
8080
8081 print_instantiation_context ();
8082
8083 return 0;
8084 }
8085
8086 /* If the current instantiation caused problems, don't let it instantiate
8087 anything else. Do allow deduction substitution and decls usable in
8088 constant expressions. */
8089 if (limit_bad_template_recursion (d))
8090 return 0;
8091
8092 new_level = ggc_alloc_tinst_level ();
8093 new_level->decl = d;
8094 new_level->locus = input_location;
8095 new_level->errors = errorcount+sorrycount;
8096 new_level->in_system_header_p = in_system_header;
8097 new_level->next = current_tinst_level;
8098 current_tinst_level = new_level;
8099
8100 ++tinst_depth;
8101 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8102 depth_reached = tinst_depth;
8103
8104 return 1;
8105 }
8106
8107 /* We're done instantiating this template; return to the instantiation
8108 context. */
8109
8110 void
8111 pop_tinst_level (void)
8112 {
8113 /* Restore the filename and line number stashed away when we started
8114 this instantiation. */
8115 input_location = current_tinst_level->locus;
8116 current_tinst_level = current_tinst_level->next;
8117 --tinst_depth;
8118 }
8119
8120 /* We're instantiating a deferred template; restore the template
8121 instantiation context in which the instantiation was requested, which
8122 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8123
8124 static tree
8125 reopen_tinst_level (struct tinst_level *level)
8126 {
8127 struct tinst_level *t;
8128
8129 tinst_depth = 0;
8130 for (t = level; t; t = t->next)
8131 ++tinst_depth;
8132
8133 current_tinst_level = level;
8134 pop_tinst_level ();
8135 if (current_tinst_level)
8136 current_tinst_level->errors = errorcount+sorrycount;
8137 return level->decl;
8138 }
8139
8140 /* Returns the TINST_LEVEL which gives the original instantiation
8141 context. */
8142
8143 struct tinst_level *
8144 outermost_tinst_level (void)
8145 {
8146 struct tinst_level *level = current_tinst_level;
8147 if (level)
8148 while (level->next)
8149 level = level->next;
8150 return level;
8151 }
8152
8153 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8154 vector of template arguments, as for tsubst.
8155
8156 Returns an appropriate tsubst'd friend declaration. */
8157
8158 static tree
8159 tsubst_friend_function (tree decl, tree args)
8160 {
8161 tree new_friend;
8162
8163 if (TREE_CODE (decl) == FUNCTION_DECL
8164 && DECL_TEMPLATE_INSTANTIATION (decl)
8165 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8166 /* This was a friend declared with an explicit template
8167 argument list, e.g.:
8168
8169 friend void f<>(T);
8170
8171 to indicate that f was a template instantiation, not a new
8172 function declaration. Now, we have to figure out what
8173 instantiation of what template. */
8174 {
8175 tree template_id, arglist, fns;
8176 tree new_args;
8177 tree tmpl;
8178 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8179
8180 /* Friend functions are looked up in the containing namespace scope.
8181 We must enter that scope, to avoid finding member functions of the
8182 current class with same name. */
8183 push_nested_namespace (ns);
8184 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8185 tf_warning_or_error, NULL_TREE,
8186 /*integral_constant_expression_p=*/false);
8187 pop_nested_namespace (ns);
8188 arglist = tsubst (DECL_TI_ARGS (decl), args,
8189 tf_warning_or_error, NULL_TREE);
8190 template_id = lookup_template_function (fns, arglist);
8191
8192 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8193 tmpl = determine_specialization (template_id, new_friend,
8194 &new_args,
8195 /*need_member_template=*/0,
8196 TREE_VEC_LENGTH (args),
8197 tsk_none);
8198 return instantiate_template (tmpl, new_args, tf_error);
8199 }
8200
8201 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8202
8203 /* The NEW_FRIEND will look like an instantiation, to the
8204 compiler, but is not an instantiation from the point of view of
8205 the language. For example, we might have had:
8206
8207 template <class T> struct S {
8208 template <class U> friend void f(T, U);
8209 };
8210
8211 Then, in S<int>, template <class U> void f(int, U) is not an
8212 instantiation of anything. */
8213 if (new_friend == error_mark_node)
8214 return error_mark_node;
8215
8216 DECL_USE_TEMPLATE (new_friend) = 0;
8217 if (TREE_CODE (decl) == TEMPLATE_DECL)
8218 {
8219 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8220 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8221 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8222 }
8223
8224 /* The mangled name for the NEW_FRIEND is incorrect. The function
8225 is not a template instantiation and should not be mangled like
8226 one. Therefore, we forget the mangling here; we'll recompute it
8227 later if we need it. */
8228 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8229 {
8230 SET_DECL_RTL (new_friend, NULL);
8231 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8232 }
8233
8234 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8235 {
8236 tree old_decl;
8237 tree new_friend_template_info;
8238 tree new_friend_result_template_info;
8239 tree ns;
8240 int new_friend_is_defn;
8241
8242 /* We must save some information from NEW_FRIEND before calling
8243 duplicate decls since that function will free NEW_FRIEND if
8244 possible. */
8245 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8246 new_friend_is_defn =
8247 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8248 (template_for_substitution (new_friend)))
8249 != NULL_TREE);
8250 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8251 {
8252 /* This declaration is a `primary' template. */
8253 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8254
8255 new_friend_result_template_info
8256 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8257 }
8258 else
8259 new_friend_result_template_info = NULL_TREE;
8260
8261 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8262 if (new_friend_is_defn)
8263 DECL_INITIAL (new_friend) = error_mark_node;
8264
8265 /* Inside pushdecl_namespace_level, we will push into the
8266 current namespace. However, the friend function should go
8267 into the namespace of the template. */
8268 ns = decl_namespace_context (new_friend);
8269 push_nested_namespace (ns);
8270 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8271 pop_nested_namespace (ns);
8272
8273 if (old_decl == error_mark_node)
8274 return error_mark_node;
8275
8276 if (old_decl != new_friend)
8277 {
8278 /* This new friend declaration matched an existing
8279 declaration. For example, given:
8280
8281 template <class T> void f(T);
8282 template <class U> class C {
8283 template <class T> friend void f(T) {}
8284 };
8285
8286 the friend declaration actually provides the definition
8287 of `f', once C has been instantiated for some type. So,
8288 old_decl will be the out-of-class template declaration,
8289 while new_friend is the in-class definition.
8290
8291 But, if `f' was called before this point, the
8292 instantiation of `f' will have DECL_TI_ARGS corresponding
8293 to `T' but not to `U', references to which might appear
8294 in the definition of `f'. Previously, the most general
8295 template for an instantiation of `f' was the out-of-class
8296 version; now it is the in-class version. Therefore, we
8297 run through all specialization of `f', adding to their
8298 DECL_TI_ARGS appropriately. In particular, they need a
8299 new set of outer arguments, corresponding to the
8300 arguments for this class instantiation.
8301
8302 The same situation can arise with something like this:
8303
8304 friend void f(int);
8305 template <class T> class C {
8306 friend void f(T) {}
8307 };
8308
8309 when `C<int>' is instantiated. Now, `f(int)' is defined
8310 in the class. */
8311
8312 if (!new_friend_is_defn)
8313 /* On the other hand, if the in-class declaration does
8314 *not* provide a definition, then we don't want to alter
8315 existing definitions. We can just leave everything
8316 alone. */
8317 ;
8318 else
8319 {
8320 tree new_template = TI_TEMPLATE (new_friend_template_info);
8321 tree new_args = TI_ARGS (new_friend_template_info);
8322
8323 /* Overwrite whatever template info was there before, if
8324 any, with the new template information pertaining to
8325 the declaration. */
8326 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8327
8328 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8329 {
8330 /* We should have called reregister_specialization in
8331 duplicate_decls. */
8332 gcc_assert (retrieve_specialization (new_template,
8333 new_args, 0)
8334 == old_decl);
8335
8336 /* Instantiate it if the global has already been used. */
8337 if (DECL_ODR_USED (old_decl))
8338 instantiate_decl (old_decl, /*defer_ok=*/true,
8339 /*expl_inst_class_mem_p=*/false);
8340 }
8341 else
8342 {
8343 tree t;
8344
8345 /* Indicate that the old function template is a partial
8346 instantiation. */
8347 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8348 = new_friend_result_template_info;
8349
8350 gcc_assert (new_template
8351 == most_general_template (new_template));
8352 gcc_assert (new_template != old_decl);
8353
8354 /* Reassign any specializations already in the hash table
8355 to the new more general template, and add the
8356 additional template args. */
8357 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8358 t != NULL_TREE;
8359 t = TREE_CHAIN (t))
8360 {
8361 tree spec = TREE_VALUE (t);
8362 spec_entry elt;
8363
8364 elt.tmpl = old_decl;
8365 elt.args = DECL_TI_ARGS (spec);
8366 elt.spec = NULL_TREE;
8367
8368 htab_remove_elt (decl_specializations, &elt);
8369
8370 DECL_TI_ARGS (spec)
8371 = add_outermost_template_args (new_args,
8372 DECL_TI_ARGS (spec));
8373
8374 register_specialization
8375 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8376
8377 }
8378 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8379 }
8380 }
8381
8382 /* The information from NEW_FRIEND has been merged into OLD_DECL
8383 by duplicate_decls. */
8384 new_friend = old_decl;
8385 }
8386 }
8387 else
8388 {
8389 tree context = DECL_CONTEXT (new_friend);
8390 bool dependent_p;
8391
8392 /* In the code
8393 template <class T> class C {
8394 template <class U> friend void C1<U>::f (); // case 1
8395 friend void C2<T>::f (); // case 2
8396 };
8397 we only need to make sure CONTEXT is a complete type for
8398 case 2. To distinguish between the two cases, we note that
8399 CONTEXT of case 1 remains dependent type after tsubst while
8400 this isn't true for case 2. */
8401 ++processing_template_decl;
8402 dependent_p = dependent_type_p (context);
8403 --processing_template_decl;
8404
8405 if (!dependent_p
8406 && !complete_type_or_else (context, NULL_TREE))
8407 return error_mark_node;
8408
8409 if (COMPLETE_TYPE_P (context))
8410 {
8411 /* Check to see that the declaration is really present, and,
8412 possibly obtain an improved declaration. */
8413 tree fn = check_classfn (context,
8414 new_friend, NULL_TREE);
8415
8416 if (fn)
8417 new_friend = fn;
8418 }
8419 }
8420
8421 return new_friend;
8422 }
8423
8424 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8425 template arguments, as for tsubst.
8426
8427 Returns an appropriate tsubst'd friend type or error_mark_node on
8428 failure. */
8429
8430 static tree
8431 tsubst_friend_class (tree friend_tmpl, tree args)
8432 {
8433 tree friend_type;
8434 tree tmpl;
8435 tree context;
8436
8437 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8438 {
8439 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8440 return TREE_TYPE (t);
8441 }
8442
8443 context = CP_DECL_CONTEXT (friend_tmpl);
8444
8445 if (context != global_namespace)
8446 {
8447 if (TREE_CODE (context) == NAMESPACE_DECL)
8448 push_nested_namespace (context);
8449 else
8450 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8451 }
8452
8453 /* Look for a class template declaration. We look for hidden names
8454 because two friend declarations of the same template are the
8455 same. For example, in:
8456
8457 struct A {
8458 template <typename> friend class F;
8459 };
8460 template <typename> struct B {
8461 template <typename> friend class F;
8462 };
8463
8464 both F templates are the same. */
8465 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8466 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8467
8468 /* But, if we don't find one, it might be because we're in a
8469 situation like this:
8470
8471 template <class T>
8472 struct S {
8473 template <class U>
8474 friend struct S;
8475 };
8476
8477 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8478 for `S<int>', not the TEMPLATE_DECL. */
8479 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8480 {
8481 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8482 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8483 }
8484
8485 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8486 {
8487 /* The friend template has already been declared. Just
8488 check to see that the declarations match, and install any new
8489 default parameters. We must tsubst the default parameters,
8490 of course. We only need the innermost template parameters
8491 because that is all that redeclare_class_template will look
8492 at. */
8493 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8494 > TMPL_ARGS_DEPTH (args))
8495 {
8496 tree parms;
8497 location_t saved_input_location;
8498 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8499 args, tf_warning_or_error);
8500
8501 saved_input_location = input_location;
8502 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8503 redeclare_class_template (TREE_TYPE (tmpl), parms);
8504 input_location = saved_input_location;
8505
8506 }
8507
8508 friend_type = TREE_TYPE (tmpl);
8509 }
8510 else
8511 {
8512 /* The friend template has not already been declared. In this
8513 case, the instantiation of the template class will cause the
8514 injection of this template into the global scope. */
8515 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8516 if (tmpl == error_mark_node)
8517 return error_mark_node;
8518
8519 /* The new TMPL is not an instantiation of anything, so we
8520 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8521 the new type because that is supposed to be the corresponding
8522 template decl, i.e., TMPL. */
8523 DECL_USE_TEMPLATE (tmpl) = 0;
8524 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8525 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8526 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8527 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8528
8529 /* Inject this template into the global scope. */
8530 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8531 }
8532
8533 if (context != global_namespace)
8534 {
8535 if (TREE_CODE (context) == NAMESPACE_DECL)
8536 pop_nested_namespace (context);
8537 else
8538 pop_nested_class ();
8539 }
8540
8541 return friend_type;
8542 }
8543
8544 /* Returns zero if TYPE cannot be completed later due to circularity.
8545 Otherwise returns one. */
8546
8547 static int
8548 can_complete_type_without_circularity (tree type)
8549 {
8550 if (type == NULL_TREE || type == error_mark_node)
8551 return 0;
8552 else if (COMPLETE_TYPE_P (type))
8553 return 1;
8554 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8555 return can_complete_type_without_circularity (TREE_TYPE (type));
8556 else if (CLASS_TYPE_P (type)
8557 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8558 return 0;
8559 else
8560 return 1;
8561 }
8562
8563 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8564
8565 /* Apply any attributes which had to be deferred until instantiation
8566 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8567 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8568
8569 static void
8570 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8571 tree args, tsubst_flags_t complain, tree in_decl)
8572 {
8573 tree last_dep = NULL_TREE;
8574 tree t;
8575 tree *p;
8576
8577 for (t = attributes; t; t = TREE_CHAIN (t))
8578 if (ATTR_IS_DEPENDENT (t))
8579 {
8580 last_dep = t;
8581 attributes = copy_list (attributes);
8582 break;
8583 }
8584
8585 if (DECL_P (*decl_p))
8586 {
8587 if (TREE_TYPE (*decl_p) == error_mark_node)
8588 return;
8589 p = &DECL_ATTRIBUTES (*decl_p);
8590 }
8591 else
8592 p = &TYPE_ATTRIBUTES (*decl_p);
8593
8594 if (last_dep)
8595 {
8596 tree late_attrs = NULL_TREE;
8597 tree *q = &late_attrs;
8598
8599 for (*p = attributes; *p; )
8600 {
8601 t = *p;
8602 if (ATTR_IS_DEPENDENT (t))
8603 {
8604 *p = TREE_CHAIN (t);
8605 TREE_CHAIN (t) = NULL_TREE;
8606 if (flag_openmp
8607 && is_attribute_p ("omp declare simd",
8608 get_attribute_name (t))
8609 && TREE_VALUE (t))
8610 {
8611 tree clauses = TREE_VALUE (TREE_VALUE (t));
8612 clauses = tsubst_omp_clauses (clauses, true, args,
8613 complain, in_decl);
8614 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8615 clauses = finish_omp_clauses (clauses);
8616 tree parms = DECL_ARGUMENTS (*decl_p);
8617 clauses
8618 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8619 if (clauses)
8620 TREE_VALUE (TREE_VALUE (t)) = clauses;
8621 else
8622 TREE_VALUE (t) = NULL_TREE;
8623 }
8624 /* If the first attribute argument is an identifier, don't
8625 pass it through tsubst. Attributes like mode, format,
8626 cleanup and several target specific attributes expect it
8627 unmodified. */
8628 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8629 {
8630 tree chain
8631 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8632 in_decl,
8633 /*integral_constant_expression_p=*/false);
8634 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8635 TREE_VALUE (t)
8636 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8637 chain);
8638 }
8639 else
8640 TREE_VALUE (t)
8641 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8642 /*integral_constant_expression_p=*/false);
8643 *q = t;
8644 q = &TREE_CHAIN (t);
8645 }
8646 else
8647 p = &TREE_CHAIN (t);
8648 }
8649
8650 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8651 }
8652 }
8653
8654 /* Perform (or defer) access check for typedefs that were referenced
8655 from within the template TMPL code.
8656 This is a subroutine of instantiate_decl and instantiate_class_template.
8657 TMPL is the template to consider and TARGS is the list of arguments of
8658 that template. */
8659
8660 static void
8661 perform_typedefs_access_check (tree tmpl, tree targs)
8662 {
8663 location_t saved_location;
8664 unsigned i;
8665 qualified_typedef_usage_t *iter;
8666
8667 if (!tmpl
8668 || (!CLASS_TYPE_P (tmpl)
8669 && TREE_CODE (tmpl) != FUNCTION_DECL))
8670 return;
8671
8672 saved_location = input_location;
8673 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8674 {
8675 tree type_decl = iter->typedef_decl;
8676 tree type_scope = iter->context;
8677
8678 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8679 continue;
8680
8681 if (uses_template_parms (type_decl))
8682 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8683 if (uses_template_parms (type_scope))
8684 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8685
8686 /* Make access check error messages point to the location
8687 of the use of the typedef. */
8688 input_location = iter->locus;
8689 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8690 type_decl, type_decl,
8691 tf_warning_or_error);
8692 }
8693 input_location = saved_location;
8694 }
8695
8696 static tree
8697 instantiate_class_template_1 (tree type)
8698 {
8699 tree templ, args, pattern, t, member;
8700 tree typedecl;
8701 tree pbinfo;
8702 tree base_list;
8703 unsigned int saved_maximum_field_alignment;
8704 tree fn_context;
8705
8706 if (type == error_mark_node)
8707 return error_mark_node;
8708
8709 if (COMPLETE_OR_OPEN_TYPE_P (type)
8710 || uses_template_parms (type))
8711 return type;
8712
8713 /* Figure out which template is being instantiated. */
8714 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8715 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8716
8717 /* Determine what specialization of the original template to
8718 instantiate. */
8719 t = most_specialized_class (type, templ, tf_warning_or_error);
8720 if (t == error_mark_node)
8721 {
8722 TYPE_BEING_DEFINED (type) = 1;
8723 return error_mark_node;
8724 }
8725 else if (t)
8726 {
8727 /* This TYPE is actually an instantiation of a partial
8728 specialization. We replace the innermost set of ARGS with
8729 the arguments appropriate for substitution. For example,
8730 given:
8731
8732 template <class T> struct S {};
8733 template <class T> struct S<T*> {};
8734
8735 and supposing that we are instantiating S<int*>, ARGS will
8736 presently be {int*} -- but we need {int}. */
8737 pattern = TREE_TYPE (t);
8738 args = TREE_PURPOSE (t);
8739 }
8740 else
8741 {
8742 pattern = TREE_TYPE (templ);
8743 args = CLASSTYPE_TI_ARGS (type);
8744 }
8745
8746 /* If the template we're instantiating is incomplete, then clearly
8747 there's nothing we can do. */
8748 if (!COMPLETE_TYPE_P (pattern))
8749 return type;
8750
8751 /* If we've recursively instantiated too many templates, stop. */
8752 if (! push_tinst_level (type))
8753 return type;
8754
8755 /* Now we're really doing the instantiation. Mark the type as in
8756 the process of being defined. */
8757 TYPE_BEING_DEFINED (type) = 1;
8758
8759 /* We may be in the middle of deferred access check. Disable
8760 it now. */
8761 push_deferring_access_checks (dk_no_deferred);
8762
8763 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8764 if (!fn_context)
8765 push_to_top_level ();
8766 /* Use #pragma pack from the template context. */
8767 saved_maximum_field_alignment = maximum_field_alignment;
8768 maximum_field_alignment = TYPE_PRECISION (pattern);
8769
8770 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8771
8772 /* Set the input location to the most specialized template definition.
8773 This is needed if tsubsting causes an error. */
8774 typedecl = TYPE_MAIN_DECL (pattern);
8775 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8776 DECL_SOURCE_LOCATION (typedecl);
8777
8778 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8779 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8780 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8781 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8782 if (ANON_AGGR_TYPE_P (pattern))
8783 SET_ANON_AGGR_TYPE_P (type);
8784 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8785 {
8786 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8787 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8788 /* Adjust visibility for template arguments. */
8789 determine_visibility (TYPE_MAIN_DECL (type));
8790 }
8791 if (CLASS_TYPE_P (type))
8792 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8793
8794 pbinfo = TYPE_BINFO (pattern);
8795
8796 /* We should never instantiate a nested class before its enclosing
8797 class; we need to look up the nested class by name before we can
8798 instantiate it, and that lookup should instantiate the enclosing
8799 class. */
8800 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8801 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8802
8803 base_list = NULL_TREE;
8804 if (BINFO_N_BASE_BINFOS (pbinfo))
8805 {
8806 tree pbase_binfo;
8807 tree pushed_scope;
8808 int i;
8809
8810 /* We must enter the scope containing the type, as that is where
8811 the accessibility of types named in dependent bases are
8812 looked up from. */
8813 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8814
8815 /* Substitute into each of the bases to determine the actual
8816 basetypes. */
8817 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8818 {
8819 tree base;
8820 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8821 tree expanded_bases = NULL_TREE;
8822 int idx, len = 1;
8823
8824 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8825 {
8826 expanded_bases =
8827 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8828 args, tf_error, NULL_TREE);
8829 if (expanded_bases == error_mark_node)
8830 continue;
8831
8832 len = TREE_VEC_LENGTH (expanded_bases);
8833 }
8834
8835 for (idx = 0; idx < len; idx++)
8836 {
8837 if (expanded_bases)
8838 /* Extract the already-expanded base class. */
8839 base = TREE_VEC_ELT (expanded_bases, idx);
8840 else
8841 /* Substitute to figure out the base class. */
8842 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8843 NULL_TREE);
8844
8845 if (base == error_mark_node)
8846 continue;
8847
8848 base_list = tree_cons (access, base, base_list);
8849 if (BINFO_VIRTUAL_P (pbase_binfo))
8850 TREE_TYPE (base_list) = integer_type_node;
8851 }
8852 }
8853
8854 /* The list is now in reverse order; correct that. */
8855 base_list = nreverse (base_list);
8856
8857 if (pushed_scope)
8858 pop_scope (pushed_scope);
8859 }
8860 /* Now call xref_basetypes to set up all the base-class
8861 information. */
8862 xref_basetypes (type, base_list);
8863
8864 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8865 (int) ATTR_FLAG_TYPE_IN_PLACE,
8866 args, tf_error, NULL_TREE);
8867 fixup_attribute_variants (type);
8868
8869 /* Now that our base classes are set up, enter the scope of the
8870 class, so that name lookups into base classes, etc. will work
8871 correctly. This is precisely analogous to what we do in
8872 begin_class_definition when defining an ordinary non-template
8873 class, except we also need to push the enclosing classes. */
8874 push_nested_class (type);
8875
8876 /* Now members are processed in the order of declaration. */
8877 for (member = CLASSTYPE_DECL_LIST (pattern);
8878 member; member = TREE_CHAIN (member))
8879 {
8880 tree t = TREE_VALUE (member);
8881
8882 if (TREE_PURPOSE (member))
8883 {
8884 if (TYPE_P (t))
8885 {
8886 /* Build new CLASSTYPE_NESTED_UTDS. */
8887
8888 tree newtag;
8889 bool class_template_p;
8890
8891 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8892 && TYPE_LANG_SPECIFIC (t)
8893 && CLASSTYPE_IS_TEMPLATE (t));
8894 /* If the member is a class template, then -- even after
8895 substitution -- there may be dependent types in the
8896 template argument list for the class. We increment
8897 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8898 that function will assume that no types are dependent
8899 when outside of a template. */
8900 if (class_template_p)
8901 ++processing_template_decl;
8902 newtag = tsubst (t, args, tf_error, NULL_TREE);
8903 if (class_template_p)
8904 --processing_template_decl;
8905 if (newtag == error_mark_node)
8906 continue;
8907
8908 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8909 {
8910 tree name = TYPE_IDENTIFIER (t);
8911
8912 if (class_template_p)
8913 /* Unfortunately, lookup_template_class sets
8914 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8915 instantiation (i.e., for the type of a member
8916 template class nested within a template class.)
8917 This behavior is required for
8918 maybe_process_partial_specialization to work
8919 correctly, but is not accurate in this case;
8920 the TAG is not an instantiation of anything.
8921 (The corresponding TEMPLATE_DECL is an
8922 instantiation, but the TYPE is not.) */
8923 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8924
8925 /* Now, we call pushtag to put this NEWTAG into the scope of
8926 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8927 pushtag calling push_template_decl. We don't have to do
8928 this for enums because it will already have been done in
8929 tsubst_enum. */
8930 if (name)
8931 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8932 pushtag (name, newtag, /*tag_scope=*/ts_current);
8933 }
8934 }
8935 else if (DECL_DECLARES_FUNCTION_P (t))
8936 {
8937 /* Build new TYPE_METHODS. */
8938 tree r;
8939
8940 if (TREE_CODE (t) == TEMPLATE_DECL)
8941 ++processing_template_decl;
8942 r = tsubst (t, args, tf_error, NULL_TREE);
8943 if (TREE_CODE (t) == TEMPLATE_DECL)
8944 --processing_template_decl;
8945 set_current_access_from_decl (r);
8946 finish_member_declaration (r);
8947 /* Instantiate members marked with attribute used. */
8948 if (r != error_mark_node && DECL_PRESERVE_P (r))
8949 mark_used (r);
8950 if (TREE_CODE (r) == FUNCTION_DECL
8951 && DECL_OMP_DECLARE_REDUCTION_P (r))
8952 cp_check_omp_declare_reduction (r);
8953 }
8954 else
8955 {
8956 /* Build new TYPE_FIELDS. */
8957 if (TREE_CODE (t) == STATIC_ASSERT)
8958 {
8959 tree condition;
8960
8961 ++c_inhibit_evaluation_warnings;
8962 condition =
8963 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8964 tf_warning_or_error, NULL_TREE,
8965 /*integral_constant_expression_p=*/true);
8966 --c_inhibit_evaluation_warnings;
8967
8968 finish_static_assert (condition,
8969 STATIC_ASSERT_MESSAGE (t),
8970 STATIC_ASSERT_SOURCE_LOCATION (t),
8971 /*member_p=*/true);
8972 }
8973 else if (TREE_CODE (t) != CONST_DECL)
8974 {
8975 tree r;
8976 tree vec = NULL_TREE;
8977 int len = 1;
8978
8979 /* The file and line for this declaration, to
8980 assist in error message reporting. Since we
8981 called push_tinst_level above, we don't need to
8982 restore these. */
8983 input_location = DECL_SOURCE_LOCATION (t);
8984
8985 if (TREE_CODE (t) == TEMPLATE_DECL)
8986 ++processing_template_decl;
8987 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8988 if (TREE_CODE (t) == TEMPLATE_DECL)
8989 --processing_template_decl;
8990
8991 if (TREE_CODE (r) == TREE_VEC)
8992 {
8993 /* A capture pack became multiple fields. */
8994 vec = r;
8995 len = TREE_VEC_LENGTH (vec);
8996 }
8997
8998 for (int i = 0; i < len; ++i)
8999 {
9000 if (vec)
9001 r = TREE_VEC_ELT (vec, i);
9002 if (VAR_P (r))
9003 {
9004 /* In [temp.inst]:
9005
9006 [t]he initialization (and any associated
9007 side-effects) of a static data member does
9008 not occur unless the static data member is
9009 itself used in a way that requires the
9010 definition of the static data member to
9011 exist.
9012
9013 Therefore, we do not substitute into the
9014 initialized for the static data member here. */
9015 finish_static_data_member_decl
9016 (r,
9017 /*init=*/NULL_TREE,
9018 /*init_const_expr_p=*/false,
9019 /*asmspec_tree=*/NULL_TREE,
9020 /*flags=*/0);
9021 /* Instantiate members marked with attribute used. */
9022 if (r != error_mark_node && DECL_PRESERVE_P (r))
9023 mark_used (r);
9024 }
9025 else if (TREE_CODE (r) == FIELD_DECL)
9026 {
9027 /* Determine whether R has a valid type and can be
9028 completed later. If R is invalid, then its type
9029 is replaced by error_mark_node. */
9030 tree rtype = TREE_TYPE (r);
9031 if (can_complete_type_without_circularity (rtype))
9032 complete_type (rtype);
9033
9034 if (!COMPLETE_TYPE_P (rtype))
9035 {
9036 cxx_incomplete_type_error (r, rtype);
9037 TREE_TYPE (r) = error_mark_node;
9038 }
9039 }
9040
9041 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9042 such a thing will already have been added to the field
9043 list by tsubst_enum in finish_member_declaration in the
9044 CLASSTYPE_NESTED_UTDS case above. */
9045 if (!(TREE_CODE (r) == TYPE_DECL
9046 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9047 && DECL_ARTIFICIAL (r)))
9048 {
9049 set_current_access_from_decl (r);
9050 finish_member_declaration (r);
9051 }
9052 }
9053 }
9054 }
9055 }
9056 else
9057 {
9058 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9059 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9060 {
9061 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9062
9063 tree friend_type = t;
9064 bool adjust_processing_template_decl = false;
9065
9066 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9067 {
9068 /* template <class T> friend class C; */
9069 friend_type = tsubst_friend_class (friend_type, args);
9070 adjust_processing_template_decl = true;
9071 }
9072 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9073 {
9074 /* template <class T> friend class C::D; */
9075 friend_type = tsubst (friend_type, args,
9076 tf_warning_or_error, NULL_TREE);
9077 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9078 friend_type = TREE_TYPE (friend_type);
9079 adjust_processing_template_decl = true;
9080 }
9081 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9082 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9083 {
9084 /* This could be either
9085
9086 friend class T::C;
9087
9088 when dependent_type_p is false or
9089
9090 template <class U> friend class T::C;
9091
9092 otherwise. */
9093 friend_type = tsubst (friend_type, args,
9094 tf_warning_or_error, NULL_TREE);
9095 /* Bump processing_template_decl for correct
9096 dependent_type_p calculation. */
9097 ++processing_template_decl;
9098 if (dependent_type_p (friend_type))
9099 adjust_processing_template_decl = true;
9100 --processing_template_decl;
9101 }
9102 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9103 && hidden_name_p (TYPE_NAME (friend_type)))
9104 {
9105 /* friend class C;
9106
9107 where C hasn't been declared yet. Let's lookup name
9108 from namespace scope directly, bypassing any name that
9109 come from dependent base class. */
9110 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9111
9112 /* The call to xref_tag_from_type does injection for friend
9113 classes. */
9114 push_nested_namespace (ns);
9115 friend_type =
9116 xref_tag_from_type (friend_type, NULL_TREE,
9117 /*tag_scope=*/ts_current);
9118 pop_nested_namespace (ns);
9119 }
9120 else if (uses_template_parms (friend_type))
9121 /* friend class C<T>; */
9122 friend_type = tsubst (friend_type, args,
9123 tf_warning_or_error, NULL_TREE);
9124 /* Otherwise it's
9125
9126 friend class C;
9127
9128 where C is already declared or
9129
9130 friend class C<int>;
9131
9132 We don't have to do anything in these cases. */
9133
9134 if (adjust_processing_template_decl)
9135 /* Trick make_friend_class into realizing that the friend
9136 we're adding is a template, not an ordinary class. It's
9137 important that we use make_friend_class since it will
9138 perform some error-checking and output cross-reference
9139 information. */
9140 ++processing_template_decl;
9141
9142 if (friend_type != error_mark_node)
9143 make_friend_class (type, friend_type, /*complain=*/false);
9144
9145 if (adjust_processing_template_decl)
9146 --processing_template_decl;
9147 }
9148 else
9149 {
9150 /* Build new DECL_FRIENDLIST. */
9151 tree r;
9152
9153 /* The file and line for this declaration, to
9154 assist in error message reporting. Since we
9155 called push_tinst_level above, we don't need to
9156 restore these. */
9157 input_location = DECL_SOURCE_LOCATION (t);
9158
9159 if (TREE_CODE (t) == TEMPLATE_DECL)
9160 {
9161 ++processing_template_decl;
9162 push_deferring_access_checks (dk_no_check);
9163 }
9164
9165 r = tsubst_friend_function (t, args);
9166 add_friend (type, r, /*complain=*/false);
9167 if (TREE_CODE (t) == TEMPLATE_DECL)
9168 {
9169 pop_deferring_access_checks ();
9170 --processing_template_decl;
9171 }
9172 }
9173 }
9174 }
9175
9176 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9177 {
9178 tree decl = lambda_function (type);
9179 if (decl)
9180 {
9181 if (!DECL_TEMPLATE_INFO (decl)
9182 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9183 instantiate_decl (decl, false, false);
9184
9185 /* We need to instantiate the capture list from the template
9186 after we've instantiated the closure members, but before we
9187 consider adding the conversion op. Also keep any captures
9188 that may have been added during instantiation of the op(). */
9189 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9190 tree tmpl_cap
9191 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9192 args, tf_warning_or_error, NULL_TREE,
9193 false, false);
9194
9195 LAMBDA_EXPR_CAPTURE_LIST (expr)
9196 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9197
9198 maybe_add_lambda_conv_op (type);
9199 }
9200 else
9201 gcc_assert (errorcount);
9202 }
9203
9204 /* Set the file and line number information to whatever is given for
9205 the class itself. This puts error messages involving generated
9206 implicit functions at a predictable point, and the same point
9207 that would be used for non-template classes. */
9208 input_location = DECL_SOURCE_LOCATION (typedecl);
9209
9210 unreverse_member_declarations (type);
9211 finish_struct_1 (type);
9212 TYPE_BEING_DEFINED (type) = 0;
9213
9214 /* We don't instantiate default arguments for member functions. 14.7.1:
9215
9216 The implicit instantiation of a class template specialization causes
9217 the implicit instantiation of the declarations, but not of the
9218 definitions or default arguments, of the class member functions,
9219 member classes, static data members and member templates.... */
9220
9221 /* Some typedefs referenced from within the template code need to be access
9222 checked at template instantiation time, i.e now. These types were
9223 added to the template at parsing time. Let's get those and perform
9224 the access checks then. */
9225 perform_typedefs_access_check (pattern, args);
9226 perform_deferred_access_checks (tf_warning_or_error);
9227 pop_nested_class ();
9228 maximum_field_alignment = saved_maximum_field_alignment;
9229 if (!fn_context)
9230 pop_from_top_level ();
9231 pop_deferring_access_checks ();
9232 pop_tinst_level ();
9233
9234 /* The vtable for a template class can be emitted in any translation
9235 unit in which the class is instantiated. When there is no key
9236 method, however, finish_struct_1 will already have added TYPE to
9237 the keyed_classes list. */
9238 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9239 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9240
9241 return type;
9242 }
9243
9244 /* Wrapper for instantiate_class_template_1. */
9245
9246 tree
9247 instantiate_class_template (tree type)
9248 {
9249 tree ret;
9250 timevar_push (TV_TEMPLATE_INST);
9251 ret = instantiate_class_template_1 (type);
9252 timevar_pop (TV_TEMPLATE_INST);
9253 return ret;
9254 }
9255
9256 static tree
9257 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9258 {
9259 tree r;
9260
9261 if (!t)
9262 r = t;
9263 else if (TYPE_P (t))
9264 r = tsubst (t, args, complain, in_decl);
9265 else
9266 {
9267 if (!(complain & tf_warning))
9268 ++c_inhibit_evaluation_warnings;
9269 r = tsubst_expr (t, args, complain, in_decl,
9270 /*integral_constant_expression_p=*/true);
9271 if (!(complain & tf_warning))
9272 --c_inhibit_evaluation_warnings;
9273 /* Preserve the raw-reference nature of T. */
9274 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9275 && REFERENCE_REF_P (r))
9276 r = TREE_OPERAND (r, 0);
9277 }
9278 return r;
9279 }
9280
9281 /* Given a function parameter pack TMPL_PARM and some function parameters
9282 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9283 and set *SPEC_P to point at the next point in the list. */
9284
9285 static tree
9286 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9287 {
9288 /* Collect all of the extra "packed" parameters into an
9289 argument pack. */
9290 tree parmvec;
9291 tree parmtypevec;
9292 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9293 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9294 tree spec_parm = *spec_p;
9295 int i, len;
9296
9297 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9298 if (tmpl_parm
9299 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9300 break;
9301
9302 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9303 parmvec = make_tree_vec (len);
9304 parmtypevec = make_tree_vec (len);
9305 spec_parm = *spec_p;
9306 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9307 {
9308 TREE_VEC_ELT (parmvec, i) = spec_parm;
9309 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9310 }
9311
9312 /* Build the argument packs. */
9313 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9314 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9315 TREE_TYPE (argpack) = argtypepack;
9316 *spec_p = spec_parm;
9317
9318 return argpack;
9319 }
9320
9321 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9322 NONTYPE_ARGUMENT_PACK. */
9323
9324 static tree
9325 make_fnparm_pack (tree spec_parm)
9326 {
9327 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9328 }
9329
9330 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9331 pack expansion. */
9332
9333 static bool
9334 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9335 {
9336 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9337 if (i >= TREE_VEC_LENGTH (vec))
9338 return false;
9339 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9340 }
9341
9342
9343 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9344
9345 static tree
9346 make_argument_pack_select (tree arg_pack, unsigned index)
9347 {
9348 tree aps = make_node (ARGUMENT_PACK_SELECT);
9349
9350 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9351 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9352
9353 return aps;
9354 }
9355
9356 /* This is a subroutine of tsubst_pack_expansion.
9357
9358 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9359 mechanism to store the (non complete list of) arguments of the
9360 substitution and return a non substituted pack expansion, in order
9361 to wait for when we have enough arguments to really perform the
9362 substitution. */
9363
9364 static bool
9365 use_pack_expansion_extra_args_p (tree parm_packs,
9366 int arg_pack_len,
9367 bool has_empty_arg)
9368 {
9369 /* If one pack has an expansion and another pack has a normal
9370 argument or if one pack has an empty argument and an another
9371 one hasn't then tsubst_pack_expansion cannot perform the
9372 substitution and need to fall back on the
9373 PACK_EXPANSION_EXTRA mechanism. */
9374 if (parm_packs == NULL_TREE)
9375 return false;
9376 else if (has_empty_arg)
9377 return true;
9378
9379 bool has_expansion_arg = false;
9380 for (int i = 0 ; i < arg_pack_len; ++i)
9381 {
9382 bool has_non_expansion_arg = false;
9383 for (tree parm_pack = parm_packs;
9384 parm_pack;
9385 parm_pack = TREE_CHAIN (parm_pack))
9386 {
9387 tree arg = TREE_VALUE (parm_pack);
9388
9389 if (argument_pack_element_is_expansion_p (arg, i))
9390 has_expansion_arg = true;
9391 else
9392 has_non_expansion_arg = true;
9393 }
9394
9395 if (has_expansion_arg && has_non_expansion_arg)
9396 return true;
9397 }
9398 return false;
9399 }
9400
9401 /* [temp.variadic]/6 says that:
9402
9403 The instantiation of a pack expansion [...]
9404 produces a list E1,E2, ..., En, where N is the number of elements
9405 in the pack expansion parameters.
9406
9407 This subroutine of tsubst_pack_expansion produces one of these Ei.
9408
9409 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9410 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9411 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9412 INDEX is the index 'i' of the element Ei to produce. ARGS,
9413 COMPLAIN, and IN_DECL are the same parameters as for the
9414 tsubst_pack_expansion function.
9415
9416 The function returns the resulting Ei upon successful completion,
9417 or error_mark_node.
9418
9419 Note that this function possibly modifies the ARGS parameter, so
9420 it's the responsibility of the caller to restore it. */
9421
9422 static tree
9423 gen_elem_of_pack_expansion_instantiation (tree pattern,
9424 tree parm_packs,
9425 unsigned index,
9426 tree args /* This parm gets
9427 modified. */,
9428 tsubst_flags_t complain,
9429 tree in_decl)
9430 {
9431 tree t;
9432 bool ith_elem_is_expansion = false;
9433
9434 /* For each parameter pack, change the substitution of the parameter
9435 pack to the ith argument in its argument pack, then expand the
9436 pattern. */
9437 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9438 {
9439 tree parm = TREE_PURPOSE (pack);
9440 tree arg_pack = TREE_VALUE (pack);
9441 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9442
9443 ith_elem_is_expansion |=
9444 argument_pack_element_is_expansion_p (arg_pack, index);
9445
9446 /* Select the Ith argument from the pack. */
9447 if (TREE_CODE (parm) == PARM_DECL
9448 || TREE_CODE (parm) == FIELD_DECL)
9449 {
9450 if (index == 0)
9451 {
9452 aps = make_argument_pack_select (arg_pack, index);
9453 mark_used (parm);
9454 register_local_specialization (aps, parm);
9455 }
9456 else
9457 aps = retrieve_local_specialization (parm);
9458 }
9459 else
9460 {
9461 int idx, level;
9462 template_parm_level_and_index (parm, &level, &idx);
9463
9464 if (index == 0)
9465 {
9466 aps = make_argument_pack_select (arg_pack, index);
9467 /* Update the corresponding argument. */
9468 TMPL_ARG (args, level, idx) = aps;
9469 }
9470 else
9471 /* Re-use the ARGUMENT_PACK_SELECT. */
9472 aps = TMPL_ARG (args, level, idx);
9473 }
9474 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9475 }
9476
9477 /* Substitute into the PATTERN with the (possibly altered)
9478 arguments. */
9479 if (!TYPE_P (pattern))
9480 t = tsubst_expr (pattern, args, complain, in_decl,
9481 /*integral_constant_expression_p=*/false);
9482 else
9483 t = tsubst (pattern, args, complain, in_decl);
9484
9485 /* If the Ith argument pack element is a pack expansion, then
9486 the Ith element resulting from the substituting is going to
9487 be a pack expansion as well. */
9488 if (ith_elem_is_expansion)
9489 t = make_pack_expansion (t);
9490
9491 return t;
9492 }
9493
9494 /* Substitute ARGS into T, which is an pack expansion
9495 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9496 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9497 (if only a partial substitution could be performed) or
9498 ERROR_MARK_NODE if there was an error. */
9499 tree
9500 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9501 tree in_decl)
9502 {
9503 tree pattern;
9504 tree pack, packs = NULL_TREE;
9505 bool unsubstituted_packs = false;
9506 int i, len = -1;
9507 tree result;
9508 struct pointer_map_t *saved_local_specializations = NULL;
9509 bool need_local_specializations = false;
9510 int levels;
9511
9512 gcc_assert (PACK_EXPANSION_P (t));
9513 pattern = PACK_EXPANSION_PATTERN (t);
9514
9515 /* Add in any args remembered from an earlier partial instantiation. */
9516 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9517
9518 levels = TMPL_ARGS_DEPTH (args);
9519
9520 /* Determine the argument packs that will instantiate the parameter
9521 packs used in the expansion expression. While we're at it,
9522 compute the number of arguments to be expanded and make sure it
9523 is consistent. */
9524 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9525 pack = TREE_CHAIN (pack))
9526 {
9527 tree parm_pack = TREE_VALUE (pack);
9528 tree arg_pack = NULL_TREE;
9529 tree orig_arg = NULL_TREE;
9530 int level = 0;
9531
9532 if (TREE_CODE (parm_pack) == BASES)
9533 {
9534 if (BASES_DIRECT (parm_pack))
9535 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9536 args, complain, in_decl, false));
9537 else
9538 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9539 args, complain, in_decl, false));
9540 }
9541 if (TREE_CODE (parm_pack) == PARM_DECL)
9542 {
9543 if (PACK_EXPANSION_LOCAL_P (t))
9544 arg_pack = retrieve_local_specialization (parm_pack);
9545 else
9546 {
9547 /* We can't rely on local_specializations for a parameter
9548 name used later in a function declaration (such as in a
9549 late-specified return type). Even if it exists, it might
9550 have the wrong value for a recursive call. Just make a
9551 dummy decl, since it's only used for its type. */
9552 arg_pack = tsubst_decl (parm_pack, args, complain);
9553 if (arg_pack && DECL_PACK_P (arg_pack))
9554 /* Partial instantiation of the parm_pack, we can't build
9555 up an argument pack yet. */
9556 arg_pack = NULL_TREE;
9557 else
9558 arg_pack = make_fnparm_pack (arg_pack);
9559 need_local_specializations = true;
9560 }
9561 }
9562 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9563 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9564 else
9565 {
9566 int idx;
9567 template_parm_level_and_index (parm_pack, &level, &idx);
9568
9569 if (level <= levels)
9570 arg_pack = TMPL_ARG (args, level, idx);
9571 }
9572
9573 orig_arg = arg_pack;
9574 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9575 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9576
9577 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9578 /* This can only happen if we forget to expand an argument
9579 pack somewhere else. Just return an error, silently. */
9580 {
9581 result = make_tree_vec (1);
9582 TREE_VEC_ELT (result, 0) = error_mark_node;
9583 return result;
9584 }
9585
9586 if (arg_pack)
9587 {
9588 int my_len =
9589 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9590
9591 /* Don't bother trying to do a partial substitution with
9592 incomplete packs; we'll try again after deduction. */
9593 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9594 return t;
9595
9596 if (len < 0)
9597 len = my_len;
9598 else if (len != my_len)
9599 {
9600 if (!(complain & tf_error))
9601 /* Fail quietly. */;
9602 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9603 error ("mismatched argument pack lengths while expanding "
9604 "%<%T%>",
9605 pattern);
9606 else
9607 error ("mismatched argument pack lengths while expanding "
9608 "%<%E%>",
9609 pattern);
9610 return error_mark_node;
9611 }
9612
9613 /* Keep track of the parameter packs and their corresponding
9614 argument packs. */
9615 packs = tree_cons (parm_pack, arg_pack, packs);
9616 TREE_TYPE (packs) = orig_arg;
9617 }
9618 else
9619 {
9620 /* We can't substitute for this parameter pack. We use a flag as
9621 well as the missing_level counter because function parameter
9622 packs don't have a level. */
9623 unsubstituted_packs = true;
9624 }
9625 }
9626
9627 /* We cannot expand this expansion expression, because we don't have
9628 all of the argument packs we need. */
9629 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9630 {
9631 /* We got some full packs, but we can't substitute them in until we
9632 have values for all the packs. So remember these until then. */
9633
9634 t = make_pack_expansion (pattern);
9635 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9636 return t;
9637 }
9638 else if (unsubstituted_packs)
9639 {
9640 /* There were no real arguments, we're just replacing a parameter
9641 pack with another version of itself. Substitute into the
9642 pattern and return a PACK_EXPANSION_*. The caller will need to
9643 deal with that. */
9644 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9645 t = tsubst_expr (pattern, args, complain, in_decl,
9646 /*integral_constant_expression_p=*/false);
9647 else
9648 t = tsubst (pattern, args, complain, in_decl);
9649 t = make_pack_expansion (t);
9650 return t;
9651 }
9652
9653 gcc_assert (len >= 0);
9654
9655 if (need_local_specializations)
9656 {
9657 /* We're in a late-specified return type, so create our own local
9658 specializations map; the current map is either NULL or (in the
9659 case of recursive unification) might have bindings that we don't
9660 want to use or alter. */
9661 saved_local_specializations = local_specializations;
9662 local_specializations = pointer_map_create ();
9663 }
9664
9665 /* For each argument in each argument pack, substitute into the
9666 pattern. */
9667 result = make_tree_vec (len);
9668 for (i = 0; i < len; ++i)
9669 {
9670 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9671 i,
9672 args, complain,
9673 in_decl);
9674 TREE_VEC_ELT (result, i) = t;
9675 if (t == error_mark_node)
9676 {
9677 result = error_mark_node;
9678 break;
9679 }
9680 }
9681
9682 /* Update ARGS to restore the substitution from parameter packs to
9683 their argument packs. */
9684 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9685 {
9686 tree parm = TREE_PURPOSE (pack);
9687
9688 if (TREE_CODE (parm) == PARM_DECL
9689 || TREE_CODE (parm) == FIELD_DECL)
9690 register_local_specialization (TREE_TYPE (pack), parm);
9691 else
9692 {
9693 int idx, level;
9694
9695 if (TREE_VALUE (pack) == NULL_TREE)
9696 continue;
9697
9698 template_parm_level_and_index (parm, &level, &idx);
9699
9700 /* Update the corresponding argument. */
9701 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9702 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9703 TREE_TYPE (pack);
9704 else
9705 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9706 }
9707 }
9708
9709 if (need_local_specializations)
9710 {
9711 pointer_map_destroy (local_specializations);
9712 local_specializations = saved_local_specializations;
9713 }
9714
9715 return result;
9716 }
9717
9718 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9719 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9720 parameter packs; all parms generated from a function parameter pack will
9721 have the same DECL_PARM_INDEX. */
9722
9723 tree
9724 get_pattern_parm (tree parm, tree tmpl)
9725 {
9726 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9727 tree patparm;
9728
9729 if (DECL_ARTIFICIAL (parm))
9730 {
9731 for (patparm = DECL_ARGUMENTS (pattern);
9732 patparm; patparm = DECL_CHAIN (patparm))
9733 if (DECL_ARTIFICIAL (patparm)
9734 && DECL_NAME (parm) == DECL_NAME (patparm))
9735 break;
9736 }
9737 else
9738 {
9739 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9740 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9741 gcc_assert (DECL_PARM_INDEX (patparm)
9742 == DECL_PARM_INDEX (parm));
9743 }
9744
9745 return patparm;
9746 }
9747
9748 /* Substitute ARGS into the vector or list of template arguments T. */
9749
9750 static tree
9751 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9752 {
9753 tree orig_t = t;
9754 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9755 tree *elts;
9756
9757 if (t == error_mark_node)
9758 return error_mark_node;
9759
9760 len = TREE_VEC_LENGTH (t);
9761 elts = XALLOCAVEC (tree, len);
9762
9763 for (i = 0; i < len; i++)
9764 {
9765 tree orig_arg = TREE_VEC_ELT (t, i);
9766 tree new_arg;
9767
9768 if (TREE_CODE (orig_arg) == TREE_VEC)
9769 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9770 else if (PACK_EXPANSION_P (orig_arg))
9771 {
9772 /* Substitute into an expansion expression. */
9773 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9774
9775 if (TREE_CODE (new_arg) == TREE_VEC)
9776 /* Add to the expanded length adjustment the number of
9777 expanded arguments. We subtract one from this
9778 measurement, because the argument pack expression
9779 itself is already counted as 1 in
9780 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9781 the argument pack is empty. */
9782 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9783 }
9784 else if (ARGUMENT_PACK_P (orig_arg))
9785 {
9786 /* Substitute into each of the arguments. */
9787 new_arg = TYPE_P (orig_arg)
9788 ? cxx_make_type (TREE_CODE (orig_arg))
9789 : make_node (TREE_CODE (orig_arg));
9790
9791 SET_ARGUMENT_PACK_ARGS (
9792 new_arg,
9793 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9794 args, complain, in_decl));
9795
9796 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9797 new_arg = error_mark_node;
9798
9799 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9800 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9801 complain, in_decl);
9802 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9803
9804 if (TREE_TYPE (new_arg) == error_mark_node)
9805 new_arg = error_mark_node;
9806 }
9807 }
9808 else
9809 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9810
9811 if (new_arg == error_mark_node)
9812 return error_mark_node;
9813
9814 elts[i] = new_arg;
9815 if (new_arg != orig_arg)
9816 need_new = 1;
9817 }
9818
9819 if (!need_new)
9820 return t;
9821
9822 /* Make space for the expanded arguments coming from template
9823 argument packs. */
9824 t = make_tree_vec (len + expanded_len_adjust);
9825 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9826 arguments for a member template.
9827 In that case each TREE_VEC in ORIG_T represents a level of template
9828 arguments, and ORIG_T won't carry any non defaulted argument count.
9829 It will rather be the nested TREE_VECs that will carry one.
9830 In other words, ORIG_T carries a non defaulted argument count only
9831 if it doesn't contain any nested TREE_VEC. */
9832 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9833 {
9834 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9835 count += expanded_len_adjust;
9836 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9837 }
9838 for (i = 0, out = 0; i < len; i++)
9839 {
9840 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9841 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9842 && TREE_CODE (elts[i]) == TREE_VEC)
9843 {
9844 int idx;
9845
9846 /* Now expand the template argument pack "in place". */
9847 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9848 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9849 }
9850 else
9851 {
9852 TREE_VEC_ELT (t, out) = elts[i];
9853 out++;
9854 }
9855 }
9856
9857 return t;
9858 }
9859
9860 /* Return the result of substituting ARGS into the template parameters
9861 given by PARMS. If there are m levels of ARGS and m + n levels of
9862 PARMS, then the result will contain n levels of PARMS. For
9863 example, if PARMS is `template <class T> template <class U>
9864 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9865 result will be `template <int*, double, class V>'. */
9866
9867 static tree
9868 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9869 {
9870 tree r = NULL_TREE;
9871 tree* new_parms;
9872
9873 /* When substituting into a template, we must set
9874 PROCESSING_TEMPLATE_DECL as the template parameters may be
9875 dependent if they are based on one-another, and the dependency
9876 predicates are short-circuit outside of templates. */
9877 ++processing_template_decl;
9878
9879 for (new_parms = &r;
9880 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9881 new_parms = &(TREE_CHAIN (*new_parms)),
9882 parms = TREE_CHAIN (parms))
9883 {
9884 tree new_vec =
9885 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9886 int i;
9887
9888 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9889 {
9890 tree tuple;
9891
9892 if (parms == error_mark_node)
9893 continue;
9894
9895 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9896
9897 if (tuple == error_mark_node)
9898 continue;
9899
9900 TREE_VEC_ELT (new_vec, i) =
9901 tsubst_template_parm (tuple, args, complain);
9902 }
9903
9904 *new_parms =
9905 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9906 - TMPL_ARGS_DEPTH (args)),
9907 new_vec, NULL_TREE);
9908 }
9909
9910 --processing_template_decl;
9911
9912 return r;
9913 }
9914
9915 /* Return the result of substituting ARGS into one template parameter
9916 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9917 parameter and which TREE_PURPOSE is the default argument of the
9918 template parameter. */
9919
9920 static tree
9921 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9922 {
9923 tree default_value, parm_decl;
9924
9925 if (args == NULL_TREE
9926 || t == NULL_TREE
9927 || t == error_mark_node)
9928 return t;
9929
9930 gcc_assert (TREE_CODE (t) == TREE_LIST);
9931
9932 default_value = TREE_PURPOSE (t);
9933 parm_decl = TREE_VALUE (t);
9934
9935 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9936 if (TREE_CODE (parm_decl) == PARM_DECL
9937 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9938 parm_decl = error_mark_node;
9939 default_value = tsubst_template_arg (default_value, args,
9940 complain, NULL_TREE);
9941
9942 return build_tree_list (default_value, parm_decl);
9943 }
9944
9945 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9946 type T. If T is not an aggregate or enumeration type, it is
9947 handled as if by tsubst. IN_DECL is as for tsubst. If
9948 ENTERING_SCOPE is nonzero, T is the context for a template which
9949 we are presently tsubst'ing. Return the substituted value. */
9950
9951 static tree
9952 tsubst_aggr_type (tree t,
9953 tree args,
9954 tsubst_flags_t complain,
9955 tree in_decl,
9956 int entering_scope)
9957 {
9958 if (t == NULL_TREE)
9959 return NULL_TREE;
9960
9961 switch (TREE_CODE (t))
9962 {
9963 case RECORD_TYPE:
9964 if (TYPE_PTRMEMFUNC_P (t))
9965 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9966
9967 /* Else fall through. */
9968 case ENUMERAL_TYPE:
9969 case UNION_TYPE:
9970 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9971 {
9972 tree argvec;
9973 tree context;
9974 tree r;
9975 int saved_unevaluated_operand;
9976 int saved_inhibit_evaluation_warnings;
9977
9978 /* In "sizeof(X<I>)" we need to evaluate "I". */
9979 saved_unevaluated_operand = cp_unevaluated_operand;
9980 cp_unevaluated_operand = 0;
9981 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9982 c_inhibit_evaluation_warnings = 0;
9983
9984 /* First, determine the context for the type we are looking
9985 up. */
9986 context = TYPE_CONTEXT (t);
9987 if (context && TYPE_P (context))
9988 {
9989 context = tsubst_aggr_type (context, args, complain,
9990 in_decl, /*entering_scope=*/1);
9991 /* If context is a nested class inside a class template,
9992 it may still need to be instantiated (c++/33959). */
9993 context = complete_type (context);
9994 }
9995
9996 /* Then, figure out what arguments are appropriate for the
9997 type we are trying to find. For example, given:
9998
9999 template <class T> struct S;
10000 template <class T, class U> void f(T, U) { S<U> su; }
10001
10002 and supposing that we are instantiating f<int, double>,
10003 then our ARGS will be {int, double}, but, when looking up
10004 S we only want {double}. */
10005 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10006 complain, in_decl);
10007 if (argvec == error_mark_node)
10008 r = error_mark_node;
10009 else
10010 {
10011 r = lookup_template_class (t, argvec, in_decl, context,
10012 entering_scope, complain);
10013 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10014 }
10015
10016 cp_unevaluated_operand = saved_unevaluated_operand;
10017 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10018
10019 return r;
10020 }
10021 else
10022 /* This is not a template type, so there's nothing to do. */
10023 return t;
10024
10025 default:
10026 return tsubst (t, args, complain, in_decl);
10027 }
10028 }
10029
10030 /* Substitute into the default argument ARG (a default argument for
10031 FN), which has the indicated TYPE. */
10032
10033 tree
10034 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10035 {
10036 tree saved_class_ptr = NULL_TREE;
10037 tree saved_class_ref = NULL_TREE;
10038 int errs = errorcount + sorrycount;
10039
10040 /* This can happen in invalid code. */
10041 if (TREE_CODE (arg) == DEFAULT_ARG)
10042 return arg;
10043
10044 /* This default argument came from a template. Instantiate the
10045 default argument here, not in tsubst. In the case of
10046 something like:
10047
10048 template <class T>
10049 struct S {
10050 static T t();
10051 void f(T = t());
10052 };
10053
10054 we must be careful to do name lookup in the scope of S<T>,
10055 rather than in the current class. */
10056 push_access_scope (fn);
10057 /* The "this" pointer is not valid in a default argument. */
10058 if (cfun)
10059 {
10060 saved_class_ptr = current_class_ptr;
10061 cp_function_chain->x_current_class_ptr = NULL_TREE;
10062 saved_class_ref = current_class_ref;
10063 cp_function_chain->x_current_class_ref = NULL_TREE;
10064 }
10065
10066 push_deferring_access_checks(dk_no_deferred);
10067 /* The default argument expression may cause implicitly defined
10068 member functions to be synthesized, which will result in garbage
10069 collection. We must treat this situation as if we were within
10070 the body of function so as to avoid collecting live data on the
10071 stack. */
10072 ++function_depth;
10073 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10074 complain, NULL_TREE,
10075 /*integral_constant_expression_p=*/false);
10076 --function_depth;
10077 pop_deferring_access_checks();
10078
10079 /* Restore the "this" pointer. */
10080 if (cfun)
10081 {
10082 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10083 cp_function_chain->x_current_class_ref = saved_class_ref;
10084 }
10085
10086 if (errorcount+sorrycount > errs
10087 && (complain & tf_warning_or_error))
10088 inform (input_location,
10089 " when instantiating default argument for call to %D", fn);
10090
10091 /* Make sure the default argument is reasonable. */
10092 arg = check_default_argument (type, arg, complain);
10093
10094 pop_access_scope (fn);
10095
10096 return arg;
10097 }
10098
10099 /* Substitute into all the default arguments for FN. */
10100
10101 static void
10102 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10103 {
10104 tree arg;
10105 tree tmpl_args;
10106
10107 tmpl_args = DECL_TI_ARGS (fn);
10108
10109 /* If this function is not yet instantiated, we certainly don't need
10110 its default arguments. */
10111 if (uses_template_parms (tmpl_args))
10112 return;
10113 /* Don't do this again for clones. */
10114 if (DECL_CLONED_FUNCTION_P (fn))
10115 return;
10116
10117 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10118 arg;
10119 arg = TREE_CHAIN (arg))
10120 if (TREE_PURPOSE (arg))
10121 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10122 TREE_VALUE (arg),
10123 TREE_PURPOSE (arg),
10124 complain);
10125 }
10126
10127 /* Substitute the ARGS into the T, which is a _DECL. Return the
10128 result of the substitution. Issue error and warning messages under
10129 control of COMPLAIN. */
10130
10131 static tree
10132 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10133 {
10134 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10135 location_t saved_loc;
10136 tree r = NULL_TREE;
10137 tree in_decl = t;
10138 hashval_t hash = 0;
10139
10140 /* Set the filename and linenumber to improve error-reporting. */
10141 saved_loc = input_location;
10142 input_location = DECL_SOURCE_LOCATION (t);
10143
10144 switch (TREE_CODE (t))
10145 {
10146 case TEMPLATE_DECL:
10147 {
10148 /* We can get here when processing a member function template,
10149 member class template, or template template parameter. */
10150 tree decl = DECL_TEMPLATE_RESULT (t);
10151 tree spec;
10152 tree tmpl_args;
10153 tree full_args;
10154
10155 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10156 {
10157 /* Template template parameter is treated here. */
10158 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10159 if (new_type == error_mark_node)
10160 RETURN (error_mark_node);
10161 /* If we get a real template back, return it. This can happen in
10162 the context of most_specialized_class. */
10163 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10164 return new_type;
10165
10166 r = copy_decl (t);
10167 DECL_CHAIN (r) = NULL_TREE;
10168 TREE_TYPE (r) = new_type;
10169 DECL_TEMPLATE_RESULT (r)
10170 = build_decl (DECL_SOURCE_LOCATION (decl),
10171 TYPE_DECL, DECL_NAME (decl), new_type);
10172 DECL_TEMPLATE_PARMS (r)
10173 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10174 complain);
10175 TYPE_NAME (new_type) = r;
10176 break;
10177 }
10178
10179 /* We might already have an instance of this template.
10180 The ARGS are for the surrounding class type, so the
10181 full args contain the tsubst'd args for the context,
10182 plus the innermost args from the template decl. */
10183 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10184 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10185 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10186 /* Because this is a template, the arguments will still be
10187 dependent, even after substitution. If
10188 PROCESSING_TEMPLATE_DECL is not set, the dependency
10189 predicates will short-circuit. */
10190 ++processing_template_decl;
10191 full_args = tsubst_template_args (tmpl_args, args,
10192 complain, in_decl);
10193 --processing_template_decl;
10194 if (full_args == error_mark_node)
10195 RETURN (error_mark_node);
10196
10197 /* If this is a default template template argument,
10198 tsubst might not have changed anything. */
10199 if (full_args == tmpl_args)
10200 RETURN (t);
10201
10202 hash = hash_tmpl_and_args (t, full_args);
10203 spec = retrieve_specialization (t, full_args, hash);
10204 if (spec != NULL_TREE)
10205 {
10206 r = spec;
10207 break;
10208 }
10209
10210 /* Make a new template decl. It will be similar to the
10211 original, but will record the current template arguments.
10212 We also create a new function declaration, which is just
10213 like the old one, but points to this new template, rather
10214 than the old one. */
10215 r = copy_decl (t);
10216 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10217 DECL_CHAIN (r) = NULL_TREE;
10218
10219 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10220
10221 if (TREE_CODE (decl) == TYPE_DECL
10222 && !TYPE_DECL_ALIAS_P (decl))
10223 {
10224 tree new_type;
10225 ++processing_template_decl;
10226 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10227 --processing_template_decl;
10228 if (new_type == error_mark_node)
10229 RETURN (error_mark_node);
10230
10231 TREE_TYPE (r) = new_type;
10232 /* For a partial specialization, we need to keep pointing to
10233 the primary template. */
10234 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10235 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10236 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10237 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10238 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10239 }
10240 else
10241 {
10242 tree new_decl;
10243 ++processing_template_decl;
10244 new_decl = tsubst (decl, args, complain, in_decl);
10245 --processing_template_decl;
10246 if (new_decl == error_mark_node)
10247 RETURN (error_mark_node);
10248
10249 DECL_TEMPLATE_RESULT (r) = new_decl;
10250 DECL_TI_TEMPLATE (new_decl) = r;
10251 TREE_TYPE (r) = TREE_TYPE (new_decl);
10252 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10253 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10254 }
10255
10256 SET_DECL_IMPLICIT_INSTANTIATION (r);
10257 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10258 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10259
10260 /* The template parameters for this new template are all the
10261 template parameters for the old template, except the
10262 outermost level of parameters. */
10263 DECL_TEMPLATE_PARMS (r)
10264 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10265 complain);
10266
10267 if (PRIMARY_TEMPLATE_P (t))
10268 DECL_PRIMARY_TEMPLATE (r) = r;
10269
10270 if (TREE_CODE (decl) != TYPE_DECL)
10271 /* Record this non-type partial instantiation. */
10272 register_specialization (r, t,
10273 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10274 false, hash);
10275 }
10276 break;
10277
10278 case FUNCTION_DECL:
10279 {
10280 tree ctx;
10281 tree argvec = NULL_TREE;
10282 tree *friends;
10283 tree gen_tmpl;
10284 tree type;
10285 int member;
10286 int args_depth;
10287 int parms_depth;
10288
10289 /* Nobody should be tsubst'ing into non-template functions. */
10290 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10291
10292 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10293 {
10294 tree spec;
10295 bool dependent_p;
10296
10297 /* If T is not dependent, just return it. We have to
10298 increment PROCESSING_TEMPLATE_DECL because
10299 value_dependent_expression_p assumes that nothing is
10300 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10301 ++processing_template_decl;
10302 dependent_p = value_dependent_expression_p (t);
10303 --processing_template_decl;
10304 if (!dependent_p)
10305 RETURN (t);
10306
10307 /* Calculate the most general template of which R is a
10308 specialization, and the complete set of arguments used to
10309 specialize R. */
10310 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10311 argvec = tsubst_template_args (DECL_TI_ARGS
10312 (DECL_TEMPLATE_RESULT
10313 (DECL_TI_TEMPLATE (t))),
10314 args, complain, in_decl);
10315 if (argvec == error_mark_node)
10316 RETURN (error_mark_node);
10317
10318 /* Check to see if we already have this specialization. */
10319 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10320 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10321
10322 if (spec)
10323 {
10324 r = spec;
10325 break;
10326 }
10327
10328 /* We can see more levels of arguments than parameters if
10329 there was a specialization of a member template, like
10330 this:
10331
10332 template <class T> struct S { template <class U> void f(); }
10333 template <> template <class U> void S<int>::f(U);
10334
10335 Here, we'll be substituting into the specialization,
10336 because that's where we can find the code we actually
10337 want to generate, but we'll have enough arguments for
10338 the most general template.
10339
10340 We also deal with the peculiar case:
10341
10342 template <class T> struct S {
10343 template <class U> friend void f();
10344 };
10345 template <class U> void f() {}
10346 template S<int>;
10347 template void f<double>();
10348
10349 Here, the ARGS for the instantiation of will be {int,
10350 double}. But, we only need as many ARGS as there are
10351 levels of template parameters in CODE_PATTERN. We are
10352 careful not to get fooled into reducing the ARGS in
10353 situations like:
10354
10355 template <class T> struct S { template <class U> void f(U); }
10356 template <class T> template <> void S<T>::f(int) {}
10357
10358 which we can spot because the pattern will be a
10359 specialization in this case. */
10360 args_depth = TMPL_ARGS_DEPTH (args);
10361 parms_depth =
10362 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10363 if (args_depth > parms_depth
10364 && !DECL_TEMPLATE_SPECIALIZATION (t))
10365 args = get_innermost_template_args (args, parms_depth);
10366 }
10367 else
10368 {
10369 /* This special case arises when we have something like this:
10370
10371 template <class T> struct S {
10372 friend void f<int>(int, double);
10373 };
10374
10375 Here, the DECL_TI_TEMPLATE for the friend declaration
10376 will be an IDENTIFIER_NODE. We are being called from
10377 tsubst_friend_function, and we want only to create a
10378 new decl (R) with appropriate types so that we can call
10379 determine_specialization. */
10380 gen_tmpl = NULL_TREE;
10381 }
10382
10383 if (DECL_CLASS_SCOPE_P (t))
10384 {
10385 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10386 member = 2;
10387 else
10388 member = 1;
10389 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10390 complain, t, /*entering_scope=*/1);
10391 }
10392 else
10393 {
10394 member = 0;
10395 ctx = DECL_CONTEXT (t);
10396 }
10397 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10398 if (type == error_mark_node)
10399 RETURN (error_mark_node);
10400
10401 /* If we hit excessive deduction depth, the type is bogus even if
10402 it isn't error_mark_node, so don't build a decl. */
10403 if (excessive_deduction_depth)
10404 RETURN (error_mark_node);
10405
10406 /* We do NOT check for matching decls pushed separately at this
10407 point, as they may not represent instantiations of this
10408 template, and in any case are considered separate under the
10409 discrete model. */
10410 r = copy_decl (t);
10411 DECL_USE_TEMPLATE (r) = 0;
10412 TREE_TYPE (r) = type;
10413 /* Clear out the mangled name and RTL for the instantiation. */
10414 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10415 SET_DECL_RTL (r, NULL);
10416 /* Leave DECL_INITIAL set on deleted instantiations. */
10417 if (!DECL_DELETED_FN (r))
10418 DECL_INITIAL (r) = NULL_TREE;
10419 DECL_CONTEXT (r) = ctx;
10420
10421 /* OpenMP UDRs have the only argument a reference to the declared
10422 type. We want to diagnose if the declared type is a reference,
10423 which is invalid, but as references to references are usually
10424 quietly merged, diagnose it here. */
10425 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10426 {
10427 tree argtype
10428 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10429 argtype = tsubst (argtype, args, complain, in_decl);
10430 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10431 error_at (DECL_SOURCE_LOCATION (t),
10432 "reference type %qT in "
10433 "%<#pragma omp declare reduction%>", argtype);
10434 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10435 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10436 argtype);
10437 }
10438
10439 if (member && DECL_CONV_FN_P (r))
10440 /* Type-conversion operator. Reconstruct the name, in
10441 case it's the name of one of the template's parameters. */
10442 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10443
10444 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10445 complain, t);
10446 DECL_RESULT (r) = NULL_TREE;
10447
10448 TREE_STATIC (r) = 0;
10449 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10450 DECL_EXTERNAL (r) = 1;
10451 /* If this is an instantiation of a function with internal
10452 linkage, we already know what object file linkage will be
10453 assigned to the instantiation. */
10454 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10455 DECL_DEFER_OUTPUT (r) = 0;
10456 DECL_CHAIN (r) = NULL_TREE;
10457 DECL_PENDING_INLINE_INFO (r) = 0;
10458 DECL_PENDING_INLINE_P (r) = 0;
10459 DECL_SAVED_TREE (r) = NULL_TREE;
10460 DECL_STRUCT_FUNCTION (r) = NULL;
10461 TREE_USED (r) = 0;
10462 /* We'll re-clone as appropriate in instantiate_template. */
10463 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10464
10465 /* If we aren't complaining now, return on error before we register
10466 the specialization so that we'll complain eventually. */
10467 if ((complain & tf_error) == 0
10468 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10469 && !grok_op_properties (r, /*complain=*/false))
10470 RETURN (error_mark_node);
10471
10472 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10473 this in the special friend case mentioned above where
10474 GEN_TMPL is NULL. */
10475 if (gen_tmpl)
10476 {
10477 DECL_TEMPLATE_INFO (r)
10478 = build_template_info (gen_tmpl, argvec);
10479 SET_DECL_IMPLICIT_INSTANTIATION (r);
10480
10481 tree new_r
10482 = register_specialization (r, gen_tmpl, argvec, false, hash);
10483 if (new_r != r)
10484 /* We instantiated this while substituting into
10485 the type earlier (template/friend54.C). */
10486 RETURN (new_r);
10487
10488 /* We're not supposed to instantiate default arguments
10489 until they are called, for a template. But, for a
10490 declaration like:
10491
10492 template <class T> void f ()
10493 { extern void g(int i = T()); }
10494
10495 we should do the substitution when the template is
10496 instantiated. We handle the member function case in
10497 instantiate_class_template since the default arguments
10498 might refer to other members of the class. */
10499 if (!member
10500 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10501 && !uses_template_parms (argvec))
10502 tsubst_default_arguments (r, complain);
10503 }
10504 else
10505 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10506
10507 /* Copy the list of befriending classes. */
10508 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10509 *friends;
10510 friends = &TREE_CHAIN (*friends))
10511 {
10512 *friends = copy_node (*friends);
10513 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10514 args, complain,
10515 in_decl);
10516 }
10517
10518 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10519 {
10520 maybe_retrofit_in_chrg (r);
10521 if (DECL_CONSTRUCTOR_P (r))
10522 grok_ctor_properties (ctx, r);
10523 if (DECL_INHERITED_CTOR_BASE (r))
10524 deduce_inheriting_ctor (r);
10525 /* If this is an instantiation of a member template, clone it.
10526 If it isn't, that'll be handled by
10527 clone_constructors_and_destructors. */
10528 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10529 clone_function_decl (r, /*update_method_vec_p=*/0);
10530 }
10531 else if ((complain & tf_error) != 0
10532 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10533 && !grok_op_properties (r, /*complain=*/true))
10534 RETURN (error_mark_node);
10535
10536 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10537 SET_DECL_FRIEND_CONTEXT (r,
10538 tsubst (DECL_FRIEND_CONTEXT (t),
10539 args, complain, in_decl));
10540
10541 /* Possibly limit visibility based on template args. */
10542 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10543 if (DECL_VISIBILITY_SPECIFIED (t))
10544 {
10545 DECL_VISIBILITY_SPECIFIED (r) = 0;
10546 DECL_ATTRIBUTES (r)
10547 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10548 }
10549 determine_visibility (r);
10550 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10551 && !processing_template_decl)
10552 defaulted_late_check (r);
10553
10554 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10555 args, complain, in_decl);
10556 }
10557 break;
10558
10559 case PARM_DECL:
10560 {
10561 tree type = NULL_TREE;
10562 int i, len = 1;
10563 tree expanded_types = NULL_TREE;
10564 tree prev_r = NULL_TREE;
10565 tree first_r = NULL_TREE;
10566
10567 if (DECL_PACK_P (t))
10568 {
10569 /* If there is a local specialization that isn't a
10570 parameter pack, it means that we're doing a "simple"
10571 substitution from inside tsubst_pack_expansion. Just
10572 return the local specialization (which will be a single
10573 parm). */
10574 tree spec = retrieve_local_specialization (t);
10575 if (spec
10576 && TREE_CODE (spec) == PARM_DECL
10577 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10578 RETURN (spec);
10579
10580 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10581 the parameters in this function parameter pack. */
10582 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10583 complain, in_decl);
10584 if (TREE_CODE (expanded_types) == TREE_VEC)
10585 {
10586 len = TREE_VEC_LENGTH (expanded_types);
10587
10588 /* Zero-length parameter packs are boring. Just substitute
10589 into the chain. */
10590 if (len == 0)
10591 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10592 TREE_CHAIN (t)));
10593 }
10594 else
10595 {
10596 /* All we did was update the type. Make a note of that. */
10597 type = expanded_types;
10598 expanded_types = NULL_TREE;
10599 }
10600 }
10601
10602 /* Loop through all of the parameters we'll build. When T is
10603 a function parameter pack, LEN is the number of expanded
10604 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10605 r = NULL_TREE;
10606 for (i = 0; i < len; ++i)
10607 {
10608 prev_r = r;
10609 r = copy_node (t);
10610 if (DECL_TEMPLATE_PARM_P (t))
10611 SET_DECL_TEMPLATE_PARM_P (r);
10612
10613 if (expanded_types)
10614 /* We're on the Ith parameter of the function parameter
10615 pack. */
10616 {
10617 /* Get the Ith type. */
10618 type = TREE_VEC_ELT (expanded_types, i);
10619
10620 /* Rename the parameter to include the index. */
10621 DECL_NAME (r)
10622 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10623 }
10624 else if (!type)
10625 /* We're dealing with a normal parameter. */
10626 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10627
10628 type = type_decays_to (type);
10629 TREE_TYPE (r) = type;
10630 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10631
10632 if (DECL_INITIAL (r))
10633 {
10634 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10635 DECL_INITIAL (r) = TREE_TYPE (r);
10636 else
10637 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10638 complain, in_decl);
10639 }
10640
10641 DECL_CONTEXT (r) = NULL_TREE;
10642
10643 if (!DECL_TEMPLATE_PARM_P (r))
10644 DECL_ARG_TYPE (r) = type_passed_as (type);
10645
10646 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10647 args, complain, in_decl);
10648
10649 /* Keep track of the first new parameter we
10650 generate. That's what will be returned to the
10651 caller. */
10652 if (!first_r)
10653 first_r = r;
10654
10655 /* Build a proper chain of parameters when substituting
10656 into a function parameter pack. */
10657 if (prev_r)
10658 DECL_CHAIN (prev_r) = r;
10659 }
10660
10661 /* If cp_unevaluated_operand is set, we're just looking for a
10662 single dummy parameter, so don't keep going. */
10663 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10664 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10665 complain, DECL_CHAIN (t));
10666
10667 /* FIRST_R contains the start of the chain we've built. */
10668 r = first_r;
10669 }
10670 break;
10671
10672 case FIELD_DECL:
10673 {
10674 tree type = NULL_TREE;
10675 tree vec = NULL_TREE;
10676 tree expanded_types = NULL_TREE;
10677 int len = 1;
10678
10679 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10680 {
10681 /* This field is a lambda capture pack. Return a TREE_VEC of
10682 the expanded fields to instantiate_class_template_1 and
10683 store them in the specializations hash table as a
10684 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10685 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10686 complain, in_decl);
10687 if (TREE_CODE (expanded_types) == TREE_VEC)
10688 {
10689 len = TREE_VEC_LENGTH (expanded_types);
10690 vec = make_tree_vec (len);
10691 }
10692 else
10693 {
10694 /* All we did was update the type. Make a note of that. */
10695 type = expanded_types;
10696 expanded_types = NULL_TREE;
10697 }
10698 }
10699
10700 for (int i = 0; i < len; ++i)
10701 {
10702 r = copy_decl (t);
10703 if (expanded_types)
10704 {
10705 type = TREE_VEC_ELT (expanded_types, i);
10706 DECL_NAME (r)
10707 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10708 }
10709 else if (!type)
10710 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10711
10712 if (type == error_mark_node)
10713 RETURN (error_mark_node);
10714 TREE_TYPE (r) = type;
10715 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10716
10717 if (DECL_C_BIT_FIELD (r))
10718 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10719 non-bit-fields DECL_INITIAL is a non-static data member
10720 initializer, which gets deferred instantiation. */
10721 DECL_INITIAL (r)
10722 = tsubst_expr (DECL_INITIAL (t), args,
10723 complain, in_decl,
10724 /*integral_constant_expression_p=*/true);
10725 else if (DECL_INITIAL (t))
10726 {
10727 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10728 NSDMI in perform_member_init. Still set DECL_INITIAL
10729 so that we know there is one. */
10730 DECL_INITIAL (r) = void_zero_node;
10731 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10732 retrofit_lang_decl (r);
10733 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10734 }
10735 /* We don't have to set DECL_CONTEXT here; it is set by
10736 finish_member_declaration. */
10737 DECL_CHAIN (r) = NULL_TREE;
10738
10739 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10740 args, complain, in_decl);
10741
10742 if (vec)
10743 TREE_VEC_ELT (vec, i) = r;
10744 }
10745
10746 if (vec)
10747 {
10748 r = vec;
10749 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10750 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10751 SET_ARGUMENT_PACK_ARGS (pack, vec);
10752 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10753 TREE_TYPE (pack) = tpack;
10754 register_specialization (pack, t, args, false, 0);
10755 }
10756 }
10757 break;
10758
10759 case USING_DECL:
10760 /* We reach here only for member using decls. We also need to check
10761 uses_template_parms because DECL_DEPENDENT_P is not set for a
10762 using-declaration that designates a member of the current
10763 instantiation (c++/53549). */
10764 if (DECL_DEPENDENT_P (t)
10765 || uses_template_parms (USING_DECL_SCOPE (t)))
10766 {
10767 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10768 complain, in_decl);
10769 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10770 r = do_class_using_decl (inst_scope, name);
10771 if (!r)
10772 r = error_mark_node;
10773 else
10774 {
10775 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10776 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10777 }
10778 }
10779 else
10780 {
10781 r = copy_node (t);
10782 DECL_CHAIN (r) = NULL_TREE;
10783 }
10784 break;
10785
10786 case TYPE_DECL:
10787 case VAR_DECL:
10788 {
10789 tree argvec = NULL_TREE;
10790 tree gen_tmpl = NULL_TREE;
10791 tree spec;
10792 tree tmpl = NULL_TREE;
10793 tree ctx;
10794 tree type = NULL_TREE;
10795 bool local_p;
10796
10797 if (TREE_CODE (t) == TYPE_DECL
10798 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10799 {
10800 /* If this is the canonical decl, we don't have to
10801 mess with instantiations, and often we can't (for
10802 typename, template type parms and such). Note that
10803 TYPE_NAME is not correct for the above test if
10804 we've copied the type for a typedef. */
10805 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10806 if (type == error_mark_node)
10807 RETURN (error_mark_node);
10808 r = TYPE_NAME (type);
10809 break;
10810 }
10811
10812 /* Check to see if we already have the specialization we
10813 need. */
10814 spec = NULL_TREE;
10815 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10816 {
10817 /* T is a static data member or namespace-scope entity.
10818 We have to substitute into namespace-scope variables
10819 (even though such entities are never templates) because
10820 of cases like:
10821
10822 template <class T> void f() { extern T t; }
10823
10824 where the entity referenced is not known until
10825 instantiation time. */
10826 local_p = false;
10827 ctx = DECL_CONTEXT (t);
10828 if (DECL_CLASS_SCOPE_P (t))
10829 {
10830 ctx = tsubst_aggr_type (ctx, args,
10831 complain,
10832 in_decl, /*entering_scope=*/1);
10833 /* If CTX is unchanged, then T is in fact the
10834 specialization we want. That situation occurs when
10835 referencing a static data member within in its own
10836 class. We can use pointer equality, rather than
10837 same_type_p, because DECL_CONTEXT is always
10838 canonical... */
10839 if (ctx == DECL_CONTEXT (t)
10840 && (TREE_CODE (t) != TYPE_DECL
10841 /* ... unless T is a member template; in which
10842 case our caller can be willing to create a
10843 specialization of that template represented
10844 by T. */
10845 || !(DECL_TI_TEMPLATE (t)
10846 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10847 spec = t;
10848 }
10849
10850 if (!spec)
10851 {
10852 tmpl = DECL_TI_TEMPLATE (t);
10853 gen_tmpl = most_general_template (tmpl);
10854 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10855 if (argvec == error_mark_node)
10856 RETURN (error_mark_node);
10857 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10858 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10859 }
10860 }
10861 else
10862 {
10863 /* A local variable. */
10864 local_p = true;
10865 /* Subsequent calls to pushdecl will fill this in. */
10866 ctx = NULL_TREE;
10867 spec = retrieve_local_specialization (t);
10868 }
10869 /* If we already have the specialization we need, there is
10870 nothing more to do. */
10871 if (spec)
10872 {
10873 r = spec;
10874 break;
10875 }
10876
10877 /* Create a new node for the specialization we need. */
10878 r = copy_decl (t);
10879 if (type == NULL_TREE)
10880 {
10881 if (is_typedef_decl (t))
10882 type = DECL_ORIGINAL_TYPE (t);
10883 else
10884 type = TREE_TYPE (t);
10885 if (VAR_P (t)
10886 && VAR_HAD_UNKNOWN_BOUND (t)
10887 && type != error_mark_node)
10888 type = strip_array_domain (type);
10889 type = tsubst (type, args, complain, in_decl);
10890 }
10891 if (VAR_P (r))
10892 {
10893 /* Even if the original location is out of scope, the
10894 newly substituted one is not. */
10895 DECL_DEAD_FOR_LOCAL (r) = 0;
10896 DECL_INITIALIZED_P (r) = 0;
10897 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10898 if (type == error_mark_node)
10899 RETURN (error_mark_node);
10900 if (TREE_CODE (type) == FUNCTION_TYPE)
10901 {
10902 /* It may seem that this case cannot occur, since:
10903
10904 typedef void f();
10905 void g() { f x; }
10906
10907 declares a function, not a variable. However:
10908
10909 typedef void f();
10910 template <typename T> void g() { T t; }
10911 template void g<f>();
10912
10913 is an attempt to declare a variable with function
10914 type. */
10915 error ("variable %qD has function type",
10916 /* R is not yet sufficiently initialized, so we
10917 just use its name. */
10918 DECL_NAME (r));
10919 RETURN (error_mark_node);
10920 }
10921 type = complete_type (type);
10922 /* Wait until cp_finish_decl to set this again, to handle
10923 circular dependency (template/instantiate6.C). */
10924 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10925 type = check_var_type (DECL_NAME (r), type);
10926
10927 if (DECL_HAS_VALUE_EXPR_P (t))
10928 {
10929 tree ve = DECL_VALUE_EXPR (t);
10930 ve = tsubst_expr (ve, args, complain, in_decl,
10931 /*constant_expression_p=*/false);
10932 if (REFERENCE_REF_P (ve))
10933 {
10934 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10935 ve = TREE_OPERAND (ve, 0);
10936 }
10937 SET_DECL_VALUE_EXPR (r, ve);
10938 }
10939 }
10940 else if (DECL_SELF_REFERENCE_P (t))
10941 SET_DECL_SELF_REFERENCE_P (r);
10942 TREE_TYPE (r) = type;
10943 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10944 DECL_CONTEXT (r) = ctx;
10945 /* Clear out the mangled name and RTL for the instantiation. */
10946 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10947 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10948 SET_DECL_RTL (r, NULL);
10949 /* The initializer must not be expanded until it is required;
10950 see [temp.inst]. */
10951 DECL_INITIAL (r) = NULL_TREE;
10952 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10953 SET_DECL_RTL (r, NULL);
10954 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10955 if (VAR_P (r))
10956 {
10957 /* Possibly limit visibility based on template args. */
10958 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10959 if (DECL_VISIBILITY_SPECIFIED (t))
10960 {
10961 DECL_VISIBILITY_SPECIFIED (r) = 0;
10962 DECL_ATTRIBUTES (r)
10963 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10964 }
10965 determine_visibility (r);
10966 }
10967
10968 if (!local_p)
10969 {
10970 /* A static data member declaration is always marked
10971 external when it is declared in-class, even if an
10972 initializer is present. We mimic the non-template
10973 processing here. */
10974 DECL_EXTERNAL (r) = 1;
10975
10976 register_specialization (r, gen_tmpl, argvec, false, hash);
10977 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10978 SET_DECL_IMPLICIT_INSTANTIATION (r);
10979 }
10980 else if (cp_unevaluated_operand)
10981 gcc_unreachable ();
10982 else
10983 register_local_specialization (r, t);
10984
10985 DECL_CHAIN (r) = NULL_TREE;
10986
10987 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10988 /*flags=*/0,
10989 args, complain, in_decl);
10990
10991 /* Preserve a typedef that names a type. */
10992 if (is_typedef_decl (r))
10993 {
10994 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10995 set_underlying_type (r);
10996 }
10997
10998 layout_decl (r, 0);
10999 }
11000 break;
11001
11002 default:
11003 gcc_unreachable ();
11004 }
11005 #undef RETURN
11006
11007 out:
11008 /* Restore the file and line information. */
11009 input_location = saved_loc;
11010
11011 return r;
11012 }
11013
11014 /* Substitute into the ARG_TYPES of a function type.
11015 If END is a TREE_CHAIN, leave it and any following types
11016 un-substituted. */
11017
11018 static tree
11019 tsubst_arg_types (tree arg_types,
11020 tree args,
11021 tree end,
11022 tsubst_flags_t complain,
11023 tree in_decl)
11024 {
11025 tree remaining_arg_types;
11026 tree type = NULL_TREE;
11027 int i = 1;
11028 tree expanded_args = NULL_TREE;
11029 tree default_arg;
11030
11031 if (!arg_types || arg_types == void_list_node || arg_types == end)
11032 return arg_types;
11033
11034 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11035 args, end, complain, in_decl);
11036 if (remaining_arg_types == error_mark_node)
11037 return error_mark_node;
11038
11039 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11040 {
11041 /* For a pack expansion, perform substitution on the
11042 entire expression. Later on, we'll handle the arguments
11043 one-by-one. */
11044 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11045 args, complain, in_decl);
11046
11047 if (TREE_CODE (expanded_args) == TREE_VEC)
11048 /* So that we'll spin through the parameters, one by one. */
11049 i = TREE_VEC_LENGTH (expanded_args);
11050 else
11051 {
11052 /* We only partially substituted into the parameter
11053 pack. Our type is TYPE_PACK_EXPANSION. */
11054 type = expanded_args;
11055 expanded_args = NULL_TREE;
11056 }
11057 }
11058
11059 while (i > 0) {
11060 --i;
11061
11062 if (expanded_args)
11063 type = TREE_VEC_ELT (expanded_args, i);
11064 else if (!type)
11065 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11066
11067 if (type == error_mark_node)
11068 return error_mark_node;
11069 if (VOID_TYPE_P (type))
11070 {
11071 if (complain & tf_error)
11072 {
11073 error ("invalid parameter type %qT", type);
11074 if (in_decl)
11075 error ("in declaration %q+D", in_decl);
11076 }
11077 return error_mark_node;
11078 }
11079 /* DR 657. */
11080 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11081 return error_mark_node;
11082
11083 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11084 top-level qualifiers as required. */
11085 type = cv_unqualified (type_decays_to (type));
11086
11087 /* We do not substitute into default arguments here. The standard
11088 mandates that they be instantiated only when needed, which is
11089 done in build_over_call. */
11090 default_arg = TREE_PURPOSE (arg_types);
11091
11092 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11093 {
11094 /* We've instantiated a template before its default arguments
11095 have been parsed. This can happen for a nested template
11096 class, and is not an error unless we require the default
11097 argument in a call of this function. */
11098 remaining_arg_types =
11099 tree_cons (default_arg, type, remaining_arg_types);
11100 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11101 }
11102 else
11103 remaining_arg_types =
11104 hash_tree_cons (default_arg, type, remaining_arg_types);
11105 }
11106
11107 return remaining_arg_types;
11108 }
11109
11110 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11111 *not* handle the exception-specification for FNTYPE, because the
11112 initial substitution of explicitly provided template parameters
11113 during argument deduction forbids substitution into the
11114 exception-specification:
11115
11116 [temp.deduct]
11117
11118 All references in the function type of the function template to the
11119 corresponding template parameters are replaced by the specified tem-
11120 plate argument values. If a substitution in a template parameter or
11121 in the function type of the function template results in an invalid
11122 type, type deduction fails. [Note: The equivalent substitution in
11123 exception specifications is done only when the function is instanti-
11124 ated, at which point a program is ill-formed if the substitution
11125 results in an invalid type.] */
11126
11127 static tree
11128 tsubst_function_type (tree t,
11129 tree args,
11130 tsubst_flags_t complain,
11131 tree in_decl)
11132 {
11133 tree return_type;
11134 tree arg_types;
11135 tree fntype;
11136
11137 /* The TYPE_CONTEXT is not used for function/method types. */
11138 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11139
11140 /* Substitute the return type. */
11141 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11142 if (return_type == error_mark_node)
11143 return error_mark_node;
11144 /* DR 486 clarifies that creation of a function type with an
11145 invalid return type is a deduction failure. */
11146 if (TREE_CODE (return_type) == ARRAY_TYPE
11147 || TREE_CODE (return_type) == FUNCTION_TYPE)
11148 {
11149 if (complain & tf_error)
11150 {
11151 if (TREE_CODE (return_type) == ARRAY_TYPE)
11152 error ("function returning an array");
11153 else
11154 error ("function returning a function");
11155 }
11156 return error_mark_node;
11157 }
11158 /* And DR 657. */
11159 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11160 return error_mark_node;
11161
11162 /* Substitute the argument types. */
11163 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11164 complain, in_decl);
11165 if (arg_types == error_mark_node)
11166 return error_mark_node;
11167
11168 /* Construct a new type node and return it. */
11169 if (TREE_CODE (t) == FUNCTION_TYPE)
11170 {
11171 fntype = build_function_type (return_type, arg_types);
11172 fntype = apply_memfn_quals (fntype,
11173 type_memfn_quals (t),
11174 type_memfn_rqual (t));
11175 }
11176 else
11177 {
11178 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11179 if (! MAYBE_CLASS_TYPE_P (r))
11180 {
11181 /* [temp.deduct]
11182
11183 Type deduction may fail for any of the following
11184 reasons:
11185
11186 -- Attempting to create "pointer to member of T" when T
11187 is not a class type. */
11188 if (complain & tf_error)
11189 error ("creating pointer to member function of non-class type %qT",
11190 r);
11191 return error_mark_node;
11192 }
11193
11194 fntype = build_method_type_directly (r, return_type,
11195 TREE_CHAIN (arg_types));
11196 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11197 }
11198 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11199
11200 return fntype;
11201 }
11202
11203 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11204 ARGS into that specification, and return the substituted
11205 specification. If there is no specification, return NULL_TREE. */
11206
11207 static tree
11208 tsubst_exception_specification (tree fntype,
11209 tree args,
11210 tsubst_flags_t complain,
11211 tree in_decl,
11212 bool defer_ok)
11213 {
11214 tree specs;
11215 tree new_specs;
11216
11217 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11218 new_specs = NULL_TREE;
11219 if (specs && TREE_PURPOSE (specs))
11220 {
11221 /* A noexcept-specifier. */
11222 tree expr = TREE_PURPOSE (specs);
11223 if (TREE_CODE (expr) == INTEGER_CST)
11224 new_specs = expr;
11225 else if (defer_ok)
11226 {
11227 /* Defer instantiation of noexcept-specifiers to avoid
11228 excessive instantiations (c++/49107). */
11229 new_specs = make_node (DEFERRED_NOEXCEPT);
11230 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11231 {
11232 /* We already partially instantiated this member template,
11233 so combine the new args with the old. */
11234 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11235 = DEFERRED_NOEXCEPT_PATTERN (expr);
11236 DEFERRED_NOEXCEPT_ARGS (new_specs)
11237 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11238 }
11239 else
11240 {
11241 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11242 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11243 }
11244 }
11245 else
11246 new_specs = tsubst_copy_and_build
11247 (expr, args, complain, in_decl, /*function_p=*/false,
11248 /*integral_constant_expression_p=*/true);
11249 new_specs = build_noexcept_spec (new_specs, complain);
11250 }
11251 else if (specs)
11252 {
11253 if (! TREE_VALUE (specs))
11254 new_specs = specs;
11255 else
11256 while (specs)
11257 {
11258 tree spec;
11259 int i, len = 1;
11260 tree expanded_specs = NULL_TREE;
11261
11262 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11263 {
11264 /* Expand the pack expansion type. */
11265 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11266 args, complain,
11267 in_decl);
11268
11269 if (expanded_specs == error_mark_node)
11270 return error_mark_node;
11271 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11272 len = TREE_VEC_LENGTH (expanded_specs);
11273 else
11274 {
11275 /* We're substituting into a member template, so
11276 we got a TYPE_PACK_EXPANSION back. Add that
11277 expansion and move on. */
11278 gcc_assert (TREE_CODE (expanded_specs)
11279 == TYPE_PACK_EXPANSION);
11280 new_specs = add_exception_specifier (new_specs,
11281 expanded_specs,
11282 complain);
11283 specs = TREE_CHAIN (specs);
11284 continue;
11285 }
11286 }
11287
11288 for (i = 0; i < len; ++i)
11289 {
11290 if (expanded_specs)
11291 spec = TREE_VEC_ELT (expanded_specs, i);
11292 else
11293 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11294 if (spec == error_mark_node)
11295 return spec;
11296 new_specs = add_exception_specifier (new_specs, spec,
11297 complain);
11298 }
11299
11300 specs = TREE_CHAIN (specs);
11301 }
11302 }
11303 return new_specs;
11304 }
11305
11306 /* Take the tree structure T and replace template parameters used
11307 therein with the argument vector ARGS. IN_DECL is an associated
11308 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11309 Issue error and warning messages under control of COMPLAIN. Note
11310 that we must be relatively non-tolerant of extensions here, in
11311 order to preserve conformance; if we allow substitutions that
11312 should not be allowed, we may allow argument deductions that should
11313 not succeed, and therefore report ambiguous overload situations
11314 where there are none. In theory, we could allow the substitution,
11315 but indicate that it should have failed, and allow our caller to
11316 make sure that the right thing happens, but we don't try to do this
11317 yet.
11318
11319 This function is used for dealing with types, decls and the like;
11320 for expressions, use tsubst_expr or tsubst_copy. */
11321
11322 tree
11323 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11324 {
11325 enum tree_code code;
11326 tree type, r = NULL_TREE;
11327
11328 if (t == NULL_TREE || t == error_mark_node
11329 || t == integer_type_node
11330 || t == void_type_node
11331 || t == char_type_node
11332 || t == unknown_type_node
11333 || TREE_CODE (t) == NAMESPACE_DECL
11334 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11335 return t;
11336
11337 if (DECL_P (t))
11338 return tsubst_decl (t, args, complain);
11339
11340 if (args == NULL_TREE)
11341 return t;
11342
11343 code = TREE_CODE (t);
11344
11345 if (code == IDENTIFIER_NODE)
11346 type = IDENTIFIER_TYPE_VALUE (t);
11347 else
11348 type = TREE_TYPE (t);
11349
11350 gcc_assert (type != unknown_type_node);
11351
11352 /* Reuse typedefs. We need to do this to handle dependent attributes,
11353 such as attribute aligned. */
11354 if (TYPE_P (t)
11355 && typedef_variant_p (t))
11356 {
11357 tree decl = TYPE_NAME (t);
11358
11359 if (alias_template_specialization_p (t))
11360 {
11361 /* DECL represents an alias template and we want to
11362 instantiate it. */
11363 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11364 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11365 r = instantiate_alias_template (tmpl, gen_args, complain);
11366 }
11367 else if (DECL_CLASS_SCOPE_P (decl)
11368 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11369 && uses_template_parms (DECL_CONTEXT (decl)))
11370 {
11371 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11372 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11373 r = retrieve_specialization (tmpl, gen_args, 0);
11374 }
11375 else if (DECL_FUNCTION_SCOPE_P (decl)
11376 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11377 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11378 r = retrieve_local_specialization (decl);
11379 else
11380 /* The typedef is from a non-template context. */
11381 return t;
11382
11383 if (r)
11384 {
11385 r = TREE_TYPE (r);
11386 r = cp_build_qualified_type_real
11387 (r, cp_type_quals (t) | cp_type_quals (r),
11388 complain | tf_ignore_bad_quals);
11389 return r;
11390 }
11391 else
11392 {
11393 /* We don't have an instantiation yet, so drop the typedef. */
11394 int quals = cp_type_quals (t);
11395 t = DECL_ORIGINAL_TYPE (decl);
11396 t = cp_build_qualified_type_real (t, quals,
11397 complain | tf_ignore_bad_quals);
11398 }
11399 }
11400
11401 if (type
11402 && code != TYPENAME_TYPE
11403 && code != TEMPLATE_TYPE_PARM
11404 && code != IDENTIFIER_NODE
11405 && code != FUNCTION_TYPE
11406 && code != METHOD_TYPE)
11407 type = tsubst (type, args, complain, in_decl);
11408 if (type == error_mark_node)
11409 return error_mark_node;
11410
11411 switch (code)
11412 {
11413 case RECORD_TYPE:
11414 case UNION_TYPE:
11415 case ENUMERAL_TYPE:
11416 return tsubst_aggr_type (t, args, complain, in_decl,
11417 /*entering_scope=*/0);
11418
11419 case ERROR_MARK:
11420 case IDENTIFIER_NODE:
11421 case VOID_TYPE:
11422 case REAL_TYPE:
11423 case COMPLEX_TYPE:
11424 case VECTOR_TYPE:
11425 case BOOLEAN_TYPE:
11426 case NULLPTR_TYPE:
11427 case LANG_TYPE:
11428 return t;
11429
11430 case INTEGER_TYPE:
11431 if (t == integer_type_node)
11432 return t;
11433
11434 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11435 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11436 return t;
11437
11438 {
11439 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11440
11441 max = tsubst_expr (omax, args, complain, in_decl,
11442 /*integral_constant_expression_p=*/false);
11443
11444 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11445 needed. */
11446 if (TREE_CODE (max) == NOP_EXPR
11447 && TREE_SIDE_EFFECTS (omax)
11448 && !TREE_TYPE (max))
11449 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11450
11451 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11452 with TREE_SIDE_EFFECTS that indicates this is not an integral
11453 constant expression. */
11454 if (processing_template_decl
11455 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11456 {
11457 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11458 TREE_SIDE_EFFECTS (max) = 1;
11459 }
11460
11461 return compute_array_index_type (NULL_TREE, max, complain);
11462 }
11463
11464 case TEMPLATE_TYPE_PARM:
11465 case TEMPLATE_TEMPLATE_PARM:
11466 case BOUND_TEMPLATE_TEMPLATE_PARM:
11467 case TEMPLATE_PARM_INDEX:
11468 {
11469 int idx;
11470 int level;
11471 int levels;
11472 tree arg = NULL_TREE;
11473
11474 r = NULL_TREE;
11475
11476 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11477 template_parm_level_and_index (t, &level, &idx);
11478
11479 levels = TMPL_ARGS_DEPTH (args);
11480 if (level <= levels)
11481 {
11482 arg = TMPL_ARG (args, level, idx);
11483
11484 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11485 {
11486 /* See through ARGUMENT_PACK_SELECT arguments. */
11487 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11488 /* If the selected argument is an expansion E, that most
11489 likely means we were called from
11490 gen_elem_of_pack_expansion_instantiation during the
11491 substituting of pack an argument pack (which Ith
11492 element is a pack expansion, where I is
11493 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11494 In this case, the Ith element resulting from this
11495 substituting is going to be a pack expansion, which
11496 pattern is the pattern of E. Let's return the
11497 pattern of E, and
11498 gen_elem_of_pack_expansion_instantiation will
11499 build the resulting pack expansion from it. */
11500 if (PACK_EXPANSION_P (arg))
11501 arg = PACK_EXPANSION_PATTERN (arg);
11502 }
11503 }
11504
11505 if (arg == error_mark_node)
11506 return error_mark_node;
11507 else if (arg != NULL_TREE)
11508 {
11509 if (ARGUMENT_PACK_P (arg))
11510 /* If ARG is an argument pack, we don't actually want to
11511 perform a substitution here, because substitutions
11512 for argument packs are only done
11513 element-by-element. We can get to this point when
11514 substituting the type of a non-type template
11515 parameter pack, when that type actually contains
11516 template parameter packs from an outer template, e.g.,
11517
11518 template<typename... Types> struct A {
11519 template<Types... Values> struct B { };
11520 }; */
11521 return t;
11522
11523 if (code == TEMPLATE_TYPE_PARM)
11524 {
11525 int quals;
11526 gcc_assert (TYPE_P (arg));
11527
11528 quals = cp_type_quals (arg) | cp_type_quals (t);
11529
11530 return cp_build_qualified_type_real
11531 (arg, quals, complain | tf_ignore_bad_quals);
11532 }
11533 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11534 {
11535 /* We are processing a type constructed from a
11536 template template parameter. */
11537 tree argvec = tsubst (TYPE_TI_ARGS (t),
11538 args, complain, in_decl);
11539 if (argvec == error_mark_node)
11540 return error_mark_node;
11541
11542 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11543 || TREE_CODE (arg) == TEMPLATE_DECL
11544 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11545
11546 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11547 /* Consider this code:
11548
11549 template <template <class> class Template>
11550 struct Internal {
11551 template <class Arg> using Bind = Template<Arg>;
11552 };
11553
11554 template <template <class> class Template, class Arg>
11555 using Instantiate = Template<Arg>; //#0
11556
11557 template <template <class> class Template,
11558 class Argument>
11559 using Bind =
11560 Instantiate<Internal<Template>::template Bind,
11561 Argument>; //#1
11562
11563 When #1 is parsed, the
11564 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11565 parameter `Template' in #0 matches the
11566 UNBOUND_CLASS_TEMPLATE representing the argument
11567 `Internal<Template>::template Bind'; We then want
11568 to assemble the type `Bind<Argument>' that can't
11569 be fully created right now, because
11570 `Internal<Template>' not being complete, the Bind
11571 template cannot be looked up in that context. So
11572 we need to "store" `Bind<Argument>' for later
11573 when the context of Bind becomes complete. Let's
11574 store that in a TYPENAME_TYPE. */
11575 return make_typename_type (TYPE_CONTEXT (arg),
11576 build_nt (TEMPLATE_ID_EXPR,
11577 TYPE_IDENTIFIER (arg),
11578 argvec),
11579 typename_type,
11580 complain);
11581
11582 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11583 are resolving nested-types in the signature of a
11584 member function templates. Otherwise ARG is a
11585 TEMPLATE_DECL and is the real template to be
11586 instantiated. */
11587 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11588 arg = TYPE_NAME (arg);
11589
11590 r = lookup_template_class (arg,
11591 argvec, in_decl,
11592 DECL_CONTEXT (arg),
11593 /*entering_scope=*/0,
11594 complain);
11595 return cp_build_qualified_type_real
11596 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11597 }
11598 else
11599 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11600 return convert_from_reference (unshare_expr (arg));
11601 }
11602
11603 if (level == 1)
11604 /* This can happen during the attempted tsubst'ing in
11605 unify. This means that we don't yet have any information
11606 about the template parameter in question. */
11607 return t;
11608
11609 /* Early in template argument deduction substitution, we don't
11610 want to reduce the level of 'auto', or it will be confused
11611 with a normal template parm in subsequent deduction. */
11612 if (is_auto (t) && (complain & tf_partial))
11613 return t;
11614
11615 /* If we get here, we must have been looking at a parm for a
11616 more deeply nested template. Make a new version of this
11617 template parameter, but with a lower level. */
11618 switch (code)
11619 {
11620 case TEMPLATE_TYPE_PARM:
11621 case TEMPLATE_TEMPLATE_PARM:
11622 case BOUND_TEMPLATE_TEMPLATE_PARM:
11623 if (cp_type_quals (t))
11624 {
11625 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11626 r = cp_build_qualified_type_real
11627 (r, cp_type_quals (t),
11628 complain | (code == TEMPLATE_TYPE_PARM
11629 ? tf_ignore_bad_quals : 0));
11630 }
11631 else
11632 {
11633 r = copy_type (t);
11634 TEMPLATE_TYPE_PARM_INDEX (r)
11635 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11636 r, levels, args, complain);
11637 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11638 TYPE_MAIN_VARIANT (r) = r;
11639 TYPE_POINTER_TO (r) = NULL_TREE;
11640 TYPE_REFERENCE_TO (r) = NULL_TREE;
11641
11642 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11643 /* We have reduced the level of the template
11644 template parameter, but not the levels of its
11645 template parameters, so canonical_type_parameter
11646 will not be able to find the canonical template
11647 template parameter for this level. Thus, we
11648 require structural equality checking to compare
11649 TEMPLATE_TEMPLATE_PARMs. */
11650 SET_TYPE_STRUCTURAL_EQUALITY (r);
11651 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11652 SET_TYPE_STRUCTURAL_EQUALITY (r);
11653 else
11654 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11655
11656 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11657 {
11658 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11659 complain, in_decl);
11660 if (argvec == error_mark_node)
11661 return error_mark_node;
11662
11663 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11664 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11665 }
11666 }
11667 break;
11668
11669 case TEMPLATE_PARM_INDEX:
11670 r = reduce_template_parm_level (t, type, levels, args, complain);
11671 break;
11672
11673 default:
11674 gcc_unreachable ();
11675 }
11676
11677 return r;
11678 }
11679
11680 case TREE_LIST:
11681 {
11682 tree purpose, value, chain;
11683
11684 if (t == void_list_node)
11685 return t;
11686
11687 purpose = TREE_PURPOSE (t);
11688 if (purpose)
11689 {
11690 purpose = tsubst (purpose, args, complain, in_decl);
11691 if (purpose == error_mark_node)
11692 return error_mark_node;
11693 }
11694 value = TREE_VALUE (t);
11695 if (value)
11696 {
11697 value = tsubst (value, args, complain, in_decl);
11698 if (value == error_mark_node)
11699 return error_mark_node;
11700 }
11701 chain = TREE_CHAIN (t);
11702 if (chain && chain != void_type_node)
11703 {
11704 chain = tsubst (chain, args, complain, in_decl);
11705 if (chain == error_mark_node)
11706 return error_mark_node;
11707 }
11708 if (purpose == TREE_PURPOSE (t)
11709 && value == TREE_VALUE (t)
11710 && chain == TREE_CHAIN (t))
11711 return t;
11712 return hash_tree_cons (purpose, value, chain);
11713 }
11714
11715 case TREE_BINFO:
11716 /* We should never be tsubsting a binfo. */
11717 gcc_unreachable ();
11718
11719 case TREE_VEC:
11720 /* A vector of template arguments. */
11721 gcc_assert (!type);
11722 return tsubst_template_args (t, args, complain, in_decl);
11723
11724 case POINTER_TYPE:
11725 case REFERENCE_TYPE:
11726 {
11727 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11728 return t;
11729
11730 /* [temp.deduct]
11731
11732 Type deduction may fail for any of the following
11733 reasons:
11734
11735 -- Attempting to create a pointer to reference type.
11736 -- Attempting to create a reference to a reference type or
11737 a reference to void.
11738
11739 Core issue 106 says that creating a reference to a reference
11740 during instantiation is no longer a cause for failure. We
11741 only enforce this check in strict C++98 mode. */
11742 if ((TREE_CODE (type) == REFERENCE_TYPE
11743 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11744 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11745 {
11746 static location_t last_loc;
11747
11748 /* We keep track of the last time we issued this error
11749 message to avoid spewing a ton of messages during a
11750 single bad template instantiation. */
11751 if (complain & tf_error
11752 && last_loc != input_location)
11753 {
11754 if (VOID_TYPE_P (type))
11755 error ("forming reference to void");
11756 else if (code == POINTER_TYPE)
11757 error ("forming pointer to reference type %qT", type);
11758 else
11759 error ("forming reference to reference type %qT", type);
11760 last_loc = input_location;
11761 }
11762
11763 return error_mark_node;
11764 }
11765 else if (TREE_CODE (type) == FUNCTION_TYPE
11766 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11767 || type_memfn_rqual (type) != REF_QUAL_NONE))
11768 {
11769 if (complain & tf_error)
11770 {
11771 if (code == POINTER_TYPE)
11772 error ("forming pointer to qualified function type %qT",
11773 type);
11774 else
11775 error ("forming reference to qualified function type %qT",
11776 type);
11777 }
11778 return error_mark_node;
11779 }
11780 else if (code == POINTER_TYPE)
11781 {
11782 r = build_pointer_type (type);
11783 if (TREE_CODE (type) == METHOD_TYPE)
11784 r = build_ptrmemfunc_type (r);
11785 }
11786 else if (TREE_CODE (type) == REFERENCE_TYPE)
11787 /* In C++0x, during template argument substitution, when there is an
11788 attempt to create a reference to a reference type, reference
11789 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11790
11791 "If a template-argument for a template-parameter T names a type
11792 that is a reference to a type A, an attempt to create the type
11793 'lvalue reference to cv T' creates the type 'lvalue reference to
11794 A,' while an attempt to create the type type rvalue reference to
11795 cv T' creates the type T"
11796 */
11797 r = cp_build_reference_type
11798 (TREE_TYPE (type),
11799 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11800 else
11801 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11802 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11803
11804 if (cxx_dialect >= cxx1y
11805 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11806 && array_of_runtime_bound_p (type))
11807 {
11808 if (complain & tf_warning_or_error)
11809 pedwarn
11810 (input_location, OPT_Wvla,
11811 code == REFERENCE_TYPE
11812 ? G_("cannot declare reference to array of runtime bound")
11813 : G_("cannot declare pointer to array of runtime bound"));
11814 else
11815 r = error_mark_node;
11816 }
11817
11818 if (r != error_mark_node)
11819 /* Will this ever be needed for TYPE_..._TO values? */
11820 layout_type (r);
11821
11822 return r;
11823 }
11824 case OFFSET_TYPE:
11825 {
11826 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11827 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11828 {
11829 /* [temp.deduct]
11830
11831 Type deduction may fail for any of the following
11832 reasons:
11833
11834 -- Attempting to create "pointer to member of T" when T
11835 is not a class type. */
11836 if (complain & tf_error)
11837 error ("creating pointer to member of non-class type %qT", r);
11838 return error_mark_node;
11839 }
11840 if (TREE_CODE (type) == REFERENCE_TYPE)
11841 {
11842 if (complain & tf_error)
11843 error ("creating pointer to member reference type %qT", type);
11844 return error_mark_node;
11845 }
11846 if (VOID_TYPE_P (type))
11847 {
11848 if (complain & tf_error)
11849 error ("creating pointer to member of type void");
11850 return error_mark_node;
11851 }
11852 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11853 if (TREE_CODE (type) == FUNCTION_TYPE)
11854 {
11855 /* The type of the implicit object parameter gets its
11856 cv-qualifiers from the FUNCTION_TYPE. */
11857 tree memptr;
11858 tree method_type
11859 = build_memfn_type (type, r, type_memfn_quals (type),
11860 type_memfn_rqual (type));
11861 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11862 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11863 complain);
11864 }
11865 else
11866 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11867 cp_type_quals (t),
11868 complain);
11869 }
11870 case FUNCTION_TYPE:
11871 case METHOD_TYPE:
11872 {
11873 tree fntype;
11874 tree specs;
11875 fntype = tsubst_function_type (t, args, complain, in_decl);
11876 if (fntype == error_mark_node)
11877 return error_mark_node;
11878
11879 /* Substitute the exception specification. */
11880 specs = tsubst_exception_specification (t, args, complain,
11881 in_decl, /*defer_ok*/true);
11882 if (specs == error_mark_node)
11883 return error_mark_node;
11884 if (specs)
11885 fntype = build_exception_variant (fntype, specs);
11886 return fntype;
11887 }
11888 case ARRAY_TYPE:
11889 {
11890 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11891 if (domain == error_mark_node)
11892 return error_mark_node;
11893
11894 /* As an optimization, we avoid regenerating the array type if
11895 it will obviously be the same as T. */
11896 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11897 return t;
11898
11899 /* These checks should match the ones in grokdeclarator.
11900
11901 [temp.deduct]
11902
11903 The deduction may fail for any of the following reasons:
11904
11905 -- Attempting to create an array with an element type that
11906 is void, a function type, or a reference type, or [DR337]
11907 an abstract class type. */
11908 if (VOID_TYPE_P (type)
11909 || TREE_CODE (type) == FUNCTION_TYPE
11910 || TREE_CODE (type) == REFERENCE_TYPE)
11911 {
11912 if (complain & tf_error)
11913 error ("creating array of %qT", type);
11914 return error_mark_node;
11915 }
11916
11917 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11918 return error_mark_node;
11919
11920 r = build_cplus_array_type (type, domain);
11921
11922 if (TYPE_USER_ALIGN (t))
11923 {
11924 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11925 TYPE_USER_ALIGN (r) = 1;
11926 }
11927
11928 return r;
11929 }
11930
11931 case TYPENAME_TYPE:
11932 {
11933 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11934 in_decl, /*entering_scope=*/1);
11935 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11936 complain, in_decl);
11937
11938 if (ctx == error_mark_node || f == error_mark_node)
11939 return error_mark_node;
11940
11941 if (!MAYBE_CLASS_TYPE_P (ctx))
11942 {
11943 if (complain & tf_error)
11944 error ("%qT is not a class, struct, or union type", ctx);
11945 return error_mark_node;
11946 }
11947 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11948 {
11949 /* Normally, make_typename_type does not require that the CTX
11950 have complete type in order to allow things like:
11951
11952 template <class T> struct S { typename S<T>::X Y; };
11953
11954 But, such constructs have already been resolved by this
11955 point, so here CTX really should have complete type, unless
11956 it's a partial instantiation. */
11957 ctx = complete_type (ctx);
11958 if (!COMPLETE_TYPE_P (ctx))
11959 {
11960 if (complain & tf_error)
11961 cxx_incomplete_type_error (NULL_TREE, ctx);
11962 return error_mark_node;
11963 }
11964 }
11965
11966 f = make_typename_type (ctx, f, typename_type,
11967 complain | tf_keep_type_decl);
11968 if (f == error_mark_node)
11969 return f;
11970 if (TREE_CODE (f) == TYPE_DECL)
11971 {
11972 complain |= tf_ignore_bad_quals;
11973 f = TREE_TYPE (f);
11974 }
11975
11976 if (TREE_CODE (f) != TYPENAME_TYPE)
11977 {
11978 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11979 {
11980 if (complain & tf_error)
11981 error ("%qT resolves to %qT, which is not an enumeration type",
11982 t, f);
11983 else
11984 return error_mark_node;
11985 }
11986 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11987 {
11988 if (complain & tf_error)
11989 error ("%qT resolves to %qT, which is is not a class type",
11990 t, f);
11991 else
11992 return error_mark_node;
11993 }
11994 }
11995
11996 return cp_build_qualified_type_real
11997 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11998 }
11999
12000 case UNBOUND_CLASS_TEMPLATE:
12001 {
12002 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12003 in_decl, /*entering_scope=*/1);
12004 tree name = TYPE_IDENTIFIER (t);
12005 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12006
12007 if (ctx == error_mark_node || name == error_mark_node)
12008 return error_mark_node;
12009
12010 if (parm_list)
12011 parm_list = tsubst_template_parms (parm_list, args, complain);
12012 return make_unbound_class_template (ctx, name, parm_list, complain);
12013 }
12014
12015 case TYPEOF_TYPE:
12016 {
12017 tree type;
12018
12019 ++cp_unevaluated_operand;
12020 ++c_inhibit_evaluation_warnings;
12021
12022 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12023 complain, in_decl,
12024 /*integral_constant_expression_p=*/false);
12025
12026 --cp_unevaluated_operand;
12027 --c_inhibit_evaluation_warnings;
12028
12029 type = finish_typeof (type);
12030 return cp_build_qualified_type_real (type,
12031 cp_type_quals (t)
12032 | cp_type_quals (type),
12033 complain);
12034 }
12035
12036 case DECLTYPE_TYPE:
12037 {
12038 tree type;
12039
12040 ++cp_unevaluated_operand;
12041 ++c_inhibit_evaluation_warnings;
12042
12043 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12044 complain|tf_decltype, in_decl,
12045 /*function_p*/false,
12046 /*integral_constant_expression*/false);
12047
12048 --cp_unevaluated_operand;
12049 --c_inhibit_evaluation_warnings;
12050
12051 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12052 type = lambda_capture_field_type (type,
12053 DECLTYPE_FOR_INIT_CAPTURE (t));
12054 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12055 type = lambda_proxy_type (type);
12056 else
12057 {
12058 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12059 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12060 && EXPR_P (type))
12061 /* In a template ~id could be either a complement expression
12062 or an unqualified-id naming a destructor; if instantiating
12063 it produces an expression, it's not an id-expression or
12064 member access. */
12065 id = false;
12066 type = finish_decltype_type (type, id, complain);
12067 }
12068 return cp_build_qualified_type_real (type,
12069 cp_type_quals (t)
12070 | cp_type_quals (type),
12071 complain);
12072 }
12073
12074 case UNDERLYING_TYPE:
12075 {
12076 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12077 complain, in_decl);
12078 return finish_underlying_type (type);
12079 }
12080
12081 case TYPE_ARGUMENT_PACK:
12082 case NONTYPE_ARGUMENT_PACK:
12083 {
12084 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12085 tree packed_out =
12086 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12087 args,
12088 complain,
12089 in_decl);
12090 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12091
12092 /* For template nontype argument packs, also substitute into
12093 the type. */
12094 if (code == NONTYPE_ARGUMENT_PACK)
12095 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12096
12097 return r;
12098 }
12099 break;
12100
12101 case INTEGER_CST:
12102 case REAL_CST:
12103 case STRING_CST:
12104 case PLUS_EXPR:
12105 case MINUS_EXPR:
12106 case NEGATE_EXPR:
12107 case NOP_EXPR:
12108 case INDIRECT_REF:
12109 case ADDR_EXPR:
12110 case CALL_EXPR:
12111 case ARRAY_REF:
12112 case SCOPE_REF:
12113 /* We should use one of the expression tsubsts for these codes. */
12114 gcc_unreachable ();
12115
12116 default:
12117 sorry ("use of %qs in template", get_tree_code_name (code));
12118 return error_mark_node;
12119 }
12120 }
12121
12122 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12123 type of the expression on the left-hand side of the "." or "->"
12124 operator. */
12125
12126 static tree
12127 tsubst_baselink (tree baselink, tree object_type,
12128 tree args, tsubst_flags_t complain, tree in_decl)
12129 {
12130 tree name;
12131 tree qualifying_scope;
12132 tree fns;
12133 tree optype;
12134 tree template_args = 0;
12135 bool template_id_p = false;
12136 bool qualified = BASELINK_QUALIFIED_P (baselink);
12137
12138 /* A baselink indicates a function from a base class. Both the
12139 BASELINK_ACCESS_BINFO and the base class referenced may
12140 indicate bases of the template class, rather than the
12141 instantiated class. In addition, lookups that were not
12142 ambiguous before may be ambiguous now. Therefore, we perform
12143 the lookup again. */
12144 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12145 qualifying_scope = tsubst (qualifying_scope, args,
12146 complain, in_decl);
12147 fns = BASELINK_FUNCTIONS (baselink);
12148 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12149 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12150 {
12151 template_id_p = true;
12152 template_args = TREE_OPERAND (fns, 1);
12153 fns = TREE_OPERAND (fns, 0);
12154 if (template_args)
12155 template_args = tsubst_template_args (template_args, args,
12156 complain, in_decl);
12157 }
12158 name = DECL_NAME (get_first_fn (fns));
12159 if (IDENTIFIER_TYPENAME_P (name))
12160 name = mangle_conv_op_name_for_type (optype);
12161 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12162 if (!baselink)
12163 return error_mark_node;
12164
12165 /* If lookup found a single function, mark it as used at this
12166 point. (If it lookup found multiple functions the one selected
12167 later by overload resolution will be marked as used at that
12168 point.) */
12169 if (BASELINK_P (baselink))
12170 fns = BASELINK_FUNCTIONS (baselink);
12171 if (!template_id_p && !really_overloaded_fn (fns))
12172 mark_used (OVL_CURRENT (fns));
12173
12174 /* Add back the template arguments, if present. */
12175 if (BASELINK_P (baselink) && template_id_p)
12176 BASELINK_FUNCTIONS (baselink)
12177 = build_nt (TEMPLATE_ID_EXPR,
12178 BASELINK_FUNCTIONS (baselink),
12179 template_args);
12180 /* Update the conversion operator type. */
12181 BASELINK_OPTYPE (baselink) = optype;
12182
12183 if (!object_type)
12184 object_type = current_class_type;
12185
12186 if (qualified)
12187 baselink = adjust_result_of_qualified_name_lookup (baselink,
12188 qualifying_scope,
12189 object_type);
12190 return baselink;
12191 }
12192
12193 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12194 true if the qualified-id will be a postfix-expression in-and-of
12195 itself; false if more of the postfix-expression follows the
12196 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12197 of "&". */
12198
12199 static tree
12200 tsubst_qualified_id (tree qualified_id, tree args,
12201 tsubst_flags_t complain, tree in_decl,
12202 bool done, bool address_p)
12203 {
12204 tree expr;
12205 tree scope;
12206 tree name;
12207 bool is_template;
12208 tree template_args;
12209 location_t loc = UNKNOWN_LOCATION;
12210
12211 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12212
12213 /* Figure out what name to look up. */
12214 name = TREE_OPERAND (qualified_id, 1);
12215 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12216 {
12217 is_template = true;
12218 loc = EXPR_LOCATION (name);
12219 template_args = TREE_OPERAND (name, 1);
12220 if (template_args)
12221 template_args = tsubst_template_args (template_args, args,
12222 complain, in_decl);
12223 name = TREE_OPERAND (name, 0);
12224 }
12225 else
12226 {
12227 is_template = false;
12228 template_args = NULL_TREE;
12229 }
12230
12231 /* Substitute into the qualifying scope. When there are no ARGS, we
12232 are just trying to simplify a non-dependent expression. In that
12233 case the qualifying scope may be dependent, and, in any case,
12234 substituting will not help. */
12235 scope = TREE_OPERAND (qualified_id, 0);
12236 if (args)
12237 {
12238 scope = tsubst (scope, args, complain, in_decl);
12239 expr = tsubst_copy (name, args, complain, in_decl);
12240 }
12241 else
12242 expr = name;
12243
12244 if (dependent_scope_p (scope))
12245 {
12246 if (is_template)
12247 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12248 return build_qualified_name (NULL_TREE, scope, expr,
12249 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12250 }
12251
12252 if (!BASELINK_P (name) && !DECL_P (expr))
12253 {
12254 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12255 {
12256 /* A BIT_NOT_EXPR is used to represent a destructor. */
12257 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12258 {
12259 error ("qualifying type %qT does not match destructor name ~%qT",
12260 scope, TREE_OPERAND (expr, 0));
12261 expr = error_mark_node;
12262 }
12263 else
12264 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12265 /*is_type_p=*/0, false);
12266 }
12267 else
12268 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12269 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12270 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12271 {
12272 if (complain & tf_error)
12273 {
12274 error ("dependent-name %qE is parsed as a non-type, but "
12275 "instantiation yields a type", qualified_id);
12276 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12277 }
12278 return error_mark_node;
12279 }
12280 }
12281
12282 if (DECL_P (expr))
12283 {
12284 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12285 scope);
12286 /* Remember that there was a reference to this entity. */
12287 mark_used (expr);
12288 }
12289
12290 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12291 {
12292 if (complain & tf_error)
12293 qualified_name_lookup_error (scope,
12294 TREE_OPERAND (qualified_id, 1),
12295 expr, input_location);
12296 return error_mark_node;
12297 }
12298
12299 if (is_template)
12300 expr = lookup_template_function (expr, template_args);
12301
12302 if (expr == error_mark_node && complain & tf_error)
12303 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12304 expr, input_location);
12305 else if (TYPE_P (scope))
12306 {
12307 expr = (adjust_result_of_qualified_name_lookup
12308 (expr, scope, current_nonlambda_class_type ()));
12309 expr = (finish_qualified_id_expr
12310 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12311 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12312 /*template_arg_p=*/false, complain));
12313 }
12314
12315 /* Expressions do not generally have reference type. */
12316 if (TREE_CODE (expr) != SCOPE_REF
12317 /* However, if we're about to form a pointer-to-member, we just
12318 want the referenced member referenced. */
12319 && TREE_CODE (expr) != OFFSET_REF)
12320 expr = convert_from_reference (expr);
12321
12322 return expr;
12323 }
12324
12325 /* Like tsubst, but deals with expressions. This function just replaces
12326 template parms; to finish processing the resultant expression, use
12327 tsubst_copy_and_build or tsubst_expr. */
12328
12329 static tree
12330 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12331 {
12332 enum tree_code code;
12333 tree r;
12334
12335 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12336 return t;
12337
12338 code = TREE_CODE (t);
12339
12340 switch (code)
12341 {
12342 case PARM_DECL:
12343 r = retrieve_local_specialization (t);
12344
12345 if (r == NULL_TREE)
12346 {
12347 /* We get here for a use of 'this' in an NSDMI. */
12348 if (DECL_NAME (t) == this_identifier
12349 && at_function_scope_p ()
12350 && DECL_CONSTRUCTOR_P (current_function_decl))
12351 return current_class_ptr;
12352
12353 /* This can happen for a parameter name used later in a function
12354 declaration (such as in a late-specified return type). Just
12355 make a dummy decl, since it's only used for its type. */
12356 gcc_assert (cp_unevaluated_operand != 0);
12357 r = tsubst_decl (t, args, complain);
12358 /* Give it the template pattern as its context; its true context
12359 hasn't been instantiated yet and this is good enough for
12360 mangling. */
12361 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12362 }
12363
12364 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12365 r = ARGUMENT_PACK_SELECT_ARG (r);
12366 mark_used (r);
12367 return r;
12368
12369 case CONST_DECL:
12370 {
12371 tree enum_type;
12372 tree v;
12373
12374 if (DECL_TEMPLATE_PARM_P (t))
12375 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12376 /* There is no need to substitute into namespace-scope
12377 enumerators. */
12378 if (DECL_NAMESPACE_SCOPE_P (t))
12379 return t;
12380 /* If ARGS is NULL, then T is known to be non-dependent. */
12381 if (args == NULL_TREE)
12382 return integral_constant_value (t);
12383
12384 /* Unfortunately, we cannot just call lookup_name here.
12385 Consider:
12386
12387 template <int I> int f() {
12388 enum E { a = I };
12389 struct S { void g() { E e = a; } };
12390 };
12391
12392 When we instantiate f<7>::S::g(), say, lookup_name is not
12393 clever enough to find f<7>::a. */
12394 enum_type
12395 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12396 /*entering_scope=*/0);
12397
12398 for (v = TYPE_VALUES (enum_type);
12399 v != NULL_TREE;
12400 v = TREE_CHAIN (v))
12401 if (TREE_PURPOSE (v) == DECL_NAME (t))
12402 return TREE_VALUE (v);
12403
12404 /* We didn't find the name. That should never happen; if
12405 name-lookup found it during preliminary parsing, we
12406 should find it again here during instantiation. */
12407 gcc_unreachable ();
12408 }
12409 return t;
12410
12411 case FIELD_DECL:
12412 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12413 {
12414 /* Check for a local specialization set up by
12415 tsubst_pack_expansion. */
12416 tree r = retrieve_local_specialization (t);
12417 if (r)
12418 {
12419 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12420 r = ARGUMENT_PACK_SELECT_ARG (r);
12421 return r;
12422 }
12423
12424 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12425 tsubst_decl put in the hash table. */
12426 return retrieve_specialization (t, args, 0);
12427 }
12428
12429 if (DECL_CONTEXT (t))
12430 {
12431 tree ctx;
12432
12433 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12434 /*entering_scope=*/1);
12435 if (ctx != DECL_CONTEXT (t))
12436 {
12437 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12438 if (!r)
12439 {
12440 if (complain & tf_error)
12441 error ("using invalid field %qD", t);
12442 return error_mark_node;
12443 }
12444 return r;
12445 }
12446 }
12447
12448 return t;
12449
12450 case VAR_DECL:
12451 case FUNCTION_DECL:
12452 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12453 r = tsubst (t, args, complain, in_decl);
12454 else if (local_variable_p (t))
12455 {
12456 r = retrieve_local_specialization (t);
12457 if (r == NULL_TREE)
12458 {
12459 if (DECL_ANON_UNION_VAR_P (t))
12460 {
12461 /* Just use name lookup to find a member alias for an
12462 anonymous union, but then add it to the hash table. */
12463 r = lookup_name (DECL_NAME (t));
12464 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12465 register_local_specialization (r, t);
12466 }
12467 else
12468 {
12469 gcc_assert (errorcount || sorrycount);
12470 return error_mark_node;
12471 }
12472 }
12473 }
12474 else
12475 r = t;
12476 mark_used (r);
12477 return r;
12478
12479 case NAMESPACE_DECL:
12480 return t;
12481
12482 case OVERLOAD:
12483 /* An OVERLOAD will always be a non-dependent overload set; an
12484 overload set from function scope will just be represented with an
12485 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12486 gcc_assert (!uses_template_parms (t));
12487 return t;
12488
12489 case BASELINK:
12490 return tsubst_baselink (t, current_nonlambda_class_type (),
12491 args, complain, in_decl);
12492
12493 case TEMPLATE_DECL:
12494 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12495 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12496 args, complain, in_decl);
12497 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12498 return tsubst (t, args, complain, in_decl);
12499 else if (DECL_CLASS_SCOPE_P (t)
12500 && uses_template_parms (DECL_CONTEXT (t)))
12501 {
12502 /* Template template argument like the following example need
12503 special treatment:
12504
12505 template <template <class> class TT> struct C {};
12506 template <class T> struct D {
12507 template <class U> struct E {};
12508 C<E> c; // #1
12509 };
12510 D<int> d; // #2
12511
12512 We are processing the template argument `E' in #1 for
12513 the template instantiation #2. Originally, `E' is a
12514 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12515 have to substitute this with one having context `D<int>'. */
12516
12517 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12518 return lookup_field (context, DECL_NAME(t), 0, false);
12519 }
12520 else
12521 /* Ordinary template template argument. */
12522 return t;
12523
12524 case CAST_EXPR:
12525 case REINTERPRET_CAST_EXPR:
12526 case CONST_CAST_EXPR:
12527 case STATIC_CAST_EXPR:
12528 case DYNAMIC_CAST_EXPR:
12529 case IMPLICIT_CONV_EXPR:
12530 case CONVERT_EXPR:
12531 case NOP_EXPR:
12532 return build1
12533 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12534 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12535
12536 case SIZEOF_EXPR:
12537 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12538 {
12539
12540 tree expanded, op = TREE_OPERAND (t, 0);
12541 int len = 0;
12542
12543 if (SIZEOF_EXPR_TYPE_P (t))
12544 op = TREE_TYPE (op);
12545
12546 ++cp_unevaluated_operand;
12547 ++c_inhibit_evaluation_warnings;
12548 /* We only want to compute the number of arguments. */
12549 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12550 --cp_unevaluated_operand;
12551 --c_inhibit_evaluation_warnings;
12552
12553 if (TREE_CODE (expanded) == TREE_VEC)
12554 len = TREE_VEC_LENGTH (expanded);
12555
12556 if (expanded == error_mark_node)
12557 return error_mark_node;
12558 else if (PACK_EXPANSION_P (expanded)
12559 || (TREE_CODE (expanded) == TREE_VEC
12560 && len > 0
12561 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12562 {
12563 if (TREE_CODE (expanded) == TREE_VEC)
12564 expanded = TREE_VEC_ELT (expanded, len - 1);
12565
12566 if (TYPE_P (expanded))
12567 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12568 complain & tf_error);
12569 else
12570 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12571 complain & tf_error);
12572 }
12573 else
12574 return build_int_cst (size_type_node, len);
12575 }
12576 if (SIZEOF_EXPR_TYPE_P (t))
12577 {
12578 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12579 args, complain, in_decl);
12580 r = build1 (NOP_EXPR, r, error_mark_node);
12581 r = build1 (SIZEOF_EXPR,
12582 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12583 SIZEOF_EXPR_TYPE_P (r) = 1;
12584 return r;
12585 }
12586 /* Fall through */
12587
12588 case INDIRECT_REF:
12589 case NEGATE_EXPR:
12590 case TRUTH_NOT_EXPR:
12591 case BIT_NOT_EXPR:
12592 case ADDR_EXPR:
12593 case UNARY_PLUS_EXPR: /* Unary + */
12594 case ALIGNOF_EXPR:
12595 case AT_ENCODE_EXPR:
12596 case ARROW_EXPR:
12597 case THROW_EXPR:
12598 case TYPEID_EXPR:
12599 case REALPART_EXPR:
12600 case IMAGPART_EXPR:
12601 case PAREN_EXPR:
12602 return build1
12603 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12604 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12605
12606 case COMPONENT_REF:
12607 {
12608 tree object;
12609 tree name;
12610
12611 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12612 name = TREE_OPERAND (t, 1);
12613 if (TREE_CODE (name) == BIT_NOT_EXPR)
12614 {
12615 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12616 complain, in_decl);
12617 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12618 }
12619 else if (TREE_CODE (name) == SCOPE_REF
12620 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12621 {
12622 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12623 complain, in_decl);
12624 name = TREE_OPERAND (name, 1);
12625 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12626 complain, in_decl);
12627 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12628 name = build_qualified_name (/*type=*/NULL_TREE,
12629 base, name,
12630 /*template_p=*/false);
12631 }
12632 else if (BASELINK_P (name))
12633 name = tsubst_baselink (name,
12634 non_reference (TREE_TYPE (object)),
12635 args, complain,
12636 in_decl);
12637 else
12638 name = tsubst_copy (name, args, complain, in_decl);
12639 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12640 }
12641
12642 case PLUS_EXPR:
12643 case MINUS_EXPR:
12644 case MULT_EXPR:
12645 case TRUNC_DIV_EXPR:
12646 case CEIL_DIV_EXPR:
12647 case FLOOR_DIV_EXPR:
12648 case ROUND_DIV_EXPR:
12649 case EXACT_DIV_EXPR:
12650 case BIT_AND_EXPR:
12651 case BIT_IOR_EXPR:
12652 case BIT_XOR_EXPR:
12653 case TRUNC_MOD_EXPR:
12654 case FLOOR_MOD_EXPR:
12655 case TRUTH_ANDIF_EXPR:
12656 case TRUTH_ORIF_EXPR:
12657 case TRUTH_AND_EXPR:
12658 case TRUTH_OR_EXPR:
12659 case RSHIFT_EXPR:
12660 case LSHIFT_EXPR:
12661 case RROTATE_EXPR:
12662 case LROTATE_EXPR:
12663 case EQ_EXPR:
12664 case NE_EXPR:
12665 case MAX_EXPR:
12666 case MIN_EXPR:
12667 case LE_EXPR:
12668 case GE_EXPR:
12669 case LT_EXPR:
12670 case GT_EXPR:
12671 case COMPOUND_EXPR:
12672 case DOTSTAR_EXPR:
12673 case MEMBER_REF:
12674 case PREDECREMENT_EXPR:
12675 case PREINCREMENT_EXPR:
12676 case POSTDECREMENT_EXPR:
12677 case POSTINCREMENT_EXPR:
12678 return build_nt
12679 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12680 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12681
12682 case SCOPE_REF:
12683 return build_qualified_name (/*type=*/NULL_TREE,
12684 tsubst_copy (TREE_OPERAND (t, 0),
12685 args, complain, in_decl),
12686 tsubst_copy (TREE_OPERAND (t, 1),
12687 args, complain, in_decl),
12688 QUALIFIED_NAME_IS_TEMPLATE (t));
12689
12690 case ARRAY_REF:
12691 return build_nt
12692 (ARRAY_REF,
12693 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12694 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12695 NULL_TREE, NULL_TREE);
12696
12697 case CALL_EXPR:
12698 {
12699 int n = VL_EXP_OPERAND_LENGTH (t);
12700 tree result = build_vl_exp (CALL_EXPR, n);
12701 int i;
12702 for (i = 0; i < n; i++)
12703 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12704 complain, in_decl);
12705 return result;
12706 }
12707
12708 case COND_EXPR:
12709 case MODOP_EXPR:
12710 case PSEUDO_DTOR_EXPR:
12711 case VEC_PERM_EXPR:
12712 {
12713 r = build_nt
12714 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12715 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12716 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12717 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12718 return r;
12719 }
12720
12721 case NEW_EXPR:
12722 {
12723 r = build_nt
12724 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12725 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12726 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12727 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12728 return r;
12729 }
12730
12731 case DELETE_EXPR:
12732 {
12733 r = build_nt
12734 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12735 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12736 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12737 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12738 return r;
12739 }
12740
12741 case TEMPLATE_ID_EXPR:
12742 {
12743 /* Substituted template arguments */
12744 tree fn = TREE_OPERAND (t, 0);
12745 tree targs = TREE_OPERAND (t, 1);
12746
12747 fn = tsubst_copy (fn, args, complain, in_decl);
12748 if (targs)
12749 targs = tsubst_template_args (targs, args, complain, in_decl);
12750
12751 return lookup_template_function (fn, targs);
12752 }
12753
12754 case TREE_LIST:
12755 {
12756 tree purpose, value, chain;
12757
12758 if (t == void_list_node)
12759 return t;
12760
12761 purpose = TREE_PURPOSE (t);
12762 if (purpose)
12763 purpose = tsubst_copy (purpose, args, complain, in_decl);
12764 value = TREE_VALUE (t);
12765 if (value)
12766 value = tsubst_copy (value, args, complain, in_decl);
12767 chain = TREE_CHAIN (t);
12768 if (chain && chain != void_type_node)
12769 chain = tsubst_copy (chain, args, complain, in_decl);
12770 if (purpose == TREE_PURPOSE (t)
12771 && value == TREE_VALUE (t)
12772 && chain == TREE_CHAIN (t))
12773 return t;
12774 return tree_cons (purpose, value, chain);
12775 }
12776
12777 case RECORD_TYPE:
12778 case UNION_TYPE:
12779 case ENUMERAL_TYPE:
12780 case INTEGER_TYPE:
12781 case TEMPLATE_TYPE_PARM:
12782 case TEMPLATE_TEMPLATE_PARM:
12783 case BOUND_TEMPLATE_TEMPLATE_PARM:
12784 case TEMPLATE_PARM_INDEX:
12785 case POINTER_TYPE:
12786 case REFERENCE_TYPE:
12787 case OFFSET_TYPE:
12788 case FUNCTION_TYPE:
12789 case METHOD_TYPE:
12790 case ARRAY_TYPE:
12791 case TYPENAME_TYPE:
12792 case UNBOUND_CLASS_TEMPLATE:
12793 case TYPEOF_TYPE:
12794 case DECLTYPE_TYPE:
12795 case TYPE_DECL:
12796 return tsubst (t, args, complain, in_decl);
12797
12798 case USING_DECL:
12799 t = DECL_NAME (t);
12800 /* Fall through. */
12801 case IDENTIFIER_NODE:
12802 if (IDENTIFIER_TYPENAME_P (t))
12803 {
12804 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12805 return mangle_conv_op_name_for_type (new_type);
12806 }
12807 else
12808 return t;
12809
12810 case CONSTRUCTOR:
12811 /* This is handled by tsubst_copy_and_build. */
12812 gcc_unreachable ();
12813
12814 case VA_ARG_EXPR:
12815 return build_x_va_arg (EXPR_LOCATION (t),
12816 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12817 in_decl),
12818 tsubst (TREE_TYPE (t), args, complain, in_decl));
12819
12820 case CLEANUP_POINT_EXPR:
12821 /* We shouldn't have built any of these during initial template
12822 generation. Instead, they should be built during instantiation
12823 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12824 gcc_unreachable ();
12825
12826 case OFFSET_REF:
12827 r = build2
12828 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12829 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12830 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12831 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12832 mark_used (TREE_OPERAND (r, 1));
12833 return r;
12834
12835 case EXPR_PACK_EXPANSION:
12836 error ("invalid use of pack expansion expression");
12837 return error_mark_node;
12838
12839 case NONTYPE_ARGUMENT_PACK:
12840 error ("use %<...%> to expand argument pack");
12841 return error_mark_node;
12842
12843 case INTEGER_CST:
12844 case REAL_CST:
12845 case STRING_CST:
12846 case COMPLEX_CST:
12847 {
12848 /* Instantiate any typedefs in the type. */
12849 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12850 r = fold_convert (type, t);
12851 gcc_assert (TREE_CODE (r) == code);
12852 return r;
12853 }
12854
12855 case PTRMEM_CST:
12856 /* These can sometimes show up in a partial instantiation, but never
12857 involve template parms. */
12858 gcc_assert (!uses_template_parms (t));
12859 return t;
12860
12861 default:
12862 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12863 gcc_checking_assert (false);
12864 return t;
12865 }
12866 }
12867
12868 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12869
12870 static tree
12871 tsubst_omp_clauses (tree clauses, bool declare_simd,
12872 tree args, tsubst_flags_t complain, tree in_decl)
12873 {
12874 tree new_clauses = NULL, nc, oc;
12875
12876 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12877 {
12878 nc = copy_node (oc);
12879 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12880 new_clauses = nc;
12881
12882 switch (OMP_CLAUSE_CODE (nc))
12883 {
12884 case OMP_CLAUSE_LASTPRIVATE:
12885 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12886 {
12887 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12888 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12889 in_decl, /*integral_constant_expression_p=*/false);
12890 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12891 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12892 }
12893 /* FALLTHRU */
12894 case OMP_CLAUSE_PRIVATE:
12895 case OMP_CLAUSE_SHARED:
12896 case OMP_CLAUSE_FIRSTPRIVATE:
12897 case OMP_CLAUSE_COPYIN:
12898 case OMP_CLAUSE_COPYPRIVATE:
12899 case OMP_CLAUSE_IF:
12900 case OMP_CLAUSE_NUM_THREADS:
12901 case OMP_CLAUSE_SCHEDULE:
12902 case OMP_CLAUSE_COLLAPSE:
12903 case OMP_CLAUSE_FINAL:
12904 case OMP_CLAUSE_DEPEND:
12905 case OMP_CLAUSE_FROM:
12906 case OMP_CLAUSE_TO:
12907 case OMP_CLAUSE_UNIFORM:
12908 case OMP_CLAUSE_MAP:
12909 case OMP_CLAUSE_DEVICE:
12910 case OMP_CLAUSE_DIST_SCHEDULE:
12911 case OMP_CLAUSE_NUM_TEAMS:
12912 case OMP_CLAUSE_THREAD_LIMIT:
12913 case OMP_CLAUSE_SAFELEN:
12914 case OMP_CLAUSE_SIMDLEN:
12915 OMP_CLAUSE_OPERAND (nc, 0)
12916 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12917 in_decl, /*integral_constant_expression_p=*/false);
12918 break;
12919 case OMP_CLAUSE_REDUCTION:
12920 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12921 {
12922 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12923 if (TREE_CODE (placeholder) == SCOPE_REF)
12924 {
12925 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12926 complain, in_decl);
12927 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12928 = build_qualified_name (NULL_TREE, scope,
12929 TREE_OPERAND (placeholder, 1),
12930 false);
12931 }
12932 else
12933 gcc_assert (identifier_p (placeholder));
12934 }
12935 OMP_CLAUSE_OPERAND (nc, 0)
12936 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12937 in_decl, /*integral_constant_expression_p=*/false);
12938 break;
12939 case OMP_CLAUSE_LINEAR:
12940 case OMP_CLAUSE_ALIGNED:
12941 OMP_CLAUSE_OPERAND (nc, 0)
12942 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12943 in_decl, /*integral_constant_expression_p=*/false);
12944 OMP_CLAUSE_OPERAND (nc, 1)
12945 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12946 in_decl, /*integral_constant_expression_p=*/false);
12947 break;
12948
12949 case OMP_CLAUSE_NOWAIT:
12950 case OMP_CLAUSE_ORDERED:
12951 case OMP_CLAUSE_DEFAULT:
12952 case OMP_CLAUSE_UNTIED:
12953 case OMP_CLAUSE_MERGEABLE:
12954 case OMP_CLAUSE_INBRANCH:
12955 case OMP_CLAUSE_NOTINBRANCH:
12956 case OMP_CLAUSE_PROC_BIND:
12957 case OMP_CLAUSE_FOR:
12958 case OMP_CLAUSE_PARALLEL:
12959 case OMP_CLAUSE_SECTIONS:
12960 case OMP_CLAUSE_TASKGROUP:
12961 break;
12962 default:
12963 gcc_unreachable ();
12964 }
12965 }
12966
12967 new_clauses = nreverse (new_clauses);
12968 if (!declare_simd)
12969 new_clauses = finish_omp_clauses (new_clauses);
12970 return new_clauses;
12971 }
12972
12973 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12974
12975 static tree
12976 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12977 tree in_decl)
12978 {
12979 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12980
12981 tree purpose, value, chain;
12982
12983 if (t == NULL)
12984 return t;
12985
12986 if (TREE_CODE (t) != TREE_LIST)
12987 return tsubst_copy_and_build (t, args, complain, in_decl,
12988 /*function_p=*/false,
12989 /*integral_constant_expression_p=*/false);
12990
12991 if (t == void_list_node)
12992 return t;
12993
12994 purpose = TREE_PURPOSE (t);
12995 if (purpose)
12996 purpose = RECUR (purpose);
12997 value = TREE_VALUE (t);
12998 if (value)
12999 {
13000 if (TREE_CODE (value) != LABEL_DECL)
13001 value = RECUR (value);
13002 else
13003 {
13004 value = lookup_label (DECL_NAME (value));
13005 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13006 TREE_USED (value) = 1;
13007 }
13008 }
13009 chain = TREE_CHAIN (t);
13010 if (chain && chain != void_type_node)
13011 chain = RECUR (chain);
13012 return tree_cons (purpose, value, chain);
13013 #undef RECUR
13014 }
13015
13016 /* Substitute one OMP_FOR iterator. */
13017
13018 static void
13019 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13020 tree condv, tree incrv, tree *clauses,
13021 tree args, tsubst_flags_t complain, tree in_decl,
13022 bool integral_constant_expression_p)
13023 {
13024 #define RECUR(NODE) \
13025 tsubst_expr ((NODE), args, complain, in_decl, \
13026 integral_constant_expression_p)
13027 tree decl, init, cond, incr;
13028 bool init_decl;
13029
13030 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13031 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13032 decl = TREE_OPERAND (init, 0);
13033 init = TREE_OPERAND (init, 1);
13034 /* Do this before substituting into decl to handle 'auto'. */
13035 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13036 init = RECUR (init);
13037 decl = RECUR (decl);
13038 if (init_decl)
13039 {
13040 gcc_assert (!processing_template_decl);
13041 init = DECL_INITIAL (decl);
13042 DECL_INITIAL (decl) = NULL_TREE;
13043 }
13044
13045 gcc_assert (!type_dependent_expression_p (decl));
13046
13047 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13048 {
13049 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13050 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13051 if (TREE_CODE (incr) == MODIFY_EXPR)
13052 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13053 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13054 RECUR (TREE_OPERAND (incr, 1)),
13055 complain);
13056 else
13057 incr = RECUR (incr);
13058 TREE_VEC_ELT (declv, i) = decl;
13059 TREE_VEC_ELT (initv, i) = init;
13060 TREE_VEC_ELT (condv, i) = cond;
13061 TREE_VEC_ELT (incrv, i) = incr;
13062 return;
13063 }
13064
13065 if (init && !init_decl)
13066 {
13067 tree c;
13068 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13069 {
13070 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13071 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13072 && OMP_CLAUSE_DECL (c) == decl)
13073 break;
13074 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13075 && OMP_CLAUSE_DECL (c) == decl)
13076 error ("iteration variable %qD should not be firstprivate", decl);
13077 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13078 && OMP_CLAUSE_DECL (c) == decl)
13079 error ("iteration variable %qD should not be reduction", decl);
13080 }
13081 if (c == NULL)
13082 {
13083 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13084 OMP_CLAUSE_DECL (c) = decl;
13085 c = finish_omp_clauses (c);
13086 if (c)
13087 {
13088 OMP_CLAUSE_CHAIN (c) = *clauses;
13089 *clauses = c;
13090 }
13091 }
13092 }
13093 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13094 if (COMPARISON_CLASS_P (cond))
13095 cond = build2 (TREE_CODE (cond), boolean_type_node,
13096 RECUR (TREE_OPERAND (cond, 0)),
13097 RECUR (TREE_OPERAND (cond, 1)));
13098 else
13099 cond = RECUR (cond);
13100 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13101 switch (TREE_CODE (incr))
13102 {
13103 case PREINCREMENT_EXPR:
13104 case PREDECREMENT_EXPR:
13105 case POSTINCREMENT_EXPR:
13106 case POSTDECREMENT_EXPR:
13107 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13108 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13109 break;
13110 case MODIFY_EXPR:
13111 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13112 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13113 {
13114 tree rhs = TREE_OPERAND (incr, 1);
13115 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13116 RECUR (TREE_OPERAND (incr, 0)),
13117 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13118 RECUR (TREE_OPERAND (rhs, 0)),
13119 RECUR (TREE_OPERAND (rhs, 1))));
13120 }
13121 else
13122 incr = RECUR (incr);
13123 break;
13124 case MODOP_EXPR:
13125 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13126 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13127 {
13128 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13129 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13130 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13131 TREE_TYPE (decl), lhs,
13132 RECUR (TREE_OPERAND (incr, 2))));
13133 }
13134 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13135 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13136 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13137 {
13138 tree rhs = TREE_OPERAND (incr, 2);
13139 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13140 RECUR (TREE_OPERAND (incr, 0)),
13141 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13142 RECUR (TREE_OPERAND (rhs, 0)),
13143 RECUR (TREE_OPERAND (rhs, 1))));
13144 }
13145 else
13146 incr = RECUR (incr);
13147 break;
13148 default:
13149 incr = RECUR (incr);
13150 break;
13151 }
13152
13153 TREE_VEC_ELT (declv, i) = decl;
13154 TREE_VEC_ELT (initv, i) = init;
13155 TREE_VEC_ELT (condv, i) = cond;
13156 TREE_VEC_ELT (incrv, i) = incr;
13157 #undef RECUR
13158 }
13159
13160 /* Like tsubst_copy for expressions, etc. but also does semantic
13161 processing. */
13162
13163 static tree
13164 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13165 bool integral_constant_expression_p)
13166 {
13167 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13168 #define RECUR(NODE) \
13169 tsubst_expr ((NODE), args, complain, in_decl, \
13170 integral_constant_expression_p)
13171
13172 tree stmt, tmp;
13173 tree r;
13174 location_t loc;
13175
13176 if (t == NULL_TREE || t == error_mark_node)
13177 return t;
13178
13179 loc = input_location;
13180 if (EXPR_HAS_LOCATION (t))
13181 input_location = EXPR_LOCATION (t);
13182 if (STATEMENT_CODE_P (TREE_CODE (t)))
13183 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13184
13185 switch (TREE_CODE (t))
13186 {
13187 case STATEMENT_LIST:
13188 {
13189 tree_stmt_iterator i;
13190 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13191 RECUR (tsi_stmt (i));
13192 break;
13193 }
13194
13195 case CTOR_INITIALIZER:
13196 finish_mem_initializers (tsubst_initializer_list
13197 (TREE_OPERAND (t, 0), args));
13198 break;
13199
13200 case RETURN_EXPR:
13201 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13202 break;
13203
13204 case EXPR_STMT:
13205 tmp = RECUR (EXPR_STMT_EXPR (t));
13206 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13207 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13208 else
13209 finish_expr_stmt (tmp);
13210 break;
13211
13212 case USING_STMT:
13213 do_using_directive (USING_STMT_NAMESPACE (t));
13214 break;
13215
13216 case DECL_EXPR:
13217 {
13218 tree decl, pattern_decl;
13219 tree init;
13220
13221 pattern_decl = decl = DECL_EXPR_DECL (t);
13222 if (TREE_CODE (decl) == LABEL_DECL)
13223 finish_label_decl (DECL_NAME (decl));
13224 else if (TREE_CODE (decl) == USING_DECL)
13225 {
13226 tree scope = USING_DECL_SCOPE (decl);
13227 tree name = DECL_NAME (decl);
13228 tree decl;
13229
13230 scope = tsubst (scope, args, complain, in_decl);
13231 decl = lookup_qualified_name (scope, name,
13232 /*is_type_p=*/false,
13233 /*complain=*/false);
13234 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13235 qualified_name_lookup_error (scope, name, decl, input_location);
13236 else
13237 do_local_using_decl (decl, scope, name);
13238 }
13239 else if (DECL_PACK_P (decl))
13240 {
13241 /* Don't build up decls for a variadic capture proxy, we'll
13242 instantiate the elements directly as needed. */
13243 break;
13244 }
13245 else
13246 {
13247 init = DECL_INITIAL (decl);
13248 decl = tsubst (decl, args, complain, in_decl);
13249 if (decl != error_mark_node)
13250 {
13251 /* By marking the declaration as instantiated, we avoid
13252 trying to instantiate it. Since instantiate_decl can't
13253 handle local variables, and since we've already done
13254 all that needs to be done, that's the right thing to
13255 do. */
13256 if (VAR_P (decl))
13257 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13258 if (VAR_P (decl)
13259 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13260 /* Anonymous aggregates are a special case. */
13261 finish_anon_union (decl);
13262 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13263 {
13264 DECL_CONTEXT (decl) = current_function_decl;
13265 if (DECL_NAME (decl) == this_identifier)
13266 {
13267 tree lam = DECL_CONTEXT (current_function_decl);
13268 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13269 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13270 }
13271 insert_capture_proxy (decl);
13272 }
13273 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13274 /* We already did a pushtag. */;
13275 else if (TREE_CODE (decl) == FUNCTION_DECL
13276 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13277 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13278 {
13279 DECL_CONTEXT (decl) = NULL_TREE;
13280 pushdecl (decl);
13281 DECL_CONTEXT (decl) = current_function_decl;
13282 cp_check_omp_declare_reduction (decl);
13283 }
13284 else
13285 {
13286 int const_init = false;
13287 maybe_push_decl (decl);
13288 if (VAR_P (decl)
13289 && DECL_PRETTY_FUNCTION_P (decl))
13290 {
13291 /* For __PRETTY_FUNCTION__ we have to adjust the
13292 initializer. */
13293 const char *const name
13294 = cxx_printable_name (current_function_decl, 2);
13295 init = cp_fname_init (name, &TREE_TYPE (decl));
13296 }
13297 else
13298 {
13299 tree t = RECUR (init);
13300
13301 if (init && !t)
13302 {
13303 /* If we had an initializer but it
13304 instantiated to nothing,
13305 value-initialize the object. This will
13306 only occur when the initializer was a
13307 pack expansion where the parameter packs
13308 used in that expansion were of length
13309 zero. */
13310 init = build_value_init (TREE_TYPE (decl),
13311 complain);
13312 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13313 init = get_target_expr_sfinae (init, complain);
13314 }
13315 else
13316 init = t;
13317 }
13318
13319 if (VAR_P (decl))
13320 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13321 (pattern_decl));
13322 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13323 }
13324 }
13325 }
13326
13327 break;
13328 }
13329
13330 case FOR_STMT:
13331 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13332 RECUR (FOR_INIT_STMT (t));
13333 finish_for_init_stmt (stmt);
13334 tmp = RECUR (FOR_COND (t));
13335 finish_for_cond (tmp, stmt, false);
13336 tmp = RECUR (FOR_EXPR (t));
13337 finish_for_expr (tmp, stmt);
13338 RECUR (FOR_BODY (t));
13339 finish_for_stmt (stmt);
13340 break;
13341
13342 case RANGE_FOR_STMT:
13343 {
13344 tree decl, expr;
13345 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13346 decl = RANGE_FOR_DECL (t);
13347 decl = tsubst (decl, args, complain, in_decl);
13348 maybe_push_decl (decl);
13349 expr = RECUR (RANGE_FOR_EXPR (t));
13350 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13351 RECUR (RANGE_FOR_BODY (t));
13352 finish_for_stmt (stmt);
13353 }
13354 break;
13355
13356 case WHILE_STMT:
13357 stmt = begin_while_stmt ();
13358 tmp = RECUR (WHILE_COND (t));
13359 finish_while_stmt_cond (tmp, stmt, false);
13360 RECUR (WHILE_BODY (t));
13361 finish_while_stmt (stmt);
13362 break;
13363
13364 case DO_STMT:
13365 stmt = begin_do_stmt ();
13366 RECUR (DO_BODY (t));
13367 finish_do_body (stmt);
13368 tmp = RECUR (DO_COND (t));
13369 finish_do_stmt (tmp, stmt, false);
13370 break;
13371
13372 case IF_STMT:
13373 stmt = begin_if_stmt ();
13374 tmp = RECUR (IF_COND (t));
13375 finish_if_stmt_cond (tmp, stmt);
13376 RECUR (THEN_CLAUSE (t));
13377 finish_then_clause (stmt);
13378
13379 if (ELSE_CLAUSE (t))
13380 {
13381 begin_else_clause (stmt);
13382 RECUR (ELSE_CLAUSE (t));
13383 finish_else_clause (stmt);
13384 }
13385
13386 finish_if_stmt (stmt);
13387 break;
13388
13389 case BIND_EXPR:
13390 if (BIND_EXPR_BODY_BLOCK (t))
13391 stmt = begin_function_body ();
13392 else
13393 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13394 ? BCS_TRY_BLOCK : 0);
13395
13396 RECUR (BIND_EXPR_BODY (t));
13397
13398 if (BIND_EXPR_BODY_BLOCK (t))
13399 finish_function_body (stmt);
13400 else
13401 finish_compound_stmt (stmt);
13402 break;
13403
13404 case BREAK_STMT:
13405 finish_break_stmt ();
13406 break;
13407
13408 case CONTINUE_STMT:
13409 finish_continue_stmt ();
13410 break;
13411
13412 case SWITCH_STMT:
13413 stmt = begin_switch_stmt ();
13414 tmp = RECUR (SWITCH_STMT_COND (t));
13415 finish_switch_cond (tmp, stmt);
13416 RECUR (SWITCH_STMT_BODY (t));
13417 finish_switch_stmt (stmt);
13418 break;
13419
13420 case CASE_LABEL_EXPR:
13421 finish_case_label (EXPR_LOCATION (t),
13422 RECUR (CASE_LOW (t)),
13423 RECUR (CASE_HIGH (t)));
13424 break;
13425
13426 case LABEL_EXPR:
13427 {
13428 tree decl = LABEL_EXPR_LABEL (t);
13429 tree label;
13430
13431 label = finish_label_stmt (DECL_NAME (decl));
13432 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13433 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13434 }
13435 break;
13436
13437 case GOTO_EXPR:
13438 tmp = GOTO_DESTINATION (t);
13439 if (TREE_CODE (tmp) != LABEL_DECL)
13440 /* Computed goto's must be tsubst'd into. On the other hand,
13441 non-computed gotos must not be; the identifier in question
13442 will have no binding. */
13443 tmp = RECUR (tmp);
13444 else
13445 tmp = DECL_NAME (tmp);
13446 finish_goto_stmt (tmp);
13447 break;
13448
13449 case ASM_EXPR:
13450 tmp = finish_asm_stmt
13451 (ASM_VOLATILE_P (t),
13452 RECUR (ASM_STRING (t)),
13453 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13454 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13455 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13456 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13457 {
13458 tree asm_expr = tmp;
13459 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13460 asm_expr = TREE_OPERAND (asm_expr, 0);
13461 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13462 }
13463 break;
13464
13465 case TRY_BLOCK:
13466 if (CLEANUP_P (t))
13467 {
13468 stmt = begin_try_block ();
13469 RECUR (TRY_STMTS (t));
13470 finish_cleanup_try_block (stmt);
13471 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13472 }
13473 else
13474 {
13475 tree compound_stmt = NULL_TREE;
13476
13477 if (FN_TRY_BLOCK_P (t))
13478 stmt = begin_function_try_block (&compound_stmt);
13479 else
13480 stmt = begin_try_block ();
13481
13482 RECUR (TRY_STMTS (t));
13483
13484 if (FN_TRY_BLOCK_P (t))
13485 finish_function_try_block (stmt);
13486 else
13487 finish_try_block (stmt);
13488
13489 RECUR (TRY_HANDLERS (t));
13490 if (FN_TRY_BLOCK_P (t))
13491 finish_function_handler_sequence (stmt, compound_stmt);
13492 else
13493 finish_handler_sequence (stmt);
13494 }
13495 break;
13496
13497 case HANDLER:
13498 {
13499 tree decl = HANDLER_PARMS (t);
13500
13501 if (decl)
13502 {
13503 decl = tsubst (decl, args, complain, in_decl);
13504 /* Prevent instantiate_decl from trying to instantiate
13505 this variable. We've already done all that needs to be
13506 done. */
13507 if (decl != error_mark_node)
13508 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13509 }
13510 stmt = begin_handler ();
13511 finish_handler_parms (decl, stmt);
13512 RECUR (HANDLER_BODY (t));
13513 finish_handler (stmt);
13514 }
13515 break;
13516
13517 case TAG_DEFN:
13518 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13519 if (CLASS_TYPE_P (tmp))
13520 {
13521 /* Local classes are not independent templates; they are
13522 instantiated along with their containing function. And this
13523 way we don't have to deal with pushing out of one local class
13524 to instantiate a member of another local class. */
13525 tree fn;
13526 /* Closures are handled by the LAMBDA_EXPR. */
13527 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13528 complete_type (tmp);
13529 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13530 if (!DECL_ARTIFICIAL (fn))
13531 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13532 }
13533 break;
13534
13535 case STATIC_ASSERT:
13536 {
13537 tree condition;
13538
13539 ++c_inhibit_evaluation_warnings;
13540 condition =
13541 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13542 args,
13543 complain, in_decl,
13544 /*integral_constant_expression_p=*/true);
13545 --c_inhibit_evaluation_warnings;
13546
13547 finish_static_assert (condition,
13548 STATIC_ASSERT_MESSAGE (t),
13549 STATIC_ASSERT_SOURCE_LOCATION (t),
13550 /*member_p=*/false);
13551 }
13552 break;
13553
13554 case OMP_PARALLEL:
13555 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13556 args, complain, in_decl);
13557 stmt = begin_omp_parallel ();
13558 RECUR (OMP_PARALLEL_BODY (t));
13559 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13560 = OMP_PARALLEL_COMBINED (t);
13561 break;
13562
13563 case OMP_TASK:
13564 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13565 args, complain, in_decl);
13566 stmt = begin_omp_task ();
13567 RECUR (OMP_TASK_BODY (t));
13568 finish_omp_task (tmp, stmt);
13569 break;
13570
13571 case OMP_FOR:
13572 case OMP_SIMD:
13573 case CILK_SIMD:
13574 case OMP_DISTRIBUTE:
13575 {
13576 tree clauses, body, pre_body;
13577 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13578 tree incrv = NULL_TREE;
13579 int i;
13580
13581 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13582 args, complain, in_decl);
13583 if (OMP_FOR_INIT (t) != NULL_TREE)
13584 {
13585 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13586 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13587 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13588 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13589 }
13590
13591 stmt = begin_omp_structured_block ();
13592
13593 pre_body = push_stmt_list ();
13594 RECUR (OMP_FOR_PRE_BODY (t));
13595 pre_body = pop_stmt_list (pre_body);
13596
13597 if (OMP_FOR_INIT (t) != NULL_TREE)
13598 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13599 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13600 &clauses, args, complain, in_decl,
13601 integral_constant_expression_p);
13602
13603 body = push_stmt_list ();
13604 RECUR (OMP_FOR_BODY (t));
13605 body = pop_stmt_list (body);
13606
13607 if (OMP_FOR_INIT (t) != NULL_TREE)
13608 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13609 condv, incrv, body, pre_body, clauses);
13610 else
13611 {
13612 t = make_node (TREE_CODE (t));
13613 TREE_TYPE (t) = void_type_node;
13614 OMP_FOR_BODY (t) = body;
13615 OMP_FOR_PRE_BODY (t) = pre_body;
13616 OMP_FOR_CLAUSES (t) = clauses;
13617 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13618 add_stmt (t);
13619 }
13620
13621 add_stmt (finish_omp_structured_block (stmt));
13622 }
13623 break;
13624
13625 case OMP_SECTIONS:
13626 case OMP_SINGLE:
13627 case OMP_TEAMS:
13628 case OMP_TARGET_DATA:
13629 case OMP_TARGET:
13630 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13631 args, complain, in_decl);
13632 stmt = push_stmt_list ();
13633 RECUR (OMP_BODY (t));
13634 stmt = pop_stmt_list (stmt);
13635
13636 t = copy_node (t);
13637 OMP_BODY (t) = stmt;
13638 OMP_CLAUSES (t) = tmp;
13639 add_stmt (t);
13640 break;
13641
13642 case OMP_TARGET_UPDATE:
13643 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13644 args, complain, in_decl);
13645 t = copy_node (t);
13646 OMP_CLAUSES (t) = tmp;
13647 add_stmt (t);
13648 break;
13649
13650 case OMP_SECTION:
13651 case OMP_CRITICAL:
13652 case OMP_MASTER:
13653 case OMP_TASKGROUP:
13654 case OMP_ORDERED:
13655 stmt = push_stmt_list ();
13656 RECUR (OMP_BODY (t));
13657 stmt = pop_stmt_list (stmt);
13658
13659 t = copy_node (t);
13660 OMP_BODY (t) = stmt;
13661 add_stmt (t);
13662 break;
13663
13664 case OMP_ATOMIC:
13665 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13666 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13667 {
13668 tree op1 = TREE_OPERAND (t, 1);
13669 tree rhs1 = NULL_TREE;
13670 tree lhs, rhs;
13671 if (TREE_CODE (op1) == COMPOUND_EXPR)
13672 {
13673 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13674 op1 = TREE_OPERAND (op1, 1);
13675 }
13676 lhs = RECUR (TREE_OPERAND (op1, 0));
13677 rhs = RECUR (TREE_OPERAND (op1, 1));
13678 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13679 NULL_TREE, NULL_TREE, rhs1,
13680 OMP_ATOMIC_SEQ_CST (t));
13681 }
13682 else
13683 {
13684 tree op1 = TREE_OPERAND (t, 1);
13685 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13686 tree rhs1 = NULL_TREE;
13687 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13688 enum tree_code opcode = NOP_EXPR;
13689 if (code == OMP_ATOMIC_READ)
13690 {
13691 v = RECUR (TREE_OPERAND (op1, 0));
13692 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13693 }
13694 else if (code == OMP_ATOMIC_CAPTURE_OLD
13695 || code == OMP_ATOMIC_CAPTURE_NEW)
13696 {
13697 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13698 v = RECUR (TREE_OPERAND (op1, 0));
13699 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13700 if (TREE_CODE (op11) == COMPOUND_EXPR)
13701 {
13702 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13703 op11 = TREE_OPERAND (op11, 1);
13704 }
13705 lhs = RECUR (TREE_OPERAND (op11, 0));
13706 rhs = RECUR (TREE_OPERAND (op11, 1));
13707 opcode = TREE_CODE (op11);
13708 if (opcode == MODIFY_EXPR)
13709 opcode = NOP_EXPR;
13710 }
13711 else
13712 {
13713 code = OMP_ATOMIC;
13714 lhs = RECUR (TREE_OPERAND (op1, 0));
13715 rhs = RECUR (TREE_OPERAND (op1, 1));
13716 }
13717 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13718 OMP_ATOMIC_SEQ_CST (t));
13719 }
13720 break;
13721
13722 case TRANSACTION_EXPR:
13723 {
13724 int flags = 0;
13725 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13726 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13727
13728 if (TRANSACTION_EXPR_IS_STMT (t))
13729 {
13730 tree body = TRANSACTION_EXPR_BODY (t);
13731 tree noex = NULL_TREE;
13732 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13733 {
13734 noex = MUST_NOT_THROW_COND (body);
13735 if (noex == NULL_TREE)
13736 noex = boolean_true_node;
13737 body = TREE_OPERAND (body, 0);
13738 }
13739 stmt = begin_transaction_stmt (input_location, NULL, flags);
13740 RECUR (body);
13741 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13742 }
13743 else
13744 {
13745 stmt = build_transaction_expr (EXPR_LOCATION (t),
13746 RECUR (TRANSACTION_EXPR_BODY (t)),
13747 flags, NULL_TREE);
13748 RETURN (stmt);
13749 }
13750 }
13751 break;
13752
13753 case MUST_NOT_THROW_EXPR:
13754 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13755 RECUR (MUST_NOT_THROW_COND (t))));
13756
13757 case EXPR_PACK_EXPANSION:
13758 error ("invalid use of pack expansion expression");
13759 RETURN (error_mark_node);
13760
13761 case NONTYPE_ARGUMENT_PACK:
13762 error ("use %<...%> to expand argument pack");
13763 RETURN (error_mark_node);
13764
13765 case COMPOUND_EXPR:
13766 tmp = RECUR (TREE_OPERAND (t, 0));
13767 if (tmp == NULL_TREE)
13768 /* If the first operand was a statement, we're done with it. */
13769 RETURN (RECUR (TREE_OPERAND (t, 1)));
13770 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13771 RECUR (TREE_OPERAND (t, 1)),
13772 complain));
13773
13774 default:
13775 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13776
13777 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13778 /*function_p=*/false,
13779 integral_constant_expression_p));
13780 }
13781
13782 RETURN (NULL_TREE);
13783 out:
13784 input_location = loc;
13785 return r;
13786 #undef RECUR
13787 #undef RETURN
13788 }
13789
13790 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13791 function. For description of the body see comment above
13792 cp_parser_omp_declare_reduction_exprs. */
13793
13794 static void
13795 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13796 {
13797 if (t == NULL_TREE || t == error_mark_node)
13798 return;
13799
13800 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13801
13802 tree_stmt_iterator tsi;
13803 int i;
13804 tree stmts[7];
13805 memset (stmts, 0, sizeof stmts);
13806 for (i = 0, tsi = tsi_start (t);
13807 i < 7 && !tsi_end_p (tsi);
13808 i++, tsi_next (&tsi))
13809 stmts[i] = tsi_stmt (tsi);
13810 gcc_assert (tsi_end_p (tsi));
13811
13812 if (i >= 3)
13813 {
13814 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13815 && TREE_CODE (stmts[1]) == DECL_EXPR);
13816 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13817 args, complain, in_decl);
13818 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13819 args, complain, in_decl);
13820 DECL_CONTEXT (omp_out) = current_function_decl;
13821 DECL_CONTEXT (omp_in) = current_function_decl;
13822 keep_next_level (true);
13823 tree block = begin_omp_structured_block ();
13824 tsubst_expr (stmts[2], args, complain, in_decl, false);
13825 block = finish_omp_structured_block (block);
13826 block = maybe_cleanup_point_expr_void (block);
13827 add_decl_expr (omp_out);
13828 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13829 TREE_NO_WARNING (omp_out) = 1;
13830 add_decl_expr (omp_in);
13831 finish_expr_stmt (block);
13832 }
13833 if (i >= 6)
13834 {
13835 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13836 && TREE_CODE (stmts[4]) == DECL_EXPR);
13837 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13838 args, complain, in_decl);
13839 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13840 args, complain, in_decl);
13841 DECL_CONTEXT (omp_priv) = current_function_decl;
13842 DECL_CONTEXT (omp_orig) = current_function_decl;
13843 keep_next_level (true);
13844 tree block = begin_omp_structured_block ();
13845 tsubst_expr (stmts[5], args, complain, in_decl, false);
13846 block = finish_omp_structured_block (block);
13847 block = maybe_cleanup_point_expr_void (block);
13848 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13849 add_decl_expr (omp_priv);
13850 add_decl_expr (omp_orig);
13851 finish_expr_stmt (block);
13852 if (i == 7)
13853 add_decl_expr (omp_orig);
13854 }
13855 }
13856
13857 /* T is a postfix-expression that is not being used in a function
13858 call. Return the substituted version of T. */
13859
13860 static tree
13861 tsubst_non_call_postfix_expression (tree t, tree args,
13862 tsubst_flags_t complain,
13863 tree in_decl)
13864 {
13865 if (TREE_CODE (t) == SCOPE_REF)
13866 t = tsubst_qualified_id (t, args, complain, in_decl,
13867 /*done=*/false, /*address_p=*/false);
13868 else
13869 t = tsubst_copy_and_build (t, args, complain, in_decl,
13870 /*function_p=*/false,
13871 /*integral_constant_expression_p=*/false);
13872
13873 return t;
13874 }
13875
13876 /* Like tsubst but deals with expressions and performs semantic
13877 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13878
13879 tree
13880 tsubst_copy_and_build (tree t,
13881 tree args,
13882 tsubst_flags_t complain,
13883 tree in_decl,
13884 bool function_p,
13885 bool integral_constant_expression_p)
13886 {
13887 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13888 #define RECUR(NODE) \
13889 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13890 /*function_p=*/false, \
13891 integral_constant_expression_p)
13892
13893 tree retval, op1;
13894 location_t loc;
13895
13896 if (t == NULL_TREE || t == error_mark_node)
13897 return t;
13898
13899 loc = input_location;
13900 if (EXPR_HAS_LOCATION (t))
13901 input_location = EXPR_LOCATION (t);
13902
13903 /* N3276 decltype magic only applies to calls at the top level or on the
13904 right side of a comma. */
13905 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13906 complain &= ~tf_decltype;
13907
13908 switch (TREE_CODE (t))
13909 {
13910 case USING_DECL:
13911 t = DECL_NAME (t);
13912 /* Fall through. */
13913 case IDENTIFIER_NODE:
13914 {
13915 tree decl;
13916 cp_id_kind idk;
13917 bool non_integral_constant_expression_p;
13918 const char *error_msg;
13919
13920 if (IDENTIFIER_TYPENAME_P (t))
13921 {
13922 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13923 t = mangle_conv_op_name_for_type (new_type);
13924 }
13925
13926 /* Look up the name. */
13927 decl = lookup_name (t);
13928
13929 /* By convention, expressions use ERROR_MARK_NODE to indicate
13930 failure, not NULL_TREE. */
13931 if (decl == NULL_TREE)
13932 decl = error_mark_node;
13933
13934 decl = finish_id_expression (t, decl, NULL_TREE,
13935 &idk,
13936 integral_constant_expression_p,
13937 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13938 &non_integral_constant_expression_p,
13939 /*template_p=*/false,
13940 /*done=*/true,
13941 /*address_p=*/false,
13942 /*template_arg_p=*/false,
13943 &error_msg,
13944 input_location);
13945 if (error_msg)
13946 error (error_msg);
13947 if (!function_p && identifier_p (decl))
13948 {
13949 if (complain & tf_error)
13950 unqualified_name_lookup_error (decl);
13951 decl = error_mark_node;
13952 }
13953 RETURN (decl);
13954 }
13955
13956 case TEMPLATE_ID_EXPR:
13957 {
13958 tree object;
13959 tree templ = RECUR (TREE_OPERAND (t, 0));
13960 tree targs = TREE_OPERAND (t, 1);
13961
13962 if (targs)
13963 targs = tsubst_template_args (targs, args, complain, in_decl);
13964
13965 if (TREE_CODE (templ) == COMPONENT_REF)
13966 {
13967 object = TREE_OPERAND (templ, 0);
13968 templ = TREE_OPERAND (templ, 1);
13969 }
13970 else
13971 object = NULL_TREE;
13972 templ = lookup_template_function (templ, targs);
13973
13974 if (object)
13975 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13976 object, templ, NULL_TREE));
13977 else
13978 RETURN (baselink_for_fns (templ));
13979 }
13980
13981 case INDIRECT_REF:
13982 {
13983 tree r = RECUR (TREE_OPERAND (t, 0));
13984
13985 if (REFERENCE_REF_P (t))
13986 {
13987 /* A type conversion to reference type will be enclosed in
13988 such an indirect ref, but the substitution of the cast
13989 will have also added such an indirect ref. */
13990 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13991 r = convert_from_reference (r);
13992 }
13993 else
13994 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13995 complain|decltype_flag);
13996 RETURN (r);
13997 }
13998
13999 case NOP_EXPR:
14000 RETURN (build_nop
14001 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14002 RECUR (TREE_OPERAND (t, 0))));
14003
14004 case IMPLICIT_CONV_EXPR:
14005 {
14006 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14007 tree expr = RECUR (TREE_OPERAND (t, 0));
14008 int flags = LOOKUP_IMPLICIT;
14009 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14010 flags = LOOKUP_NORMAL;
14011 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14012 flags));
14013 }
14014
14015 case CONVERT_EXPR:
14016 RETURN (build1
14017 (CONVERT_EXPR,
14018 tsubst (TREE_TYPE (t), args, complain, in_decl),
14019 RECUR (TREE_OPERAND (t, 0))));
14020
14021 case CAST_EXPR:
14022 case REINTERPRET_CAST_EXPR:
14023 case CONST_CAST_EXPR:
14024 case DYNAMIC_CAST_EXPR:
14025 case STATIC_CAST_EXPR:
14026 {
14027 tree type;
14028 tree op, r = NULL_TREE;
14029
14030 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14031 if (integral_constant_expression_p
14032 && !cast_valid_in_integral_constant_expression_p (type))
14033 {
14034 if (complain & tf_error)
14035 error ("a cast to a type other than an integral or "
14036 "enumeration type cannot appear in a constant-expression");
14037 RETURN (error_mark_node);
14038 }
14039
14040 op = RECUR (TREE_OPERAND (t, 0));
14041
14042 ++c_inhibit_evaluation_warnings;
14043 switch (TREE_CODE (t))
14044 {
14045 case CAST_EXPR:
14046 r = build_functional_cast (type, op, complain);
14047 break;
14048 case REINTERPRET_CAST_EXPR:
14049 r = build_reinterpret_cast (type, op, complain);
14050 break;
14051 case CONST_CAST_EXPR:
14052 r = build_const_cast (type, op, complain);
14053 break;
14054 case DYNAMIC_CAST_EXPR:
14055 r = build_dynamic_cast (type, op, complain);
14056 break;
14057 case STATIC_CAST_EXPR:
14058 r = build_static_cast (type, op, complain);
14059 break;
14060 default:
14061 gcc_unreachable ();
14062 }
14063 --c_inhibit_evaluation_warnings;
14064
14065 RETURN (r);
14066 }
14067
14068 case POSTDECREMENT_EXPR:
14069 case POSTINCREMENT_EXPR:
14070 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14071 args, complain, in_decl);
14072 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14073 complain|decltype_flag));
14074
14075 case PREDECREMENT_EXPR:
14076 case PREINCREMENT_EXPR:
14077 case NEGATE_EXPR:
14078 case BIT_NOT_EXPR:
14079 case ABS_EXPR:
14080 case TRUTH_NOT_EXPR:
14081 case UNARY_PLUS_EXPR: /* Unary + */
14082 case REALPART_EXPR:
14083 case IMAGPART_EXPR:
14084 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14085 RECUR (TREE_OPERAND (t, 0)),
14086 complain|decltype_flag));
14087
14088 case FIX_TRUNC_EXPR:
14089 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14090 0, complain));
14091
14092 case ADDR_EXPR:
14093 op1 = TREE_OPERAND (t, 0);
14094 if (TREE_CODE (op1) == LABEL_DECL)
14095 RETURN (finish_label_address_expr (DECL_NAME (op1),
14096 EXPR_LOCATION (op1)));
14097 if (TREE_CODE (op1) == SCOPE_REF)
14098 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14099 /*done=*/true, /*address_p=*/true);
14100 else
14101 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14102 in_decl);
14103 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14104 complain|decltype_flag));
14105
14106 case PLUS_EXPR:
14107 case MINUS_EXPR:
14108 case MULT_EXPR:
14109 case TRUNC_DIV_EXPR:
14110 case CEIL_DIV_EXPR:
14111 case FLOOR_DIV_EXPR:
14112 case ROUND_DIV_EXPR:
14113 case EXACT_DIV_EXPR:
14114 case BIT_AND_EXPR:
14115 case BIT_IOR_EXPR:
14116 case BIT_XOR_EXPR:
14117 case TRUNC_MOD_EXPR:
14118 case FLOOR_MOD_EXPR:
14119 case TRUTH_ANDIF_EXPR:
14120 case TRUTH_ORIF_EXPR:
14121 case TRUTH_AND_EXPR:
14122 case TRUTH_OR_EXPR:
14123 case RSHIFT_EXPR:
14124 case LSHIFT_EXPR:
14125 case RROTATE_EXPR:
14126 case LROTATE_EXPR:
14127 case EQ_EXPR:
14128 case NE_EXPR:
14129 case MAX_EXPR:
14130 case MIN_EXPR:
14131 case LE_EXPR:
14132 case GE_EXPR:
14133 case LT_EXPR:
14134 case GT_EXPR:
14135 case MEMBER_REF:
14136 case DOTSTAR_EXPR:
14137 {
14138 tree r;
14139
14140 ++c_inhibit_evaluation_warnings;
14141
14142 r = build_x_binary_op
14143 (input_location, TREE_CODE (t),
14144 RECUR (TREE_OPERAND (t, 0)),
14145 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14146 ? ERROR_MARK
14147 : TREE_CODE (TREE_OPERAND (t, 0))),
14148 RECUR (TREE_OPERAND (t, 1)),
14149 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14150 ? ERROR_MARK
14151 : TREE_CODE (TREE_OPERAND (t, 1))),
14152 /*overload=*/NULL,
14153 complain|decltype_flag);
14154 if (EXPR_P (r) && TREE_NO_WARNING (t))
14155 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14156
14157 --c_inhibit_evaluation_warnings;
14158
14159 RETURN (r);
14160 }
14161
14162 case SCOPE_REF:
14163 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14164 /*address_p=*/false));
14165 case ARRAY_REF:
14166 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14167 args, complain, in_decl);
14168 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14169 RECUR (TREE_OPERAND (t, 1)),
14170 complain|decltype_flag));
14171
14172 case ARRAY_NOTATION_REF:
14173 {
14174 tree start_index, length, stride;
14175 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14176 args, complain, in_decl);
14177 start_index = RECUR (ARRAY_NOTATION_START (t));
14178 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14179 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14180 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14181 length, stride, TREE_TYPE (op1)));
14182 }
14183 case SIZEOF_EXPR:
14184 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14185 RETURN (tsubst_copy (t, args, complain, in_decl));
14186 /* Fall through */
14187
14188 case ALIGNOF_EXPR:
14189 {
14190 tree r;
14191
14192 op1 = TREE_OPERAND (t, 0);
14193 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14194 op1 = TREE_TYPE (op1);
14195 if (!args)
14196 {
14197 /* When there are no ARGS, we are trying to evaluate a
14198 non-dependent expression from the parser. Trying to do
14199 the substitutions may not work. */
14200 if (!TYPE_P (op1))
14201 op1 = TREE_TYPE (op1);
14202 }
14203 else
14204 {
14205 ++cp_unevaluated_operand;
14206 ++c_inhibit_evaluation_warnings;
14207 if (TYPE_P (op1))
14208 op1 = tsubst (op1, args, complain, in_decl);
14209 else
14210 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14211 /*function_p=*/false,
14212 /*integral_constant_expression_p=*/
14213 false);
14214 --cp_unevaluated_operand;
14215 --c_inhibit_evaluation_warnings;
14216 }
14217 if (TYPE_P (op1))
14218 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14219 complain & tf_error);
14220 else
14221 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14222 complain & tf_error);
14223 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14224 {
14225 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14226 {
14227 if (!processing_template_decl && TYPE_P (op1))
14228 {
14229 r = build_min (SIZEOF_EXPR, size_type_node,
14230 build1 (NOP_EXPR, op1, error_mark_node));
14231 SIZEOF_EXPR_TYPE_P (r) = 1;
14232 }
14233 else
14234 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14235 TREE_SIDE_EFFECTS (r) = 0;
14236 TREE_READONLY (r) = 1;
14237 }
14238 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14239 }
14240 RETURN (r);
14241 }
14242
14243 case AT_ENCODE_EXPR:
14244 {
14245 op1 = TREE_OPERAND (t, 0);
14246 ++cp_unevaluated_operand;
14247 ++c_inhibit_evaluation_warnings;
14248 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14249 /*function_p=*/false,
14250 /*integral_constant_expression_p=*/false);
14251 --cp_unevaluated_operand;
14252 --c_inhibit_evaluation_warnings;
14253 RETURN (objc_build_encode_expr (op1));
14254 }
14255
14256 case NOEXCEPT_EXPR:
14257 op1 = TREE_OPERAND (t, 0);
14258 ++cp_unevaluated_operand;
14259 ++c_inhibit_evaluation_warnings;
14260 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14261 /*function_p=*/false,
14262 /*integral_constant_expression_p=*/false);
14263 --cp_unevaluated_operand;
14264 --c_inhibit_evaluation_warnings;
14265 RETURN (finish_noexcept_expr (op1, complain));
14266
14267 case MODOP_EXPR:
14268 {
14269 tree r;
14270
14271 ++c_inhibit_evaluation_warnings;
14272
14273 r = build_x_modify_expr
14274 (EXPR_LOCATION (t),
14275 RECUR (TREE_OPERAND (t, 0)),
14276 TREE_CODE (TREE_OPERAND (t, 1)),
14277 RECUR (TREE_OPERAND (t, 2)),
14278 complain|decltype_flag);
14279 /* TREE_NO_WARNING must be set if either the expression was
14280 parenthesized or it uses an operator such as >>= rather
14281 than plain assignment. In the former case, it was already
14282 set and must be copied. In the latter case,
14283 build_x_modify_expr sets it and it must not be reset
14284 here. */
14285 if (TREE_NO_WARNING (t))
14286 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14287
14288 --c_inhibit_evaluation_warnings;
14289
14290 RETURN (r);
14291 }
14292
14293 case ARROW_EXPR:
14294 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14295 args, complain, in_decl);
14296 /* Remember that there was a reference to this entity. */
14297 if (DECL_P (op1))
14298 mark_used (op1);
14299 RETURN (build_x_arrow (input_location, op1, complain));
14300
14301 case NEW_EXPR:
14302 {
14303 tree placement = RECUR (TREE_OPERAND (t, 0));
14304 tree init = RECUR (TREE_OPERAND (t, 3));
14305 vec<tree, va_gc> *placement_vec;
14306 vec<tree, va_gc> *init_vec;
14307 tree ret;
14308
14309 if (placement == NULL_TREE)
14310 placement_vec = NULL;
14311 else
14312 {
14313 placement_vec = make_tree_vector ();
14314 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14315 vec_safe_push (placement_vec, TREE_VALUE (placement));
14316 }
14317
14318 /* If there was an initializer in the original tree, but it
14319 instantiated to an empty list, then we should pass a
14320 non-NULL empty vector to tell build_new that it was an
14321 empty initializer() rather than no initializer. This can
14322 only happen when the initializer is a pack expansion whose
14323 parameter packs are of length zero. */
14324 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14325 init_vec = NULL;
14326 else
14327 {
14328 init_vec = make_tree_vector ();
14329 if (init == void_zero_node)
14330 gcc_assert (init_vec != NULL);
14331 else
14332 {
14333 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14334 vec_safe_push (init_vec, TREE_VALUE (init));
14335 }
14336 }
14337
14338 ret = build_new (&placement_vec,
14339 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14340 RECUR (TREE_OPERAND (t, 2)),
14341 &init_vec,
14342 NEW_EXPR_USE_GLOBAL (t),
14343 complain);
14344
14345 if (placement_vec != NULL)
14346 release_tree_vector (placement_vec);
14347 if (init_vec != NULL)
14348 release_tree_vector (init_vec);
14349
14350 RETURN (ret);
14351 }
14352
14353 case DELETE_EXPR:
14354 RETURN (delete_sanity
14355 (RECUR (TREE_OPERAND (t, 0)),
14356 RECUR (TREE_OPERAND (t, 1)),
14357 DELETE_EXPR_USE_VEC (t),
14358 DELETE_EXPR_USE_GLOBAL (t),
14359 complain));
14360
14361 case COMPOUND_EXPR:
14362 {
14363 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14364 complain & ~tf_decltype, in_decl,
14365 /*function_p=*/false,
14366 integral_constant_expression_p);
14367 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14368 op0,
14369 RECUR (TREE_OPERAND (t, 1)),
14370 complain|decltype_flag));
14371 }
14372
14373 case CALL_EXPR:
14374 {
14375 tree function;
14376 vec<tree, va_gc> *call_args;
14377 unsigned int nargs, i;
14378 bool qualified_p;
14379 bool koenig_p;
14380 tree ret;
14381
14382 function = CALL_EXPR_FN (t);
14383 /* When we parsed the expression, we determined whether or
14384 not Koenig lookup should be performed. */
14385 koenig_p = KOENIG_LOOKUP_P (t);
14386 if (TREE_CODE (function) == SCOPE_REF)
14387 {
14388 qualified_p = true;
14389 function = tsubst_qualified_id (function, args, complain, in_decl,
14390 /*done=*/false,
14391 /*address_p=*/false);
14392 }
14393 else if (koenig_p && identifier_p (function))
14394 {
14395 /* Do nothing; calling tsubst_copy_and_build on an identifier
14396 would incorrectly perform unqualified lookup again.
14397
14398 Note that we can also have an IDENTIFIER_NODE if the earlier
14399 unqualified lookup found a member function; in that case
14400 koenig_p will be false and we do want to do the lookup
14401 again to find the instantiated member function.
14402
14403 FIXME but doing that causes c++/15272, so we need to stop
14404 using IDENTIFIER_NODE in that situation. */
14405 qualified_p = false;
14406 }
14407 else
14408 {
14409 if (TREE_CODE (function) == COMPONENT_REF)
14410 {
14411 tree op = TREE_OPERAND (function, 1);
14412
14413 qualified_p = (TREE_CODE (op) == SCOPE_REF
14414 || (BASELINK_P (op)
14415 && BASELINK_QUALIFIED_P (op)));
14416 }
14417 else
14418 qualified_p = false;
14419
14420 if (TREE_CODE (function) == ADDR_EXPR
14421 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14422 /* Avoid error about taking the address of a constructor. */
14423 function = TREE_OPERAND (function, 0);
14424
14425 function = tsubst_copy_and_build (function, args, complain,
14426 in_decl,
14427 !qualified_p,
14428 integral_constant_expression_p);
14429
14430 if (BASELINK_P (function))
14431 qualified_p = true;
14432 }
14433
14434 nargs = call_expr_nargs (t);
14435 call_args = make_tree_vector ();
14436 for (i = 0; i < nargs; ++i)
14437 {
14438 tree arg = CALL_EXPR_ARG (t, i);
14439
14440 if (!PACK_EXPANSION_P (arg))
14441 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14442 else
14443 {
14444 /* Expand the pack expansion and push each entry onto
14445 CALL_ARGS. */
14446 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14447 if (TREE_CODE (arg) == TREE_VEC)
14448 {
14449 unsigned int len, j;
14450
14451 len = TREE_VEC_LENGTH (arg);
14452 for (j = 0; j < len; ++j)
14453 {
14454 tree value = TREE_VEC_ELT (arg, j);
14455 if (value != NULL_TREE)
14456 value = convert_from_reference (value);
14457 vec_safe_push (call_args, value);
14458 }
14459 }
14460 else
14461 {
14462 /* A partial substitution. Add one entry. */
14463 vec_safe_push (call_args, arg);
14464 }
14465 }
14466 }
14467
14468 /* We do not perform argument-dependent lookup if normal
14469 lookup finds a non-function, in accordance with the
14470 expected resolution of DR 218. */
14471 if (koenig_p
14472 && ((is_overloaded_fn (function)
14473 /* If lookup found a member function, the Koenig lookup is
14474 not appropriate, even if an unqualified-name was used
14475 to denote the function. */
14476 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14477 || identifier_p (function))
14478 /* Only do this when substitution turns a dependent call
14479 into a non-dependent call. */
14480 && type_dependent_expression_p_push (t)
14481 && !any_type_dependent_arguments_p (call_args))
14482 function = perform_koenig_lookup (function, call_args, false,
14483 tf_none);
14484
14485 if (identifier_p (function)
14486 && !any_type_dependent_arguments_p (call_args))
14487 {
14488 if (koenig_p && (complain & tf_warning_or_error))
14489 {
14490 /* For backwards compatibility and good diagnostics, try
14491 the unqualified lookup again if we aren't in SFINAE
14492 context. */
14493 tree unq = (tsubst_copy_and_build
14494 (function, args, complain, in_decl, true,
14495 integral_constant_expression_p));
14496 if (unq == error_mark_node)
14497 RETURN (error_mark_node);
14498
14499 if (unq != function)
14500 {
14501 tree fn = unq;
14502 if (INDIRECT_REF_P (fn))
14503 fn = TREE_OPERAND (fn, 0);
14504 if (TREE_CODE (fn) == COMPONENT_REF)
14505 fn = TREE_OPERAND (fn, 1);
14506 if (is_overloaded_fn (fn))
14507 fn = get_first_fn (fn);
14508 if (permerror (EXPR_LOC_OR_HERE (t),
14509 "%qD was not declared in this scope, "
14510 "and no declarations were found by "
14511 "argument-dependent lookup at the point "
14512 "of instantiation", function))
14513 {
14514 if (!DECL_P (fn))
14515 /* Can't say anything more. */;
14516 else if (DECL_CLASS_SCOPE_P (fn))
14517 {
14518 inform (EXPR_LOC_OR_HERE (t),
14519 "declarations in dependent base %qT are "
14520 "not found by unqualified lookup",
14521 DECL_CLASS_CONTEXT (fn));
14522 if (current_class_ptr)
14523 inform (EXPR_LOC_OR_HERE (t),
14524 "use %<this->%D%> instead", function);
14525 else
14526 inform (EXPR_LOC_OR_HERE (t),
14527 "use %<%T::%D%> instead",
14528 current_class_name, function);
14529 }
14530 else
14531 inform (0, "%q+D declared here, later in the "
14532 "translation unit", fn);
14533 }
14534 function = unq;
14535 }
14536 }
14537 if (identifier_p (function))
14538 {
14539 if (complain & tf_error)
14540 unqualified_name_lookup_error (function);
14541 release_tree_vector (call_args);
14542 RETURN (error_mark_node);
14543 }
14544 }
14545
14546 /* Remember that there was a reference to this entity. */
14547 if (DECL_P (function))
14548 mark_used (function);
14549
14550 /* Put back tf_decltype for the actual call. */
14551 complain |= decltype_flag;
14552
14553 if (TREE_CODE (function) == OFFSET_REF)
14554 ret = build_offset_ref_call_from_tree (function, &call_args,
14555 complain);
14556 else if (TREE_CODE (function) == COMPONENT_REF)
14557 {
14558 tree instance = TREE_OPERAND (function, 0);
14559 tree fn = TREE_OPERAND (function, 1);
14560
14561 if (processing_template_decl
14562 && (type_dependent_expression_p (instance)
14563 || (!BASELINK_P (fn)
14564 && TREE_CODE (fn) != FIELD_DECL)
14565 || type_dependent_expression_p (fn)
14566 || any_type_dependent_arguments_p (call_args)))
14567 ret = build_nt_call_vec (function, call_args);
14568 else if (!BASELINK_P (fn))
14569 ret = finish_call_expr (function, &call_args,
14570 /*disallow_virtual=*/false,
14571 /*koenig_p=*/false,
14572 complain);
14573 else
14574 ret = (build_new_method_call
14575 (instance, fn,
14576 &call_args, NULL_TREE,
14577 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14578 /*fn_p=*/NULL,
14579 complain));
14580 }
14581 else
14582 ret = finish_call_expr (function, &call_args,
14583 /*disallow_virtual=*/qualified_p,
14584 koenig_p,
14585 complain);
14586
14587 release_tree_vector (call_args);
14588
14589 RETURN (ret);
14590 }
14591
14592 case COND_EXPR:
14593 {
14594 tree cond = RECUR (TREE_OPERAND (t, 0));
14595 tree exp1, exp2;
14596
14597 if (TREE_CODE (cond) == INTEGER_CST)
14598 {
14599 if (integer_zerop (cond))
14600 {
14601 ++c_inhibit_evaluation_warnings;
14602 exp1 = RECUR (TREE_OPERAND (t, 1));
14603 --c_inhibit_evaluation_warnings;
14604 exp2 = RECUR (TREE_OPERAND (t, 2));
14605 }
14606 else
14607 {
14608 exp1 = RECUR (TREE_OPERAND (t, 1));
14609 ++c_inhibit_evaluation_warnings;
14610 exp2 = RECUR (TREE_OPERAND (t, 2));
14611 --c_inhibit_evaluation_warnings;
14612 }
14613 }
14614 else
14615 {
14616 exp1 = RECUR (TREE_OPERAND (t, 1));
14617 exp2 = RECUR (TREE_OPERAND (t, 2));
14618 }
14619
14620 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14621 cond, exp1, exp2, complain));
14622 }
14623
14624 case PSEUDO_DTOR_EXPR:
14625 RETURN (finish_pseudo_destructor_expr
14626 (RECUR (TREE_OPERAND (t, 0)),
14627 RECUR (TREE_OPERAND (t, 1)),
14628 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14629 input_location));
14630
14631 case TREE_LIST:
14632 {
14633 tree purpose, value, chain;
14634
14635 if (t == void_list_node)
14636 RETURN (t);
14637
14638 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14639 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14640 {
14641 /* We have pack expansions, so expand those and
14642 create a new list out of it. */
14643 tree purposevec = NULL_TREE;
14644 tree valuevec = NULL_TREE;
14645 tree chain;
14646 int i, len = -1;
14647
14648 /* Expand the argument expressions. */
14649 if (TREE_PURPOSE (t))
14650 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14651 complain, in_decl);
14652 if (TREE_VALUE (t))
14653 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14654 complain, in_decl);
14655
14656 /* Build the rest of the list. */
14657 chain = TREE_CHAIN (t);
14658 if (chain && chain != void_type_node)
14659 chain = RECUR (chain);
14660
14661 /* Determine the number of arguments. */
14662 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14663 {
14664 len = TREE_VEC_LENGTH (purposevec);
14665 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14666 }
14667 else if (TREE_CODE (valuevec) == TREE_VEC)
14668 len = TREE_VEC_LENGTH (valuevec);
14669 else
14670 {
14671 /* Since we only performed a partial substitution into
14672 the argument pack, we only RETURN (a single list
14673 node. */
14674 if (purposevec == TREE_PURPOSE (t)
14675 && valuevec == TREE_VALUE (t)
14676 && chain == TREE_CHAIN (t))
14677 RETURN (t);
14678
14679 RETURN (tree_cons (purposevec, valuevec, chain));
14680 }
14681
14682 /* Convert the argument vectors into a TREE_LIST */
14683 i = len;
14684 while (i > 0)
14685 {
14686 /* Grab the Ith values. */
14687 i--;
14688 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14689 : NULL_TREE;
14690 value
14691 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14692 : NULL_TREE;
14693
14694 /* Build the list (backwards). */
14695 chain = tree_cons (purpose, value, chain);
14696 }
14697
14698 RETURN (chain);
14699 }
14700
14701 purpose = TREE_PURPOSE (t);
14702 if (purpose)
14703 purpose = RECUR (purpose);
14704 value = TREE_VALUE (t);
14705 if (value)
14706 value = RECUR (value);
14707 chain = TREE_CHAIN (t);
14708 if (chain && chain != void_type_node)
14709 chain = RECUR (chain);
14710 if (purpose == TREE_PURPOSE (t)
14711 && value == TREE_VALUE (t)
14712 && chain == TREE_CHAIN (t))
14713 RETURN (t);
14714 RETURN (tree_cons (purpose, value, chain));
14715 }
14716
14717 case COMPONENT_REF:
14718 {
14719 tree object;
14720 tree object_type;
14721 tree member;
14722
14723 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14724 args, complain, in_decl);
14725 /* Remember that there was a reference to this entity. */
14726 if (DECL_P (object))
14727 mark_used (object);
14728 object_type = TREE_TYPE (object);
14729
14730 member = TREE_OPERAND (t, 1);
14731 if (BASELINK_P (member))
14732 member = tsubst_baselink (member,
14733 non_reference (TREE_TYPE (object)),
14734 args, complain, in_decl);
14735 else
14736 member = tsubst_copy (member, args, complain, in_decl);
14737 if (member == error_mark_node)
14738 RETURN (error_mark_node);
14739
14740 if (type_dependent_expression_p (object))
14741 /* We can't do much here. */;
14742 else if (!CLASS_TYPE_P (object_type))
14743 {
14744 if (scalarish_type_p (object_type))
14745 {
14746 tree s = NULL_TREE;
14747 tree dtor = member;
14748
14749 if (TREE_CODE (dtor) == SCOPE_REF)
14750 {
14751 s = TREE_OPERAND (dtor, 0);
14752 dtor = TREE_OPERAND (dtor, 1);
14753 }
14754 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14755 {
14756 dtor = TREE_OPERAND (dtor, 0);
14757 if (TYPE_P (dtor))
14758 RETURN (finish_pseudo_destructor_expr
14759 (object, s, dtor, input_location));
14760 }
14761 }
14762 }
14763 else if (TREE_CODE (member) == SCOPE_REF
14764 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14765 {
14766 /* Lookup the template functions now that we know what the
14767 scope is. */
14768 tree scope = TREE_OPERAND (member, 0);
14769 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14770 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14771 member = lookup_qualified_name (scope, tmpl,
14772 /*is_type_p=*/false,
14773 /*complain=*/false);
14774 if (BASELINK_P (member))
14775 {
14776 BASELINK_FUNCTIONS (member)
14777 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14778 args);
14779 member = (adjust_result_of_qualified_name_lookup
14780 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14781 object_type));
14782 }
14783 else
14784 {
14785 qualified_name_lookup_error (scope, tmpl, member,
14786 input_location);
14787 RETURN (error_mark_node);
14788 }
14789 }
14790 else if (TREE_CODE (member) == SCOPE_REF
14791 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14792 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14793 {
14794 if (complain & tf_error)
14795 {
14796 if (TYPE_P (TREE_OPERAND (member, 0)))
14797 error ("%qT is not a class or namespace",
14798 TREE_OPERAND (member, 0));
14799 else
14800 error ("%qD is not a class or namespace",
14801 TREE_OPERAND (member, 0));
14802 }
14803 RETURN (error_mark_node);
14804 }
14805 else if (TREE_CODE (member) == FIELD_DECL)
14806 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14807
14808 RETURN (finish_class_member_access_expr (object, member,
14809 /*template_p=*/false,
14810 complain));
14811 }
14812
14813 case THROW_EXPR:
14814 RETURN (build_throw
14815 (RECUR (TREE_OPERAND (t, 0))));
14816
14817 case CONSTRUCTOR:
14818 {
14819 vec<constructor_elt, va_gc> *n;
14820 constructor_elt *ce;
14821 unsigned HOST_WIDE_INT idx;
14822 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14823 bool process_index_p;
14824 int newlen;
14825 bool need_copy_p = false;
14826 tree r;
14827
14828 if (type == error_mark_node)
14829 RETURN (error_mark_node);
14830
14831 /* digest_init will do the wrong thing if we let it. */
14832 if (type && TYPE_PTRMEMFUNC_P (type))
14833 RETURN (t);
14834
14835 /* We do not want to process the index of aggregate
14836 initializers as they are identifier nodes which will be
14837 looked up by digest_init. */
14838 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14839
14840 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14841 newlen = vec_safe_length (n);
14842 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14843 {
14844 if (ce->index && process_index_p
14845 /* An identifier index is looked up in the type
14846 being initialized, not the current scope. */
14847 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14848 ce->index = RECUR (ce->index);
14849
14850 if (PACK_EXPANSION_P (ce->value))
14851 {
14852 /* Substitute into the pack expansion. */
14853 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14854 in_decl);
14855
14856 if (ce->value == error_mark_node
14857 || PACK_EXPANSION_P (ce->value))
14858 ;
14859 else if (TREE_VEC_LENGTH (ce->value) == 1)
14860 /* Just move the argument into place. */
14861 ce->value = TREE_VEC_ELT (ce->value, 0);
14862 else
14863 {
14864 /* Update the length of the final CONSTRUCTOR
14865 arguments vector, and note that we will need to
14866 copy.*/
14867 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14868 need_copy_p = true;
14869 }
14870 }
14871 else
14872 ce->value = RECUR (ce->value);
14873 }
14874
14875 if (need_copy_p)
14876 {
14877 vec<constructor_elt, va_gc> *old_n = n;
14878
14879 vec_alloc (n, newlen);
14880 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14881 {
14882 if (TREE_CODE (ce->value) == TREE_VEC)
14883 {
14884 int i, len = TREE_VEC_LENGTH (ce->value);
14885 for (i = 0; i < len; ++i)
14886 CONSTRUCTOR_APPEND_ELT (n, 0,
14887 TREE_VEC_ELT (ce->value, i));
14888 }
14889 else
14890 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14891 }
14892 }
14893
14894 r = build_constructor (init_list_type_node, n);
14895 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14896
14897 if (TREE_HAS_CONSTRUCTOR (t))
14898 RETURN (finish_compound_literal (type, r, complain));
14899
14900 TREE_TYPE (r) = type;
14901 RETURN (r);
14902 }
14903
14904 case TYPEID_EXPR:
14905 {
14906 tree operand_0 = TREE_OPERAND (t, 0);
14907 if (TYPE_P (operand_0))
14908 {
14909 operand_0 = tsubst (operand_0, args, complain, in_decl);
14910 RETURN (get_typeid (operand_0, complain));
14911 }
14912 else
14913 {
14914 operand_0 = RECUR (operand_0);
14915 RETURN (build_typeid (operand_0, complain));
14916 }
14917 }
14918
14919 case VAR_DECL:
14920 if (!args)
14921 RETURN (t);
14922 else if (DECL_PACK_P (t))
14923 {
14924 /* We don't build decls for an instantiation of a
14925 variadic capture proxy, we instantiate the elements
14926 when needed. */
14927 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14928 return RECUR (DECL_VALUE_EXPR (t));
14929 }
14930 /* Fall through */
14931
14932 case PARM_DECL:
14933 {
14934 tree r = tsubst_copy (t, args, complain, in_decl);
14935
14936 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14937 /* If the original type was a reference, we'll be wrapped in
14938 the appropriate INDIRECT_REF. */
14939 r = convert_from_reference (r);
14940 RETURN (r);
14941 }
14942
14943 case VA_ARG_EXPR:
14944 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14945 RECUR (TREE_OPERAND (t, 0)),
14946 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14947
14948 case OFFSETOF_EXPR:
14949 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14950
14951 case TRAIT_EXPR:
14952 {
14953 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14954 complain, in_decl);
14955
14956 tree type2 = TRAIT_EXPR_TYPE2 (t);
14957 if (type2)
14958 type2 = tsubst_copy (type2, args, complain, in_decl);
14959
14960 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14961 }
14962
14963 case STMT_EXPR:
14964 {
14965 tree old_stmt_expr = cur_stmt_expr;
14966 tree stmt_expr = begin_stmt_expr ();
14967
14968 cur_stmt_expr = stmt_expr;
14969 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14970 integral_constant_expression_p);
14971 stmt_expr = finish_stmt_expr (stmt_expr, false);
14972 cur_stmt_expr = old_stmt_expr;
14973
14974 /* If the resulting list of expression statement is empty,
14975 fold it further into void_zero_node. */
14976 if (empty_expr_stmt_p (stmt_expr))
14977 stmt_expr = void_zero_node;
14978
14979 RETURN (stmt_expr);
14980 }
14981
14982 case LAMBDA_EXPR:
14983 {
14984 tree r = build_lambda_expr ();
14985
14986 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14987 LAMBDA_EXPR_CLOSURE (r) = type;
14988 CLASSTYPE_LAMBDA_EXPR (type) = r;
14989
14990 LAMBDA_EXPR_LOCATION (r)
14991 = LAMBDA_EXPR_LOCATION (t);
14992 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14993 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14994 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14995 LAMBDA_EXPR_DISCRIMINATOR (r)
14996 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14997 /* For a function scope, we want to use tsubst so that we don't
14998 complain about referring to an auto function before its return
14999 type has been deduced. Otherwise, we want to use tsubst_copy so
15000 that we look up the existing field/parameter/variable rather
15001 than build a new one. */
15002 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15003 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15004 scope = tsubst (scope, args, complain, in_decl);
15005 else if (scope && TREE_CODE (scope) == PARM_DECL)
15006 {
15007 /* Look up the parameter we want directly, as tsubst_copy
15008 doesn't do what we need. */
15009 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15010 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15011 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15012 parm = DECL_CHAIN (parm);
15013 scope = parm;
15014 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15015 if (DECL_CONTEXT (scope) == NULL_TREE)
15016 DECL_CONTEXT (scope) = fn;
15017 }
15018 else
15019 scope = RECUR (scope);
15020 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15021 LAMBDA_EXPR_RETURN_TYPE (r)
15022 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15023
15024 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15025 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15026
15027 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15028 determine_visibility (TYPE_NAME (type));
15029 /* Now that we know visibility, instantiate the type so we have a
15030 declaration of the op() for later calls to lambda_function. */
15031 complete_type (type);
15032
15033 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15034
15035 RETURN (build_lambda_object (r));
15036 }
15037
15038 case TARGET_EXPR:
15039 /* We can get here for a constant initializer of non-dependent type.
15040 FIXME stop folding in cp_parser_initializer_clause. */
15041 {
15042 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15043 complain);
15044 RETURN (r);
15045 }
15046
15047 case TRANSACTION_EXPR:
15048 RETURN (tsubst_expr(t, args, complain, in_decl,
15049 integral_constant_expression_p));
15050
15051 case PAREN_EXPR:
15052 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15053
15054 case VEC_PERM_EXPR:
15055 RETURN (build_x_vec_perm_expr (input_location,
15056 RECUR (TREE_OPERAND (t, 0)),
15057 RECUR (TREE_OPERAND (t, 1)),
15058 RECUR (TREE_OPERAND (t, 2)),
15059 complain));
15060
15061 default:
15062 /* Handle Objective-C++ constructs, if appropriate. */
15063 {
15064 tree subst
15065 = objcp_tsubst_copy_and_build (t, args, complain,
15066 in_decl, /*function_p=*/false);
15067 if (subst)
15068 RETURN (subst);
15069 }
15070 RETURN (tsubst_copy (t, args, complain, in_decl));
15071 }
15072
15073 #undef RECUR
15074 #undef RETURN
15075 out:
15076 input_location = loc;
15077 return retval;
15078 }
15079
15080 /* Verify that the instantiated ARGS are valid. For type arguments,
15081 make sure that the type's linkage is ok. For non-type arguments,
15082 make sure they are constants if they are integral or enumerations.
15083 Emit an error under control of COMPLAIN, and return TRUE on error. */
15084
15085 static bool
15086 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15087 {
15088 if (dependent_template_arg_p (t))
15089 return false;
15090 if (ARGUMENT_PACK_P (t))
15091 {
15092 tree vec = ARGUMENT_PACK_ARGS (t);
15093 int len = TREE_VEC_LENGTH (vec);
15094 bool result = false;
15095 int i;
15096
15097 for (i = 0; i < len; ++i)
15098 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15099 result = true;
15100 return result;
15101 }
15102 else if (TYPE_P (t))
15103 {
15104 /* [basic.link]: A name with no linkage (notably, the name
15105 of a class or enumeration declared in a local scope)
15106 shall not be used to declare an entity with linkage.
15107 This implies that names with no linkage cannot be used as
15108 template arguments
15109
15110 DR 757 relaxes this restriction for C++0x. */
15111 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15112 : no_linkage_check (t, /*relaxed_p=*/false));
15113
15114 if (nt)
15115 {
15116 /* DR 488 makes use of a type with no linkage cause
15117 type deduction to fail. */
15118 if (complain & tf_error)
15119 {
15120 if (TYPE_ANONYMOUS_P (nt))
15121 error ("%qT is/uses anonymous type", t);
15122 else
15123 error ("template argument for %qD uses local type %qT",
15124 tmpl, t);
15125 }
15126 return true;
15127 }
15128 /* In order to avoid all sorts of complications, we do not
15129 allow variably-modified types as template arguments. */
15130 else if (variably_modified_type_p (t, NULL_TREE))
15131 {
15132 if (complain & tf_error)
15133 error ("%qT is a variably modified type", t);
15134 return true;
15135 }
15136 }
15137 /* Class template and alias template arguments should be OK. */
15138 else if (DECL_TYPE_TEMPLATE_P (t))
15139 ;
15140 /* A non-type argument of integral or enumerated type must be a
15141 constant. */
15142 else if (TREE_TYPE (t)
15143 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15144 && !TREE_CONSTANT (t))
15145 {
15146 if (complain & tf_error)
15147 error ("integral expression %qE is not constant", t);
15148 return true;
15149 }
15150 return false;
15151 }
15152
15153 static bool
15154 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15155 {
15156 int ix, len = DECL_NTPARMS (tmpl);
15157 bool result = false;
15158
15159 for (ix = 0; ix != len; ix++)
15160 {
15161 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15162 result = true;
15163 }
15164 if (result && (complain & tf_error))
15165 error (" trying to instantiate %qD", tmpl);
15166 return result;
15167 }
15168
15169 /* We're out of SFINAE context now, so generate diagnostics for the access
15170 errors we saw earlier when instantiating D from TMPL and ARGS. */
15171
15172 static void
15173 recheck_decl_substitution (tree d, tree tmpl, tree args)
15174 {
15175 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15176 tree type = TREE_TYPE (pattern);
15177 location_t loc = input_location;
15178
15179 push_access_scope (d);
15180 push_deferring_access_checks (dk_no_deferred);
15181 input_location = DECL_SOURCE_LOCATION (pattern);
15182 tsubst (type, args, tf_warning_or_error, d);
15183 input_location = loc;
15184 pop_deferring_access_checks ();
15185 pop_access_scope (d);
15186 }
15187
15188 /* Instantiate the indicated variable, function, or alias template TMPL with
15189 the template arguments in TARG_PTR. */
15190
15191 static tree
15192 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15193 {
15194 tree targ_ptr = orig_args;
15195 tree fndecl;
15196 tree gen_tmpl;
15197 tree spec;
15198 bool access_ok = true;
15199
15200 if (tmpl == error_mark_node)
15201 return error_mark_node;
15202
15203 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15204
15205 /* If this function is a clone, handle it specially. */
15206 if (DECL_CLONED_FUNCTION_P (tmpl))
15207 {
15208 tree spec;
15209 tree clone;
15210
15211 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15212 DECL_CLONED_FUNCTION. */
15213 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15214 targ_ptr, complain);
15215 if (spec == error_mark_node)
15216 return error_mark_node;
15217
15218 /* Look for the clone. */
15219 FOR_EACH_CLONE (clone, spec)
15220 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15221 return clone;
15222 /* We should always have found the clone by now. */
15223 gcc_unreachable ();
15224 return NULL_TREE;
15225 }
15226
15227 /* Check to see if we already have this specialization. */
15228 gen_tmpl = most_general_template (tmpl);
15229 if (tmpl != gen_tmpl)
15230 /* The TMPL is a partial instantiation. To get a full set of
15231 arguments we must add the arguments used to perform the
15232 partial instantiation. */
15233 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15234 targ_ptr);
15235
15236 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15237 but it doesn't seem to be on the hot path. */
15238 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15239
15240 gcc_assert (tmpl == gen_tmpl
15241 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15242 == spec)
15243 || fndecl == NULL_TREE);
15244
15245 if (spec != NULL_TREE)
15246 {
15247 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15248 {
15249 if (complain & tf_error)
15250 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15251 return error_mark_node;
15252 }
15253 return spec;
15254 }
15255
15256 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15257 complain))
15258 return error_mark_node;
15259
15260 /* We are building a FUNCTION_DECL, during which the access of its
15261 parameters and return types have to be checked. However this
15262 FUNCTION_DECL which is the desired context for access checking
15263 is not built yet. We solve this chicken-and-egg problem by
15264 deferring all checks until we have the FUNCTION_DECL. */
15265 push_deferring_access_checks (dk_deferred);
15266
15267 /* Instantiation of the function happens in the context of the function
15268 template, not the context of the overload resolution we're doing. */
15269 push_to_top_level ();
15270 /* If there are dependent arguments, e.g. because we're doing partial
15271 ordering, make sure processing_template_decl stays set. */
15272 if (uses_template_parms (targ_ptr))
15273 ++processing_template_decl;
15274 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15275 {
15276 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15277 complain, gen_tmpl);
15278 push_nested_class (ctx);
15279 }
15280 /* Substitute template parameters to obtain the specialization. */
15281 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15282 targ_ptr, complain, gen_tmpl);
15283 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15284 pop_nested_class ();
15285 pop_from_top_level ();
15286
15287 if (fndecl == error_mark_node)
15288 {
15289 pop_deferring_access_checks ();
15290 return error_mark_node;
15291 }
15292
15293 /* The DECL_TI_TEMPLATE should always be the immediate parent
15294 template, not the most general template. */
15295 DECL_TI_TEMPLATE (fndecl) = tmpl;
15296
15297 /* Now we know the specialization, compute access previously
15298 deferred. */
15299 push_access_scope (fndecl);
15300 if (!perform_deferred_access_checks (complain))
15301 access_ok = false;
15302 pop_access_scope (fndecl);
15303 pop_deferring_access_checks ();
15304
15305 /* If we've just instantiated the main entry point for a function,
15306 instantiate all the alternate entry points as well. We do this
15307 by cloning the instantiation of the main entry point, not by
15308 instantiating the template clones. */
15309 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15310 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15311
15312 if (!access_ok)
15313 {
15314 if (!(complain & tf_error))
15315 {
15316 /* Remember to reinstantiate when we're out of SFINAE so the user
15317 can see the errors. */
15318 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15319 }
15320 return error_mark_node;
15321 }
15322 return fndecl;
15323 }
15324
15325 /* Wrapper for instantiate_template_1. */
15326
15327 tree
15328 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15329 {
15330 tree ret;
15331 timevar_push (TV_TEMPLATE_INST);
15332 ret = instantiate_template_1 (tmpl, orig_args, complain);
15333 timevar_pop (TV_TEMPLATE_INST);
15334 return ret;
15335 }
15336
15337 /* Instantiate the alias template TMPL with ARGS. Also push a template
15338 instantiation level, which instantiate_template doesn't do because
15339 functions and variables have sufficient context established by the
15340 callers. */
15341
15342 static tree
15343 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15344 {
15345 struct pending_template *old_last_pend = last_pending_template;
15346 struct tinst_level *old_error_tinst = last_error_tinst_level;
15347 if (tmpl == error_mark_node || args == error_mark_node)
15348 return error_mark_node;
15349 tree tinst = build_tree_list (tmpl, args);
15350 if (!push_tinst_level (tinst))
15351 {
15352 ggc_free (tinst);
15353 return error_mark_node;
15354 }
15355
15356 args =
15357 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15358 args, tmpl, complain,
15359 /*require_all_args=*/true,
15360 /*use_default_args=*/true);
15361
15362 tree r = instantiate_template (tmpl, args, complain);
15363 pop_tinst_level ();
15364 /* We can't free this if a pending_template entry or last_error_tinst_level
15365 is pointing at it. */
15366 if (last_pending_template == old_last_pend
15367 && last_error_tinst_level == old_error_tinst)
15368 ggc_free (tinst);
15369
15370 return r;
15371 }
15372
15373 /* PARM is a template parameter pack for FN. Returns true iff
15374 PARM is used in a deducible way in the argument list of FN. */
15375
15376 static bool
15377 pack_deducible_p (tree parm, tree fn)
15378 {
15379 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15380 for (; t; t = TREE_CHAIN (t))
15381 {
15382 tree type = TREE_VALUE (t);
15383 tree packs;
15384 if (!PACK_EXPANSION_P (type))
15385 continue;
15386 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15387 packs; packs = TREE_CHAIN (packs))
15388 if (TREE_VALUE (packs) == parm)
15389 {
15390 /* The template parameter pack is used in a function parameter
15391 pack. If this is the end of the parameter list, the
15392 template parameter pack is deducible. */
15393 if (TREE_CHAIN (t) == void_list_node)
15394 return true;
15395 else
15396 /* Otherwise, not. Well, it could be deduced from
15397 a non-pack parameter, but doing so would end up with
15398 a deduction mismatch, so don't bother. */
15399 return false;
15400 }
15401 }
15402 /* The template parameter pack isn't used in any function parameter
15403 packs, but it might be used deeper, e.g. tuple<Args...>. */
15404 return true;
15405 }
15406
15407 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15408 NARGS elements of the arguments that are being used when calling
15409 it. TARGS is a vector into which the deduced template arguments
15410 are placed.
15411
15412 Return zero for success, 2 for an incomplete match that doesn't resolve
15413 all the types, and 1 for complete failure. An error message will be
15414 printed only for an incomplete match.
15415
15416 If FN is a conversion operator, or we are trying to produce a specific
15417 specialization, RETURN_TYPE is the return type desired.
15418
15419 The EXPLICIT_TARGS are explicit template arguments provided via a
15420 template-id.
15421
15422 The parameter STRICT is one of:
15423
15424 DEDUCE_CALL:
15425 We are deducing arguments for a function call, as in
15426 [temp.deduct.call].
15427
15428 DEDUCE_CONV:
15429 We are deducing arguments for a conversion function, as in
15430 [temp.deduct.conv].
15431
15432 DEDUCE_EXACT:
15433 We are deducing arguments when doing an explicit instantiation
15434 as in [temp.explicit], when determining an explicit specialization
15435 as in [temp.expl.spec], or when taking the address of a function
15436 template, as in [temp.deduct.funcaddr]. */
15437
15438 tree
15439 fn_type_unification (tree fn,
15440 tree explicit_targs,
15441 tree targs,
15442 const tree *args,
15443 unsigned int nargs,
15444 tree return_type,
15445 unification_kind_t strict,
15446 int flags,
15447 bool explain_p,
15448 bool decltype_p)
15449 {
15450 tree parms;
15451 tree fntype;
15452 tree decl = NULL_TREE;
15453 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15454 bool ok;
15455 static int deduction_depth;
15456 struct pending_template *old_last_pend = last_pending_template;
15457 struct tinst_level *old_error_tinst = last_error_tinst_level;
15458 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15459 tree tinst;
15460 tree r = error_mark_node;
15461
15462 if (decltype_p)
15463 complain |= tf_decltype;
15464
15465 /* In C++0x, it's possible to have a function template whose type depends
15466 on itself recursively. This is most obvious with decltype, but can also
15467 occur with enumeration scope (c++/48969). So we need to catch infinite
15468 recursion and reject the substitution at deduction time; this function
15469 will return error_mark_node for any repeated substitution.
15470
15471 This also catches excessive recursion such as when f<N> depends on
15472 f<N-1> across all integers, and returns error_mark_node for all the
15473 substitutions back up to the initial one.
15474
15475 This is, of course, not reentrant. */
15476 if (excessive_deduction_depth)
15477 return error_mark_node;
15478 tinst = build_tree_list (fn, NULL_TREE);
15479 ++deduction_depth;
15480
15481 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15482
15483 fntype = TREE_TYPE (fn);
15484 if (explicit_targs)
15485 {
15486 /* [temp.deduct]
15487
15488 The specified template arguments must match the template
15489 parameters in kind (i.e., type, nontype, template), and there
15490 must not be more arguments than there are parameters;
15491 otherwise type deduction fails.
15492
15493 Nontype arguments must match the types of the corresponding
15494 nontype template parameters, or must be convertible to the
15495 types of the corresponding nontype parameters as specified in
15496 _temp.arg.nontype_, otherwise type deduction fails.
15497
15498 All references in the function type of the function template
15499 to the corresponding template parameters are replaced by the
15500 specified template argument values. If a substitution in a
15501 template parameter or in the function type of the function
15502 template results in an invalid type, type deduction fails. */
15503 int i, len = TREE_VEC_LENGTH (tparms);
15504 location_t loc = input_location;
15505 bool incomplete = false;
15506
15507 /* Adjust any explicit template arguments before entering the
15508 substitution context. */
15509 explicit_targs
15510 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15511 complain,
15512 /*require_all_args=*/false,
15513 /*use_default_args=*/false));
15514 if (explicit_targs == error_mark_node)
15515 goto fail;
15516
15517 /* Substitute the explicit args into the function type. This is
15518 necessary so that, for instance, explicitly declared function
15519 arguments can match null pointed constants. If we were given
15520 an incomplete set of explicit args, we must not do semantic
15521 processing during substitution as we could create partial
15522 instantiations. */
15523 for (i = 0; i < len; i++)
15524 {
15525 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15526 bool parameter_pack = false;
15527 tree targ = TREE_VEC_ELT (explicit_targs, i);
15528
15529 /* Dig out the actual parm. */
15530 if (TREE_CODE (parm) == TYPE_DECL
15531 || TREE_CODE (parm) == TEMPLATE_DECL)
15532 {
15533 parm = TREE_TYPE (parm);
15534 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15535 }
15536 else if (TREE_CODE (parm) == PARM_DECL)
15537 {
15538 parm = DECL_INITIAL (parm);
15539 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15540 }
15541
15542 if (!parameter_pack && targ == NULL_TREE)
15543 /* No explicit argument for this template parameter. */
15544 incomplete = true;
15545
15546 if (parameter_pack && pack_deducible_p (parm, fn))
15547 {
15548 /* Mark the argument pack as "incomplete". We could
15549 still deduce more arguments during unification.
15550 We remove this mark in type_unification_real. */
15551 if (targ)
15552 {
15553 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15554 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15555 = ARGUMENT_PACK_ARGS (targ);
15556 }
15557
15558 /* We have some incomplete argument packs. */
15559 incomplete = true;
15560 }
15561 }
15562
15563 TREE_VALUE (tinst) = explicit_targs;
15564 if (!push_tinst_level (tinst))
15565 {
15566 excessive_deduction_depth = true;
15567 goto fail;
15568 }
15569 processing_template_decl += incomplete;
15570 input_location = DECL_SOURCE_LOCATION (fn);
15571 /* Ignore any access checks; we'll see them again in
15572 instantiate_template and they might have the wrong
15573 access path at this point. */
15574 push_deferring_access_checks (dk_deferred);
15575 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15576 complain | tf_partial, NULL_TREE);
15577 pop_deferring_access_checks ();
15578 input_location = loc;
15579 processing_template_decl -= incomplete;
15580 pop_tinst_level ();
15581
15582 if (fntype == error_mark_node)
15583 goto fail;
15584
15585 /* Place the explicitly specified arguments in TARGS. */
15586 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15587 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15588 }
15589
15590 /* Never do unification on the 'this' parameter. */
15591 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15592
15593 if (return_type)
15594 {
15595 tree *new_args;
15596
15597 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15598 new_args = XALLOCAVEC (tree, nargs + 1);
15599 new_args[0] = return_type;
15600 memcpy (new_args + 1, args, nargs * sizeof (tree));
15601 args = new_args;
15602 ++nargs;
15603 }
15604
15605 /* We allow incomplete unification without an error message here
15606 because the standard doesn't seem to explicitly prohibit it. Our
15607 callers must be ready to deal with unification failures in any
15608 event. */
15609
15610 TREE_VALUE (tinst) = targs;
15611 /* If we aren't explaining yet, push tinst context so we can see where
15612 any errors (e.g. from class instantiations triggered by instantiation
15613 of default template arguments) come from. If we are explaining, this
15614 context is redundant. */
15615 if (!explain_p && !push_tinst_level (tinst))
15616 {
15617 excessive_deduction_depth = true;
15618 goto fail;
15619 }
15620
15621 /* type_unification_real will pass back any access checks from default
15622 template argument substitution. */
15623 vec<deferred_access_check, va_gc> *checks;
15624 checks = NULL;
15625
15626 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15627 targs, parms, args, nargs, /*subr=*/0,
15628 strict, flags, &checks, explain_p);
15629 if (!explain_p)
15630 pop_tinst_level ();
15631 if (!ok)
15632 goto fail;
15633
15634 /* Now that we have bindings for all of the template arguments,
15635 ensure that the arguments deduced for the template template
15636 parameters have compatible template parameter lists. We cannot
15637 check this property before we have deduced all template
15638 arguments, because the template parameter types of a template
15639 template parameter might depend on prior template parameters
15640 deduced after the template template parameter. The following
15641 ill-formed example illustrates this issue:
15642
15643 template<typename T, template<T> class C> void f(C<5>, T);
15644
15645 template<int N> struct X {};
15646
15647 void g() {
15648 f(X<5>(), 5l); // error: template argument deduction fails
15649 }
15650
15651 The template parameter list of 'C' depends on the template type
15652 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15653 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15654 time that we deduce 'C'. */
15655 if (!template_template_parm_bindings_ok_p
15656 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15657 {
15658 unify_inconsistent_template_template_parameters (explain_p);
15659 goto fail;
15660 }
15661
15662 /* All is well so far. Now, check:
15663
15664 [temp.deduct]
15665
15666 When all template arguments have been deduced, all uses of
15667 template parameters in nondeduced contexts are replaced with
15668 the corresponding deduced argument values. If the
15669 substitution results in an invalid type, as described above,
15670 type deduction fails. */
15671 TREE_VALUE (tinst) = targs;
15672 if (!push_tinst_level (tinst))
15673 {
15674 excessive_deduction_depth = true;
15675 goto fail;
15676 }
15677
15678 /* Also collect access checks from the instantiation. */
15679 reopen_deferring_access_checks (checks);
15680
15681 decl = instantiate_template (fn, targs, complain);
15682
15683 checks = get_deferred_access_checks ();
15684 pop_deferring_access_checks ();
15685
15686 pop_tinst_level ();
15687
15688 if (decl == error_mark_node)
15689 goto fail;
15690
15691 /* Now perform any access checks encountered during substitution. */
15692 push_access_scope (decl);
15693 ok = perform_access_checks (checks, complain);
15694 pop_access_scope (decl);
15695 if (!ok)
15696 goto fail;
15697
15698 /* If we're looking for an exact match, check that what we got
15699 is indeed an exact match. It might not be if some template
15700 parameters are used in non-deduced contexts. */
15701 if (strict == DEDUCE_EXACT)
15702 {
15703 tree substed = TREE_TYPE (decl);
15704 unsigned int i;
15705
15706 tree sarg
15707 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15708 if (return_type)
15709 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15710 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15711 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15712 {
15713 unify_type_mismatch (explain_p, args[i],
15714 TREE_VALUE (sarg));
15715 goto fail;
15716 }
15717 }
15718
15719 r = decl;
15720
15721 fail:
15722 --deduction_depth;
15723 if (excessive_deduction_depth)
15724 {
15725 if (deduction_depth == 0)
15726 /* Reset once we're all the way out. */
15727 excessive_deduction_depth = false;
15728 }
15729
15730 /* We can't free this if a pending_template entry or last_error_tinst_level
15731 is pointing at it. */
15732 if (last_pending_template == old_last_pend
15733 && last_error_tinst_level == old_error_tinst)
15734 ggc_free (tinst);
15735
15736 return r;
15737 }
15738
15739 /* Adjust types before performing type deduction, as described in
15740 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15741 sections are symmetric. PARM is the type of a function parameter
15742 or the return type of the conversion function. ARG is the type of
15743 the argument passed to the call, or the type of the value
15744 initialized with the result of the conversion function.
15745 ARG_EXPR is the original argument expression, which may be null. */
15746
15747 static int
15748 maybe_adjust_types_for_deduction (unification_kind_t strict,
15749 tree* parm,
15750 tree* arg,
15751 tree arg_expr)
15752 {
15753 int result = 0;
15754
15755 switch (strict)
15756 {
15757 case DEDUCE_CALL:
15758 break;
15759
15760 case DEDUCE_CONV:
15761 {
15762 /* Swap PARM and ARG throughout the remainder of this
15763 function; the handling is precisely symmetric since PARM
15764 will initialize ARG rather than vice versa. */
15765 tree* temp = parm;
15766 parm = arg;
15767 arg = temp;
15768 break;
15769 }
15770
15771 case DEDUCE_EXACT:
15772 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15773 too, but here handle it by stripping the reference from PARM
15774 rather than by adding it to ARG. */
15775 if (TREE_CODE (*parm) == REFERENCE_TYPE
15776 && TYPE_REF_IS_RVALUE (*parm)
15777 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15778 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15779 && TREE_CODE (*arg) == REFERENCE_TYPE
15780 && !TYPE_REF_IS_RVALUE (*arg))
15781 *parm = TREE_TYPE (*parm);
15782 /* Nothing else to do in this case. */
15783 return 0;
15784
15785 default:
15786 gcc_unreachable ();
15787 }
15788
15789 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15790 {
15791 /* [temp.deduct.call]
15792
15793 If P is not a reference type:
15794
15795 --If A is an array type, the pointer type produced by the
15796 array-to-pointer standard conversion (_conv.array_) is
15797 used in place of A for type deduction; otherwise,
15798
15799 --If A is a function type, the pointer type produced by
15800 the function-to-pointer standard conversion
15801 (_conv.func_) is used in place of A for type deduction;
15802 otherwise,
15803
15804 --If A is a cv-qualified type, the top level
15805 cv-qualifiers of A's type are ignored for type
15806 deduction. */
15807 if (TREE_CODE (*arg) == ARRAY_TYPE)
15808 *arg = build_pointer_type (TREE_TYPE (*arg));
15809 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15810 *arg = build_pointer_type (*arg);
15811 else
15812 *arg = TYPE_MAIN_VARIANT (*arg);
15813 }
15814
15815 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15816 of the form T&&, where T is a template parameter, and the argument
15817 is an lvalue, T is deduced as A& */
15818 if (TREE_CODE (*parm) == REFERENCE_TYPE
15819 && TYPE_REF_IS_RVALUE (*parm)
15820 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15821 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15822 && (arg_expr ? real_lvalue_p (arg_expr)
15823 /* try_one_overload doesn't provide an arg_expr, but
15824 functions are always lvalues. */
15825 : TREE_CODE (*arg) == FUNCTION_TYPE))
15826 *arg = build_reference_type (*arg);
15827
15828 /* [temp.deduct.call]
15829
15830 If P is a cv-qualified type, the top level cv-qualifiers
15831 of P's type are ignored for type deduction. If P is a
15832 reference type, the type referred to by P is used for
15833 type deduction. */
15834 *parm = TYPE_MAIN_VARIANT (*parm);
15835 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15836 {
15837 *parm = TREE_TYPE (*parm);
15838 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15839 }
15840
15841 /* DR 322. For conversion deduction, remove a reference type on parm
15842 too (which has been swapped into ARG). */
15843 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15844 *arg = TREE_TYPE (*arg);
15845
15846 return result;
15847 }
15848
15849 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15850 template which does contain any deducible template parameters; check if
15851 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15852 unify_one_argument. */
15853
15854 static int
15855 check_non_deducible_conversion (tree parm, tree arg, int strict,
15856 int flags, bool explain_p)
15857 {
15858 tree type;
15859
15860 if (!TYPE_P (arg))
15861 type = TREE_TYPE (arg);
15862 else
15863 type = arg;
15864
15865 if (same_type_p (parm, type))
15866 return unify_success (explain_p);
15867
15868 if (strict == DEDUCE_CONV)
15869 {
15870 if (can_convert_arg (type, parm, NULL_TREE, flags,
15871 explain_p ? tf_warning_or_error : tf_none))
15872 return unify_success (explain_p);
15873 }
15874 else if (strict != DEDUCE_EXACT)
15875 {
15876 if (can_convert_arg (parm, type,
15877 TYPE_P (arg) ? NULL_TREE : arg,
15878 flags, explain_p ? tf_warning_or_error : tf_none))
15879 return unify_success (explain_p);
15880 }
15881
15882 if (strict == DEDUCE_EXACT)
15883 return unify_type_mismatch (explain_p, parm, arg);
15884 else
15885 return unify_arg_conversion (explain_p, parm, type, arg);
15886 }
15887
15888 static bool uses_deducible_template_parms (tree type);
15889
15890 /* Returns true iff the expression EXPR is one from which a template
15891 argument can be deduced. In other words, if it's an undecorated
15892 use of a template non-type parameter. */
15893
15894 static bool
15895 deducible_expression (tree expr)
15896 {
15897 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15898 }
15899
15900 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15901 deducible way; that is, if it has a max value of <PARM> - 1. */
15902
15903 static bool
15904 deducible_array_bound (tree domain)
15905 {
15906 if (domain == NULL_TREE)
15907 return false;
15908
15909 tree max = TYPE_MAX_VALUE (domain);
15910 if (TREE_CODE (max) != MINUS_EXPR)
15911 return false;
15912
15913 return deducible_expression (TREE_OPERAND (max, 0));
15914 }
15915
15916 /* Returns true iff the template arguments ARGS use a template parameter
15917 in a deducible way. */
15918
15919 static bool
15920 deducible_template_args (tree args)
15921 {
15922 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15923 {
15924 bool deducible;
15925 tree elt = TREE_VEC_ELT (args, i);
15926 if (ARGUMENT_PACK_P (elt))
15927 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15928 else
15929 {
15930 if (PACK_EXPANSION_P (elt))
15931 elt = PACK_EXPANSION_PATTERN (elt);
15932 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15933 deducible = true;
15934 else if (TYPE_P (elt))
15935 deducible = uses_deducible_template_parms (elt);
15936 else
15937 deducible = deducible_expression (elt);
15938 }
15939 if (deducible)
15940 return true;
15941 }
15942 return false;
15943 }
15944
15945 /* Returns true iff TYPE contains any deducible references to template
15946 parameters, as per 14.8.2.5. */
15947
15948 static bool
15949 uses_deducible_template_parms (tree type)
15950 {
15951 if (PACK_EXPANSION_P (type))
15952 type = PACK_EXPANSION_PATTERN (type);
15953
15954 /* T
15955 cv-list T
15956 TT<T>
15957 TT<i>
15958 TT<> */
15959 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15960 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15961 return true;
15962
15963 /* T*
15964 T&
15965 T&& */
15966 if (POINTER_TYPE_P (type))
15967 return uses_deducible_template_parms (TREE_TYPE (type));
15968
15969 /* T[integer-constant ]
15970 type [i] */
15971 if (TREE_CODE (type) == ARRAY_TYPE)
15972 return (uses_deducible_template_parms (TREE_TYPE (type))
15973 || deducible_array_bound (TYPE_DOMAIN (type)));
15974
15975 /* T type ::*
15976 type T::*
15977 T T::*
15978 T (type ::*)()
15979 type (T::*)()
15980 type (type ::*)(T)
15981 type (T::*)(T)
15982 T (type ::*)(T)
15983 T (T::*)()
15984 T (T::*)(T) */
15985 if (TYPE_PTRMEM_P (type))
15986 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15987 || (uses_deducible_template_parms
15988 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15989
15990 /* template-name <T> (where template-name refers to a class template)
15991 template-name <i> (where template-name refers to a class template) */
15992 if (CLASS_TYPE_P (type)
15993 && CLASSTYPE_TEMPLATE_INFO (type)
15994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15995 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15996 (CLASSTYPE_TI_ARGS (type)));
15997
15998 /* type (T)
15999 T()
16000 T(T) */
16001 if (TREE_CODE (type) == FUNCTION_TYPE
16002 || TREE_CODE (type) == METHOD_TYPE)
16003 {
16004 if (uses_deducible_template_parms (TREE_TYPE (type)))
16005 return true;
16006 tree parm = TYPE_ARG_TYPES (type);
16007 if (TREE_CODE (type) == METHOD_TYPE)
16008 parm = TREE_CHAIN (parm);
16009 for (; parm; parm = TREE_CHAIN (parm))
16010 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16011 return true;
16012 }
16013
16014 return false;
16015 }
16016
16017 /* Subroutine of type_unification_real and unify_pack_expansion to
16018 handle unification of a single P/A pair. Parameters are as
16019 for those functions. */
16020
16021 static int
16022 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16023 int subr, unification_kind_t strict, int flags,
16024 bool explain_p)
16025 {
16026 tree arg_expr = NULL_TREE;
16027 int arg_strict;
16028
16029 if (arg == error_mark_node || parm == error_mark_node)
16030 return unify_invalid (explain_p);
16031 if (arg == unknown_type_node)
16032 /* We can't deduce anything from this, but we might get all the
16033 template args from other function args. */
16034 return unify_success (explain_p);
16035
16036 /* Implicit conversions (Clause 4) will be performed on a function
16037 argument to convert it to the type of the corresponding function
16038 parameter if the parameter type contains no template-parameters that
16039 participate in template argument deduction. */
16040 if (TYPE_P (parm) && !uses_template_parms (parm))
16041 /* For function parameters that contain no template-parameters at all,
16042 we have historically checked for convertibility in order to shortcut
16043 consideration of this candidate. */
16044 return check_non_deducible_conversion (parm, arg, strict, flags,
16045 explain_p);
16046 else if (strict == DEDUCE_CALL
16047 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16048 /* For function parameters with only non-deducible template parameters,
16049 just return. */
16050 return unify_success (explain_p);
16051
16052 switch (strict)
16053 {
16054 case DEDUCE_CALL:
16055 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16056 | UNIFY_ALLOW_MORE_CV_QUAL
16057 | UNIFY_ALLOW_DERIVED);
16058 break;
16059
16060 case DEDUCE_CONV:
16061 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16062 break;
16063
16064 case DEDUCE_EXACT:
16065 arg_strict = UNIFY_ALLOW_NONE;
16066 break;
16067
16068 default:
16069 gcc_unreachable ();
16070 }
16071
16072 /* We only do these transformations if this is the top-level
16073 parameter_type_list in a call or declaration matching; in other
16074 situations (nested function declarators, template argument lists) we
16075 won't be comparing a type to an expression, and we don't do any type
16076 adjustments. */
16077 if (!subr)
16078 {
16079 if (!TYPE_P (arg))
16080 {
16081 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16082 if (type_unknown_p (arg))
16083 {
16084 /* [temp.deduct.type] A template-argument can be
16085 deduced from a pointer to function or pointer
16086 to member function argument if the set of
16087 overloaded functions does not contain function
16088 templates and at most one of a set of
16089 overloaded functions provides a unique
16090 match. */
16091
16092 if (resolve_overloaded_unification
16093 (tparms, targs, parm, arg, strict,
16094 arg_strict, explain_p))
16095 return unify_success (explain_p);
16096 return unify_overload_resolution_failure (explain_p, arg);
16097 }
16098
16099 arg_expr = arg;
16100 arg = unlowered_expr_type (arg);
16101 if (arg == error_mark_node)
16102 return unify_invalid (explain_p);
16103 }
16104
16105 arg_strict |=
16106 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16107 }
16108 else
16109 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16110 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16111
16112 /* For deduction from an init-list we need the actual list. */
16113 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16114 arg = arg_expr;
16115 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16116 }
16117
16118 /* Most parms like fn_type_unification.
16119
16120 If SUBR is 1, we're being called recursively (to unify the
16121 arguments of a function or method parameter of a function
16122 template).
16123
16124 CHECKS is a pointer to a vector of access checks encountered while
16125 substituting default template arguments. */
16126
16127 static int
16128 type_unification_real (tree tparms,
16129 tree targs,
16130 tree xparms,
16131 const tree *xargs,
16132 unsigned int xnargs,
16133 int subr,
16134 unification_kind_t strict,
16135 int flags,
16136 vec<deferred_access_check, va_gc> **checks,
16137 bool explain_p)
16138 {
16139 tree parm, arg;
16140 int i;
16141 int ntparms = TREE_VEC_LENGTH (tparms);
16142 int saw_undeduced = 0;
16143 tree parms;
16144 const tree *args;
16145 unsigned int nargs;
16146 unsigned int ia;
16147
16148 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16149 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16150 gcc_assert (ntparms > 0);
16151
16152 /* Reset the number of non-defaulted template arguments contained
16153 in TARGS. */
16154 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16155
16156 again:
16157 parms = xparms;
16158 args = xargs;
16159 nargs = xnargs;
16160
16161 ia = 0;
16162 while (parms && parms != void_list_node
16163 && ia < nargs)
16164 {
16165 parm = TREE_VALUE (parms);
16166
16167 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16168 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16169 /* For a function parameter pack that occurs at the end of the
16170 parameter-declaration-list, the type A of each remaining
16171 argument of the call is compared with the type P of the
16172 declarator-id of the function parameter pack. */
16173 break;
16174
16175 parms = TREE_CHAIN (parms);
16176
16177 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16178 /* For a function parameter pack that does not occur at the
16179 end of the parameter-declaration-list, the type of the
16180 parameter pack is a non-deduced context. */
16181 continue;
16182
16183 arg = args[ia];
16184 ++ia;
16185
16186 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16187 flags, explain_p))
16188 return 1;
16189 }
16190
16191 if (parms
16192 && parms != void_list_node
16193 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16194 {
16195 /* Unify the remaining arguments with the pack expansion type. */
16196 tree argvec;
16197 tree parmvec = make_tree_vec (1);
16198
16199 /* Allocate a TREE_VEC and copy in all of the arguments */
16200 argvec = make_tree_vec (nargs - ia);
16201 for (i = 0; ia < nargs; ++ia, ++i)
16202 TREE_VEC_ELT (argvec, i) = args[ia];
16203
16204 /* Copy the parameter into parmvec. */
16205 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16206 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16207 /*subr=*/subr, explain_p))
16208 return 1;
16209
16210 /* Advance to the end of the list of parameters. */
16211 parms = TREE_CHAIN (parms);
16212 }
16213
16214 /* Fail if we've reached the end of the parm list, and more args
16215 are present, and the parm list isn't variadic. */
16216 if (ia < nargs && parms == void_list_node)
16217 return unify_too_many_arguments (explain_p, nargs, ia);
16218 /* Fail if parms are left and they don't have default values. */
16219 if (parms && parms != void_list_node
16220 && TREE_PURPOSE (parms) == NULL_TREE)
16221 {
16222 unsigned int count = nargs;
16223 tree p = parms;
16224 while (p && p != void_list_node)
16225 {
16226 count++;
16227 p = TREE_CHAIN (p);
16228 }
16229 return unify_too_few_arguments (explain_p, ia, count);
16230 }
16231
16232 if (!subr)
16233 {
16234 tsubst_flags_t complain = (explain_p
16235 ? tf_warning_or_error
16236 : tf_none);
16237
16238 for (i = 0; i < ntparms; i++)
16239 {
16240 tree targ = TREE_VEC_ELT (targs, i);
16241 tree tparm = TREE_VEC_ELT (tparms, i);
16242
16243 /* Clear the "incomplete" flags on all argument packs now so that
16244 substituting them into later default arguments works. */
16245 if (targ && ARGUMENT_PACK_P (targ))
16246 {
16247 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16248 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16249 }
16250
16251 if (targ || tparm == error_mark_node)
16252 continue;
16253 tparm = TREE_VALUE (tparm);
16254
16255 /* If this is an undeduced nontype parameter that depends on
16256 a type parameter, try another pass; its type may have been
16257 deduced from a later argument than the one from which
16258 this parameter can be deduced. */
16259 if (TREE_CODE (tparm) == PARM_DECL
16260 && uses_template_parms (TREE_TYPE (tparm))
16261 && !saw_undeduced++)
16262 goto again;
16263
16264 /* Core issue #226 (C++0x) [temp.deduct]:
16265
16266 If a template argument has not been deduced, its
16267 default template argument, if any, is used.
16268
16269 When we are in C++98 mode, TREE_PURPOSE will either
16270 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16271 to explicitly check cxx_dialect here. */
16272 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16273 {
16274 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16275 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16276 reopen_deferring_access_checks (*checks);
16277 location_t save_loc = input_location;
16278 if (DECL_P (parm))
16279 input_location = DECL_SOURCE_LOCATION (parm);
16280 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16281 arg = convert_template_argument (parm, arg, targs, complain,
16282 i, NULL_TREE);
16283 input_location = save_loc;
16284 *checks = get_deferred_access_checks ();
16285 pop_deferring_access_checks ();
16286 if (arg == error_mark_node)
16287 return 1;
16288 else
16289 {
16290 TREE_VEC_ELT (targs, i) = arg;
16291 /* The position of the first default template argument,
16292 is also the number of non-defaulted arguments in TARGS.
16293 Record that. */
16294 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16295 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16296 continue;
16297 }
16298 }
16299
16300 /* If the type parameter is a parameter pack, then it will
16301 be deduced to an empty parameter pack. */
16302 if (template_parameter_pack_p (tparm))
16303 {
16304 tree arg;
16305
16306 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16307 {
16308 arg = make_node (NONTYPE_ARGUMENT_PACK);
16309 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16310 TREE_CONSTANT (arg) = 1;
16311 }
16312 else
16313 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16314
16315 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16316
16317 TREE_VEC_ELT (targs, i) = arg;
16318 continue;
16319 }
16320
16321 return unify_parameter_deduction_failure (explain_p, tparm);
16322 }
16323 }
16324 #ifdef ENABLE_CHECKING
16325 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16326 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16327 #endif
16328
16329 return unify_success (explain_p);
16330 }
16331
16332 /* Subroutine of type_unification_real. Args are like the variables
16333 at the call site. ARG is an overloaded function (or template-id);
16334 we try deducing template args from each of the overloads, and if
16335 only one succeeds, we go with that. Modifies TARGS and returns
16336 true on success. */
16337
16338 static bool
16339 resolve_overloaded_unification (tree tparms,
16340 tree targs,
16341 tree parm,
16342 tree arg,
16343 unification_kind_t strict,
16344 int sub_strict,
16345 bool explain_p)
16346 {
16347 tree tempargs = copy_node (targs);
16348 int good = 0;
16349 tree goodfn = NULL_TREE;
16350 bool addr_p;
16351
16352 if (TREE_CODE (arg) == ADDR_EXPR)
16353 {
16354 arg = TREE_OPERAND (arg, 0);
16355 addr_p = true;
16356 }
16357 else
16358 addr_p = false;
16359
16360 if (TREE_CODE (arg) == COMPONENT_REF)
16361 /* Handle `&x' where `x' is some static or non-static member
16362 function name. */
16363 arg = TREE_OPERAND (arg, 1);
16364
16365 if (TREE_CODE (arg) == OFFSET_REF)
16366 arg = TREE_OPERAND (arg, 1);
16367
16368 /* Strip baselink information. */
16369 if (BASELINK_P (arg))
16370 arg = BASELINK_FUNCTIONS (arg);
16371
16372 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16373 {
16374 /* If we got some explicit template args, we need to plug them into
16375 the affected templates before we try to unify, in case the
16376 explicit args will completely resolve the templates in question. */
16377
16378 int ok = 0;
16379 tree expl_subargs = TREE_OPERAND (arg, 1);
16380 arg = TREE_OPERAND (arg, 0);
16381
16382 for (; arg; arg = OVL_NEXT (arg))
16383 {
16384 tree fn = OVL_CURRENT (arg);
16385 tree subargs, elem;
16386
16387 if (TREE_CODE (fn) != TEMPLATE_DECL)
16388 continue;
16389
16390 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16391 expl_subargs, NULL_TREE, tf_none,
16392 /*require_all_args=*/true,
16393 /*use_default_args=*/true);
16394 if (subargs != error_mark_node
16395 && !any_dependent_template_arguments_p (subargs))
16396 {
16397 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16398 if (try_one_overload (tparms, targs, tempargs, parm,
16399 elem, strict, sub_strict, addr_p, explain_p)
16400 && (!goodfn || !same_type_p (goodfn, elem)))
16401 {
16402 goodfn = elem;
16403 ++good;
16404 }
16405 }
16406 else if (subargs)
16407 ++ok;
16408 }
16409 /* If no templates (or more than one) are fully resolved by the
16410 explicit arguments, this template-id is a non-deduced context; it
16411 could still be OK if we deduce all template arguments for the
16412 enclosing call through other arguments. */
16413 if (good != 1)
16414 good = ok;
16415 }
16416 else if (TREE_CODE (arg) != OVERLOAD
16417 && TREE_CODE (arg) != FUNCTION_DECL)
16418 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16419 -- but the deduction does not succeed because the expression is
16420 not just the function on its own. */
16421 return false;
16422 else
16423 for (; arg; arg = OVL_NEXT (arg))
16424 if (try_one_overload (tparms, targs, tempargs, parm,
16425 TREE_TYPE (OVL_CURRENT (arg)),
16426 strict, sub_strict, addr_p, explain_p)
16427 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16428 {
16429 goodfn = OVL_CURRENT (arg);
16430 ++good;
16431 }
16432
16433 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16434 to function or pointer to member function argument if the set of
16435 overloaded functions does not contain function templates and at most
16436 one of a set of overloaded functions provides a unique match.
16437
16438 So if we found multiple possibilities, we return success but don't
16439 deduce anything. */
16440
16441 if (good == 1)
16442 {
16443 int i = TREE_VEC_LENGTH (targs);
16444 for (; i--; )
16445 if (TREE_VEC_ELT (tempargs, i))
16446 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16447 }
16448 if (good)
16449 return true;
16450
16451 return false;
16452 }
16453
16454 /* Core DR 115: In contexts where deduction is done and fails, or in
16455 contexts where deduction is not done, if a template argument list is
16456 specified and it, along with any default template arguments, identifies
16457 a single function template specialization, then the template-id is an
16458 lvalue for the function template specialization. */
16459
16460 tree
16461 resolve_nondeduced_context (tree orig_expr)
16462 {
16463 tree expr, offset, baselink;
16464 bool addr;
16465
16466 if (!type_unknown_p (orig_expr))
16467 return orig_expr;
16468
16469 expr = orig_expr;
16470 addr = false;
16471 offset = NULL_TREE;
16472 baselink = NULL_TREE;
16473
16474 if (TREE_CODE (expr) == ADDR_EXPR)
16475 {
16476 expr = TREE_OPERAND (expr, 0);
16477 addr = true;
16478 }
16479 if (TREE_CODE (expr) == OFFSET_REF)
16480 {
16481 offset = expr;
16482 expr = TREE_OPERAND (expr, 1);
16483 }
16484 if (BASELINK_P (expr))
16485 {
16486 baselink = expr;
16487 expr = BASELINK_FUNCTIONS (expr);
16488 }
16489
16490 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16491 {
16492 int good = 0;
16493 tree goodfn = NULL_TREE;
16494
16495 /* If we got some explicit template args, we need to plug them into
16496 the affected templates before we try to unify, in case the
16497 explicit args will completely resolve the templates in question. */
16498
16499 tree expl_subargs = TREE_OPERAND (expr, 1);
16500 tree arg = TREE_OPERAND (expr, 0);
16501 tree badfn = NULL_TREE;
16502 tree badargs = NULL_TREE;
16503
16504 for (; arg; arg = OVL_NEXT (arg))
16505 {
16506 tree fn = OVL_CURRENT (arg);
16507 tree subargs, elem;
16508
16509 if (TREE_CODE (fn) != TEMPLATE_DECL)
16510 continue;
16511
16512 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16513 expl_subargs, NULL_TREE, tf_none,
16514 /*require_all_args=*/true,
16515 /*use_default_args=*/true);
16516 if (subargs != error_mark_node
16517 && !any_dependent_template_arguments_p (subargs))
16518 {
16519 elem = instantiate_template (fn, subargs, tf_none);
16520 if (elem == error_mark_node)
16521 {
16522 badfn = fn;
16523 badargs = subargs;
16524 }
16525 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16526 {
16527 goodfn = elem;
16528 ++good;
16529 }
16530 }
16531 }
16532 if (good == 1)
16533 {
16534 mark_used (goodfn);
16535 expr = goodfn;
16536 if (baselink)
16537 expr = build_baselink (BASELINK_BINFO (baselink),
16538 BASELINK_ACCESS_BINFO (baselink),
16539 expr, BASELINK_OPTYPE (baselink));
16540 if (offset)
16541 {
16542 tree base
16543 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16544 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16545 }
16546 if (addr)
16547 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16548 return expr;
16549 }
16550 else if (good == 0 && badargs)
16551 /* There were no good options and at least one bad one, so let the
16552 user know what the problem is. */
16553 instantiate_template (badfn, badargs, tf_warning_or_error);
16554 }
16555 return orig_expr;
16556 }
16557
16558 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16559 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16560 different overloads deduce different arguments for a given parm.
16561 ADDR_P is true if the expression for which deduction is being
16562 performed was of the form "& fn" rather than simply "fn".
16563
16564 Returns 1 on success. */
16565
16566 static int
16567 try_one_overload (tree tparms,
16568 tree orig_targs,
16569 tree targs,
16570 tree parm,
16571 tree arg,
16572 unification_kind_t strict,
16573 int sub_strict,
16574 bool addr_p,
16575 bool explain_p)
16576 {
16577 int nargs;
16578 tree tempargs;
16579 int i;
16580
16581 if (arg == error_mark_node)
16582 return 0;
16583
16584 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16585 to function or pointer to member function argument if the set of
16586 overloaded functions does not contain function templates and at most
16587 one of a set of overloaded functions provides a unique match.
16588
16589 So if this is a template, just return success. */
16590
16591 if (uses_template_parms (arg))
16592 return 1;
16593
16594 if (TREE_CODE (arg) == METHOD_TYPE)
16595 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16596 else if (addr_p)
16597 arg = build_pointer_type (arg);
16598
16599 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16600
16601 /* We don't copy orig_targs for this because if we have already deduced
16602 some template args from previous args, unify would complain when we
16603 try to deduce a template parameter for the same argument, even though
16604 there isn't really a conflict. */
16605 nargs = TREE_VEC_LENGTH (targs);
16606 tempargs = make_tree_vec (nargs);
16607
16608 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16609 return 0;
16610
16611 /* First make sure we didn't deduce anything that conflicts with
16612 explicitly specified args. */
16613 for (i = nargs; i--; )
16614 {
16615 tree elt = TREE_VEC_ELT (tempargs, i);
16616 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16617
16618 if (!elt)
16619 /*NOP*/;
16620 else if (uses_template_parms (elt))
16621 /* Since we're unifying against ourselves, we will fill in
16622 template args used in the function parm list with our own
16623 template parms. Discard them. */
16624 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16625 else if (oldelt && !template_args_equal (oldelt, elt))
16626 return 0;
16627 }
16628
16629 for (i = nargs; i--; )
16630 {
16631 tree elt = TREE_VEC_ELT (tempargs, i);
16632
16633 if (elt)
16634 TREE_VEC_ELT (targs, i) = elt;
16635 }
16636
16637 return 1;
16638 }
16639
16640 /* PARM is a template class (perhaps with unbound template
16641 parameters). ARG is a fully instantiated type. If ARG can be
16642 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16643 TARGS are as for unify. */
16644
16645 static tree
16646 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16647 bool explain_p)
16648 {
16649 tree copy_of_targs;
16650
16651 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16652 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16653 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16654 return NULL_TREE;
16655
16656 /* We need to make a new template argument vector for the call to
16657 unify. If we used TARGS, we'd clutter it up with the result of
16658 the attempted unification, even if this class didn't work out.
16659 We also don't want to commit ourselves to all the unifications
16660 we've already done, since unification is supposed to be done on
16661 an argument-by-argument basis. In other words, consider the
16662 following pathological case:
16663
16664 template <int I, int J, int K>
16665 struct S {};
16666
16667 template <int I, int J>
16668 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16669
16670 template <int I, int J, int K>
16671 void f(S<I, J, K>, S<I, I, I>);
16672
16673 void g() {
16674 S<0, 0, 0> s0;
16675 S<0, 1, 2> s2;
16676
16677 f(s0, s2);
16678 }
16679
16680 Now, by the time we consider the unification involving `s2', we
16681 already know that we must have `f<0, 0, 0>'. But, even though
16682 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16683 because there are two ways to unify base classes of S<0, 1, 2>
16684 with S<I, I, I>. If we kept the already deduced knowledge, we
16685 would reject the possibility I=1. */
16686 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16687
16688 /* If unification failed, we're done. */
16689 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16690 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16691 return NULL_TREE;
16692
16693 return arg;
16694 }
16695
16696 /* Given a template type PARM and a class type ARG, find the unique
16697 base type in ARG that is an instance of PARM. We do not examine
16698 ARG itself; only its base-classes. If there is not exactly one
16699 appropriate base class, return NULL_TREE. PARM may be the type of
16700 a partial specialization, as well as a plain template type. Used
16701 by unify. */
16702
16703 static enum template_base_result
16704 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16705 bool explain_p, tree *result)
16706 {
16707 tree rval = NULL_TREE;
16708 tree binfo;
16709
16710 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16711
16712 binfo = TYPE_BINFO (complete_type (arg));
16713 if (!binfo)
16714 {
16715 /* The type could not be completed. */
16716 *result = NULL_TREE;
16717 return tbr_incomplete_type;
16718 }
16719
16720 /* Walk in inheritance graph order. The search order is not
16721 important, and this avoids multiple walks of virtual bases. */
16722 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16723 {
16724 tree r = try_class_unification (tparms, targs, parm,
16725 BINFO_TYPE (binfo), explain_p);
16726
16727 if (r)
16728 {
16729 /* If there is more than one satisfactory baseclass, then:
16730
16731 [temp.deduct.call]
16732
16733 If they yield more than one possible deduced A, the type
16734 deduction fails.
16735
16736 applies. */
16737 if (rval && !same_type_p (r, rval))
16738 {
16739 *result = NULL_TREE;
16740 return tbr_ambiguous_baseclass;
16741 }
16742
16743 rval = r;
16744 }
16745 }
16746
16747 *result = rval;
16748 return tbr_success;
16749 }
16750
16751 /* Returns the level of DECL, which declares a template parameter. */
16752
16753 static int
16754 template_decl_level (tree decl)
16755 {
16756 switch (TREE_CODE (decl))
16757 {
16758 case TYPE_DECL:
16759 case TEMPLATE_DECL:
16760 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16761
16762 case PARM_DECL:
16763 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16764
16765 default:
16766 gcc_unreachable ();
16767 }
16768 return 0;
16769 }
16770
16771 /* Decide whether ARG can be unified with PARM, considering only the
16772 cv-qualifiers of each type, given STRICT as documented for unify.
16773 Returns nonzero iff the unification is OK on that basis. */
16774
16775 static int
16776 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16777 {
16778 int arg_quals = cp_type_quals (arg);
16779 int parm_quals = cp_type_quals (parm);
16780
16781 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16782 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16783 {
16784 /* Although a CVR qualifier is ignored when being applied to a
16785 substituted template parameter ([8.3.2]/1 for example), that
16786 does not allow us to unify "const T" with "int&" because both
16787 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16788 It is ok when we're allowing additional CV qualifiers
16789 at the outer level [14.8.2.1]/3,1st bullet. */
16790 if ((TREE_CODE (arg) == REFERENCE_TYPE
16791 || TREE_CODE (arg) == FUNCTION_TYPE
16792 || TREE_CODE (arg) == METHOD_TYPE)
16793 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16794 return 0;
16795
16796 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16797 && (parm_quals & TYPE_QUAL_RESTRICT))
16798 return 0;
16799 }
16800
16801 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16802 && (arg_quals & parm_quals) != parm_quals)
16803 return 0;
16804
16805 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16806 && (parm_quals & arg_quals) != arg_quals)
16807 return 0;
16808
16809 return 1;
16810 }
16811
16812 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16813 void
16814 template_parm_level_and_index (tree parm, int* level, int* index)
16815 {
16816 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16817 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16818 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16819 {
16820 *index = TEMPLATE_TYPE_IDX (parm);
16821 *level = TEMPLATE_TYPE_LEVEL (parm);
16822 }
16823 else
16824 {
16825 *index = TEMPLATE_PARM_IDX (parm);
16826 *level = TEMPLATE_PARM_LEVEL (parm);
16827 }
16828 }
16829
16830 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16831 do { \
16832 if (unify (TP, TA, P, A, S, EP)) \
16833 return 1; \
16834 } while (0);
16835
16836 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16837 expansion at the end of PACKED_PARMS. Returns 0 if the type
16838 deduction succeeds, 1 otherwise. STRICT is the same as in
16839 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16840 call argument list. We'll need to adjust the arguments to make them
16841 types. SUBR tells us if this is from a recursive call to
16842 type_unification_real, or for comparing two template argument
16843 lists. */
16844
16845 static int
16846 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16847 tree packed_args, unification_kind_t strict,
16848 bool subr, bool explain_p)
16849 {
16850 tree parm
16851 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16852 tree pattern = PACK_EXPANSION_PATTERN (parm);
16853 tree pack, packs = NULL_TREE;
16854 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16855 int len = TREE_VEC_LENGTH (packed_args);
16856
16857 /* Determine the parameter packs we will be deducing from the
16858 pattern, and record their current deductions. */
16859 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16860 pack; pack = TREE_CHAIN (pack))
16861 {
16862 tree parm_pack = TREE_VALUE (pack);
16863 int idx, level;
16864
16865 /* Determine the index and level of this parameter pack. */
16866 template_parm_level_and_index (parm_pack, &level, &idx);
16867
16868 /* Keep track of the parameter packs and their corresponding
16869 argument packs. */
16870 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16871 TREE_TYPE (packs) = make_tree_vec (len - start);
16872 }
16873
16874 /* Loop through all of the arguments that have not yet been
16875 unified and unify each with the pattern. */
16876 for (i = start; i < len; i++)
16877 {
16878 tree parm;
16879 bool any_explicit = false;
16880 tree arg = TREE_VEC_ELT (packed_args, i);
16881
16882 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16883 or the element of its argument pack at the current index if
16884 this argument was explicitly specified. */
16885 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16886 {
16887 int idx, level;
16888 tree arg, pargs;
16889 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16890
16891 arg = NULL_TREE;
16892 if (TREE_VALUE (pack)
16893 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16894 && (i - start < TREE_VEC_LENGTH (pargs)))
16895 {
16896 any_explicit = true;
16897 arg = TREE_VEC_ELT (pargs, i - start);
16898 }
16899 TMPL_ARG (targs, level, idx) = arg;
16900 }
16901
16902 /* If we had explicit template arguments, substitute them into the
16903 pattern before deduction. */
16904 if (any_explicit)
16905 {
16906 /* Some arguments might still be unspecified or dependent. */
16907 bool dependent;
16908 ++processing_template_decl;
16909 dependent = any_dependent_template_arguments_p (targs);
16910 if (!dependent)
16911 --processing_template_decl;
16912 parm = tsubst (pattern, targs,
16913 explain_p ? tf_warning_or_error : tf_none,
16914 NULL_TREE);
16915 if (dependent)
16916 --processing_template_decl;
16917 if (parm == error_mark_node)
16918 return 1;
16919 }
16920 else
16921 parm = pattern;
16922
16923 /* Unify the pattern with the current argument. */
16924 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16925 LOOKUP_IMPLICIT, explain_p))
16926 return 1;
16927
16928 /* For each parameter pack, collect the deduced value. */
16929 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16930 {
16931 int idx, level;
16932 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16933
16934 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16935 TMPL_ARG (targs, level, idx);
16936 }
16937 }
16938
16939 /* Verify that the results of unification with the parameter packs
16940 produce results consistent with what we've seen before, and make
16941 the deduced argument packs available. */
16942 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16943 {
16944 tree old_pack = TREE_VALUE (pack);
16945 tree new_args = TREE_TYPE (pack);
16946 int i, len = TREE_VEC_LENGTH (new_args);
16947 int idx, level;
16948 bool nondeduced_p = false;
16949
16950 /* By default keep the original deduced argument pack.
16951 If necessary, more specific code is going to update the
16952 resulting deduced argument later down in this function. */
16953 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16954 TMPL_ARG (targs, level, idx) = old_pack;
16955
16956 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16957 actually deduce anything. */
16958 for (i = 0; i < len && !nondeduced_p; ++i)
16959 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16960 nondeduced_p = true;
16961 if (nondeduced_p)
16962 continue;
16963
16964 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16965 {
16966 /* If we had fewer function args than explicit template args,
16967 just use the explicits. */
16968 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16969 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16970 if (len < explicit_len)
16971 new_args = explicit_args;
16972 }
16973
16974 if (!old_pack)
16975 {
16976 tree result;
16977 /* Build the deduced *_ARGUMENT_PACK. */
16978 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16979 {
16980 result = make_node (NONTYPE_ARGUMENT_PACK);
16981 TREE_TYPE (result) =
16982 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16983 TREE_CONSTANT (result) = 1;
16984 }
16985 else
16986 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16987
16988 SET_ARGUMENT_PACK_ARGS (result, new_args);
16989
16990 /* Note the deduced argument packs for this parameter
16991 pack. */
16992 TMPL_ARG (targs, level, idx) = result;
16993 }
16994 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16995 && (ARGUMENT_PACK_ARGS (old_pack)
16996 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16997 {
16998 /* We only had the explicitly-provided arguments before, but
16999 now we have a complete set of arguments. */
17000 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17001
17002 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17003 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17004 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17005 }
17006 else
17007 {
17008 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17009 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17010
17011 if (!comp_template_args_with_info (old_args, new_args,
17012 &bad_old_arg, &bad_new_arg))
17013 /* Inconsistent unification of this parameter pack. */
17014 return unify_parameter_pack_inconsistent (explain_p,
17015 bad_old_arg,
17016 bad_new_arg);
17017 }
17018 }
17019
17020 return unify_success (explain_p);
17021 }
17022
17023 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17024 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17025 parameters and return value are as for unify. */
17026
17027 static int
17028 unify_array_domain (tree tparms, tree targs,
17029 tree parm_dom, tree arg_dom,
17030 bool explain_p)
17031 {
17032 tree parm_max;
17033 tree arg_max;
17034 bool parm_cst;
17035 bool arg_cst;
17036
17037 /* Our representation of array types uses "N - 1" as the
17038 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17039 not an integer constant. We cannot unify arbitrarily
17040 complex expressions, so we eliminate the MINUS_EXPRs
17041 here. */
17042 parm_max = TYPE_MAX_VALUE (parm_dom);
17043 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17044 if (!parm_cst)
17045 {
17046 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17047 parm_max = TREE_OPERAND (parm_max, 0);
17048 }
17049 arg_max = TYPE_MAX_VALUE (arg_dom);
17050 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17051 if (!arg_cst)
17052 {
17053 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17054 trying to unify the type of a variable with the type
17055 of a template parameter. For example:
17056
17057 template <unsigned int N>
17058 void f (char (&) [N]);
17059 int g();
17060 void h(int i) {
17061 char a[g(i)];
17062 f(a);
17063 }
17064
17065 Here, the type of the ARG will be "int [g(i)]", and
17066 may be a SAVE_EXPR, etc. */
17067 if (TREE_CODE (arg_max) != MINUS_EXPR)
17068 return unify_vla_arg (explain_p, arg_dom);
17069 arg_max = TREE_OPERAND (arg_max, 0);
17070 }
17071
17072 /* If only one of the bounds used a MINUS_EXPR, compensate
17073 by adding one to the other bound. */
17074 if (parm_cst && !arg_cst)
17075 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17076 integer_type_node,
17077 parm_max,
17078 integer_one_node);
17079 else if (arg_cst && !parm_cst)
17080 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17081 integer_type_node,
17082 arg_max,
17083 integer_one_node);
17084
17085 return unify (tparms, targs, parm_max, arg_max,
17086 UNIFY_ALLOW_INTEGER, explain_p);
17087 }
17088
17089 /* Deduce the value of template parameters. TPARMS is the (innermost)
17090 set of template parameters to a template. TARGS is the bindings
17091 for those template parameters, as determined thus far; TARGS may
17092 include template arguments for outer levels of template parameters
17093 as well. PARM is a parameter to a template function, or a
17094 subcomponent of that parameter; ARG is the corresponding argument.
17095 This function attempts to match PARM with ARG in a manner
17096 consistent with the existing assignments in TARGS. If more values
17097 are deduced, then TARGS is updated.
17098
17099 Returns 0 if the type deduction succeeds, 1 otherwise. The
17100 parameter STRICT is a bitwise or of the following flags:
17101
17102 UNIFY_ALLOW_NONE:
17103 Require an exact match between PARM and ARG.
17104 UNIFY_ALLOW_MORE_CV_QUAL:
17105 Allow the deduced ARG to be more cv-qualified (by qualification
17106 conversion) than ARG.
17107 UNIFY_ALLOW_LESS_CV_QUAL:
17108 Allow the deduced ARG to be less cv-qualified than ARG.
17109 UNIFY_ALLOW_DERIVED:
17110 Allow the deduced ARG to be a template base class of ARG,
17111 or a pointer to a template base class of the type pointed to by
17112 ARG.
17113 UNIFY_ALLOW_INTEGER:
17114 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17115 case for more information.
17116 UNIFY_ALLOW_OUTER_LEVEL:
17117 This is the outermost level of a deduction. Used to determine validity
17118 of qualification conversions. A valid qualification conversion must
17119 have const qualified pointers leading up to the inner type which
17120 requires additional CV quals, except at the outer level, where const
17121 is not required [conv.qual]. It would be normal to set this flag in
17122 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17123 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17124 This is the outermost level of a deduction, and PARM can be more CV
17125 qualified at this point.
17126 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17127 This is the outermost level of a deduction, and PARM can be less CV
17128 qualified at this point. */
17129
17130 static int
17131 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17132 bool explain_p)
17133 {
17134 int idx;
17135 tree targ;
17136 tree tparm;
17137 int strict_in = strict;
17138
17139 /* I don't think this will do the right thing with respect to types.
17140 But the only case I've seen it in so far has been array bounds, where
17141 signedness is the only information lost, and I think that will be
17142 okay. */
17143 while (TREE_CODE (parm) == NOP_EXPR)
17144 parm = TREE_OPERAND (parm, 0);
17145
17146 if (arg == error_mark_node)
17147 return unify_invalid (explain_p);
17148 if (arg == unknown_type_node
17149 || arg == init_list_type_node)
17150 /* We can't deduce anything from this, but we might get all the
17151 template args from other function args. */
17152 return unify_success (explain_p);
17153
17154 /* If PARM uses template parameters, then we can't bail out here,
17155 even if ARG == PARM, since we won't record unifications for the
17156 template parameters. We might need them if we're trying to
17157 figure out which of two things is more specialized. */
17158 if (arg == parm && !uses_template_parms (parm))
17159 return unify_success (explain_p);
17160
17161 /* Handle init lists early, so the rest of the function can assume
17162 we're dealing with a type. */
17163 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17164 {
17165 tree elt, elttype;
17166 unsigned i;
17167 tree orig_parm = parm;
17168
17169 /* Replace T with std::initializer_list<T> for deduction. */
17170 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17171 && flag_deduce_init_list)
17172 parm = listify (parm);
17173
17174 if (!is_std_init_list (parm)
17175 && TREE_CODE (parm) != ARRAY_TYPE)
17176 /* We can only deduce from an initializer list argument if the
17177 parameter is std::initializer_list or an array; otherwise this
17178 is a non-deduced context. */
17179 return unify_success (explain_p);
17180
17181 if (TREE_CODE (parm) == ARRAY_TYPE)
17182 elttype = TREE_TYPE (parm);
17183 else
17184 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17185
17186 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17187 {
17188 int elt_strict = strict;
17189
17190 if (elt == error_mark_node)
17191 return unify_invalid (explain_p);
17192
17193 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17194 {
17195 tree type = TREE_TYPE (elt);
17196 /* It should only be possible to get here for a call. */
17197 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17198 elt_strict |= maybe_adjust_types_for_deduction
17199 (DEDUCE_CALL, &elttype, &type, elt);
17200 elt = type;
17201 }
17202
17203 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17204 explain_p);
17205 }
17206
17207 if (TREE_CODE (parm) == ARRAY_TYPE)
17208 {
17209 /* Also deduce from the length of the initializer list. */
17210 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17211 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17212 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17213 idx, explain_p);
17214 }
17215
17216 /* If the std::initializer_list<T> deduction worked, replace the
17217 deduced A with std::initializer_list<A>. */
17218 if (orig_parm != parm)
17219 {
17220 idx = TEMPLATE_TYPE_IDX (orig_parm);
17221 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17222 targ = listify (targ);
17223 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17224 }
17225 return unify_success (explain_p);
17226 }
17227
17228 /* Immediately reject some pairs that won't unify because of
17229 cv-qualification mismatches. */
17230 if (TREE_CODE (arg) == TREE_CODE (parm)
17231 && TYPE_P (arg)
17232 /* It is the elements of the array which hold the cv quals of an array
17233 type, and the elements might be template type parms. We'll check
17234 when we recurse. */
17235 && TREE_CODE (arg) != ARRAY_TYPE
17236 /* We check the cv-qualifiers when unifying with template type
17237 parameters below. We want to allow ARG `const T' to unify with
17238 PARM `T' for example, when computing which of two templates
17239 is more specialized, for example. */
17240 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17241 && !check_cv_quals_for_unify (strict_in, arg, parm))
17242 return unify_cv_qual_mismatch (explain_p, parm, arg);
17243
17244 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17245 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17246 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17247 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17248 strict &= ~UNIFY_ALLOW_DERIVED;
17249 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17250 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17251
17252 switch (TREE_CODE (parm))
17253 {
17254 case TYPENAME_TYPE:
17255 case SCOPE_REF:
17256 case UNBOUND_CLASS_TEMPLATE:
17257 /* In a type which contains a nested-name-specifier, template
17258 argument values cannot be deduced for template parameters used
17259 within the nested-name-specifier. */
17260 return unify_success (explain_p);
17261
17262 case TEMPLATE_TYPE_PARM:
17263 case TEMPLATE_TEMPLATE_PARM:
17264 case BOUND_TEMPLATE_TEMPLATE_PARM:
17265 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17266 if (tparm == error_mark_node)
17267 return unify_invalid (explain_p);
17268
17269 if (TEMPLATE_TYPE_LEVEL (parm)
17270 != template_decl_level (tparm))
17271 /* The PARM is not one we're trying to unify. Just check
17272 to see if it matches ARG. */
17273 {
17274 if (TREE_CODE (arg) == TREE_CODE (parm)
17275 && (is_auto (parm) ? is_auto (arg)
17276 : same_type_p (parm, arg)))
17277 return unify_success (explain_p);
17278 else
17279 return unify_type_mismatch (explain_p, parm, arg);
17280 }
17281 idx = TEMPLATE_TYPE_IDX (parm);
17282 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17283 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17284 if (tparm == error_mark_node)
17285 return unify_invalid (explain_p);
17286
17287 /* Check for mixed types and values. */
17288 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17289 && TREE_CODE (tparm) != TYPE_DECL)
17290 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17291 && TREE_CODE (tparm) != TEMPLATE_DECL))
17292 gcc_unreachable ();
17293
17294 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17295 {
17296 /* ARG must be constructed from a template class or a template
17297 template parameter. */
17298 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17299 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17300 return unify_template_deduction_failure (explain_p, parm, arg);
17301
17302 {
17303 tree parmvec = TYPE_TI_ARGS (parm);
17304 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17305 tree full_argvec = add_to_template_args (targs, argvec);
17306 tree parm_parms
17307 = DECL_INNERMOST_TEMPLATE_PARMS
17308 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17309 int i, len;
17310 int parm_variadic_p = 0;
17311
17312 /* The resolution to DR150 makes clear that default
17313 arguments for an N-argument may not be used to bind T
17314 to a template template parameter with fewer than N
17315 parameters. It is not safe to permit the binding of
17316 default arguments as an extension, as that may change
17317 the meaning of a conforming program. Consider:
17318
17319 struct Dense { static const unsigned int dim = 1; };
17320
17321 template <template <typename> class View,
17322 typename Block>
17323 void operator+(float, View<Block> const&);
17324
17325 template <typename Block,
17326 unsigned int Dim = Block::dim>
17327 struct Lvalue_proxy { operator float() const; };
17328
17329 void
17330 test_1d (void) {
17331 Lvalue_proxy<Dense> p;
17332 float b;
17333 b + p;
17334 }
17335
17336 Here, if Lvalue_proxy is permitted to bind to View, then
17337 the global operator+ will be used; if they are not, the
17338 Lvalue_proxy will be converted to float. */
17339 if (coerce_template_parms (parm_parms,
17340 full_argvec,
17341 TYPE_TI_TEMPLATE (parm),
17342 (explain_p
17343 ? tf_warning_or_error
17344 : tf_none),
17345 /*require_all_args=*/true,
17346 /*use_default_args=*/false)
17347 == error_mark_node)
17348 return 1;
17349
17350 /* Deduce arguments T, i from TT<T> or TT<i>.
17351 We check each element of PARMVEC and ARGVEC individually
17352 rather than the whole TREE_VEC since they can have
17353 different number of elements. */
17354
17355 parmvec = expand_template_argument_pack (parmvec);
17356 argvec = expand_template_argument_pack (argvec);
17357
17358 len = TREE_VEC_LENGTH (parmvec);
17359
17360 /* Check if the parameters end in a pack, making them
17361 variadic. */
17362 if (len > 0
17363 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17364 parm_variadic_p = 1;
17365
17366 for (i = 0; i < len - parm_variadic_p; ++i)
17367 /* If the template argument list of P contains a pack
17368 expansion that is not the last template argument, the
17369 entire template argument list is a non-deduced
17370 context. */
17371 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17372 return unify_success (explain_p);
17373
17374 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17375 return unify_too_few_arguments (explain_p,
17376 TREE_VEC_LENGTH (argvec), len);
17377
17378 for (i = 0; i < len - parm_variadic_p; ++i)
17379 {
17380 RECUR_AND_CHECK_FAILURE (tparms, targs,
17381 TREE_VEC_ELT (parmvec, i),
17382 TREE_VEC_ELT (argvec, i),
17383 UNIFY_ALLOW_NONE, explain_p);
17384 }
17385
17386 if (parm_variadic_p
17387 && unify_pack_expansion (tparms, targs,
17388 parmvec, argvec,
17389 DEDUCE_EXACT,
17390 /*subr=*/true, explain_p))
17391 return 1;
17392 }
17393 arg = TYPE_TI_TEMPLATE (arg);
17394
17395 /* Fall through to deduce template name. */
17396 }
17397
17398 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17399 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17400 {
17401 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17402
17403 /* Simple cases: Value already set, does match or doesn't. */
17404 if (targ != NULL_TREE && template_args_equal (targ, arg))
17405 return unify_success (explain_p);
17406 else if (targ)
17407 return unify_inconsistency (explain_p, parm, targ, arg);
17408 }
17409 else
17410 {
17411 /* If PARM is `const T' and ARG is only `int', we don't have
17412 a match unless we are allowing additional qualification.
17413 If ARG is `const int' and PARM is just `T' that's OK;
17414 that binds `const int' to `T'. */
17415 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17416 arg, parm))
17417 return unify_cv_qual_mismatch (explain_p, parm, arg);
17418
17419 /* Consider the case where ARG is `const volatile int' and
17420 PARM is `const T'. Then, T should be `volatile int'. */
17421 arg = cp_build_qualified_type_real
17422 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17423 if (arg == error_mark_node)
17424 return unify_invalid (explain_p);
17425
17426 /* Simple cases: Value already set, does match or doesn't. */
17427 if (targ != NULL_TREE && same_type_p (targ, arg))
17428 return unify_success (explain_p);
17429 else if (targ)
17430 return unify_inconsistency (explain_p, parm, targ, arg);
17431
17432 /* Make sure that ARG is not a variable-sized array. (Note
17433 that were talking about variable-sized arrays (like
17434 `int[n]'), rather than arrays of unknown size (like
17435 `int[]').) We'll get very confused by such a type since
17436 the bound of the array is not constant, and therefore
17437 not mangleable. Besides, such types are not allowed in
17438 ISO C++, so we can do as we please here. We do allow
17439 them for 'auto' deduction, since that isn't ABI-exposed. */
17440 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17441 return unify_vla_arg (explain_p, arg);
17442
17443 /* Strip typedefs as in convert_template_argument. */
17444 arg = canonicalize_type_argument (arg, tf_none);
17445 }
17446
17447 /* If ARG is a parameter pack or an expansion, we cannot unify
17448 against it unless PARM is also a parameter pack. */
17449 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17450 && !template_parameter_pack_p (parm))
17451 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17452
17453 /* If the argument deduction results is a METHOD_TYPE,
17454 then there is a problem.
17455 METHOD_TYPE doesn't map to any real C++ type the result of
17456 the deduction can not be of that type. */
17457 if (TREE_CODE (arg) == METHOD_TYPE)
17458 return unify_method_type_error (explain_p, arg);
17459
17460 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17461 return unify_success (explain_p);
17462
17463 case TEMPLATE_PARM_INDEX:
17464 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17465 if (tparm == error_mark_node)
17466 return unify_invalid (explain_p);
17467
17468 if (TEMPLATE_PARM_LEVEL (parm)
17469 != template_decl_level (tparm))
17470 {
17471 /* The PARM is not one we're trying to unify. Just check
17472 to see if it matches ARG. */
17473 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17474 && cp_tree_equal (parm, arg));
17475 if (result)
17476 unify_expression_unequal (explain_p, parm, arg);
17477 return result;
17478 }
17479
17480 idx = TEMPLATE_PARM_IDX (parm);
17481 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17482
17483 if (targ)
17484 {
17485 int x = !cp_tree_equal (targ, arg);
17486 if (x)
17487 unify_inconsistency (explain_p, parm, targ, arg);
17488 return x;
17489 }
17490
17491 /* [temp.deduct.type] If, in the declaration of a function template
17492 with a non-type template-parameter, the non-type
17493 template-parameter is used in an expression in the function
17494 parameter-list and, if the corresponding template-argument is
17495 deduced, the template-argument type shall match the type of the
17496 template-parameter exactly, except that a template-argument
17497 deduced from an array bound may be of any integral type.
17498 The non-type parameter might use already deduced type parameters. */
17499 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17500 if (!TREE_TYPE (arg))
17501 /* Template-parameter dependent expression. Just accept it for now.
17502 It will later be processed in convert_template_argument. */
17503 ;
17504 else if (same_type_p (TREE_TYPE (arg), tparm))
17505 /* OK */;
17506 else if ((strict & UNIFY_ALLOW_INTEGER)
17507 && CP_INTEGRAL_TYPE_P (tparm))
17508 /* Convert the ARG to the type of PARM; the deduced non-type
17509 template argument must exactly match the types of the
17510 corresponding parameter. */
17511 arg = fold (build_nop (tparm, arg));
17512 else if (uses_template_parms (tparm))
17513 /* We haven't deduced the type of this parameter yet. Try again
17514 later. */
17515 return unify_success (explain_p);
17516 else
17517 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17518
17519 /* If ARG is a parameter pack or an expansion, we cannot unify
17520 against it unless PARM is also a parameter pack. */
17521 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17522 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17523 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17524
17525 arg = strip_typedefs_expr (arg);
17526 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17527 return unify_success (explain_p);
17528
17529 case PTRMEM_CST:
17530 {
17531 /* A pointer-to-member constant can be unified only with
17532 another constant. */
17533 if (TREE_CODE (arg) != PTRMEM_CST)
17534 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17535
17536 /* Just unify the class member. It would be useless (and possibly
17537 wrong, depending on the strict flags) to unify also
17538 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17539 arg refer to the same variable, even if through different
17540 classes. For instance:
17541
17542 struct A { int x; };
17543 struct B : A { };
17544
17545 Unification of &A::x and &B::x must succeed. */
17546 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17547 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17548 }
17549
17550 case POINTER_TYPE:
17551 {
17552 if (!TYPE_PTR_P (arg))
17553 return unify_type_mismatch (explain_p, parm, arg);
17554
17555 /* [temp.deduct.call]
17556
17557 A can be another pointer or pointer to member type that can
17558 be converted to the deduced A via a qualification
17559 conversion (_conv.qual_).
17560
17561 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17562 This will allow for additional cv-qualification of the
17563 pointed-to types if appropriate. */
17564
17565 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17566 /* The derived-to-base conversion only persists through one
17567 level of pointers. */
17568 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17569
17570 return unify (tparms, targs, TREE_TYPE (parm),
17571 TREE_TYPE (arg), strict, explain_p);
17572 }
17573
17574 case REFERENCE_TYPE:
17575 if (TREE_CODE (arg) != REFERENCE_TYPE)
17576 return unify_type_mismatch (explain_p, parm, arg);
17577 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17578 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17579
17580 case ARRAY_TYPE:
17581 if (TREE_CODE (arg) != ARRAY_TYPE)
17582 return unify_type_mismatch (explain_p, parm, arg);
17583 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17584 != (TYPE_DOMAIN (arg) == NULL_TREE))
17585 return unify_type_mismatch (explain_p, parm, arg);
17586 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17587 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17588 if (TYPE_DOMAIN (parm) != NULL_TREE)
17589 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17590 TYPE_DOMAIN (arg), explain_p);
17591 return unify_success (explain_p);
17592
17593 case REAL_TYPE:
17594 case COMPLEX_TYPE:
17595 case VECTOR_TYPE:
17596 case INTEGER_TYPE:
17597 case BOOLEAN_TYPE:
17598 case ENUMERAL_TYPE:
17599 case VOID_TYPE:
17600 case NULLPTR_TYPE:
17601 if (TREE_CODE (arg) != TREE_CODE (parm))
17602 return unify_type_mismatch (explain_p, parm, arg);
17603
17604 /* We have already checked cv-qualification at the top of the
17605 function. */
17606 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17607 return unify_type_mismatch (explain_p, parm, arg);
17608
17609 /* As far as unification is concerned, this wins. Later checks
17610 will invalidate it if necessary. */
17611 return unify_success (explain_p);
17612
17613 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17614 /* Type INTEGER_CST can come from ordinary constant template args. */
17615 case INTEGER_CST:
17616 while (TREE_CODE (arg) == NOP_EXPR)
17617 arg = TREE_OPERAND (arg, 0);
17618
17619 if (TREE_CODE (arg) != INTEGER_CST)
17620 return unify_template_argument_mismatch (explain_p, parm, arg);
17621 return (tree_int_cst_equal (parm, arg)
17622 ? unify_success (explain_p)
17623 : unify_template_argument_mismatch (explain_p, parm, arg));
17624
17625 case TREE_VEC:
17626 {
17627 int i, len, argslen;
17628 int parm_variadic_p = 0;
17629
17630 if (TREE_CODE (arg) != TREE_VEC)
17631 return unify_template_argument_mismatch (explain_p, parm, arg);
17632
17633 len = TREE_VEC_LENGTH (parm);
17634 argslen = TREE_VEC_LENGTH (arg);
17635
17636 /* Check for pack expansions in the parameters. */
17637 for (i = 0; i < len; ++i)
17638 {
17639 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17640 {
17641 if (i == len - 1)
17642 /* We can unify against something with a trailing
17643 parameter pack. */
17644 parm_variadic_p = 1;
17645 else
17646 /* [temp.deduct.type]/9: If the template argument list of
17647 P contains a pack expansion that is not the last
17648 template argument, the entire template argument list
17649 is a non-deduced context. */
17650 return unify_success (explain_p);
17651 }
17652 }
17653
17654 /* If we don't have enough arguments to satisfy the parameters
17655 (not counting the pack expression at the end), or we have
17656 too many arguments for a parameter list that doesn't end in
17657 a pack expression, we can't unify. */
17658 if (parm_variadic_p
17659 ? argslen < len - parm_variadic_p
17660 : argslen != len)
17661 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17662
17663 /* Unify all of the parameters that precede the (optional)
17664 pack expression. */
17665 for (i = 0; i < len - parm_variadic_p; ++i)
17666 {
17667 RECUR_AND_CHECK_FAILURE (tparms, targs,
17668 TREE_VEC_ELT (parm, i),
17669 TREE_VEC_ELT (arg, i),
17670 UNIFY_ALLOW_NONE, explain_p);
17671 }
17672 if (parm_variadic_p)
17673 return unify_pack_expansion (tparms, targs, parm, arg,
17674 DEDUCE_EXACT,
17675 /*subr=*/true, explain_p);
17676 return unify_success (explain_p);
17677 }
17678
17679 case RECORD_TYPE:
17680 case UNION_TYPE:
17681 if (TREE_CODE (arg) != TREE_CODE (parm))
17682 return unify_type_mismatch (explain_p, parm, arg);
17683
17684 if (TYPE_PTRMEMFUNC_P (parm))
17685 {
17686 if (!TYPE_PTRMEMFUNC_P (arg))
17687 return unify_type_mismatch (explain_p, parm, arg);
17688
17689 return unify (tparms, targs,
17690 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17691 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17692 strict, explain_p);
17693 }
17694
17695 if (CLASSTYPE_TEMPLATE_INFO (parm))
17696 {
17697 tree t = NULL_TREE;
17698
17699 if (strict_in & UNIFY_ALLOW_DERIVED)
17700 {
17701 /* First, we try to unify the PARM and ARG directly. */
17702 t = try_class_unification (tparms, targs,
17703 parm, arg, explain_p);
17704
17705 if (!t)
17706 {
17707 /* Fallback to the special case allowed in
17708 [temp.deduct.call]:
17709
17710 If P is a class, and P has the form
17711 template-id, then A can be a derived class of
17712 the deduced A. Likewise, if P is a pointer to
17713 a class of the form template-id, A can be a
17714 pointer to a derived class pointed to by the
17715 deduced A. */
17716 enum template_base_result r;
17717 r = get_template_base (tparms, targs, parm, arg,
17718 explain_p, &t);
17719
17720 if (!t)
17721 return unify_no_common_base (explain_p, r, parm, arg);
17722 }
17723 }
17724 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17725 && (CLASSTYPE_TI_TEMPLATE (parm)
17726 == CLASSTYPE_TI_TEMPLATE (arg)))
17727 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17728 Then, we should unify `int' and `U'. */
17729 t = arg;
17730 else
17731 /* There's no chance of unification succeeding. */
17732 return unify_type_mismatch (explain_p, parm, arg);
17733
17734 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17735 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17736 }
17737 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17738 return unify_type_mismatch (explain_p, parm, arg);
17739 return unify_success (explain_p);
17740
17741 case METHOD_TYPE:
17742 case FUNCTION_TYPE:
17743 {
17744 unsigned int nargs;
17745 tree *args;
17746 tree a;
17747 unsigned int i;
17748
17749 if (TREE_CODE (arg) != TREE_CODE (parm))
17750 return unify_type_mismatch (explain_p, parm, arg);
17751
17752 /* CV qualifications for methods can never be deduced, they must
17753 match exactly. We need to check them explicitly here,
17754 because type_unification_real treats them as any other
17755 cv-qualified parameter. */
17756 if (TREE_CODE (parm) == METHOD_TYPE
17757 && (!check_cv_quals_for_unify
17758 (UNIFY_ALLOW_NONE,
17759 class_of_this_parm (arg),
17760 class_of_this_parm (parm))))
17761 return unify_cv_qual_mismatch (explain_p, parm, arg);
17762
17763 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17764 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17765
17766 nargs = list_length (TYPE_ARG_TYPES (arg));
17767 args = XALLOCAVEC (tree, nargs);
17768 for (a = TYPE_ARG_TYPES (arg), i = 0;
17769 a != NULL_TREE && a != void_list_node;
17770 a = TREE_CHAIN (a), ++i)
17771 args[i] = TREE_VALUE (a);
17772 nargs = i;
17773
17774 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17775 args, nargs, 1, DEDUCE_EXACT,
17776 LOOKUP_NORMAL, NULL, explain_p);
17777 }
17778
17779 case OFFSET_TYPE:
17780 /* Unify a pointer to member with a pointer to member function, which
17781 deduces the type of the member as a function type. */
17782 if (TYPE_PTRMEMFUNC_P (arg))
17783 {
17784 /* Check top-level cv qualifiers */
17785 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17786 return unify_cv_qual_mismatch (explain_p, parm, arg);
17787
17788 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17789 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17790 UNIFY_ALLOW_NONE, explain_p);
17791
17792 /* Determine the type of the function we are unifying against. */
17793 tree fntype = static_fn_type (arg);
17794
17795 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17796 }
17797
17798 if (TREE_CODE (arg) != OFFSET_TYPE)
17799 return unify_type_mismatch (explain_p, parm, arg);
17800 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17801 TYPE_OFFSET_BASETYPE (arg),
17802 UNIFY_ALLOW_NONE, explain_p);
17803 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17804 strict, explain_p);
17805
17806 case CONST_DECL:
17807 if (DECL_TEMPLATE_PARM_P (parm))
17808 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17809 if (arg != integral_constant_value (parm))
17810 return unify_template_argument_mismatch (explain_p, parm, arg);
17811 return unify_success (explain_p);
17812
17813 case FIELD_DECL:
17814 case TEMPLATE_DECL:
17815 /* Matched cases are handled by the ARG == PARM test above. */
17816 return unify_template_argument_mismatch (explain_p, parm, arg);
17817
17818 case VAR_DECL:
17819 /* A non-type template parameter that is a variable should be a
17820 an integral constant, in which case, it whould have been
17821 folded into its (constant) value. So we should not be getting
17822 a variable here. */
17823 gcc_unreachable ();
17824
17825 case TYPE_ARGUMENT_PACK:
17826 case NONTYPE_ARGUMENT_PACK:
17827 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17828 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17829
17830 case TYPEOF_TYPE:
17831 case DECLTYPE_TYPE:
17832 case UNDERLYING_TYPE:
17833 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17834 or UNDERLYING_TYPE nodes. */
17835 return unify_success (explain_p);
17836
17837 case ERROR_MARK:
17838 /* Unification fails if we hit an error node. */
17839 return unify_invalid (explain_p);
17840
17841 default:
17842 /* An unresolved overload is a nondeduced context. */
17843 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17844 return unify_success (explain_p);
17845 gcc_assert (EXPR_P (parm));
17846
17847 /* We must be looking at an expression. This can happen with
17848 something like:
17849
17850 template <int I>
17851 void foo(S<I>, S<I + 2>);
17852
17853 This is a "nondeduced context":
17854
17855 [deduct.type]
17856
17857 The nondeduced contexts are:
17858
17859 --A type that is a template-id in which one or more of
17860 the template-arguments is an expression that references
17861 a template-parameter.
17862
17863 In these cases, we assume deduction succeeded, but don't
17864 actually infer any unifications. */
17865
17866 if (!uses_template_parms (parm)
17867 && !template_args_equal (parm, arg))
17868 return unify_expression_unequal (explain_p, parm, arg);
17869 else
17870 return unify_success (explain_p);
17871 }
17872 }
17873 #undef RECUR_AND_CHECK_FAILURE
17874 \f
17875 /* Note that DECL can be defined in this translation unit, if
17876 required. */
17877
17878 static void
17879 mark_definable (tree decl)
17880 {
17881 tree clone;
17882 DECL_NOT_REALLY_EXTERN (decl) = 1;
17883 FOR_EACH_CLONE (clone, decl)
17884 DECL_NOT_REALLY_EXTERN (clone) = 1;
17885 }
17886
17887 /* Called if RESULT is explicitly instantiated, or is a member of an
17888 explicitly instantiated class. */
17889
17890 void
17891 mark_decl_instantiated (tree result, int extern_p)
17892 {
17893 SET_DECL_EXPLICIT_INSTANTIATION (result);
17894
17895 /* If this entity has already been written out, it's too late to
17896 make any modifications. */
17897 if (TREE_ASM_WRITTEN (result))
17898 return;
17899
17900 /* For anonymous namespace we don't need to do anything. */
17901 if (decl_anon_ns_mem_p (result))
17902 {
17903 gcc_assert (!TREE_PUBLIC (result));
17904 return;
17905 }
17906
17907 if (TREE_CODE (result) != FUNCTION_DECL)
17908 /* The TREE_PUBLIC flag for function declarations will have been
17909 set correctly by tsubst. */
17910 TREE_PUBLIC (result) = 1;
17911
17912 /* This might have been set by an earlier implicit instantiation. */
17913 DECL_COMDAT (result) = 0;
17914
17915 if (extern_p)
17916 DECL_NOT_REALLY_EXTERN (result) = 0;
17917 else
17918 {
17919 mark_definable (result);
17920 /* Always make artificials weak. */
17921 if (DECL_ARTIFICIAL (result) && flag_weak)
17922 comdat_linkage (result);
17923 /* For WIN32 we also want to put explicit instantiations in
17924 linkonce sections. */
17925 else if (TREE_PUBLIC (result))
17926 maybe_make_one_only (result);
17927 }
17928
17929 /* If EXTERN_P, then this function will not be emitted -- unless
17930 followed by an explicit instantiation, at which point its linkage
17931 will be adjusted. If !EXTERN_P, then this function will be
17932 emitted here. In neither circumstance do we want
17933 import_export_decl to adjust the linkage. */
17934 DECL_INTERFACE_KNOWN (result) = 1;
17935 }
17936
17937 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17938 important template arguments. If any are missing, we check whether
17939 they're important by using error_mark_node for substituting into any
17940 args that were used for partial ordering (the ones between ARGS and END)
17941 and seeing if it bubbles up. */
17942
17943 static bool
17944 check_undeduced_parms (tree targs, tree args, tree end)
17945 {
17946 bool found = false;
17947 int i;
17948 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17949 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17950 {
17951 found = true;
17952 TREE_VEC_ELT (targs, i) = error_mark_node;
17953 }
17954 if (found)
17955 {
17956 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17957 if (substed == error_mark_node)
17958 return true;
17959 }
17960 return false;
17961 }
17962
17963 /* Given two function templates PAT1 and PAT2, return:
17964
17965 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17966 -1 if PAT2 is more specialized than PAT1.
17967 0 if neither is more specialized.
17968
17969 LEN indicates the number of parameters we should consider
17970 (defaulted parameters should not be considered).
17971
17972 The 1998 std underspecified function template partial ordering, and
17973 DR214 addresses the issue. We take pairs of arguments, one from
17974 each of the templates, and deduce them against each other. One of
17975 the templates will be more specialized if all the *other*
17976 template's arguments deduce against its arguments and at least one
17977 of its arguments *does* *not* deduce against the other template's
17978 corresponding argument. Deduction is done as for class templates.
17979 The arguments used in deduction have reference and top level cv
17980 qualifiers removed. Iff both arguments were originally reference
17981 types *and* deduction succeeds in both directions, an lvalue reference
17982 wins against an rvalue reference and otherwise the template
17983 with the more cv-qualified argument wins for that pairing (if
17984 neither is more cv-qualified, they both are equal). Unlike regular
17985 deduction, after all the arguments have been deduced in this way,
17986 we do *not* verify the deduced template argument values can be
17987 substituted into non-deduced contexts.
17988
17989 The logic can be a bit confusing here, because we look at deduce1 and
17990 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17991 can find template arguments for pat1 to make arg1 look like arg2, that
17992 means that arg2 is at least as specialized as arg1. */
17993
17994 int
17995 more_specialized_fn (tree pat1, tree pat2, int len)
17996 {
17997 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17998 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17999 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18000 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18001 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18002 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18003 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18004 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18005 tree origs1, origs2;
18006 bool lose1 = false;
18007 bool lose2 = false;
18008
18009 /* Remove the this parameter from non-static member functions. If
18010 one is a non-static member function and the other is not a static
18011 member function, remove the first parameter from that function
18012 also. This situation occurs for operator functions where we
18013 locate both a member function (with this pointer) and non-member
18014 operator (with explicit first operand). */
18015 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18016 {
18017 len--; /* LEN is the number of significant arguments for DECL1 */
18018 args1 = TREE_CHAIN (args1);
18019 if (!DECL_STATIC_FUNCTION_P (decl2))
18020 args2 = TREE_CHAIN (args2);
18021 }
18022 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18023 {
18024 args2 = TREE_CHAIN (args2);
18025 if (!DECL_STATIC_FUNCTION_P (decl1))
18026 {
18027 len--;
18028 args1 = TREE_CHAIN (args1);
18029 }
18030 }
18031
18032 /* If only one is a conversion operator, they are unordered. */
18033 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18034 return 0;
18035
18036 /* Consider the return type for a conversion function */
18037 if (DECL_CONV_FN_P (decl1))
18038 {
18039 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18040 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18041 len++;
18042 }
18043
18044 processing_template_decl++;
18045
18046 origs1 = args1;
18047 origs2 = args2;
18048
18049 while (len--
18050 /* Stop when an ellipsis is seen. */
18051 && args1 != NULL_TREE && args2 != NULL_TREE)
18052 {
18053 tree arg1 = TREE_VALUE (args1);
18054 tree arg2 = TREE_VALUE (args2);
18055 int deduce1, deduce2;
18056 int quals1 = -1;
18057 int quals2 = -1;
18058 int ref1 = 0;
18059 int ref2 = 0;
18060
18061 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18062 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18063 {
18064 /* When both arguments are pack expansions, we need only
18065 unify the patterns themselves. */
18066 arg1 = PACK_EXPANSION_PATTERN (arg1);
18067 arg2 = PACK_EXPANSION_PATTERN (arg2);
18068
18069 /* This is the last comparison we need to do. */
18070 len = 0;
18071 }
18072
18073 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18074 {
18075 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18076 arg1 = TREE_TYPE (arg1);
18077 quals1 = cp_type_quals (arg1);
18078 }
18079
18080 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18081 {
18082 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18083 arg2 = TREE_TYPE (arg2);
18084 quals2 = cp_type_quals (arg2);
18085 }
18086
18087 arg1 = TYPE_MAIN_VARIANT (arg1);
18088 arg2 = TYPE_MAIN_VARIANT (arg2);
18089
18090 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18091 {
18092 int i, len2 = list_length (args2);
18093 tree parmvec = make_tree_vec (1);
18094 tree argvec = make_tree_vec (len2);
18095 tree ta = args2;
18096
18097 /* Setup the parameter vector, which contains only ARG1. */
18098 TREE_VEC_ELT (parmvec, 0) = arg1;
18099
18100 /* Setup the argument vector, which contains the remaining
18101 arguments. */
18102 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18103 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18104
18105 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18106 argvec, DEDUCE_EXACT,
18107 /*subr=*/true, /*explain_p=*/false)
18108 == 0);
18109
18110 /* We cannot deduce in the other direction, because ARG1 is
18111 a pack expansion but ARG2 is not. */
18112 deduce2 = 0;
18113 }
18114 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18115 {
18116 int i, len1 = list_length (args1);
18117 tree parmvec = make_tree_vec (1);
18118 tree argvec = make_tree_vec (len1);
18119 tree ta = args1;
18120
18121 /* Setup the parameter vector, which contains only ARG1. */
18122 TREE_VEC_ELT (parmvec, 0) = arg2;
18123
18124 /* Setup the argument vector, which contains the remaining
18125 arguments. */
18126 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18127 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18128
18129 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18130 argvec, DEDUCE_EXACT,
18131 /*subr=*/true, /*explain_p=*/false)
18132 == 0);
18133
18134 /* We cannot deduce in the other direction, because ARG2 is
18135 a pack expansion but ARG1 is not.*/
18136 deduce1 = 0;
18137 }
18138
18139 else
18140 {
18141 /* The normal case, where neither argument is a pack
18142 expansion. */
18143 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18144 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18145 == 0);
18146 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18147 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18148 == 0);
18149 }
18150
18151 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18152 arg2, then arg2 is not as specialized as arg1. */
18153 if (!deduce1)
18154 lose2 = true;
18155 if (!deduce2)
18156 lose1 = true;
18157
18158 /* "If, for a given type, deduction succeeds in both directions
18159 (i.e., the types are identical after the transformations above)
18160 and both P and A were reference types (before being replaced with
18161 the type referred to above):
18162 - if the type from the argument template was an lvalue reference and
18163 the type from the parameter template was not, the argument type is
18164 considered to be more specialized than the other; otherwise,
18165 - if the type from the argument template is more cv-qualified
18166 than the type from the parameter template (as described above),
18167 the argument type is considered to be more specialized than the other;
18168 otherwise,
18169 - neither type is more specialized than the other." */
18170
18171 if (deduce1 && deduce2)
18172 {
18173 if (ref1 && ref2 && ref1 != ref2)
18174 {
18175 if (ref1 > ref2)
18176 lose1 = true;
18177 else
18178 lose2 = true;
18179 }
18180 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18181 {
18182 if ((quals1 & quals2) == quals2)
18183 lose2 = true;
18184 if ((quals1 & quals2) == quals1)
18185 lose1 = true;
18186 }
18187 }
18188
18189 if (lose1 && lose2)
18190 /* We've failed to deduce something in either direction.
18191 These must be unordered. */
18192 break;
18193
18194 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18195 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18196 /* We have already processed all of the arguments in our
18197 handing of the pack expansion type. */
18198 len = 0;
18199
18200 args1 = TREE_CHAIN (args1);
18201 args2 = TREE_CHAIN (args2);
18202 }
18203
18204 /* "In most cases, all template parameters must have values in order for
18205 deduction to succeed, but for partial ordering purposes a template
18206 parameter may remain without a value provided it is not used in the
18207 types being used for partial ordering."
18208
18209 Thus, if we are missing any of the targs1 we need to substitute into
18210 origs1, then pat2 is not as specialized as pat1. This can happen when
18211 there is a nondeduced context. */
18212 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18213 lose2 = true;
18214 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18215 lose1 = true;
18216
18217 processing_template_decl--;
18218
18219 /* All things being equal, if the next argument is a pack expansion
18220 for one function but not for the other, prefer the
18221 non-variadic function. FIXME this is bogus; see c++/41958. */
18222 if (lose1 == lose2
18223 && args1 && TREE_VALUE (args1)
18224 && args2 && TREE_VALUE (args2))
18225 {
18226 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18227 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18228 }
18229
18230 if (lose1 == lose2)
18231 return 0;
18232 else if (!lose1)
18233 return 1;
18234 else
18235 return -1;
18236 }
18237
18238 /* Determine which of two partial specializations of MAIN_TMPL is more
18239 specialized.
18240
18241 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18242 to the first partial specialization. The TREE_VALUE is the
18243 innermost set of template parameters for the partial
18244 specialization. PAT2 is similar, but for the second template.
18245
18246 Return 1 if the first partial specialization is more specialized;
18247 -1 if the second is more specialized; 0 if neither is more
18248 specialized.
18249
18250 See [temp.class.order] for information about determining which of
18251 two templates is more specialized. */
18252
18253 static int
18254 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18255 {
18256 tree targs;
18257 tree tmpl1, tmpl2;
18258 int winner = 0;
18259 bool any_deductions = false;
18260
18261 tmpl1 = TREE_TYPE (pat1);
18262 tmpl2 = TREE_TYPE (pat2);
18263
18264 /* Just like what happens for functions, if we are ordering between
18265 different class template specializations, we may encounter dependent
18266 types in the arguments, and we need our dependency check functions
18267 to behave correctly. */
18268 ++processing_template_decl;
18269 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18270 CLASSTYPE_TI_ARGS (tmpl1),
18271 CLASSTYPE_TI_ARGS (tmpl2));
18272 if (targs)
18273 {
18274 --winner;
18275 any_deductions = true;
18276 }
18277
18278 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18279 CLASSTYPE_TI_ARGS (tmpl2),
18280 CLASSTYPE_TI_ARGS (tmpl1));
18281 if (targs)
18282 {
18283 ++winner;
18284 any_deductions = true;
18285 }
18286 --processing_template_decl;
18287
18288 /* In the case of a tie where at least one of the class templates
18289 has a parameter pack at the end, the template with the most
18290 non-packed parameters wins. */
18291 if (winner == 0
18292 && any_deductions
18293 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18294 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18295 {
18296 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18297 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18298 int len1 = TREE_VEC_LENGTH (args1);
18299 int len2 = TREE_VEC_LENGTH (args2);
18300
18301 /* We don't count the pack expansion at the end. */
18302 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18303 --len1;
18304 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18305 --len2;
18306
18307 if (len1 > len2)
18308 return 1;
18309 else if (len1 < len2)
18310 return -1;
18311 }
18312
18313 return winner;
18314 }
18315
18316 /* Return the template arguments that will produce the function signature
18317 DECL from the function template FN, with the explicit template
18318 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18319 also match. Return NULL_TREE if no satisfactory arguments could be
18320 found. */
18321
18322 static tree
18323 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18324 {
18325 int ntparms = DECL_NTPARMS (fn);
18326 tree targs = make_tree_vec (ntparms);
18327 tree decl_type = TREE_TYPE (decl);
18328 tree decl_arg_types;
18329 tree *args;
18330 unsigned int nargs, ix;
18331 tree arg;
18332
18333 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18334
18335 /* Never do unification on the 'this' parameter. */
18336 decl_arg_types = skip_artificial_parms_for (decl,
18337 TYPE_ARG_TYPES (decl_type));
18338
18339 nargs = list_length (decl_arg_types);
18340 args = XALLOCAVEC (tree, nargs);
18341 for (arg = decl_arg_types, ix = 0;
18342 arg != NULL_TREE && arg != void_list_node;
18343 arg = TREE_CHAIN (arg), ++ix)
18344 args[ix] = TREE_VALUE (arg);
18345
18346 if (fn_type_unification (fn, explicit_args, targs,
18347 args, ix,
18348 (check_rettype || DECL_CONV_FN_P (fn)
18349 ? TREE_TYPE (decl_type) : NULL_TREE),
18350 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18351 /*decltype*/false)
18352 == error_mark_node)
18353 return NULL_TREE;
18354
18355 return targs;
18356 }
18357
18358 /* Return the innermost template arguments that, when applied to a partial
18359 specialization of MAIN_TMPL whose innermost template parameters are
18360 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18361 ARGS.
18362
18363 For example, suppose we have:
18364
18365 template <class T, class U> struct S {};
18366 template <class T> struct S<T*, int> {};
18367
18368 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18369 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18370 int}. The resulting vector will be {double}, indicating that `T'
18371 is bound to `double'. */
18372
18373 static tree
18374 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18375 {
18376 int i, ntparms = TREE_VEC_LENGTH (tparms);
18377 tree deduced_args;
18378 tree innermost_deduced_args;
18379
18380 innermost_deduced_args = make_tree_vec (ntparms);
18381 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18382 {
18383 deduced_args = copy_node (args);
18384 SET_TMPL_ARGS_LEVEL (deduced_args,
18385 TMPL_ARGS_DEPTH (deduced_args),
18386 innermost_deduced_args);
18387 }
18388 else
18389 deduced_args = innermost_deduced_args;
18390
18391 if (unify (tparms, deduced_args,
18392 INNERMOST_TEMPLATE_ARGS (spec_args),
18393 INNERMOST_TEMPLATE_ARGS (args),
18394 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18395 return NULL_TREE;
18396
18397 for (i = 0; i < ntparms; ++i)
18398 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18399 return NULL_TREE;
18400
18401 /* Verify that nondeduced template arguments agree with the type
18402 obtained from argument deduction.
18403
18404 For example:
18405
18406 struct A { typedef int X; };
18407 template <class T, class U> struct C {};
18408 template <class T> struct C<T, typename T::X> {};
18409
18410 Then with the instantiation `C<A, int>', we can deduce that
18411 `T' is `A' but unify () does not check whether `typename T::X'
18412 is `int'. */
18413 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18414 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18415 spec_args, main_tmpl,
18416 tf_none, false, false);
18417 if (spec_args == error_mark_node
18418 /* We only need to check the innermost arguments; the other
18419 arguments will always agree. */
18420 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18421 INNERMOST_TEMPLATE_ARGS (args)))
18422 return NULL_TREE;
18423
18424 /* Now that we have bindings for all of the template arguments,
18425 ensure that the arguments deduced for the template template
18426 parameters have compatible template parameter lists. See the use
18427 of template_template_parm_bindings_ok_p in fn_type_unification
18428 for more information. */
18429 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18430 return NULL_TREE;
18431
18432 return deduced_args;
18433 }
18434
18435 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18436 Return the TREE_LIST node with the most specialized template, if
18437 any. If there is no most specialized template, the error_mark_node
18438 is returned.
18439
18440 Note that this function does not look at, or modify, the
18441 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18442 returned is one of the elements of INSTANTIATIONS, callers may
18443 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18444 and retrieve it from the value returned. */
18445
18446 tree
18447 most_specialized_instantiation (tree templates)
18448 {
18449 tree fn, champ;
18450
18451 ++processing_template_decl;
18452
18453 champ = templates;
18454 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18455 {
18456 int fate = 0;
18457
18458 if (get_bindings (TREE_VALUE (champ),
18459 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18460 NULL_TREE, /*check_ret=*/true))
18461 fate--;
18462
18463 if (get_bindings (TREE_VALUE (fn),
18464 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18465 NULL_TREE, /*check_ret=*/true))
18466 fate++;
18467
18468 if (fate == -1)
18469 champ = fn;
18470 else if (!fate)
18471 {
18472 /* Equally specialized, move to next function. If there
18473 is no next function, nothing's most specialized. */
18474 fn = TREE_CHAIN (fn);
18475 champ = fn;
18476 if (!fn)
18477 break;
18478 }
18479 }
18480
18481 if (champ)
18482 /* Now verify that champ is better than everything earlier in the
18483 instantiation list. */
18484 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18485 if (get_bindings (TREE_VALUE (champ),
18486 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18487 NULL_TREE, /*check_ret=*/true)
18488 || !get_bindings (TREE_VALUE (fn),
18489 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18490 NULL_TREE, /*check_ret=*/true))
18491 {
18492 champ = NULL_TREE;
18493 break;
18494 }
18495
18496 processing_template_decl--;
18497
18498 if (!champ)
18499 return error_mark_node;
18500
18501 return champ;
18502 }
18503
18504 /* If DECL is a specialization of some template, return the most
18505 general such template. Otherwise, returns NULL_TREE.
18506
18507 For example, given:
18508
18509 template <class T> struct S { template <class U> void f(U); };
18510
18511 if TMPL is `template <class U> void S<int>::f(U)' this will return
18512 the full template. This function will not trace past partial
18513 specializations, however. For example, given in addition:
18514
18515 template <class T> struct S<T*> { template <class U> void f(U); };
18516
18517 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18518 `template <class T> template <class U> S<T*>::f(U)'. */
18519
18520 tree
18521 most_general_template (tree decl)
18522 {
18523 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18524 an immediate specialization. */
18525 if (TREE_CODE (decl) == FUNCTION_DECL)
18526 {
18527 if (DECL_TEMPLATE_INFO (decl)) {
18528 decl = DECL_TI_TEMPLATE (decl);
18529
18530 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18531 template friend. */
18532 if (TREE_CODE (decl) != TEMPLATE_DECL)
18533 return NULL_TREE;
18534 } else
18535 return NULL_TREE;
18536 }
18537
18538 /* Look for more and more general templates. */
18539 while (DECL_TEMPLATE_INFO (decl))
18540 {
18541 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18542 (See cp-tree.h for details.) */
18543 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18544 break;
18545
18546 if (CLASS_TYPE_P (TREE_TYPE (decl))
18547 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18548 break;
18549
18550 /* Stop if we run into an explicitly specialized class template. */
18551 if (!DECL_NAMESPACE_SCOPE_P (decl)
18552 && DECL_CONTEXT (decl)
18553 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18554 break;
18555
18556 decl = DECL_TI_TEMPLATE (decl);
18557 }
18558
18559 return decl;
18560 }
18561
18562 /* Return the most specialized of the class template partial
18563 specializations of TMPL which can produce TYPE, a specialization of
18564 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18565 a _TYPE node corresponding to the partial specialization, while the
18566 TREE_PURPOSE is the set of template arguments that must be
18567 substituted into the TREE_TYPE in order to generate TYPE.
18568
18569 If the choice of partial specialization is ambiguous, a diagnostic
18570 is issued, and the error_mark_node is returned. If there are no
18571 partial specializations of TMPL matching TYPE, then NULL_TREE is
18572 returned. */
18573
18574 static tree
18575 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18576 {
18577 tree list = NULL_TREE;
18578 tree t;
18579 tree champ;
18580 int fate;
18581 bool ambiguous_p;
18582 tree args;
18583 tree outer_args = NULL_TREE;
18584
18585 tmpl = most_general_template (tmpl);
18586 args = CLASSTYPE_TI_ARGS (type);
18587
18588 /* For determining which partial specialization to use, only the
18589 innermost args are interesting. */
18590 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18591 {
18592 outer_args = strip_innermost_template_args (args, 1);
18593 args = INNERMOST_TEMPLATE_ARGS (args);
18594 }
18595
18596 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18597 {
18598 tree partial_spec_args;
18599 tree spec_args;
18600 tree spec_tmpl = TREE_VALUE (t);
18601 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18602
18603 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18604
18605 ++processing_template_decl;
18606
18607 if (outer_args)
18608 {
18609 /* Discard the outer levels of args, and then substitute in the
18610 template args from the enclosing class. */
18611 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18612 partial_spec_args = tsubst_template_args
18613 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18614
18615 /* And the same for the partial specialization TEMPLATE_DECL. */
18616 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18617 }
18618
18619 partial_spec_args =
18620 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18621 add_to_template_args (outer_args,
18622 partial_spec_args),
18623 tmpl, tf_none,
18624 /*require_all_args=*/true,
18625 /*use_default_args=*/true);
18626
18627 --processing_template_decl;
18628
18629 if (partial_spec_args == error_mark_node)
18630 return error_mark_node;
18631 if (spec_tmpl == error_mark_node)
18632 return error_mark_node;
18633
18634 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18635 spec_args = get_class_bindings (tmpl, parms,
18636 partial_spec_args,
18637 args);
18638 if (spec_args)
18639 {
18640 if (outer_args)
18641 spec_args = add_to_template_args (outer_args, spec_args);
18642 list = tree_cons (spec_args, orig_parms, list);
18643 TREE_TYPE (list) = TREE_TYPE (t);
18644 }
18645 }
18646
18647 if (! list)
18648 return NULL_TREE;
18649
18650 ambiguous_p = false;
18651 t = list;
18652 champ = t;
18653 t = TREE_CHAIN (t);
18654 for (; t; t = TREE_CHAIN (t))
18655 {
18656 fate = more_specialized_class (tmpl, champ, t);
18657 if (fate == 1)
18658 ;
18659 else
18660 {
18661 if (fate == 0)
18662 {
18663 t = TREE_CHAIN (t);
18664 if (! t)
18665 {
18666 ambiguous_p = true;
18667 break;
18668 }
18669 }
18670 champ = t;
18671 }
18672 }
18673
18674 if (!ambiguous_p)
18675 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18676 {
18677 fate = more_specialized_class (tmpl, champ, t);
18678 if (fate != 1)
18679 {
18680 ambiguous_p = true;
18681 break;
18682 }
18683 }
18684
18685 if (ambiguous_p)
18686 {
18687 const char *str;
18688 char *spaces = NULL;
18689 if (!(complain & tf_error))
18690 return error_mark_node;
18691 error ("ambiguous class template instantiation for %q#T", type);
18692 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18693 for (t = list; t; t = TREE_CHAIN (t))
18694 {
18695 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18696 spaces = spaces ? spaces : get_spaces (str);
18697 }
18698 free (spaces);
18699 return error_mark_node;
18700 }
18701
18702 return champ;
18703 }
18704
18705 /* Explicitly instantiate DECL. */
18706
18707 void
18708 do_decl_instantiation (tree decl, tree storage)
18709 {
18710 tree result = NULL_TREE;
18711 int extern_p = 0;
18712
18713 if (!decl || decl == error_mark_node)
18714 /* An error occurred, for which grokdeclarator has already issued
18715 an appropriate message. */
18716 return;
18717 else if (! DECL_LANG_SPECIFIC (decl))
18718 {
18719 error ("explicit instantiation of non-template %q#D", decl);
18720 return;
18721 }
18722 else if (VAR_P (decl))
18723 {
18724 /* There is an asymmetry here in the way VAR_DECLs and
18725 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18726 the latter, the DECL we get back will be marked as a
18727 template instantiation, and the appropriate
18728 DECL_TEMPLATE_INFO will be set up. This does not happen for
18729 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18730 should handle VAR_DECLs as it currently handles
18731 FUNCTION_DECLs. */
18732 if (!DECL_CLASS_SCOPE_P (decl))
18733 {
18734 error ("%qD is not a static data member of a class template", decl);
18735 return;
18736 }
18737 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18738 if (!result || !VAR_P (result))
18739 {
18740 error ("no matching template for %qD found", decl);
18741 return;
18742 }
18743 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18744 {
18745 error ("type %qT for explicit instantiation %qD does not match "
18746 "declared type %qT", TREE_TYPE (result), decl,
18747 TREE_TYPE (decl));
18748 return;
18749 }
18750 }
18751 else if (TREE_CODE (decl) != FUNCTION_DECL)
18752 {
18753 error ("explicit instantiation of %q#D", decl);
18754 return;
18755 }
18756 else
18757 result = decl;
18758
18759 /* Check for various error cases. Note that if the explicit
18760 instantiation is valid the RESULT will currently be marked as an
18761 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18762 until we get here. */
18763
18764 if (DECL_TEMPLATE_SPECIALIZATION (result))
18765 {
18766 /* DR 259 [temp.spec].
18767
18768 Both an explicit instantiation and a declaration of an explicit
18769 specialization shall not appear in a program unless the explicit
18770 instantiation follows a declaration of the explicit specialization.
18771
18772 For a given set of template parameters, if an explicit
18773 instantiation of a template appears after a declaration of an
18774 explicit specialization for that template, the explicit
18775 instantiation has no effect. */
18776 return;
18777 }
18778 else if (DECL_EXPLICIT_INSTANTIATION (result))
18779 {
18780 /* [temp.spec]
18781
18782 No program shall explicitly instantiate any template more
18783 than once.
18784
18785 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18786 the first instantiation was `extern' and the second is not,
18787 and EXTERN_P for the opposite case. */
18788 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18789 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18790 /* If an "extern" explicit instantiation follows an ordinary
18791 explicit instantiation, the template is instantiated. */
18792 if (extern_p)
18793 return;
18794 }
18795 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18796 {
18797 error ("no matching template for %qD found", result);
18798 return;
18799 }
18800 else if (!DECL_TEMPLATE_INFO (result))
18801 {
18802 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18803 return;
18804 }
18805
18806 if (storage == NULL_TREE)
18807 ;
18808 else if (storage == ridpointers[(int) RID_EXTERN])
18809 {
18810 if (!in_system_header && (cxx_dialect == cxx98))
18811 pedwarn (input_location, OPT_Wpedantic,
18812 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18813 "instantiations");
18814 extern_p = 1;
18815 }
18816 else
18817 error ("storage class %qD applied to template instantiation", storage);
18818
18819 check_explicit_instantiation_namespace (result);
18820 mark_decl_instantiated (result, extern_p);
18821 if (! extern_p)
18822 instantiate_decl (result, /*defer_ok=*/1,
18823 /*expl_inst_class_mem_p=*/false);
18824 }
18825
18826 static void
18827 mark_class_instantiated (tree t, int extern_p)
18828 {
18829 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18830 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18831 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18832 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18833 if (! extern_p)
18834 {
18835 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18836 rest_of_type_compilation (t, 1);
18837 }
18838 }
18839
18840 /* Called from do_type_instantiation through binding_table_foreach to
18841 do recursive instantiation for the type bound in ENTRY. */
18842 static void
18843 bt_instantiate_type_proc (binding_entry entry, void *data)
18844 {
18845 tree storage = *(tree *) data;
18846
18847 if (MAYBE_CLASS_TYPE_P (entry->type)
18848 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18849 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18850 }
18851
18852 /* Called from do_type_instantiation to instantiate a member
18853 (a member function or a static member variable) of an
18854 explicitly instantiated class template. */
18855 static void
18856 instantiate_class_member (tree decl, int extern_p)
18857 {
18858 mark_decl_instantiated (decl, extern_p);
18859 if (! extern_p)
18860 instantiate_decl (decl, /*defer_ok=*/1,
18861 /*expl_inst_class_mem_p=*/true);
18862 }
18863
18864 /* Perform an explicit instantiation of template class T. STORAGE, if
18865 non-null, is the RID for extern, inline or static. COMPLAIN is
18866 nonzero if this is called from the parser, zero if called recursively,
18867 since the standard is unclear (as detailed below). */
18868
18869 void
18870 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18871 {
18872 int extern_p = 0;
18873 int nomem_p = 0;
18874 int static_p = 0;
18875 int previous_instantiation_extern_p = 0;
18876
18877 if (TREE_CODE (t) == TYPE_DECL)
18878 t = TREE_TYPE (t);
18879
18880 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18881 {
18882 tree tmpl =
18883 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18884 if (tmpl)
18885 error ("explicit instantiation of non-class template %qD", tmpl);
18886 else
18887 error ("explicit instantiation of non-template type %qT", t);
18888 return;
18889 }
18890
18891 complete_type (t);
18892
18893 if (!COMPLETE_TYPE_P (t))
18894 {
18895 if (complain & tf_error)
18896 error ("explicit instantiation of %q#T before definition of template",
18897 t);
18898 return;
18899 }
18900
18901 if (storage != NULL_TREE)
18902 {
18903 if (!in_system_header)
18904 {
18905 if (storage == ridpointers[(int) RID_EXTERN])
18906 {
18907 if (cxx_dialect == cxx98)
18908 pedwarn (input_location, OPT_Wpedantic,
18909 "ISO C++ 1998 forbids the use of %<extern%> on "
18910 "explicit instantiations");
18911 }
18912 else
18913 pedwarn (input_location, OPT_Wpedantic,
18914 "ISO C++ forbids the use of %qE"
18915 " on explicit instantiations", storage);
18916 }
18917
18918 if (storage == ridpointers[(int) RID_INLINE])
18919 nomem_p = 1;
18920 else if (storage == ridpointers[(int) RID_EXTERN])
18921 extern_p = 1;
18922 else if (storage == ridpointers[(int) RID_STATIC])
18923 static_p = 1;
18924 else
18925 {
18926 error ("storage class %qD applied to template instantiation",
18927 storage);
18928 extern_p = 0;
18929 }
18930 }
18931
18932 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18933 {
18934 /* DR 259 [temp.spec].
18935
18936 Both an explicit instantiation and a declaration of an explicit
18937 specialization shall not appear in a program unless the explicit
18938 instantiation follows a declaration of the explicit specialization.
18939
18940 For a given set of template parameters, if an explicit
18941 instantiation of a template appears after a declaration of an
18942 explicit specialization for that template, the explicit
18943 instantiation has no effect. */
18944 return;
18945 }
18946 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18947 {
18948 /* [temp.spec]
18949
18950 No program shall explicitly instantiate any template more
18951 than once.
18952
18953 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18954 instantiation was `extern'. If EXTERN_P then the second is.
18955 These cases are OK. */
18956 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18957
18958 if (!previous_instantiation_extern_p && !extern_p
18959 && (complain & tf_error))
18960 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18961
18962 /* If we've already instantiated the template, just return now. */
18963 if (!CLASSTYPE_INTERFACE_ONLY (t))
18964 return;
18965 }
18966
18967 check_explicit_instantiation_namespace (TYPE_NAME (t));
18968 mark_class_instantiated (t, extern_p);
18969
18970 if (nomem_p)
18971 return;
18972
18973 {
18974 tree tmp;
18975
18976 /* In contrast to implicit instantiation, where only the
18977 declarations, and not the definitions, of members are
18978 instantiated, we have here:
18979
18980 [temp.explicit]
18981
18982 The explicit instantiation of a class template specialization
18983 implies the instantiation of all of its members not
18984 previously explicitly specialized in the translation unit
18985 containing the explicit instantiation.
18986
18987 Of course, we can't instantiate member template classes, since
18988 we don't have any arguments for them. Note that the standard
18989 is unclear on whether the instantiation of the members are
18990 *explicit* instantiations or not. However, the most natural
18991 interpretation is that it should be an explicit instantiation. */
18992
18993 if (! static_p)
18994 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18995 if (TREE_CODE (tmp) == FUNCTION_DECL
18996 && DECL_TEMPLATE_INSTANTIATION (tmp))
18997 instantiate_class_member (tmp, extern_p);
18998
18999 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19000 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19001 instantiate_class_member (tmp, extern_p);
19002
19003 if (CLASSTYPE_NESTED_UTDS (t))
19004 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19005 bt_instantiate_type_proc, &storage);
19006 }
19007 }
19008
19009 /* Given a function DECL, which is a specialization of TMPL, modify
19010 DECL to be a re-instantiation of TMPL with the same template
19011 arguments. TMPL should be the template into which tsubst'ing
19012 should occur for DECL, not the most general template.
19013
19014 One reason for doing this is a scenario like this:
19015
19016 template <class T>
19017 void f(const T&, int i);
19018
19019 void g() { f(3, 7); }
19020
19021 template <class T>
19022 void f(const T& t, const int i) { }
19023
19024 Note that when the template is first instantiated, with
19025 instantiate_template, the resulting DECL will have no name for the
19026 first parameter, and the wrong type for the second. So, when we go
19027 to instantiate the DECL, we regenerate it. */
19028
19029 static void
19030 regenerate_decl_from_template (tree decl, tree tmpl)
19031 {
19032 /* The arguments used to instantiate DECL, from the most general
19033 template. */
19034 tree args;
19035 tree code_pattern;
19036
19037 args = DECL_TI_ARGS (decl);
19038 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19039
19040 /* Make sure that we can see identifiers, and compute access
19041 correctly. */
19042 push_access_scope (decl);
19043
19044 if (TREE_CODE (decl) == FUNCTION_DECL)
19045 {
19046 tree decl_parm;
19047 tree pattern_parm;
19048 tree specs;
19049 int args_depth;
19050 int parms_depth;
19051
19052 args_depth = TMPL_ARGS_DEPTH (args);
19053 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19054 if (args_depth > parms_depth)
19055 args = get_innermost_template_args (args, parms_depth);
19056
19057 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19058 args, tf_error, NULL_TREE,
19059 /*defer_ok*/false);
19060 if (specs && specs != error_mark_node)
19061 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19062 specs);
19063
19064 /* Merge parameter declarations. */
19065 decl_parm = skip_artificial_parms_for (decl,
19066 DECL_ARGUMENTS (decl));
19067 pattern_parm
19068 = skip_artificial_parms_for (code_pattern,
19069 DECL_ARGUMENTS (code_pattern));
19070 while (decl_parm && !DECL_PACK_P (pattern_parm))
19071 {
19072 tree parm_type;
19073 tree attributes;
19074
19075 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19076 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19077 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19078 NULL_TREE);
19079 parm_type = type_decays_to (parm_type);
19080 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19081 TREE_TYPE (decl_parm) = parm_type;
19082 attributes = DECL_ATTRIBUTES (pattern_parm);
19083 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19084 {
19085 DECL_ATTRIBUTES (decl_parm) = attributes;
19086 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19087 }
19088 decl_parm = DECL_CHAIN (decl_parm);
19089 pattern_parm = DECL_CHAIN (pattern_parm);
19090 }
19091 /* Merge any parameters that match with the function parameter
19092 pack. */
19093 if (pattern_parm && DECL_PACK_P (pattern_parm))
19094 {
19095 int i, len;
19096 tree expanded_types;
19097 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19098 the parameters in this function parameter pack. */
19099 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19100 args, tf_error, NULL_TREE);
19101 len = TREE_VEC_LENGTH (expanded_types);
19102 for (i = 0; i < len; i++)
19103 {
19104 tree parm_type;
19105 tree attributes;
19106
19107 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19108 /* Rename the parameter to include the index. */
19109 DECL_NAME (decl_parm) =
19110 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19111 parm_type = TREE_VEC_ELT (expanded_types, i);
19112 parm_type = type_decays_to (parm_type);
19113 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19114 TREE_TYPE (decl_parm) = parm_type;
19115 attributes = DECL_ATTRIBUTES (pattern_parm);
19116 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19117 {
19118 DECL_ATTRIBUTES (decl_parm) = attributes;
19119 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19120 }
19121 decl_parm = DECL_CHAIN (decl_parm);
19122 }
19123 }
19124 /* Merge additional specifiers from the CODE_PATTERN. */
19125 if (DECL_DECLARED_INLINE_P (code_pattern)
19126 && !DECL_DECLARED_INLINE_P (decl))
19127 DECL_DECLARED_INLINE_P (decl) = 1;
19128 }
19129 else if (VAR_P (decl))
19130 {
19131 DECL_INITIAL (decl) =
19132 tsubst_expr (DECL_INITIAL (code_pattern), args,
19133 tf_error, DECL_TI_TEMPLATE (decl),
19134 /*integral_constant_expression_p=*/false);
19135 if (VAR_HAD_UNKNOWN_BOUND (decl))
19136 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19137 tf_error, DECL_TI_TEMPLATE (decl));
19138 }
19139 else
19140 gcc_unreachable ();
19141
19142 pop_access_scope (decl);
19143 }
19144
19145 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19146 substituted to get DECL. */
19147
19148 tree
19149 template_for_substitution (tree decl)
19150 {
19151 tree tmpl = DECL_TI_TEMPLATE (decl);
19152
19153 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19154 for the instantiation. This is not always the most general
19155 template. Consider, for example:
19156
19157 template <class T>
19158 struct S { template <class U> void f();
19159 template <> void f<int>(); };
19160
19161 and an instantiation of S<double>::f<int>. We want TD to be the
19162 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19163 while (/* An instantiation cannot have a definition, so we need a
19164 more general template. */
19165 DECL_TEMPLATE_INSTANTIATION (tmpl)
19166 /* We must also deal with friend templates. Given:
19167
19168 template <class T> struct S {
19169 template <class U> friend void f() {};
19170 };
19171
19172 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19173 so far as the language is concerned, but that's still
19174 where we get the pattern for the instantiation from. On
19175 other hand, if the definition comes outside the class, say:
19176
19177 template <class T> struct S {
19178 template <class U> friend void f();
19179 };
19180 template <class U> friend void f() {}
19181
19182 we don't need to look any further. That's what the check for
19183 DECL_INITIAL is for. */
19184 || (TREE_CODE (decl) == FUNCTION_DECL
19185 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19186 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19187 {
19188 /* The present template, TD, should not be a definition. If it
19189 were a definition, we should be using it! Note that we
19190 cannot restructure the loop to just keep going until we find
19191 a template with a definition, since that might go too far if
19192 a specialization was declared, but not defined. */
19193 gcc_assert (!VAR_P (decl)
19194 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19195
19196 /* Fetch the more general template. */
19197 tmpl = DECL_TI_TEMPLATE (tmpl);
19198 }
19199
19200 return tmpl;
19201 }
19202
19203 /* Returns true if we need to instantiate this template instance even if we
19204 know we aren't going to emit it.. */
19205
19206 bool
19207 always_instantiate_p (tree decl)
19208 {
19209 /* We always instantiate inline functions so that we can inline them. An
19210 explicit instantiation declaration prohibits implicit instantiation of
19211 non-inline functions. With high levels of optimization, we would
19212 normally inline non-inline functions -- but we're not allowed to do
19213 that for "extern template" functions. Therefore, we check
19214 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19215 return ((TREE_CODE (decl) == FUNCTION_DECL
19216 && (DECL_DECLARED_INLINE_P (decl)
19217 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19218 /* And we need to instantiate static data members so that
19219 their initializers are available in integral constant
19220 expressions. */
19221 || (VAR_P (decl)
19222 && decl_maybe_constant_var_p (decl)));
19223 }
19224
19225 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19226 instantiate it now, modifying TREE_TYPE (fn). */
19227
19228 void
19229 maybe_instantiate_noexcept (tree fn)
19230 {
19231 tree fntype, spec, noex, clone;
19232
19233 if (DECL_CLONED_FUNCTION_P (fn))
19234 fn = DECL_CLONED_FUNCTION (fn);
19235 fntype = TREE_TYPE (fn);
19236 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19237
19238 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19239 return;
19240
19241 noex = TREE_PURPOSE (spec);
19242
19243 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19244 {
19245 if (push_tinst_level (fn))
19246 {
19247 push_access_scope (fn);
19248 push_deferring_access_checks (dk_no_deferred);
19249 input_location = DECL_SOURCE_LOCATION (fn);
19250 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19251 DEFERRED_NOEXCEPT_ARGS (noex),
19252 tf_warning_or_error, fn,
19253 /*function_p=*/false,
19254 /*integral_constant_expression_p=*/true);
19255 pop_deferring_access_checks ();
19256 pop_access_scope (fn);
19257 pop_tinst_level ();
19258 spec = build_noexcept_spec (noex, tf_warning_or_error);
19259 if (spec == error_mark_node)
19260 spec = noexcept_false_spec;
19261 }
19262 else
19263 spec = noexcept_false_spec;
19264 }
19265 else
19266 {
19267 /* This is an implicitly declared function, so NOEX is a list of
19268 other functions to evaluate and merge. */
19269 tree elt;
19270 spec = noexcept_true_spec;
19271 for (elt = noex; elt; elt = OVL_NEXT (elt))
19272 {
19273 tree fn = OVL_CURRENT (elt);
19274 tree subspec;
19275 maybe_instantiate_noexcept (fn);
19276 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19277 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19278 }
19279 }
19280
19281 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19282
19283 FOR_EACH_CLONE (clone, fn)
19284 {
19285 if (TREE_TYPE (clone) == fntype)
19286 TREE_TYPE (clone) = TREE_TYPE (fn);
19287 else
19288 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19289 }
19290 }
19291
19292 /* Produce the definition of D, a _DECL generated from a template. If
19293 DEFER_OK is nonzero, then we don't have to actually do the
19294 instantiation now; we just have to do it sometime. Normally it is
19295 an error if this is an explicit instantiation but D is undefined.
19296 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19297 explicitly instantiated class template. */
19298
19299 tree
19300 instantiate_decl (tree d, int defer_ok,
19301 bool expl_inst_class_mem_p)
19302 {
19303 tree tmpl = DECL_TI_TEMPLATE (d);
19304 tree gen_args;
19305 tree args;
19306 tree td;
19307 tree code_pattern;
19308 tree spec;
19309 tree gen_tmpl;
19310 bool pattern_defined;
19311 location_t saved_loc = input_location;
19312 int saved_unevaluated_operand = cp_unevaluated_operand;
19313 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19314 bool external_p;
19315 tree fn_context;
19316 bool nested;
19317
19318 /* This function should only be used to instantiate templates for
19319 functions and static member variables. */
19320 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19321
19322 /* Variables are never deferred; if instantiation is required, they
19323 are instantiated right away. That allows for better code in the
19324 case that an expression refers to the value of the variable --
19325 if the variable has a constant value the referring expression can
19326 take advantage of that fact. */
19327 if (VAR_P (d)
19328 || DECL_DECLARED_CONSTEXPR_P (d))
19329 defer_ok = 0;
19330
19331 /* Don't instantiate cloned functions. Instead, instantiate the
19332 functions they cloned. */
19333 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19334 d = DECL_CLONED_FUNCTION (d);
19335
19336 if (DECL_TEMPLATE_INSTANTIATED (d)
19337 || (TREE_CODE (d) == FUNCTION_DECL
19338 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19339 || DECL_TEMPLATE_SPECIALIZATION (d))
19340 /* D has already been instantiated or explicitly specialized, so
19341 there's nothing for us to do here.
19342
19343 It might seem reasonable to check whether or not D is an explicit
19344 instantiation, and, if so, stop here. But when an explicit
19345 instantiation is deferred until the end of the compilation,
19346 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19347 the instantiation. */
19348 return d;
19349
19350 /* Check to see whether we know that this template will be
19351 instantiated in some other file, as with "extern template"
19352 extension. */
19353 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19354
19355 /* In general, we do not instantiate such templates. */
19356 if (external_p && !always_instantiate_p (d))
19357 return d;
19358
19359 gen_tmpl = most_general_template (tmpl);
19360 gen_args = DECL_TI_ARGS (d);
19361
19362 if (tmpl != gen_tmpl)
19363 /* We should already have the extra args. */
19364 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19365 == TMPL_ARGS_DEPTH (gen_args));
19366 /* And what's in the hash table should match D. */
19367 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19368 || spec == NULL_TREE);
19369
19370 /* This needs to happen before any tsubsting. */
19371 if (! push_tinst_level (d))
19372 return d;
19373
19374 timevar_push (TV_TEMPLATE_INST);
19375
19376 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19377 for the instantiation. */
19378 td = template_for_substitution (d);
19379 code_pattern = DECL_TEMPLATE_RESULT (td);
19380
19381 /* We should never be trying to instantiate a member of a class
19382 template or partial specialization. */
19383 gcc_assert (d != code_pattern);
19384
19385 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19386 || DECL_TEMPLATE_SPECIALIZATION (td))
19387 /* In the case of a friend template whose definition is provided
19388 outside the class, we may have too many arguments. Drop the
19389 ones we don't need. The same is true for specializations. */
19390 args = get_innermost_template_args
19391 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19392 else
19393 args = gen_args;
19394
19395 if (TREE_CODE (d) == FUNCTION_DECL)
19396 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19397 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19398 else
19399 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19400
19401 /* We may be in the middle of deferred access check. Disable it now. */
19402 push_deferring_access_checks (dk_no_deferred);
19403
19404 /* Unless an explicit instantiation directive has already determined
19405 the linkage of D, remember that a definition is available for
19406 this entity. */
19407 if (pattern_defined
19408 && !DECL_INTERFACE_KNOWN (d)
19409 && !DECL_NOT_REALLY_EXTERN (d))
19410 mark_definable (d);
19411
19412 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19413 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19414 input_location = DECL_SOURCE_LOCATION (d);
19415
19416 /* If D is a member of an explicitly instantiated class template,
19417 and no definition is available, treat it like an implicit
19418 instantiation. */
19419 if (!pattern_defined && expl_inst_class_mem_p
19420 && DECL_EXPLICIT_INSTANTIATION (d))
19421 {
19422 /* Leave linkage flags alone on instantiations with anonymous
19423 visibility. */
19424 if (TREE_PUBLIC (d))
19425 {
19426 DECL_NOT_REALLY_EXTERN (d) = 0;
19427 DECL_INTERFACE_KNOWN (d) = 0;
19428 }
19429 SET_DECL_IMPLICIT_INSTANTIATION (d);
19430 }
19431
19432 if (TREE_CODE (d) == FUNCTION_DECL)
19433 maybe_instantiate_noexcept (d);
19434
19435 /* Defer all other templates, unless we have been explicitly
19436 forbidden from doing so. */
19437 if (/* If there is no definition, we cannot instantiate the
19438 template. */
19439 ! pattern_defined
19440 /* If it's OK to postpone instantiation, do so. */
19441 || defer_ok
19442 /* If this is a static data member that will be defined
19443 elsewhere, we don't want to instantiate the entire data
19444 member, but we do want to instantiate the initializer so that
19445 we can substitute that elsewhere. */
19446 || (external_p && VAR_P (d)))
19447 {
19448 /* The definition of the static data member is now required so
19449 we must substitute the initializer. */
19450 if (VAR_P (d)
19451 && !DECL_INITIAL (d)
19452 && DECL_INITIAL (code_pattern))
19453 {
19454 tree ns;
19455 tree init;
19456 bool const_init = false;
19457
19458 ns = decl_namespace_context (d);
19459 push_nested_namespace (ns);
19460 push_nested_class (DECL_CONTEXT (d));
19461 init = tsubst_expr (DECL_INITIAL (code_pattern),
19462 args,
19463 tf_warning_or_error, NULL_TREE,
19464 /*integral_constant_expression_p=*/false);
19465 /* Make sure the initializer is still constant, in case of
19466 circular dependency (template/instantiate6.C). */
19467 const_init
19468 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19469 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19470 /*asmspec_tree=*/NULL_TREE,
19471 LOOKUP_ONLYCONVERTING);
19472 pop_nested_class ();
19473 pop_nested_namespace (ns);
19474 }
19475
19476 /* We restore the source position here because it's used by
19477 add_pending_template. */
19478 input_location = saved_loc;
19479
19480 if (at_eof && !pattern_defined
19481 && DECL_EXPLICIT_INSTANTIATION (d)
19482 && DECL_NOT_REALLY_EXTERN (d))
19483 /* [temp.explicit]
19484
19485 The definition of a non-exported function template, a
19486 non-exported member function template, or a non-exported
19487 member function or static data member of a class template
19488 shall be present in every translation unit in which it is
19489 explicitly instantiated. */
19490 permerror (input_location, "explicit instantiation of %qD "
19491 "but no definition available", d);
19492
19493 /* If we're in unevaluated context, we just wanted to get the
19494 constant value; this isn't an odr use, so don't queue
19495 a full instantiation. */
19496 if (cp_unevaluated_operand != 0)
19497 goto out;
19498 /* ??? Historically, we have instantiated inline functions, even
19499 when marked as "extern template". */
19500 if (!(external_p && VAR_P (d)))
19501 add_pending_template (d);
19502 goto out;
19503 }
19504 /* Tell the repository that D is available in this translation unit
19505 -- and see if it is supposed to be instantiated here. */
19506 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19507 {
19508 /* In a PCH file, despite the fact that the repository hasn't
19509 requested instantiation in the PCH it is still possible that
19510 an instantiation will be required in a file that includes the
19511 PCH. */
19512 if (pch_file)
19513 add_pending_template (d);
19514 /* Instantiate inline functions so that the inliner can do its
19515 job, even though we'll not be emitting a copy of this
19516 function. */
19517 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19518 goto out;
19519 }
19520
19521 fn_context = decl_function_context (d);
19522 nested = (current_function_decl != NULL_TREE);
19523 if (!fn_context)
19524 push_to_top_level ();
19525 else
19526 {
19527 if (nested)
19528 push_function_context ();
19529 cp_unevaluated_operand = 0;
19530 c_inhibit_evaluation_warnings = 0;
19531 }
19532
19533 /* Mark D as instantiated so that recursive calls to
19534 instantiate_decl do not try to instantiate it again. */
19535 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19536
19537 /* Regenerate the declaration in case the template has been modified
19538 by a subsequent redeclaration. */
19539 regenerate_decl_from_template (d, td);
19540
19541 /* We already set the file and line above. Reset them now in case
19542 they changed as a result of calling regenerate_decl_from_template. */
19543 input_location = DECL_SOURCE_LOCATION (d);
19544
19545 if (VAR_P (d))
19546 {
19547 tree init;
19548 bool const_init = false;
19549
19550 /* Clear out DECL_RTL; whatever was there before may not be right
19551 since we've reset the type of the declaration. */
19552 SET_DECL_RTL (d, NULL);
19553 DECL_IN_AGGR_P (d) = 0;
19554
19555 /* The initializer is placed in DECL_INITIAL by
19556 regenerate_decl_from_template so we don't need to
19557 push/pop_access_scope again here. Pull it out so that
19558 cp_finish_decl can process it. */
19559 init = DECL_INITIAL (d);
19560 DECL_INITIAL (d) = NULL_TREE;
19561 DECL_INITIALIZED_P (d) = 0;
19562
19563 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19564 initializer. That function will defer actual emission until
19565 we have a chance to determine linkage. */
19566 DECL_EXTERNAL (d) = 0;
19567
19568 /* Enter the scope of D so that access-checking works correctly. */
19569 push_nested_class (DECL_CONTEXT (d));
19570 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19571 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19572 pop_nested_class ();
19573 }
19574 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19575 synthesize_method (d);
19576 else if (TREE_CODE (d) == FUNCTION_DECL)
19577 {
19578 struct pointer_map_t *saved_local_specializations;
19579 tree subst_decl;
19580 tree tmpl_parm;
19581 tree spec_parm;
19582 tree block = NULL_TREE;
19583
19584 /* Save away the current list, in case we are instantiating one
19585 template from within the body of another. */
19586 saved_local_specializations = local_specializations;
19587
19588 /* Set up the list of local specializations. */
19589 local_specializations = pointer_map_create ();
19590
19591 /* Set up context. */
19592 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19593 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19594 block = push_stmt_list ();
19595 else
19596 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19597
19598 /* Some typedefs referenced from within the template code need to be
19599 access checked at template instantiation time, i.e now. These
19600 types were added to the template at parsing time. Let's get those
19601 and perform the access checks then. */
19602 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19603 gen_args);
19604
19605 /* Create substitution entries for the parameters. */
19606 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19607 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19608 spec_parm = DECL_ARGUMENTS (d);
19609 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19610 {
19611 register_local_specialization (spec_parm, tmpl_parm);
19612 spec_parm = skip_artificial_parms_for (d, spec_parm);
19613 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19614 }
19615 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19616 {
19617 if (!DECL_PACK_P (tmpl_parm))
19618 {
19619 register_local_specialization (spec_parm, tmpl_parm);
19620 spec_parm = DECL_CHAIN (spec_parm);
19621 }
19622 else
19623 {
19624 /* Register the (value) argument pack as a specialization of
19625 TMPL_PARM, then move on. */
19626 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19627 register_local_specialization (argpack, tmpl_parm);
19628 }
19629 }
19630 gcc_assert (!spec_parm);
19631
19632 /* Substitute into the body of the function. */
19633 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19634 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19635 tf_warning_or_error, tmpl);
19636 else
19637 {
19638 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19639 tf_warning_or_error, tmpl,
19640 /*integral_constant_expression_p=*/false);
19641
19642 /* Set the current input_location to the end of the function
19643 so that finish_function knows where we are. */
19644 input_location
19645 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19646 }
19647
19648 /* We don't need the local specializations any more. */
19649 pointer_map_destroy (local_specializations);
19650 local_specializations = saved_local_specializations;
19651
19652 /* Finish the function. */
19653 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19654 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19655 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19656 else
19657 {
19658 d = finish_function (0);
19659 expand_or_defer_fn (d);
19660 }
19661
19662 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19663 cp_check_omp_declare_reduction (d);
19664 }
19665
19666 /* We're not deferring instantiation any more. */
19667 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19668
19669 if (!fn_context)
19670 pop_from_top_level ();
19671 else if (nested)
19672 pop_function_context ();
19673
19674 out:
19675 input_location = saved_loc;
19676 cp_unevaluated_operand = saved_unevaluated_operand;
19677 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19678 pop_deferring_access_checks ();
19679 pop_tinst_level ();
19680
19681 timevar_pop (TV_TEMPLATE_INST);
19682
19683 return d;
19684 }
19685
19686 /* Run through the list of templates that we wish we could
19687 instantiate, and instantiate any we can. RETRIES is the
19688 number of times we retry pending template instantiation. */
19689
19690 void
19691 instantiate_pending_templates (int retries)
19692 {
19693 int reconsider;
19694 location_t saved_loc = input_location;
19695
19696 /* Instantiating templates may trigger vtable generation. This in turn
19697 may require further template instantiations. We place a limit here
19698 to avoid infinite loop. */
19699 if (pending_templates && retries >= max_tinst_depth)
19700 {
19701 tree decl = pending_templates->tinst->decl;
19702
19703 error ("template instantiation depth exceeds maximum of %d"
19704 " instantiating %q+D, possibly from virtual table generation"
19705 " (use -ftemplate-depth= to increase the maximum)",
19706 max_tinst_depth, decl);
19707 if (TREE_CODE (decl) == FUNCTION_DECL)
19708 /* Pretend that we defined it. */
19709 DECL_INITIAL (decl) = error_mark_node;
19710 return;
19711 }
19712
19713 do
19714 {
19715 struct pending_template **t = &pending_templates;
19716 struct pending_template *last = NULL;
19717 reconsider = 0;
19718 while (*t)
19719 {
19720 tree instantiation = reopen_tinst_level ((*t)->tinst);
19721 bool complete = false;
19722
19723 if (TYPE_P (instantiation))
19724 {
19725 tree fn;
19726
19727 if (!COMPLETE_TYPE_P (instantiation))
19728 {
19729 instantiate_class_template (instantiation);
19730 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19731 for (fn = TYPE_METHODS (instantiation);
19732 fn;
19733 fn = TREE_CHAIN (fn))
19734 if (! DECL_ARTIFICIAL (fn))
19735 instantiate_decl (fn,
19736 /*defer_ok=*/0,
19737 /*expl_inst_class_mem_p=*/false);
19738 if (COMPLETE_TYPE_P (instantiation))
19739 reconsider = 1;
19740 }
19741
19742 complete = COMPLETE_TYPE_P (instantiation);
19743 }
19744 else
19745 {
19746 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19747 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19748 {
19749 instantiation
19750 = instantiate_decl (instantiation,
19751 /*defer_ok=*/0,
19752 /*expl_inst_class_mem_p=*/false);
19753 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19754 reconsider = 1;
19755 }
19756
19757 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19758 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19759 }
19760
19761 if (complete)
19762 /* If INSTANTIATION has been instantiated, then we don't
19763 need to consider it again in the future. */
19764 *t = (*t)->next;
19765 else
19766 {
19767 last = *t;
19768 t = &(*t)->next;
19769 }
19770 tinst_depth = 0;
19771 current_tinst_level = NULL;
19772 }
19773 last_pending_template = last;
19774 }
19775 while (reconsider);
19776
19777 input_location = saved_loc;
19778 }
19779
19780 /* Substitute ARGVEC into T, which is a list of initializers for
19781 either base class or a non-static data member. The TREE_PURPOSEs
19782 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19783 instantiate_decl. */
19784
19785 static tree
19786 tsubst_initializer_list (tree t, tree argvec)
19787 {
19788 tree inits = NULL_TREE;
19789
19790 for (; t; t = TREE_CHAIN (t))
19791 {
19792 tree decl;
19793 tree init;
19794 tree expanded_bases = NULL_TREE;
19795 tree expanded_arguments = NULL_TREE;
19796 int i, len = 1;
19797
19798 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19799 {
19800 tree expr;
19801 tree arg;
19802
19803 /* Expand the base class expansion type into separate base
19804 classes. */
19805 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19806 tf_warning_or_error,
19807 NULL_TREE);
19808 if (expanded_bases == error_mark_node)
19809 continue;
19810
19811 /* We'll be building separate TREE_LISTs of arguments for
19812 each base. */
19813 len = TREE_VEC_LENGTH (expanded_bases);
19814 expanded_arguments = make_tree_vec (len);
19815 for (i = 0; i < len; i++)
19816 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19817
19818 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19819 expand each argument in the TREE_VALUE of t. */
19820 expr = make_node (EXPR_PACK_EXPANSION);
19821 PACK_EXPANSION_LOCAL_P (expr) = true;
19822 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19823 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19824
19825 if (TREE_VALUE (t) == void_type_node)
19826 /* VOID_TYPE_NODE is used to indicate
19827 value-initialization. */
19828 {
19829 for (i = 0; i < len; i++)
19830 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19831 }
19832 else
19833 {
19834 /* Substitute parameter packs into each argument in the
19835 TREE_LIST. */
19836 in_base_initializer = 1;
19837 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19838 {
19839 tree expanded_exprs;
19840
19841 /* Expand the argument. */
19842 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19843 expanded_exprs
19844 = tsubst_pack_expansion (expr, argvec,
19845 tf_warning_or_error,
19846 NULL_TREE);
19847 if (expanded_exprs == error_mark_node)
19848 continue;
19849
19850 /* Prepend each of the expanded expressions to the
19851 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19852 for (i = 0; i < len; i++)
19853 {
19854 TREE_VEC_ELT (expanded_arguments, i) =
19855 tree_cons (NULL_TREE,
19856 TREE_VEC_ELT (expanded_exprs, i),
19857 TREE_VEC_ELT (expanded_arguments, i));
19858 }
19859 }
19860 in_base_initializer = 0;
19861
19862 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19863 since we built them backwards. */
19864 for (i = 0; i < len; i++)
19865 {
19866 TREE_VEC_ELT (expanded_arguments, i) =
19867 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19868 }
19869 }
19870 }
19871
19872 for (i = 0; i < len; ++i)
19873 {
19874 if (expanded_bases)
19875 {
19876 decl = TREE_VEC_ELT (expanded_bases, i);
19877 decl = expand_member_init (decl);
19878 init = TREE_VEC_ELT (expanded_arguments, i);
19879 }
19880 else
19881 {
19882 tree tmp;
19883 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19884 tf_warning_or_error, NULL_TREE);
19885
19886 decl = expand_member_init (decl);
19887 if (decl && !DECL_P (decl))
19888 in_base_initializer = 1;
19889
19890 init = TREE_VALUE (t);
19891 tmp = init;
19892 if (init != void_type_node)
19893 init = tsubst_expr (init, argvec,
19894 tf_warning_or_error, NULL_TREE,
19895 /*integral_constant_expression_p=*/false);
19896 if (init == NULL_TREE && tmp != NULL_TREE)
19897 /* If we had an initializer but it instantiated to nothing,
19898 value-initialize the object. This will only occur when
19899 the initializer was a pack expansion where the parameter
19900 packs used in that expansion were of length zero. */
19901 init = void_type_node;
19902 in_base_initializer = 0;
19903 }
19904
19905 if (decl)
19906 {
19907 init = build_tree_list (decl, init);
19908 TREE_CHAIN (init) = inits;
19909 inits = init;
19910 }
19911 }
19912 }
19913 return inits;
19914 }
19915
19916 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19917
19918 static void
19919 set_current_access_from_decl (tree decl)
19920 {
19921 if (TREE_PRIVATE (decl))
19922 current_access_specifier = access_private_node;
19923 else if (TREE_PROTECTED (decl))
19924 current_access_specifier = access_protected_node;
19925 else
19926 current_access_specifier = access_public_node;
19927 }
19928
19929 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19930 is the instantiation (which should have been created with
19931 start_enum) and ARGS are the template arguments to use. */
19932
19933 static void
19934 tsubst_enum (tree tag, tree newtag, tree args)
19935 {
19936 tree e;
19937
19938 if (SCOPED_ENUM_P (newtag))
19939 begin_scope (sk_scoped_enum, newtag);
19940
19941 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19942 {
19943 tree value;
19944 tree decl;
19945
19946 decl = TREE_VALUE (e);
19947 /* Note that in a template enum, the TREE_VALUE is the
19948 CONST_DECL, not the corresponding INTEGER_CST. */
19949 value = tsubst_expr (DECL_INITIAL (decl),
19950 args, tf_warning_or_error, NULL_TREE,
19951 /*integral_constant_expression_p=*/true);
19952
19953 /* Give this enumeration constant the correct access. */
19954 set_current_access_from_decl (decl);
19955
19956 /* Actually build the enumerator itself. */
19957 build_enumerator
19958 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19959 }
19960
19961 if (SCOPED_ENUM_P (newtag))
19962 finish_scope ();
19963
19964 finish_enum_value_list (newtag);
19965 finish_enum (newtag);
19966
19967 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19968 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19969 }
19970
19971 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19972 its type -- but without substituting the innermost set of template
19973 arguments. So, innermost set of template parameters will appear in
19974 the type. */
19975
19976 tree
19977 get_mostly_instantiated_function_type (tree decl)
19978 {
19979 tree fn_type;
19980 tree tmpl;
19981 tree targs;
19982 tree tparms;
19983 int parm_depth;
19984
19985 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19986 targs = DECL_TI_ARGS (decl);
19987 tparms = DECL_TEMPLATE_PARMS (tmpl);
19988 parm_depth = TMPL_PARMS_DEPTH (tparms);
19989
19990 /* There should be as many levels of arguments as there are levels
19991 of parameters. */
19992 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19993
19994 fn_type = TREE_TYPE (tmpl);
19995
19996 if (parm_depth == 1)
19997 /* No substitution is necessary. */
19998 ;
19999 else
20000 {
20001 int i;
20002 tree partial_args;
20003
20004 /* Replace the innermost level of the TARGS with NULL_TREEs to
20005 let tsubst know not to substitute for those parameters. */
20006 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20007 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20008 SET_TMPL_ARGS_LEVEL (partial_args, i,
20009 TMPL_ARGS_LEVEL (targs, i));
20010 SET_TMPL_ARGS_LEVEL (partial_args,
20011 TMPL_ARGS_DEPTH (targs),
20012 make_tree_vec (DECL_NTPARMS (tmpl)));
20013
20014 /* Make sure that we can see identifiers, and compute access
20015 correctly. */
20016 push_access_scope (decl);
20017
20018 ++processing_template_decl;
20019 /* Now, do the (partial) substitution to figure out the
20020 appropriate function type. */
20021 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20022 --processing_template_decl;
20023
20024 /* Substitute into the template parameters to obtain the real
20025 innermost set of parameters. This step is important if the
20026 innermost set of template parameters contains value
20027 parameters whose types depend on outer template parameters. */
20028 TREE_VEC_LENGTH (partial_args)--;
20029 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20030
20031 pop_access_scope (decl);
20032 }
20033
20034 return fn_type;
20035 }
20036
20037 /* Return truthvalue if we're processing a template different from
20038 the last one involved in diagnostics. */
20039 int
20040 problematic_instantiation_changed (void)
20041 {
20042 return current_tinst_level != last_error_tinst_level;
20043 }
20044
20045 /* Remember current template involved in diagnostics. */
20046 void
20047 record_last_problematic_instantiation (void)
20048 {
20049 last_error_tinst_level = current_tinst_level;
20050 }
20051
20052 struct tinst_level *
20053 current_instantiation (void)
20054 {
20055 return current_tinst_level;
20056 }
20057
20058 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20059 type. Return zero for ok, nonzero for disallowed. Issue error and
20060 warning messages under control of COMPLAIN. */
20061
20062 static int
20063 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20064 {
20065 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20066 return 0;
20067 else if (POINTER_TYPE_P (type))
20068 return 0;
20069 else if (TYPE_PTRMEM_P (type))
20070 return 0;
20071 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20072 return 0;
20073 else if (TREE_CODE (type) == TYPENAME_TYPE)
20074 return 0;
20075 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20076 return 0;
20077 else if (TREE_CODE (type) == NULLPTR_TYPE)
20078 return 0;
20079
20080 if (complain & tf_error)
20081 {
20082 if (type == error_mark_node)
20083 inform (input_location, "invalid template non-type parameter");
20084 else
20085 error ("%q#T is not a valid type for a template non-type parameter",
20086 type);
20087 }
20088 return 1;
20089 }
20090
20091 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20092 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20093
20094 static bool
20095 dependent_type_p_r (tree type)
20096 {
20097 tree scope;
20098
20099 /* [temp.dep.type]
20100
20101 A type is dependent if it is:
20102
20103 -- a template parameter. Template template parameters are types
20104 for us (since TYPE_P holds true for them) so we handle
20105 them here. */
20106 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20107 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20108 return true;
20109 /* -- a qualified-id with a nested-name-specifier which contains a
20110 class-name that names a dependent type or whose unqualified-id
20111 names a dependent type. */
20112 if (TREE_CODE (type) == TYPENAME_TYPE)
20113 return true;
20114 /* -- a cv-qualified type where the cv-unqualified type is
20115 dependent. */
20116 type = TYPE_MAIN_VARIANT (type);
20117 /* -- a compound type constructed from any dependent type. */
20118 if (TYPE_PTRMEM_P (type))
20119 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20120 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20121 (type)));
20122 else if (TYPE_PTR_P (type)
20123 || TREE_CODE (type) == REFERENCE_TYPE)
20124 return dependent_type_p (TREE_TYPE (type));
20125 else if (TREE_CODE (type) == FUNCTION_TYPE
20126 || TREE_CODE (type) == METHOD_TYPE)
20127 {
20128 tree arg_type;
20129
20130 if (dependent_type_p (TREE_TYPE (type)))
20131 return true;
20132 for (arg_type = TYPE_ARG_TYPES (type);
20133 arg_type;
20134 arg_type = TREE_CHAIN (arg_type))
20135 if (dependent_type_p (TREE_VALUE (arg_type)))
20136 return true;
20137 return false;
20138 }
20139 /* -- an array type constructed from any dependent type or whose
20140 size is specified by a constant expression that is
20141 value-dependent.
20142
20143 We checked for type- and value-dependence of the bounds in
20144 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20145 if (TREE_CODE (type) == ARRAY_TYPE)
20146 {
20147 if (TYPE_DOMAIN (type)
20148 && dependent_type_p (TYPE_DOMAIN (type)))
20149 return true;
20150 return dependent_type_p (TREE_TYPE (type));
20151 }
20152
20153 /* -- a template-id in which either the template name is a template
20154 parameter ... */
20155 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20156 return true;
20157 /* ... or any of the template arguments is a dependent type or
20158 an expression that is type-dependent or value-dependent. */
20159 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20160 && (any_dependent_template_arguments_p
20161 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20162 return true;
20163
20164 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20165 dependent; if the argument of the `typeof' expression is not
20166 type-dependent, then it should already been have resolved. */
20167 if (TREE_CODE (type) == TYPEOF_TYPE
20168 || TREE_CODE (type) == DECLTYPE_TYPE
20169 || TREE_CODE (type) == UNDERLYING_TYPE)
20170 return true;
20171
20172 /* A template argument pack is dependent if any of its packed
20173 arguments are. */
20174 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20175 {
20176 tree args = ARGUMENT_PACK_ARGS (type);
20177 int i, len = TREE_VEC_LENGTH (args);
20178 for (i = 0; i < len; ++i)
20179 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20180 return true;
20181 }
20182
20183 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20184 be template parameters. */
20185 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20186 return true;
20187
20188 /* The standard does not specifically mention types that are local
20189 to template functions or local classes, but they should be
20190 considered dependent too. For example:
20191
20192 template <int I> void f() {
20193 enum E { a = I };
20194 S<sizeof (E)> s;
20195 }
20196
20197 The size of `E' cannot be known until the value of `I' has been
20198 determined. Therefore, `E' must be considered dependent. */
20199 scope = TYPE_CONTEXT (type);
20200 if (scope && TYPE_P (scope))
20201 return dependent_type_p (scope);
20202 /* Don't use type_dependent_expression_p here, as it can lead
20203 to infinite recursion trying to determine whether a lambda
20204 nested in a lambda is dependent (c++/47687). */
20205 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20206 && DECL_LANG_SPECIFIC (scope)
20207 && DECL_TEMPLATE_INFO (scope)
20208 && (any_dependent_template_arguments_p
20209 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20210 return true;
20211
20212 /* Other types are non-dependent. */
20213 return false;
20214 }
20215
20216 /* Returns TRUE if TYPE is dependent, in the sense of
20217 [temp.dep.type]. Note that a NULL type is considered dependent. */
20218
20219 bool
20220 dependent_type_p (tree type)
20221 {
20222 /* If there are no template parameters in scope, then there can't be
20223 any dependent types. */
20224 if (!processing_template_decl)
20225 {
20226 /* If we are not processing a template, then nobody should be
20227 providing us with a dependent type. */
20228 gcc_assert (type);
20229 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20230 return false;
20231 }
20232
20233 /* If the type is NULL, we have not computed a type for the entity
20234 in question; in that case, the type is dependent. */
20235 if (!type)
20236 return true;
20237
20238 /* Erroneous types can be considered non-dependent. */
20239 if (type == error_mark_node)
20240 return false;
20241
20242 /* If we have not already computed the appropriate value for TYPE,
20243 do so now. */
20244 if (!TYPE_DEPENDENT_P_VALID (type))
20245 {
20246 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20247 TYPE_DEPENDENT_P_VALID (type) = 1;
20248 }
20249
20250 return TYPE_DEPENDENT_P (type);
20251 }
20252
20253 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20254 lookup. In other words, a dependent type that is not the current
20255 instantiation. */
20256
20257 bool
20258 dependent_scope_p (tree scope)
20259 {
20260 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20261 && !currently_open_class (scope));
20262 }
20263
20264 /* T is a SCOPE_REF; return whether we need to consider it
20265 instantiation-dependent so that we can check access at instantiation
20266 time even though we know which member it resolves to. */
20267
20268 static bool
20269 instantiation_dependent_scope_ref_p (tree t)
20270 {
20271 if (DECL_P (TREE_OPERAND (t, 1))
20272 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20273 && accessible_in_template_p (TREE_OPERAND (t, 0),
20274 TREE_OPERAND (t, 1)))
20275 return false;
20276 else
20277 return true;
20278 }
20279
20280 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20281 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20282 expression. */
20283
20284 /* Note that this predicate is not appropriate for general expressions;
20285 only constant expressions (that satisfy potential_constant_expression)
20286 can be tested for value dependence. */
20287
20288 bool
20289 value_dependent_expression_p (tree expression)
20290 {
20291 if (!processing_template_decl)
20292 return false;
20293
20294 /* A name declared with a dependent type. */
20295 if (DECL_P (expression) && type_dependent_expression_p (expression))
20296 return true;
20297
20298 switch (TREE_CODE (expression))
20299 {
20300 case IDENTIFIER_NODE:
20301 /* A name that has not been looked up -- must be dependent. */
20302 return true;
20303
20304 case TEMPLATE_PARM_INDEX:
20305 /* A non-type template parm. */
20306 return true;
20307
20308 case CONST_DECL:
20309 /* A non-type template parm. */
20310 if (DECL_TEMPLATE_PARM_P (expression))
20311 return true;
20312 return value_dependent_expression_p (DECL_INITIAL (expression));
20313
20314 case VAR_DECL:
20315 /* A constant with literal type and is initialized
20316 with an expression that is value-dependent.
20317
20318 Note that a non-dependent parenthesized initializer will have
20319 already been replaced with its constant value, so if we see
20320 a TREE_LIST it must be dependent. */
20321 if (DECL_INITIAL (expression)
20322 && decl_constant_var_p (expression)
20323 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20324 || value_dependent_expression_p (DECL_INITIAL (expression))))
20325 return true;
20326 return false;
20327
20328 case DYNAMIC_CAST_EXPR:
20329 case STATIC_CAST_EXPR:
20330 case CONST_CAST_EXPR:
20331 case REINTERPRET_CAST_EXPR:
20332 case CAST_EXPR:
20333 /* These expressions are value-dependent if the type to which
20334 the cast occurs is dependent or the expression being casted
20335 is value-dependent. */
20336 {
20337 tree type = TREE_TYPE (expression);
20338
20339 if (dependent_type_p (type))
20340 return true;
20341
20342 /* A functional cast has a list of operands. */
20343 expression = TREE_OPERAND (expression, 0);
20344 if (!expression)
20345 {
20346 /* If there are no operands, it must be an expression such
20347 as "int()". This should not happen for aggregate types
20348 because it would form non-constant expressions. */
20349 gcc_assert (cxx_dialect >= cxx11
20350 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20351
20352 return false;
20353 }
20354
20355 if (TREE_CODE (expression) == TREE_LIST)
20356 return any_value_dependent_elements_p (expression);
20357
20358 return value_dependent_expression_p (expression);
20359 }
20360
20361 case SIZEOF_EXPR:
20362 if (SIZEOF_EXPR_TYPE_P (expression))
20363 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20364 /* FALLTHRU */
20365 case ALIGNOF_EXPR:
20366 case TYPEID_EXPR:
20367 /* A `sizeof' expression is value-dependent if the operand is
20368 type-dependent or is a pack expansion. */
20369 expression = TREE_OPERAND (expression, 0);
20370 if (PACK_EXPANSION_P (expression))
20371 return true;
20372 else if (TYPE_P (expression))
20373 return dependent_type_p (expression);
20374 return instantiation_dependent_expression_p (expression);
20375
20376 case AT_ENCODE_EXPR:
20377 /* An 'encode' expression is value-dependent if the operand is
20378 type-dependent. */
20379 expression = TREE_OPERAND (expression, 0);
20380 return dependent_type_p (expression);
20381
20382 case NOEXCEPT_EXPR:
20383 expression = TREE_OPERAND (expression, 0);
20384 return instantiation_dependent_expression_p (expression);
20385
20386 case SCOPE_REF:
20387 /* All instantiation-dependent expressions should also be considered
20388 value-dependent. */
20389 return instantiation_dependent_scope_ref_p (expression);
20390
20391 case COMPONENT_REF:
20392 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20393 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20394
20395 case NONTYPE_ARGUMENT_PACK:
20396 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20397 is value-dependent. */
20398 {
20399 tree values = ARGUMENT_PACK_ARGS (expression);
20400 int i, len = TREE_VEC_LENGTH (values);
20401
20402 for (i = 0; i < len; ++i)
20403 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20404 return true;
20405
20406 return false;
20407 }
20408
20409 case TRAIT_EXPR:
20410 {
20411 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20412 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20413 || (type2 ? dependent_type_p (type2) : false));
20414 }
20415
20416 case MODOP_EXPR:
20417 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20418 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20419
20420 case ARRAY_REF:
20421 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20422 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20423
20424 case ADDR_EXPR:
20425 {
20426 tree op = TREE_OPERAND (expression, 0);
20427 return (value_dependent_expression_p (op)
20428 || has_value_dependent_address (op));
20429 }
20430
20431 case CALL_EXPR:
20432 {
20433 tree fn = get_callee_fndecl (expression);
20434 int i, nargs;
20435 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20436 return true;
20437 nargs = call_expr_nargs (expression);
20438 for (i = 0; i < nargs; ++i)
20439 {
20440 tree op = CALL_EXPR_ARG (expression, i);
20441 /* In a call to a constexpr member function, look through the
20442 implicit ADDR_EXPR on the object argument so that it doesn't
20443 cause the call to be considered value-dependent. We also
20444 look through it in potential_constant_expression. */
20445 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20446 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20447 && TREE_CODE (op) == ADDR_EXPR)
20448 op = TREE_OPERAND (op, 0);
20449 if (value_dependent_expression_p (op))
20450 return true;
20451 }
20452 return false;
20453 }
20454
20455 case TEMPLATE_ID_EXPR:
20456 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20457 type-dependent. */
20458 return type_dependent_expression_p (expression);
20459
20460 case CONSTRUCTOR:
20461 {
20462 unsigned ix;
20463 tree val;
20464 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20465 if (value_dependent_expression_p (val))
20466 return true;
20467 return false;
20468 }
20469
20470 case STMT_EXPR:
20471 /* Treat a GNU statement expression as dependent to avoid crashing
20472 under fold_non_dependent_expr; it can't be constant. */
20473 return true;
20474
20475 default:
20476 /* A constant expression is value-dependent if any subexpression is
20477 value-dependent. */
20478 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20479 {
20480 case tcc_reference:
20481 case tcc_unary:
20482 case tcc_comparison:
20483 case tcc_binary:
20484 case tcc_expression:
20485 case tcc_vl_exp:
20486 {
20487 int i, len = cp_tree_operand_length (expression);
20488
20489 for (i = 0; i < len; i++)
20490 {
20491 tree t = TREE_OPERAND (expression, i);
20492
20493 /* In some cases, some of the operands may be missing.l
20494 (For example, in the case of PREDECREMENT_EXPR, the
20495 amount to increment by may be missing.) That doesn't
20496 make the expression dependent. */
20497 if (t && value_dependent_expression_p (t))
20498 return true;
20499 }
20500 }
20501 break;
20502 default:
20503 break;
20504 }
20505 break;
20506 }
20507
20508 /* The expression is not value-dependent. */
20509 return false;
20510 }
20511
20512 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20513 [temp.dep.expr]. Note that an expression with no type is
20514 considered dependent. Other parts of the compiler arrange for an
20515 expression with type-dependent subexpressions to have no type, so
20516 this function doesn't have to be fully recursive. */
20517
20518 bool
20519 type_dependent_expression_p (tree expression)
20520 {
20521 if (!processing_template_decl)
20522 return false;
20523
20524 if (expression == NULL_TREE || expression == error_mark_node)
20525 return false;
20526
20527 /* An unresolved name is always dependent. */
20528 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20529 return true;
20530
20531 /* Some expression forms are never type-dependent. */
20532 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20533 || TREE_CODE (expression) == SIZEOF_EXPR
20534 || TREE_CODE (expression) == ALIGNOF_EXPR
20535 || TREE_CODE (expression) == AT_ENCODE_EXPR
20536 || TREE_CODE (expression) == NOEXCEPT_EXPR
20537 || TREE_CODE (expression) == TRAIT_EXPR
20538 || TREE_CODE (expression) == TYPEID_EXPR
20539 || TREE_CODE (expression) == DELETE_EXPR
20540 || TREE_CODE (expression) == VEC_DELETE_EXPR
20541 || TREE_CODE (expression) == THROW_EXPR)
20542 return false;
20543
20544 /* The types of these expressions depends only on the type to which
20545 the cast occurs. */
20546 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20547 || TREE_CODE (expression) == STATIC_CAST_EXPR
20548 || TREE_CODE (expression) == CONST_CAST_EXPR
20549 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20550 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20551 || TREE_CODE (expression) == CAST_EXPR)
20552 return dependent_type_p (TREE_TYPE (expression));
20553
20554 /* The types of these expressions depends only on the type created
20555 by the expression. */
20556 if (TREE_CODE (expression) == NEW_EXPR
20557 || TREE_CODE (expression) == VEC_NEW_EXPR)
20558 {
20559 /* For NEW_EXPR tree nodes created inside a template, either
20560 the object type itself or a TREE_LIST may appear as the
20561 operand 1. */
20562 tree type = TREE_OPERAND (expression, 1);
20563 if (TREE_CODE (type) == TREE_LIST)
20564 /* This is an array type. We need to check array dimensions
20565 as well. */
20566 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20567 || value_dependent_expression_p
20568 (TREE_OPERAND (TREE_VALUE (type), 1));
20569 else
20570 return dependent_type_p (type);
20571 }
20572
20573 if (TREE_CODE (expression) == SCOPE_REF)
20574 {
20575 tree scope = TREE_OPERAND (expression, 0);
20576 tree name = TREE_OPERAND (expression, 1);
20577
20578 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20579 contains an identifier associated by name lookup with one or more
20580 declarations declared with a dependent type, or...a
20581 nested-name-specifier or qualified-id that names a member of an
20582 unknown specialization. */
20583 return (type_dependent_expression_p (name)
20584 || dependent_scope_p (scope));
20585 }
20586
20587 if (TREE_CODE (expression) == FUNCTION_DECL
20588 && DECL_LANG_SPECIFIC (expression)
20589 && DECL_TEMPLATE_INFO (expression)
20590 && (any_dependent_template_arguments_p
20591 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20592 return true;
20593
20594 if (TREE_CODE (expression) == TEMPLATE_DECL
20595 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20596 return false;
20597
20598 if (TREE_CODE (expression) == STMT_EXPR)
20599 expression = stmt_expr_value_expr (expression);
20600
20601 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20602 {
20603 tree elt;
20604 unsigned i;
20605
20606 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20607 {
20608 if (type_dependent_expression_p (elt))
20609 return true;
20610 }
20611 return false;
20612 }
20613
20614 /* A static data member of the current instantiation with incomplete
20615 array type is type-dependent, as the definition and specializations
20616 can have different bounds. */
20617 if (VAR_P (expression)
20618 && DECL_CLASS_SCOPE_P (expression)
20619 && dependent_type_p (DECL_CONTEXT (expression))
20620 && VAR_HAD_UNKNOWN_BOUND (expression))
20621 return true;
20622
20623 /* An array of unknown bound depending on a variadic parameter, eg:
20624
20625 template<typename... Args>
20626 void foo (Args... args)
20627 {
20628 int arr[] = { args... };
20629 }
20630
20631 template<int... vals>
20632 void bar ()
20633 {
20634 int arr[] = { vals... };
20635 }
20636
20637 If the array has no length and has an initializer, it must be that
20638 we couldn't determine its length in cp_complete_array_type because
20639 it is dependent. */
20640 if (VAR_P (expression)
20641 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20642 && !TYPE_DOMAIN (TREE_TYPE (expression))
20643 && DECL_INITIAL (expression))
20644 return true;
20645
20646 if (TREE_TYPE (expression) == unknown_type_node)
20647 {
20648 if (TREE_CODE (expression) == ADDR_EXPR)
20649 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20650 if (TREE_CODE (expression) == COMPONENT_REF
20651 || TREE_CODE (expression) == OFFSET_REF)
20652 {
20653 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20654 return true;
20655 expression = TREE_OPERAND (expression, 1);
20656 if (identifier_p (expression))
20657 return false;
20658 }
20659 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20660 if (TREE_CODE (expression) == SCOPE_REF)
20661 return false;
20662
20663 /* Always dependent, on the number of arguments if nothing else. */
20664 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20665 return true;
20666
20667 if (BASELINK_P (expression))
20668 expression = BASELINK_FUNCTIONS (expression);
20669
20670 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20671 {
20672 if (any_dependent_template_arguments_p
20673 (TREE_OPERAND (expression, 1)))
20674 return true;
20675 expression = TREE_OPERAND (expression, 0);
20676 }
20677 gcc_assert (TREE_CODE (expression) == OVERLOAD
20678 || TREE_CODE (expression) == FUNCTION_DECL);
20679
20680 while (expression)
20681 {
20682 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20683 return true;
20684 expression = OVL_NEXT (expression);
20685 }
20686 return false;
20687 }
20688
20689 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20690
20691 return (dependent_type_p (TREE_TYPE (expression)));
20692 }
20693
20694 /* walk_tree callback function for instantiation_dependent_expression_p,
20695 below. Returns non-zero if a dependent subexpression is found. */
20696
20697 static tree
20698 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20699 void * /*data*/)
20700 {
20701 if (TYPE_P (*tp))
20702 {
20703 /* We don't have to worry about decltype currently because decltype
20704 of an instantiation-dependent expr is a dependent type. This
20705 might change depending on the resolution of DR 1172. */
20706 *walk_subtrees = false;
20707 return NULL_TREE;
20708 }
20709 enum tree_code code = TREE_CODE (*tp);
20710 switch (code)
20711 {
20712 /* Don't treat an argument list as dependent just because it has no
20713 TREE_TYPE. */
20714 case TREE_LIST:
20715 case TREE_VEC:
20716 return NULL_TREE;
20717
20718 case VAR_DECL:
20719 case CONST_DECL:
20720 /* A constant with a dependent initializer is dependent. */
20721 if (value_dependent_expression_p (*tp))
20722 return *tp;
20723 break;
20724
20725 case TEMPLATE_PARM_INDEX:
20726 return *tp;
20727
20728 /* Handle expressions with type operands. */
20729 case SIZEOF_EXPR:
20730 case ALIGNOF_EXPR:
20731 case TYPEID_EXPR:
20732 case AT_ENCODE_EXPR:
20733 {
20734 tree op = TREE_OPERAND (*tp, 0);
20735 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20736 op = TREE_TYPE (op);
20737 if (TYPE_P (op))
20738 {
20739 if (dependent_type_p (op))
20740 return *tp;
20741 else
20742 {
20743 *walk_subtrees = false;
20744 return NULL_TREE;
20745 }
20746 }
20747 break;
20748 }
20749
20750 case TRAIT_EXPR:
20751 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20752 || (TRAIT_EXPR_TYPE2 (*tp)
20753 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20754 return *tp;
20755 *walk_subtrees = false;
20756 return NULL_TREE;
20757
20758 case COMPONENT_REF:
20759 if (identifier_p (TREE_OPERAND (*tp, 1)))
20760 /* In a template, finish_class_member_access_expr creates a
20761 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20762 type-dependent, so that we can check access control at
20763 instantiation time (PR 42277). See also Core issue 1273. */
20764 return *tp;
20765 break;
20766
20767 case SCOPE_REF:
20768 if (instantiation_dependent_scope_ref_p (*tp))
20769 return *tp;
20770 else
20771 break;
20772
20773 /* Treat statement-expressions as dependent. */
20774 case BIND_EXPR:
20775 return *tp;
20776
20777 default:
20778 break;
20779 }
20780
20781 if (type_dependent_expression_p (*tp))
20782 return *tp;
20783 else
20784 return NULL_TREE;
20785 }
20786
20787 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20788 sense defined by the ABI:
20789
20790 "An expression is instantiation-dependent if it is type-dependent
20791 or value-dependent, or it has a subexpression that is type-dependent
20792 or value-dependent." */
20793
20794 bool
20795 instantiation_dependent_expression_p (tree expression)
20796 {
20797 tree result;
20798
20799 if (!processing_template_decl)
20800 return false;
20801
20802 if (expression == error_mark_node)
20803 return false;
20804
20805 result = cp_walk_tree_without_duplicates (&expression,
20806 instantiation_dependent_r, NULL);
20807 return result != NULL_TREE;
20808 }
20809
20810 /* Like type_dependent_expression_p, but it also works while not processing
20811 a template definition, i.e. during substitution or mangling. */
20812
20813 bool
20814 type_dependent_expression_p_push (tree expr)
20815 {
20816 bool b;
20817 ++processing_template_decl;
20818 b = type_dependent_expression_p (expr);
20819 --processing_template_decl;
20820 return b;
20821 }
20822
20823 /* Returns TRUE if ARGS contains a type-dependent expression. */
20824
20825 bool
20826 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20827 {
20828 unsigned int i;
20829 tree arg;
20830
20831 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20832 {
20833 if (type_dependent_expression_p (arg))
20834 return true;
20835 }
20836 return false;
20837 }
20838
20839 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20840 expressions) contains any type-dependent expressions. */
20841
20842 bool
20843 any_type_dependent_elements_p (const_tree list)
20844 {
20845 for (; list; list = TREE_CHAIN (list))
20846 if (type_dependent_expression_p (TREE_VALUE (list)))
20847 return true;
20848
20849 return false;
20850 }
20851
20852 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20853 expressions) contains any value-dependent expressions. */
20854
20855 bool
20856 any_value_dependent_elements_p (const_tree list)
20857 {
20858 for (; list; list = TREE_CHAIN (list))
20859 if (value_dependent_expression_p (TREE_VALUE (list)))
20860 return true;
20861
20862 return false;
20863 }
20864
20865 /* Returns TRUE if the ARG (a template argument) is dependent. */
20866
20867 bool
20868 dependent_template_arg_p (tree arg)
20869 {
20870 if (!processing_template_decl)
20871 return false;
20872
20873 /* Assume a template argument that was wrongly written by the user
20874 is dependent. This is consistent with what
20875 any_dependent_template_arguments_p [that calls this function]
20876 does. */
20877 if (!arg || arg == error_mark_node)
20878 return true;
20879
20880 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20881 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20882
20883 if (TREE_CODE (arg) == TEMPLATE_DECL
20884 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20885 return dependent_template_p (arg);
20886 else if (ARGUMENT_PACK_P (arg))
20887 {
20888 tree args = ARGUMENT_PACK_ARGS (arg);
20889 int i, len = TREE_VEC_LENGTH (args);
20890 for (i = 0; i < len; ++i)
20891 {
20892 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20893 return true;
20894 }
20895
20896 return false;
20897 }
20898 else if (TYPE_P (arg))
20899 return dependent_type_p (arg);
20900 else
20901 return (type_dependent_expression_p (arg)
20902 || value_dependent_expression_p (arg));
20903 }
20904
20905 /* Returns true if ARGS (a collection of template arguments) contains
20906 any types that require structural equality testing. */
20907
20908 bool
20909 any_template_arguments_need_structural_equality_p (tree args)
20910 {
20911 int i;
20912 int j;
20913
20914 if (!args)
20915 return false;
20916 if (args == error_mark_node)
20917 return true;
20918
20919 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20920 {
20921 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20922 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20923 {
20924 tree arg = TREE_VEC_ELT (level, j);
20925 tree packed_args = NULL_TREE;
20926 int k, len = 1;
20927
20928 if (ARGUMENT_PACK_P (arg))
20929 {
20930 /* Look inside the argument pack. */
20931 packed_args = ARGUMENT_PACK_ARGS (arg);
20932 len = TREE_VEC_LENGTH (packed_args);
20933 }
20934
20935 for (k = 0; k < len; ++k)
20936 {
20937 if (packed_args)
20938 arg = TREE_VEC_ELT (packed_args, k);
20939
20940 if (error_operand_p (arg))
20941 return true;
20942 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20943 continue;
20944 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20945 return true;
20946 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20947 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20948 return true;
20949 }
20950 }
20951 }
20952
20953 return false;
20954 }
20955
20956 /* Returns true if ARGS (a collection of template arguments) contains
20957 any dependent arguments. */
20958
20959 bool
20960 any_dependent_template_arguments_p (const_tree args)
20961 {
20962 int i;
20963 int j;
20964
20965 if (!args)
20966 return false;
20967 if (args == error_mark_node)
20968 return true;
20969
20970 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20971 {
20972 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20973 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20974 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20975 return true;
20976 }
20977
20978 return false;
20979 }
20980
20981 /* Returns TRUE if the template TMPL is dependent. */
20982
20983 bool
20984 dependent_template_p (tree tmpl)
20985 {
20986 if (TREE_CODE (tmpl) == OVERLOAD)
20987 {
20988 while (tmpl)
20989 {
20990 if (dependent_template_p (OVL_CURRENT (tmpl)))
20991 return true;
20992 tmpl = OVL_NEXT (tmpl);
20993 }
20994 return false;
20995 }
20996
20997 /* Template template parameters are dependent. */
20998 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20999 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21000 return true;
21001 /* So are names that have not been looked up. */
21002 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21003 return true;
21004 /* So are member templates of dependent classes. */
21005 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21006 return dependent_type_p (DECL_CONTEXT (tmpl));
21007 return false;
21008 }
21009
21010 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21011
21012 bool
21013 dependent_template_id_p (tree tmpl, tree args)
21014 {
21015 return (dependent_template_p (tmpl)
21016 || any_dependent_template_arguments_p (args));
21017 }
21018
21019 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21020 is dependent. */
21021
21022 bool
21023 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21024 {
21025 int i;
21026
21027 if (!processing_template_decl)
21028 return false;
21029
21030 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21031 {
21032 tree decl = TREE_VEC_ELT (declv, i);
21033 tree init = TREE_VEC_ELT (initv, i);
21034 tree cond = TREE_VEC_ELT (condv, i);
21035 tree incr = TREE_VEC_ELT (incrv, i);
21036
21037 if (type_dependent_expression_p (decl))
21038 return true;
21039
21040 if (init && type_dependent_expression_p (init))
21041 return true;
21042
21043 if (type_dependent_expression_p (cond))
21044 return true;
21045
21046 if (COMPARISON_CLASS_P (cond)
21047 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21048 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21049 return true;
21050
21051 if (TREE_CODE (incr) == MODOP_EXPR)
21052 {
21053 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21054 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21055 return true;
21056 }
21057 else if (type_dependent_expression_p (incr))
21058 return true;
21059 else if (TREE_CODE (incr) == MODIFY_EXPR)
21060 {
21061 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21062 return true;
21063 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21064 {
21065 tree t = TREE_OPERAND (incr, 1);
21066 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21067 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21068 return true;
21069 }
21070 }
21071 }
21072
21073 return false;
21074 }
21075
21076 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21077 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21078 no such TYPE can be found. Note that this function peers inside
21079 uninstantiated templates and therefore should be used only in
21080 extremely limited situations. ONLY_CURRENT_P restricts this
21081 peering to the currently open classes hierarchy (which is required
21082 when comparing types). */
21083
21084 tree
21085 resolve_typename_type (tree type, bool only_current_p)
21086 {
21087 tree scope;
21088 tree name;
21089 tree decl;
21090 int quals;
21091 tree pushed_scope;
21092 tree result;
21093
21094 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21095
21096 scope = TYPE_CONTEXT (type);
21097 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21098 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21099 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21100 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21101 identifier of the TYPENAME_TYPE anymore.
21102 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21103 TYPENAME_TYPE instead, we avoid messing up with a possible
21104 typedef variant case. */
21105 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21106
21107 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21108 it first before we can figure out what NAME refers to. */
21109 if (TREE_CODE (scope) == TYPENAME_TYPE)
21110 {
21111 if (TYPENAME_IS_RESOLVING_P (scope))
21112 /* Given a class template A with a dependent base with nested type C,
21113 typedef typename A::C::C C will land us here, as trying to resolve
21114 the initial A::C leads to the local C typedef, which leads back to
21115 A::C::C. So we break the recursion now. */
21116 return type;
21117 else
21118 scope = resolve_typename_type (scope, only_current_p);
21119 }
21120 /* If we don't know what SCOPE refers to, then we cannot resolve the
21121 TYPENAME_TYPE. */
21122 if (TREE_CODE (scope) == TYPENAME_TYPE)
21123 return type;
21124 /* If the SCOPE is a template type parameter, we have no way of
21125 resolving the name. */
21126 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21127 return type;
21128 /* If the SCOPE is not the current instantiation, there's no reason
21129 to look inside it. */
21130 if (only_current_p && !currently_open_class (scope))
21131 return type;
21132 /* If this is a typedef, we don't want to look inside (c++/11987). */
21133 if (typedef_variant_p (type))
21134 return type;
21135 /* If SCOPE isn't the template itself, it will not have a valid
21136 TYPE_FIELDS list. */
21137 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21138 /* scope is either the template itself or a compatible instantiation
21139 like X<T>, so look up the name in the original template. */
21140 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21141 else
21142 /* scope is a partial instantiation, so we can't do the lookup or we
21143 will lose the template arguments. */
21144 return type;
21145 /* Enter the SCOPE so that name lookup will be resolved as if we
21146 were in the class definition. In particular, SCOPE will no
21147 longer be considered a dependent type. */
21148 pushed_scope = push_scope (scope);
21149 /* Look up the declaration. */
21150 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21151 tf_warning_or_error);
21152
21153 result = NULL_TREE;
21154
21155 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21156 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21157 if (!decl)
21158 /*nop*/;
21159 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21160 && TREE_CODE (decl) == TYPE_DECL)
21161 {
21162 result = TREE_TYPE (decl);
21163 if (result == error_mark_node)
21164 result = NULL_TREE;
21165 }
21166 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21167 && DECL_CLASS_TEMPLATE_P (decl))
21168 {
21169 tree tmpl;
21170 tree args;
21171 /* Obtain the template and the arguments. */
21172 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21173 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21174 /* Instantiate the template. */
21175 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21176 /*entering_scope=*/0,
21177 tf_error | tf_user);
21178 if (result == error_mark_node)
21179 result = NULL_TREE;
21180 }
21181
21182 /* Leave the SCOPE. */
21183 if (pushed_scope)
21184 pop_scope (pushed_scope);
21185
21186 /* If we failed to resolve it, return the original typename. */
21187 if (!result)
21188 return type;
21189
21190 /* If lookup found a typename type, resolve that too. */
21191 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21192 {
21193 /* Ill-formed programs can cause infinite recursion here, so we
21194 must catch that. */
21195 TYPENAME_IS_RESOLVING_P (type) = 1;
21196 result = resolve_typename_type (result, only_current_p);
21197 TYPENAME_IS_RESOLVING_P (type) = 0;
21198 }
21199
21200 /* Qualify the resulting type. */
21201 quals = cp_type_quals (type);
21202 if (quals)
21203 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21204
21205 return result;
21206 }
21207
21208 /* EXPR is an expression which is not type-dependent. Return a proxy
21209 for EXPR that can be used to compute the types of larger
21210 expressions containing EXPR. */
21211
21212 tree
21213 build_non_dependent_expr (tree expr)
21214 {
21215 tree inner_expr;
21216
21217 #ifdef ENABLE_CHECKING
21218 /* Try to get a constant value for all non-dependent expressions in
21219 order to expose bugs in *_dependent_expression_p and constexpr. */
21220 if (cxx_dialect >= cxx11)
21221 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21222 #endif
21223
21224 /* Preserve OVERLOADs; the functions must be available to resolve
21225 types. */
21226 inner_expr = expr;
21227 if (TREE_CODE (inner_expr) == STMT_EXPR)
21228 inner_expr = stmt_expr_value_expr (inner_expr);
21229 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21230 inner_expr = TREE_OPERAND (inner_expr, 0);
21231 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21232 inner_expr = TREE_OPERAND (inner_expr, 1);
21233 if (is_overloaded_fn (inner_expr)
21234 || TREE_CODE (inner_expr) == OFFSET_REF)
21235 return expr;
21236 /* There is no need to return a proxy for a variable. */
21237 if (VAR_P (expr))
21238 return expr;
21239 /* Preserve string constants; conversions from string constants to
21240 "char *" are allowed, even though normally a "const char *"
21241 cannot be used to initialize a "char *". */
21242 if (TREE_CODE (expr) == STRING_CST)
21243 return expr;
21244 /* Preserve arithmetic constants, as an optimization -- there is no
21245 reason to create a new node. */
21246 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21247 return expr;
21248 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21249 There is at least one place where we want to know that a
21250 particular expression is a throw-expression: when checking a ?:
21251 expression, there are special rules if the second or third
21252 argument is a throw-expression. */
21253 if (TREE_CODE (expr) == THROW_EXPR)
21254 return expr;
21255
21256 /* Don't wrap an initializer list, we need to be able to look inside. */
21257 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21258 return expr;
21259
21260 /* Don't wrap a dummy object, we need to be able to test for it. */
21261 if (is_dummy_object (expr))
21262 return expr;
21263
21264 if (TREE_CODE (expr) == COND_EXPR)
21265 return build3 (COND_EXPR,
21266 TREE_TYPE (expr),
21267 TREE_OPERAND (expr, 0),
21268 (TREE_OPERAND (expr, 1)
21269 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21270 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21271 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21272 if (TREE_CODE (expr) == COMPOUND_EXPR
21273 && !COMPOUND_EXPR_OVERLOADED (expr))
21274 return build2 (COMPOUND_EXPR,
21275 TREE_TYPE (expr),
21276 TREE_OPERAND (expr, 0),
21277 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21278
21279 /* If the type is unknown, it can't really be non-dependent */
21280 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21281
21282 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21283 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21284 }
21285
21286 /* ARGS is a vector of expressions as arguments to a function call.
21287 Replace the arguments with equivalent non-dependent expressions.
21288 This modifies ARGS in place. */
21289
21290 void
21291 make_args_non_dependent (vec<tree, va_gc> *args)
21292 {
21293 unsigned int ix;
21294 tree arg;
21295
21296 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21297 {
21298 tree newarg = build_non_dependent_expr (arg);
21299 if (newarg != arg)
21300 (*args)[ix] = newarg;
21301 }
21302 }
21303
21304 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21305 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21306 parms. */
21307
21308 static tree
21309 make_auto_1 (tree name)
21310 {
21311 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21312 TYPE_NAME (au) = build_decl (input_location,
21313 TYPE_DECL, name, au);
21314 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21315 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21316 (0, processing_template_decl + 1, processing_template_decl + 1,
21317 TYPE_NAME (au), NULL_TREE);
21318 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21319 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21320 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21321
21322 return au;
21323 }
21324
21325 tree
21326 make_decltype_auto (void)
21327 {
21328 return make_auto_1 (get_identifier ("decltype(auto)"));
21329 }
21330
21331 tree
21332 make_auto (void)
21333 {
21334 return make_auto_1 (get_identifier ("auto"));
21335 }
21336
21337 /* Given type ARG, return std::initializer_list<ARG>. */
21338
21339 static tree
21340 listify (tree arg)
21341 {
21342 tree std_init_list = namespace_binding
21343 (get_identifier ("initializer_list"), std_node);
21344 tree argvec;
21345 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21346 {
21347 error ("deducing from brace-enclosed initializer list requires "
21348 "#include <initializer_list>");
21349 return error_mark_node;
21350 }
21351 argvec = make_tree_vec (1);
21352 TREE_VEC_ELT (argvec, 0) = arg;
21353 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21354 NULL_TREE, 0, tf_warning_or_error);
21355 }
21356
21357 /* Replace auto in TYPE with std::initializer_list<auto>. */
21358
21359 static tree
21360 listify_autos (tree type, tree auto_node)
21361 {
21362 tree init_auto = listify (auto_node);
21363 tree argvec = make_tree_vec (1);
21364 TREE_VEC_ELT (argvec, 0) = init_auto;
21365 if (processing_template_decl)
21366 argvec = add_to_template_args (current_template_args (), argvec);
21367 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21368 }
21369
21370 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21371 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21372
21373 tree
21374 do_auto_deduction (tree type, tree init, tree auto_node)
21375 {
21376 tree targs;
21377
21378 if (init == error_mark_node)
21379 return error_mark_node;
21380
21381 if (type_dependent_expression_p (init))
21382 /* Defining a subset of type-dependent expressions that we can deduce
21383 from ahead of time isn't worth the trouble. */
21384 return type;
21385
21386 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21387 with either a new invented type template parameter U or, if the
21388 initializer is a braced-init-list (8.5.4), with
21389 std::initializer_list<U>. */
21390 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21391 type = listify_autos (type, auto_node);
21392
21393 init = resolve_nondeduced_context (init);
21394
21395 targs = make_tree_vec (1);
21396 if (AUTO_IS_DECLTYPE (auto_node))
21397 {
21398 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21399 TREE_VEC_ELT (targs, 0)
21400 = finish_decltype_type (init, id, tf_warning_or_error);
21401 if (type != auto_node)
21402 {
21403 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21404 return error_mark_node;
21405 }
21406 }
21407 else
21408 {
21409 tree parms = build_tree_list (NULL_TREE, type);
21410 tree tparms = make_tree_vec (1);
21411 int val;
21412
21413 TREE_VEC_ELT (tparms, 0)
21414 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21415 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21416 DEDUCE_CALL, LOOKUP_NORMAL,
21417 NULL, /*explain_p=*/false);
21418 if (val > 0)
21419 {
21420 if (processing_template_decl)
21421 /* Try again at instantiation time. */
21422 return type;
21423 if (type && type != error_mark_node)
21424 /* If type is error_mark_node a diagnostic must have been
21425 emitted by now. Also, having a mention to '<type error>'
21426 in the diagnostic is not really useful to the user. */
21427 {
21428 if (cfun && auto_node == current_function_auto_return_pattern
21429 && LAMBDA_FUNCTION_P (current_function_decl))
21430 error ("unable to deduce lambda return type from %qE", init);
21431 else
21432 error ("unable to deduce %qT from %qE", type, init);
21433 }
21434 return error_mark_node;
21435 }
21436 }
21437
21438 /* If the list of declarators contains more than one declarator, the type
21439 of each declared variable is determined as described above. If the
21440 type deduced for the template parameter U is not the same in each
21441 deduction, the program is ill-formed. */
21442 if (TREE_TYPE (auto_node)
21443 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21444 {
21445 if (cfun && auto_node == current_function_auto_return_pattern
21446 && LAMBDA_FUNCTION_P (current_function_decl))
21447 error ("inconsistent types %qT and %qT deduced for "
21448 "lambda return type", TREE_TYPE (auto_node),
21449 TREE_VEC_ELT (targs, 0));
21450 else
21451 error ("inconsistent deduction for %qT: %qT and then %qT",
21452 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21453 return error_mark_node;
21454 }
21455 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21456
21457 if (processing_template_decl)
21458 targs = add_to_template_args (current_template_args (), targs);
21459 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21460 }
21461
21462 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21463 result. */
21464
21465 tree
21466 splice_late_return_type (tree type, tree late_return_type)
21467 {
21468 tree argvec;
21469
21470 if (late_return_type == NULL_TREE)
21471 return type;
21472 argvec = make_tree_vec (1);
21473 TREE_VEC_ELT (argvec, 0) = late_return_type;
21474 if (processing_template_parmlist)
21475 /* For a late-specified return type in a template type-parameter, we
21476 need to add a dummy argument level for its parmlist. */
21477 argvec = add_to_template_args
21478 (make_tree_vec (processing_template_parmlist), argvec);
21479 if (current_template_parms)
21480 argvec = add_to_template_args (current_template_args (), argvec);
21481 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21482 }
21483
21484 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21485 'decltype(auto)'. */
21486
21487 bool
21488 is_auto (const_tree type)
21489 {
21490 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21491 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21492 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21493 return true;
21494 else
21495 return false;
21496 }
21497
21498 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21499 a use of `auto'. Returns NULL_TREE otherwise. */
21500
21501 tree
21502 type_uses_auto (tree type)
21503 {
21504 return find_type_usage (type, is_auto);
21505 }
21506
21507 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21508 'decltype(auto)' or a concept. */
21509
21510 bool
21511 is_auto_or_concept (const_tree type)
21512 {
21513 return is_auto (type); // or concept
21514 }
21515
21516 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21517 a concept identifier) iff TYPE contains a use of a generic type. Returns
21518 NULL_TREE otherwise. */
21519
21520 tree
21521 type_uses_auto_or_concept (tree type)
21522 {
21523 return find_type_usage (type, is_auto_or_concept);
21524 }
21525
21526
21527 /* For a given template T, return the vector of typedefs referenced
21528 in T for which access check is needed at T instantiation time.
21529 T is either a FUNCTION_DECL or a RECORD_TYPE.
21530 Those typedefs were added to T by the function
21531 append_type_to_template_for_access_check. */
21532
21533 vec<qualified_typedef_usage_t, va_gc> *
21534 get_types_needing_access_check (tree t)
21535 {
21536 tree ti;
21537 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21538
21539 if (!t || t == error_mark_node)
21540 return NULL;
21541
21542 if (!(ti = get_template_info (t)))
21543 return NULL;
21544
21545 if (CLASS_TYPE_P (t)
21546 || TREE_CODE (t) == FUNCTION_DECL)
21547 {
21548 if (!TI_TEMPLATE (ti))
21549 return NULL;
21550
21551 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21552 }
21553
21554 return result;
21555 }
21556
21557 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21558 tied to T. That list of typedefs will be access checked at
21559 T instantiation time.
21560 T is either a FUNCTION_DECL or a RECORD_TYPE.
21561 TYPE_DECL is a TYPE_DECL node representing a typedef.
21562 SCOPE is the scope through which TYPE_DECL is accessed.
21563 LOCATION is the location of the usage point of TYPE_DECL.
21564
21565 This function is a subroutine of
21566 append_type_to_template_for_access_check. */
21567
21568 static void
21569 append_type_to_template_for_access_check_1 (tree t,
21570 tree type_decl,
21571 tree scope,
21572 location_t location)
21573 {
21574 qualified_typedef_usage_t typedef_usage;
21575 tree ti;
21576
21577 if (!t || t == error_mark_node)
21578 return;
21579
21580 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21581 || CLASS_TYPE_P (t))
21582 && type_decl
21583 && TREE_CODE (type_decl) == TYPE_DECL
21584 && scope);
21585
21586 if (!(ti = get_template_info (t)))
21587 return;
21588
21589 gcc_assert (TI_TEMPLATE (ti));
21590
21591 typedef_usage.typedef_decl = type_decl;
21592 typedef_usage.context = scope;
21593 typedef_usage.locus = location;
21594
21595 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21596 }
21597
21598 /* Append TYPE_DECL to the template TEMPL.
21599 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21600 At TEMPL instanciation time, TYPE_DECL will be checked to see
21601 if it can be accessed through SCOPE.
21602 LOCATION is the location of the usage point of TYPE_DECL.
21603
21604 e.g. consider the following code snippet:
21605
21606 class C
21607 {
21608 typedef int myint;
21609 };
21610
21611 template<class U> struct S
21612 {
21613 C::myint mi; // <-- usage point of the typedef C::myint
21614 };
21615
21616 S<char> s;
21617
21618 At S<char> instantiation time, we need to check the access of C::myint
21619 In other words, we need to check the access of the myint typedef through
21620 the C scope. For that purpose, this function will add the myint typedef
21621 and the scope C through which its being accessed to a list of typedefs
21622 tied to the template S. That list will be walked at template instantiation
21623 time and access check performed on each typedefs it contains.
21624 Note that this particular code snippet should yield an error because
21625 myint is private to C. */
21626
21627 void
21628 append_type_to_template_for_access_check (tree templ,
21629 tree type_decl,
21630 tree scope,
21631 location_t location)
21632 {
21633 qualified_typedef_usage_t *iter;
21634 unsigned i;
21635
21636 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21637
21638 /* Make sure we don't append the type to the template twice. */
21639 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21640 if (iter->typedef_decl == type_decl && scope == iter->context)
21641 return;
21642
21643 append_type_to_template_for_access_check_1 (templ, type_decl,
21644 scope, location);
21645 }
21646
21647 /* Convert the generic type parameters in PARM that match the types given in the
21648 range [START_IDX, END_IDX) from the current_template_parms into generic type
21649 packs. */
21650
21651 tree
21652 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21653 {
21654 tree current = current_template_parms;
21655 int depth = TMPL_PARMS_DEPTH (current);
21656 current = INNERMOST_TEMPLATE_PARMS (current);
21657 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21658
21659 for (int i = 0; i < start_idx; ++i)
21660 TREE_VEC_ELT (replacement, i)
21661 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21662
21663 for (int i = start_idx; i < end_idx; ++i)
21664 {
21665 /* Create a distinct parameter pack type from the current parm and add it
21666 to the replacement args to tsubst below into the generic function
21667 parameter. */
21668
21669 tree o = TREE_TYPE (TREE_VALUE
21670 (TREE_VEC_ELT (current, i)));
21671 tree t = copy_type (o);
21672 TEMPLATE_TYPE_PARM_INDEX (t)
21673 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21674 o, 0, 0, tf_none);
21675 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21676 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21677 TYPE_MAIN_VARIANT (t) = t;
21678 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21679 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21680 TREE_VEC_ELT (replacement, i) = t;
21681 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21682 }
21683
21684 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21685 TREE_VEC_ELT (replacement, i)
21686 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21687
21688 /* If there are more levels then build up the replacement with the outer
21689 template parms. */
21690 if (depth > 1)
21691 replacement = add_to_template_args (template_parms_to_args
21692 (TREE_CHAIN (current_template_parms)),
21693 replacement);
21694
21695 return tsubst (parm, replacement, tf_none, NULL_TREE);
21696 }
21697
21698
21699 /* Set up the hash tables for template instantiations. */
21700
21701 void
21702 init_template_processing (void)
21703 {
21704 decl_specializations = htab_create_ggc (37,
21705 hash_specialization,
21706 eq_specializations,
21707 ggc_free);
21708 type_specializations = htab_create_ggc (37,
21709 hash_specialization,
21710 eq_specializations,
21711 ggc_free);
21712 }
21713
21714 /* Print stats about the template hash tables for -fstats. */
21715
21716 void
21717 print_template_statistics (void)
21718 {
21719 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21720 "%f collisions\n", (long) htab_size (decl_specializations),
21721 (long) htab_elements (decl_specializations),
21722 htab_collisions (decl_specializations));
21723 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21724 "%f collisions\n", (long) htab_size (type_specializations),
21725 (long) htab_elements (type_specializations),
21726 htab_collisions (type_specializations));
21727 }
21728
21729 #include "gt-cp-pt.h"