]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
96343edecdac94d81fc48b304125338b0b7286d8
[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_LOC (arg, input_location),
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_LOC (expr, input_location),
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_at (input_location);
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_LOC (t, input_location),
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 location_t loc = EXPR_LOC_OR_LOC (t,
14519 input_location);
14520 inform (loc,
14521 "declarations in dependent base %qT are "
14522 "not found by unqualified lookup",
14523 DECL_CLASS_CONTEXT (fn));
14524 if (current_class_ptr)
14525 inform (loc,
14526 "use %<this->%D%> instead", function);
14527 else
14528 inform (loc,
14529 "use %<%T::%D%> instead",
14530 current_class_name, function);
14531 }
14532 else
14533 inform (0, "%q+D declared here, later in the "
14534 "translation unit", fn);
14535 }
14536 function = unq;
14537 }
14538 }
14539 if (identifier_p (function))
14540 {
14541 if (complain & tf_error)
14542 unqualified_name_lookup_error (function);
14543 release_tree_vector (call_args);
14544 RETURN (error_mark_node);
14545 }
14546 }
14547
14548 /* Remember that there was a reference to this entity. */
14549 if (DECL_P (function))
14550 mark_used (function);
14551
14552 /* Put back tf_decltype for the actual call. */
14553 complain |= decltype_flag;
14554
14555 if (TREE_CODE (function) == OFFSET_REF)
14556 ret = build_offset_ref_call_from_tree (function, &call_args,
14557 complain);
14558 else if (TREE_CODE (function) == COMPONENT_REF)
14559 {
14560 tree instance = TREE_OPERAND (function, 0);
14561 tree fn = TREE_OPERAND (function, 1);
14562
14563 if (processing_template_decl
14564 && (type_dependent_expression_p (instance)
14565 || (!BASELINK_P (fn)
14566 && TREE_CODE (fn) != FIELD_DECL)
14567 || type_dependent_expression_p (fn)
14568 || any_type_dependent_arguments_p (call_args)))
14569 ret = build_nt_call_vec (function, call_args);
14570 else if (!BASELINK_P (fn))
14571 ret = finish_call_expr (function, &call_args,
14572 /*disallow_virtual=*/false,
14573 /*koenig_p=*/false,
14574 complain);
14575 else
14576 ret = (build_new_method_call
14577 (instance, fn,
14578 &call_args, NULL_TREE,
14579 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14580 /*fn_p=*/NULL,
14581 complain));
14582 }
14583 else
14584 ret = finish_call_expr (function, &call_args,
14585 /*disallow_virtual=*/qualified_p,
14586 koenig_p,
14587 complain);
14588
14589 release_tree_vector (call_args);
14590
14591 RETURN (ret);
14592 }
14593
14594 case COND_EXPR:
14595 {
14596 tree cond = RECUR (TREE_OPERAND (t, 0));
14597 tree exp1, exp2;
14598
14599 if (TREE_CODE (cond) == INTEGER_CST)
14600 {
14601 if (integer_zerop (cond))
14602 {
14603 ++c_inhibit_evaluation_warnings;
14604 exp1 = RECUR (TREE_OPERAND (t, 1));
14605 --c_inhibit_evaluation_warnings;
14606 exp2 = RECUR (TREE_OPERAND (t, 2));
14607 }
14608 else
14609 {
14610 exp1 = RECUR (TREE_OPERAND (t, 1));
14611 ++c_inhibit_evaluation_warnings;
14612 exp2 = RECUR (TREE_OPERAND (t, 2));
14613 --c_inhibit_evaluation_warnings;
14614 }
14615 }
14616 else
14617 {
14618 exp1 = RECUR (TREE_OPERAND (t, 1));
14619 exp2 = RECUR (TREE_OPERAND (t, 2));
14620 }
14621
14622 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14623 cond, exp1, exp2, complain));
14624 }
14625
14626 case PSEUDO_DTOR_EXPR:
14627 RETURN (finish_pseudo_destructor_expr
14628 (RECUR (TREE_OPERAND (t, 0)),
14629 RECUR (TREE_OPERAND (t, 1)),
14630 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14631 input_location));
14632
14633 case TREE_LIST:
14634 {
14635 tree purpose, value, chain;
14636
14637 if (t == void_list_node)
14638 RETURN (t);
14639
14640 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14641 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14642 {
14643 /* We have pack expansions, so expand those and
14644 create a new list out of it. */
14645 tree purposevec = NULL_TREE;
14646 tree valuevec = NULL_TREE;
14647 tree chain;
14648 int i, len = -1;
14649
14650 /* Expand the argument expressions. */
14651 if (TREE_PURPOSE (t))
14652 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14653 complain, in_decl);
14654 if (TREE_VALUE (t))
14655 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14656 complain, in_decl);
14657
14658 /* Build the rest of the list. */
14659 chain = TREE_CHAIN (t);
14660 if (chain && chain != void_type_node)
14661 chain = RECUR (chain);
14662
14663 /* Determine the number of arguments. */
14664 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14665 {
14666 len = TREE_VEC_LENGTH (purposevec);
14667 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14668 }
14669 else if (TREE_CODE (valuevec) == TREE_VEC)
14670 len = TREE_VEC_LENGTH (valuevec);
14671 else
14672 {
14673 /* Since we only performed a partial substitution into
14674 the argument pack, we only RETURN (a single list
14675 node. */
14676 if (purposevec == TREE_PURPOSE (t)
14677 && valuevec == TREE_VALUE (t)
14678 && chain == TREE_CHAIN (t))
14679 RETURN (t);
14680
14681 RETURN (tree_cons (purposevec, valuevec, chain));
14682 }
14683
14684 /* Convert the argument vectors into a TREE_LIST */
14685 i = len;
14686 while (i > 0)
14687 {
14688 /* Grab the Ith values. */
14689 i--;
14690 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14691 : NULL_TREE;
14692 value
14693 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14694 : NULL_TREE;
14695
14696 /* Build the list (backwards). */
14697 chain = tree_cons (purpose, value, chain);
14698 }
14699
14700 RETURN (chain);
14701 }
14702
14703 purpose = TREE_PURPOSE (t);
14704 if (purpose)
14705 purpose = RECUR (purpose);
14706 value = TREE_VALUE (t);
14707 if (value)
14708 value = RECUR (value);
14709 chain = TREE_CHAIN (t);
14710 if (chain && chain != void_type_node)
14711 chain = RECUR (chain);
14712 if (purpose == TREE_PURPOSE (t)
14713 && value == TREE_VALUE (t)
14714 && chain == TREE_CHAIN (t))
14715 RETURN (t);
14716 RETURN (tree_cons (purpose, value, chain));
14717 }
14718
14719 case COMPONENT_REF:
14720 {
14721 tree object;
14722 tree object_type;
14723 tree member;
14724
14725 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14726 args, complain, in_decl);
14727 /* Remember that there was a reference to this entity. */
14728 if (DECL_P (object))
14729 mark_used (object);
14730 object_type = TREE_TYPE (object);
14731
14732 member = TREE_OPERAND (t, 1);
14733 if (BASELINK_P (member))
14734 member = tsubst_baselink (member,
14735 non_reference (TREE_TYPE (object)),
14736 args, complain, in_decl);
14737 else
14738 member = tsubst_copy (member, args, complain, in_decl);
14739 if (member == error_mark_node)
14740 RETURN (error_mark_node);
14741
14742 if (type_dependent_expression_p (object))
14743 /* We can't do much here. */;
14744 else if (!CLASS_TYPE_P (object_type))
14745 {
14746 if (scalarish_type_p (object_type))
14747 {
14748 tree s = NULL_TREE;
14749 tree dtor = member;
14750
14751 if (TREE_CODE (dtor) == SCOPE_REF)
14752 {
14753 s = TREE_OPERAND (dtor, 0);
14754 dtor = TREE_OPERAND (dtor, 1);
14755 }
14756 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14757 {
14758 dtor = TREE_OPERAND (dtor, 0);
14759 if (TYPE_P (dtor))
14760 RETURN (finish_pseudo_destructor_expr
14761 (object, s, dtor, input_location));
14762 }
14763 }
14764 }
14765 else if (TREE_CODE (member) == SCOPE_REF
14766 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14767 {
14768 /* Lookup the template functions now that we know what the
14769 scope is. */
14770 tree scope = TREE_OPERAND (member, 0);
14771 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14772 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14773 member = lookup_qualified_name (scope, tmpl,
14774 /*is_type_p=*/false,
14775 /*complain=*/false);
14776 if (BASELINK_P (member))
14777 {
14778 BASELINK_FUNCTIONS (member)
14779 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14780 args);
14781 member = (adjust_result_of_qualified_name_lookup
14782 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14783 object_type));
14784 }
14785 else
14786 {
14787 qualified_name_lookup_error (scope, tmpl, member,
14788 input_location);
14789 RETURN (error_mark_node);
14790 }
14791 }
14792 else if (TREE_CODE (member) == SCOPE_REF
14793 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14794 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14795 {
14796 if (complain & tf_error)
14797 {
14798 if (TYPE_P (TREE_OPERAND (member, 0)))
14799 error ("%qT is not a class or namespace",
14800 TREE_OPERAND (member, 0));
14801 else
14802 error ("%qD is not a class or namespace",
14803 TREE_OPERAND (member, 0));
14804 }
14805 RETURN (error_mark_node);
14806 }
14807 else if (TREE_CODE (member) == FIELD_DECL)
14808 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14809
14810 RETURN (finish_class_member_access_expr (object, member,
14811 /*template_p=*/false,
14812 complain));
14813 }
14814
14815 case THROW_EXPR:
14816 RETURN (build_throw
14817 (RECUR (TREE_OPERAND (t, 0))));
14818
14819 case CONSTRUCTOR:
14820 {
14821 vec<constructor_elt, va_gc> *n;
14822 constructor_elt *ce;
14823 unsigned HOST_WIDE_INT idx;
14824 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14825 bool process_index_p;
14826 int newlen;
14827 bool need_copy_p = false;
14828 tree r;
14829
14830 if (type == error_mark_node)
14831 RETURN (error_mark_node);
14832
14833 /* digest_init will do the wrong thing if we let it. */
14834 if (type && TYPE_PTRMEMFUNC_P (type))
14835 RETURN (t);
14836
14837 /* We do not want to process the index of aggregate
14838 initializers as they are identifier nodes which will be
14839 looked up by digest_init. */
14840 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14841
14842 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14843 newlen = vec_safe_length (n);
14844 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14845 {
14846 if (ce->index && process_index_p
14847 /* An identifier index is looked up in the type
14848 being initialized, not the current scope. */
14849 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14850 ce->index = RECUR (ce->index);
14851
14852 if (PACK_EXPANSION_P (ce->value))
14853 {
14854 /* Substitute into the pack expansion. */
14855 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14856 in_decl);
14857
14858 if (ce->value == error_mark_node
14859 || PACK_EXPANSION_P (ce->value))
14860 ;
14861 else if (TREE_VEC_LENGTH (ce->value) == 1)
14862 /* Just move the argument into place. */
14863 ce->value = TREE_VEC_ELT (ce->value, 0);
14864 else
14865 {
14866 /* Update the length of the final CONSTRUCTOR
14867 arguments vector, and note that we will need to
14868 copy.*/
14869 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14870 need_copy_p = true;
14871 }
14872 }
14873 else
14874 ce->value = RECUR (ce->value);
14875 }
14876
14877 if (need_copy_p)
14878 {
14879 vec<constructor_elt, va_gc> *old_n = n;
14880
14881 vec_alloc (n, newlen);
14882 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14883 {
14884 if (TREE_CODE (ce->value) == TREE_VEC)
14885 {
14886 int i, len = TREE_VEC_LENGTH (ce->value);
14887 for (i = 0; i < len; ++i)
14888 CONSTRUCTOR_APPEND_ELT (n, 0,
14889 TREE_VEC_ELT (ce->value, i));
14890 }
14891 else
14892 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14893 }
14894 }
14895
14896 r = build_constructor (init_list_type_node, n);
14897 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14898
14899 if (TREE_HAS_CONSTRUCTOR (t))
14900 RETURN (finish_compound_literal (type, r, complain));
14901
14902 TREE_TYPE (r) = type;
14903 RETURN (r);
14904 }
14905
14906 case TYPEID_EXPR:
14907 {
14908 tree operand_0 = TREE_OPERAND (t, 0);
14909 if (TYPE_P (operand_0))
14910 {
14911 operand_0 = tsubst (operand_0, args, complain, in_decl);
14912 RETURN (get_typeid (operand_0, complain));
14913 }
14914 else
14915 {
14916 operand_0 = RECUR (operand_0);
14917 RETURN (build_typeid (operand_0, complain));
14918 }
14919 }
14920
14921 case VAR_DECL:
14922 if (!args)
14923 RETURN (t);
14924 else if (DECL_PACK_P (t))
14925 {
14926 /* We don't build decls for an instantiation of a
14927 variadic capture proxy, we instantiate the elements
14928 when needed. */
14929 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14930 return RECUR (DECL_VALUE_EXPR (t));
14931 }
14932 /* Fall through */
14933
14934 case PARM_DECL:
14935 {
14936 tree r = tsubst_copy (t, args, complain, in_decl);
14937
14938 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14939 /* If the original type was a reference, we'll be wrapped in
14940 the appropriate INDIRECT_REF. */
14941 r = convert_from_reference (r);
14942 RETURN (r);
14943 }
14944
14945 case VA_ARG_EXPR:
14946 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14947 RECUR (TREE_OPERAND (t, 0)),
14948 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14949
14950 case OFFSETOF_EXPR:
14951 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14952
14953 case TRAIT_EXPR:
14954 {
14955 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14956 complain, in_decl);
14957
14958 tree type2 = TRAIT_EXPR_TYPE2 (t);
14959 if (type2)
14960 type2 = tsubst_copy (type2, args, complain, in_decl);
14961
14962 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14963 }
14964
14965 case STMT_EXPR:
14966 {
14967 tree old_stmt_expr = cur_stmt_expr;
14968 tree stmt_expr = begin_stmt_expr ();
14969
14970 cur_stmt_expr = stmt_expr;
14971 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14972 integral_constant_expression_p);
14973 stmt_expr = finish_stmt_expr (stmt_expr, false);
14974 cur_stmt_expr = old_stmt_expr;
14975
14976 /* If the resulting list of expression statement is empty,
14977 fold it further into void_zero_node. */
14978 if (empty_expr_stmt_p (stmt_expr))
14979 stmt_expr = void_zero_node;
14980
14981 RETURN (stmt_expr);
14982 }
14983
14984 case LAMBDA_EXPR:
14985 {
14986 tree r = build_lambda_expr ();
14987
14988 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14989 LAMBDA_EXPR_CLOSURE (r) = type;
14990 CLASSTYPE_LAMBDA_EXPR (type) = r;
14991
14992 LAMBDA_EXPR_LOCATION (r)
14993 = LAMBDA_EXPR_LOCATION (t);
14994 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14995 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14996 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14997 LAMBDA_EXPR_DISCRIMINATOR (r)
14998 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14999 /* For a function scope, we want to use tsubst so that we don't
15000 complain about referring to an auto function before its return
15001 type has been deduced. Otherwise, we want to use tsubst_copy so
15002 that we look up the existing field/parameter/variable rather
15003 than build a new one. */
15004 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15005 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15006 scope = tsubst (scope, args, complain, in_decl);
15007 else if (scope && TREE_CODE (scope) == PARM_DECL)
15008 {
15009 /* Look up the parameter we want directly, as tsubst_copy
15010 doesn't do what we need. */
15011 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15012 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15013 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15014 parm = DECL_CHAIN (parm);
15015 scope = parm;
15016 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15017 if (DECL_CONTEXT (scope) == NULL_TREE)
15018 DECL_CONTEXT (scope) = fn;
15019 }
15020 else
15021 scope = RECUR (scope);
15022 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15023 LAMBDA_EXPR_RETURN_TYPE (r)
15024 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15025
15026 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15027 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15028
15029 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15030 determine_visibility (TYPE_NAME (type));
15031 /* Now that we know visibility, instantiate the type so we have a
15032 declaration of the op() for later calls to lambda_function. */
15033 complete_type (type);
15034
15035 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15036
15037 RETURN (build_lambda_object (r));
15038 }
15039
15040 case TARGET_EXPR:
15041 /* We can get here for a constant initializer of non-dependent type.
15042 FIXME stop folding in cp_parser_initializer_clause. */
15043 {
15044 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15045 complain);
15046 RETURN (r);
15047 }
15048
15049 case TRANSACTION_EXPR:
15050 RETURN (tsubst_expr(t, args, complain, in_decl,
15051 integral_constant_expression_p));
15052
15053 case PAREN_EXPR:
15054 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15055
15056 case VEC_PERM_EXPR:
15057 RETURN (build_x_vec_perm_expr (input_location,
15058 RECUR (TREE_OPERAND (t, 0)),
15059 RECUR (TREE_OPERAND (t, 1)),
15060 RECUR (TREE_OPERAND (t, 2)),
15061 complain));
15062
15063 default:
15064 /* Handle Objective-C++ constructs, if appropriate. */
15065 {
15066 tree subst
15067 = objcp_tsubst_copy_and_build (t, args, complain,
15068 in_decl, /*function_p=*/false);
15069 if (subst)
15070 RETURN (subst);
15071 }
15072 RETURN (tsubst_copy (t, args, complain, in_decl));
15073 }
15074
15075 #undef RECUR
15076 #undef RETURN
15077 out:
15078 input_location = loc;
15079 return retval;
15080 }
15081
15082 /* Verify that the instantiated ARGS are valid. For type arguments,
15083 make sure that the type's linkage is ok. For non-type arguments,
15084 make sure they are constants if they are integral or enumerations.
15085 Emit an error under control of COMPLAIN, and return TRUE on error. */
15086
15087 static bool
15088 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15089 {
15090 if (dependent_template_arg_p (t))
15091 return false;
15092 if (ARGUMENT_PACK_P (t))
15093 {
15094 tree vec = ARGUMENT_PACK_ARGS (t);
15095 int len = TREE_VEC_LENGTH (vec);
15096 bool result = false;
15097 int i;
15098
15099 for (i = 0; i < len; ++i)
15100 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15101 result = true;
15102 return result;
15103 }
15104 else if (TYPE_P (t))
15105 {
15106 /* [basic.link]: A name with no linkage (notably, the name
15107 of a class or enumeration declared in a local scope)
15108 shall not be used to declare an entity with linkage.
15109 This implies that names with no linkage cannot be used as
15110 template arguments
15111
15112 DR 757 relaxes this restriction for C++0x. */
15113 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15114 : no_linkage_check (t, /*relaxed_p=*/false));
15115
15116 if (nt)
15117 {
15118 /* DR 488 makes use of a type with no linkage cause
15119 type deduction to fail. */
15120 if (complain & tf_error)
15121 {
15122 if (TYPE_ANONYMOUS_P (nt))
15123 error ("%qT is/uses anonymous type", t);
15124 else
15125 error ("template argument for %qD uses local type %qT",
15126 tmpl, t);
15127 }
15128 return true;
15129 }
15130 /* In order to avoid all sorts of complications, we do not
15131 allow variably-modified types as template arguments. */
15132 else if (variably_modified_type_p (t, NULL_TREE))
15133 {
15134 if (complain & tf_error)
15135 error ("%qT is a variably modified type", t);
15136 return true;
15137 }
15138 }
15139 /* Class template and alias template arguments should be OK. */
15140 else if (DECL_TYPE_TEMPLATE_P (t))
15141 ;
15142 /* A non-type argument of integral or enumerated type must be a
15143 constant. */
15144 else if (TREE_TYPE (t)
15145 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15146 && !TREE_CONSTANT (t))
15147 {
15148 if (complain & tf_error)
15149 error ("integral expression %qE is not constant", t);
15150 return true;
15151 }
15152 return false;
15153 }
15154
15155 static bool
15156 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15157 {
15158 int ix, len = DECL_NTPARMS (tmpl);
15159 bool result = false;
15160
15161 for (ix = 0; ix != len; ix++)
15162 {
15163 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15164 result = true;
15165 }
15166 if (result && (complain & tf_error))
15167 error (" trying to instantiate %qD", tmpl);
15168 return result;
15169 }
15170
15171 /* We're out of SFINAE context now, so generate diagnostics for the access
15172 errors we saw earlier when instantiating D from TMPL and ARGS. */
15173
15174 static void
15175 recheck_decl_substitution (tree d, tree tmpl, tree args)
15176 {
15177 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15178 tree type = TREE_TYPE (pattern);
15179 location_t loc = input_location;
15180
15181 push_access_scope (d);
15182 push_deferring_access_checks (dk_no_deferred);
15183 input_location = DECL_SOURCE_LOCATION (pattern);
15184 tsubst (type, args, tf_warning_or_error, d);
15185 input_location = loc;
15186 pop_deferring_access_checks ();
15187 pop_access_scope (d);
15188 }
15189
15190 /* Instantiate the indicated variable, function, or alias template TMPL with
15191 the template arguments in TARG_PTR. */
15192
15193 static tree
15194 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15195 {
15196 tree targ_ptr = orig_args;
15197 tree fndecl;
15198 tree gen_tmpl;
15199 tree spec;
15200 bool access_ok = true;
15201
15202 if (tmpl == error_mark_node)
15203 return error_mark_node;
15204
15205 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15206
15207 /* If this function is a clone, handle it specially. */
15208 if (DECL_CLONED_FUNCTION_P (tmpl))
15209 {
15210 tree spec;
15211 tree clone;
15212
15213 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15214 DECL_CLONED_FUNCTION. */
15215 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15216 targ_ptr, complain);
15217 if (spec == error_mark_node)
15218 return error_mark_node;
15219
15220 /* Look for the clone. */
15221 FOR_EACH_CLONE (clone, spec)
15222 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15223 return clone;
15224 /* We should always have found the clone by now. */
15225 gcc_unreachable ();
15226 return NULL_TREE;
15227 }
15228
15229 /* Check to see if we already have this specialization. */
15230 gen_tmpl = most_general_template (tmpl);
15231 if (tmpl != gen_tmpl)
15232 /* The TMPL is a partial instantiation. To get a full set of
15233 arguments we must add the arguments used to perform the
15234 partial instantiation. */
15235 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15236 targ_ptr);
15237
15238 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15239 but it doesn't seem to be on the hot path. */
15240 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15241
15242 gcc_assert (tmpl == gen_tmpl
15243 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15244 == spec)
15245 || fndecl == NULL_TREE);
15246
15247 if (spec != NULL_TREE)
15248 {
15249 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15250 {
15251 if (complain & tf_error)
15252 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15253 return error_mark_node;
15254 }
15255 return spec;
15256 }
15257
15258 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15259 complain))
15260 return error_mark_node;
15261
15262 /* We are building a FUNCTION_DECL, during which the access of its
15263 parameters and return types have to be checked. However this
15264 FUNCTION_DECL which is the desired context for access checking
15265 is not built yet. We solve this chicken-and-egg problem by
15266 deferring all checks until we have the FUNCTION_DECL. */
15267 push_deferring_access_checks (dk_deferred);
15268
15269 /* Instantiation of the function happens in the context of the function
15270 template, not the context of the overload resolution we're doing. */
15271 push_to_top_level ();
15272 /* If there are dependent arguments, e.g. because we're doing partial
15273 ordering, make sure processing_template_decl stays set. */
15274 if (uses_template_parms (targ_ptr))
15275 ++processing_template_decl;
15276 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15277 {
15278 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15279 complain, gen_tmpl);
15280 push_nested_class (ctx);
15281 }
15282 /* Substitute template parameters to obtain the specialization. */
15283 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15284 targ_ptr, complain, gen_tmpl);
15285 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15286 pop_nested_class ();
15287 pop_from_top_level ();
15288
15289 if (fndecl == error_mark_node)
15290 {
15291 pop_deferring_access_checks ();
15292 return error_mark_node;
15293 }
15294
15295 /* The DECL_TI_TEMPLATE should always be the immediate parent
15296 template, not the most general template. */
15297 DECL_TI_TEMPLATE (fndecl) = tmpl;
15298
15299 /* Now we know the specialization, compute access previously
15300 deferred. */
15301 push_access_scope (fndecl);
15302 if (!perform_deferred_access_checks (complain))
15303 access_ok = false;
15304 pop_access_scope (fndecl);
15305 pop_deferring_access_checks ();
15306
15307 /* If we've just instantiated the main entry point for a function,
15308 instantiate all the alternate entry points as well. We do this
15309 by cloning the instantiation of the main entry point, not by
15310 instantiating the template clones. */
15311 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15312 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15313
15314 if (!access_ok)
15315 {
15316 if (!(complain & tf_error))
15317 {
15318 /* Remember to reinstantiate when we're out of SFINAE so the user
15319 can see the errors. */
15320 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15321 }
15322 return error_mark_node;
15323 }
15324 return fndecl;
15325 }
15326
15327 /* Wrapper for instantiate_template_1. */
15328
15329 tree
15330 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15331 {
15332 tree ret;
15333 timevar_push (TV_TEMPLATE_INST);
15334 ret = instantiate_template_1 (tmpl, orig_args, complain);
15335 timevar_pop (TV_TEMPLATE_INST);
15336 return ret;
15337 }
15338
15339 /* Instantiate the alias template TMPL with ARGS. Also push a template
15340 instantiation level, which instantiate_template doesn't do because
15341 functions and variables have sufficient context established by the
15342 callers. */
15343
15344 static tree
15345 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15346 {
15347 struct pending_template *old_last_pend = last_pending_template;
15348 struct tinst_level *old_error_tinst = last_error_tinst_level;
15349 if (tmpl == error_mark_node || args == error_mark_node)
15350 return error_mark_node;
15351 tree tinst = build_tree_list (tmpl, args);
15352 if (!push_tinst_level (tinst))
15353 {
15354 ggc_free (tinst);
15355 return error_mark_node;
15356 }
15357
15358 args =
15359 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15360 args, tmpl, complain,
15361 /*require_all_args=*/true,
15362 /*use_default_args=*/true);
15363
15364 tree r = instantiate_template (tmpl, args, complain);
15365 pop_tinst_level ();
15366 /* We can't free this if a pending_template entry or last_error_tinst_level
15367 is pointing at it. */
15368 if (last_pending_template == old_last_pend
15369 && last_error_tinst_level == old_error_tinst)
15370 ggc_free (tinst);
15371
15372 return r;
15373 }
15374
15375 /* PARM is a template parameter pack for FN. Returns true iff
15376 PARM is used in a deducible way in the argument list of FN. */
15377
15378 static bool
15379 pack_deducible_p (tree parm, tree fn)
15380 {
15381 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15382 for (; t; t = TREE_CHAIN (t))
15383 {
15384 tree type = TREE_VALUE (t);
15385 tree packs;
15386 if (!PACK_EXPANSION_P (type))
15387 continue;
15388 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15389 packs; packs = TREE_CHAIN (packs))
15390 if (TREE_VALUE (packs) == parm)
15391 {
15392 /* The template parameter pack is used in a function parameter
15393 pack. If this is the end of the parameter list, the
15394 template parameter pack is deducible. */
15395 if (TREE_CHAIN (t) == void_list_node)
15396 return true;
15397 else
15398 /* Otherwise, not. Well, it could be deduced from
15399 a non-pack parameter, but doing so would end up with
15400 a deduction mismatch, so don't bother. */
15401 return false;
15402 }
15403 }
15404 /* The template parameter pack isn't used in any function parameter
15405 packs, but it might be used deeper, e.g. tuple<Args...>. */
15406 return true;
15407 }
15408
15409 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15410 NARGS elements of the arguments that are being used when calling
15411 it. TARGS is a vector into which the deduced template arguments
15412 are placed.
15413
15414 Return zero for success, 2 for an incomplete match that doesn't resolve
15415 all the types, and 1 for complete failure. An error message will be
15416 printed only for an incomplete match.
15417
15418 If FN is a conversion operator, or we are trying to produce a specific
15419 specialization, RETURN_TYPE is the return type desired.
15420
15421 The EXPLICIT_TARGS are explicit template arguments provided via a
15422 template-id.
15423
15424 The parameter STRICT is one of:
15425
15426 DEDUCE_CALL:
15427 We are deducing arguments for a function call, as in
15428 [temp.deduct.call].
15429
15430 DEDUCE_CONV:
15431 We are deducing arguments for a conversion function, as in
15432 [temp.deduct.conv].
15433
15434 DEDUCE_EXACT:
15435 We are deducing arguments when doing an explicit instantiation
15436 as in [temp.explicit], when determining an explicit specialization
15437 as in [temp.expl.spec], or when taking the address of a function
15438 template, as in [temp.deduct.funcaddr]. */
15439
15440 tree
15441 fn_type_unification (tree fn,
15442 tree explicit_targs,
15443 tree targs,
15444 const tree *args,
15445 unsigned int nargs,
15446 tree return_type,
15447 unification_kind_t strict,
15448 int flags,
15449 bool explain_p,
15450 bool decltype_p)
15451 {
15452 tree parms;
15453 tree fntype;
15454 tree decl = NULL_TREE;
15455 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15456 bool ok;
15457 static int deduction_depth;
15458 struct pending_template *old_last_pend = last_pending_template;
15459 struct tinst_level *old_error_tinst = last_error_tinst_level;
15460 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15461 tree tinst;
15462 tree r = error_mark_node;
15463
15464 if (decltype_p)
15465 complain |= tf_decltype;
15466
15467 /* In C++0x, it's possible to have a function template whose type depends
15468 on itself recursively. This is most obvious with decltype, but can also
15469 occur with enumeration scope (c++/48969). So we need to catch infinite
15470 recursion and reject the substitution at deduction time; this function
15471 will return error_mark_node for any repeated substitution.
15472
15473 This also catches excessive recursion such as when f<N> depends on
15474 f<N-1> across all integers, and returns error_mark_node for all the
15475 substitutions back up to the initial one.
15476
15477 This is, of course, not reentrant. */
15478 if (excessive_deduction_depth)
15479 return error_mark_node;
15480 tinst = build_tree_list (fn, NULL_TREE);
15481 ++deduction_depth;
15482
15483 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15484
15485 fntype = TREE_TYPE (fn);
15486 if (explicit_targs)
15487 {
15488 /* [temp.deduct]
15489
15490 The specified template arguments must match the template
15491 parameters in kind (i.e., type, nontype, template), and there
15492 must not be more arguments than there are parameters;
15493 otherwise type deduction fails.
15494
15495 Nontype arguments must match the types of the corresponding
15496 nontype template parameters, or must be convertible to the
15497 types of the corresponding nontype parameters as specified in
15498 _temp.arg.nontype_, otherwise type deduction fails.
15499
15500 All references in the function type of the function template
15501 to the corresponding template parameters are replaced by the
15502 specified template argument values. If a substitution in a
15503 template parameter or in the function type of the function
15504 template results in an invalid type, type deduction fails. */
15505 int i, len = TREE_VEC_LENGTH (tparms);
15506 location_t loc = input_location;
15507 bool incomplete = false;
15508
15509 /* Adjust any explicit template arguments before entering the
15510 substitution context. */
15511 explicit_targs
15512 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15513 complain,
15514 /*require_all_args=*/false,
15515 /*use_default_args=*/false));
15516 if (explicit_targs == error_mark_node)
15517 goto fail;
15518
15519 /* Substitute the explicit args into the function type. This is
15520 necessary so that, for instance, explicitly declared function
15521 arguments can match null pointed constants. If we were given
15522 an incomplete set of explicit args, we must not do semantic
15523 processing during substitution as we could create partial
15524 instantiations. */
15525 for (i = 0; i < len; i++)
15526 {
15527 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15528 bool parameter_pack = false;
15529 tree targ = TREE_VEC_ELT (explicit_targs, i);
15530
15531 /* Dig out the actual parm. */
15532 if (TREE_CODE (parm) == TYPE_DECL
15533 || TREE_CODE (parm) == TEMPLATE_DECL)
15534 {
15535 parm = TREE_TYPE (parm);
15536 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15537 }
15538 else if (TREE_CODE (parm) == PARM_DECL)
15539 {
15540 parm = DECL_INITIAL (parm);
15541 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15542 }
15543
15544 if (!parameter_pack && targ == NULL_TREE)
15545 /* No explicit argument for this template parameter. */
15546 incomplete = true;
15547
15548 if (parameter_pack && pack_deducible_p (parm, fn))
15549 {
15550 /* Mark the argument pack as "incomplete". We could
15551 still deduce more arguments during unification.
15552 We remove this mark in type_unification_real. */
15553 if (targ)
15554 {
15555 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15556 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15557 = ARGUMENT_PACK_ARGS (targ);
15558 }
15559
15560 /* We have some incomplete argument packs. */
15561 incomplete = true;
15562 }
15563 }
15564
15565 TREE_VALUE (tinst) = explicit_targs;
15566 if (!push_tinst_level (tinst))
15567 {
15568 excessive_deduction_depth = true;
15569 goto fail;
15570 }
15571 processing_template_decl += incomplete;
15572 input_location = DECL_SOURCE_LOCATION (fn);
15573 /* Ignore any access checks; we'll see them again in
15574 instantiate_template and they might have the wrong
15575 access path at this point. */
15576 push_deferring_access_checks (dk_deferred);
15577 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15578 complain | tf_partial, NULL_TREE);
15579 pop_deferring_access_checks ();
15580 input_location = loc;
15581 processing_template_decl -= incomplete;
15582 pop_tinst_level ();
15583
15584 if (fntype == error_mark_node)
15585 goto fail;
15586
15587 /* Place the explicitly specified arguments in TARGS. */
15588 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15589 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15590 }
15591
15592 /* Never do unification on the 'this' parameter. */
15593 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15594
15595 if (return_type)
15596 {
15597 tree *new_args;
15598
15599 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15600 new_args = XALLOCAVEC (tree, nargs + 1);
15601 new_args[0] = return_type;
15602 memcpy (new_args + 1, args, nargs * sizeof (tree));
15603 args = new_args;
15604 ++nargs;
15605 }
15606
15607 /* We allow incomplete unification without an error message here
15608 because the standard doesn't seem to explicitly prohibit it. Our
15609 callers must be ready to deal with unification failures in any
15610 event. */
15611
15612 TREE_VALUE (tinst) = targs;
15613 /* If we aren't explaining yet, push tinst context so we can see where
15614 any errors (e.g. from class instantiations triggered by instantiation
15615 of default template arguments) come from. If we are explaining, this
15616 context is redundant. */
15617 if (!explain_p && !push_tinst_level (tinst))
15618 {
15619 excessive_deduction_depth = true;
15620 goto fail;
15621 }
15622
15623 /* type_unification_real will pass back any access checks from default
15624 template argument substitution. */
15625 vec<deferred_access_check, va_gc> *checks;
15626 checks = NULL;
15627
15628 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15629 targs, parms, args, nargs, /*subr=*/0,
15630 strict, flags, &checks, explain_p);
15631 if (!explain_p)
15632 pop_tinst_level ();
15633 if (!ok)
15634 goto fail;
15635
15636 /* Now that we have bindings for all of the template arguments,
15637 ensure that the arguments deduced for the template template
15638 parameters have compatible template parameter lists. We cannot
15639 check this property before we have deduced all template
15640 arguments, because the template parameter types of a template
15641 template parameter might depend on prior template parameters
15642 deduced after the template template parameter. The following
15643 ill-formed example illustrates this issue:
15644
15645 template<typename T, template<T> class C> void f(C<5>, T);
15646
15647 template<int N> struct X {};
15648
15649 void g() {
15650 f(X<5>(), 5l); // error: template argument deduction fails
15651 }
15652
15653 The template parameter list of 'C' depends on the template type
15654 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15655 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15656 time that we deduce 'C'. */
15657 if (!template_template_parm_bindings_ok_p
15658 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15659 {
15660 unify_inconsistent_template_template_parameters (explain_p);
15661 goto fail;
15662 }
15663
15664 /* All is well so far. Now, check:
15665
15666 [temp.deduct]
15667
15668 When all template arguments have been deduced, all uses of
15669 template parameters in nondeduced contexts are replaced with
15670 the corresponding deduced argument values. If the
15671 substitution results in an invalid type, as described above,
15672 type deduction fails. */
15673 TREE_VALUE (tinst) = targs;
15674 if (!push_tinst_level (tinst))
15675 {
15676 excessive_deduction_depth = true;
15677 goto fail;
15678 }
15679
15680 /* Also collect access checks from the instantiation. */
15681 reopen_deferring_access_checks (checks);
15682
15683 decl = instantiate_template (fn, targs, complain);
15684
15685 checks = get_deferred_access_checks ();
15686 pop_deferring_access_checks ();
15687
15688 pop_tinst_level ();
15689
15690 if (decl == error_mark_node)
15691 goto fail;
15692
15693 /* Now perform any access checks encountered during substitution. */
15694 push_access_scope (decl);
15695 ok = perform_access_checks (checks, complain);
15696 pop_access_scope (decl);
15697 if (!ok)
15698 goto fail;
15699
15700 /* If we're looking for an exact match, check that what we got
15701 is indeed an exact match. It might not be if some template
15702 parameters are used in non-deduced contexts. */
15703 if (strict == DEDUCE_EXACT)
15704 {
15705 tree substed = TREE_TYPE (decl);
15706 unsigned int i;
15707
15708 tree sarg
15709 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15710 if (return_type)
15711 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15712 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15713 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15714 {
15715 unify_type_mismatch (explain_p, args[i],
15716 TREE_VALUE (sarg));
15717 goto fail;
15718 }
15719 }
15720
15721 r = decl;
15722
15723 fail:
15724 --deduction_depth;
15725 if (excessive_deduction_depth)
15726 {
15727 if (deduction_depth == 0)
15728 /* Reset once we're all the way out. */
15729 excessive_deduction_depth = false;
15730 }
15731
15732 /* We can't free this if a pending_template entry or last_error_tinst_level
15733 is pointing at it. */
15734 if (last_pending_template == old_last_pend
15735 && last_error_tinst_level == old_error_tinst)
15736 ggc_free (tinst);
15737
15738 return r;
15739 }
15740
15741 /* Adjust types before performing type deduction, as described in
15742 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15743 sections are symmetric. PARM is the type of a function parameter
15744 or the return type of the conversion function. ARG is the type of
15745 the argument passed to the call, or the type of the value
15746 initialized with the result of the conversion function.
15747 ARG_EXPR is the original argument expression, which may be null. */
15748
15749 static int
15750 maybe_adjust_types_for_deduction (unification_kind_t strict,
15751 tree* parm,
15752 tree* arg,
15753 tree arg_expr)
15754 {
15755 int result = 0;
15756
15757 switch (strict)
15758 {
15759 case DEDUCE_CALL:
15760 break;
15761
15762 case DEDUCE_CONV:
15763 {
15764 /* Swap PARM and ARG throughout the remainder of this
15765 function; the handling is precisely symmetric since PARM
15766 will initialize ARG rather than vice versa. */
15767 tree* temp = parm;
15768 parm = arg;
15769 arg = temp;
15770 break;
15771 }
15772
15773 case DEDUCE_EXACT:
15774 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15775 too, but here handle it by stripping the reference from PARM
15776 rather than by adding it to ARG. */
15777 if (TREE_CODE (*parm) == REFERENCE_TYPE
15778 && TYPE_REF_IS_RVALUE (*parm)
15779 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15780 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15781 && TREE_CODE (*arg) == REFERENCE_TYPE
15782 && !TYPE_REF_IS_RVALUE (*arg))
15783 *parm = TREE_TYPE (*parm);
15784 /* Nothing else to do in this case. */
15785 return 0;
15786
15787 default:
15788 gcc_unreachable ();
15789 }
15790
15791 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15792 {
15793 /* [temp.deduct.call]
15794
15795 If P is not a reference type:
15796
15797 --If A is an array type, the pointer type produced by the
15798 array-to-pointer standard conversion (_conv.array_) is
15799 used in place of A for type deduction; otherwise,
15800
15801 --If A is a function type, the pointer type produced by
15802 the function-to-pointer standard conversion
15803 (_conv.func_) is used in place of A for type deduction;
15804 otherwise,
15805
15806 --If A is a cv-qualified type, the top level
15807 cv-qualifiers of A's type are ignored for type
15808 deduction. */
15809 if (TREE_CODE (*arg) == ARRAY_TYPE)
15810 *arg = build_pointer_type (TREE_TYPE (*arg));
15811 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15812 *arg = build_pointer_type (*arg);
15813 else
15814 *arg = TYPE_MAIN_VARIANT (*arg);
15815 }
15816
15817 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15818 of the form T&&, where T is a template parameter, and the argument
15819 is an lvalue, T is deduced as A& */
15820 if (TREE_CODE (*parm) == REFERENCE_TYPE
15821 && TYPE_REF_IS_RVALUE (*parm)
15822 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15823 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15824 && (arg_expr ? real_lvalue_p (arg_expr)
15825 /* try_one_overload doesn't provide an arg_expr, but
15826 functions are always lvalues. */
15827 : TREE_CODE (*arg) == FUNCTION_TYPE))
15828 *arg = build_reference_type (*arg);
15829
15830 /* [temp.deduct.call]
15831
15832 If P is a cv-qualified type, the top level cv-qualifiers
15833 of P's type are ignored for type deduction. If P is a
15834 reference type, the type referred to by P is used for
15835 type deduction. */
15836 *parm = TYPE_MAIN_VARIANT (*parm);
15837 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15838 {
15839 *parm = TREE_TYPE (*parm);
15840 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15841 }
15842
15843 /* DR 322. For conversion deduction, remove a reference type on parm
15844 too (which has been swapped into ARG). */
15845 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15846 *arg = TREE_TYPE (*arg);
15847
15848 return result;
15849 }
15850
15851 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15852 template which does contain any deducible template parameters; check if
15853 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15854 unify_one_argument. */
15855
15856 static int
15857 check_non_deducible_conversion (tree parm, tree arg, int strict,
15858 int flags, bool explain_p)
15859 {
15860 tree type;
15861
15862 if (!TYPE_P (arg))
15863 type = TREE_TYPE (arg);
15864 else
15865 type = arg;
15866
15867 if (same_type_p (parm, type))
15868 return unify_success (explain_p);
15869
15870 if (strict == DEDUCE_CONV)
15871 {
15872 if (can_convert_arg (type, parm, NULL_TREE, flags,
15873 explain_p ? tf_warning_or_error : tf_none))
15874 return unify_success (explain_p);
15875 }
15876 else if (strict != DEDUCE_EXACT)
15877 {
15878 if (can_convert_arg (parm, type,
15879 TYPE_P (arg) ? NULL_TREE : arg,
15880 flags, explain_p ? tf_warning_or_error : tf_none))
15881 return unify_success (explain_p);
15882 }
15883
15884 if (strict == DEDUCE_EXACT)
15885 return unify_type_mismatch (explain_p, parm, arg);
15886 else
15887 return unify_arg_conversion (explain_p, parm, type, arg);
15888 }
15889
15890 static bool uses_deducible_template_parms (tree type);
15891
15892 /* Returns true iff the expression EXPR is one from which a template
15893 argument can be deduced. In other words, if it's an undecorated
15894 use of a template non-type parameter. */
15895
15896 static bool
15897 deducible_expression (tree expr)
15898 {
15899 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15900 }
15901
15902 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15903 deducible way; that is, if it has a max value of <PARM> - 1. */
15904
15905 static bool
15906 deducible_array_bound (tree domain)
15907 {
15908 if (domain == NULL_TREE)
15909 return false;
15910
15911 tree max = TYPE_MAX_VALUE (domain);
15912 if (TREE_CODE (max) != MINUS_EXPR)
15913 return false;
15914
15915 return deducible_expression (TREE_OPERAND (max, 0));
15916 }
15917
15918 /* Returns true iff the template arguments ARGS use a template parameter
15919 in a deducible way. */
15920
15921 static bool
15922 deducible_template_args (tree args)
15923 {
15924 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15925 {
15926 bool deducible;
15927 tree elt = TREE_VEC_ELT (args, i);
15928 if (ARGUMENT_PACK_P (elt))
15929 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15930 else
15931 {
15932 if (PACK_EXPANSION_P (elt))
15933 elt = PACK_EXPANSION_PATTERN (elt);
15934 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15935 deducible = true;
15936 else if (TYPE_P (elt))
15937 deducible = uses_deducible_template_parms (elt);
15938 else
15939 deducible = deducible_expression (elt);
15940 }
15941 if (deducible)
15942 return true;
15943 }
15944 return false;
15945 }
15946
15947 /* Returns true iff TYPE contains any deducible references to template
15948 parameters, as per 14.8.2.5. */
15949
15950 static bool
15951 uses_deducible_template_parms (tree type)
15952 {
15953 if (PACK_EXPANSION_P (type))
15954 type = PACK_EXPANSION_PATTERN (type);
15955
15956 /* T
15957 cv-list T
15958 TT<T>
15959 TT<i>
15960 TT<> */
15961 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15962 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15963 return true;
15964
15965 /* T*
15966 T&
15967 T&& */
15968 if (POINTER_TYPE_P (type))
15969 return uses_deducible_template_parms (TREE_TYPE (type));
15970
15971 /* T[integer-constant ]
15972 type [i] */
15973 if (TREE_CODE (type) == ARRAY_TYPE)
15974 return (uses_deducible_template_parms (TREE_TYPE (type))
15975 || deducible_array_bound (TYPE_DOMAIN (type)));
15976
15977 /* T type ::*
15978 type T::*
15979 T T::*
15980 T (type ::*)()
15981 type (T::*)()
15982 type (type ::*)(T)
15983 type (T::*)(T)
15984 T (type ::*)(T)
15985 T (T::*)()
15986 T (T::*)(T) */
15987 if (TYPE_PTRMEM_P (type))
15988 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15989 || (uses_deducible_template_parms
15990 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15991
15992 /* template-name <T> (where template-name refers to a class template)
15993 template-name <i> (where template-name refers to a class template) */
15994 if (CLASS_TYPE_P (type)
15995 && CLASSTYPE_TEMPLATE_INFO (type)
15996 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15997 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15998 (CLASSTYPE_TI_ARGS (type)));
15999
16000 /* type (T)
16001 T()
16002 T(T) */
16003 if (TREE_CODE (type) == FUNCTION_TYPE
16004 || TREE_CODE (type) == METHOD_TYPE)
16005 {
16006 if (uses_deducible_template_parms (TREE_TYPE (type)))
16007 return true;
16008 tree parm = TYPE_ARG_TYPES (type);
16009 if (TREE_CODE (type) == METHOD_TYPE)
16010 parm = TREE_CHAIN (parm);
16011 for (; parm; parm = TREE_CHAIN (parm))
16012 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16013 return true;
16014 }
16015
16016 return false;
16017 }
16018
16019 /* Subroutine of type_unification_real and unify_pack_expansion to
16020 handle unification of a single P/A pair. Parameters are as
16021 for those functions. */
16022
16023 static int
16024 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16025 int subr, unification_kind_t strict, int flags,
16026 bool explain_p)
16027 {
16028 tree arg_expr = NULL_TREE;
16029 int arg_strict;
16030
16031 if (arg == error_mark_node || parm == error_mark_node)
16032 return unify_invalid (explain_p);
16033 if (arg == unknown_type_node)
16034 /* We can't deduce anything from this, but we might get all the
16035 template args from other function args. */
16036 return unify_success (explain_p);
16037
16038 /* Implicit conversions (Clause 4) will be performed on a function
16039 argument to convert it to the type of the corresponding function
16040 parameter if the parameter type contains no template-parameters that
16041 participate in template argument deduction. */
16042 if (TYPE_P (parm) && !uses_template_parms (parm))
16043 /* For function parameters that contain no template-parameters at all,
16044 we have historically checked for convertibility in order to shortcut
16045 consideration of this candidate. */
16046 return check_non_deducible_conversion (parm, arg, strict, flags,
16047 explain_p);
16048 else if (strict == DEDUCE_CALL
16049 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16050 /* For function parameters with only non-deducible template parameters,
16051 just return. */
16052 return unify_success (explain_p);
16053
16054 switch (strict)
16055 {
16056 case DEDUCE_CALL:
16057 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16058 | UNIFY_ALLOW_MORE_CV_QUAL
16059 | UNIFY_ALLOW_DERIVED);
16060 break;
16061
16062 case DEDUCE_CONV:
16063 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16064 break;
16065
16066 case DEDUCE_EXACT:
16067 arg_strict = UNIFY_ALLOW_NONE;
16068 break;
16069
16070 default:
16071 gcc_unreachable ();
16072 }
16073
16074 /* We only do these transformations if this is the top-level
16075 parameter_type_list in a call or declaration matching; in other
16076 situations (nested function declarators, template argument lists) we
16077 won't be comparing a type to an expression, and we don't do any type
16078 adjustments. */
16079 if (!subr)
16080 {
16081 if (!TYPE_P (arg))
16082 {
16083 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16084 if (type_unknown_p (arg))
16085 {
16086 /* [temp.deduct.type] A template-argument can be
16087 deduced from a pointer to function or pointer
16088 to member function argument if the set of
16089 overloaded functions does not contain function
16090 templates and at most one of a set of
16091 overloaded functions provides a unique
16092 match. */
16093
16094 if (resolve_overloaded_unification
16095 (tparms, targs, parm, arg, strict,
16096 arg_strict, explain_p))
16097 return unify_success (explain_p);
16098 return unify_overload_resolution_failure (explain_p, arg);
16099 }
16100
16101 arg_expr = arg;
16102 arg = unlowered_expr_type (arg);
16103 if (arg == error_mark_node)
16104 return unify_invalid (explain_p);
16105 }
16106
16107 arg_strict |=
16108 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16109 }
16110 else
16111 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16112 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16113
16114 /* For deduction from an init-list we need the actual list. */
16115 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16116 arg = arg_expr;
16117 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16118 }
16119
16120 /* Most parms like fn_type_unification.
16121
16122 If SUBR is 1, we're being called recursively (to unify the
16123 arguments of a function or method parameter of a function
16124 template).
16125
16126 CHECKS is a pointer to a vector of access checks encountered while
16127 substituting default template arguments. */
16128
16129 static int
16130 type_unification_real (tree tparms,
16131 tree targs,
16132 tree xparms,
16133 const tree *xargs,
16134 unsigned int xnargs,
16135 int subr,
16136 unification_kind_t strict,
16137 int flags,
16138 vec<deferred_access_check, va_gc> **checks,
16139 bool explain_p)
16140 {
16141 tree parm, arg;
16142 int i;
16143 int ntparms = TREE_VEC_LENGTH (tparms);
16144 int saw_undeduced = 0;
16145 tree parms;
16146 const tree *args;
16147 unsigned int nargs;
16148 unsigned int ia;
16149
16150 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16151 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16152 gcc_assert (ntparms > 0);
16153
16154 /* Reset the number of non-defaulted template arguments contained
16155 in TARGS. */
16156 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16157
16158 again:
16159 parms = xparms;
16160 args = xargs;
16161 nargs = xnargs;
16162
16163 ia = 0;
16164 while (parms && parms != void_list_node
16165 && ia < nargs)
16166 {
16167 parm = TREE_VALUE (parms);
16168
16169 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16170 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16171 /* For a function parameter pack that occurs at the end of the
16172 parameter-declaration-list, the type A of each remaining
16173 argument of the call is compared with the type P of the
16174 declarator-id of the function parameter pack. */
16175 break;
16176
16177 parms = TREE_CHAIN (parms);
16178
16179 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16180 /* For a function parameter pack that does not occur at the
16181 end of the parameter-declaration-list, the type of the
16182 parameter pack is a non-deduced context. */
16183 continue;
16184
16185 arg = args[ia];
16186 ++ia;
16187
16188 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16189 flags, explain_p))
16190 return 1;
16191 }
16192
16193 if (parms
16194 && parms != void_list_node
16195 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16196 {
16197 /* Unify the remaining arguments with the pack expansion type. */
16198 tree argvec;
16199 tree parmvec = make_tree_vec (1);
16200
16201 /* Allocate a TREE_VEC and copy in all of the arguments */
16202 argvec = make_tree_vec (nargs - ia);
16203 for (i = 0; ia < nargs; ++ia, ++i)
16204 TREE_VEC_ELT (argvec, i) = args[ia];
16205
16206 /* Copy the parameter into parmvec. */
16207 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16208 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16209 /*subr=*/subr, explain_p))
16210 return 1;
16211
16212 /* Advance to the end of the list of parameters. */
16213 parms = TREE_CHAIN (parms);
16214 }
16215
16216 /* Fail if we've reached the end of the parm list, and more args
16217 are present, and the parm list isn't variadic. */
16218 if (ia < nargs && parms == void_list_node)
16219 return unify_too_many_arguments (explain_p, nargs, ia);
16220 /* Fail if parms are left and they don't have default values. */
16221 if (parms && parms != void_list_node
16222 && TREE_PURPOSE (parms) == NULL_TREE)
16223 {
16224 unsigned int count = nargs;
16225 tree p = parms;
16226 while (p && p != void_list_node)
16227 {
16228 count++;
16229 p = TREE_CHAIN (p);
16230 }
16231 return unify_too_few_arguments (explain_p, ia, count);
16232 }
16233
16234 if (!subr)
16235 {
16236 tsubst_flags_t complain = (explain_p
16237 ? tf_warning_or_error
16238 : tf_none);
16239
16240 for (i = 0; i < ntparms; i++)
16241 {
16242 tree targ = TREE_VEC_ELT (targs, i);
16243 tree tparm = TREE_VEC_ELT (tparms, i);
16244
16245 /* Clear the "incomplete" flags on all argument packs now so that
16246 substituting them into later default arguments works. */
16247 if (targ && ARGUMENT_PACK_P (targ))
16248 {
16249 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16250 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16251 }
16252
16253 if (targ || tparm == error_mark_node)
16254 continue;
16255 tparm = TREE_VALUE (tparm);
16256
16257 /* If this is an undeduced nontype parameter that depends on
16258 a type parameter, try another pass; its type may have been
16259 deduced from a later argument than the one from which
16260 this parameter can be deduced. */
16261 if (TREE_CODE (tparm) == PARM_DECL
16262 && uses_template_parms (TREE_TYPE (tparm))
16263 && !saw_undeduced++)
16264 goto again;
16265
16266 /* Core issue #226 (C++0x) [temp.deduct]:
16267
16268 If a template argument has not been deduced, its
16269 default template argument, if any, is used.
16270
16271 When we are in C++98 mode, TREE_PURPOSE will either
16272 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16273 to explicitly check cxx_dialect here. */
16274 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16275 {
16276 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16277 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16278 reopen_deferring_access_checks (*checks);
16279 location_t save_loc = input_location;
16280 if (DECL_P (parm))
16281 input_location = DECL_SOURCE_LOCATION (parm);
16282 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16283 arg = convert_template_argument (parm, arg, targs, complain,
16284 i, NULL_TREE);
16285 input_location = save_loc;
16286 *checks = get_deferred_access_checks ();
16287 pop_deferring_access_checks ();
16288 if (arg == error_mark_node)
16289 return 1;
16290 else
16291 {
16292 TREE_VEC_ELT (targs, i) = arg;
16293 /* The position of the first default template argument,
16294 is also the number of non-defaulted arguments in TARGS.
16295 Record that. */
16296 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16297 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16298 continue;
16299 }
16300 }
16301
16302 /* If the type parameter is a parameter pack, then it will
16303 be deduced to an empty parameter pack. */
16304 if (template_parameter_pack_p (tparm))
16305 {
16306 tree arg;
16307
16308 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16309 {
16310 arg = make_node (NONTYPE_ARGUMENT_PACK);
16311 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16312 TREE_CONSTANT (arg) = 1;
16313 }
16314 else
16315 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16316
16317 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16318
16319 TREE_VEC_ELT (targs, i) = arg;
16320 continue;
16321 }
16322
16323 return unify_parameter_deduction_failure (explain_p, tparm);
16324 }
16325 }
16326 #ifdef ENABLE_CHECKING
16327 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16329 #endif
16330
16331 return unify_success (explain_p);
16332 }
16333
16334 /* Subroutine of type_unification_real. Args are like the variables
16335 at the call site. ARG is an overloaded function (or template-id);
16336 we try deducing template args from each of the overloads, and if
16337 only one succeeds, we go with that. Modifies TARGS and returns
16338 true on success. */
16339
16340 static bool
16341 resolve_overloaded_unification (tree tparms,
16342 tree targs,
16343 tree parm,
16344 tree arg,
16345 unification_kind_t strict,
16346 int sub_strict,
16347 bool explain_p)
16348 {
16349 tree tempargs = copy_node (targs);
16350 int good = 0;
16351 tree goodfn = NULL_TREE;
16352 bool addr_p;
16353
16354 if (TREE_CODE (arg) == ADDR_EXPR)
16355 {
16356 arg = TREE_OPERAND (arg, 0);
16357 addr_p = true;
16358 }
16359 else
16360 addr_p = false;
16361
16362 if (TREE_CODE (arg) == COMPONENT_REF)
16363 /* Handle `&x' where `x' is some static or non-static member
16364 function name. */
16365 arg = TREE_OPERAND (arg, 1);
16366
16367 if (TREE_CODE (arg) == OFFSET_REF)
16368 arg = TREE_OPERAND (arg, 1);
16369
16370 /* Strip baselink information. */
16371 if (BASELINK_P (arg))
16372 arg = BASELINK_FUNCTIONS (arg);
16373
16374 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16375 {
16376 /* If we got some explicit template args, we need to plug them into
16377 the affected templates before we try to unify, in case the
16378 explicit args will completely resolve the templates in question. */
16379
16380 int ok = 0;
16381 tree expl_subargs = TREE_OPERAND (arg, 1);
16382 arg = TREE_OPERAND (arg, 0);
16383
16384 for (; arg; arg = OVL_NEXT (arg))
16385 {
16386 tree fn = OVL_CURRENT (arg);
16387 tree subargs, elem;
16388
16389 if (TREE_CODE (fn) != TEMPLATE_DECL)
16390 continue;
16391
16392 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16393 expl_subargs, NULL_TREE, tf_none,
16394 /*require_all_args=*/true,
16395 /*use_default_args=*/true);
16396 if (subargs != error_mark_node
16397 && !any_dependent_template_arguments_p (subargs))
16398 {
16399 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16400 if (try_one_overload (tparms, targs, tempargs, parm,
16401 elem, strict, sub_strict, addr_p, explain_p)
16402 && (!goodfn || !same_type_p (goodfn, elem)))
16403 {
16404 goodfn = elem;
16405 ++good;
16406 }
16407 }
16408 else if (subargs)
16409 ++ok;
16410 }
16411 /* If no templates (or more than one) are fully resolved by the
16412 explicit arguments, this template-id is a non-deduced context; it
16413 could still be OK if we deduce all template arguments for the
16414 enclosing call through other arguments. */
16415 if (good != 1)
16416 good = ok;
16417 }
16418 else if (TREE_CODE (arg) != OVERLOAD
16419 && TREE_CODE (arg) != FUNCTION_DECL)
16420 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16421 -- but the deduction does not succeed because the expression is
16422 not just the function on its own. */
16423 return false;
16424 else
16425 for (; arg; arg = OVL_NEXT (arg))
16426 if (try_one_overload (tparms, targs, tempargs, parm,
16427 TREE_TYPE (OVL_CURRENT (arg)),
16428 strict, sub_strict, addr_p, explain_p)
16429 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16430 {
16431 goodfn = OVL_CURRENT (arg);
16432 ++good;
16433 }
16434
16435 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16436 to function or pointer to member function argument if the set of
16437 overloaded functions does not contain function templates and at most
16438 one of a set of overloaded functions provides a unique match.
16439
16440 So if we found multiple possibilities, we return success but don't
16441 deduce anything. */
16442
16443 if (good == 1)
16444 {
16445 int i = TREE_VEC_LENGTH (targs);
16446 for (; i--; )
16447 if (TREE_VEC_ELT (tempargs, i))
16448 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16449 }
16450 if (good)
16451 return true;
16452
16453 return false;
16454 }
16455
16456 /* Core DR 115: In contexts where deduction is done and fails, or in
16457 contexts where deduction is not done, if a template argument list is
16458 specified and it, along with any default template arguments, identifies
16459 a single function template specialization, then the template-id is an
16460 lvalue for the function template specialization. */
16461
16462 tree
16463 resolve_nondeduced_context (tree orig_expr)
16464 {
16465 tree expr, offset, baselink;
16466 bool addr;
16467
16468 if (!type_unknown_p (orig_expr))
16469 return orig_expr;
16470
16471 expr = orig_expr;
16472 addr = false;
16473 offset = NULL_TREE;
16474 baselink = NULL_TREE;
16475
16476 if (TREE_CODE (expr) == ADDR_EXPR)
16477 {
16478 expr = TREE_OPERAND (expr, 0);
16479 addr = true;
16480 }
16481 if (TREE_CODE (expr) == OFFSET_REF)
16482 {
16483 offset = expr;
16484 expr = TREE_OPERAND (expr, 1);
16485 }
16486 if (BASELINK_P (expr))
16487 {
16488 baselink = expr;
16489 expr = BASELINK_FUNCTIONS (expr);
16490 }
16491
16492 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16493 {
16494 int good = 0;
16495 tree goodfn = NULL_TREE;
16496
16497 /* If we got some explicit template args, we need to plug them into
16498 the affected templates before we try to unify, in case the
16499 explicit args will completely resolve the templates in question. */
16500
16501 tree expl_subargs = TREE_OPERAND (expr, 1);
16502 tree arg = TREE_OPERAND (expr, 0);
16503 tree badfn = NULL_TREE;
16504 tree badargs = NULL_TREE;
16505
16506 for (; arg; arg = OVL_NEXT (arg))
16507 {
16508 tree fn = OVL_CURRENT (arg);
16509 tree subargs, elem;
16510
16511 if (TREE_CODE (fn) != TEMPLATE_DECL)
16512 continue;
16513
16514 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16515 expl_subargs, NULL_TREE, tf_none,
16516 /*require_all_args=*/true,
16517 /*use_default_args=*/true);
16518 if (subargs != error_mark_node
16519 && !any_dependent_template_arguments_p (subargs))
16520 {
16521 elem = instantiate_template (fn, subargs, tf_none);
16522 if (elem == error_mark_node)
16523 {
16524 badfn = fn;
16525 badargs = subargs;
16526 }
16527 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16528 {
16529 goodfn = elem;
16530 ++good;
16531 }
16532 }
16533 }
16534 if (good == 1)
16535 {
16536 mark_used (goodfn);
16537 expr = goodfn;
16538 if (baselink)
16539 expr = build_baselink (BASELINK_BINFO (baselink),
16540 BASELINK_ACCESS_BINFO (baselink),
16541 expr, BASELINK_OPTYPE (baselink));
16542 if (offset)
16543 {
16544 tree base
16545 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16546 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16547 }
16548 if (addr)
16549 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16550 return expr;
16551 }
16552 else if (good == 0 && badargs)
16553 /* There were no good options and at least one bad one, so let the
16554 user know what the problem is. */
16555 instantiate_template (badfn, badargs, tf_warning_or_error);
16556 }
16557 return orig_expr;
16558 }
16559
16560 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16561 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16562 different overloads deduce different arguments for a given parm.
16563 ADDR_P is true if the expression for which deduction is being
16564 performed was of the form "& fn" rather than simply "fn".
16565
16566 Returns 1 on success. */
16567
16568 static int
16569 try_one_overload (tree tparms,
16570 tree orig_targs,
16571 tree targs,
16572 tree parm,
16573 tree arg,
16574 unification_kind_t strict,
16575 int sub_strict,
16576 bool addr_p,
16577 bool explain_p)
16578 {
16579 int nargs;
16580 tree tempargs;
16581 int i;
16582
16583 if (arg == error_mark_node)
16584 return 0;
16585
16586 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16587 to function or pointer to member function argument if the set of
16588 overloaded functions does not contain function templates and at most
16589 one of a set of overloaded functions provides a unique match.
16590
16591 So if this is a template, just return success. */
16592
16593 if (uses_template_parms (arg))
16594 return 1;
16595
16596 if (TREE_CODE (arg) == METHOD_TYPE)
16597 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16598 else if (addr_p)
16599 arg = build_pointer_type (arg);
16600
16601 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16602
16603 /* We don't copy orig_targs for this because if we have already deduced
16604 some template args from previous args, unify would complain when we
16605 try to deduce a template parameter for the same argument, even though
16606 there isn't really a conflict. */
16607 nargs = TREE_VEC_LENGTH (targs);
16608 tempargs = make_tree_vec (nargs);
16609
16610 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16611 return 0;
16612
16613 /* First make sure we didn't deduce anything that conflicts with
16614 explicitly specified args. */
16615 for (i = nargs; i--; )
16616 {
16617 tree elt = TREE_VEC_ELT (tempargs, i);
16618 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16619
16620 if (!elt)
16621 /*NOP*/;
16622 else if (uses_template_parms (elt))
16623 /* Since we're unifying against ourselves, we will fill in
16624 template args used in the function parm list with our own
16625 template parms. Discard them. */
16626 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16627 else if (oldelt && !template_args_equal (oldelt, elt))
16628 return 0;
16629 }
16630
16631 for (i = nargs; i--; )
16632 {
16633 tree elt = TREE_VEC_ELT (tempargs, i);
16634
16635 if (elt)
16636 TREE_VEC_ELT (targs, i) = elt;
16637 }
16638
16639 return 1;
16640 }
16641
16642 /* PARM is a template class (perhaps with unbound template
16643 parameters). ARG is a fully instantiated type. If ARG can be
16644 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16645 TARGS are as for unify. */
16646
16647 static tree
16648 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16649 bool explain_p)
16650 {
16651 tree copy_of_targs;
16652
16653 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16654 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16655 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16656 return NULL_TREE;
16657
16658 /* We need to make a new template argument vector for the call to
16659 unify. If we used TARGS, we'd clutter it up with the result of
16660 the attempted unification, even if this class didn't work out.
16661 We also don't want to commit ourselves to all the unifications
16662 we've already done, since unification is supposed to be done on
16663 an argument-by-argument basis. In other words, consider the
16664 following pathological case:
16665
16666 template <int I, int J, int K>
16667 struct S {};
16668
16669 template <int I, int J>
16670 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16671
16672 template <int I, int J, int K>
16673 void f(S<I, J, K>, S<I, I, I>);
16674
16675 void g() {
16676 S<0, 0, 0> s0;
16677 S<0, 1, 2> s2;
16678
16679 f(s0, s2);
16680 }
16681
16682 Now, by the time we consider the unification involving `s2', we
16683 already know that we must have `f<0, 0, 0>'. But, even though
16684 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16685 because there are two ways to unify base classes of S<0, 1, 2>
16686 with S<I, I, I>. If we kept the already deduced knowledge, we
16687 would reject the possibility I=1. */
16688 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16689
16690 /* If unification failed, we're done. */
16691 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16692 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16693 return NULL_TREE;
16694
16695 return arg;
16696 }
16697
16698 /* Given a template type PARM and a class type ARG, find the unique
16699 base type in ARG that is an instance of PARM. We do not examine
16700 ARG itself; only its base-classes. If there is not exactly one
16701 appropriate base class, return NULL_TREE. PARM may be the type of
16702 a partial specialization, as well as a plain template type. Used
16703 by unify. */
16704
16705 static enum template_base_result
16706 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16707 bool explain_p, tree *result)
16708 {
16709 tree rval = NULL_TREE;
16710 tree binfo;
16711
16712 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16713
16714 binfo = TYPE_BINFO (complete_type (arg));
16715 if (!binfo)
16716 {
16717 /* The type could not be completed. */
16718 *result = NULL_TREE;
16719 return tbr_incomplete_type;
16720 }
16721
16722 /* Walk in inheritance graph order. The search order is not
16723 important, and this avoids multiple walks of virtual bases. */
16724 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16725 {
16726 tree r = try_class_unification (tparms, targs, parm,
16727 BINFO_TYPE (binfo), explain_p);
16728
16729 if (r)
16730 {
16731 /* If there is more than one satisfactory baseclass, then:
16732
16733 [temp.deduct.call]
16734
16735 If they yield more than one possible deduced A, the type
16736 deduction fails.
16737
16738 applies. */
16739 if (rval && !same_type_p (r, rval))
16740 {
16741 *result = NULL_TREE;
16742 return tbr_ambiguous_baseclass;
16743 }
16744
16745 rval = r;
16746 }
16747 }
16748
16749 *result = rval;
16750 return tbr_success;
16751 }
16752
16753 /* Returns the level of DECL, which declares a template parameter. */
16754
16755 static int
16756 template_decl_level (tree decl)
16757 {
16758 switch (TREE_CODE (decl))
16759 {
16760 case TYPE_DECL:
16761 case TEMPLATE_DECL:
16762 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16763
16764 case PARM_DECL:
16765 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16766
16767 default:
16768 gcc_unreachable ();
16769 }
16770 return 0;
16771 }
16772
16773 /* Decide whether ARG can be unified with PARM, considering only the
16774 cv-qualifiers of each type, given STRICT as documented for unify.
16775 Returns nonzero iff the unification is OK on that basis. */
16776
16777 static int
16778 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16779 {
16780 int arg_quals = cp_type_quals (arg);
16781 int parm_quals = cp_type_quals (parm);
16782
16783 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16784 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16785 {
16786 /* Although a CVR qualifier is ignored when being applied to a
16787 substituted template parameter ([8.3.2]/1 for example), that
16788 does not allow us to unify "const T" with "int&" because both
16789 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16790 It is ok when we're allowing additional CV qualifiers
16791 at the outer level [14.8.2.1]/3,1st bullet. */
16792 if ((TREE_CODE (arg) == REFERENCE_TYPE
16793 || TREE_CODE (arg) == FUNCTION_TYPE
16794 || TREE_CODE (arg) == METHOD_TYPE)
16795 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16796 return 0;
16797
16798 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16799 && (parm_quals & TYPE_QUAL_RESTRICT))
16800 return 0;
16801 }
16802
16803 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16804 && (arg_quals & parm_quals) != parm_quals)
16805 return 0;
16806
16807 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16808 && (parm_quals & arg_quals) != arg_quals)
16809 return 0;
16810
16811 return 1;
16812 }
16813
16814 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16815 void
16816 template_parm_level_and_index (tree parm, int* level, int* index)
16817 {
16818 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16819 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16820 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16821 {
16822 *index = TEMPLATE_TYPE_IDX (parm);
16823 *level = TEMPLATE_TYPE_LEVEL (parm);
16824 }
16825 else
16826 {
16827 *index = TEMPLATE_PARM_IDX (parm);
16828 *level = TEMPLATE_PARM_LEVEL (parm);
16829 }
16830 }
16831
16832 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16833 do { \
16834 if (unify (TP, TA, P, A, S, EP)) \
16835 return 1; \
16836 } while (0);
16837
16838 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16839 expansion at the end of PACKED_PARMS. Returns 0 if the type
16840 deduction succeeds, 1 otherwise. STRICT is the same as in
16841 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16842 call argument list. We'll need to adjust the arguments to make them
16843 types. SUBR tells us if this is from a recursive call to
16844 type_unification_real, or for comparing two template argument
16845 lists. */
16846
16847 static int
16848 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16849 tree packed_args, unification_kind_t strict,
16850 bool subr, bool explain_p)
16851 {
16852 tree parm
16853 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16854 tree pattern = PACK_EXPANSION_PATTERN (parm);
16855 tree pack, packs = NULL_TREE;
16856 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16857 int len = TREE_VEC_LENGTH (packed_args);
16858
16859 /* Determine the parameter packs we will be deducing from the
16860 pattern, and record their current deductions. */
16861 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16862 pack; pack = TREE_CHAIN (pack))
16863 {
16864 tree parm_pack = TREE_VALUE (pack);
16865 int idx, level;
16866
16867 /* Determine the index and level of this parameter pack. */
16868 template_parm_level_and_index (parm_pack, &level, &idx);
16869
16870 /* Keep track of the parameter packs and their corresponding
16871 argument packs. */
16872 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16873 TREE_TYPE (packs) = make_tree_vec (len - start);
16874 }
16875
16876 /* Loop through all of the arguments that have not yet been
16877 unified and unify each with the pattern. */
16878 for (i = start; i < len; i++)
16879 {
16880 tree parm;
16881 bool any_explicit = false;
16882 tree arg = TREE_VEC_ELT (packed_args, i);
16883
16884 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16885 or the element of its argument pack at the current index if
16886 this argument was explicitly specified. */
16887 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16888 {
16889 int idx, level;
16890 tree arg, pargs;
16891 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16892
16893 arg = NULL_TREE;
16894 if (TREE_VALUE (pack)
16895 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16896 && (i - start < TREE_VEC_LENGTH (pargs)))
16897 {
16898 any_explicit = true;
16899 arg = TREE_VEC_ELT (pargs, i - start);
16900 }
16901 TMPL_ARG (targs, level, idx) = arg;
16902 }
16903
16904 /* If we had explicit template arguments, substitute them into the
16905 pattern before deduction. */
16906 if (any_explicit)
16907 {
16908 /* Some arguments might still be unspecified or dependent. */
16909 bool dependent;
16910 ++processing_template_decl;
16911 dependent = any_dependent_template_arguments_p (targs);
16912 if (!dependent)
16913 --processing_template_decl;
16914 parm = tsubst (pattern, targs,
16915 explain_p ? tf_warning_or_error : tf_none,
16916 NULL_TREE);
16917 if (dependent)
16918 --processing_template_decl;
16919 if (parm == error_mark_node)
16920 return 1;
16921 }
16922 else
16923 parm = pattern;
16924
16925 /* Unify the pattern with the current argument. */
16926 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16927 LOOKUP_IMPLICIT, explain_p))
16928 return 1;
16929
16930 /* For each parameter pack, collect the deduced value. */
16931 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16932 {
16933 int idx, level;
16934 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16935
16936 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16937 TMPL_ARG (targs, level, idx);
16938 }
16939 }
16940
16941 /* Verify that the results of unification with the parameter packs
16942 produce results consistent with what we've seen before, and make
16943 the deduced argument packs available. */
16944 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16945 {
16946 tree old_pack = TREE_VALUE (pack);
16947 tree new_args = TREE_TYPE (pack);
16948 int i, len = TREE_VEC_LENGTH (new_args);
16949 int idx, level;
16950 bool nondeduced_p = false;
16951
16952 /* By default keep the original deduced argument pack.
16953 If necessary, more specific code is going to update the
16954 resulting deduced argument later down in this function. */
16955 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16956 TMPL_ARG (targs, level, idx) = old_pack;
16957
16958 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16959 actually deduce anything. */
16960 for (i = 0; i < len && !nondeduced_p; ++i)
16961 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16962 nondeduced_p = true;
16963 if (nondeduced_p)
16964 continue;
16965
16966 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16967 {
16968 /* If we had fewer function args than explicit template args,
16969 just use the explicits. */
16970 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16971 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16972 if (len < explicit_len)
16973 new_args = explicit_args;
16974 }
16975
16976 if (!old_pack)
16977 {
16978 tree result;
16979 /* Build the deduced *_ARGUMENT_PACK. */
16980 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16981 {
16982 result = make_node (NONTYPE_ARGUMENT_PACK);
16983 TREE_TYPE (result) =
16984 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16985 TREE_CONSTANT (result) = 1;
16986 }
16987 else
16988 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16989
16990 SET_ARGUMENT_PACK_ARGS (result, new_args);
16991
16992 /* Note the deduced argument packs for this parameter
16993 pack. */
16994 TMPL_ARG (targs, level, idx) = result;
16995 }
16996 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16997 && (ARGUMENT_PACK_ARGS (old_pack)
16998 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16999 {
17000 /* We only had the explicitly-provided arguments before, but
17001 now we have a complete set of arguments. */
17002 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17003
17004 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17005 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17006 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17007 }
17008 else
17009 {
17010 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17011 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17012
17013 if (!comp_template_args_with_info (old_args, new_args,
17014 &bad_old_arg, &bad_new_arg))
17015 /* Inconsistent unification of this parameter pack. */
17016 return unify_parameter_pack_inconsistent (explain_p,
17017 bad_old_arg,
17018 bad_new_arg);
17019 }
17020 }
17021
17022 return unify_success (explain_p);
17023 }
17024
17025 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17026 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17027 parameters and return value are as for unify. */
17028
17029 static int
17030 unify_array_domain (tree tparms, tree targs,
17031 tree parm_dom, tree arg_dom,
17032 bool explain_p)
17033 {
17034 tree parm_max;
17035 tree arg_max;
17036 bool parm_cst;
17037 bool arg_cst;
17038
17039 /* Our representation of array types uses "N - 1" as the
17040 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17041 not an integer constant. We cannot unify arbitrarily
17042 complex expressions, so we eliminate the MINUS_EXPRs
17043 here. */
17044 parm_max = TYPE_MAX_VALUE (parm_dom);
17045 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17046 if (!parm_cst)
17047 {
17048 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17049 parm_max = TREE_OPERAND (parm_max, 0);
17050 }
17051 arg_max = TYPE_MAX_VALUE (arg_dom);
17052 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17053 if (!arg_cst)
17054 {
17055 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17056 trying to unify the type of a variable with the type
17057 of a template parameter. For example:
17058
17059 template <unsigned int N>
17060 void f (char (&) [N]);
17061 int g();
17062 void h(int i) {
17063 char a[g(i)];
17064 f(a);
17065 }
17066
17067 Here, the type of the ARG will be "int [g(i)]", and
17068 may be a SAVE_EXPR, etc. */
17069 if (TREE_CODE (arg_max) != MINUS_EXPR)
17070 return unify_vla_arg (explain_p, arg_dom);
17071 arg_max = TREE_OPERAND (arg_max, 0);
17072 }
17073
17074 /* If only one of the bounds used a MINUS_EXPR, compensate
17075 by adding one to the other bound. */
17076 if (parm_cst && !arg_cst)
17077 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17078 integer_type_node,
17079 parm_max,
17080 integer_one_node);
17081 else if (arg_cst && !parm_cst)
17082 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17083 integer_type_node,
17084 arg_max,
17085 integer_one_node);
17086
17087 return unify (tparms, targs, parm_max, arg_max,
17088 UNIFY_ALLOW_INTEGER, explain_p);
17089 }
17090
17091 /* Deduce the value of template parameters. TPARMS is the (innermost)
17092 set of template parameters to a template. TARGS is the bindings
17093 for those template parameters, as determined thus far; TARGS may
17094 include template arguments for outer levels of template parameters
17095 as well. PARM is a parameter to a template function, or a
17096 subcomponent of that parameter; ARG is the corresponding argument.
17097 This function attempts to match PARM with ARG in a manner
17098 consistent with the existing assignments in TARGS. If more values
17099 are deduced, then TARGS is updated.
17100
17101 Returns 0 if the type deduction succeeds, 1 otherwise. The
17102 parameter STRICT is a bitwise or of the following flags:
17103
17104 UNIFY_ALLOW_NONE:
17105 Require an exact match between PARM and ARG.
17106 UNIFY_ALLOW_MORE_CV_QUAL:
17107 Allow the deduced ARG to be more cv-qualified (by qualification
17108 conversion) than ARG.
17109 UNIFY_ALLOW_LESS_CV_QUAL:
17110 Allow the deduced ARG to be less cv-qualified than ARG.
17111 UNIFY_ALLOW_DERIVED:
17112 Allow the deduced ARG to be a template base class of ARG,
17113 or a pointer to a template base class of the type pointed to by
17114 ARG.
17115 UNIFY_ALLOW_INTEGER:
17116 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17117 case for more information.
17118 UNIFY_ALLOW_OUTER_LEVEL:
17119 This is the outermost level of a deduction. Used to determine validity
17120 of qualification conversions. A valid qualification conversion must
17121 have const qualified pointers leading up to the inner type which
17122 requires additional CV quals, except at the outer level, where const
17123 is not required [conv.qual]. It would be normal to set this flag in
17124 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17125 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17126 This is the outermost level of a deduction, and PARM can be more CV
17127 qualified at this point.
17128 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17129 This is the outermost level of a deduction, and PARM can be less CV
17130 qualified at this point. */
17131
17132 static int
17133 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17134 bool explain_p)
17135 {
17136 int idx;
17137 tree targ;
17138 tree tparm;
17139 int strict_in = strict;
17140
17141 /* I don't think this will do the right thing with respect to types.
17142 But the only case I've seen it in so far has been array bounds, where
17143 signedness is the only information lost, and I think that will be
17144 okay. */
17145 while (TREE_CODE (parm) == NOP_EXPR)
17146 parm = TREE_OPERAND (parm, 0);
17147
17148 if (arg == error_mark_node)
17149 return unify_invalid (explain_p);
17150 if (arg == unknown_type_node
17151 || arg == init_list_type_node)
17152 /* We can't deduce anything from this, but we might get all the
17153 template args from other function args. */
17154 return unify_success (explain_p);
17155
17156 /* If PARM uses template parameters, then we can't bail out here,
17157 even if ARG == PARM, since we won't record unifications for the
17158 template parameters. We might need them if we're trying to
17159 figure out which of two things is more specialized. */
17160 if (arg == parm && !uses_template_parms (parm))
17161 return unify_success (explain_p);
17162
17163 /* Handle init lists early, so the rest of the function can assume
17164 we're dealing with a type. */
17165 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17166 {
17167 tree elt, elttype;
17168 unsigned i;
17169 tree orig_parm = parm;
17170
17171 /* Replace T with std::initializer_list<T> for deduction. */
17172 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17173 && flag_deduce_init_list)
17174 parm = listify (parm);
17175
17176 if (!is_std_init_list (parm)
17177 && TREE_CODE (parm) != ARRAY_TYPE)
17178 /* We can only deduce from an initializer list argument if the
17179 parameter is std::initializer_list or an array; otherwise this
17180 is a non-deduced context. */
17181 return unify_success (explain_p);
17182
17183 if (TREE_CODE (parm) == ARRAY_TYPE)
17184 elttype = TREE_TYPE (parm);
17185 else
17186 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17187
17188 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17189 {
17190 int elt_strict = strict;
17191
17192 if (elt == error_mark_node)
17193 return unify_invalid (explain_p);
17194
17195 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17196 {
17197 tree type = TREE_TYPE (elt);
17198 /* It should only be possible to get here for a call. */
17199 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17200 elt_strict |= maybe_adjust_types_for_deduction
17201 (DEDUCE_CALL, &elttype, &type, elt);
17202 elt = type;
17203 }
17204
17205 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17206 explain_p);
17207 }
17208
17209 if (TREE_CODE (parm) == ARRAY_TYPE)
17210 {
17211 /* Also deduce from the length of the initializer list. */
17212 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17213 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17214 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17215 idx, explain_p);
17216 }
17217
17218 /* If the std::initializer_list<T> deduction worked, replace the
17219 deduced A with std::initializer_list<A>. */
17220 if (orig_parm != parm)
17221 {
17222 idx = TEMPLATE_TYPE_IDX (orig_parm);
17223 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17224 targ = listify (targ);
17225 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17226 }
17227 return unify_success (explain_p);
17228 }
17229
17230 /* Immediately reject some pairs that won't unify because of
17231 cv-qualification mismatches. */
17232 if (TREE_CODE (arg) == TREE_CODE (parm)
17233 && TYPE_P (arg)
17234 /* It is the elements of the array which hold the cv quals of an array
17235 type, and the elements might be template type parms. We'll check
17236 when we recurse. */
17237 && TREE_CODE (arg) != ARRAY_TYPE
17238 /* We check the cv-qualifiers when unifying with template type
17239 parameters below. We want to allow ARG `const T' to unify with
17240 PARM `T' for example, when computing which of two templates
17241 is more specialized, for example. */
17242 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17243 && !check_cv_quals_for_unify (strict_in, arg, parm))
17244 return unify_cv_qual_mismatch (explain_p, parm, arg);
17245
17246 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17247 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17248 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17249 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17250 strict &= ~UNIFY_ALLOW_DERIVED;
17251 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17252 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17253
17254 switch (TREE_CODE (parm))
17255 {
17256 case TYPENAME_TYPE:
17257 case SCOPE_REF:
17258 case UNBOUND_CLASS_TEMPLATE:
17259 /* In a type which contains a nested-name-specifier, template
17260 argument values cannot be deduced for template parameters used
17261 within the nested-name-specifier. */
17262 return unify_success (explain_p);
17263
17264 case TEMPLATE_TYPE_PARM:
17265 case TEMPLATE_TEMPLATE_PARM:
17266 case BOUND_TEMPLATE_TEMPLATE_PARM:
17267 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17268 if (tparm == error_mark_node)
17269 return unify_invalid (explain_p);
17270
17271 if (TEMPLATE_TYPE_LEVEL (parm)
17272 != template_decl_level (tparm))
17273 /* The PARM is not one we're trying to unify. Just check
17274 to see if it matches ARG. */
17275 {
17276 if (TREE_CODE (arg) == TREE_CODE (parm)
17277 && (is_auto (parm) ? is_auto (arg)
17278 : same_type_p (parm, arg)))
17279 return unify_success (explain_p);
17280 else
17281 return unify_type_mismatch (explain_p, parm, arg);
17282 }
17283 idx = TEMPLATE_TYPE_IDX (parm);
17284 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17285 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17286 if (tparm == error_mark_node)
17287 return unify_invalid (explain_p);
17288
17289 /* Check for mixed types and values. */
17290 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17291 && TREE_CODE (tparm) != TYPE_DECL)
17292 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17293 && TREE_CODE (tparm) != TEMPLATE_DECL))
17294 gcc_unreachable ();
17295
17296 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17297 {
17298 /* ARG must be constructed from a template class or a template
17299 template parameter. */
17300 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17301 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17302 return unify_template_deduction_failure (explain_p, parm, arg);
17303
17304 {
17305 tree parmvec = TYPE_TI_ARGS (parm);
17306 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17307 tree full_argvec = add_to_template_args (targs, argvec);
17308 tree parm_parms
17309 = DECL_INNERMOST_TEMPLATE_PARMS
17310 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17311 int i, len;
17312 int parm_variadic_p = 0;
17313
17314 /* The resolution to DR150 makes clear that default
17315 arguments for an N-argument may not be used to bind T
17316 to a template template parameter with fewer than N
17317 parameters. It is not safe to permit the binding of
17318 default arguments as an extension, as that may change
17319 the meaning of a conforming program. Consider:
17320
17321 struct Dense { static const unsigned int dim = 1; };
17322
17323 template <template <typename> class View,
17324 typename Block>
17325 void operator+(float, View<Block> const&);
17326
17327 template <typename Block,
17328 unsigned int Dim = Block::dim>
17329 struct Lvalue_proxy { operator float() const; };
17330
17331 void
17332 test_1d (void) {
17333 Lvalue_proxy<Dense> p;
17334 float b;
17335 b + p;
17336 }
17337
17338 Here, if Lvalue_proxy is permitted to bind to View, then
17339 the global operator+ will be used; if they are not, the
17340 Lvalue_proxy will be converted to float. */
17341 if (coerce_template_parms (parm_parms,
17342 full_argvec,
17343 TYPE_TI_TEMPLATE (parm),
17344 (explain_p
17345 ? tf_warning_or_error
17346 : tf_none),
17347 /*require_all_args=*/true,
17348 /*use_default_args=*/false)
17349 == error_mark_node)
17350 return 1;
17351
17352 /* Deduce arguments T, i from TT<T> or TT<i>.
17353 We check each element of PARMVEC and ARGVEC individually
17354 rather than the whole TREE_VEC since they can have
17355 different number of elements. */
17356
17357 parmvec = expand_template_argument_pack (parmvec);
17358 argvec = expand_template_argument_pack (argvec);
17359
17360 len = TREE_VEC_LENGTH (parmvec);
17361
17362 /* Check if the parameters end in a pack, making them
17363 variadic. */
17364 if (len > 0
17365 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17366 parm_variadic_p = 1;
17367
17368 for (i = 0; i < len - parm_variadic_p; ++i)
17369 /* If the template argument list of P contains a pack
17370 expansion that is not the last template argument, the
17371 entire template argument list is a non-deduced
17372 context. */
17373 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17374 return unify_success (explain_p);
17375
17376 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17377 return unify_too_few_arguments (explain_p,
17378 TREE_VEC_LENGTH (argvec), len);
17379
17380 for (i = 0; i < len - parm_variadic_p; ++i)
17381 {
17382 RECUR_AND_CHECK_FAILURE (tparms, targs,
17383 TREE_VEC_ELT (parmvec, i),
17384 TREE_VEC_ELT (argvec, i),
17385 UNIFY_ALLOW_NONE, explain_p);
17386 }
17387
17388 if (parm_variadic_p
17389 && unify_pack_expansion (tparms, targs,
17390 parmvec, argvec,
17391 DEDUCE_EXACT,
17392 /*subr=*/true, explain_p))
17393 return 1;
17394 }
17395 arg = TYPE_TI_TEMPLATE (arg);
17396
17397 /* Fall through to deduce template name. */
17398 }
17399
17400 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17401 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17402 {
17403 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17404
17405 /* Simple cases: Value already set, does match or doesn't. */
17406 if (targ != NULL_TREE && template_args_equal (targ, arg))
17407 return unify_success (explain_p);
17408 else if (targ)
17409 return unify_inconsistency (explain_p, parm, targ, arg);
17410 }
17411 else
17412 {
17413 /* If PARM is `const T' and ARG is only `int', we don't have
17414 a match unless we are allowing additional qualification.
17415 If ARG is `const int' and PARM is just `T' that's OK;
17416 that binds `const int' to `T'. */
17417 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17418 arg, parm))
17419 return unify_cv_qual_mismatch (explain_p, parm, arg);
17420
17421 /* Consider the case where ARG is `const volatile int' and
17422 PARM is `const T'. Then, T should be `volatile int'. */
17423 arg = cp_build_qualified_type_real
17424 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17425 if (arg == error_mark_node)
17426 return unify_invalid (explain_p);
17427
17428 /* Simple cases: Value already set, does match or doesn't. */
17429 if (targ != NULL_TREE && same_type_p (targ, arg))
17430 return unify_success (explain_p);
17431 else if (targ)
17432 return unify_inconsistency (explain_p, parm, targ, arg);
17433
17434 /* Make sure that ARG is not a variable-sized array. (Note
17435 that were talking about variable-sized arrays (like
17436 `int[n]'), rather than arrays of unknown size (like
17437 `int[]').) We'll get very confused by such a type since
17438 the bound of the array is not constant, and therefore
17439 not mangleable. Besides, such types are not allowed in
17440 ISO C++, so we can do as we please here. We do allow
17441 them for 'auto' deduction, since that isn't ABI-exposed. */
17442 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17443 return unify_vla_arg (explain_p, arg);
17444
17445 /* Strip typedefs as in convert_template_argument. */
17446 arg = canonicalize_type_argument (arg, tf_none);
17447 }
17448
17449 /* If ARG is a parameter pack or an expansion, we cannot unify
17450 against it unless PARM is also a parameter pack. */
17451 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17452 && !template_parameter_pack_p (parm))
17453 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17454
17455 /* If the argument deduction results is a METHOD_TYPE,
17456 then there is a problem.
17457 METHOD_TYPE doesn't map to any real C++ type the result of
17458 the deduction can not be of that type. */
17459 if (TREE_CODE (arg) == METHOD_TYPE)
17460 return unify_method_type_error (explain_p, arg);
17461
17462 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17463 return unify_success (explain_p);
17464
17465 case TEMPLATE_PARM_INDEX:
17466 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17467 if (tparm == error_mark_node)
17468 return unify_invalid (explain_p);
17469
17470 if (TEMPLATE_PARM_LEVEL (parm)
17471 != template_decl_level (tparm))
17472 {
17473 /* The PARM is not one we're trying to unify. Just check
17474 to see if it matches ARG. */
17475 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17476 && cp_tree_equal (parm, arg));
17477 if (result)
17478 unify_expression_unequal (explain_p, parm, arg);
17479 return result;
17480 }
17481
17482 idx = TEMPLATE_PARM_IDX (parm);
17483 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17484
17485 if (targ)
17486 {
17487 int x = !cp_tree_equal (targ, arg);
17488 if (x)
17489 unify_inconsistency (explain_p, parm, targ, arg);
17490 return x;
17491 }
17492
17493 /* [temp.deduct.type] If, in the declaration of a function template
17494 with a non-type template-parameter, the non-type
17495 template-parameter is used in an expression in the function
17496 parameter-list and, if the corresponding template-argument is
17497 deduced, the template-argument type shall match the type of the
17498 template-parameter exactly, except that a template-argument
17499 deduced from an array bound may be of any integral type.
17500 The non-type parameter might use already deduced type parameters. */
17501 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17502 if (!TREE_TYPE (arg))
17503 /* Template-parameter dependent expression. Just accept it for now.
17504 It will later be processed in convert_template_argument. */
17505 ;
17506 else if (same_type_p (TREE_TYPE (arg), tparm))
17507 /* OK */;
17508 else if ((strict & UNIFY_ALLOW_INTEGER)
17509 && CP_INTEGRAL_TYPE_P (tparm))
17510 /* Convert the ARG to the type of PARM; the deduced non-type
17511 template argument must exactly match the types of the
17512 corresponding parameter. */
17513 arg = fold (build_nop (tparm, arg));
17514 else if (uses_template_parms (tparm))
17515 /* We haven't deduced the type of this parameter yet. Try again
17516 later. */
17517 return unify_success (explain_p);
17518 else
17519 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17520
17521 /* If ARG is a parameter pack or an expansion, we cannot unify
17522 against it unless PARM is also a parameter pack. */
17523 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17524 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17525 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17526
17527 arg = strip_typedefs_expr (arg);
17528 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17529 return unify_success (explain_p);
17530
17531 case PTRMEM_CST:
17532 {
17533 /* A pointer-to-member constant can be unified only with
17534 another constant. */
17535 if (TREE_CODE (arg) != PTRMEM_CST)
17536 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17537
17538 /* Just unify the class member. It would be useless (and possibly
17539 wrong, depending on the strict flags) to unify also
17540 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17541 arg refer to the same variable, even if through different
17542 classes. For instance:
17543
17544 struct A { int x; };
17545 struct B : A { };
17546
17547 Unification of &A::x and &B::x must succeed. */
17548 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17549 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17550 }
17551
17552 case POINTER_TYPE:
17553 {
17554 if (!TYPE_PTR_P (arg))
17555 return unify_type_mismatch (explain_p, parm, arg);
17556
17557 /* [temp.deduct.call]
17558
17559 A can be another pointer or pointer to member type that can
17560 be converted to the deduced A via a qualification
17561 conversion (_conv.qual_).
17562
17563 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17564 This will allow for additional cv-qualification of the
17565 pointed-to types if appropriate. */
17566
17567 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17568 /* The derived-to-base conversion only persists through one
17569 level of pointers. */
17570 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17571
17572 return unify (tparms, targs, TREE_TYPE (parm),
17573 TREE_TYPE (arg), strict, explain_p);
17574 }
17575
17576 case REFERENCE_TYPE:
17577 if (TREE_CODE (arg) != REFERENCE_TYPE)
17578 return unify_type_mismatch (explain_p, parm, arg);
17579 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17580 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17581
17582 case ARRAY_TYPE:
17583 if (TREE_CODE (arg) != ARRAY_TYPE)
17584 return unify_type_mismatch (explain_p, parm, arg);
17585 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17586 != (TYPE_DOMAIN (arg) == NULL_TREE))
17587 return unify_type_mismatch (explain_p, parm, arg);
17588 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17589 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17590 if (TYPE_DOMAIN (parm) != NULL_TREE)
17591 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17592 TYPE_DOMAIN (arg), explain_p);
17593 return unify_success (explain_p);
17594
17595 case REAL_TYPE:
17596 case COMPLEX_TYPE:
17597 case VECTOR_TYPE:
17598 case INTEGER_TYPE:
17599 case BOOLEAN_TYPE:
17600 case ENUMERAL_TYPE:
17601 case VOID_TYPE:
17602 case NULLPTR_TYPE:
17603 if (TREE_CODE (arg) != TREE_CODE (parm))
17604 return unify_type_mismatch (explain_p, parm, arg);
17605
17606 /* We have already checked cv-qualification at the top of the
17607 function. */
17608 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17609 return unify_type_mismatch (explain_p, parm, arg);
17610
17611 /* As far as unification is concerned, this wins. Later checks
17612 will invalidate it if necessary. */
17613 return unify_success (explain_p);
17614
17615 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17616 /* Type INTEGER_CST can come from ordinary constant template args. */
17617 case INTEGER_CST:
17618 while (TREE_CODE (arg) == NOP_EXPR)
17619 arg = TREE_OPERAND (arg, 0);
17620
17621 if (TREE_CODE (arg) != INTEGER_CST)
17622 return unify_template_argument_mismatch (explain_p, parm, arg);
17623 return (tree_int_cst_equal (parm, arg)
17624 ? unify_success (explain_p)
17625 : unify_template_argument_mismatch (explain_p, parm, arg));
17626
17627 case TREE_VEC:
17628 {
17629 int i, len, argslen;
17630 int parm_variadic_p = 0;
17631
17632 if (TREE_CODE (arg) != TREE_VEC)
17633 return unify_template_argument_mismatch (explain_p, parm, arg);
17634
17635 len = TREE_VEC_LENGTH (parm);
17636 argslen = TREE_VEC_LENGTH (arg);
17637
17638 /* Check for pack expansions in the parameters. */
17639 for (i = 0; i < len; ++i)
17640 {
17641 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17642 {
17643 if (i == len - 1)
17644 /* We can unify against something with a trailing
17645 parameter pack. */
17646 parm_variadic_p = 1;
17647 else
17648 /* [temp.deduct.type]/9: If the template argument list of
17649 P contains a pack expansion that is not the last
17650 template argument, the entire template argument list
17651 is a non-deduced context. */
17652 return unify_success (explain_p);
17653 }
17654 }
17655
17656 /* If we don't have enough arguments to satisfy the parameters
17657 (not counting the pack expression at the end), or we have
17658 too many arguments for a parameter list that doesn't end in
17659 a pack expression, we can't unify. */
17660 if (parm_variadic_p
17661 ? argslen < len - parm_variadic_p
17662 : argslen != len)
17663 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17664
17665 /* Unify all of the parameters that precede the (optional)
17666 pack expression. */
17667 for (i = 0; i < len - parm_variadic_p; ++i)
17668 {
17669 RECUR_AND_CHECK_FAILURE (tparms, targs,
17670 TREE_VEC_ELT (parm, i),
17671 TREE_VEC_ELT (arg, i),
17672 UNIFY_ALLOW_NONE, explain_p);
17673 }
17674 if (parm_variadic_p)
17675 return unify_pack_expansion (tparms, targs, parm, arg,
17676 DEDUCE_EXACT,
17677 /*subr=*/true, explain_p);
17678 return unify_success (explain_p);
17679 }
17680
17681 case RECORD_TYPE:
17682 case UNION_TYPE:
17683 if (TREE_CODE (arg) != TREE_CODE (parm))
17684 return unify_type_mismatch (explain_p, parm, arg);
17685
17686 if (TYPE_PTRMEMFUNC_P (parm))
17687 {
17688 if (!TYPE_PTRMEMFUNC_P (arg))
17689 return unify_type_mismatch (explain_p, parm, arg);
17690
17691 return unify (tparms, targs,
17692 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17693 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17694 strict, explain_p);
17695 }
17696
17697 if (CLASSTYPE_TEMPLATE_INFO (parm))
17698 {
17699 tree t = NULL_TREE;
17700
17701 if (strict_in & UNIFY_ALLOW_DERIVED)
17702 {
17703 /* First, we try to unify the PARM and ARG directly. */
17704 t = try_class_unification (tparms, targs,
17705 parm, arg, explain_p);
17706
17707 if (!t)
17708 {
17709 /* Fallback to the special case allowed in
17710 [temp.deduct.call]:
17711
17712 If P is a class, and P has the form
17713 template-id, then A can be a derived class of
17714 the deduced A. Likewise, if P is a pointer to
17715 a class of the form template-id, A can be a
17716 pointer to a derived class pointed to by the
17717 deduced A. */
17718 enum template_base_result r;
17719 r = get_template_base (tparms, targs, parm, arg,
17720 explain_p, &t);
17721
17722 if (!t)
17723 return unify_no_common_base (explain_p, r, parm, arg);
17724 }
17725 }
17726 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17727 && (CLASSTYPE_TI_TEMPLATE (parm)
17728 == CLASSTYPE_TI_TEMPLATE (arg)))
17729 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17730 Then, we should unify `int' and `U'. */
17731 t = arg;
17732 else
17733 /* There's no chance of unification succeeding. */
17734 return unify_type_mismatch (explain_p, parm, arg);
17735
17736 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17737 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17738 }
17739 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17740 return unify_type_mismatch (explain_p, parm, arg);
17741 return unify_success (explain_p);
17742
17743 case METHOD_TYPE:
17744 case FUNCTION_TYPE:
17745 {
17746 unsigned int nargs;
17747 tree *args;
17748 tree a;
17749 unsigned int i;
17750
17751 if (TREE_CODE (arg) != TREE_CODE (parm))
17752 return unify_type_mismatch (explain_p, parm, arg);
17753
17754 /* CV qualifications for methods can never be deduced, they must
17755 match exactly. We need to check them explicitly here,
17756 because type_unification_real treats them as any other
17757 cv-qualified parameter. */
17758 if (TREE_CODE (parm) == METHOD_TYPE
17759 && (!check_cv_quals_for_unify
17760 (UNIFY_ALLOW_NONE,
17761 class_of_this_parm (arg),
17762 class_of_this_parm (parm))))
17763 return unify_cv_qual_mismatch (explain_p, parm, arg);
17764
17765 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17766 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17767
17768 nargs = list_length (TYPE_ARG_TYPES (arg));
17769 args = XALLOCAVEC (tree, nargs);
17770 for (a = TYPE_ARG_TYPES (arg), i = 0;
17771 a != NULL_TREE && a != void_list_node;
17772 a = TREE_CHAIN (a), ++i)
17773 args[i] = TREE_VALUE (a);
17774 nargs = i;
17775
17776 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17777 args, nargs, 1, DEDUCE_EXACT,
17778 LOOKUP_NORMAL, NULL, explain_p);
17779 }
17780
17781 case OFFSET_TYPE:
17782 /* Unify a pointer to member with a pointer to member function, which
17783 deduces the type of the member as a function type. */
17784 if (TYPE_PTRMEMFUNC_P (arg))
17785 {
17786 /* Check top-level cv qualifiers */
17787 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17788 return unify_cv_qual_mismatch (explain_p, parm, arg);
17789
17790 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17791 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17792 UNIFY_ALLOW_NONE, explain_p);
17793
17794 /* Determine the type of the function we are unifying against. */
17795 tree fntype = static_fn_type (arg);
17796
17797 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17798 }
17799
17800 if (TREE_CODE (arg) != OFFSET_TYPE)
17801 return unify_type_mismatch (explain_p, parm, arg);
17802 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17803 TYPE_OFFSET_BASETYPE (arg),
17804 UNIFY_ALLOW_NONE, explain_p);
17805 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17806 strict, explain_p);
17807
17808 case CONST_DECL:
17809 if (DECL_TEMPLATE_PARM_P (parm))
17810 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17811 if (arg != integral_constant_value (parm))
17812 return unify_template_argument_mismatch (explain_p, parm, arg);
17813 return unify_success (explain_p);
17814
17815 case FIELD_DECL:
17816 case TEMPLATE_DECL:
17817 /* Matched cases are handled by the ARG == PARM test above. */
17818 return unify_template_argument_mismatch (explain_p, parm, arg);
17819
17820 case VAR_DECL:
17821 /* A non-type template parameter that is a variable should be a
17822 an integral constant, in which case, it whould have been
17823 folded into its (constant) value. So we should not be getting
17824 a variable here. */
17825 gcc_unreachable ();
17826
17827 case TYPE_ARGUMENT_PACK:
17828 case NONTYPE_ARGUMENT_PACK:
17829 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17830 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17831
17832 case TYPEOF_TYPE:
17833 case DECLTYPE_TYPE:
17834 case UNDERLYING_TYPE:
17835 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17836 or UNDERLYING_TYPE nodes. */
17837 return unify_success (explain_p);
17838
17839 case ERROR_MARK:
17840 /* Unification fails if we hit an error node. */
17841 return unify_invalid (explain_p);
17842
17843 default:
17844 /* An unresolved overload is a nondeduced context. */
17845 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17846 return unify_success (explain_p);
17847 gcc_assert (EXPR_P (parm));
17848
17849 /* We must be looking at an expression. This can happen with
17850 something like:
17851
17852 template <int I>
17853 void foo(S<I>, S<I + 2>);
17854
17855 This is a "nondeduced context":
17856
17857 [deduct.type]
17858
17859 The nondeduced contexts are:
17860
17861 --A type that is a template-id in which one or more of
17862 the template-arguments is an expression that references
17863 a template-parameter.
17864
17865 In these cases, we assume deduction succeeded, but don't
17866 actually infer any unifications. */
17867
17868 if (!uses_template_parms (parm)
17869 && !template_args_equal (parm, arg))
17870 return unify_expression_unequal (explain_p, parm, arg);
17871 else
17872 return unify_success (explain_p);
17873 }
17874 }
17875 #undef RECUR_AND_CHECK_FAILURE
17876 \f
17877 /* Note that DECL can be defined in this translation unit, if
17878 required. */
17879
17880 static void
17881 mark_definable (tree decl)
17882 {
17883 tree clone;
17884 DECL_NOT_REALLY_EXTERN (decl) = 1;
17885 FOR_EACH_CLONE (clone, decl)
17886 DECL_NOT_REALLY_EXTERN (clone) = 1;
17887 }
17888
17889 /* Called if RESULT is explicitly instantiated, or is a member of an
17890 explicitly instantiated class. */
17891
17892 void
17893 mark_decl_instantiated (tree result, int extern_p)
17894 {
17895 SET_DECL_EXPLICIT_INSTANTIATION (result);
17896
17897 /* If this entity has already been written out, it's too late to
17898 make any modifications. */
17899 if (TREE_ASM_WRITTEN (result))
17900 return;
17901
17902 /* For anonymous namespace we don't need to do anything. */
17903 if (decl_anon_ns_mem_p (result))
17904 {
17905 gcc_assert (!TREE_PUBLIC (result));
17906 return;
17907 }
17908
17909 if (TREE_CODE (result) != FUNCTION_DECL)
17910 /* The TREE_PUBLIC flag for function declarations will have been
17911 set correctly by tsubst. */
17912 TREE_PUBLIC (result) = 1;
17913
17914 /* This might have been set by an earlier implicit instantiation. */
17915 DECL_COMDAT (result) = 0;
17916
17917 if (extern_p)
17918 DECL_NOT_REALLY_EXTERN (result) = 0;
17919 else
17920 {
17921 mark_definable (result);
17922 /* Always make artificials weak. */
17923 if (DECL_ARTIFICIAL (result) && flag_weak)
17924 comdat_linkage (result);
17925 /* For WIN32 we also want to put explicit instantiations in
17926 linkonce sections. */
17927 else if (TREE_PUBLIC (result))
17928 maybe_make_one_only (result);
17929 }
17930
17931 /* If EXTERN_P, then this function will not be emitted -- unless
17932 followed by an explicit instantiation, at which point its linkage
17933 will be adjusted. If !EXTERN_P, then this function will be
17934 emitted here. In neither circumstance do we want
17935 import_export_decl to adjust the linkage. */
17936 DECL_INTERFACE_KNOWN (result) = 1;
17937 }
17938
17939 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17940 important template arguments. If any are missing, we check whether
17941 they're important by using error_mark_node for substituting into any
17942 args that were used for partial ordering (the ones between ARGS and END)
17943 and seeing if it bubbles up. */
17944
17945 static bool
17946 check_undeduced_parms (tree targs, tree args, tree end)
17947 {
17948 bool found = false;
17949 int i;
17950 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17951 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17952 {
17953 found = true;
17954 TREE_VEC_ELT (targs, i) = error_mark_node;
17955 }
17956 if (found)
17957 {
17958 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17959 if (substed == error_mark_node)
17960 return true;
17961 }
17962 return false;
17963 }
17964
17965 /* Given two function templates PAT1 and PAT2, return:
17966
17967 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17968 -1 if PAT2 is more specialized than PAT1.
17969 0 if neither is more specialized.
17970
17971 LEN indicates the number of parameters we should consider
17972 (defaulted parameters should not be considered).
17973
17974 The 1998 std underspecified function template partial ordering, and
17975 DR214 addresses the issue. We take pairs of arguments, one from
17976 each of the templates, and deduce them against each other. One of
17977 the templates will be more specialized if all the *other*
17978 template's arguments deduce against its arguments and at least one
17979 of its arguments *does* *not* deduce against the other template's
17980 corresponding argument. Deduction is done as for class templates.
17981 The arguments used in deduction have reference and top level cv
17982 qualifiers removed. Iff both arguments were originally reference
17983 types *and* deduction succeeds in both directions, an lvalue reference
17984 wins against an rvalue reference and otherwise the template
17985 with the more cv-qualified argument wins for that pairing (if
17986 neither is more cv-qualified, they both are equal). Unlike regular
17987 deduction, after all the arguments have been deduced in this way,
17988 we do *not* verify the deduced template argument values can be
17989 substituted into non-deduced contexts.
17990
17991 The logic can be a bit confusing here, because we look at deduce1 and
17992 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17993 can find template arguments for pat1 to make arg1 look like arg2, that
17994 means that arg2 is at least as specialized as arg1. */
17995
17996 int
17997 more_specialized_fn (tree pat1, tree pat2, int len)
17998 {
17999 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18000 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18001 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18002 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18003 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18004 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18005 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18006 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18007 tree origs1, origs2;
18008 bool lose1 = false;
18009 bool lose2 = false;
18010
18011 /* Remove the this parameter from non-static member functions. If
18012 one is a non-static member function and the other is not a static
18013 member function, remove the first parameter from that function
18014 also. This situation occurs for operator functions where we
18015 locate both a member function (with this pointer) and non-member
18016 operator (with explicit first operand). */
18017 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18018 {
18019 len--; /* LEN is the number of significant arguments for DECL1 */
18020 args1 = TREE_CHAIN (args1);
18021 if (!DECL_STATIC_FUNCTION_P (decl2))
18022 args2 = TREE_CHAIN (args2);
18023 }
18024 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18025 {
18026 args2 = TREE_CHAIN (args2);
18027 if (!DECL_STATIC_FUNCTION_P (decl1))
18028 {
18029 len--;
18030 args1 = TREE_CHAIN (args1);
18031 }
18032 }
18033
18034 /* If only one is a conversion operator, they are unordered. */
18035 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18036 return 0;
18037
18038 /* Consider the return type for a conversion function */
18039 if (DECL_CONV_FN_P (decl1))
18040 {
18041 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18042 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18043 len++;
18044 }
18045
18046 processing_template_decl++;
18047
18048 origs1 = args1;
18049 origs2 = args2;
18050
18051 while (len--
18052 /* Stop when an ellipsis is seen. */
18053 && args1 != NULL_TREE && args2 != NULL_TREE)
18054 {
18055 tree arg1 = TREE_VALUE (args1);
18056 tree arg2 = TREE_VALUE (args2);
18057 int deduce1, deduce2;
18058 int quals1 = -1;
18059 int quals2 = -1;
18060 int ref1 = 0;
18061 int ref2 = 0;
18062
18063 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18064 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18065 {
18066 /* When both arguments are pack expansions, we need only
18067 unify the patterns themselves. */
18068 arg1 = PACK_EXPANSION_PATTERN (arg1);
18069 arg2 = PACK_EXPANSION_PATTERN (arg2);
18070
18071 /* This is the last comparison we need to do. */
18072 len = 0;
18073 }
18074
18075 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18076 {
18077 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18078 arg1 = TREE_TYPE (arg1);
18079 quals1 = cp_type_quals (arg1);
18080 }
18081
18082 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18083 {
18084 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18085 arg2 = TREE_TYPE (arg2);
18086 quals2 = cp_type_quals (arg2);
18087 }
18088
18089 arg1 = TYPE_MAIN_VARIANT (arg1);
18090 arg2 = TYPE_MAIN_VARIANT (arg2);
18091
18092 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18093 {
18094 int i, len2 = list_length (args2);
18095 tree parmvec = make_tree_vec (1);
18096 tree argvec = make_tree_vec (len2);
18097 tree ta = args2;
18098
18099 /* Setup the parameter vector, which contains only ARG1. */
18100 TREE_VEC_ELT (parmvec, 0) = arg1;
18101
18102 /* Setup the argument vector, which contains the remaining
18103 arguments. */
18104 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18105 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18106
18107 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18108 argvec, DEDUCE_EXACT,
18109 /*subr=*/true, /*explain_p=*/false)
18110 == 0);
18111
18112 /* We cannot deduce in the other direction, because ARG1 is
18113 a pack expansion but ARG2 is not. */
18114 deduce2 = 0;
18115 }
18116 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18117 {
18118 int i, len1 = list_length (args1);
18119 tree parmvec = make_tree_vec (1);
18120 tree argvec = make_tree_vec (len1);
18121 tree ta = args1;
18122
18123 /* Setup the parameter vector, which contains only ARG1. */
18124 TREE_VEC_ELT (parmvec, 0) = arg2;
18125
18126 /* Setup the argument vector, which contains the remaining
18127 arguments. */
18128 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18129 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18130
18131 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18132 argvec, DEDUCE_EXACT,
18133 /*subr=*/true, /*explain_p=*/false)
18134 == 0);
18135
18136 /* We cannot deduce in the other direction, because ARG2 is
18137 a pack expansion but ARG1 is not.*/
18138 deduce1 = 0;
18139 }
18140
18141 else
18142 {
18143 /* The normal case, where neither argument is a pack
18144 expansion. */
18145 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18146 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18147 == 0);
18148 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18149 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18150 == 0);
18151 }
18152
18153 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18154 arg2, then arg2 is not as specialized as arg1. */
18155 if (!deduce1)
18156 lose2 = true;
18157 if (!deduce2)
18158 lose1 = true;
18159
18160 /* "If, for a given type, deduction succeeds in both directions
18161 (i.e., the types are identical after the transformations above)
18162 and both P and A were reference types (before being replaced with
18163 the type referred to above):
18164 - if the type from the argument template was an lvalue reference and
18165 the type from the parameter template was not, the argument type is
18166 considered to be more specialized than the other; otherwise,
18167 - if the type from the argument template is more cv-qualified
18168 than the type from the parameter template (as described above),
18169 the argument type is considered to be more specialized than the other;
18170 otherwise,
18171 - neither type is more specialized than the other." */
18172
18173 if (deduce1 && deduce2)
18174 {
18175 if (ref1 && ref2 && ref1 != ref2)
18176 {
18177 if (ref1 > ref2)
18178 lose1 = true;
18179 else
18180 lose2 = true;
18181 }
18182 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18183 {
18184 if ((quals1 & quals2) == quals2)
18185 lose2 = true;
18186 if ((quals1 & quals2) == quals1)
18187 lose1 = true;
18188 }
18189 }
18190
18191 if (lose1 && lose2)
18192 /* We've failed to deduce something in either direction.
18193 These must be unordered. */
18194 break;
18195
18196 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18197 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18198 /* We have already processed all of the arguments in our
18199 handing of the pack expansion type. */
18200 len = 0;
18201
18202 args1 = TREE_CHAIN (args1);
18203 args2 = TREE_CHAIN (args2);
18204 }
18205
18206 /* "In most cases, all template parameters must have values in order for
18207 deduction to succeed, but for partial ordering purposes a template
18208 parameter may remain without a value provided it is not used in the
18209 types being used for partial ordering."
18210
18211 Thus, if we are missing any of the targs1 we need to substitute into
18212 origs1, then pat2 is not as specialized as pat1. This can happen when
18213 there is a nondeduced context. */
18214 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18215 lose2 = true;
18216 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18217 lose1 = true;
18218
18219 processing_template_decl--;
18220
18221 /* All things being equal, if the next argument is a pack expansion
18222 for one function but not for the other, prefer the
18223 non-variadic function. FIXME this is bogus; see c++/41958. */
18224 if (lose1 == lose2
18225 && args1 && TREE_VALUE (args1)
18226 && args2 && TREE_VALUE (args2))
18227 {
18228 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18229 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18230 }
18231
18232 if (lose1 == lose2)
18233 return 0;
18234 else if (!lose1)
18235 return 1;
18236 else
18237 return -1;
18238 }
18239
18240 /* Determine which of two partial specializations of MAIN_TMPL is more
18241 specialized.
18242
18243 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18244 to the first partial specialization. The TREE_VALUE is the
18245 innermost set of template parameters for the partial
18246 specialization. PAT2 is similar, but for the second template.
18247
18248 Return 1 if the first partial specialization is more specialized;
18249 -1 if the second is more specialized; 0 if neither is more
18250 specialized.
18251
18252 See [temp.class.order] for information about determining which of
18253 two templates is more specialized. */
18254
18255 static int
18256 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18257 {
18258 tree targs;
18259 tree tmpl1, tmpl2;
18260 int winner = 0;
18261 bool any_deductions = false;
18262
18263 tmpl1 = TREE_TYPE (pat1);
18264 tmpl2 = TREE_TYPE (pat2);
18265
18266 /* Just like what happens for functions, if we are ordering between
18267 different class template specializations, we may encounter dependent
18268 types in the arguments, and we need our dependency check functions
18269 to behave correctly. */
18270 ++processing_template_decl;
18271 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18272 CLASSTYPE_TI_ARGS (tmpl1),
18273 CLASSTYPE_TI_ARGS (tmpl2));
18274 if (targs)
18275 {
18276 --winner;
18277 any_deductions = true;
18278 }
18279
18280 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18281 CLASSTYPE_TI_ARGS (tmpl2),
18282 CLASSTYPE_TI_ARGS (tmpl1));
18283 if (targs)
18284 {
18285 ++winner;
18286 any_deductions = true;
18287 }
18288 --processing_template_decl;
18289
18290 /* In the case of a tie where at least one of the class templates
18291 has a parameter pack at the end, the template with the most
18292 non-packed parameters wins. */
18293 if (winner == 0
18294 && any_deductions
18295 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18296 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18297 {
18298 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18299 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18300 int len1 = TREE_VEC_LENGTH (args1);
18301 int len2 = TREE_VEC_LENGTH (args2);
18302
18303 /* We don't count the pack expansion at the end. */
18304 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18305 --len1;
18306 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18307 --len2;
18308
18309 if (len1 > len2)
18310 return 1;
18311 else if (len1 < len2)
18312 return -1;
18313 }
18314
18315 return winner;
18316 }
18317
18318 /* Return the template arguments that will produce the function signature
18319 DECL from the function template FN, with the explicit template
18320 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18321 also match. Return NULL_TREE if no satisfactory arguments could be
18322 found. */
18323
18324 static tree
18325 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18326 {
18327 int ntparms = DECL_NTPARMS (fn);
18328 tree targs = make_tree_vec (ntparms);
18329 tree decl_type = TREE_TYPE (decl);
18330 tree decl_arg_types;
18331 tree *args;
18332 unsigned int nargs, ix;
18333 tree arg;
18334
18335 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18336
18337 /* Never do unification on the 'this' parameter. */
18338 decl_arg_types = skip_artificial_parms_for (decl,
18339 TYPE_ARG_TYPES (decl_type));
18340
18341 nargs = list_length (decl_arg_types);
18342 args = XALLOCAVEC (tree, nargs);
18343 for (arg = decl_arg_types, ix = 0;
18344 arg != NULL_TREE && arg != void_list_node;
18345 arg = TREE_CHAIN (arg), ++ix)
18346 args[ix] = TREE_VALUE (arg);
18347
18348 if (fn_type_unification (fn, explicit_args, targs,
18349 args, ix,
18350 (check_rettype || DECL_CONV_FN_P (fn)
18351 ? TREE_TYPE (decl_type) : NULL_TREE),
18352 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18353 /*decltype*/false)
18354 == error_mark_node)
18355 return NULL_TREE;
18356
18357 return targs;
18358 }
18359
18360 /* Return the innermost template arguments that, when applied to a partial
18361 specialization of MAIN_TMPL whose innermost template parameters are
18362 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18363 ARGS.
18364
18365 For example, suppose we have:
18366
18367 template <class T, class U> struct S {};
18368 template <class T> struct S<T*, int> {};
18369
18370 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18371 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18372 int}. The resulting vector will be {double}, indicating that `T'
18373 is bound to `double'. */
18374
18375 static tree
18376 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18377 {
18378 int i, ntparms = TREE_VEC_LENGTH (tparms);
18379 tree deduced_args;
18380 tree innermost_deduced_args;
18381
18382 innermost_deduced_args = make_tree_vec (ntparms);
18383 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18384 {
18385 deduced_args = copy_node (args);
18386 SET_TMPL_ARGS_LEVEL (deduced_args,
18387 TMPL_ARGS_DEPTH (deduced_args),
18388 innermost_deduced_args);
18389 }
18390 else
18391 deduced_args = innermost_deduced_args;
18392
18393 if (unify (tparms, deduced_args,
18394 INNERMOST_TEMPLATE_ARGS (spec_args),
18395 INNERMOST_TEMPLATE_ARGS (args),
18396 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18397 return NULL_TREE;
18398
18399 for (i = 0; i < ntparms; ++i)
18400 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18401 return NULL_TREE;
18402
18403 /* Verify that nondeduced template arguments agree with the type
18404 obtained from argument deduction.
18405
18406 For example:
18407
18408 struct A { typedef int X; };
18409 template <class T, class U> struct C {};
18410 template <class T> struct C<T, typename T::X> {};
18411
18412 Then with the instantiation `C<A, int>', we can deduce that
18413 `T' is `A' but unify () does not check whether `typename T::X'
18414 is `int'. */
18415 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18416 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18417 spec_args, main_tmpl,
18418 tf_none, false, false);
18419 if (spec_args == error_mark_node
18420 /* We only need to check the innermost arguments; the other
18421 arguments will always agree. */
18422 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18423 INNERMOST_TEMPLATE_ARGS (args)))
18424 return NULL_TREE;
18425
18426 /* Now that we have bindings for all of the template arguments,
18427 ensure that the arguments deduced for the template template
18428 parameters have compatible template parameter lists. See the use
18429 of template_template_parm_bindings_ok_p in fn_type_unification
18430 for more information. */
18431 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18432 return NULL_TREE;
18433
18434 return deduced_args;
18435 }
18436
18437 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18438 Return the TREE_LIST node with the most specialized template, if
18439 any. If there is no most specialized template, the error_mark_node
18440 is returned.
18441
18442 Note that this function does not look at, or modify, the
18443 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18444 returned is one of the elements of INSTANTIATIONS, callers may
18445 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18446 and retrieve it from the value returned. */
18447
18448 tree
18449 most_specialized_instantiation (tree templates)
18450 {
18451 tree fn, champ;
18452
18453 ++processing_template_decl;
18454
18455 champ = templates;
18456 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18457 {
18458 int fate = 0;
18459
18460 if (get_bindings (TREE_VALUE (champ),
18461 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18462 NULL_TREE, /*check_ret=*/true))
18463 fate--;
18464
18465 if (get_bindings (TREE_VALUE (fn),
18466 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18467 NULL_TREE, /*check_ret=*/true))
18468 fate++;
18469
18470 if (fate == -1)
18471 champ = fn;
18472 else if (!fate)
18473 {
18474 /* Equally specialized, move to next function. If there
18475 is no next function, nothing's most specialized. */
18476 fn = TREE_CHAIN (fn);
18477 champ = fn;
18478 if (!fn)
18479 break;
18480 }
18481 }
18482
18483 if (champ)
18484 /* Now verify that champ is better than everything earlier in the
18485 instantiation list. */
18486 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18487 if (get_bindings (TREE_VALUE (champ),
18488 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18489 NULL_TREE, /*check_ret=*/true)
18490 || !get_bindings (TREE_VALUE (fn),
18491 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18492 NULL_TREE, /*check_ret=*/true))
18493 {
18494 champ = NULL_TREE;
18495 break;
18496 }
18497
18498 processing_template_decl--;
18499
18500 if (!champ)
18501 return error_mark_node;
18502
18503 return champ;
18504 }
18505
18506 /* If DECL is a specialization of some template, return the most
18507 general such template. Otherwise, returns NULL_TREE.
18508
18509 For example, given:
18510
18511 template <class T> struct S { template <class U> void f(U); };
18512
18513 if TMPL is `template <class U> void S<int>::f(U)' this will return
18514 the full template. This function will not trace past partial
18515 specializations, however. For example, given in addition:
18516
18517 template <class T> struct S<T*> { template <class U> void f(U); };
18518
18519 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18520 `template <class T> template <class U> S<T*>::f(U)'. */
18521
18522 tree
18523 most_general_template (tree decl)
18524 {
18525 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18526 an immediate specialization. */
18527 if (TREE_CODE (decl) == FUNCTION_DECL)
18528 {
18529 if (DECL_TEMPLATE_INFO (decl)) {
18530 decl = DECL_TI_TEMPLATE (decl);
18531
18532 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18533 template friend. */
18534 if (TREE_CODE (decl) != TEMPLATE_DECL)
18535 return NULL_TREE;
18536 } else
18537 return NULL_TREE;
18538 }
18539
18540 /* Look for more and more general templates. */
18541 while (DECL_TEMPLATE_INFO (decl))
18542 {
18543 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18544 (See cp-tree.h for details.) */
18545 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18546 break;
18547
18548 if (CLASS_TYPE_P (TREE_TYPE (decl))
18549 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18550 break;
18551
18552 /* Stop if we run into an explicitly specialized class template. */
18553 if (!DECL_NAMESPACE_SCOPE_P (decl)
18554 && DECL_CONTEXT (decl)
18555 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18556 break;
18557
18558 decl = DECL_TI_TEMPLATE (decl);
18559 }
18560
18561 return decl;
18562 }
18563
18564 /* Return the most specialized of the class template partial
18565 specializations of TMPL which can produce TYPE, a specialization of
18566 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18567 a _TYPE node corresponding to the partial specialization, while the
18568 TREE_PURPOSE is the set of template arguments that must be
18569 substituted into the TREE_TYPE in order to generate TYPE.
18570
18571 If the choice of partial specialization is ambiguous, a diagnostic
18572 is issued, and the error_mark_node is returned. If there are no
18573 partial specializations of TMPL matching TYPE, then NULL_TREE is
18574 returned. */
18575
18576 static tree
18577 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18578 {
18579 tree list = NULL_TREE;
18580 tree t;
18581 tree champ;
18582 int fate;
18583 bool ambiguous_p;
18584 tree args;
18585 tree outer_args = NULL_TREE;
18586
18587 tmpl = most_general_template (tmpl);
18588 args = CLASSTYPE_TI_ARGS (type);
18589
18590 /* For determining which partial specialization to use, only the
18591 innermost args are interesting. */
18592 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18593 {
18594 outer_args = strip_innermost_template_args (args, 1);
18595 args = INNERMOST_TEMPLATE_ARGS (args);
18596 }
18597
18598 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18599 {
18600 tree partial_spec_args;
18601 tree spec_args;
18602 tree spec_tmpl = TREE_VALUE (t);
18603 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18604
18605 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18606
18607 ++processing_template_decl;
18608
18609 if (outer_args)
18610 {
18611 /* Discard the outer levels of args, and then substitute in the
18612 template args from the enclosing class. */
18613 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18614 partial_spec_args = tsubst_template_args
18615 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18616
18617 /* And the same for the partial specialization TEMPLATE_DECL. */
18618 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18619 }
18620
18621 partial_spec_args =
18622 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18623 add_to_template_args (outer_args,
18624 partial_spec_args),
18625 tmpl, tf_none,
18626 /*require_all_args=*/true,
18627 /*use_default_args=*/true);
18628
18629 --processing_template_decl;
18630
18631 if (partial_spec_args == error_mark_node)
18632 return error_mark_node;
18633 if (spec_tmpl == error_mark_node)
18634 return error_mark_node;
18635
18636 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18637 spec_args = get_class_bindings (tmpl, parms,
18638 partial_spec_args,
18639 args);
18640 if (spec_args)
18641 {
18642 if (outer_args)
18643 spec_args = add_to_template_args (outer_args, spec_args);
18644 list = tree_cons (spec_args, orig_parms, list);
18645 TREE_TYPE (list) = TREE_TYPE (t);
18646 }
18647 }
18648
18649 if (! list)
18650 return NULL_TREE;
18651
18652 ambiguous_p = false;
18653 t = list;
18654 champ = t;
18655 t = TREE_CHAIN (t);
18656 for (; t; t = TREE_CHAIN (t))
18657 {
18658 fate = more_specialized_class (tmpl, champ, t);
18659 if (fate == 1)
18660 ;
18661 else
18662 {
18663 if (fate == 0)
18664 {
18665 t = TREE_CHAIN (t);
18666 if (! t)
18667 {
18668 ambiguous_p = true;
18669 break;
18670 }
18671 }
18672 champ = t;
18673 }
18674 }
18675
18676 if (!ambiguous_p)
18677 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18678 {
18679 fate = more_specialized_class (tmpl, champ, t);
18680 if (fate != 1)
18681 {
18682 ambiguous_p = true;
18683 break;
18684 }
18685 }
18686
18687 if (ambiguous_p)
18688 {
18689 const char *str;
18690 char *spaces = NULL;
18691 if (!(complain & tf_error))
18692 return error_mark_node;
18693 error ("ambiguous class template instantiation for %q#T", type);
18694 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18695 for (t = list; t; t = TREE_CHAIN (t))
18696 {
18697 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18698 spaces = spaces ? spaces : get_spaces (str);
18699 }
18700 free (spaces);
18701 return error_mark_node;
18702 }
18703
18704 return champ;
18705 }
18706
18707 /* Explicitly instantiate DECL. */
18708
18709 void
18710 do_decl_instantiation (tree decl, tree storage)
18711 {
18712 tree result = NULL_TREE;
18713 int extern_p = 0;
18714
18715 if (!decl || decl == error_mark_node)
18716 /* An error occurred, for which grokdeclarator has already issued
18717 an appropriate message. */
18718 return;
18719 else if (! DECL_LANG_SPECIFIC (decl))
18720 {
18721 error ("explicit instantiation of non-template %q#D", decl);
18722 return;
18723 }
18724 else if (VAR_P (decl))
18725 {
18726 /* There is an asymmetry here in the way VAR_DECLs and
18727 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18728 the latter, the DECL we get back will be marked as a
18729 template instantiation, and the appropriate
18730 DECL_TEMPLATE_INFO will be set up. This does not happen for
18731 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18732 should handle VAR_DECLs as it currently handles
18733 FUNCTION_DECLs. */
18734 if (!DECL_CLASS_SCOPE_P (decl))
18735 {
18736 error ("%qD is not a static data member of a class template", decl);
18737 return;
18738 }
18739 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18740 if (!result || !VAR_P (result))
18741 {
18742 error ("no matching template for %qD found", decl);
18743 return;
18744 }
18745 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18746 {
18747 error ("type %qT for explicit instantiation %qD does not match "
18748 "declared type %qT", TREE_TYPE (result), decl,
18749 TREE_TYPE (decl));
18750 return;
18751 }
18752 }
18753 else if (TREE_CODE (decl) != FUNCTION_DECL)
18754 {
18755 error ("explicit instantiation of %q#D", decl);
18756 return;
18757 }
18758 else
18759 result = decl;
18760
18761 /* Check for various error cases. Note that if the explicit
18762 instantiation is valid the RESULT will currently be marked as an
18763 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18764 until we get here. */
18765
18766 if (DECL_TEMPLATE_SPECIALIZATION (result))
18767 {
18768 /* DR 259 [temp.spec].
18769
18770 Both an explicit instantiation and a declaration of an explicit
18771 specialization shall not appear in a program unless the explicit
18772 instantiation follows a declaration of the explicit specialization.
18773
18774 For a given set of template parameters, if an explicit
18775 instantiation of a template appears after a declaration of an
18776 explicit specialization for that template, the explicit
18777 instantiation has no effect. */
18778 return;
18779 }
18780 else if (DECL_EXPLICIT_INSTANTIATION (result))
18781 {
18782 /* [temp.spec]
18783
18784 No program shall explicitly instantiate any template more
18785 than once.
18786
18787 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18788 the first instantiation was `extern' and the second is not,
18789 and EXTERN_P for the opposite case. */
18790 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18791 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18792 /* If an "extern" explicit instantiation follows an ordinary
18793 explicit instantiation, the template is instantiated. */
18794 if (extern_p)
18795 return;
18796 }
18797 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18798 {
18799 error ("no matching template for %qD found", result);
18800 return;
18801 }
18802 else if (!DECL_TEMPLATE_INFO (result))
18803 {
18804 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18805 return;
18806 }
18807
18808 if (storage == NULL_TREE)
18809 ;
18810 else if (storage == ridpointers[(int) RID_EXTERN])
18811 {
18812 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
18813 pedwarn (input_location, OPT_Wpedantic,
18814 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18815 "instantiations");
18816 extern_p = 1;
18817 }
18818 else
18819 error ("storage class %qD applied to template instantiation", storage);
18820
18821 check_explicit_instantiation_namespace (result);
18822 mark_decl_instantiated (result, extern_p);
18823 if (! extern_p)
18824 instantiate_decl (result, /*defer_ok=*/1,
18825 /*expl_inst_class_mem_p=*/false);
18826 }
18827
18828 static void
18829 mark_class_instantiated (tree t, int extern_p)
18830 {
18831 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18832 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18833 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18834 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18835 if (! extern_p)
18836 {
18837 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18838 rest_of_type_compilation (t, 1);
18839 }
18840 }
18841
18842 /* Called from do_type_instantiation through binding_table_foreach to
18843 do recursive instantiation for the type bound in ENTRY. */
18844 static void
18845 bt_instantiate_type_proc (binding_entry entry, void *data)
18846 {
18847 tree storage = *(tree *) data;
18848
18849 if (MAYBE_CLASS_TYPE_P (entry->type)
18850 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18851 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18852 }
18853
18854 /* Called from do_type_instantiation to instantiate a member
18855 (a member function or a static member variable) of an
18856 explicitly instantiated class template. */
18857 static void
18858 instantiate_class_member (tree decl, int extern_p)
18859 {
18860 mark_decl_instantiated (decl, extern_p);
18861 if (! extern_p)
18862 instantiate_decl (decl, /*defer_ok=*/1,
18863 /*expl_inst_class_mem_p=*/true);
18864 }
18865
18866 /* Perform an explicit instantiation of template class T. STORAGE, if
18867 non-null, is the RID for extern, inline or static. COMPLAIN is
18868 nonzero if this is called from the parser, zero if called recursively,
18869 since the standard is unclear (as detailed below). */
18870
18871 void
18872 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18873 {
18874 int extern_p = 0;
18875 int nomem_p = 0;
18876 int static_p = 0;
18877 int previous_instantiation_extern_p = 0;
18878
18879 if (TREE_CODE (t) == TYPE_DECL)
18880 t = TREE_TYPE (t);
18881
18882 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18883 {
18884 tree tmpl =
18885 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18886 if (tmpl)
18887 error ("explicit instantiation of non-class template %qD", tmpl);
18888 else
18889 error ("explicit instantiation of non-template type %qT", t);
18890 return;
18891 }
18892
18893 complete_type (t);
18894
18895 if (!COMPLETE_TYPE_P (t))
18896 {
18897 if (complain & tf_error)
18898 error ("explicit instantiation of %q#T before definition of template",
18899 t);
18900 return;
18901 }
18902
18903 if (storage != NULL_TREE)
18904 {
18905 if (!in_system_header_at (input_location))
18906 {
18907 if (storage == ridpointers[(int) RID_EXTERN])
18908 {
18909 if (cxx_dialect == cxx98)
18910 pedwarn (input_location, OPT_Wpedantic,
18911 "ISO C++ 1998 forbids the use of %<extern%> on "
18912 "explicit instantiations");
18913 }
18914 else
18915 pedwarn (input_location, OPT_Wpedantic,
18916 "ISO C++ forbids the use of %qE"
18917 " on explicit instantiations", storage);
18918 }
18919
18920 if (storage == ridpointers[(int) RID_INLINE])
18921 nomem_p = 1;
18922 else if (storage == ridpointers[(int) RID_EXTERN])
18923 extern_p = 1;
18924 else if (storage == ridpointers[(int) RID_STATIC])
18925 static_p = 1;
18926 else
18927 {
18928 error ("storage class %qD applied to template instantiation",
18929 storage);
18930 extern_p = 0;
18931 }
18932 }
18933
18934 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18935 {
18936 /* DR 259 [temp.spec].
18937
18938 Both an explicit instantiation and a declaration of an explicit
18939 specialization shall not appear in a program unless the explicit
18940 instantiation follows a declaration of the explicit specialization.
18941
18942 For a given set of template parameters, if an explicit
18943 instantiation of a template appears after a declaration of an
18944 explicit specialization for that template, the explicit
18945 instantiation has no effect. */
18946 return;
18947 }
18948 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18949 {
18950 /* [temp.spec]
18951
18952 No program shall explicitly instantiate any template more
18953 than once.
18954
18955 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18956 instantiation was `extern'. If EXTERN_P then the second is.
18957 These cases are OK. */
18958 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18959
18960 if (!previous_instantiation_extern_p && !extern_p
18961 && (complain & tf_error))
18962 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18963
18964 /* If we've already instantiated the template, just return now. */
18965 if (!CLASSTYPE_INTERFACE_ONLY (t))
18966 return;
18967 }
18968
18969 check_explicit_instantiation_namespace (TYPE_NAME (t));
18970 mark_class_instantiated (t, extern_p);
18971
18972 if (nomem_p)
18973 return;
18974
18975 {
18976 tree tmp;
18977
18978 /* In contrast to implicit instantiation, where only the
18979 declarations, and not the definitions, of members are
18980 instantiated, we have here:
18981
18982 [temp.explicit]
18983
18984 The explicit instantiation of a class template specialization
18985 implies the instantiation of all of its members not
18986 previously explicitly specialized in the translation unit
18987 containing the explicit instantiation.
18988
18989 Of course, we can't instantiate member template classes, since
18990 we don't have any arguments for them. Note that the standard
18991 is unclear on whether the instantiation of the members are
18992 *explicit* instantiations or not. However, the most natural
18993 interpretation is that it should be an explicit instantiation. */
18994
18995 if (! static_p)
18996 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18997 if (TREE_CODE (tmp) == FUNCTION_DECL
18998 && DECL_TEMPLATE_INSTANTIATION (tmp))
18999 instantiate_class_member (tmp, extern_p);
19000
19001 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19002 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19003 instantiate_class_member (tmp, extern_p);
19004
19005 if (CLASSTYPE_NESTED_UTDS (t))
19006 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19007 bt_instantiate_type_proc, &storage);
19008 }
19009 }
19010
19011 /* Given a function DECL, which is a specialization of TMPL, modify
19012 DECL to be a re-instantiation of TMPL with the same template
19013 arguments. TMPL should be the template into which tsubst'ing
19014 should occur for DECL, not the most general template.
19015
19016 One reason for doing this is a scenario like this:
19017
19018 template <class T>
19019 void f(const T&, int i);
19020
19021 void g() { f(3, 7); }
19022
19023 template <class T>
19024 void f(const T& t, const int i) { }
19025
19026 Note that when the template is first instantiated, with
19027 instantiate_template, the resulting DECL will have no name for the
19028 first parameter, and the wrong type for the second. So, when we go
19029 to instantiate the DECL, we regenerate it. */
19030
19031 static void
19032 regenerate_decl_from_template (tree decl, tree tmpl)
19033 {
19034 /* The arguments used to instantiate DECL, from the most general
19035 template. */
19036 tree args;
19037 tree code_pattern;
19038
19039 args = DECL_TI_ARGS (decl);
19040 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19041
19042 /* Make sure that we can see identifiers, and compute access
19043 correctly. */
19044 push_access_scope (decl);
19045
19046 if (TREE_CODE (decl) == FUNCTION_DECL)
19047 {
19048 tree decl_parm;
19049 tree pattern_parm;
19050 tree specs;
19051 int args_depth;
19052 int parms_depth;
19053
19054 args_depth = TMPL_ARGS_DEPTH (args);
19055 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19056 if (args_depth > parms_depth)
19057 args = get_innermost_template_args (args, parms_depth);
19058
19059 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19060 args, tf_error, NULL_TREE,
19061 /*defer_ok*/false);
19062 if (specs && specs != error_mark_node)
19063 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19064 specs);
19065
19066 /* Merge parameter declarations. */
19067 decl_parm = skip_artificial_parms_for (decl,
19068 DECL_ARGUMENTS (decl));
19069 pattern_parm
19070 = skip_artificial_parms_for (code_pattern,
19071 DECL_ARGUMENTS (code_pattern));
19072 while (decl_parm && !DECL_PACK_P (pattern_parm))
19073 {
19074 tree parm_type;
19075 tree attributes;
19076
19077 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19078 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19079 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19080 NULL_TREE);
19081 parm_type = type_decays_to (parm_type);
19082 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19083 TREE_TYPE (decl_parm) = parm_type;
19084 attributes = DECL_ATTRIBUTES (pattern_parm);
19085 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19086 {
19087 DECL_ATTRIBUTES (decl_parm) = attributes;
19088 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19089 }
19090 decl_parm = DECL_CHAIN (decl_parm);
19091 pattern_parm = DECL_CHAIN (pattern_parm);
19092 }
19093 /* Merge any parameters that match with the function parameter
19094 pack. */
19095 if (pattern_parm && DECL_PACK_P (pattern_parm))
19096 {
19097 int i, len;
19098 tree expanded_types;
19099 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19100 the parameters in this function parameter pack. */
19101 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19102 args, tf_error, NULL_TREE);
19103 len = TREE_VEC_LENGTH (expanded_types);
19104 for (i = 0; i < len; i++)
19105 {
19106 tree parm_type;
19107 tree attributes;
19108
19109 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19110 /* Rename the parameter to include the index. */
19111 DECL_NAME (decl_parm) =
19112 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19113 parm_type = TREE_VEC_ELT (expanded_types, i);
19114 parm_type = type_decays_to (parm_type);
19115 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19116 TREE_TYPE (decl_parm) = parm_type;
19117 attributes = DECL_ATTRIBUTES (pattern_parm);
19118 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19119 {
19120 DECL_ATTRIBUTES (decl_parm) = attributes;
19121 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19122 }
19123 decl_parm = DECL_CHAIN (decl_parm);
19124 }
19125 }
19126 /* Merge additional specifiers from the CODE_PATTERN. */
19127 if (DECL_DECLARED_INLINE_P (code_pattern)
19128 && !DECL_DECLARED_INLINE_P (decl))
19129 DECL_DECLARED_INLINE_P (decl) = 1;
19130 }
19131 else if (VAR_P (decl))
19132 {
19133 DECL_INITIAL (decl) =
19134 tsubst_expr (DECL_INITIAL (code_pattern), args,
19135 tf_error, DECL_TI_TEMPLATE (decl),
19136 /*integral_constant_expression_p=*/false);
19137 if (VAR_HAD_UNKNOWN_BOUND (decl))
19138 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19139 tf_error, DECL_TI_TEMPLATE (decl));
19140 }
19141 else
19142 gcc_unreachable ();
19143
19144 pop_access_scope (decl);
19145 }
19146
19147 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19148 substituted to get DECL. */
19149
19150 tree
19151 template_for_substitution (tree decl)
19152 {
19153 tree tmpl = DECL_TI_TEMPLATE (decl);
19154
19155 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19156 for the instantiation. This is not always the most general
19157 template. Consider, for example:
19158
19159 template <class T>
19160 struct S { template <class U> void f();
19161 template <> void f<int>(); };
19162
19163 and an instantiation of S<double>::f<int>. We want TD to be the
19164 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19165 while (/* An instantiation cannot have a definition, so we need a
19166 more general template. */
19167 DECL_TEMPLATE_INSTANTIATION (tmpl)
19168 /* We must also deal with friend templates. Given:
19169
19170 template <class T> struct S {
19171 template <class U> friend void f() {};
19172 };
19173
19174 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19175 so far as the language is concerned, but that's still
19176 where we get the pattern for the instantiation from. On
19177 other hand, if the definition comes outside the class, say:
19178
19179 template <class T> struct S {
19180 template <class U> friend void f();
19181 };
19182 template <class U> friend void f() {}
19183
19184 we don't need to look any further. That's what the check for
19185 DECL_INITIAL is for. */
19186 || (TREE_CODE (decl) == FUNCTION_DECL
19187 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19188 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19189 {
19190 /* The present template, TD, should not be a definition. If it
19191 were a definition, we should be using it! Note that we
19192 cannot restructure the loop to just keep going until we find
19193 a template with a definition, since that might go too far if
19194 a specialization was declared, but not defined. */
19195 gcc_assert (!VAR_P (decl)
19196 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19197
19198 /* Fetch the more general template. */
19199 tmpl = DECL_TI_TEMPLATE (tmpl);
19200 }
19201
19202 return tmpl;
19203 }
19204
19205 /* Returns true if we need to instantiate this template instance even if we
19206 know we aren't going to emit it.. */
19207
19208 bool
19209 always_instantiate_p (tree decl)
19210 {
19211 /* We always instantiate inline functions so that we can inline them. An
19212 explicit instantiation declaration prohibits implicit instantiation of
19213 non-inline functions. With high levels of optimization, we would
19214 normally inline non-inline functions -- but we're not allowed to do
19215 that for "extern template" functions. Therefore, we check
19216 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19217 return ((TREE_CODE (decl) == FUNCTION_DECL
19218 && (DECL_DECLARED_INLINE_P (decl)
19219 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19220 /* And we need to instantiate static data members so that
19221 their initializers are available in integral constant
19222 expressions. */
19223 || (VAR_P (decl)
19224 && decl_maybe_constant_var_p (decl)));
19225 }
19226
19227 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19228 instantiate it now, modifying TREE_TYPE (fn). */
19229
19230 void
19231 maybe_instantiate_noexcept (tree fn)
19232 {
19233 tree fntype, spec, noex, clone;
19234
19235 if (DECL_CLONED_FUNCTION_P (fn))
19236 fn = DECL_CLONED_FUNCTION (fn);
19237 fntype = TREE_TYPE (fn);
19238 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19239
19240 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19241 return;
19242
19243 noex = TREE_PURPOSE (spec);
19244
19245 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19246 {
19247 if (push_tinst_level (fn))
19248 {
19249 push_access_scope (fn);
19250 push_deferring_access_checks (dk_no_deferred);
19251 input_location = DECL_SOURCE_LOCATION (fn);
19252 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19253 DEFERRED_NOEXCEPT_ARGS (noex),
19254 tf_warning_or_error, fn,
19255 /*function_p=*/false,
19256 /*integral_constant_expression_p=*/true);
19257 pop_deferring_access_checks ();
19258 pop_access_scope (fn);
19259 pop_tinst_level ();
19260 spec = build_noexcept_spec (noex, tf_warning_or_error);
19261 if (spec == error_mark_node)
19262 spec = noexcept_false_spec;
19263 }
19264 else
19265 spec = noexcept_false_spec;
19266 }
19267 else
19268 {
19269 /* This is an implicitly declared function, so NOEX is a list of
19270 other functions to evaluate and merge. */
19271 tree elt;
19272 spec = noexcept_true_spec;
19273 for (elt = noex; elt; elt = OVL_NEXT (elt))
19274 {
19275 tree fn = OVL_CURRENT (elt);
19276 tree subspec;
19277 maybe_instantiate_noexcept (fn);
19278 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19279 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19280 }
19281 }
19282
19283 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19284
19285 FOR_EACH_CLONE (clone, fn)
19286 {
19287 if (TREE_TYPE (clone) == fntype)
19288 TREE_TYPE (clone) = TREE_TYPE (fn);
19289 else
19290 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19291 }
19292 }
19293
19294 /* Produce the definition of D, a _DECL generated from a template. If
19295 DEFER_OK is nonzero, then we don't have to actually do the
19296 instantiation now; we just have to do it sometime. Normally it is
19297 an error if this is an explicit instantiation but D is undefined.
19298 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19299 explicitly instantiated class template. */
19300
19301 tree
19302 instantiate_decl (tree d, int defer_ok,
19303 bool expl_inst_class_mem_p)
19304 {
19305 tree tmpl = DECL_TI_TEMPLATE (d);
19306 tree gen_args;
19307 tree args;
19308 tree td;
19309 tree code_pattern;
19310 tree spec;
19311 tree gen_tmpl;
19312 bool pattern_defined;
19313 location_t saved_loc = input_location;
19314 int saved_unevaluated_operand = cp_unevaluated_operand;
19315 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19316 bool external_p;
19317 tree fn_context;
19318 bool nested;
19319
19320 /* This function should only be used to instantiate templates for
19321 functions and static member variables. */
19322 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19323
19324 /* Variables are never deferred; if instantiation is required, they
19325 are instantiated right away. That allows for better code in the
19326 case that an expression refers to the value of the variable --
19327 if the variable has a constant value the referring expression can
19328 take advantage of that fact. */
19329 if (VAR_P (d)
19330 || DECL_DECLARED_CONSTEXPR_P (d))
19331 defer_ok = 0;
19332
19333 /* Don't instantiate cloned functions. Instead, instantiate the
19334 functions they cloned. */
19335 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19336 d = DECL_CLONED_FUNCTION (d);
19337
19338 if (DECL_TEMPLATE_INSTANTIATED (d)
19339 || (TREE_CODE (d) == FUNCTION_DECL
19340 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19341 || DECL_TEMPLATE_SPECIALIZATION (d))
19342 /* D has already been instantiated or explicitly specialized, so
19343 there's nothing for us to do here.
19344
19345 It might seem reasonable to check whether or not D is an explicit
19346 instantiation, and, if so, stop here. But when an explicit
19347 instantiation is deferred until the end of the compilation,
19348 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19349 the instantiation. */
19350 return d;
19351
19352 /* Check to see whether we know that this template will be
19353 instantiated in some other file, as with "extern template"
19354 extension. */
19355 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19356
19357 /* In general, we do not instantiate such templates. */
19358 if (external_p && !always_instantiate_p (d))
19359 return d;
19360
19361 gen_tmpl = most_general_template (tmpl);
19362 gen_args = DECL_TI_ARGS (d);
19363
19364 if (tmpl != gen_tmpl)
19365 /* We should already have the extra args. */
19366 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19367 == TMPL_ARGS_DEPTH (gen_args));
19368 /* And what's in the hash table should match D. */
19369 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19370 || spec == NULL_TREE);
19371
19372 /* This needs to happen before any tsubsting. */
19373 if (! push_tinst_level (d))
19374 return d;
19375
19376 timevar_push (TV_TEMPLATE_INST);
19377
19378 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19379 for the instantiation. */
19380 td = template_for_substitution (d);
19381 code_pattern = DECL_TEMPLATE_RESULT (td);
19382
19383 /* We should never be trying to instantiate a member of a class
19384 template or partial specialization. */
19385 gcc_assert (d != code_pattern);
19386
19387 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19388 || DECL_TEMPLATE_SPECIALIZATION (td))
19389 /* In the case of a friend template whose definition is provided
19390 outside the class, we may have too many arguments. Drop the
19391 ones we don't need. The same is true for specializations. */
19392 args = get_innermost_template_args
19393 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19394 else
19395 args = gen_args;
19396
19397 if (TREE_CODE (d) == FUNCTION_DECL)
19398 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19399 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19400 else
19401 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19402
19403 /* We may be in the middle of deferred access check. Disable it now. */
19404 push_deferring_access_checks (dk_no_deferred);
19405
19406 /* Unless an explicit instantiation directive has already determined
19407 the linkage of D, remember that a definition is available for
19408 this entity. */
19409 if (pattern_defined
19410 && !DECL_INTERFACE_KNOWN (d)
19411 && !DECL_NOT_REALLY_EXTERN (d))
19412 mark_definable (d);
19413
19414 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19415 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19416 input_location = DECL_SOURCE_LOCATION (d);
19417
19418 /* If D is a member of an explicitly instantiated class template,
19419 and no definition is available, treat it like an implicit
19420 instantiation. */
19421 if (!pattern_defined && expl_inst_class_mem_p
19422 && DECL_EXPLICIT_INSTANTIATION (d))
19423 {
19424 /* Leave linkage flags alone on instantiations with anonymous
19425 visibility. */
19426 if (TREE_PUBLIC (d))
19427 {
19428 DECL_NOT_REALLY_EXTERN (d) = 0;
19429 DECL_INTERFACE_KNOWN (d) = 0;
19430 }
19431 SET_DECL_IMPLICIT_INSTANTIATION (d);
19432 }
19433
19434 if (TREE_CODE (d) == FUNCTION_DECL)
19435 maybe_instantiate_noexcept (d);
19436
19437 /* Defer all other templates, unless we have been explicitly
19438 forbidden from doing so. */
19439 if (/* If there is no definition, we cannot instantiate the
19440 template. */
19441 ! pattern_defined
19442 /* If it's OK to postpone instantiation, do so. */
19443 || defer_ok
19444 /* If this is a static data member that will be defined
19445 elsewhere, we don't want to instantiate the entire data
19446 member, but we do want to instantiate the initializer so that
19447 we can substitute that elsewhere. */
19448 || (external_p && VAR_P (d)))
19449 {
19450 /* The definition of the static data member is now required so
19451 we must substitute the initializer. */
19452 if (VAR_P (d)
19453 && !DECL_INITIAL (d)
19454 && DECL_INITIAL (code_pattern))
19455 {
19456 tree ns;
19457 tree init;
19458 bool const_init = false;
19459
19460 ns = decl_namespace_context (d);
19461 push_nested_namespace (ns);
19462 push_nested_class (DECL_CONTEXT (d));
19463 init = tsubst_expr (DECL_INITIAL (code_pattern),
19464 args,
19465 tf_warning_or_error, NULL_TREE,
19466 /*integral_constant_expression_p=*/false);
19467 /* Make sure the initializer is still constant, in case of
19468 circular dependency (template/instantiate6.C). */
19469 const_init
19470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19471 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19472 /*asmspec_tree=*/NULL_TREE,
19473 LOOKUP_ONLYCONVERTING);
19474 pop_nested_class ();
19475 pop_nested_namespace (ns);
19476 }
19477
19478 /* We restore the source position here because it's used by
19479 add_pending_template. */
19480 input_location = saved_loc;
19481
19482 if (at_eof && !pattern_defined
19483 && DECL_EXPLICIT_INSTANTIATION (d)
19484 && DECL_NOT_REALLY_EXTERN (d))
19485 /* [temp.explicit]
19486
19487 The definition of a non-exported function template, a
19488 non-exported member function template, or a non-exported
19489 member function or static data member of a class template
19490 shall be present in every translation unit in which it is
19491 explicitly instantiated. */
19492 permerror (input_location, "explicit instantiation of %qD "
19493 "but no definition available", d);
19494
19495 /* If we're in unevaluated context, we just wanted to get the
19496 constant value; this isn't an odr use, so don't queue
19497 a full instantiation. */
19498 if (cp_unevaluated_operand != 0)
19499 goto out;
19500 /* ??? Historically, we have instantiated inline functions, even
19501 when marked as "extern template". */
19502 if (!(external_p && VAR_P (d)))
19503 add_pending_template (d);
19504 goto out;
19505 }
19506 /* Tell the repository that D is available in this translation unit
19507 -- and see if it is supposed to be instantiated here. */
19508 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19509 {
19510 /* In a PCH file, despite the fact that the repository hasn't
19511 requested instantiation in the PCH it is still possible that
19512 an instantiation will be required in a file that includes the
19513 PCH. */
19514 if (pch_file)
19515 add_pending_template (d);
19516 /* Instantiate inline functions so that the inliner can do its
19517 job, even though we'll not be emitting a copy of this
19518 function. */
19519 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19520 goto out;
19521 }
19522
19523 fn_context = decl_function_context (d);
19524 nested = (current_function_decl != NULL_TREE);
19525 if (!fn_context)
19526 push_to_top_level ();
19527 else
19528 {
19529 if (nested)
19530 push_function_context ();
19531 cp_unevaluated_operand = 0;
19532 c_inhibit_evaluation_warnings = 0;
19533 }
19534
19535 /* Mark D as instantiated so that recursive calls to
19536 instantiate_decl do not try to instantiate it again. */
19537 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19538
19539 /* Regenerate the declaration in case the template has been modified
19540 by a subsequent redeclaration. */
19541 regenerate_decl_from_template (d, td);
19542
19543 /* We already set the file and line above. Reset them now in case
19544 they changed as a result of calling regenerate_decl_from_template. */
19545 input_location = DECL_SOURCE_LOCATION (d);
19546
19547 if (VAR_P (d))
19548 {
19549 tree init;
19550 bool const_init = false;
19551
19552 /* Clear out DECL_RTL; whatever was there before may not be right
19553 since we've reset the type of the declaration. */
19554 SET_DECL_RTL (d, NULL);
19555 DECL_IN_AGGR_P (d) = 0;
19556
19557 /* The initializer is placed in DECL_INITIAL by
19558 regenerate_decl_from_template so we don't need to
19559 push/pop_access_scope again here. Pull it out so that
19560 cp_finish_decl can process it. */
19561 init = DECL_INITIAL (d);
19562 DECL_INITIAL (d) = NULL_TREE;
19563 DECL_INITIALIZED_P (d) = 0;
19564
19565 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19566 initializer. That function will defer actual emission until
19567 we have a chance to determine linkage. */
19568 DECL_EXTERNAL (d) = 0;
19569
19570 /* Enter the scope of D so that access-checking works correctly. */
19571 push_nested_class (DECL_CONTEXT (d));
19572 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19573 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19574 pop_nested_class ();
19575 }
19576 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19577 synthesize_method (d);
19578 else if (TREE_CODE (d) == FUNCTION_DECL)
19579 {
19580 struct pointer_map_t *saved_local_specializations;
19581 tree subst_decl;
19582 tree tmpl_parm;
19583 tree spec_parm;
19584 tree block = NULL_TREE;
19585
19586 /* Save away the current list, in case we are instantiating one
19587 template from within the body of another. */
19588 saved_local_specializations = local_specializations;
19589
19590 /* Set up the list of local specializations. */
19591 local_specializations = pointer_map_create ();
19592
19593 /* Set up context. */
19594 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19595 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19596 block = push_stmt_list ();
19597 else
19598 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19599
19600 /* Some typedefs referenced from within the template code need to be
19601 access checked at template instantiation time, i.e now. These
19602 types were added to the template at parsing time. Let's get those
19603 and perform the access checks then. */
19604 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19605 gen_args);
19606
19607 /* Create substitution entries for the parameters. */
19608 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19609 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19610 spec_parm = DECL_ARGUMENTS (d);
19611 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19612 {
19613 register_local_specialization (spec_parm, tmpl_parm);
19614 spec_parm = skip_artificial_parms_for (d, spec_parm);
19615 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19616 }
19617 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19618 {
19619 if (!DECL_PACK_P (tmpl_parm))
19620 {
19621 register_local_specialization (spec_parm, tmpl_parm);
19622 spec_parm = DECL_CHAIN (spec_parm);
19623 }
19624 else
19625 {
19626 /* Register the (value) argument pack as a specialization of
19627 TMPL_PARM, then move on. */
19628 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19629 register_local_specialization (argpack, tmpl_parm);
19630 }
19631 }
19632 gcc_assert (!spec_parm);
19633
19634 /* Substitute into the body of the function. */
19635 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19636 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19637 tf_warning_or_error, tmpl);
19638 else
19639 {
19640 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19641 tf_warning_or_error, tmpl,
19642 /*integral_constant_expression_p=*/false);
19643
19644 /* Set the current input_location to the end of the function
19645 so that finish_function knows where we are. */
19646 input_location
19647 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19648 }
19649
19650 /* We don't need the local specializations any more. */
19651 pointer_map_destroy (local_specializations);
19652 local_specializations = saved_local_specializations;
19653
19654 /* Finish the function. */
19655 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19656 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19657 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19658 else
19659 {
19660 d = finish_function (0);
19661 expand_or_defer_fn (d);
19662 }
19663
19664 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19665 cp_check_omp_declare_reduction (d);
19666 }
19667
19668 /* We're not deferring instantiation any more. */
19669 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19670
19671 if (!fn_context)
19672 pop_from_top_level ();
19673 else if (nested)
19674 pop_function_context ();
19675
19676 out:
19677 input_location = saved_loc;
19678 cp_unevaluated_operand = saved_unevaluated_operand;
19679 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19680 pop_deferring_access_checks ();
19681 pop_tinst_level ();
19682
19683 timevar_pop (TV_TEMPLATE_INST);
19684
19685 return d;
19686 }
19687
19688 /* Run through the list of templates that we wish we could
19689 instantiate, and instantiate any we can. RETRIES is the
19690 number of times we retry pending template instantiation. */
19691
19692 void
19693 instantiate_pending_templates (int retries)
19694 {
19695 int reconsider;
19696 location_t saved_loc = input_location;
19697
19698 /* Instantiating templates may trigger vtable generation. This in turn
19699 may require further template instantiations. We place a limit here
19700 to avoid infinite loop. */
19701 if (pending_templates && retries >= max_tinst_depth)
19702 {
19703 tree decl = pending_templates->tinst->decl;
19704
19705 error ("template instantiation depth exceeds maximum of %d"
19706 " instantiating %q+D, possibly from virtual table generation"
19707 " (use -ftemplate-depth= to increase the maximum)",
19708 max_tinst_depth, decl);
19709 if (TREE_CODE (decl) == FUNCTION_DECL)
19710 /* Pretend that we defined it. */
19711 DECL_INITIAL (decl) = error_mark_node;
19712 return;
19713 }
19714
19715 do
19716 {
19717 struct pending_template **t = &pending_templates;
19718 struct pending_template *last = NULL;
19719 reconsider = 0;
19720 while (*t)
19721 {
19722 tree instantiation = reopen_tinst_level ((*t)->tinst);
19723 bool complete = false;
19724
19725 if (TYPE_P (instantiation))
19726 {
19727 tree fn;
19728
19729 if (!COMPLETE_TYPE_P (instantiation))
19730 {
19731 instantiate_class_template (instantiation);
19732 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19733 for (fn = TYPE_METHODS (instantiation);
19734 fn;
19735 fn = TREE_CHAIN (fn))
19736 if (! DECL_ARTIFICIAL (fn))
19737 instantiate_decl (fn,
19738 /*defer_ok=*/0,
19739 /*expl_inst_class_mem_p=*/false);
19740 if (COMPLETE_TYPE_P (instantiation))
19741 reconsider = 1;
19742 }
19743
19744 complete = COMPLETE_TYPE_P (instantiation);
19745 }
19746 else
19747 {
19748 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19749 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19750 {
19751 instantiation
19752 = instantiate_decl (instantiation,
19753 /*defer_ok=*/0,
19754 /*expl_inst_class_mem_p=*/false);
19755 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19756 reconsider = 1;
19757 }
19758
19759 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19760 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19761 }
19762
19763 if (complete)
19764 /* If INSTANTIATION has been instantiated, then we don't
19765 need to consider it again in the future. */
19766 *t = (*t)->next;
19767 else
19768 {
19769 last = *t;
19770 t = &(*t)->next;
19771 }
19772 tinst_depth = 0;
19773 current_tinst_level = NULL;
19774 }
19775 last_pending_template = last;
19776 }
19777 while (reconsider);
19778
19779 input_location = saved_loc;
19780 }
19781
19782 /* Substitute ARGVEC into T, which is a list of initializers for
19783 either base class or a non-static data member. The TREE_PURPOSEs
19784 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19785 instantiate_decl. */
19786
19787 static tree
19788 tsubst_initializer_list (tree t, tree argvec)
19789 {
19790 tree inits = NULL_TREE;
19791
19792 for (; t; t = TREE_CHAIN (t))
19793 {
19794 tree decl;
19795 tree init;
19796 tree expanded_bases = NULL_TREE;
19797 tree expanded_arguments = NULL_TREE;
19798 int i, len = 1;
19799
19800 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19801 {
19802 tree expr;
19803 tree arg;
19804
19805 /* Expand the base class expansion type into separate base
19806 classes. */
19807 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19808 tf_warning_or_error,
19809 NULL_TREE);
19810 if (expanded_bases == error_mark_node)
19811 continue;
19812
19813 /* We'll be building separate TREE_LISTs of arguments for
19814 each base. */
19815 len = TREE_VEC_LENGTH (expanded_bases);
19816 expanded_arguments = make_tree_vec (len);
19817 for (i = 0; i < len; i++)
19818 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19819
19820 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19821 expand each argument in the TREE_VALUE of t. */
19822 expr = make_node (EXPR_PACK_EXPANSION);
19823 PACK_EXPANSION_LOCAL_P (expr) = true;
19824 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19825 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19826
19827 if (TREE_VALUE (t) == void_type_node)
19828 /* VOID_TYPE_NODE is used to indicate
19829 value-initialization. */
19830 {
19831 for (i = 0; i < len; i++)
19832 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19833 }
19834 else
19835 {
19836 /* Substitute parameter packs into each argument in the
19837 TREE_LIST. */
19838 in_base_initializer = 1;
19839 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19840 {
19841 tree expanded_exprs;
19842
19843 /* Expand the argument. */
19844 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19845 expanded_exprs
19846 = tsubst_pack_expansion (expr, argvec,
19847 tf_warning_or_error,
19848 NULL_TREE);
19849 if (expanded_exprs == error_mark_node)
19850 continue;
19851
19852 /* Prepend each of the expanded expressions to the
19853 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19854 for (i = 0; i < len; i++)
19855 {
19856 TREE_VEC_ELT (expanded_arguments, i) =
19857 tree_cons (NULL_TREE,
19858 TREE_VEC_ELT (expanded_exprs, i),
19859 TREE_VEC_ELT (expanded_arguments, i));
19860 }
19861 }
19862 in_base_initializer = 0;
19863
19864 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19865 since we built them backwards. */
19866 for (i = 0; i < len; i++)
19867 {
19868 TREE_VEC_ELT (expanded_arguments, i) =
19869 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19870 }
19871 }
19872 }
19873
19874 for (i = 0; i < len; ++i)
19875 {
19876 if (expanded_bases)
19877 {
19878 decl = TREE_VEC_ELT (expanded_bases, i);
19879 decl = expand_member_init (decl);
19880 init = TREE_VEC_ELT (expanded_arguments, i);
19881 }
19882 else
19883 {
19884 tree tmp;
19885 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19886 tf_warning_or_error, NULL_TREE);
19887
19888 decl = expand_member_init (decl);
19889 if (decl && !DECL_P (decl))
19890 in_base_initializer = 1;
19891
19892 init = TREE_VALUE (t);
19893 tmp = init;
19894 if (init != void_type_node)
19895 init = tsubst_expr (init, argvec,
19896 tf_warning_or_error, NULL_TREE,
19897 /*integral_constant_expression_p=*/false);
19898 if (init == NULL_TREE && tmp != NULL_TREE)
19899 /* If we had an initializer but it instantiated to nothing,
19900 value-initialize the object. This will only occur when
19901 the initializer was a pack expansion where the parameter
19902 packs used in that expansion were of length zero. */
19903 init = void_type_node;
19904 in_base_initializer = 0;
19905 }
19906
19907 if (decl)
19908 {
19909 init = build_tree_list (decl, init);
19910 TREE_CHAIN (init) = inits;
19911 inits = init;
19912 }
19913 }
19914 }
19915 return inits;
19916 }
19917
19918 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19919
19920 static void
19921 set_current_access_from_decl (tree decl)
19922 {
19923 if (TREE_PRIVATE (decl))
19924 current_access_specifier = access_private_node;
19925 else if (TREE_PROTECTED (decl))
19926 current_access_specifier = access_protected_node;
19927 else
19928 current_access_specifier = access_public_node;
19929 }
19930
19931 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19932 is the instantiation (which should have been created with
19933 start_enum) and ARGS are the template arguments to use. */
19934
19935 static void
19936 tsubst_enum (tree tag, tree newtag, tree args)
19937 {
19938 tree e;
19939
19940 if (SCOPED_ENUM_P (newtag))
19941 begin_scope (sk_scoped_enum, newtag);
19942
19943 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19944 {
19945 tree value;
19946 tree decl;
19947
19948 decl = TREE_VALUE (e);
19949 /* Note that in a template enum, the TREE_VALUE is the
19950 CONST_DECL, not the corresponding INTEGER_CST. */
19951 value = tsubst_expr (DECL_INITIAL (decl),
19952 args, tf_warning_or_error, NULL_TREE,
19953 /*integral_constant_expression_p=*/true);
19954
19955 /* Give this enumeration constant the correct access. */
19956 set_current_access_from_decl (decl);
19957
19958 /* Actually build the enumerator itself. */
19959 build_enumerator
19960 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19961 }
19962
19963 if (SCOPED_ENUM_P (newtag))
19964 finish_scope ();
19965
19966 finish_enum_value_list (newtag);
19967 finish_enum (newtag);
19968
19969 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19970 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19971 }
19972
19973 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19974 its type -- but without substituting the innermost set of template
19975 arguments. So, innermost set of template parameters will appear in
19976 the type. */
19977
19978 tree
19979 get_mostly_instantiated_function_type (tree decl)
19980 {
19981 tree fn_type;
19982 tree tmpl;
19983 tree targs;
19984 tree tparms;
19985 int parm_depth;
19986
19987 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19988 targs = DECL_TI_ARGS (decl);
19989 tparms = DECL_TEMPLATE_PARMS (tmpl);
19990 parm_depth = TMPL_PARMS_DEPTH (tparms);
19991
19992 /* There should be as many levels of arguments as there are levels
19993 of parameters. */
19994 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19995
19996 fn_type = TREE_TYPE (tmpl);
19997
19998 if (parm_depth == 1)
19999 /* No substitution is necessary. */
20000 ;
20001 else
20002 {
20003 int i;
20004 tree partial_args;
20005
20006 /* Replace the innermost level of the TARGS with NULL_TREEs to
20007 let tsubst know not to substitute for those parameters. */
20008 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20009 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20010 SET_TMPL_ARGS_LEVEL (partial_args, i,
20011 TMPL_ARGS_LEVEL (targs, i));
20012 SET_TMPL_ARGS_LEVEL (partial_args,
20013 TMPL_ARGS_DEPTH (targs),
20014 make_tree_vec (DECL_NTPARMS (tmpl)));
20015
20016 /* Make sure that we can see identifiers, and compute access
20017 correctly. */
20018 push_access_scope (decl);
20019
20020 ++processing_template_decl;
20021 /* Now, do the (partial) substitution to figure out the
20022 appropriate function type. */
20023 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20024 --processing_template_decl;
20025
20026 /* Substitute into the template parameters to obtain the real
20027 innermost set of parameters. This step is important if the
20028 innermost set of template parameters contains value
20029 parameters whose types depend on outer template parameters. */
20030 TREE_VEC_LENGTH (partial_args)--;
20031 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20032
20033 pop_access_scope (decl);
20034 }
20035
20036 return fn_type;
20037 }
20038
20039 /* Return truthvalue if we're processing a template different from
20040 the last one involved in diagnostics. */
20041 int
20042 problematic_instantiation_changed (void)
20043 {
20044 return current_tinst_level != last_error_tinst_level;
20045 }
20046
20047 /* Remember current template involved in diagnostics. */
20048 void
20049 record_last_problematic_instantiation (void)
20050 {
20051 last_error_tinst_level = current_tinst_level;
20052 }
20053
20054 struct tinst_level *
20055 current_instantiation (void)
20056 {
20057 return current_tinst_level;
20058 }
20059
20060 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20061 type. Return zero for ok, nonzero for disallowed. Issue error and
20062 warning messages under control of COMPLAIN. */
20063
20064 static int
20065 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20066 {
20067 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20068 return 0;
20069 else if (POINTER_TYPE_P (type))
20070 return 0;
20071 else if (TYPE_PTRMEM_P (type))
20072 return 0;
20073 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20074 return 0;
20075 else if (TREE_CODE (type) == TYPENAME_TYPE)
20076 return 0;
20077 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20078 return 0;
20079 else if (TREE_CODE (type) == NULLPTR_TYPE)
20080 return 0;
20081
20082 if (complain & tf_error)
20083 {
20084 if (type == error_mark_node)
20085 inform (input_location, "invalid template non-type parameter");
20086 else
20087 error ("%q#T is not a valid type for a template non-type parameter",
20088 type);
20089 }
20090 return 1;
20091 }
20092
20093 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20094 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20095
20096 static bool
20097 dependent_type_p_r (tree type)
20098 {
20099 tree scope;
20100
20101 /* [temp.dep.type]
20102
20103 A type is dependent if it is:
20104
20105 -- a template parameter. Template template parameters are types
20106 for us (since TYPE_P holds true for them) so we handle
20107 them here. */
20108 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20109 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20110 return true;
20111 /* -- a qualified-id with a nested-name-specifier which contains a
20112 class-name that names a dependent type or whose unqualified-id
20113 names a dependent type. */
20114 if (TREE_CODE (type) == TYPENAME_TYPE)
20115 return true;
20116 /* -- a cv-qualified type where the cv-unqualified type is
20117 dependent. */
20118 type = TYPE_MAIN_VARIANT (type);
20119 /* -- a compound type constructed from any dependent type. */
20120 if (TYPE_PTRMEM_P (type))
20121 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20122 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20123 (type)));
20124 else if (TYPE_PTR_P (type)
20125 || TREE_CODE (type) == REFERENCE_TYPE)
20126 return dependent_type_p (TREE_TYPE (type));
20127 else if (TREE_CODE (type) == FUNCTION_TYPE
20128 || TREE_CODE (type) == METHOD_TYPE)
20129 {
20130 tree arg_type;
20131
20132 if (dependent_type_p (TREE_TYPE (type)))
20133 return true;
20134 for (arg_type = TYPE_ARG_TYPES (type);
20135 arg_type;
20136 arg_type = TREE_CHAIN (arg_type))
20137 if (dependent_type_p (TREE_VALUE (arg_type)))
20138 return true;
20139 return false;
20140 }
20141 /* -- an array type constructed from any dependent type or whose
20142 size is specified by a constant expression that is
20143 value-dependent.
20144
20145 We checked for type- and value-dependence of the bounds in
20146 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20147 if (TREE_CODE (type) == ARRAY_TYPE)
20148 {
20149 if (TYPE_DOMAIN (type)
20150 && dependent_type_p (TYPE_DOMAIN (type)))
20151 return true;
20152 return dependent_type_p (TREE_TYPE (type));
20153 }
20154
20155 /* -- a template-id in which either the template name is a template
20156 parameter ... */
20157 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20158 return true;
20159 /* ... or any of the template arguments is a dependent type or
20160 an expression that is type-dependent or value-dependent. */
20161 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20162 && (any_dependent_template_arguments_p
20163 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20164 return true;
20165
20166 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20167 dependent; if the argument of the `typeof' expression is not
20168 type-dependent, then it should already been have resolved. */
20169 if (TREE_CODE (type) == TYPEOF_TYPE
20170 || TREE_CODE (type) == DECLTYPE_TYPE
20171 || TREE_CODE (type) == UNDERLYING_TYPE)
20172 return true;
20173
20174 /* A template argument pack is dependent if any of its packed
20175 arguments are. */
20176 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20177 {
20178 tree args = ARGUMENT_PACK_ARGS (type);
20179 int i, len = TREE_VEC_LENGTH (args);
20180 for (i = 0; i < len; ++i)
20181 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20182 return true;
20183 }
20184
20185 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20186 be template parameters. */
20187 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20188 return true;
20189
20190 /* The standard does not specifically mention types that are local
20191 to template functions or local classes, but they should be
20192 considered dependent too. For example:
20193
20194 template <int I> void f() {
20195 enum E { a = I };
20196 S<sizeof (E)> s;
20197 }
20198
20199 The size of `E' cannot be known until the value of `I' has been
20200 determined. Therefore, `E' must be considered dependent. */
20201 scope = TYPE_CONTEXT (type);
20202 if (scope && TYPE_P (scope))
20203 return dependent_type_p (scope);
20204 /* Don't use type_dependent_expression_p here, as it can lead
20205 to infinite recursion trying to determine whether a lambda
20206 nested in a lambda is dependent (c++/47687). */
20207 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20208 && DECL_LANG_SPECIFIC (scope)
20209 && DECL_TEMPLATE_INFO (scope)
20210 && (any_dependent_template_arguments_p
20211 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20212 return true;
20213
20214 /* Other types are non-dependent. */
20215 return false;
20216 }
20217
20218 /* Returns TRUE if TYPE is dependent, in the sense of
20219 [temp.dep.type]. Note that a NULL type is considered dependent. */
20220
20221 bool
20222 dependent_type_p (tree type)
20223 {
20224 /* If there are no template parameters in scope, then there can't be
20225 any dependent types. */
20226 if (!processing_template_decl)
20227 {
20228 /* If we are not processing a template, then nobody should be
20229 providing us with a dependent type. */
20230 gcc_assert (type);
20231 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20232 return false;
20233 }
20234
20235 /* If the type is NULL, we have not computed a type for the entity
20236 in question; in that case, the type is dependent. */
20237 if (!type)
20238 return true;
20239
20240 /* Erroneous types can be considered non-dependent. */
20241 if (type == error_mark_node)
20242 return false;
20243
20244 /* If we have not already computed the appropriate value for TYPE,
20245 do so now. */
20246 if (!TYPE_DEPENDENT_P_VALID (type))
20247 {
20248 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20249 TYPE_DEPENDENT_P_VALID (type) = 1;
20250 }
20251
20252 return TYPE_DEPENDENT_P (type);
20253 }
20254
20255 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20256 lookup. In other words, a dependent type that is not the current
20257 instantiation. */
20258
20259 bool
20260 dependent_scope_p (tree scope)
20261 {
20262 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20263 && !currently_open_class (scope));
20264 }
20265
20266 /* T is a SCOPE_REF; return whether we need to consider it
20267 instantiation-dependent so that we can check access at instantiation
20268 time even though we know which member it resolves to. */
20269
20270 static bool
20271 instantiation_dependent_scope_ref_p (tree t)
20272 {
20273 if (DECL_P (TREE_OPERAND (t, 1))
20274 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20275 && accessible_in_template_p (TREE_OPERAND (t, 0),
20276 TREE_OPERAND (t, 1)))
20277 return false;
20278 else
20279 return true;
20280 }
20281
20282 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20283 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20284 expression. */
20285
20286 /* Note that this predicate is not appropriate for general expressions;
20287 only constant expressions (that satisfy potential_constant_expression)
20288 can be tested for value dependence. */
20289
20290 bool
20291 value_dependent_expression_p (tree expression)
20292 {
20293 if (!processing_template_decl)
20294 return false;
20295
20296 /* A name declared with a dependent type. */
20297 if (DECL_P (expression) && type_dependent_expression_p (expression))
20298 return true;
20299
20300 switch (TREE_CODE (expression))
20301 {
20302 case IDENTIFIER_NODE:
20303 /* A name that has not been looked up -- must be dependent. */
20304 return true;
20305
20306 case TEMPLATE_PARM_INDEX:
20307 /* A non-type template parm. */
20308 return true;
20309
20310 case CONST_DECL:
20311 /* A non-type template parm. */
20312 if (DECL_TEMPLATE_PARM_P (expression))
20313 return true;
20314 return value_dependent_expression_p (DECL_INITIAL (expression));
20315
20316 case VAR_DECL:
20317 /* A constant with literal type and is initialized
20318 with an expression that is value-dependent.
20319
20320 Note that a non-dependent parenthesized initializer will have
20321 already been replaced with its constant value, so if we see
20322 a TREE_LIST it must be dependent. */
20323 if (DECL_INITIAL (expression)
20324 && decl_constant_var_p (expression)
20325 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20326 || value_dependent_expression_p (DECL_INITIAL (expression))))
20327 return true;
20328 return false;
20329
20330 case DYNAMIC_CAST_EXPR:
20331 case STATIC_CAST_EXPR:
20332 case CONST_CAST_EXPR:
20333 case REINTERPRET_CAST_EXPR:
20334 case CAST_EXPR:
20335 /* These expressions are value-dependent if the type to which
20336 the cast occurs is dependent or the expression being casted
20337 is value-dependent. */
20338 {
20339 tree type = TREE_TYPE (expression);
20340
20341 if (dependent_type_p (type))
20342 return true;
20343
20344 /* A functional cast has a list of operands. */
20345 expression = TREE_OPERAND (expression, 0);
20346 if (!expression)
20347 {
20348 /* If there are no operands, it must be an expression such
20349 as "int()". This should not happen for aggregate types
20350 because it would form non-constant expressions. */
20351 gcc_assert (cxx_dialect >= cxx11
20352 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20353
20354 return false;
20355 }
20356
20357 if (TREE_CODE (expression) == TREE_LIST)
20358 return any_value_dependent_elements_p (expression);
20359
20360 return value_dependent_expression_p (expression);
20361 }
20362
20363 case SIZEOF_EXPR:
20364 if (SIZEOF_EXPR_TYPE_P (expression))
20365 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20366 /* FALLTHRU */
20367 case ALIGNOF_EXPR:
20368 case TYPEID_EXPR:
20369 /* A `sizeof' expression is value-dependent if the operand is
20370 type-dependent or is a pack expansion. */
20371 expression = TREE_OPERAND (expression, 0);
20372 if (PACK_EXPANSION_P (expression))
20373 return true;
20374 else if (TYPE_P (expression))
20375 return dependent_type_p (expression);
20376 return instantiation_dependent_expression_p (expression);
20377
20378 case AT_ENCODE_EXPR:
20379 /* An 'encode' expression is value-dependent if the operand is
20380 type-dependent. */
20381 expression = TREE_OPERAND (expression, 0);
20382 return dependent_type_p (expression);
20383
20384 case NOEXCEPT_EXPR:
20385 expression = TREE_OPERAND (expression, 0);
20386 return instantiation_dependent_expression_p (expression);
20387
20388 case SCOPE_REF:
20389 /* All instantiation-dependent expressions should also be considered
20390 value-dependent. */
20391 return instantiation_dependent_scope_ref_p (expression);
20392
20393 case COMPONENT_REF:
20394 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20395 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20396
20397 case NONTYPE_ARGUMENT_PACK:
20398 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20399 is value-dependent. */
20400 {
20401 tree values = ARGUMENT_PACK_ARGS (expression);
20402 int i, len = TREE_VEC_LENGTH (values);
20403
20404 for (i = 0; i < len; ++i)
20405 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20406 return true;
20407
20408 return false;
20409 }
20410
20411 case TRAIT_EXPR:
20412 {
20413 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20414 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20415 || (type2 ? dependent_type_p (type2) : false));
20416 }
20417
20418 case MODOP_EXPR:
20419 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20420 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20421
20422 case ARRAY_REF:
20423 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20424 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20425
20426 case ADDR_EXPR:
20427 {
20428 tree op = TREE_OPERAND (expression, 0);
20429 return (value_dependent_expression_p (op)
20430 || has_value_dependent_address (op));
20431 }
20432
20433 case CALL_EXPR:
20434 {
20435 tree fn = get_callee_fndecl (expression);
20436 int i, nargs;
20437 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20438 return true;
20439 nargs = call_expr_nargs (expression);
20440 for (i = 0; i < nargs; ++i)
20441 {
20442 tree op = CALL_EXPR_ARG (expression, i);
20443 /* In a call to a constexpr member function, look through the
20444 implicit ADDR_EXPR on the object argument so that it doesn't
20445 cause the call to be considered value-dependent. We also
20446 look through it in potential_constant_expression. */
20447 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20448 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20449 && TREE_CODE (op) == ADDR_EXPR)
20450 op = TREE_OPERAND (op, 0);
20451 if (value_dependent_expression_p (op))
20452 return true;
20453 }
20454 return false;
20455 }
20456
20457 case TEMPLATE_ID_EXPR:
20458 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20459 type-dependent. */
20460 return type_dependent_expression_p (expression);
20461
20462 case CONSTRUCTOR:
20463 {
20464 unsigned ix;
20465 tree val;
20466 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20467 if (value_dependent_expression_p (val))
20468 return true;
20469 return false;
20470 }
20471
20472 case STMT_EXPR:
20473 /* Treat a GNU statement expression as dependent to avoid crashing
20474 under fold_non_dependent_expr; it can't be constant. */
20475 return true;
20476
20477 default:
20478 /* A constant expression is value-dependent if any subexpression is
20479 value-dependent. */
20480 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20481 {
20482 case tcc_reference:
20483 case tcc_unary:
20484 case tcc_comparison:
20485 case tcc_binary:
20486 case tcc_expression:
20487 case tcc_vl_exp:
20488 {
20489 int i, len = cp_tree_operand_length (expression);
20490
20491 for (i = 0; i < len; i++)
20492 {
20493 tree t = TREE_OPERAND (expression, i);
20494
20495 /* In some cases, some of the operands may be missing.l
20496 (For example, in the case of PREDECREMENT_EXPR, the
20497 amount to increment by may be missing.) That doesn't
20498 make the expression dependent. */
20499 if (t && value_dependent_expression_p (t))
20500 return true;
20501 }
20502 }
20503 break;
20504 default:
20505 break;
20506 }
20507 break;
20508 }
20509
20510 /* The expression is not value-dependent. */
20511 return false;
20512 }
20513
20514 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20515 [temp.dep.expr]. Note that an expression with no type is
20516 considered dependent. Other parts of the compiler arrange for an
20517 expression with type-dependent subexpressions to have no type, so
20518 this function doesn't have to be fully recursive. */
20519
20520 bool
20521 type_dependent_expression_p (tree expression)
20522 {
20523 if (!processing_template_decl)
20524 return false;
20525
20526 if (expression == NULL_TREE || expression == error_mark_node)
20527 return false;
20528
20529 /* An unresolved name is always dependent. */
20530 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20531 return true;
20532
20533 /* Some expression forms are never type-dependent. */
20534 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20535 || TREE_CODE (expression) == SIZEOF_EXPR
20536 || TREE_CODE (expression) == ALIGNOF_EXPR
20537 || TREE_CODE (expression) == AT_ENCODE_EXPR
20538 || TREE_CODE (expression) == NOEXCEPT_EXPR
20539 || TREE_CODE (expression) == TRAIT_EXPR
20540 || TREE_CODE (expression) == TYPEID_EXPR
20541 || TREE_CODE (expression) == DELETE_EXPR
20542 || TREE_CODE (expression) == VEC_DELETE_EXPR
20543 || TREE_CODE (expression) == THROW_EXPR)
20544 return false;
20545
20546 /* The types of these expressions depends only on the type to which
20547 the cast occurs. */
20548 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20549 || TREE_CODE (expression) == STATIC_CAST_EXPR
20550 || TREE_CODE (expression) == CONST_CAST_EXPR
20551 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20552 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20553 || TREE_CODE (expression) == CAST_EXPR)
20554 return dependent_type_p (TREE_TYPE (expression));
20555
20556 /* The types of these expressions depends only on the type created
20557 by the expression. */
20558 if (TREE_CODE (expression) == NEW_EXPR
20559 || TREE_CODE (expression) == VEC_NEW_EXPR)
20560 {
20561 /* For NEW_EXPR tree nodes created inside a template, either
20562 the object type itself or a TREE_LIST may appear as the
20563 operand 1. */
20564 tree type = TREE_OPERAND (expression, 1);
20565 if (TREE_CODE (type) == TREE_LIST)
20566 /* This is an array type. We need to check array dimensions
20567 as well. */
20568 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20569 || value_dependent_expression_p
20570 (TREE_OPERAND (TREE_VALUE (type), 1));
20571 else
20572 return dependent_type_p (type);
20573 }
20574
20575 if (TREE_CODE (expression) == SCOPE_REF)
20576 {
20577 tree scope = TREE_OPERAND (expression, 0);
20578 tree name = TREE_OPERAND (expression, 1);
20579
20580 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20581 contains an identifier associated by name lookup with one or more
20582 declarations declared with a dependent type, or...a
20583 nested-name-specifier or qualified-id that names a member of an
20584 unknown specialization. */
20585 return (type_dependent_expression_p (name)
20586 || dependent_scope_p (scope));
20587 }
20588
20589 if (TREE_CODE (expression) == FUNCTION_DECL
20590 && DECL_LANG_SPECIFIC (expression)
20591 && DECL_TEMPLATE_INFO (expression)
20592 && (any_dependent_template_arguments_p
20593 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20594 return true;
20595
20596 if (TREE_CODE (expression) == TEMPLATE_DECL
20597 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20598 return false;
20599
20600 if (TREE_CODE (expression) == STMT_EXPR)
20601 expression = stmt_expr_value_expr (expression);
20602
20603 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20604 {
20605 tree elt;
20606 unsigned i;
20607
20608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20609 {
20610 if (type_dependent_expression_p (elt))
20611 return true;
20612 }
20613 return false;
20614 }
20615
20616 /* A static data member of the current instantiation with incomplete
20617 array type is type-dependent, as the definition and specializations
20618 can have different bounds. */
20619 if (VAR_P (expression)
20620 && DECL_CLASS_SCOPE_P (expression)
20621 && dependent_type_p (DECL_CONTEXT (expression))
20622 && VAR_HAD_UNKNOWN_BOUND (expression))
20623 return true;
20624
20625 /* An array of unknown bound depending on a variadic parameter, eg:
20626
20627 template<typename... Args>
20628 void foo (Args... args)
20629 {
20630 int arr[] = { args... };
20631 }
20632
20633 template<int... vals>
20634 void bar ()
20635 {
20636 int arr[] = { vals... };
20637 }
20638
20639 If the array has no length and has an initializer, it must be that
20640 we couldn't determine its length in cp_complete_array_type because
20641 it is dependent. */
20642 if (VAR_P (expression)
20643 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20644 && !TYPE_DOMAIN (TREE_TYPE (expression))
20645 && DECL_INITIAL (expression))
20646 return true;
20647
20648 if (TREE_TYPE (expression) == unknown_type_node)
20649 {
20650 if (TREE_CODE (expression) == ADDR_EXPR)
20651 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20652 if (TREE_CODE (expression) == COMPONENT_REF
20653 || TREE_CODE (expression) == OFFSET_REF)
20654 {
20655 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20656 return true;
20657 expression = TREE_OPERAND (expression, 1);
20658 if (identifier_p (expression))
20659 return false;
20660 }
20661 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20662 if (TREE_CODE (expression) == SCOPE_REF)
20663 return false;
20664
20665 /* Always dependent, on the number of arguments if nothing else. */
20666 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20667 return true;
20668
20669 if (BASELINK_P (expression))
20670 expression = BASELINK_FUNCTIONS (expression);
20671
20672 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20673 {
20674 if (any_dependent_template_arguments_p
20675 (TREE_OPERAND (expression, 1)))
20676 return true;
20677 expression = TREE_OPERAND (expression, 0);
20678 }
20679 gcc_assert (TREE_CODE (expression) == OVERLOAD
20680 || TREE_CODE (expression) == FUNCTION_DECL);
20681
20682 while (expression)
20683 {
20684 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20685 return true;
20686 expression = OVL_NEXT (expression);
20687 }
20688 return false;
20689 }
20690
20691 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20692
20693 return (dependent_type_p (TREE_TYPE (expression)));
20694 }
20695
20696 /* walk_tree callback function for instantiation_dependent_expression_p,
20697 below. Returns non-zero if a dependent subexpression is found. */
20698
20699 static tree
20700 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20701 void * /*data*/)
20702 {
20703 if (TYPE_P (*tp))
20704 {
20705 /* We don't have to worry about decltype currently because decltype
20706 of an instantiation-dependent expr is a dependent type. This
20707 might change depending on the resolution of DR 1172. */
20708 *walk_subtrees = false;
20709 return NULL_TREE;
20710 }
20711 enum tree_code code = TREE_CODE (*tp);
20712 switch (code)
20713 {
20714 /* Don't treat an argument list as dependent just because it has no
20715 TREE_TYPE. */
20716 case TREE_LIST:
20717 case TREE_VEC:
20718 return NULL_TREE;
20719
20720 case VAR_DECL:
20721 case CONST_DECL:
20722 /* A constant with a dependent initializer is dependent. */
20723 if (value_dependent_expression_p (*tp))
20724 return *tp;
20725 break;
20726
20727 case TEMPLATE_PARM_INDEX:
20728 return *tp;
20729
20730 /* Handle expressions with type operands. */
20731 case SIZEOF_EXPR:
20732 case ALIGNOF_EXPR:
20733 case TYPEID_EXPR:
20734 case AT_ENCODE_EXPR:
20735 {
20736 tree op = TREE_OPERAND (*tp, 0);
20737 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20738 op = TREE_TYPE (op);
20739 if (TYPE_P (op))
20740 {
20741 if (dependent_type_p (op))
20742 return *tp;
20743 else
20744 {
20745 *walk_subtrees = false;
20746 return NULL_TREE;
20747 }
20748 }
20749 break;
20750 }
20751
20752 case TRAIT_EXPR:
20753 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20754 || (TRAIT_EXPR_TYPE2 (*tp)
20755 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20756 return *tp;
20757 *walk_subtrees = false;
20758 return NULL_TREE;
20759
20760 case COMPONENT_REF:
20761 if (identifier_p (TREE_OPERAND (*tp, 1)))
20762 /* In a template, finish_class_member_access_expr creates a
20763 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20764 type-dependent, so that we can check access control at
20765 instantiation time (PR 42277). See also Core issue 1273. */
20766 return *tp;
20767 break;
20768
20769 case SCOPE_REF:
20770 if (instantiation_dependent_scope_ref_p (*tp))
20771 return *tp;
20772 else
20773 break;
20774
20775 /* Treat statement-expressions as dependent. */
20776 case BIND_EXPR:
20777 return *tp;
20778
20779 default:
20780 break;
20781 }
20782
20783 if (type_dependent_expression_p (*tp))
20784 return *tp;
20785 else
20786 return NULL_TREE;
20787 }
20788
20789 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20790 sense defined by the ABI:
20791
20792 "An expression is instantiation-dependent if it is type-dependent
20793 or value-dependent, or it has a subexpression that is type-dependent
20794 or value-dependent." */
20795
20796 bool
20797 instantiation_dependent_expression_p (tree expression)
20798 {
20799 tree result;
20800
20801 if (!processing_template_decl)
20802 return false;
20803
20804 if (expression == error_mark_node)
20805 return false;
20806
20807 result = cp_walk_tree_without_duplicates (&expression,
20808 instantiation_dependent_r, NULL);
20809 return result != NULL_TREE;
20810 }
20811
20812 /* Like type_dependent_expression_p, but it also works while not processing
20813 a template definition, i.e. during substitution or mangling. */
20814
20815 bool
20816 type_dependent_expression_p_push (tree expr)
20817 {
20818 bool b;
20819 ++processing_template_decl;
20820 b = type_dependent_expression_p (expr);
20821 --processing_template_decl;
20822 return b;
20823 }
20824
20825 /* Returns TRUE if ARGS contains a type-dependent expression. */
20826
20827 bool
20828 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20829 {
20830 unsigned int i;
20831 tree arg;
20832
20833 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20834 {
20835 if (type_dependent_expression_p (arg))
20836 return true;
20837 }
20838 return false;
20839 }
20840
20841 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20842 expressions) contains any type-dependent expressions. */
20843
20844 bool
20845 any_type_dependent_elements_p (const_tree list)
20846 {
20847 for (; list; list = TREE_CHAIN (list))
20848 if (type_dependent_expression_p (TREE_VALUE (list)))
20849 return true;
20850
20851 return false;
20852 }
20853
20854 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20855 expressions) contains any value-dependent expressions. */
20856
20857 bool
20858 any_value_dependent_elements_p (const_tree list)
20859 {
20860 for (; list; list = TREE_CHAIN (list))
20861 if (value_dependent_expression_p (TREE_VALUE (list)))
20862 return true;
20863
20864 return false;
20865 }
20866
20867 /* Returns TRUE if the ARG (a template argument) is dependent. */
20868
20869 bool
20870 dependent_template_arg_p (tree arg)
20871 {
20872 if (!processing_template_decl)
20873 return false;
20874
20875 /* Assume a template argument that was wrongly written by the user
20876 is dependent. This is consistent with what
20877 any_dependent_template_arguments_p [that calls this function]
20878 does. */
20879 if (!arg || arg == error_mark_node)
20880 return true;
20881
20882 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20883 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20884
20885 if (TREE_CODE (arg) == TEMPLATE_DECL
20886 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20887 return dependent_template_p (arg);
20888 else if (ARGUMENT_PACK_P (arg))
20889 {
20890 tree args = ARGUMENT_PACK_ARGS (arg);
20891 int i, len = TREE_VEC_LENGTH (args);
20892 for (i = 0; i < len; ++i)
20893 {
20894 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20895 return true;
20896 }
20897
20898 return false;
20899 }
20900 else if (TYPE_P (arg))
20901 return dependent_type_p (arg);
20902 else
20903 return (type_dependent_expression_p (arg)
20904 || value_dependent_expression_p (arg));
20905 }
20906
20907 /* Returns true if ARGS (a collection of template arguments) contains
20908 any types that require structural equality testing. */
20909
20910 bool
20911 any_template_arguments_need_structural_equality_p (tree args)
20912 {
20913 int i;
20914 int j;
20915
20916 if (!args)
20917 return false;
20918 if (args == error_mark_node)
20919 return true;
20920
20921 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20922 {
20923 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20924 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20925 {
20926 tree arg = TREE_VEC_ELT (level, j);
20927 tree packed_args = NULL_TREE;
20928 int k, len = 1;
20929
20930 if (ARGUMENT_PACK_P (arg))
20931 {
20932 /* Look inside the argument pack. */
20933 packed_args = ARGUMENT_PACK_ARGS (arg);
20934 len = TREE_VEC_LENGTH (packed_args);
20935 }
20936
20937 for (k = 0; k < len; ++k)
20938 {
20939 if (packed_args)
20940 arg = TREE_VEC_ELT (packed_args, k);
20941
20942 if (error_operand_p (arg))
20943 return true;
20944 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20945 continue;
20946 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20947 return true;
20948 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20949 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20950 return true;
20951 }
20952 }
20953 }
20954
20955 return false;
20956 }
20957
20958 /* Returns true if ARGS (a collection of template arguments) contains
20959 any dependent arguments. */
20960
20961 bool
20962 any_dependent_template_arguments_p (const_tree args)
20963 {
20964 int i;
20965 int j;
20966
20967 if (!args)
20968 return false;
20969 if (args == error_mark_node)
20970 return true;
20971
20972 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20973 {
20974 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20975 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20976 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20977 return true;
20978 }
20979
20980 return false;
20981 }
20982
20983 /* Returns TRUE if the template TMPL is dependent. */
20984
20985 bool
20986 dependent_template_p (tree tmpl)
20987 {
20988 if (TREE_CODE (tmpl) == OVERLOAD)
20989 {
20990 while (tmpl)
20991 {
20992 if (dependent_template_p (OVL_CURRENT (tmpl)))
20993 return true;
20994 tmpl = OVL_NEXT (tmpl);
20995 }
20996 return false;
20997 }
20998
20999 /* Template template parameters are dependent. */
21000 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21001 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21002 return true;
21003 /* So are names that have not been looked up. */
21004 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21005 return true;
21006 /* So are member templates of dependent classes. */
21007 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21008 return dependent_type_p (DECL_CONTEXT (tmpl));
21009 return false;
21010 }
21011
21012 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21013
21014 bool
21015 dependent_template_id_p (tree tmpl, tree args)
21016 {
21017 return (dependent_template_p (tmpl)
21018 || any_dependent_template_arguments_p (args));
21019 }
21020
21021 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21022 is dependent. */
21023
21024 bool
21025 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21026 {
21027 int i;
21028
21029 if (!processing_template_decl)
21030 return false;
21031
21032 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21033 {
21034 tree decl = TREE_VEC_ELT (declv, i);
21035 tree init = TREE_VEC_ELT (initv, i);
21036 tree cond = TREE_VEC_ELT (condv, i);
21037 tree incr = TREE_VEC_ELT (incrv, i);
21038
21039 if (type_dependent_expression_p (decl))
21040 return true;
21041
21042 if (init && type_dependent_expression_p (init))
21043 return true;
21044
21045 if (type_dependent_expression_p (cond))
21046 return true;
21047
21048 if (COMPARISON_CLASS_P (cond)
21049 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21050 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21051 return true;
21052
21053 if (TREE_CODE (incr) == MODOP_EXPR)
21054 {
21055 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21056 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21057 return true;
21058 }
21059 else if (type_dependent_expression_p (incr))
21060 return true;
21061 else if (TREE_CODE (incr) == MODIFY_EXPR)
21062 {
21063 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21064 return true;
21065 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21066 {
21067 tree t = TREE_OPERAND (incr, 1);
21068 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21069 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21070 return true;
21071 }
21072 }
21073 }
21074
21075 return false;
21076 }
21077
21078 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21079 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21080 no such TYPE can be found. Note that this function peers inside
21081 uninstantiated templates and therefore should be used only in
21082 extremely limited situations. ONLY_CURRENT_P restricts this
21083 peering to the currently open classes hierarchy (which is required
21084 when comparing types). */
21085
21086 tree
21087 resolve_typename_type (tree type, bool only_current_p)
21088 {
21089 tree scope;
21090 tree name;
21091 tree decl;
21092 int quals;
21093 tree pushed_scope;
21094 tree result;
21095
21096 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21097
21098 scope = TYPE_CONTEXT (type);
21099 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21100 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21101 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21102 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21103 identifier of the TYPENAME_TYPE anymore.
21104 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21105 TYPENAME_TYPE instead, we avoid messing up with a possible
21106 typedef variant case. */
21107 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21108
21109 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21110 it first before we can figure out what NAME refers to. */
21111 if (TREE_CODE (scope) == TYPENAME_TYPE)
21112 {
21113 if (TYPENAME_IS_RESOLVING_P (scope))
21114 /* Given a class template A with a dependent base with nested type C,
21115 typedef typename A::C::C C will land us here, as trying to resolve
21116 the initial A::C leads to the local C typedef, which leads back to
21117 A::C::C. So we break the recursion now. */
21118 return type;
21119 else
21120 scope = resolve_typename_type (scope, only_current_p);
21121 }
21122 /* If we don't know what SCOPE refers to, then we cannot resolve the
21123 TYPENAME_TYPE. */
21124 if (TREE_CODE (scope) == TYPENAME_TYPE)
21125 return type;
21126 /* If the SCOPE is a template type parameter, we have no way of
21127 resolving the name. */
21128 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21129 return type;
21130 /* If the SCOPE is not the current instantiation, there's no reason
21131 to look inside it. */
21132 if (only_current_p && !currently_open_class (scope))
21133 return type;
21134 /* If this is a typedef, we don't want to look inside (c++/11987). */
21135 if (typedef_variant_p (type))
21136 return type;
21137 /* If SCOPE isn't the template itself, it will not have a valid
21138 TYPE_FIELDS list. */
21139 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21140 /* scope is either the template itself or a compatible instantiation
21141 like X<T>, so look up the name in the original template. */
21142 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21143 else
21144 /* scope is a partial instantiation, so we can't do the lookup or we
21145 will lose the template arguments. */
21146 return type;
21147 /* Enter the SCOPE so that name lookup will be resolved as if we
21148 were in the class definition. In particular, SCOPE will no
21149 longer be considered a dependent type. */
21150 pushed_scope = push_scope (scope);
21151 /* Look up the declaration. */
21152 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21153 tf_warning_or_error);
21154
21155 result = NULL_TREE;
21156
21157 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21158 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21159 if (!decl)
21160 /*nop*/;
21161 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21162 && TREE_CODE (decl) == TYPE_DECL)
21163 {
21164 result = TREE_TYPE (decl);
21165 if (result == error_mark_node)
21166 result = NULL_TREE;
21167 }
21168 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21169 && DECL_CLASS_TEMPLATE_P (decl))
21170 {
21171 tree tmpl;
21172 tree args;
21173 /* Obtain the template and the arguments. */
21174 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21175 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21176 /* Instantiate the template. */
21177 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21178 /*entering_scope=*/0,
21179 tf_error | tf_user);
21180 if (result == error_mark_node)
21181 result = NULL_TREE;
21182 }
21183
21184 /* Leave the SCOPE. */
21185 if (pushed_scope)
21186 pop_scope (pushed_scope);
21187
21188 /* If we failed to resolve it, return the original typename. */
21189 if (!result)
21190 return type;
21191
21192 /* If lookup found a typename type, resolve that too. */
21193 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21194 {
21195 /* Ill-formed programs can cause infinite recursion here, so we
21196 must catch that. */
21197 TYPENAME_IS_RESOLVING_P (type) = 1;
21198 result = resolve_typename_type (result, only_current_p);
21199 TYPENAME_IS_RESOLVING_P (type) = 0;
21200 }
21201
21202 /* Qualify the resulting type. */
21203 quals = cp_type_quals (type);
21204 if (quals)
21205 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21206
21207 return result;
21208 }
21209
21210 /* EXPR is an expression which is not type-dependent. Return a proxy
21211 for EXPR that can be used to compute the types of larger
21212 expressions containing EXPR. */
21213
21214 tree
21215 build_non_dependent_expr (tree expr)
21216 {
21217 tree inner_expr;
21218
21219 #ifdef ENABLE_CHECKING
21220 /* Try to get a constant value for all non-dependent expressions in
21221 order to expose bugs in *_dependent_expression_p and constexpr. */
21222 if (cxx_dialect >= cxx11)
21223 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21224 #endif
21225
21226 /* Preserve OVERLOADs; the functions must be available to resolve
21227 types. */
21228 inner_expr = expr;
21229 if (TREE_CODE (inner_expr) == STMT_EXPR)
21230 inner_expr = stmt_expr_value_expr (inner_expr);
21231 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21232 inner_expr = TREE_OPERAND (inner_expr, 0);
21233 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21234 inner_expr = TREE_OPERAND (inner_expr, 1);
21235 if (is_overloaded_fn (inner_expr)
21236 || TREE_CODE (inner_expr) == OFFSET_REF)
21237 return expr;
21238 /* There is no need to return a proxy for a variable. */
21239 if (VAR_P (expr))
21240 return expr;
21241 /* Preserve string constants; conversions from string constants to
21242 "char *" are allowed, even though normally a "const char *"
21243 cannot be used to initialize a "char *". */
21244 if (TREE_CODE (expr) == STRING_CST)
21245 return expr;
21246 /* Preserve arithmetic constants, as an optimization -- there is no
21247 reason to create a new node. */
21248 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21249 return expr;
21250 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21251 There is at least one place where we want to know that a
21252 particular expression is a throw-expression: when checking a ?:
21253 expression, there are special rules if the second or third
21254 argument is a throw-expression. */
21255 if (TREE_CODE (expr) == THROW_EXPR)
21256 return expr;
21257
21258 /* Don't wrap an initializer list, we need to be able to look inside. */
21259 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21260 return expr;
21261
21262 /* Don't wrap a dummy object, we need to be able to test for it. */
21263 if (is_dummy_object (expr))
21264 return expr;
21265
21266 if (TREE_CODE (expr) == COND_EXPR)
21267 return build3 (COND_EXPR,
21268 TREE_TYPE (expr),
21269 TREE_OPERAND (expr, 0),
21270 (TREE_OPERAND (expr, 1)
21271 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21272 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21273 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21274 if (TREE_CODE (expr) == COMPOUND_EXPR
21275 && !COMPOUND_EXPR_OVERLOADED (expr))
21276 return build2 (COMPOUND_EXPR,
21277 TREE_TYPE (expr),
21278 TREE_OPERAND (expr, 0),
21279 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21280
21281 /* If the type is unknown, it can't really be non-dependent */
21282 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21283
21284 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21285 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21286 }
21287
21288 /* ARGS is a vector of expressions as arguments to a function call.
21289 Replace the arguments with equivalent non-dependent expressions.
21290 This modifies ARGS in place. */
21291
21292 void
21293 make_args_non_dependent (vec<tree, va_gc> *args)
21294 {
21295 unsigned int ix;
21296 tree arg;
21297
21298 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21299 {
21300 tree newarg = build_non_dependent_expr (arg);
21301 if (newarg != arg)
21302 (*args)[ix] = newarg;
21303 }
21304 }
21305
21306 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21307 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21308 parms. */
21309
21310 static tree
21311 make_auto_1 (tree name)
21312 {
21313 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21314 TYPE_NAME (au) = build_decl (input_location,
21315 TYPE_DECL, name, au);
21316 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21317 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21318 (0, processing_template_decl + 1, processing_template_decl + 1,
21319 TYPE_NAME (au), NULL_TREE);
21320 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21321 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21322 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21323
21324 return au;
21325 }
21326
21327 tree
21328 make_decltype_auto (void)
21329 {
21330 return make_auto_1 (get_identifier ("decltype(auto)"));
21331 }
21332
21333 tree
21334 make_auto (void)
21335 {
21336 return make_auto_1 (get_identifier ("auto"));
21337 }
21338
21339 /* Given type ARG, return std::initializer_list<ARG>. */
21340
21341 static tree
21342 listify (tree arg)
21343 {
21344 tree std_init_list = namespace_binding
21345 (get_identifier ("initializer_list"), std_node);
21346 tree argvec;
21347 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21348 {
21349 error ("deducing from brace-enclosed initializer list requires "
21350 "#include <initializer_list>");
21351 return error_mark_node;
21352 }
21353 argvec = make_tree_vec (1);
21354 TREE_VEC_ELT (argvec, 0) = arg;
21355 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21356 NULL_TREE, 0, tf_warning_or_error);
21357 }
21358
21359 /* Replace auto in TYPE with std::initializer_list<auto>. */
21360
21361 static tree
21362 listify_autos (tree type, tree auto_node)
21363 {
21364 tree init_auto = listify (auto_node);
21365 tree argvec = make_tree_vec (1);
21366 TREE_VEC_ELT (argvec, 0) = init_auto;
21367 if (processing_template_decl)
21368 argvec = add_to_template_args (current_template_args (), argvec);
21369 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21370 }
21371
21372 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21373 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21374
21375 tree
21376 do_auto_deduction (tree type, tree init, tree auto_node)
21377 {
21378 tree targs;
21379
21380 if (init == error_mark_node)
21381 return error_mark_node;
21382
21383 if (type_dependent_expression_p (init))
21384 /* Defining a subset of type-dependent expressions that we can deduce
21385 from ahead of time isn't worth the trouble. */
21386 return type;
21387
21388 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21389 with either a new invented type template parameter U or, if the
21390 initializer is a braced-init-list (8.5.4), with
21391 std::initializer_list<U>. */
21392 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21393 type = listify_autos (type, auto_node);
21394
21395 init = resolve_nondeduced_context (init);
21396
21397 targs = make_tree_vec (1);
21398 if (AUTO_IS_DECLTYPE (auto_node))
21399 {
21400 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21401 TREE_VEC_ELT (targs, 0)
21402 = finish_decltype_type (init, id, tf_warning_or_error);
21403 if (type != auto_node)
21404 {
21405 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21406 return error_mark_node;
21407 }
21408 }
21409 else
21410 {
21411 tree parms = build_tree_list (NULL_TREE, type);
21412 tree tparms = make_tree_vec (1);
21413 int val;
21414
21415 TREE_VEC_ELT (tparms, 0)
21416 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21417 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21418 DEDUCE_CALL, LOOKUP_NORMAL,
21419 NULL, /*explain_p=*/false);
21420 if (val > 0)
21421 {
21422 if (processing_template_decl)
21423 /* Try again at instantiation time. */
21424 return type;
21425 if (type && type != error_mark_node)
21426 /* If type is error_mark_node a diagnostic must have been
21427 emitted by now. Also, having a mention to '<type error>'
21428 in the diagnostic is not really useful to the user. */
21429 {
21430 if (cfun && auto_node == current_function_auto_return_pattern
21431 && LAMBDA_FUNCTION_P (current_function_decl))
21432 error ("unable to deduce lambda return type from %qE", init);
21433 else
21434 error ("unable to deduce %qT from %qE", type, init);
21435 }
21436 return error_mark_node;
21437 }
21438 }
21439
21440 /* If the list of declarators contains more than one declarator, the type
21441 of each declared variable is determined as described above. If the
21442 type deduced for the template parameter U is not the same in each
21443 deduction, the program is ill-formed. */
21444 if (TREE_TYPE (auto_node)
21445 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21446 {
21447 if (cfun && auto_node == current_function_auto_return_pattern
21448 && LAMBDA_FUNCTION_P (current_function_decl))
21449 error ("inconsistent types %qT and %qT deduced for "
21450 "lambda return type", TREE_TYPE (auto_node),
21451 TREE_VEC_ELT (targs, 0));
21452 else
21453 error ("inconsistent deduction for %qT: %qT and then %qT",
21454 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21455 return error_mark_node;
21456 }
21457 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21458
21459 if (processing_template_decl)
21460 targs = add_to_template_args (current_template_args (), targs);
21461 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21462 }
21463
21464 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21465 result. */
21466
21467 tree
21468 splice_late_return_type (tree type, tree late_return_type)
21469 {
21470 tree argvec;
21471
21472 if (late_return_type == NULL_TREE)
21473 return type;
21474 argvec = make_tree_vec (1);
21475 TREE_VEC_ELT (argvec, 0) = late_return_type;
21476 if (processing_template_parmlist)
21477 /* For a late-specified return type in a template type-parameter, we
21478 need to add a dummy argument level for its parmlist. */
21479 argvec = add_to_template_args
21480 (make_tree_vec (processing_template_parmlist), argvec);
21481 if (current_template_parms)
21482 argvec = add_to_template_args (current_template_args (), argvec);
21483 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21484 }
21485
21486 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21487 'decltype(auto)'. */
21488
21489 bool
21490 is_auto (const_tree type)
21491 {
21492 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21493 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21494 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21495 return true;
21496 else
21497 return false;
21498 }
21499
21500 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21501 a use of `auto'. Returns NULL_TREE otherwise. */
21502
21503 tree
21504 type_uses_auto (tree type)
21505 {
21506 return find_type_usage (type, is_auto);
21507 }
21508
21509 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21510 'decltype(auto)' or a concept. */
21511
21512 bool
21513 is_auto_or_concept (const_tree type)
21514 {
21515 return is_auto (type); // or concept
21516 }
21517
21518 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21519 a concept identifier) iff TYPE contains a use of a generic type. Returns
21520 NULL_TREE otherwise. */
21521
21522 tree
21523 type_uses_auto_or_concept (tree type)
21524 {
21525 return find_type_usage (type, is_auto_or_concept);
21526 }
21527
21528
21529 /* For a given template T, return the vector of typedefs referenced
21530 in T for which access check is needed at T instantiation time.
21531 T is either a FUNCTION_DECL or a RECORD_TYPE.
21532 Those typedefs were added to T by the function
21533 append_type_to_template_for_access_check. */
21534
21535 vec<qualified_typedef_usage_t, va_gc> *
21536 get_types_needing_access_check (tree t)
21537 {
21538 tree ti;
21539 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21540
21541 if (!t || t == error_mark_node)
21542 return NULL;
21543
21544 if (!(ti = get_template_info (t)))
21545 return NULL;
21546
21547 if (CLASS_TYPE_P (t)
21548 || TREE_CODE (t) == FUNCTION_DECL)
21549 {
21550 if (!TI_TEMPLATE (ti))
21551 return NULL;
21552
21553 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21554 }
21555
21556 return result;
21557 }
21558
21559 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21560 tied to T. That list of typedefs will be access checked at
21561 T instantiation time.
21562 T is either a FUNCTION_DECL or a RECORD_TYPE.
21563 TYPE_DECL is a TYPE_DECL node representing a typedef.
21564 SCOPE is the scope through which TYPE_DECL is accessed.
21565 LOCATION is the location of the usage point of TYPE_DECL.
21566
21567 This function is a subroutine of
21568 append_type_to_template_for_access_check. */
21569
21570 static void
21571 append_type_to_template_for_access_check_1 (tree t,
21572 tree type_decl,
21573 tree scope,
21574 location_t location)
21575 {
21576 qualified_typedef_usage_t typedef_usage;
21577 tree ti;
21578
21579 if (!t || t == error_mark_node)
21580 return;
21581
21582 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21583 || CLASS_TYPE_P (t))
21584 && type_decl
21585 && TREE_CODE (type_decl) == TYPE_DECL
21586 && scope);
21587
21588 if (!(ti = get_template_info (t)))
21589 return;
21590
21591 gcc_assert (TI_TEMPLATE (ti));
21592
21593 typedef_usage.typedef_decl = type_decl;
21594 typedef_usage.context = scope;
21595 typedef_usage.locus = location;
21596
21597 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21598 }
21599
21600 /* Append TYPE_DECL to the template TEMPL.
21601 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21602 At TEMPL instanciation time, TYPE_DECL will be checked to see
21603 if it can be accessed through SCOPE.
21604 LOCATION is the location of the usage point of TYPE_DECL.
21605
21606 e.g. consider the following code snippet:
21607
21608 class C
21609 {
21610 typedef int myint;
21611 };
21612
21613 template<class U> struct S
21614 {
21615 C::myint mi; // <-- usage point of the typedef C::myint
21616 };
21617
21618 S<char> s;
21619
21620 At S<char> instantiation time, we need to check the access of C::myint
21621 In other words, we need to check the access of the myint typedef through
21622 the C scope. For that purpose, this function will add the myint typedef
21623 and the scope C through which its being accessed to a list of typedefs
21624 tied to the template S. That list will be walked at template instantiation
21625 time and access check performed on each typedefs it contains.
21626 Note that this particular code snippet should yield an error because
21627 myint is private to C. */
21628
21629 void
21630 append_type_to_template_for_access_check (tree templ,
21631 tree type_decl,
21632 tree scope,
21633 location_t location)
21634 {
21635 qualified_typedef_usage_t *iter;
21636 unsigned i;
21637
21638 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21639
21640 /* Make sure we don't append the type to the template twice. */
21641 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21642 if (iter->typedef_decl == type_decl && scope == iter->context)
21643 return;
21644
21645 append_type_to_template_for_access_check_1 (templ, type_decl,
21646 scope, location);
21647 }
21648
21649 /* Convert the generic type parameters in PARM that match the types given in the
21650 range [START_IDX, END_IDX) from the current_template_parms into generic type
21651 packs. */
21652
21653 tree
21654 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21655 {
21656 tree current = current_template_parms;
21657 int depth = TMPL_PARMS_DEPTH (current);
21658 current = INNERMOST_TEMPLATE_PARMS (current);
21659 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21660
21661 for (int i = 0; i < start_idx; ++i)
21662 TREE_VEC_ELT (replacement, i)
21663 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21664
21665 for (int i = start_idx; i < end_idx; ++i)
21666 {
21667 /* Create a distinct parameter pack type from the current parm and add it
21668 to the replacement args to tsubst below into the generic function
21669 parameter. */
21670
21671 tree o = TREE_TYPE (TREE_VALUE
21672 (TREE_VEC_ELT (current, i)));
21673 tree t = copy_type (o);
21674 TEMPLATE_TYPE_PARM_INDEX (t)
21675 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21676 o, 0, 0, tf_none);
21677 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21678 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21679 TYPE_MAIN_VARIANT (t) = t;
21680 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21681 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21682 TREE_VEC_ELT (replacement, i) = t;
21683 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21684 }
21685
21686 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21687 TREE_VEC_ELT (replacement, i)
21688 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21689
21690 /* If there are more levels then build up the replacement with the outer
21691 template parms. */
21692 if (depth > 1)
21693 replacement = add_to_template_args (template_parms_to_args
21694 (TREE_CHAIN (current_template_parms)),
21695 replacement);
21696
21697 return tsubst (parm, replacement, tf_none, NULL_TREE);
21698 }
21699
21700
21701 /* Set up the hash tables for template instantiations. */
21702
21703 void
21704 init_template_processing (void)
21705 {
21706 decl_specializations = htab_create_ggc (37,
21707 hash_specialization,
21708 eq_specializations,
21709 ggc_free);
21710 type_specializations = htab_create_ggc (37,
21711 hash_specialization,
21712 eq_specializations,
21713 ggc_free);
21714 }
21715
21716 /* Print stats about the template hash tables for -fstats. */
21717
21718 void
21719 print_template_statistics (void)
21720 {
21721 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21722 "%f collisions\n", (long) htab_size (decl_specializations),
21723 (long) htab_elements (decl_specializations),
21724 htab_collisions (decl_specializations));
21725 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21726 "%f collisions\n", (long) htab_size (type_specializations),
21727 (long) htab_elements (type_specializations),
21728 htab_collisions (type_specializations));
21729 }
21730
21731 #include "gt-cp-pt.h"