re PR c++/66405 (ICE: in tsubst, at cp/pt.c:11984)
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 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 "hash-set.h"
32 #include "vec.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "stringpool.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "intl.h"
43 #include "flags.h"
44 #include "cp-tree.h"
45 #include "c-family/c-common.h"
46 #include "c-family/c-objc.h"
47 #include "cp-objcp-common.h"
48 #include "tree-inline.h"
49 #include "decl.h"
50 #include "toplev.h"
51 #include "timevar.h"
52 #include "tree-iterator.h"
53 #include "type-utils.h"
54 #include "gimplify.h"
55
56 /* The type of functions taking a tree, and some additional data, and
57 returning an int. */
58 typedef int (*tree_fn_t) (tree, void*);
59
60 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
61 instantiations have been deferred, either because their definitions
62 were not yet available, or because we were putting off doing the work. */
63 struct GTY ((chain_next ("%h.next"))) pending_template {
64 struct pending_template *next;
65 struct tinst_level *tinst;
66 };
67
68 static GTY(()) struct pending_template *pending_templates;
69 static GTY(()) struct pending_template *last_pending_template;
70
71 int processing_template_parmlist;
72 static int template_header_count;
73
74 static GTY(()) tree saved_trees;
75 static vec<int> inline_parm_levels;
76
77 static GTY(()) struct tinst_level *current_tinst_level;
78
79 static GTY(()) tree saved_access_scope;
80
81 /* Live only within one (recursive) call to tsubst_expr. We use
82 this to pass the statement expression node from the STMT_EXPR
83 to the EXPR_STMT that is its result. */
84 static tree cur_stmt_expr;
85
86 /* True if we've recursed into fn_type_unification too many times. */
87 static bool excessive_deduction_depth;
88
89 struct GTY((for_user)) spec_entry
90 {
91 tree tmpl;
92 tree args;
93 tree spec;
94 };
95
96 struct spec_hasher : ggc_hasher<spec_entry *>
97 {
98 static hashval_t hash (spec_entry *);
99 static bool equal (spec_entry *, spec_entry *);
100 };
101
102 static GTY (()) hash_table<spec_hasher> *decl_specializations;
103
104 static GTY (()) hash_table<spec_hasher> *type_specializations;
105
106 /* Contains canonical template parameter types. The vector is indexed by
107 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
108 TREE_LIST, whose TREE_VALUEs contain the canonical template
109 parameters of various types and levels. */
110 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
111
112 #define UNIFY_ALLOW_NONE 0
113 #define UNIFY_ALLOW_MORE_CV_QUAL 1
114 #define UNIFY_ALLOW_LESS_CV_QUAL 2
115 #define UNIFY_ALLOW_DERIVED 4
116 #define UNIFY_ALLOW_INTEGER 8
117 #define UNIFY_ALLOW_OUTER_LEVEL 16
118 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
119 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
120
121 enum template_base_result {
122 tbr_incomplete_type,
123 tbr_ambiguous_baseclass,
124 tbr_success
125 };
126
127 static void push_access_scope (tree);
128 static void pop_access_scope (tree);
129 static bool resolve_overloaded_unification (tree, tree, tree, tree,
130 unification_kind_t, int,
131 bool);
132 static int try_one_overload (tree, tree, tree, tree, tree,
133 unification_kind_t, int, bool, bool);
134 static int unify (tree, tree, tree, tree, int, bool);
135 static void add_pending_template (tree);
136 static tree reopen_tinst_level (struct tinst_level *);
137 static tree tsubst_initializer_list (tree, tree);
138 static tree get_partial_spec_bindings (tree, tree, tree, tree);
139 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
140 bool, bool);
141 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
142 bool, bool);
143 static void tsubst_enum (tree, tree, tree);
144 static tree add_to_template_args (tree, tree);
145 static tree add_outermost_template_args (tree, tree);
146 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
147 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
148 tree);
149 static int type_unification_real (tree, tree, tree, const tree *,
150 unsigned int, int, unification_kind_t, int,
151 vec<deferred_access_check, va_gc> **,
152 bool);
153 static void note_template_header (int);
154 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
155 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
156 static tree convert_template_argument (tree, tree, tree,
157 tsubst_flags_t, int, tree);
158 static int for_each_template_parm (tree, tree_fn_t, void*,
159 hash_set<tree> *, bool);
160 static tree expand_template_argument_pack (tree);
161 static tree build_template_parm_index (int, int, int, tree, tree);
162 static bool inline_needs_template_parms (tree, bool);
163 static void push_inline_template_parms_recursive (tree, int);
164 static tree retrieve_local_specialization (tree);
165 static void register_local_specialization (tree, tree);
166 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
167 static int mark_template_parm (tree, void *);
168 static int template_parm_this_level_p (tree, void *);
169 static tree tsubst_friend_function (tree, tree);
170 static tree tsubst_friend_class (tree, tree);
171 static int can_complete_type_without_circularity (tree);
172 static tree get_bindings (tree, tree, tree, bool);
173 static int template_decl_level (tree);
174 static int check_cv_quals_for_unify (int, tree, tree);
175 static void template_parm_level_and_index (tree, int*, int*);
176 static int unify_pack_expansion (tree, tree, tree,
177 tree, unification_kind_t, bool, bool);
178 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
180 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
181 static void regenerate_decl_from_template (tree, tree);
182 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
183 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
184 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
185 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
186 static bool check_specialization_scope (void);
187 static tree process_partial_specialization (tree);
188 static void set_current_access_from_decl (tree);
189 static enum template_base_result get_template_base (tree, tree, tree, tree,
190 bool , tree *);
191 static tree try_class_unification (tree, tree, tree, tree, bool);
192 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
193 tree, tree);
194 static bool template_template_parm_bindings_ok_p (tree, tree);
195 static int template_args_equal (tree, tree);
196 static void tsubst_default_arguments (tree, tsubst_flags_t);
197 static tree for_each_template_parm_r (tree *, int *, void *);
198 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
199 static void copy_default_args_to_explicit_spec (tree);
200 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
201 static bool dependent_template_arg_p (tree);
202 static bool any_template_arguments_need_structural_equality_p (tree);
203 static bool dependent_type_p_r (tree);
204 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
205 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
206 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree template_parm_to_arg (tree t);
214 static tree current_template_args (void);
215 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
216 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
217
218 /* Make the current scope suitable for access checking when we are
219 processing T. T can be FUNCTION_DECL for instantiated function
220 template, VAR_DECL for static member variable, or TYPE_DECL for
221 alias template (needed by instantiate_decl). */
222
223 static void
224 push_access_scope (tree t)
225 {
226 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
227 || TREE_CODE (t) == TYPE_DECL);
228
229 if (DECL_FRIEND_CONTEXT (t))
230 push_nested_class (DECL_FRIEND_CONTEXT (t));
231 else if (DECL_CLASS_SCOPE_P (t))
232 push_nested_class (DECL_CONTEXT (t));
233 else
234 push_to_top_level ();
235
236 if (TREE_CODE (t) == FUNCTION_DECL)
237 {
238 saved_access_scope = tree_cons
239 (NULL_TREE, current_function_decl, saved_access_scope);
240 current_function_decl = t;
241 }
242 }
243
244 /* Restore the scope set up by push_access_scope. T is the node we
245 are processing. */
246
247 static void
248 pop_access_scope (tree t)
249 {
250 if (TREE_CODE (t) == FUNCTION_DECL)
251 {
252 current_function_decl = TREE_VALUE (saved_access_scope);
253 saved_access_scope = TREE_CHAIN (saved_access_scope);
254 }
255
256 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
257 pop_nested_class ();
258 else
259 pop_from_top_level ();
260 }
261
262 /* Do any processing required when DECL (a member template
263 declaration) is finished. Returns the TEMPLATE_DECL corresponding
264 to DECL, unless it is a specialization, in which case the DECL
265 itself is returned. */
266
267 tree
268 finish_member_template_decl (tree decl)
269 {
270 if (decl == error_mark_node)
271 return error_mark_node;
272
273 gcc_assert (DECL_P (decl));
274
275 if (TREE_CODE (decl) == TYPE_DECL)
276 {
277 tree type;
278
279 type = TREE_TYPE (decl);
280 if (type == error_mark_node)
281 return error_mark_node;
282 if (MAYBE_CLASS_TYPE_P (type)
283 && CLASSTYPE_TEMPLATE_INFO (type)
284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
285 {
286 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
287 check_member_template (tmpl);
288 return tmpl;
289 }
290 return NULL_TREE;
291 }
292 else if (TREE_CODE (decl) == FIELD_DECL)
293 error ("data member %qD cannot be a member template", decl);
294 else if (DECL_TEMPLATE_INFO (decl))
295 {
296 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
297 {
298 check_member_template (DECL_TI_TEMPLATE (decl));
299 return DECL_TI_TEMPLATE (decl);
300 }
301 else
302 return decl;
303 }
304 else
305 error ("invalid member template declaration %qD", decl);
306
307 return error_mark_node;
308 }
309
310 /* Create a template info node. */
311
312 tree
313 build_template_info (tree template_decl, tree template_args)
314 {
315 tree result = make_node (TEMPLATE_INFO);
316 TI_TEMPLATE (result) = template_decl;
317 TI_ARGS (result) = template_args;
318 return result;
319 }
320
321 /* Return the template info node corresponding to T, whatever T is. */
322
323 tree
324 get_template_info (const_tree t)
325 {
326 tree tinfo = NULL_TREE;
327
328 if (!t || t == error_mark_node)
329 return NULL;
330
331 if (TREE_CODE (t) == NAMESPACE_DECL)
332 return NULL;
333
334 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
335 tinfo = DECL_TEMPLATE_INFO (t);
336
337 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
338 t = TREE_TYPE (t);
339
340 if (OVERLOAD_TYPE_P (t))
341 tinfo = TYPE_TEMPLATE_INFO (t);
342 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
343 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
344
345 return tinfo;
346 }
347
348 /* Returns the template nesting level of the indicated class TYPE.
349
350 For example, in:
351 template <class T>
352 struct A
353 {
354 template <class U>
355 struct B {};
356 };
357
358 A<T>::B<U> has depth two, while A<T> has depth one.
359 Both A<T>::B<int> and A<int>::B<U> have depth one, if
360 they are instantiations, not specializations.
361
362 This function is guaranteed to return 0 if passed NULL_TREE so
363 that, for example, `template_class_depth (current_class_type)' is
364 always safe. */
365
366 int
367 template_class_depth (tree type)
368 {
369 int depth;
370
371 for (depth = 0;
372 type && TREE_CODE (type) != NAMESPACE_DECL;
373 type = (TREE_CODE (type) == FUNCTION_DECL)
374 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
375 {
376 tree tinfo = get_template_info (type);
377
378 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
380 ++depth;
381 }
382
383 return depth;
384 }
385
386 /* Subroutine of maybe_begin_member_template_processing.
387 Returns true if processing DECL needs us to push template parms. */
388
389 static bool
390 inline_needs_template_parms (tree decl, bool nsdmi)
391 {
392 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
393 return false;
394
395 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
396 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
397 }
398
399 /* Subroutine of maybe_begin_member_template_processing.
400 Push the template parms in PARMS, starting from LEVELS steps into the
401 chain, and ending at the beginning, since template parms are listed
402 innermost first. */
403
404 static void
405 push_inline_template_parms_recursive (tree parmlist, int levels)
406 {
407 tree parms = TREE_VALUE (parmlist);
408 int i;
409
410 if (levels > 1)
411 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
412
413 ++processing_template_decl;
414 current_template_parms
415 = tree_cons (size_int (processing_template_decl),
416 parms, current_template_parms);
417 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
418
419 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
420 NULL);
421 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
422 {
423 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
424
425 if (error_operand_p (parm))
426 continue;
427
428 gcc_assert (DECL_P (parm));
429
430 switch (TREE_CODE (parm))
431 {
432 case TYPE_DECL:
433 case TEMPLATE_DECL:
434 pushdecl (parm);
435 break;
436
437 case PARM_DECL:
438 {
439 /* Make a CONST_DECL as is done in process_template_parm.
440 It is ugly that we recreate this here; the original
441 version built in process_template_parm is no longer
442 available. */
443 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
444 CONST_DECL, DECL_NAME (parm),
445 TREE_TYPE (parm));
446 DECL_ARTIFICIAL (decl) = 1;
447 TREE_CONSTANT (decl) = 1;
448 TREE_READONLY (decl) = 1;
449 DECL_INITIAL (decl) = DECL_INITIAL (parm);
450 SET_DECL_TEMPLATE_PARM_P (decl);
451 pushdecl (decl);
452 }
453 break;
454
455 default:
456 gcc_unreachable ();
457 }
458 }
459 }
460
461 /* Restore the template parameter context for a member template, a
462 friend template defined in a class definition, or a non-template
463 member of template class. */
464
465 void
466 maybe_begin_member_template_processing (tree decl)
467 {
468 tree parms;
469 int levels = 0;
470 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471
472 if (nsdmi)
473 {
474 tree ctx = DECL_CONTEXT (decl);
475 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
476 /* Disregard full specializations (c++/60999). */
477 && uses_template_parms (ctx)
478 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
479 }
480
481 if (inline_needs_template_parms (decl, nsdmi))
482 {
483 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
484 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485
486 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 {
488 --levels;
489 parms = TREE_CHAIN (parms);
490 }
491
492 push_inline_template_parms_recursive (parms, levels);
493 }
494
495 /* Remember how many levels of template parameters we pushed so that
496 we can pop them later. */
497 inline_parm_levels.safe_push (levels);
498 }
499
500 /* Undo the effects of maybe_begin_member_template_processing. */
501
502 void
503 maybe_end_member_template_processing (void)
504 {
505 int i;
506 int last;
507
508 if (inline_parm_levels.length () == 0)
509 return;
510
511 last = inline_parm_levels.pop ();
512 for (i = 0; i < last; ++i)
513 {
514 --processing_template_decl;
515 current_template_parms = TREE_CHAIN (current_template_parms);
516 poplevel (0, 0, 0);
517 }
518 }
519
520 /* Return a new template argument vector which contains all of ARGS,
521 but has as its innermost set of arguments the EXTRA_ARGS. */
522
523 static tree
524 add_to_template_args (tree args, tree extra_args)
525 {
526 tree new_args;
527 int extra_depth;
528 int i;
529 int j;
530
531 if (args == NULL_TREE || extra_args == error_mark_node)
532 return extra_args;
533
534 extra_depth = TMPL_ARGS_DEPTH (extra_args);
535 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536
537 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
538 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539
540 for (j = 1; j <= extra_depth; ++j, ++i)
541 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542
543 return new_args;
544 }
545
546 /* Like add_to_template_args, but only the outermost ARGS are added to
547 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
548 (EXTRA_ARGS) levels are added. This function is used to combine
549 the template arguments from a partial instantiation with the
550 template arguments used to attain the full instantiation from the
551 partial instantiation. */
552
553 static tree
554 add_outermost_template_args (tree args, tree extra_args)
555 {
556 tree new_args;
557
558 /* If there are more levels of EXTRA_ARGS than there are ARGS,
559 something very fishy is going on. */
560 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561
562 /* If *all* the new arguments will be the EXTRA_ARGS, just return
563 them. */
564 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
565 return extra_args;
566
567 /* For the moment, we make ARGS look like it contains fewer levels. */
568 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569
570 new_args = add_to_template_args (args, extra_args);
571
572 /* Now, we restore ARGS to its full dimensions. */
573 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574
575 return new_args;
576 }
577
578 /* Return the N levels of innermost template arguments from the ARGS. */
579
580 tree
581 get_innermost_template_args (tree args, int n)
582 {
583 tree new_args;
584 int extra_levels;
585 int i;
586
587 gcc_assert (n >= 0);
588
589 /* If N is 1, just return the innermost set of template arguments. */
590 if (n == 1)
591 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592
593 /* If we're not removing anything, just return the arguments we were
594 given. */
595 extra_levels = TMPL_ARGS_DEPTH (args) - n;
596 gcc_assert (extra_levels >= 0);
597 if (extra_levels == 0)
598 return args;
599
600 /* Make a new set of arguments, not containing the outer arguments. */
601 new_args = make_tree_vec (n);
602 for (i = 1; i <= n; ++i)
603 SET_TMPL_ARGS_LEVEL (new_args, i,
604 TMPL_ARGS_LEVEL (args, i + extra_levels));
605
606 return new_args;
607 }
608
609 /* The inverse of get_innermost_template_args: Return all but the innermost
610 EXTRA_LEVELS levels of template arguments from the ARGS. */
611
612 static tree
613 strip_innermost_template_args (tree args, int extra_levels)
614 {
615 tree new_args;
616 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
617 int i;
618
619 gcc_assert (n >= 0);
620
621 /* If N is 1, just return the outermost set of template arguments. */
622 if (n == 1)
623 return TMPL_ARGS_LEVEL (args, 1);
624
625 /* If we're not removing anything, just return the arguments we were
626 given. */
627 gcc_assert (extra_levels >= 0);
628 if (extra_levels == 0)
629 return args;
630
631 /* Make a new set of arguments, not containing the inner arguments. */
632 new_args = make_tree_vec (n);
633 for (i = 1; i <= n; ++i)
634 SET_TMPL_ARGS_LEVEL (new_args, i,
635 TMPL_ARGS_LEVEL (args, i));
636
637 return new_args;
638 }
639
640 /* We've got a template header coming up; push to a new level for storing
641 the parms. */
642
643 void
644 begin_template_parm_list (void)
645 {
646 /* We use a non-tag-transparent scope here, which causes pushtag to
647 put tags in this scope, rather than in the enclosing class or
648 namespace scope. This is the right thing, since we want
649 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
650 global template class, push_template_decl handles putting the
651 TEMPLATE_DECL into top-level scope. For a nested template class,
652 e.g.:
653
654 template <class T> struct S1 {
655 template <class T> struct S2 {};
656 };
657
658 pushtag contains special code to call pushdecl_with_scope on the
659 TEMPLATE_DECL for S2. */
660 begin_scope (sk_template_parms, NULL);
661 ++processing_template_decl;
662 ++processing_template_parmlist;
663 note_template_header (0);
664 }
665
666 /* This routine is called when a specialization is declared. If it is
667 invalid to declare a specialization here, an error is reported and
668 false is returned, otherwise this routine will return true. */
669
670 static bool
671 check_specialization_scope (void)
672 {
673 tree scope = current_scope ();
674
675 /* [temp.expl.spec]
676
677 An explicit specialization shall be declared in the namespace of
678 which the template is a member, or, for member templates, in the
679 namespace of which the enclosing class or enclosing class
680 template is a member. An explicit specialization of a member
681 function, member class or static data member of a class template
682 shall be declared in the namespace of which the class template
683 is a member. */
684 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
685 {
686 error ("explicit specialization in non-namespace scope %qD", scope);
687 return false;
688 }
689
690 /* [temp.expl.spec]
691
692 In an explicit specialization declaration for a member of a class
693 template or a member template that appears in namespace scope,
694 the member template and some of its enclosing class templates may
695 remain unspecialized, except that the declaration shall not
696 explicitly specialize a class member template if its enclosing
697 class templates are not explicitly specialized as well. */
698 if (current_template_parms)
699 {
700 error ("enclosing class templates are not explicitly specialized");
701 return false;
702 }
703
704 return true;
705 }
706
707 /* We've just seen template <>. */
708
709 bool
710 begin_specialization (void)
711 {
712 begin_scope (sk_template_spec, NULL);
713 note_template_header (1);
714 return check_specialization_scope ();
715 }
716
717 /* Called at then end of processing a declaration preceded by
718 template<>. */
719
720 void
721 end_specialization (void)
722 {
723 finish_scope ();
724 reset_specialization ();
725 }
726
727 /* Any template <>'s that we have seen thus far are not referring to a
728 function specialization. */
729
730 void
731 reset_specialization (void)
732 {
733 processing_specialization = 0;
734 template_header_count = 0;
735 }
736
737 /* We've just seen a template header. If SPECIALIZATION is nonzero,
738 it was of the form template <>. */
739
740 static void
741 note_template_header (int specialization)
742 {
743 processing_specialization = specialization;
744 template_header_count++;
745 }
746
747 /* We're beginning an explicit instantiation. */
748
749 void
750 begin_explicit_instantiation (void)
751 {
752 gcc_assert (!processing_explicit_instantiation);
753 processing_explicit_instantiation = true;
754 }
755
756
757 void
758 end_explicit_instantiation (void)
759 {
760 gcc_assert (processing_explicit_instantiation);
761 processing_explicit_instantiation = false;
762 }
763
764 /* An explicit specialization or partial specialization of TMPL is being
765 declared. Check that the namespace in which the specialization is
766 occurring is permissible. Returns false iff it is invalid to
767 specialize TMPL in the current namespace. */
768
769 static bool
770 check_specialization_namespace (tree tmpl)
771 {
772 tree tpl_ns = decl_namespace_context (tmpl);
773
774 /* [tmpl.expl.spec]
775
776 An explicit specialization shall be declared in the namespace of
777 which the template is a member, or, for member templates, in the
778 namespace of which the enclosing class or enclosing class
779 template is a member. An explicit specialization of a member
780 function, member class or static data member of a class template
781 shall be declared in the namespace of which the class template is
782 a member. */
783 if (current_scope() != DECL_CONTEXT (tmpl)
784 && !at_namespace_scope_p ())
785 {
786 error ("specialization of %qD must appear at namespace scope", tmpl);
787 return false;
788 }
789 if (is_associated_namespace (current_namespace, tpl_ns))
790 /* Same or super-using namespace. */
791 return true;
792 else
793 {
794 permerror (input_location, "specialization of %qD in different namespace", tmpl);
795 permerror (input_location, " from definition of %q+#D", tmpl);
796 return false;
797 }
798 }
799
800 /* SPEC is an explicit instantiation. Check that it is valid to
801 perform this explicit instantiation in the current namespace. */
802
803 static void
804 check_explicit_instantiation_namespace (tree spec)
805 {
806 tree ns;
807
808 /* DR 275: An explicit instantiation shall appear in an enclosing
809 namespace of its template. */
810 ns = decl_namespace_context (spec);
811 if (!is_ancestor (current_namespace, ns))
812 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
813 "(which does not enclose namespace %qD)",
814 spec, current_namespace, ns);
815 }
816
817 /* The TYPE is being declared. If it is a template type, that means it
818 is a partial specialization. Do appropriate error-checking. */
819
820 tree
821 maybe_process_partial_specialization (tree type)
822 {
823 tree context;
824
825 if (type == error_mark_node)
826 return error_mark_node;
827
828 /* A lambda that appears in specialization context is not itself a
829 specialization. */
830 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
831 return type;
832
833 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
834 {
835 error ("name of class shadows template template parameter %qD",
836 TYPE_NAME (type));
837 return error_mark_node;
838 }
839
840 context = TYPE_CONTEXT (type);
841
842 if (TYPE_ALIAS_P (type))
843 {
844 if (TYPE_TEMPLATE_INFO (type)
845 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
846 error ("specialization of alias template %qD",
847 TYPE_TI_TEMPLATE (type));
848 else
849 error ("explicit specialization of non-template %qT", type);
850 return error_mark_node;
851 }
852 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
853 {
854 /* This is for ordinary explicit specialization and partial
855 specialization of a template class such as:
856
857 template <> class C<int>;
858
859 or:
860
861 template <class T> class C<T*>;
862
863 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
864
865 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
866 && !COMPLETE_TYPE_P (type))
867 {
868 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
869 && !at_namespace_scope_p ())
870 return error_mark_node;
871 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
872 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
873 if (processing_template_decl)
874 {
875 if (push_template_decl (TYPE_MAIN_DECL (type))
876 == error_mark_node)
877 return error_mark_node;
878 }
879 }
880 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
881 error ("specialization of %qT after instantiation", type);
882 else if (errorcount && !processing_specialization
883 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
884 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
885 /* Trying to define a specialization either without a template<> header
886 or in an inappropriate place. We've already given an error, so just
887 bail now so we don't actually define the specialization. */
888 return error_mark_node;
889 }
890 else if (CLASS_TYPE_P (type)
891 && !CLASSTYPE_USE_TEMPLATE (type)
892 && CLASSTYPE_TEMPLATE_INFO (type)
893 && context && CLASS_TYPE_P (context)
894 && CLASSTYPE_TEMPLATE_INFO (context))
895 {
896 /* This is for an explicit specialization of member class
897 template according to [temp.expl.spec/18]:
898
899 template <> template <class U> class C<int>::D;
900
901 The context `C<int>' must be an implicit instantiation.
902 Otherwise this is just a member class template declared
903 earlier like:
904
905 template <> class C<int> { template <class U> class D; };
906 template <> template <class U> class C<int>::D;
907
908 In the first case, `C<int>::D' is a specialization of `C<T>::D'
909 while in the second case, `C<int>::D' is a primary template
910 and `C<T>::D' may not exist. */
911
912 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
913 && !COMPLETE_TYPE_P (type))
914 {
915 tree t;
916 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
917
918 if (current_namespace
919 != decl_namespace_context (tmpl))
920 {
921 permerror (input_location, "specializing %q#T in different namespace", type);
922 permerror (input_location, " from definition of %q+#D", tmpl);
923 }
924
925 /* Check for invalid specialization after instantiation:
926
927 template <> template <> class C<int>::D<int>;
928 template <> template <class U> class C<int>::D; */
929
930 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
931 t; t = TREE_CHAIN (t))
932 {
933 tree inst = TREE_VALUE (t);
934 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
935 || !COMPLETE_OR_OPEN_TYPE_P (inst))
936 {
937 /* We already have a full specialization of this partial
938 instantiation, or a full specialization has been
939 looked up but not instantiated. Reassign it to the
940 new member specialization template. */
941 spec_entry elt;
942 spec_entry *entry;
943
944 elt.tmpl = most_general_template (tmpl);
945 elt.args = CLASSTYPE_TI_ARGS (inst);
946 elt.spec = inst;
947
948 type_specializations->remove_elt (&elt);
949
950 elt.tmpl = tmpl;
951 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
952
953 spec_entry **slot
954 = type_specializations->find_slot (&elt, INSERT);
955 entry = ggc_alloc<spec_entry> ();
956 *entry = elt;
957 *slot = entry;
958 }
959 else
960 /* But if we've had an implicit instantiation, that's a
961 problem ([temp.expl.spec]/6). */
962 error ("specialization %qT after instantiation %qT",
963 type, inst);
964 }
965
966 /* Mark TYPE as a specialization. And as a result, we only
967 have one level of template argument for the innermost
968 class template. */
969 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
970 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
971 CLASSTYPE_TI_ARGS (type)
972 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
973 }
974 }
975 else if (processing_specialization)
976 {
977 /* Someday C++0x may allow for enum template specialization. */
978 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
979 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
980 pedwarn (input_location, OPT_Wpedantic, "template specialization "
981 "of %qD not allowed by ISO C++", type);
982 else
983 {
984 error ("explicit specialization of non-template %qT", type);
985 return error_mark_node;
986 }
987 }
988
989 return type;
990 }
991
992 /* Returns nonzero if we can optimize the retrieval of specializations
993 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
994 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
995
996 static inline bool
997 optimize_specialization_lookup_p (tree tmpl)
998 {
999 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1000 && DECL_CLASS_SCOPE_P (tmpl)
1001 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1002 parameter. */
1003 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1004 /* The optimized lookup depends on the fact that the
1005 template arguments for the member function template apply
1006 purely to the containing class, which is not true if the
1007 containing class is an explicit or partial
1008 specialization. */
1009 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1010 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1011 && !DECL_CONV_FN_P (tmpl)
1012 /* It is possible to have a template that is not a member
1013 template and is not a member of a template class:
1014
1015 template <typename T>
1016 struct S { friend A::f(); };
1017
1018 Here, the friend function is a template, but the context does
1019 not have template information. The optimized lookup relies
1020 on having ARGS be the template arguments for both the class
1021 and the function template. */
1022 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1023 }
1024
1025 /* Retrieve the specialization (in the sense of [temp.spec] - a
1026 specialization is either an instantiation or an explicit
1027 specialization) of TMPL for the given template ARGS. If there is
1028 no such specialization, return NULL_TREE. The ARGS are a vector of
1029 arguments, or a vector of vectors of arguments, in the case of
1030 templates with more than one level of parameters.
1031
1032 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1033 then we search for a partial specialization matching ARGS. This
1034 parameter is ignored if TMPL is not a class template.
1035
1036 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1037 result is a NONTYPE_ARGUMENT_PACK. */
1038
1039 static tree
1040 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1041 {
1042 if (tmpl == NULL_TREE)
1043 return NULL_TREE;
1044
1045 if (args == error_mark_node)
1046 return NULL_TREE;
1047
1048 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1049 || TREE_CODE (tmpl) == FIELD_DECL);
1050
1051 /* There should be as many levels of arguments as there are
1052 levels of parameters. */
1053 gcc_assert (TMPL_ARGS_DEPTH (args)
1054 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1055 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1056 : template_class_depth (DECL_CONTEXT (tmpl))));
1057
1058 #ifdef ENABLE_CHECKING
1059 /* We should have gone through coerce_template_parms by now. */
1060 ++processing_template_decl;
1061 if (!any_dependent_template_arguments_p (args))
1062 gcc_assert (strip_typedefs_expr (args, NULL) == args);
1063 --processing_template_decl;
1064 #endif
1065
1066 if (optimize_specialization_lookup_p (tmpl))
1067 {
1068 tree class_template;
1069 tree class_specialization;
1070 vec<tree, va_gc> *methods;
1071 tree fns;
1072 int idx;
1073
1074 /* The template arguments actually apply to the containing
1075 class. Find the class specialization with those
1076 arguments. */
1077 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1078 class_specialization
1079 = retrieve_specialization (class_template, args, 0);
1080 if (!class_specialization)
1081 return NULL_TREE;
1082 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1083 for the specialization. */
1084 idx = class_method_index_for_fn (class_specialization, tmpl);
1085 if (idx == -1)
1086 return NULL_TREE;
1087 /* Iterate through the methods with the indicated name, looking
1088 for the one that has an instance of TMPL. */
1089 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1090 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1091 {
1092 tree fn = OVL_CURRENT (fns);
1093 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1094 /* using-declarations can add base methods to the method vec,
1095 and we don't want those here. */
1096 && DECL_CONTEXT (fn) == class_specialization)
1097 return fn;
1098 }
1099 return NULL_TREE;
1100 }
1101 else
1102 {
1103 spec_entry *found;
1104 spec_entry elt;
1105 hash_table<spec_hasher> *specializations;
1106
1107 elt.tmpl = tmpl;
1108 elt.args = args;
1109 elt.spec = NULL_TREE;
1110
1111 if (DECL_CLASS_TEMPLATE_P (tmpl))
1112 specializations = type_specializations;
1113 else
1114 specializations = decl_specializations;
1115
1116 if (hash == 0)
1117 hash = spec_hasher::hash (&elt);
1118 found = specializations->find_with_hash (&elt, hash);
1119 if (found)
1120 return found->spec;
1121 }
1122
1123 return NULL_TREE;
1124 }
1125
1126 /* Like retrieve_specialization, but for local declarations. */
1127
1128 static tree
1129 retrieve_local_specialization (tree tmpl)
1130 {
1131 if (local_specializations == NULL)
1132 return NULL_TREE;
1133
1134 tree *slot = local_specializations->get (tmpl);
1135 return slot ? *slot : NULL_TREE;
1136 }
1137
1138 /* Returns nonzero iff DECL is a specialization of TMPL. */
1139
1140 int
1141 is_specialization_of (tree decl, tree tmpl)
1142 {
1143 tree t;
1144
1145 if (TREE_CODE (decl) == FUNCTION_DECL)
1146 {
1147 for (t = decl;
1148 t != NULL_TREE;
1149 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1150 if (t == tmpl)
1151 return 1;
1152 }
1153 else
1154 {
1155 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1156
1157 for (t = TREE_TYPE (decl);
1158 t != NULL_TREE;
1159 t = CLASSTYPE_USE_TEMPLATE (t)
1160 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1161 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1162 return 1;
1163 }
1164
1165 return 0;
1166 }
1167
1168 /* Returns nonzero iff DECL is a specialization of friend declaration
1169 FRIEND_DECL according to [temp.friend]. */
1170
1171 bool
1172 is_specialization_of_friend (tree decl, tree friend_decl)
1173 {
1174 bool need_template = true;
1175 int template_depth;
1176
1177 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1178 || TREE_CODE (decl) == TYPE_DECL);
1179
1180 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1181 of a template class, we want to check if DECL is a specialization
1182 if this. */
1183 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1184 && DECL_TEMPLATE_INFO (friend_decl)
1185 && !DECL_USE_TEMPLATE (friend_decl))
1186 {
1187 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1188 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1189 need_template = false;
1190 }
1191 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1192 && !PRIMARY_TEMPLATE_P (friend_decl))
1193 need_template = false;
1194
1195 /* There is nothing to do if this is not a template friend. */
1196 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1197 return false;
1198
1199 if (is_specialization_of (decl, friend_decl))
1200 return true;
1201
1202 /* [temp.friend/6]
1203 A member of a class template may be declared to be a friend of a
1204 non-template class. In this case, the corresponding member of
1205 every specialization of the class template is a friend of the
1206 class granting friendship.
1207
1208 For example, given a template friend declaration
1209
1210 template <class T> friend void A<T>::f();
1211
1212 the member function below is considered a friend
1213
1214 template <> struct A<int> {
1215 void f();
1216 };
1217
1218 For this type of template friend, TEMPLATE_DEPTH below will be
1219 nonzero. To determine if DECL is a friend of FRIEND, we first
1220 check if the enclosing class is a specialization of another. */
1221
1222 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1223 if (template_depth
1224 && DECL_CLASS_SCOPE_P (decl)
1225 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1226 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1227 {
1228 /* Next, we check the members themselves. In order to handle
1229 a few tricky cases, such as when FRIEND_DECL's are
1230
1231 template <class T> friend void A<T>::g(T t);
1232 template <class T> template <T t> friend void A<T>::h();
1233
1234 and DECL's are
1235
1236 void A<int>::g(int);
1237 template <int> void A<int>::h();
1238
1239 we need to figure out ARGS, the template arguments from
1240 the context of DECL. This is required for template substitution
1241 of `T' in the function parameter of `g' and template parameter
1242 of `h' in the above examples. Here ARGS corresponds to `int'. */
1243
1244 tree context = DECL_CONTEXT (decl);
1245 tree args = NULL_TREE;
1246 int current_depth = 0;
1247
1248 while (current_depth < template_depth)
1249 {
1250 if (CLASSTYPE_TEMPLATE_INFO (context))
1251 {
1252 if (current_depth == 0)
1253 args = TYPE_TI_ARGS (context);
1254 else
1255 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1256 current_depth++;
1257 }
1258 context = TYPE_CONTEXT (context);
1259 }
1260
1261 if (TREE_CODE (decl) == FUNCTION_DECL)
1262 {
1263 bool is_template;
1264 tree friend_type;
1265 tree decl_type;
1266 tree friend_args_type;
1267 tree decl_args_type;
1268
1269 /* Make sure that both DECL and FRIEND_DECL are templates or
1270 non-templates. */
1271 is_template = DECL_TEMPLATE_INFO (decl)
1272 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1273 if (need_template ^ is_template)
1274 return false;
1275 else if (is_template)
1276 {
1277 /* If both are templates, check template parameter list. */
1278 tree friend_parms
1279 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1280 args, tf_none);
1281 if (!comp_template_parms
1282 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1283 friend_parms))
1284 return false;
1285
1286 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1287 }
1288 else
1289 decl_type = TREE_TYPE (decl);
1290
1291 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1292 tf_none, NULL_TREE);
1293 if (friend_type == error_mark_node)
1294 return false;
1295
1296 /* Check if return types match. */
1297 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1298 return false;
1299
1300 /* Check if function parameter types match, ignoring the
1301 `this' parameter. */
1302 friend_args_type = TYPE_ARG_TYPES (friend_type);
1303 decl_args_type = TYPE_ARG_TYPES (decl_type);
1304 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1305 friend_args_type = TREE_CHAIN (friend_args_type);
1306 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1307 decl_args_type = TREE_CHAIN (decl_args_type);
1308
1309 return compparms (decl_args_type, friend_args_type);
1310 }
1311 else
1312 {
1313 /* DECL is a TYPE_DECL */
1314 bool is_template;
1315 tree decl_type = TREE_TYPE (decl);
1316
1317 /* Make sure that both DECL and FRIEND_DECL are templates or
1318 non-templates. */
1319 is_template
1320 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1321 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1322
1323 if (need_template ^ is_template)
1324 return false;
1325 else if (is_template)
1326 {
1327 tree friend_parms;
1328 /* If both are templates, check the name of the two
1329 TEMPLATE_DECL's first because is_friend didn't. */
1330 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1331 != DECL_NAME (friend_decl))
1332 return false;
1333
1334 /* Now check template parameter list. */
1335 friend_parms
1336 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1337 args, tf_none);
1338 return comp_template_parms
1339 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1340 friend_parms);
1341 }
1342 else
1343 return (DECL_NAME (decl)
1344 == DECL_NAME (friend_decl));
1345 }
1346 }
1347 return false;
1348 }
1349
1350 /* Register the specialization SPEC as a specialization of TMPL with
1351 the indicated ARGS. IS_FRIEND indicates whether the specialization
1352 is actually just a friend declaration. Returns SPEC, or an
1353 equivalent prior declaration, if available.
1354
1355 We also store instantiations of field packs in the hash table, even
1356 though they are not themselves templates, to make lookup easier. */
1357
1358 static tree
1359 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1360 hashval_t hash)
1361 {
1362 tree fn;
1363 spec_entry **slot = NULL;
1364 spec_entry elt;
1365
1366 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1367 || (TREE_CODE (tmpl) == FIELD_DECL
1368 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1369
1370 if (TREE_CODE (spec) == FUNCTION_DECL
1371 && uses_template_parms (DECL_TI_ARGS (spec)))
1372 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1373 register it; we want the corresponding TEMPLATE_DECL instead.
1374 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1375 the more obvious `uses_template_parms (spec)' to avoid problems
1376 with default function arguments. In particular, given
1377 something like this:
1378
1379 template <class T> void f(T t1, T t = T())
1380
1381 the default argument expression is not substituted for in an
1382 instantiation unless and until it is actually needed. */
1383 return spec;
1384
1385 if (optimize_specialization_lookup_p (tmpl))
1386 /* We don't put these specializations in the hash table, but we might
1387 want to give an error about a mismatch. */
1388 fn = retrieve_specialization (tmpl, args, 0);
1389 else
1390 {
1391 elt.tmpl = tmpl;
1392 elt.args = args;
1393 elt.spec = spec;
1394
1395 if (hash == 0)
1396 hash = spec_hasher::hash (&elt);
1397
1398 slot =
1399 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1400 if (*slot)
1401 fn = ((spec_entry *) *slot)->spec;
1402 else
1403 fn = NULL_TREE;
1404 }
1405
1406 /* We can sometimes try to re-register a specialization that we've
1407 already got. In particular, regenerate_decl_from_template calls
1408 duplicate_decls which will update the specialization list. But,
1409 we'll still get called again here anyhow. It's more convenient
1410 to simply allow this than to try to prevent it. */
1411 if (fn == spec)
1412 return spec;
1413 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1414 {
1415 if (DECL_TEMPLATE_INSTANTIATION (fn))
1416 {
1417 if (DECL_ODR_USED (fn)
1418 || DECL_EXPLICIT_INSTANTIATION (fn))
1419 {
1420 error ("specialization of %qD after instantiation",
1421 fn);
1422 return error_mark_node;
1423 }
1424 else
1425 {
1426 tree clone;
1427 /* This situation should occur only if the first
1428 specialization is an implicit instantiation, the
1429 second is an explicit specialization, and the
1430 implicit instantiation has not yet been used. That
1431 situation can occur if we have implicitly
1432 instantiated a member function and then specialized
1433 it later.
1434
1435 We can also wind up here if a friend declaration that
1436 looked like an instantiation turns out to be a
1437 specialization:
1438
1439 template <class T> void foo(T);
1440 class S { friend void foo<>(int) };
1441 template <> void foo(int);
1442
1443 We transform the existing DECL in place so that any
1444 pointers to it become pointers to the updated
1445 declaration.
1446
1447 If there was a definition for the template, but not
1448 for the specialization, we want this to look as if
1449 there were no definition, and vice versa. */
1450 DECL_INITIAL (fn) = NULL_TREE;
1451 duplicate_decls (spec, fn, is_friend);
1452 /* The call to duplicate_decls will have applied
1453 [temp.expl.spec]:
1454
1455 An explicit specialization of a function template
1456 is inline only if it is explicitly declared to be,
1457 and independently of whether its function template
1458 is.
1459
1460 to the primary function; now copy the inline bits to
1461 the various clones. */
1462 FOR_EACH_CLONE (clone, fn)
1463 {
1464 DECL_DECLARED_INLINE_P (clone)
1465 = DECL_DECLARED_INLINE_P (fn);
1466 DECL_SOURCE_LOCATION (clone)
1467 = DECL_SOURCE_LOCATION (fn);
1468 DECL_DELETED_FN (clone)
1469 = DECL_DELETED_FN (fn);
1470 }
1471 check_specialization_namespace (tmpl);
1472
1473 return fn;
1474 }
1475 }
1476 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1477 {
1478 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1479 /* Dup decl failed, but this is a new definition. Set the
1480 line number so any errors match this new
1481 definition. */
1482 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1483
1484 return fn;
1485 }
1486 }
1487 else if (fn)
1488 return duplicate_decls (spec, fn, is_friend);
1489
1490 /* A specialization must be declared in the same namespace as the
1491 template it is specializing. */
1492 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1493 && !check_specialization_namespace (tmpl))
1494 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1495
1496 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1497 {
1498 spec_entry *entry = ggc_alloc<spec_entry> ();
1499 gcc_assert (tmpl && args && spec);
1500 *entry = elt;
1501 *slot = entry;
1502 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1503 && PRIMARY_TEMPLATE_P (tmpl)
1504 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1505 || variable_template_p (tmpl))
1506 /* If TMPL is a forward declaration of a template function, keep a list
1507 of all specializations in case we need to reassign them to a friend
1508 template later in tsubst_friend_function.
1509
1510 Also keep a list of all variable template instantiations so that
1511 process_partial_specialization can check whether a later partial
1512 specialization would have used it. */
1513 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1514 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1515 }
1516
1517 return spec;
1518 }
1519
1520 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1521 TMPL and ARGS members, ignores SPEC. */
1522
1523 int comparing_specializations;
1524
1525 bool
1526 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1527 {
1528 int equal;
1529
1530 ++comparing_specializations;
1531 equal = (e1->tmpl == e2->tmpl
1532 && comp_template_args (e1->args, e2->args));
1533 --comparing_specializations;
1534
1535 return equal;
1536 }
1537
1538 /* Returns a hash for a template TMPL and template arguments ARGS. */
1539
1540 static hashval_t
1541 hash_tmpl_and_args (tree tmpl, tree args)
1542 {
1543 hashval_t val = DECL_UID (tmpl);
1544 return iterative_hash_template_arg (args, val);
1545 }
1546
1547 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1548 ignoring SPEC. */
1549
1550 hashval_t
1551 spec_hasher::hash (spec_entry *e)
1552 {
1553 return hash_tmpl_and_args (e->tmpl, e->args);
1554 }
1555
1556 /* Recursively calculate a hash value for a template argument ARG, for use
1557 in the hash tables of template specializations. */
1558
1559 hashval_t
1560 iterative_hash_template_arg (tree arg, hashval_t val)
1561 {
1562 unsigned HOST_WIDE_INT i;
1563 enum tree_code code;
1564 char tclass;
1565
1566 if (arg == NULL_TREE)
1567 return iterative_hash_object (arg, val);
1568
1569 if (!TYPE_P (arg))
1570 STRIP_NOPS (arg);
1571
1572 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1573 /* We can get one of these when re-hashing a previous entry in the middle
1574 of substituting into a pack expansion. Just look through it. */
1575 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1576
1577 code = TREE_CODE (arg);
1578 tclass = TREE_CODE_CLASS (code);
1579
1580 val = iterative_hash_object (code, val);
1581
1582 switch (code)
1583 {
1584 case ERROR_MARK:
1585 return val;
1586
1587 case IDENTIFIER_NODE:
1588 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1589
1590 case TREE_VEC:
1591 {
1592 int i, len = TREE_VEC_LENGTH (arg);
1593 for (i = 0; i < len; ++i)
1594 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1595 return val;
1596 }
1597
1598 case TYPE_PACK_EXPANSION:
1599 case EXPR_PACK_EXPANSION:
1600 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1601 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1602
1603 case TYPE_ARGUMENT_PACK:
1604 case NONTYPE_ARGUMENT_PACK:
1605 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1606
1607 case TREE_LIST:
1608 for (; arg; arg = TREE_CHAIN (arg))
1609 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1610 return val;
1611
1612 case OVERLOAD:
1613 for (; arg; arg = OVL_NEXT (arg))
1614 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1615 return val;
1616
1617 case CONSTRUCTOR:
1618 {
1619 tree field, value;
1620 iterative_hash_template_arg (TREE_TYPE (arg), val);
1621 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1622 {
1623 val = iterative_hash_template_arg (field, val);
1624 val = iterative_hash_template_arg (value, val);
1625 }
1626 return val;
1627 }
1628
1629 case PARM_DECL:
1630 if (!DECL_ARTIFICIAL (arg))
1631 {
1632 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1633 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1634 }
1635 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1636
1637 case TARGET_EXPR:
1638 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1639
1640 case PTRMEM_CST:
1641 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1642 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1643
1644 case TEMPLATE_PARM_INDEX:
1645 val = iterative_hash_template_arg
1646 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1647 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1648 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1649
1650 case TRAIT_EXPR:
1651 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1652 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1653 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1654
1655 case BASELINK:
1656 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1657 val);
1658 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1659 val);
1660
1661 case MODOP_EXPR:
1662 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1663 code = TREE_CODE (TREE_OPERAND (arg, 1));
1664 val = iterative_hash_object (code, val);
1665 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1666
1667 case LAMBDA_EXPR:
1668 /* A lambda can't appear in a template arg, but don't crash on
1669 erroneous input. */
1670 gcc_assert (seen_error ());
1671 return val;
1672
1673 case CAST_EXPR:
1674 case IMPLICIT_CONV_EXPR:
1675 case STATIC_CAST_EXPR:
1676 case REINTERPRET_CAST_EXPR:
1677 case CONST_CAST_EXPR:
1678 case DYNAMIC_CAST_EXPR:
1679 case NEW_EXPR:
1680 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1681 /* Now hash operands as usual. */
1682 break;
1683
1684 default:
1685 break;
1686 }
1687
1688 switch (tclass)
1689 {
1690 case tcc_type:
1691 if (alias_template_specialization_p (arg))
1692 {
1693 // We want an alias specialization that survived strip_typedefs
1694 // to hash differently from its TYPE_CANONICAL, to avoid hash
1695 // collisions that compare as different in template_args_equal.
1696 // These could be dependent specializations that strip_typedefs
1697 // left alone, or untouched specializations because
1698 // coerce_template_parms returns the unconverted template
1699 // arguments if it sees incomplete argument packs.
1700 tree ti = TYPE_TEMPLATE_INFO (arg);
1701 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1702 }
1703 if (TYPE_CANONICAL (arg))
1704 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1705 val);
1706 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1707 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1708 /* Otherwise just compare the types during lookup. */
1709 return val;
1710
1711 case tcc_declaration:
1712 case tcc_constant:
1713 return iterative_hash_expr (arg, val);
1714
1715 default:
1716 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1717 {
1718 unsigned n = cp_tree_operand_length (arg);
1719 for (i = 0; i < n; ++i)
1720 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1721 return val;
1722 }
1723 }
1724 gcc_unreachable ();
1725 return 0;
1726 }
1727
1728 /* Unregister the specialization SPEC as a specialization of TMPL.
1729 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1730 if the SPEC was listed as a specialization of TMPL.
1731
1732 Note that SPEC has been ggc_freed, so we can't look inside it. */
1733
1734 bool
1735 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1736 {
1737 spec_entry *entry;
1738 spec_entry elt;
1739
1740 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1741 elt.args = TI_ARGS (tinfo);
1742 elt.spec = NULL_TREE;
1743
1744 entry = decl_specializations->find (&elt);
1745 if (entry != NULL)
1746 {
1747 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1748 gcc_assert (new_spec != NULL_TREE);
1749 entry->spec = new_spec;
1750 return 1;
1751 }
1752
1753 return 0;
1754 }
1755
1756 /* Like register_specialization, but for local declarations. We are
1757 registering SPEC, an instantiation of TMPL. */
1758
1759 static void
1760 register_local_specialization (tree spec, tree tmpl)
1761 {
1762 local_specializations->put (tmpl, spec);
1763 }
1764
1765 /* TYPE is a class type. Returns true if TYPE is an explicitly
1766 specialized class. */
1767
1768 bool
1769 explicit_class_specialization_p (tree type)
1770 {
1771 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1772 return false;
1773 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1774 }
1775
1776 /* Print the list of functions at FNS, going through all the overloads
1777 for each element of the list. Alternatively, FNS can not be a
1778 TREE_LIST, in which case it will be printed together with all the
1779 overloads.
1780
1781 MORE and *STR should respectively be FALSE and NULL when the function
1782 is called from the outside. They are used internally on recursive
1783 calls. print_candidates manages the two parameters and leaves NULL
1784 in *STR when it ends. */
1785
1786 static void
1787 print_candidates_1 (tree fns, bool more, const char **str)
1788 {
1789 tree fn, fn2;
1790 char *spaces = NULL;
1791
1792 for (fn = fns; fn; fn = OVL_NEXT (fn))
1793 if (TREE_CODE (fn) == TREE_LIST)
1794 {
1795 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1796 print_candidates_1 (TREE_VALUE (fn2),
1797 TREE_CHAIN (fn2) || more, str);
1798 }
1799 else
1800 {
1801 tree cand = OVL_CURRENT (fn);
1802 if (!*str)
1803 {
1804 /* Pick the prefix string. */
1805 if (!more && !OVL_NEXT (fns))
1806 {
1807 inform (DECL_SOURCE_LOCATION (cand),
1808 "candidate is: %#D", cand);
1809 continue;
1810 }
1811
1812 *str = _("candidates are:");
1813 spaces = get_spaces (*str);
1814 }
1815 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1816 *str = spaces ? spaces : *str;
1817 }
1818
1819 if (!more)
1820 {
1821 free (spaces);
1822 *str = NULL;
1823 }
1824 }
1825
1826 /* Print the list of candidate FNS in an error message. FNS can also
1827 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1828
1829 void
1830 print_candidates (tree fns)
1831 {
1832 const char *str = NULL;
1833 print_candidates_1 (fns, false, &str);
1834 gcc_assert (str == NULL);
1835 }
1836
1837 /* Returns the template (one of the functions given by TEMPLATE_ID)
1838 which can be specialized to match the indicated DECL with the
1839 explicit template args given in TEMPLATE_ID. The DECL may be
1840 NULL_TREE if none is available. In that case, the functions in
1841 TEMPLATE_ID are non-members.
1842
1843 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1844 specialization of a member template.
1845
1846 The TEMPLATE_COUNT is the number of references to qualifying
1847 template classes that appeared in the name of the function. See
1848 check_explicit_specialization for a more accurate description.
1849
1850 TSK indicates what kind of template declaration (if any) is being
1851 declared. TSK_TEMPLATE indicates that the declaration given by
1852 DECL, though a FUNCTION_DECL, has template parameters, and is
1853 therefore a template function.
1854
1855 The template args (those explicitly specified and those deduced)
1856 are output in a newly created vector *TARGS_OUT.
1857
1858 If it is impossible to determine the result, an error message is
1859 issued. The error_mark_node is returned to indicate failure. */
1860
1861 static tree
1862 determine_specialization (tree template_id,
1863 tree decl,
1864 tree* targs_out,
1865 int need_member_template,
1866 int template_count,
1867 tmpl_spec_kind tsk)
1868 {
1869 tree fns;
1870 tree targs;
1871 tree explicit_targs;
1872 tree candidates = NULL_TREE;
1873 /* A TREE_LIST of templates of which DECL may be a specialization.
1874 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1875 corresponding TREE_PURPOSE is the set of template arguments that,
1876 when used to instantiate the template, would produce a function
1877 with the signature of DECL. */
1878 tree templates = NULL_TREE;
1879 int header_count;
1880 cp_binding_level *b;
1881
1882 *targs_out = NULL_TREE;
1883
1884 if (template_id == error_mark_node || decl == error_mark_node)
1885 return error_mark_node;
1886
1887 /* We shouldn't be specializing a member template of an
1888 unspecialized class template; we already gave an error in
1889 check_specialization_scope, now avoid crashing. */
1890 if (template_count && DECL_CLASS_SCOPE_P (decl)
1891 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1892 {
1893 gcc_assert (errorcount);
1894 return error_mark_node;
1895 }
1896
1897 fns = TREE_OPERAND (template_id, 0);
1898 explicit_targs = TREE_OPERAND (template_id, 1);
1899
1900 if (fns == error_mark_node)
1901 return error_mark_node;
1902
1903 /* Check for baselinks. */
1904 if (BASELINK_P (fns))
1905 fns = BASELINK_FUNCTIONS (fns);
1906
1907 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
1908 {
1909 error ("%qD is not a function template", fns);
1910 return error_mark_node;
1911 }
1912 else if (VAR_P (decl) && !variable_template_p (fns))
1913 {
1914 error ("%qD is not a variable template", fns);
1915 return error_mark_node;
1916 }
1917
1918 /* Count the number of template headers specified for this
1919 specialization. */
1920 header_count = 0;
1921 for (b = current_binding_level;
1922 b->kind == sk_template_parms;
1923 b = b->level_chain)
1924 ++header_count;
1925
1926 if (variable_template_p (fns))
1927 {
1928 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
1929 targs = coerce_template_parms (parms, explicit_targs, fns,
1930 tf_warning_or_error,
1931 /*req_all*/true, /*use_defarg*/true);
1932 templates = tree_cons (targs, fns, templates);
1933 }
1934 else for (; fns; fns = OVL_NEXT (fns))
1935 {
1936 tree fn = OVL_CURRENT (fns);
1937
1938 if (TREE_CODE (fn) == TEMPLATE_DECL)
1939 {
1940 tree decl_arg_types;
1941 tree fn_arg_types;
1942 tree insttype;
1943
1944 /* In case of explicit specialization, we need to check if
1945 the number of template headers appearing in the specialization
1946 is correct. This is usually done in check_explicit_specialization,
1947 but the check done there cannot be exhaustive when specializing
1948 member functions. Consider the following code:
1949
1950 template <> void A<int>::f(int);
1951 template <> template <> void A<int>::f(int);
1952
1953 Assuming that A<int> is not itself an explicit specialization
1954 already, the first line specializes "f" which is a non-template
1955 member function, whilst the second line specializes "f" which
1956 is a template member function. So both lines are syntactically
1957 correct, and check_explicit_specialization does not reject
1958 them.
1959
1960 Here, we can do better, as we are matching the specialization
1961 against the declarations. We count the number of template
1962 headers, and we check if they match TEMPLATE_COUNT + 1
1963 (TEMPLATE_COUNT is the number of qualifying template classes,
1964 plus there must be another header for the member template
1965 itself).
1966
1967 Notice that if header_count is zero, this is not a
1968 specialization but rather a template instantiation, so there
1969 is no check we can perform here. */
1970 if (header_count && header_count != template_count + 1)
1971 continue;
1972
1973 /* Check that the number of template arguments at the
1974 innermost level for DECL is the same as for FN. */
1975 if (current_binding_level->kind == sk_template_parms
1976 && !current_binding_level->explicit_spec_p
1977 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1978 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1979 (current_template_parms))))
1980 continue;
1981
1982 /* DECL might be a specialization of FN. */
1983 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1984 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1985
1986 /* For a non-static member function, we need to make sure
1987 that the const qualification is the same. Since
1988 get_bindings does not try to merge the "this" parameter,
1989 we must do the comparison explicitly. */
1990 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1991 && !same_type_p (TREE_VALUE (fn_arg_types),
1992 TREE_VALUE (decl_arg_types)))
1993 continue;
1994
1995 /* Skip the "this" parameter and, for constructors of
1996 classes with virtual bases, the VTT parameter. A
1997 full specialization of a constructor will have a VTT
1998 parameter, but a template never will. */
1999 decl_arg_types
2000 = skip_artificial_parms_for (decl, decl_arg_types);
2001 fn_arg_types
2002 = skip_artificial_parms_for (fn, fn_arg_types);
2003
2004 /* Function templates cannot be specializations; there are
2005 no partial specializations of functions. Therefore, if
2006 the type of DECL does not match FN, there is no
2007 match. */
2008 if (tsk == tsk_template)
2009 {
2010 if (compparms (fn_arg_types, decl_arg_types))
2011 candidates = tree_cons (NULL_TREE, fn, candidates);
2012 continue;
2013 }
2014
2015 /* See whether this function might be a specialization of this
2016 template. Suppress access control because we might be trying
2017 to make this specialization a friend, and we have already done
2018 access control for the declaration of the specialization. */
2019 push_deferring_access_checks (dk_no_check);
2020 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2021 pop_deferring_access_checks ();
2022
2023 if (!targs)
2024 /* We cannot deduce template arguments that when used to
2025 specialize TMPL will produce DECL. */
2026 continue;
2027
2028 /* Make sure that the deduced arguments actually work. */
2029 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2030 if (insttype == error_mark_node)
2031 continue;
2032 fn_arg_types
2033 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2034 if (!compparms (fn_arg_types, decl_arg_types))
2035 continue;
2036
2037 /* Save this template, and the arguments deduced. */
2038 templates = tree_cons (targs, fn, templates);
2039 }
2040 else if (need_member_template)
2041 /* FN is an ordinary member function, and we need a
2042 specialization of a member template. */
2043 ;
2044 else if (TREE_CODE (fn) != FUNCTION_DECL)
2045 /* We can get IDENTIFIER_NODEs here in certain erroneous
2046 cases. */
2047 ;
2048 else if (!DECL_FUNCTION_MEMBER_P (fn))
2049 /* This is just an ordinary non-member function. Nothing can
2050 be a specialization of that. */
2051 ;
2052 else if (DECL_ARTIFICIAL (fn))
2053 /* Cannot specialize functions that are created implicitly. */
2054 ;
2055 else
2056 {
2057 tree decl_arg_types;
2058
2059 /* This is an ordinary member function. However, since
2060 we're here, we can assume its enclosing class is a
2061 template class. For example,
2062
2063 template <typename T> struct S { void f(); };
2064 template <> void S<int>::f() {}
2065
2066 Here, S<int>::f is a non-template, but S<int> is a
2067 template class. If FN has the same type as DECL, we
2068 might be in business. */
2069
2070 if (!DECL_TEMPLATE_INFO (fn))
2071 /* Its enclosing class is an explicit specialization
2072 of a template class. This is not a candidate. */
2073 continue;
2074
2075 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2076 TREE_TYPE (TREE_TYPE (fn))))
2077 /* The return types differ. */
2078 continue;
2079
2080 /* Adjust the type of DECL in case FN is a static member. */
2081 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2082 if (DECL_STATIC_FUNCTION_P (fn)
2083 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2084 decl_arg_types = TREE_CHAIN (decl_arg_types);
2085
2086 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2087 decl_arg_types))
2088 /* They match! */
2089 candidates = tree_cons (NULL_TREE, fn, candidates);
2090 }
2091 }
2092
2093 if (templates && TREE_CHAIN (templates))
2094 {
2095 /* We have:
2096
2097 [temp.expl.spec]
2098
2099 It is possible for a specialization with a given function
2100 signature to be instantiated from more than one function
2101 template. In such cases, explicit specification of the
2102 template arguments must be used to uniquely identify the
2103 function template specialization being specialized.
2104
2105 Note that here, there's no suggestion that we're supposed to
2106 determine which of the candidate templates is most
2107 specialized. However, we, also have:
2108
2109 [temp.func.order]
2110
2111 Partial ordering of overloaded function template
2112 declarations is used in the following contexts to select
2113 the function template to which a function template
2114 specialization refers:
2115
2116 -- when an explicit specialization refers to a function
2117 template.
2118
2119 So, we do use the partial ordering rules, at least for now.
2120 This extension can only serve to make invalid programs valid,
2121 so it's safe. And, there is strong anecdotal evidence that
2122 the committee intended the partial ordering rules to apply;
2123 the EDG front end has that behavior, and John Spicer claims
2124 that the committee simply forgot to delete the wording in
2125 [temp.expl.spec]. */
2126 tree tmpl = most_specialized_instantiation (templates);
2127 if (tmpl != error_mark_node)
2128 {
2129 templates = tmpl;
2130 TREE_CHAIN (templates) = NULL_TREE;
2131 }
2132 }
2133
2134 if (templates == NULL_TREE && candidates == NULL_TREE)
2135 {
2136 error ("template-id %qD for %q+D does not match any template "
2137 "declaration", template_id, decl);
2138 if (header_count && header_count != template_count + 1)
2139 inform (input_location, "saw %d %<template<>%>, need %d for "
2140 "specializing a member function template",
2141 header_count, template_count + 1);
2142 return error_mark_node;
2143 }
2144 else if ((templates && TREE_CHAIN (templates))
2145 || (candidates && TREE_CHAIN (candidates))
2146 || (templates && candidates))
2147 {
2148 error ("ambiguous template specialization %qD for %q+D",
2149 template_id, decl);
2150 candidates = chainon (candidates, templates);
2151 print_candidates (candidates);
2152 return error_mark_node;
2153 }
2154
2155 /* We have one, and exactly one, match. */
2156 if (candidates)
2157 {
2158 tree fn = TREE_VALUE (candidates);
2159 *targs_out = copy_node (DECL_TI_ARGS (fn));
2160 /* DECL is a re-declaration or partial instantiation of a template
2161 function. */
2162 if (TREE_CODE (fn) == TEMPLATE_DECL)
2163 return fn;
2164 /* It was a specialization of an ordinary member function in a
2165 template class. */
2166 return DECL_TI_TEMPLATE (fn);
2167 }
2168
2169 /* It was a specialization of a template. */
2170 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2171 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2172 {
2173 *targs_out = copy_node (targs);
2174 SET_TMPL_ARGS_LEVEL (*targs_out,
2175 TMPL_ARGS_DEPTH (*targs_out),
2176 TREE_PURPOSE (templates));
2177 }
2178 else
2179 *targs_out = TREE_PURPOSE (templates);
2180 return TREE_VALUE (templates);
2181 }
2182
2183 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2184 but with the default argument values filled in from those in the
2185 TMPL_TYPES. */
2186
2187 static tree
2188 copy_default_args_to_explicit_spec_1 (tree spec_types,
2189 tree tmpl_types)
2190 {
2191 tree new_spec_types;
2192
2193 if (!spec_types)
2194 return NULL_TREE;
2195
2196 if (spec_types == void_list_node)
2197 return void_list_node;
2198
2199 /* Substitute into the rest of the list. */
2200 new_spec_types =
2201 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2202 TREE_CHAIN (tmpl_types));
2203
2204 /* Add the default argument for this parameter. */
2205 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2206 TREE_VALUE (spec_types),
2207 new_spec_types);
2208 }
2209
2210 /* DECL is an explicit specialization. Replicate default arguments
2211 from the template it specializes. (That way, code like:
2212
2213 template <class T> void f(T = 3);
2214 template <> void f(double);
2215 void g () { f (); }
2216
2217 works, as required.) An alternative approach would be to look up
2218 the correct default arguments at the call-site, but this approach
2219 is consistent with how implicit instantiations are handled. */
2220
2221 static void
2222 copy_default_args_to_explicit_spec (tree decl)
2223 {
2224 tree tmpl;
2225 tree spec_types;
2226 tree tmpl_types;
2227 tree new_spec_types;
2228 tree old_type;
2229 tree new_type;
2230 tree t;
2231 tree object_type = NULL_TREE;
2232 tree in_charge = NULL_TREE;
2233 tree vtt = NULL_TREE;
2234
2235 /* See if there's anything we need to do. */
2236 tmpl = DECL_TI_TEMPLATE (decl);
2237 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2238 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2239 if (TREE_PURPOSE (t))
2240 break;
2241 if (!t)
2242 return;
2243
2244 old_type = TREE_TYPE (decl);
2245 spec_types = TYPE_ARG_TYPES (old_type);
2246
2247 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2248 {
2249 /* Remove the this pointer, but remember the object's type for
2250 CV quals. */
2251 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2252 spec_types = TREE_CHAIN (spec_types);
2253 tmpl_types = TREE_CHAIN (tmpl_types);
2254
2255 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2256 {
2257 /* DECL may contain more parameters than TMPL due to the extra
2258 in-charge parameter in constructors and destructors. */
2259 in_charge = spec_types;
2260 spec_types = TREE_CHAIN (spec_types);
2261 }
2262 if (DECL_HAS_VTT_PARM_P (decl))
2263 {
2264 vtt = spec_types;
2265 spec_types = TREE_CHAIN (spec_types);
2266 }
2267 }
2268
2269 /* Compute the merged default arguments. */
2270 new_spec_types =
2271 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2272
2273 /* Compute the new FUNCTION_TYPE. */
2274 if (object_type)
2275 {
2276 if (vtt)
2277 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2278 TREE_VALUE (vtt),
2279 new_spec_types);
2280
2281 if (in_charge)
2282 /* Put the in-charge parameter back. */
2283 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2284 TREE_VALUE (in_charge),
2285 new_spec_types);
2286
2287 new_type = build_method_type_directly (object_type,
2288 TREE_TYPE (old_type),
2289 new_spec_types);
2290 }
2291 else
2292 new_type = build_function_type (TREE_TYPE (old_type),
2293 new_spec_types);
2294 new_type = cp_build_type_attribute_variant (new_type,
2295 TYPE_ATTRIBUTES (old_type));
2296 new_type = build_exception_variant (new_type,
2297 TYPE_RAISES_EXCEPTIONS (old_type));
2298
2299 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2300 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2301
2302 TREE_TYPE (decl) = new_type;
2303 }
2304
2305 /* Return the number of template headers we expect to see for a definition
2306 or specialization of CTYPE or one of its non-template members. */
2307
2308 int
2309 num_template_headers_for_class (tree ctype)
2310 {
2311 int num_templates = 0;
2312
2313 while (ctype && CLASS_TYPE_P (ctype))
2314 {
2315 /* You're supposed to have one `template <...>' for every
2316 template class, but you don't need one for a full
2317 specialization. For example:
2318
2319 template <class T> struct S{};
2320 template <> struct S<int> { void f(); };
2321 void S<int>::f () {}
2322
2323 is correct; there shouldn't be a `template <>' for the
2324 definition of `S<int>::f'. */
2325 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2326 /* If CTYPE does not have template information of any
2327 kind, then it is not a template, nor is it nested
2328 within a template. */
2329 break;
2330 if (explicit_class_specialization_p (ctype))
2331 break;
2332 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2333 ++num_templates;
2334
2335 ctype = TYPE_CONTEXT (ctype);
2336 }
2337
2338 return num_templates;
2339 }
2340
2341 /* Do a simple sanity check on the template headers that precede the
2342 variable declaration DECL. */
2343
2344 void
2345 check_template_variable (tree decl)
2346 {
2347 tree ctx = CP_DECL_CONTEXT (decl);
2348 int wanted = num_template_headers_for_class (ctx);
2349 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2350 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2351 {
2352 if (cxx_dialect < cxx14)
2353 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2354 "variable templates only available with "
2355 "-std=c++14 or -std=gnu++14");
2356
2357 // Namespace-scope variable templates should have a template header.
2358 ++wanted;
2359 }
2360 if (template_header_count > wanted)
2361 {
2362 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2363 "too many template headers for %D (should be %d)",
2364 decl, wanted);
2365 if (warned && CLASS_TYPE_P (ctx)
2366 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2367 inform (DECL_SOURCE_LOCATION (decl),
2368 "members of an explicitly specialized class are defined "
2369 "without a template header");
2370 }
2371 }
2372
2373 /* Check to see if the function just declared, as indicated in
2374 DECLARATOR, and in DECL, is a specialization of a function
2375 template. We may also discover that the declaration is an explicit
2376 instantiation at this point.
2377
2378 Returns DECL, or an equivalent declaration that should be used
2379 instead if all goes well. Issues an error message if something is
2380 amiss. Returns error_mark_node if the error is not easily
2381 recoverable.
2382
2383 FLAGS is a bitmask consisting of the following flags:
2384
2385 2: The function has a definition.
2386 4: The function is a friend.
2387
2388 The TEMPLATE_COUNT is the number of references to qualifying
2389 template classes that appeared in the name of the function. For
2390 example, in
2391
2392 template <class T> struct S { void f(); };
2393 void S<int>::f();
2394
2395 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2396 classes are not counted in the TEMPLATE_COUNT, so that in
2397
2398 template <class T> struct S {};
2399 template <> struct S<int> { void f(); }
2400 template <> void S<int>::f();
2401
2402 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2403 invalid; there should be no template <>.)
2404
2405 If the function is a specialization, it is marked as such via
2406 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2407 is set up correctly, and it is added to the list of specializations
2408 for that template. */
2409
2410 tree
2411 check_explicit_specialization (tree declarator,
2412 tree decl,
2413 int template_count,
2414 int flags)
2415 {
2416 int have_def = flags & 2;
2417 int is_friend = flags & 4;
2418 int specialization = 0;
2419 int explicit_instantiation = 0;
2420 int member_specialization = 0;
2421 tree ctype = DECL_CLASS_CONTEXT (decl);
2422 tree dname = DECL_NAME (decl);
2423 tmpl_spec_kind tsk;
2424
2425 if (is_friend)
2426 {
2427 if (!processing_specialization)
2428 tsk = tsk_none;
2429 else
2430 tsk = tsk_excessive_parms;
2431 }
2432 else
2433 tsk = current_tmpl_spec_kind (template_count);
2434
2435 switch (tsk)
2436 {
2437 case tsk_none:
2438 if (processing_specialization && TREE_CODE (decl) != VAR_DECL)
2439 {
2440 specialization = 1;
2441 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2442 }
2443 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2444 {
2445 if (is_friend)
2446 /* This could be something like:
2447
2448 template <class T> void f(T);
2449 class S { friend void f<>(int); } */
2450 specialization = 1;
2451 else
2452 {
2453 /* This case handles bogus declarations like template <>
2454 template <class T> void f<int>(); */
2455
2456 error ("template-id %qD in declaration of primary template",
2457 declarator);
2458 return decl;
2459 }
2460 }
2461 break;
2462
2463 case tsk_invalid_member_spec:
2464 /* The error has already been reported in
2465 check_specialization_scope. */
2466 return error_mark_node;
2467
2468 case tsk_invalid_expl_inst:
2469 error ("template parameter list used in explicit instantiation");
2470
2471 /* Fall through. */
2472
2473 case tsk_expl_inst:
2474 if (have_def)
2475 error ("definition provided for explicit instantiation");
2476
2477 explicit_instantiation = 1;
2478 break;
2479
2480 case tsk_excessive_parms:
2481 case tsk_insufficient_parms:
2482 if (tsk == tsk_excessive_parms)
2483 error ("too many template parameter lists in declaration of %qD",
2484 decl);
2485 else if (template_header_count)
2486 error("too few template parameter lists in declaration of %qD", decl);
2487 else
2488 error("explicit specialization of %qD must be introduced by "
2489 "%<template <>%>", decl);
2490
2491 /* Fall through. */
2492 case tsk_expl_spec:
2493 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2494 /* In cases like template<> constexpr bool v = true;
2495 We'll give an error in check_template_variable. */
2496 break;
2497
2498 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2499 if (ctype)
2500 member_specialization = 1;
2501 else
2502 specialization = 1;
2503 break;
2504
2505 case tsk_template:
2506 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2507 {
2508 /* This case handles bogus declarations like template <>
2509 template <class T> void f<int>(); */
2510
2511 if (!uses_template_parms (declarator))
2512 error ("template-id %qD in declaration of primary template",
2513 declarator);
2514 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2515 {
2516 /* Partial specialization of variable template. */
2517 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2518 specialization = 1;
2519 goto ok;
2520 }
2521 else if (cxx_dialect < cxx14)
2522 error ("non-type partial specialization %qD "
2523 "is not allowed", declarator);
2524 else
2525 error ("non-class, non-variable partial specialization %qD "
2526 "is not allowed", declarator);
2527 return decl;
2528 ok:;
2529 }
2530
2531 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2532 /* This is a specialization of a member template, without
2533 specialization the containing class. Something like:
2534
2535 template <class T> struct S {
2536 template <class U> void f (U);
2537 };
2538 template <> template <class U> void S<int>::f(U) {}
2539
2540 That's a specialization -- but of the entire template. */
2541 specialization = 1;
2542 break;
2543
2544 default:
2545 gcc_unreachable ();
2546 }
2547
2548 if ((specialization || member_specialization)
2549 /* This doesn't apply to variable templates. */
2550 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2551 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2552 {
2553 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2554 for (; t; t = TREE_CHAIN (t))
2555 if (TREE_PURPOSE (t))
2556 {
2557 permerror (input_location,
2558 "default argument specified in explicit specialization");
2559 break;
2560 }
2561 }
2562
2563 if (specialization || member_specialization || explicit_instantiation)
2564 {
2565 tree tmpl = NULL_TREE;
2566 tree targs = NULL_TREE;
2567 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2568
2569 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2570 if (!was_template_id)
2571 {
2572 tree fns;
2573
2574 gcc_assert (identifier_p (declarator));
2575 if (ctype)
2576 fns = dname;
2577 else
2578 {
2579 /* If there is no class context, the explicit instantiation
2580 must be at namespace scope. */
2581 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2582
2583 /* Find the namespace binding, using the declaration
2584 context. */
2585 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2586 false, true);
2587 if (fns == error_mark_node || !is_overloaded_fn (fns))
2588 {
2589 error ("%qD is not a template function", dname);
2590 fns = error_mark_node;
2591 }
2592 else
2593 {
2594 tree fn = OVL_CURRENT (fns);
2595 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2596 CP_DECL_CONTEXT (fn)))
2597 error ("%qD is not declared in %qD",
2598 decl, current_namespace);
2599 }
2600 }
2601
2602 declarator = lookup_template_function (fns, NULL_TREE);
2603 }
2604
2605 if (declarator == error_mark_node)
2606 return error_mark_node;
2607
2608 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2609 {
2610 if (!explicit_instantiation)
2611 /* A specialization in class scope. This is invalid,
2612 but the error will already have been flagged by
2613 check_specialization_scope. */
2614 return error_mark_node;
2615 else
2616 {
2617 /* It's not valid to write an explicit instantiation in
2618 class scope, e.g.:
2619
2620 class C { template void f(); }
2621
2622 This case is caught by the parser. However, on
2623 something like:
2624
2625 template class C { void f(); };
2626
2627 (which is invalid) we can get here. The error will be
2628 issued later. */
2629 ;
2630 }
2631
2632 return decl;
2633 }
2634 else if (ctype != NULL_TREE
2635 && (identifier_p (TREE_OPERAND (declarator, 0))))
2636 {
2637 // We'll match variable templates in start_decl.
2638 if (VAR_P (decl))
2639 return decl;
2640
2641 /* Find the list of functions in ctype that have the same
2642 name as the declared function. */
2643 tree name = TREE_OPERAND (declarator, 0);
2644 tree fns = NULL_TREE;
2645 int idx;
2646
2647 if (constructor_name_p (name, ctype))
2648 {
2649 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2650
2651 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2652 : !CLASSTYPE_DESTRUCTORS (ctype))
2653 {
2654 /* From [temp.expl.spec]:
2655
2656 If such an explicit specialization for the member
2657 of a class template names an implicitly-declared
2658 special member function (clause _special_), the
2659 program is ill-formed.
2660
2661 Similar language is found in [temp.explicit]. */
2662 error ("specialization of implicitly-declared special member function");
2663 return error_mark_node;
2664 }
2665
2666 name = is_constructor ? ctor_identifier : dtor_identifier;
2667 }
2668
2669 if (!DECL_CONV_FN_P (decl))
2670 {
2671 idx = lookup_fnfields_1 (ctype, name);
2672 if (idx >= 0)
2673 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2674 }
2675 else
2676 {
2677 vec<tree, va_gc> *methods;
2678 tree ovl;
2679
2680 /* For a type-conversion operator, we cannot do a
2681 name-based lookup. We might be looking for `operator
2682 int' which will be a specialization of `operator T'.
2683 So, we find *all* the conversion operators, and then
2684 select from them. */
2685 fns = NULL_TREE;
2686
2687 methods = CLASSTYPE_METHOD_VEC (ctype);
2688 if (methods)
2689 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2690 methods->iterate (idx, &ovl);
2691 ++idx)
2692 {
2693 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2694 /* There are no more conversion functions. */
2695 break;
2696
2697 /* Glue all these conversion functions together
2698 with those we already have. */
2699 for (; ovl; ovl = OVL_NEXT (ovl))
2700 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2701 }
2702 }
2703
2704 if (fns == NULL_TREE)
2705 {
2706 error ("no member function %qD declared in %qT", name, ctype);
2707 return error_mark_node;
2708 }
2709 else
2710 TREE_OPERAND (declarator, 0) = fns;
2711 }
2712
2713 /* Figure out what exactly is being specialized at this point.
2714 Note that for an explicit instantiation, even one for a
2715 member function, we cannot tell apriori whether the
2716 instantiation is for a member template, or just a member
2717 function of a template class. Even if a member template is
2718 being instantiated, the member template arguments may be
2719 elided if they can be deduced from the rest of the
2720 declaration. */
2721 tmpl = determine_specialization (declarator, decl,
2722 &targs,
2723 member_specialization,
2724 template_count,
2725 tsk);
2726
2727 if (!tmpl || tmpl == error_mark_node)
2728 /* We couldn't figure out what this declaration was
2729 specializing. */
2730 return error_mark_node;
2731 else
2732 {
2733 tree gen_tmpl = most_general_template (tmpl);
2734
2735 if (explicit_instantiation)
2736 {
2737 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2738 is done by do_decl_instantiation later. */
2739
2740 int arg_depth = TMPL_ARGS_DEPTH (targs);
2741 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2742
2743 if (arg_depth > parm_depth)
2744 {
2745 /* If TMPL is not the most general template (for
2746 example, if TMPL is a friend template that is
2747 injected into namespace scope), then there will
2748 be too many levels of TARGS. Remove some of them
2749 here. */
2750 int i;
2751 tree new_targs;
2752
2753 new_targs = make_tree_vec (parm_depth);
2754 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2755 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2756 = TREE_VEC_ELT (targs, i);
2757 targs = new_targs;
2758 }
2759
2760 return instantiate_template (tmpl, targs, tf_error);
2761 }
2762
2763 /* If we thought that the DECL was a member function, but it
2764 turns out to be specializing a static member function,
2765 make DECL a static member function as well. */
2766 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2767 && DECL_STATIC_FUNCTION_P (tmpl)
2768 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2769 revert_static_member_fn (decl);
2770
2771 /* If this is a specialization of a member template of a
2772 template class, we want to return the TEMPLATE_DECL, not
2773 the specialization of it. */
2774 if (tsk == tsk_template && !was_template_id)
2775 {
2776 tree result = DECL_TEMPLATE_RESULT (tmpl);
2777 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2778 DECL_INITIAL (result) = NULL_TREE;
2779 if (have_def)
2780 {
2781 tree parm;
2782 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2783 DECL_SOURCE_LOCATION (result)
2784 = DECL_SOURCE_LOCATION (decl);
2785 /* We want to use the argument list specified in the
2786 definition, not in the original declaration. */
2787 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2788 for (parm = DECL_ARGUMENTS (result); parm;
2789 parm = DECL_CHAIN (parm))
2790 DECL_CONTEXT (parm) = result;
2791 }
2792 return register_specialization (tmpl, gen_tmpl, targs,
2793 is_friend, 0);
2794 }
2795
2796 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2797 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2798
2799 if (was_template_id)
2800 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
2801
2802 /* Inherit default function arguments from the template
2803 DECL is specializing. */
2804 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2805 copy_default_args_to_explicit_spec (decl);
2806
2807 /* This specialization has the same protection as the
2808 template it specializes. */
2809 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2810 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2811
2812 /* 7.1.1-1 [dcl.stc]
2813
2814 A storage-class-specifier shall not be specified in an
2815 explicit specialization...
2816
2817 The parser rejects these, so unless action is taken here,
2818 explicit function specializations will always appear with
2819 global linkage.
2820
2821 The action recommended by the C++ CWG in response to C++
2822 defect report 605 is to make the storage class and linkage
2823 of the explicit specialization match the templated function:
2824
2825 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2826 */
2827 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2828 {
2829 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2830 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2831
2832 /* This specialization has the same linkage and visibility as
2833 the function template it specializes. */
2834 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2835 if (! TREE_PUBLIC (decl))
2836 {
2837 DECL_INTERFACE_KNOWN (decl) = 1;
2838 DECL_NOT_REALLY_EXTERN (decl) = 1;
2839 }
2840 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2841 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2842 {
2843 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2844 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2845 }
2846 }
2847
2848 /* If DECL is a friend declaration, declared using an
2849 unqualified name, the namespace associated with DECL may
2850 have been set incorrectly. For example, in:
2851
2852 template <typename T> void f(T);
2853 namespace N {
2854 struct S { friend void f<int>(int); }
2855 }
2856
2857 we will have set the DECL_CONTEXT for the friend
2858 declaration to N, rather than to the global namespace. */
2859 if (DECL_NAMESPACE_SCOPE_P (decl))
2860 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2861
2862 if (is_friend && !have_def)
2863 /* This is not really a declaration of a specialization.
2864 It's just the name of an instantiation. But, it's not
2865 a request for an instantiation, either. */
2866 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2867 else if (TREE_CODE (decl) == FUNCTION_DECL)
2868 /* A specialization is not necessarily COMDAT. */
2869 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
2870 && DECL_DECLARED_INLINE_P (decl));
2871 else if (TREE_CODE (decl) == VAR_DECL)
2872 DECL_COMDAT (decl) = false;
2873
2874 /* Register this specialization so that we can find it
2875 again. */
2876 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2877
2878 /* A 'structor should already have clones. */
2879 gcc_assert (decl == error_mark_node
2880 || variable_template_p (tmpl)
2881 || !(DECL_CONSTRUCTOR_P (decl)
2882 || DECL_DESTRUCTOR_P (decl))
2883 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2884 }
2885 }
2886
2887 return decl;
2888 }
2889
2890 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2891 parameters. These are represented in the same format used for
2892 DECL_TEMPLATE_PARMS. */
2893
2894 int
2895 comp_template_parms (const_tree parms1, const_tree parms2)
2896 {
2897 const_tree p1;
2898 const_tree p2;
2899
2900 if (parms1 == parms2)
2901 return 1;
2902
2903 for (p1 = parms1, p2 = parms2;
2904 p1 != NULL_TREE && p2 != NULL_TREE;
2905 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2906 {
2907 tree t1 = TREE_VALUE (p1);
2908 tree t2 = TREE_VALUE (p2);
2909 int i;
2910
2911 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2912 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2913
2914 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2915 return 0;
2916
2917 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2918 {
2919 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2920 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2921
2922 /* If either of the template parameters are invalid, assume
2923 they match for the sake of error recovery. */
2924 if (error_operand_p (parm1) || error_operand_p (parm2))
2925 return 1;
2926
2927 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2928 return 0;
2929
2930 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2931 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2932 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2933 continue;
2934 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2935 return 0;
2936 }
2937 }
2938
2939 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2940 /* One set of parameters has more parameters lists than the
2941 other. */
2942 return 0;
2943
2944 return 1;
2945 }
2946
2947 /* Determine whether PARM is a parameter pack. */
2948
2949 bool
2950 template_parameter_pack_p (const_tree parm)
2951 {
2952 /* Determine if we have a non-type template parameter pack. */
2953 if (TREE_CODE (parm) == PARM_DECL)
2954 return (DECL_TEMPLATE_PARM_P (parm)
2955 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2956 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2957 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2958
2959 /* If this is a list of template parameters, we could get a
2960 TYPE_DECL or a TEMPLATE_DECL. */
2961 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2962 parm = TREE_TYPE (parm);
2963
2964 /* Otherwise it must be a type template parameter. */
2965 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2966 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2967 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2968 }
2969
2970 /* Determine if T is a function parameter pack. */
2971
2972 bool
2973 function_parameter_pack_p (const_tree t)
2974 {
2975 if (t && TREE_CODE (t) == PARM_DECL)
2976 return DECL_PACK_P (t);
2977 return false;
2978 }
2979
2980 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2981 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2982
2983 tree
2984 get_function_template_decl (const_tree primary_func_tmpl_inst)
2985 {
2986 if (! primary_func_tmpl_inst
2987 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2988 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2989 return NULL;
2990
2991 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2992 }
2993
2994 /* Return true iff the function parameter PARAM_DECL was expanded
2995 from the function parameter pack PACK. */
2996
2997 bool
2998 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2999 {
3000 if (DECL_ARTIFICIAL (param_decl)
3001 || !function_parameter_pack_p (pack))
3002 return false;
3003
3004 /* The parameter pack and its pack arguments have the same
3005 DECL_PARM_INDEX. */
3006 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3007 }
3008
3009 /* Determine whether ARGS describes a variadic template args list,
3010 i.e., one that is terminated by a template argument pack. */
3011
3012 static bool
3013 template_args_variadic_p (tree args)
3014 {
3015 int nargs;
3016 tree last_parm;
3017
3018 if (args == NULL_TREE)
3019 return false;
3020
3021 args = INNERMOST_TEMPLATE_ARGS (args);
3022 nargs = TREE_VEC_LENGTH (args);
3023
3024 if (nargs == 0)
3025 return false;
3026
3027 last_parm = TREE_VEC_ELT (args, nargs - 1);
3028
3029 return ARGUMENT_PACK_P (last_parm);
3030 }
3031
3032 /* Generate a new name for the parameter pack name NAME (an
3033 IDENTIFIER_NODE) that incorporates its */
3034
3035 static tree
3036 make_ith_pack_parameter_name (tree name, int i)
3037 {
3038 /* Munge the name to include the parameter index. */
3039 #define NUMBUF_LEN 128
3040 char numbuf[NUMBUF_LEN];
3041 char* newname;
3042 int newname_len;
3043
3044 if (name == NULL_TREE)
3045 return name;
3046 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3047 newname_len = IDENTIFIER_LENGTH (name)
3048 + strlen (numbuf) + 2;
3049 newname = (char*)alloca (newname_len);
3050 snprintf (newname, newname_len,
3051 "%s#%i", IDENTIFIER_POINTER (name), i);
3052 return get_identifier (newname);
3053 }
3054
3055 /* Return true if T is a primary function, class or alias template
3056 instantiation. */
3057
3058 bool
3059 primary_template_instantiation_p (const_tree t)
3060 {
3061 if (!t)
3062 return false;
3063
3064 if (TREE_CODE (t) == FUNCTION_DECL)
3065 return DECL_LANG_SPECIFIC (t)
3066 && DECL_TEMPLATE_INSTANTIATION (t)
3067 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3068 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3069 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3070 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3071 else if (alias_template_specialization_p (t))
3072 return true;
3073 return false;
3074 }
3075
3076 /* Return true if PARM is a template template parameter. */
3077
3078 bool
3079 template_template_parameter_p (const_tree parm)
3080 {
3081 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3082 }
3083
3084 /* Return true iff PARM is a DECL representing a type template
3085 parameter. */
3086
3087 bool
3088 template_type_parameter_p (const_tree parm)
3089 {
3090 return (parm
3091 && (TREE_CODE (parm) == TYPE_DECL
3092 || TREE_CODE (parm) == TEMPLATE_DECL)
3093 && DECL_TEMPLATE_PARM_P (parm));
3094 }
3095
3096 /* Return the template parameters of T if T is a
3097 primary template instantiation, NULL otherwise. */
3098
3099 tree
3100 get_primary_template_innermost_parameters (const_tree t)
3101 {
3102 tree parms = NULL, template_info = NULL;
3103
3104 if ((template_info = get_template_info (t))
3105 && primary_template_instantiation_p (t))
3106 parms = INNERMOST_TEMPLATE_PARMS
3107 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3108
3109 return parms;
3110 }
3111
3112 /* Return the template parameters of the LEVELth level from the full list
3113 of template parameters PARMS. */
3114
3115 tree
3116 get_template_parms_at_level (tree parms, int level)
3117 {
3118 tree p;
3119 if (!parms
3120 || TREE_CODE (parms) != TREE_LIST
3121 || level > TMPL_PARMS_DEPTH (parms))
3122 return NULL_TREE;
3123
3124 for (p = parms; p; p = TREE_CHAIN (p))
3125 if (TMPL_PARMS_DEPTH (p) == level)
3126 return p;
3127
3128 return NULL_TREE;
3129 }
3130
3131 /* Returns the template arguments of T if T is a template instantiation,
3132 NULL otherwise. */
3133
3134 tree
3135 get_template_innermost_arguments (const_tree t)
3136 {
3137 tree args = NULL, template_info = NULL;
3138
3139 if ((template_info = get_template_info (t))
3140 && TI_ARGS (template_info))
3141 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3142
3143 return args;
3144 }
3145
3146 /* Return the argument pack elements of T if T is a template argument pack,
3147 NULL otherwise. */
3148
3149 tree
3150 get_template_argument_pack_elems (const_tree t)
3151 {
3152 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3153 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3154 return NULL;
3155
3156 return ARGUMENT_PACK_ARGS (t);
3157 }
3158
3159 /* Structure used to track the progress of find_parameter_packs_r. */
3160 struct find_parameter_pack_data
3161 {
3162 /* TREE_LIST that will contain all of the parameter packs found by
3163 the traversal. */
3164 tree* parameter_packs;
3165
3166 /* Set of AST nodes that have been visited by the traversal. */
3167 hash_set<tree> *visited;
3168 };
3169
3170 /* Identifies all of the argument packs that occur in a template
3171 argument and appends them to the TREE_LIST inside DATA, which is a
3172 find_parameter_pack_data structure. This is a subroutine of
3173 make_pack_expansion and uses_parameter_packs. */
3174 static tree
3175 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3176 {
3177 tree t = *tp;
3178 struct find_parameter_pack_data* ppd =
3179 (struct find_parameter_pack_data*)data;
3180 bool parameter_pack_p = false;
3181
3182 /* Handle type aliases/typedefs. */
3183 if (TYPE_ALIAS_P (t))
3184 {
3185 if (TYPE_TEMPLATE_INFO (t))
3186 cp_walk_tree (&TYPE_TI_ARGS (t),
3187 &find_parameter_packs_r,
3188 ppd, ppd->visited);
3189 *walk_subtrees = 0;
3190 return NULL_TREE;
3191 }
3192
3193 /* Identify whether this is a parameter pack or not. */
3194 switch (TREE_CODE (t))
3195 {
3196 case TEMPLATE_PARM_INDEX:
3197 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3198 parameter_pack_p = true;
3199 break;
3200
3201 case TEMPLATE_TYPE_PARM:
3202 t = TYPE_MAIN_VARIANT (t);
3203 case TEMPLATE_TEMPLATE_PARM:
3204 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3205 parameter_pack_p = true;
3206 break;
3207
3208 case FIELD_DECL:
3209 case PARM_DECL:
3210 if (DECL_PACK_P (t))
3211 {
3212 /* We don't want to walk into the type of a PARM_DECL,
3213 because we don't want to see the type parameter pack. */
3214 *walk_subtrees = 0;
3215 parameter_pack_p = true;
3216 }
3217 break;
3218
3219 /* Look through a lambda capture proxy to the field pack. */
3220 case VAR_DECL:
3221 if (DECL_HAS_VALUE_EXPR_P (t))
3222 {
3223 tree v = DECL_VALUE_EXPR (t);
3224 cp_walk_tree (&v,
3225 &find_parameter_packs_r,
3226 ppd, ppd->visited);
3227 *walk_subtrees = 0;
3228 }
3229 break;
3230
3231 case BASES:
3232 parameter_pack_p = true;
3233 break;
3234 default:
3235 /* Not a parameter pack. */
3236 break;
3237 }
3238
3239 if (parameter_pack_p)
3240 {
3241 /* Add this parameter pack to the list. */
3242 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3243 }
3244
3245 if (TYPE_P (t))
3246 cp_walk_tree (&TYPE_CONTEXT (t),
3247 &find_parameter_packs_r, ppd, ppd->visited);
3248
3249 /* This switch statement will return immediately if we don't find a
3250 parameter pack. */
3251 switch (TREE_CODE (t))
3252 {
3253 case TEMPLATE_PARM_INDEX:
3254 return NULL_TREE;
3255
3256 case BOUND_TEMPLATE_TEMPLATE_PARM:
3257 /* Check the template itself. */
3258 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3259 &find_parameter_packs_r, ppd, ppd->visited);
3260 /* Check the template arguments. */
3261 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3262 ppd->visited);
3263 *walk_subtrees = 0;
3264 return NULL_TREE;
3265
3266 case TEMPLATE_TYPE_PARM:
3267 case TEMPLATE_TEMPLATE_PARM:
3268 return NULL_TREE;
3269
3270 case PARM_DECL:
3271 return NULL_TREE;
3272
3273 case RECORD_TYPE:
3274 if (TYPE_PTRMEMFUNC_P (t))
3275 return NULL_TREE;
3276 /* Fall through. */
3277
3278 case UNION_TYPE:
3279 case ENUMERAL_TYPE:
3280 if (TYPE_TEMPLATE_INFO (t))
3281 cp_walk_tree (&TYPE_TI_ARGS (t),
3282 &find_parameter_packs_r, ppd, ppd->visited);
3283
3284 *walk_subtrees = 0;
3285 return NULL_TREE;
3286
3287 case CONSTRUCTOR:
3288 case TEMPLATE_DECL:
3289 cp_walk_tree (&TREE_TYPE (t),
3290 &find_parameter_packs_r, ppd, ppd->visited);
3291 return NULL_TREE;
3292
3293 case TYPENAME_TYPE:
3294 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3295 ppd, ppd->visited);
3296 *walk_subtrees = 0;
3297 return NULL_TREE;
3298
3299 case TYPE_PACK_EXPANSION:
3300 case EXPR_PACK_EXPANSION:
3301 *walk_subtrees = 0;
3302 return NULL_TREE;
3303
3304 case INTEGER_TYPE:
3305 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3306 ppd, ppd->visited);
3307 *walk_subtrees = 0;
3308 return NULL_TREE;
3309
3310 case IDENTIFIER_NODE:
3311 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3312 ppd->visited);
3313 *walk_subtrees = 0;
3314 return NULL_TREE;
3315
3316 default:
3317 return NULL_TREE;
3318 }
3319
3320 return NULL_TREE;
3321 }
3322
3323 /* Determines if the expression or type T uses any parameter packs. */
3324 bool
3325 uses_parameter_packs (tree t)
3326 {
3327 tree parameter_packs = NULL_TREE;
3328 struct find_parameter_pack_data ppd;
3329 ppd.parameter_packs = &parameter_packs;
3330 ppd.visited = new hash_set<tree>;
3331 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3332 delete ppd.visited;
3333 return parameter_packs != NULL_TREE;
3334 }
3335
3336 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3337 representation a base-class initializer into a parameter pack
3338 expansion. If all goes well, the resulting node will be an
3339 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3340 respectively. */
3341 tree
3342 make_pack_expansion (tree arg)
3343 {
3344 tree result;
3345 tree parameter_packs = NULL_TREE;
3346 bool for_types = false;
3347 struct find_parameter_pack_data ppd;
3348
3349 if (!arg || arg == error_mark_node)
3350 return arg;
3351
3352 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3353 {
3354 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3355 class initializer. In this case, the TREE_PURPOSE will be a
3356 _TYPE node (representing the base class expansion we're
3357 initializing) and the TREE_VALUE will be a TREE_LIST
3358 containing the initialization arguments.
3359
3360 The resulting expansion looks somewhat different from most
3361 expansions. Rather than returning just one _EXPANSION, we
3362 return a TREE_LIST whose TREE_PURPOSE is a
3363 TYPE_PACK_EXPANSION containing the bases that will be
3364 initialized. The TREE_VALUE will be identical to the
3365 original TREE_VALUE, which is a list of arguments that will
3366 be passed to each base. We do not introduce any new pack
3367 expansion nodes into the TREE_VALUE (although it is possible
3368 that some already exist), because the TREE_PURPOSE and
3369 TREE_VALUE all need to be expanded together with the same
3370 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3371 resulting TREE_PURPOSE will mention the parameter packs in
3372 both the bases and the arguments to the bases. */
3373 tree purpose;
3374 tree value;
3375 tree parameter_packs = NULL_TREE;
3376
3377 /* Determine which parameter packs will be used by the base
3378 class expansion. */
3379 ppd.visited = new hash_set<tree>;
3380 ppd.parameter_packs = &parameter_packs;
3381 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3382 &ppd, ppd.visited);
3383
3384 if (parameter_packs == NULL_TREE)
3385 {
3386 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3387 delete ppd.visited;
3388 return error_mark_node;
3389 }
3390
3391 if (TREE_VALUE (arg) != void_type_node)
3392 {
3393 /* Collect the sets of parameter packs used in each of the
3394 initialization arguments. */
3395 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3396 {
3397 /* Determine which parameter packs will be expanded in this
3398 argument. */
3399 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3400 &ppd, ppd.visited);
3401 }
3402 }
3403
3404 delete ppd.visited;
3405
3406 /* Create the pack expansion type for the base type. */
3407 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3408 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3409 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3410
3411 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3412 they will rarely be compared to anything. */
3413 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3414
3415 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3416 }
3417
3418 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3419 for_types = true;
3420
3421 /* Build the PACK_EXPANSION_* node. */
3422 result = for_types
3423 ? cxx_make_type (TYPE_PACK_EXPANSION)
3424 : make_node (EXPR_PACK_EXPANSION);
3425 SET_PACK_EXPANSION_PATTERN (result, arg);
3426 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3427 {
3428 /* Propagate type and const-expression information. */
3429 TREE_TYPE (result) = TREE_TYPE (arg);
3430 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3431 }
3432 else
3433 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3434 they will rarely be compared to anything. */
3435 SET_TYPE_STRUCTURAL_EQUALITY (result);
3436
3437 /* Determine which parameter packs will be expanded. */
3438 ppd.parameter_packs = &parameter_packs;
3439 ppd.visited = new hash_set<tree>;
3440 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3441 delete ppd.visited;
3442
3443 /* Make sure we found some parameter packs. */
3444 if (parameter_packs == NULL_TREE)
3445 {
3446 if (TYPE_P (arg))
3447 error ("expansion pattern %<%T%> contains no argument packs", arg);
3448 else
3449 error ("expansion pattern %<%E%> contains no argument packs", arg);
3450 return error_mark_node;
3451 }
3452 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3453
3454 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3455
3456 return result;
3457 }
3458
3459 /* Checks T for any "bare" parameter packs, which have not yet been
3460 expanded, and issues an error if any are found. This operation can
3461 only be done on full expressions or types (e.g., an expression
3462 statement, "if" condition, etc.), because we could have expressions like:
3463
3464 foo(f(g(h(args)))...)
3465
3466 where "args" is a parameter pack. check_for_bare_parameter_packs
3467 should not be called for the subexpressions args, h(args),
3468 g(h(args)), or f(g(h(args))), because we would produce erroneous
3469 error messages.
3470
3471 Returns TRUE and emits an error if there were bare parameter packs,
3472 returns FALSE otherwise. */
3473 bool
3474 check_for_bare_parameter_packs (tree t)
3475 {
3476 tree parameter_packs = NULL_TREE;
3477 struct find_parameter_pack_data ppd;
3478
3479 if (!processing_template_decl || !t || t == error_mark_node)
3480 return false;
3481
3482 if (TREE_CODE (t) == TYPE_DECL)
3483 t = TREE_TYPE (t);
3484
3485 ppd.parameter_packs = &parameter_packs;
3486 ppd.visited = new hash_set<tree>;
3487 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3488 delete ppd.visited;
3489
3490 if (parameter_packs)
3491 {
3492 error ("parameter packs not expanded with %<...%>:");
3493 while (parameter_packs)
3494 {
3495 tree pack = TREE_VALUE (parameter_packs);
3496 tree name = NULL_TREE;
3497
3498 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3499 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3500 name = TYPE_NAME (pack);
3501 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3502 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3503 else
3504 name = DECL_NAME (pack);
3505
3506 if (name)
3507 inform (input_location, " %qD", name);
3508 else
3509 inform (input_location, " <anonymous>");
3510
3511 parameter_packs = TREE_CHAIN (parameter_packs);
3512 }
3513
3514 return true;
3515 }
3516
3517 return false;
3518 }
3519
3520 /* Expand any parameter packs that occur in the template arguments in
3521 ARGS. */
3522 tree
3523 expand_template_argument_pack (tree args)
3524 {
3525 tree result_args = NULL_TREE;
3526 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3527 int num_result_args = -1;
3528 int non_default_args_count = -1;
3529
3530 /* First, determine if we need to expand anything, and the number of
3531 slots we'll need. */
3532 for (in_arg = 0; in_arg < nargs; ++in_arg)
3533 {
3534 tree arg = TREE_VEC_ELT (args, in_arg);
3535 if (arg == NULL_TREE)
3536 return args;
3537 if (ARGUMENT_PACK_P (arg))
3538 {
3539 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3540 if (num_result_args < 0)
3541 num_result_args = in_arg + num_packed;
3542 else
3543 num_result_args += num_packed;
3544 }
3545 else
3546 {
3547 if (num_result_args >= 0)
3548 num_result_args++;
3549 }
3550 }
3551
3552 /* If no expansion is necessary, we're done. */
3553 if (num_result_args < 0)
3554 return args;
3555
3556 /* Expand arguments. */
3557 result_args = make_tree_vec (num_result_args);
3558 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3559 non_default_args_count =
3560 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3561 for (in_arg = 0; in_arg < nargs; ++in_arg)
3562 {
3563 tree arg = TREE_VEC_ELT (args, in_arg);
3564 if (ARGUMENT_PACK_P (arg))
3565 {
3566 tree packed = ARGUMENT_PACK_ARGS (arg);
3567 int i, num_packed = TREE_VEC_LENGTH (packed);
3568 for (i = 0; i < num_packed; ++i, ++out_arg)
3569 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3570 if (non_default_args_count > 0)
3571 non_default_args_count += num_packed - 1;
3572 }
3573 else
3574 {
3575 TREE_VEC_ELT (result_args, out_arg) = arg;
3576 ++out_arg;
3577 }
3578 }
3579 if (non_default_args_count >= 0)
3580 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3581 return result_args;
3582 }
3583
3584 /* Checks if DECL shadows a template parameter.
3585
3586 [temp.local]: A template-parameter shall not be redeclared within its
3587 scope (including nested scopes).
3588
3589 Emits an error and returns TRUE if the DECL shadows a parameter,
3590 returns FALSE otherwise. */
3591
3592 bool
3593 check_template_shadow (tree decl)
3594 {
3595 tree olddecl;
3596
3597 /* If we're not in a template, we can't possibly shadow a template
3598 parameter. */
3599 if (!current_template_parms)
3600 return true;
3601
3602 /* Figure out what we're shadowing. */
3603 if (TREE_CODE (decl) == OVERLOAD)
3604 decl = OVL_CURRENT (decl);
3605 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3606
3607 /* If there's no previous binding for this name, we're not shadowing
3608 anything, let alone a template parameter. */
3609 if (!olddecl)
3610 return true;
3611
3612 /* If we're not shadowing a template parameter, we're done. Note
3613 that OLDDECL might be an OVERLOAD (or perhaps even an
3614 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3615 node. */
3616 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3617 return true;
3618
3619 /* We check for decl != olddecl to avoid bogus errors for using a
3620 name inside a class. We check TPFI to avoid duplicate errors for
3621 inline member templates. */
3622 if (decl == olddecl
3623 || (DECL_TEMPLATE_PARM_P (decl)
3624 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3625 return true;
3626
3627 /* Don't complain about the injected class name, as we've already
3628 complained about the class itself. */
3629 if (DECL_SELF_REFERENCE_P (decl))
3630 return false;
3631
3632 error ("declaration of %q+#D", decl);
3633 error (" shadows template parm %q+#D", olddecl);
3634 return false;
3635 }
3636
3637 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3638 ORIG_LEVEL, DECL, and TYPE. */
3639
3640 static tree
3641 build_template_parm_index (int index,
3642 int level,
3643 int orig_level,
3644 tree decl,
3645 tree type)
3646 {
3647 tree t = make_node (TEMPLATE_PARM_INDEX);
3648 TEMPLATE_PARM_IDX (t) = index;
3649 TEMPLATE_PARM_LEVEL (t) = level;
3650 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3651 TEMPLATE_PARM_DECL (t) = decl;
3652 TREE_TYPE (t) = type;
3653 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3654 TREE_READONLY (t) = TREE_READONLY (decl);
3655
3656 return t;
3657 }
3658
3659 /* Find the canonical type parameter for the given template type
3660 parameter. Returns the canonical type parameter, which may be TYPE
3661 if no such parameter existed. */
3662
3663 static tree
3664 canonical_type_parameter (tree type)
3665 {
3666 tree list;
3667 int idx = TEMPLATE_TYPE_IDX (type);
3668 if (!canonical_template_parms)
3669 vec_alloc (canonical_template_parms, idx+1);
3670
3671 while (canonical_template_parms->length () <= (unsigned)idx)
3672 vec_safe_push (canonical_template_parms, NULL_TREE);
3673
3674 list = (*canonical_template_parms)[idx];
3675 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3676 list = TREE_CHAIN (list);
3677
3678 if (list)
3679 return TREE_VALUE (list);
3680 else
3681 {
3682 (*canonical_template_parms)[idx]
3683 = tree_cons (NULL_TREE, type,
3684 (*canonical_template_parms)[idx]);
3685 return type;
3686 }
3687 }
3688
3689 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3690 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3691 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3692 new one is created. */
3693
3694 static tree
3695 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3696 tsubst_flags_t complain)
3697 {
3698 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3699 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3700 != TEMPLATE_PARM_LEVEL (index) - levels)
3701 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3702 {
3703 tree orig_decl = TEMPLATE_PARM_DECL (index);
3704 tree decl, t;
3705
3706 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3707 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3708 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3709 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3710 DECL_ARTIFICIAL (decl) = 1;
3711 SET_DECL_TEMPLATE_PARM_P (decl);
3712
3713 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3714 TEMPLATE_PARM_LEVEL (index) - levels,
3715 TEMPLATE_PARM_ORIG_LEVEL (index),
3716 decl, type);
3717 TEMPLATE_PARM_DESCENDANTS (index) = t;
3718 TEMPLATE_PARM_PARAMETER_PACK (t)
3719 = TEMPLATE_PARM_PARAMETER_PACK (index);
3720
3721 /* Template template parameters need this. */
3722 if (TREE_CODE (decl) == TEMPLATE_DECL)
3723 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3724 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3725 args, complain);
3726 }
3727
3728 return TEMPLATE_PARM_DESCENDANTS (index);
3729 }
3730
3731 /* Process information from new template parameter PARM and append it
3732 to the LIST being built. This new parameter is a non-type
3733 parameter iff IS_NON_TYPE is true. This new parameter is a
3734 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3735 is in PARM_LOC. */
3736
3737 tree
3738 process_template_parm (tree list, location_t parm_loc, tree parm,
3739 bool is_non_type, bool is_parameter_pack)
3740 {
3741 tree decl = 0;
3742 tree defval;
3743 int idx = 0;
3744
3745 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3746 defval = TREE_PURPOSE (parm);
3747
3748 if (list)
3749 {
3750 tree p = tree_last (list);
3751
3752 if (p && TREE_VALUE (p) != error_mark_node)
3753 {
3754 p = TREE_VALUE (p);
3755 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3756 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3757 else
3758 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3759 }
3760
3761 ++idx;
3762 }
3763
3764 if (is_non_type)
3765 {
3766 parm = TREE_VALUE (parm);
3767
3768 SET_DECL_TEMPLATE_PARM_P (parm);
3769
3770 if (TREE_TYPE (parm) != error_mark_node)
3771 {
3772 /* [temp.param]
3773
3774 The top-level cv-qualifiers on the template-parameter are
3775 ignored when determining its type. */
3776 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3777 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3778 TREE_TYPE (parm) = error_mark_node;
3779 else if (uses_parameter_packs (TREE_TYPE (parm))
3780 && !is_parameter_pack
3781 /* If we're in a nested template parameter list, the template
3782 template parameter could be a parameter pack. */
3783 && processing_template_parmlist == 1)
3784 {
3785 /* This template parameter is not a parameter pack, but it
3786 should be. Complain about "bare" parameter packs. */
3787 check_for_bare_parameter_packs (TREE_TYPE (parm));
3788
3789 /* Recover by calling this a parameter pack. */
3790 is_parameter_pack = true;
3791 }
3792 }
3793
3794 /* A template parameter is not modifiable. */
3795 TREE_CONSTANT (parm) = 1;
3796 TREE_READONLY (parm) = 1;
3797 decl = build_decl (parm_loc,
3798 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3799 TREE_CONSTANT (decl) = 1;
3800 TREE_READONLY (decl) = 1;
3801 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3802 = build_template_parm_index (idx, processing_template_decl,
3803 processing_template_decl,
3804 decl, TREE_TYPE (parm));
3805
3806 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3807 = is_parameter_pack;
3808 }
3809 else
3810 {
3811 tree t;
3812 parm = TREE_VALUE (TREE_VALUE (parm));
3813
3814 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3815 {
3816 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3817 /* This is for distinguishing between real templates and template
3818 template parameters */
3819 TREE_TYPE (parm) = t;
3820 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3821 decl = parm;
3822 }
3823 else
3824 {
3825 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3826 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3827 decl = build_decl (parm_loc,
3828 TYPE_DECL, parm, t);
3829 }
3830
3831 TYPE_NAME (t) = decl;
3832 TYPE_STUB_DECL (t) = decl;
3833 parm = decl;
3834 TEMPLATE_TYPE_PARM_INDEX (t)
3835 = build_template_parm_index (idx, processing_template_decl,
3836 processing_template_decl,
3837 decl, TREE_TYPE (parm));
3838 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3839 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3840 }
3841 DECL_ARTIFICIAL (decl) = 1;
3842 SET_DECL_TEMPLATE_PARM_P (decl);
3843 pushdecl (decl);
3844 parm = build_tree_list (defval, parm);
3845 return chainon (list, parm);
3846 }
3847
3848 /* The end of a template parameter list has been reached. Process the
3849 tree list into a parameter vector, converting each parameter into a more
3850 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3851 as PARM_DECLs. */
3852
3853 tree
3854 end_template_parm_list (tree parms)
3855 {
3856 int nparms;
3857 tree parm, next;
3858 tree saved_parmlist = make_tree_vec (list_length (parms));
3859
3860 current_template_parms
3861 = tree_cons (size_int (processing_template_decl),
3862 saved_parmlist, current_template_parms);
3863
3864 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3865 {
3866 next = TREE_CHAIN (parm);
3867 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3868 TREE_CHAIN (parm) = NULL_TREE;
3869 }
3870
3871 --processing_template_parmlist;
3872
3873 return saved_parmlist;
3874 }
3875
3876 /* end_template_decl is called after a template declaration is seen. */
3877
3878 void
3879 end_template_decl (void)
3880 {
3881 reset_specialization ();
3882
3883 if (! processing_template_decl)
3884 return;
3885
3886 /* This matches the pushlevel in begin_template_parm_list. */
3887 finish_scope ();
3888
3889 --processing_template_decl;
3890 current_template_parms = TREE_CHAIN (current_template_parms);
3891 }
3892
3893 /* Takes a TREE_LIST representing a template parameter and convert it
3894 into an argument suitable to be passed to the type substitution
3895 functions. Note that If the TREE_LIST contains an error_mark
3896 node, the returned argument is error_mark_node. */
3897
3898 static tree
3899 template_parm_to_arg (tree t)
3900 {
3901
3902 if (t == NULL_TREE
3903 || TREE_CODE (t) != TREE_LIST)
3904 return t;
3905
3906 if (error_operand_p (TREE_VALUE (t)))
3907 return error_mark_node;
3908
3909 t = TREE_VALUE (t);
3910
3911 if (TREE_CODE (t) == TYPE_DECL
3912 || TREE_CODE (t) == TEMPLATE_DECL)
3913 {
3914 t = TREE_TYPE (t);
3915
3916 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3917 {
3918 /* Turn this argument into a TYPE_ARGUMENT_PACK
3919 with a single element, which expands T. */
3920 tree vec = make_tree_vec (1);
3921 #ifdef ENABLE_CHECKING
3922 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3923 (vec, TREE_VEC_LENGTH (vec));
3924 #endif
3925 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3926
3927 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3928 SET_ARGUMENT_PACK_ARGS (t, vec);
3929 }
3930 }
3931 else
3932 {
3933 t = DECL_INITIAL (t);
3934
3935 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3936 {
3937 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3938 with a single element, which expands T. */
3939 tree vec = make_tree_vec (1);
3940 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3941 #ifdef ENABLE_CHECKING
3942 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3943 (vec, TREE_VEC_LENGTH (vec));
3944 #endif
3945 t = convert_from_reference (t);
3946 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3947
3948 t = make_node (NONTYPE_ARGUMENT_PACK);
3949 SET_ARGUMENT_PACK_ARGS (t, vec);
3950 TREE_TYPE (t) = type;
3951 }
3952 else
3953 t = convert_from_reference (t);
3954 }
3955 return t;
3956 }
3957
3958 /* Given a set of template parameters, return them as a set of template
3959 arguments. The template parameters are represented as a TREE_VEC, in
3960 the form documented in cp-tree.h for template arguments. */
3961
3962 static tree
3963 template_parms_to_args (tree parms)
3964 {
3965 tree header;
3966 tree args = NULL_TREE;
3967 int length = TMPL_PARMS_DEPTH (parms);
3968 int l = length;
3969
3970 /* If there is only one level of template parameters, we do not
3971 create a TREE_VEC of TREE_VECs. Instead, we return a single
3972 TREE_VEC containing the arguments. */
3973 if (length > 1)
3974 args = make_tree_vec (length);
3975
3976 for (header = parms; header; header = TREE_CHAIN (header))
3977 {
3978 tree a = copy_node (TREE_VALUE (header));
3979 int i;
3980
3981 TREE_TYPE (a) = NULL_TREE;
3982 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3983 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3984
3985 #ifdef ENABLE_CHECKING
3986 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3987 #endif
3988
3989 if (length > 1)
3990 TREE_VEC_ELT (args, --l) = a;
3991 else
3992 args = a;
3993 }
3994
3995 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3996 /* This can happen for template parms of a template template
3997 parameter, e.g:
3998
3999 template<template<class T, class U> class TT> struct S;
4000
4001 Consider the level of the parms of TT; T and U both have
4002 level 2; TT has no template parm of level 1. So in this case
4003 the first element of full_template_args is NULL_TREE. If we
4004 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4005 of 2. This will make tsubst wrongly consider that T and U
4006 have level 1. Instead, let's create a dummy vector as the
4007 first element of full_template_args so that TMPL_ARGS_DEPTH
4008 returns the correct depth for args. */
4009 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4010 return args;
4011 }
4012
4013 /* Within the declaration of a template, return the currently active
4014 template parameters as an argument TREE_VEC. */
4015
4016 static tree
4017 current_template_args (void)
4018 {
4019 return template_parms_to_args (current_template_parms);
4020 }
4021
4022 /* Update the declared TYPE by doing any lookups which were thought to be
4023 dependent, but are not now that we know the SCOPE of the declarator. */
4024
4025 tree
4026 maybe_update_decl_type (tree orig_type, tree scope)
4027 {
4028 tree type = orig_type;
4029
4030 if (type == NULL_TREE)
4031 return type;
4032
4033 if (TREE_CODE (orig_type) == TYPE_DECL)
4034 type = TREE_TYPE (type);
4035
4036 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4037 && dependent_type_p (type)
4038 /* Don't bother building up the args in this case. */
4039 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4040 {
4041 /* tsubst in the args corresponding to the template parameters,
4042 including auto if present. Most things will be unchanged, but
4043 make_typename_type and tsubst_qualified_id will resolve
4044 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4045 tree args = current_template_args ();
4046 tree auto_node = type_uses_auto (type);
4047 tree pushed;
4048 if (auto_node)
4049 {
4050 tree auto_vec = make_tree_vec (1);
4051 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4052 args = add_to_template_args (args, auto_vec);
4053 }
4054 pushed = push_scope (scope);
4055 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4056 if (pushed)
4057 pop_scope (scope);
4058 }
4059
4060 if (type == error_mark_node)
4061 return orig_type;
4062
4063 if (TREE_CODE (orig_type) == TYPE_DECL)
4064 {
4065 if (same_type_p (type, TREE_TYPE (orig_type)))
4066 type = orig_type;
4067 else
4068 type = TYPE_NAME (type);
4069 }
4070 return type;
4071 }
4072
4073 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4074 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4075 a member template. Used by push_template_decl below. */
4076
4077 static tree
4078 build_template_decl (tree decl, tree parms, bool member_template_p)
4079 {
4080 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4081 DECL_TEMPLATE_PARMS (tmpl) = parms;
4082 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4083 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4084 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4085
4086 return tmpl;
4087 }
4088
4089 struct template_parm_data
4090 {
4091 /* The level of the template parameters we are currently
4092 processing. */
4093 int level;
4094
4095 /* The index of the specialization argument we are currently
4096 processing. */
4097 int current_arg;
4098
4099 /* An array whose size is the number of template parameters. The
4100 elements are nonzero if the parameter has been used in any one
4101 of the arguments processed so far. */
4102 int* parms;
4103
4104 /* An array whose size is the number of template arguments. The
4105 elements are nonzero if the argument makes use of template
4106 parameters of this level. */
4107 int* arg_uses_template_parms;
4108 };
4109
4110 /* Subroutine of push_template_decl used to see if each template
4111 parameter in a partial specialization is used in the explicit
4112 argument list. If T is of the LEVEL given in DATA (which is
4113 treated as a template_parm_data*), then DATA->PARMS is marked
4114 appropriately. */
4115
4116 static int
4117 mark_template_parm (tree t, void* data)
4118 {
4119 int level;
4120 int idx;
4121 struct template_parm_data* tpd = (struct template_parm_data*) data;
4122
4123 template_parm_level_and_index (t, &level, &idx);
4124
4125 if (level == tpd->level)
4126 {
4127 tpd->parms[idx] = 1;
4128 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4129 }
4130
4131 /* Return zero so that for_each_template_parm will continue the
4132 traversal of the tree; we want to mark *every* template parm. */
4133 return 0;
4134 }
4135
4136 /* Process the partial specialization DECL. */
4137
4138 static tree
4139 process_partial_specialization (tree decl)
4140 {
4141 tree type = TREE_TYPE (decl);
4142 tree tinfo = get_template_info (decl);
4143 tree maintmpl = TI_TEMPLATE (tinfo);
4144 tree specargs = TI_ARGS (tinfo);
4145 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4146 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4147 tree inner_parms;
4148 tree inst;
4149 int nargs = TREE_VEC_LENGTH (inner_args);
4150 int ntparms;
4151 int i;
4152 bool did_error_intro = false;
4153 struct template_parm_data tpd;
4154 struct template_parm_data tpd2;
4155
4156 gcc_assert (current_template_parms);
4157
4158 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4159 ntparms = TREE_VEC_LENGTH (inner_parms);
4160
4161 /* We check that each of the template parameters given in the
4162 partial specialization is used in the argument list to the
4163 specialization. For example:
4164
4165 template <class T> struct S;
4166 template <class T> struct S<T*>;
4167
4168 The second declaration is OK because `T*' uses the template
4169 parameter T, whereas
4170
4171 template <class T> struct S<int>;
4172
4173 is no good. Even trickier is:
4174
4175 template <class T>
4176 struct S1
4177 {
4178 template <class U>
4179 struct S2;
4180 template <class U>
4181 struct S2<T>;
4182 };
4183
4184 The S2<T> declaration is actually invalid; it is a
4185 full-specialization. Of course,
4186
4187 template <class U>
4188 struct S2<T (*)(U)>;
4189
4190 or some such would have been OK. */
4191 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4192 tpd.parms = XALLOCAVEC (int, ntparms);
4193 memset (tpd.parms, 0, sizeof (int) * ntparms);
4194
4195 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4196 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4197 for (i = 0; i < nargs; ++i)
4198 {
4199 tpd.current_arg = i;
4200 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4201 &mark_template_parm,
4202 &tpd,
4203 NULL,
4204 /*include_nondeduced_p=*/false);
4205 }
4206 for (i = 0; i < ntparms; ++i)
4207 if (tpd.parms[i] == 0)
4208 {
4209 /* One of the template parms was not used in a deduced context in the
4210 specialization. */
4211 if (!did_error_intro)
4212 {
4213 error ("template parameters not deducible in "
4214 "partial specialization:");
4215 did_error_intro = true;
4216 }
4217
4218 inform (input_location, " %qD",
4219 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4220 }
4221
4222 if (did_error_intro)
4223 return error_mark_node;
4224
4225 /* [temp.class.spec]
4226
4227 The argument list of the specialization shall not be identical to
4228 the implicit argument list of the primary template. */
4229 tree main_args
4230 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4231 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4232 error ("partial specialization %qD does not specialize "
4233 "any template arguments", decl);
4234
4235 /* A partial specialization that replaces multiple parameters of the
4236 primary template with a pack expansion is less specialized for those
4237 parameters. */
4238 if (nargs < DECL_NTPARMS (maintmpl))
4239 {
4240 error ("partial specialization is not more specialized than the "
4241 "primary template because it replaces multiple parameters "
4242 "with a pack expansion");
4243 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4244 return decl;
4245 }
4246
4247 /* [temp.class.spec]
4248
4249 A partially specialized non-type argument expression shall not
4250 involve template parameters of the partial specialization except
4251 when the argument expression is a simple identifier.
4252
4253 The type of a template parameter corresponding to a specialized
4254 non-type argument shall not be dependent on a parameter of the
4255 specialization.
4256
4257 Also, we verify that pack expansions only occur at the
4258 end of the argument list. */
4259 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4260 tpd2.parms = 0;
4261 for (i = 0; i < nargs; ++i)
4262 {
4263 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4264 tree arg = TREE_VEC_ELT (inner_args, i);
4265 tree packed_args = NULL_TREE;
4266 int j, len = 1;
4267
4268 if (ARGUMENT_PACK_P (arg))
4269 {
4270 /* Extract the arguments from the argument pack. We'll be
4271 iterating over these in the following loop. */
4272 packed_args = ARGUMENT_PACK_ARGS (arg);
4273 len = TREE_VEC_LENGTH (packed_args);
4274 }
4275
4276 for (j = 0; j < len; j++)
4277 {
4278 if (packed_args)
4279 /* Get the Jth argument in the parameter pack. */
4280 arg = TREE_VEC_ELT (packed_args, j);
4281
4282 if (PACK_EXPANSION_P (arg))
4283 {
4284 /* Pack expansions must come at the end of the
4285 argument list. */
4286 if ((packed_args && j < len - 1)
4287 || (!packed_args && i < nargs - 1))
4288 {
4289 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4290 error ("parameter pack argument %qE must be at the "
4291 "end of the template argument list", arg);
4292 else
4293 error ("parameter pack argument %qT must be at the "
4294 "end of the template argument list", arg);
4295 }
4296 }
4297
4298 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4299 /* We only care about the pattern. */
4300 arg = PACK_EXPANSION_PATTERN (arg);
4301
4302 if (/* These first two lines are the `non-type' bit. */
4303 !TYPE_P (arg)
4304 && TREE_CODE (arg) != TEMPLATE_DECL
4305 /* This next two lines are the `argument expression is not just a
4306 simple identifier' condition and also the `specialized
4307 non-type argument' bit. */
4308 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4309 && !(REFERENCE_REF_P (arg)
4310 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4311 {
4312 if ((!packed_args && tpd.arg_uses_template_parms[i])
4313 || (packed_args && uses_template_parms (arg)))
4314 error ("template argument %qE involves template parameter(s)",
4315 arg);
4316 else
4317 {
4318 /* Look at the corresponding template parameter,
4319 marking which template parameters its type depends
4320 upon. */
4321 tree type = TREE_TYPE (parm);
4322
4323 if (!tpd2.parms)
4324 {
4325 /* We haven't yet initialized TPD2. Do so now. */
4326 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4327 /* The number of parameters here is the number in the
4328 main template, which, as checked in the assertion
4329 above, is NARGS. */
4330 tpd2.parms = XALLOCAVEC (int, nargs);
4331 tpd2.level =
4332 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4333 }
4334
4335 /* Mark the template parameters. But this time, we're
4336 looking for the template parameters of the main
4337 template, not in the specialization. */
4338 tpd2.current_arg = i;
4339 tpd2.arg_uses_template_parms[i] = 0;
4340 memset (tpd2.parms, 0, sizeof (int) * nargs);
4341 for_each_template_parm (type,
4342 &mark_template_parm,
4343 &tpd2,
4344 NULL,
4345 /*include_nondeduced_p=*/false);
4346
4347 if (tpd2.arg_uses_template_parms [i])
4348 {
4349 /* The type depended on some template parameters.
4350 If they are fully specialized in the
4351 specialization, that's OK. */
4352 int j;
4353 int count = 0;
4354 for (j = 0; j < nargs; ++j)
4355 if (tpd2.parms[j] != 0
4356 && tpd.arg_uses_template_parms [j])
4357 ++count;
4358 if (count != 0)
4359 error_n (input_location, count,
4360 "type %qT of template argument %qE depends "
4361 "on a template parameter",
4362 "type %qT of template argument %qE depends "
4363 "on template parameters",
4364 type,
4365 arg);
4366 }
4367 }
4368 }
4369 }
4370 }
4371
4372 /* We should only get here once. */
4373 if (TREE_CODE (decl) == TYPE_DECL)
4374 gcc_assert (!COMPLETE_TYPE_P (type));
4375
4376 tree tmpl = build_template_decl (decl, current_template_parms,
4377 DECL_MEMBER_TEMPLATE_P (maintmpl));
4378 TREE_TYPE (tmpl) = type;
4379 DECL_TEMPLATE_RESULT (tmpl) = decl;
4380 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4381 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4382 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4383
4384 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4385 = tree_cons (specargs, tmpl,
4386 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4387 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4388
4389 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4390 inst = TREE_CHAIN (inst))
4391 {
4392 tree instance = TREE_VALUE (inst);
4393 if (TYPE_P (instance)
4394 ? (COMPLETE_TYPE_P (instance)
4395 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4396 : DECL_TEMPLATE_INSTANTIATION (instance))
4397 {
4398 tree spec = most_specialized_partial_spec (instance, tf_none);
4399 if (spec && TREE_VALUE (spec) == tmpl)
4400 {
4401 tree inst_decl = (DECL_P (instance)
4402 ? instance : TYPE_NAME (instance));
4403 permerror (input_location,
4404 "partial specialization of %qD after instantiation "
4405 "of %qD", decl, inst_decl);
4406 }
4407 }
4408 }
4409
4410 return decl;
4411 }
4412
4413 /* PARM is a template parameter of some form; return the corresponding
4414 TEMPLATE_PARM_INDEX. */
4415
4416 static tree
4417 get_template_parm_index (tree parm)
4418 {
4419 if (TREE_CODE (parm) == PARM_DECL
4420 || TREE_CODE (parm) == CONST_DECL)
4421 parm = DECL_INITIAL (parm);
4422 else if (TREE_CODE (parm) == TYPE_DECL
4423 || TREE_CODE (parm) == TEMPLATE_DECL)
4424 parm = TREE_TYPE (parm);
4425 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4426 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4427 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4428 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4429 return parm;
4430 }
4431
4432 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4433 parameter packs used by the template parameter PARM. */
4434
4435 static void
4436 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4437 {
4438 /* A type parm can't refer to another parm. */
4439 if (TREE_CODE (parm) == TYPE_DECL)
4440 return;
4441 else if (TREE_CODE (parm) == PARM_DECL)
4442 {
4443 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4444 ppd, ppd->visited);
4445 return;
4446 }
4447
4448 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4449
4450 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4451 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4452 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4453 }
4454
4455 /* PARM is a template parameter pack. Return any parameter packs used in
4456 its type or the type of any of its template parameters. If there are
4457 any such packs, it will be instantiated into a fixed template parameter
4458 list by partial instantiation rather than be fully deduced. */
4459
4460 tree
4461 fixed_parameter_pack_p (tree parm)
4462 {
4463 /* This can only be true in a member template. */
4464 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4465 return NULL_TREE;
4466 /* This can only be true for a parameter pack. */
4467 if (!template_parameter_pack_p (parm))
4468 return NULL_TREE;
4469 /* A type parm can't refer to another parm. */
4470 if (TREE_CODE (parm) == TYPE_DECL)
4471 return NULL_TREE;
4472
4473 tree parameter_packs = NULL_TREE;
4474 struct find_parameter_pack_data ppd;
4475 ppd.parameter_packs = &parameter_packs;
4476 ppd.visited = new hash_set<tree>;
4477
4478 fixed_parameter_pack_p_1 (parm, &ppd);
4479
4480 delete ppd.visited;
4481 return parameter_packs;
4482 }
4483
4484 /* Check that a template declaration's use of default arguments and
4485 parameter packs is not invalid. Here, PARMS are the template
4486 parameters. IS_PRIMARY is true if DECL is the thing declared by
4487 a primary template. IS_PARTIAL is true if DECL is a partial
4488 specialization.
4489
4490 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4491 declaration (but not a definition); 1 indicates a declaration, 2
4492 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4493 emitted for extraneous default arguments.
4494
4495 Returns TRUE if there were no errors found, FALSE otherwise. */
4496
4497 bool
4498 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4499 bool is_partial, int is_friend_decl)
4500 {
4501 const char *msg;
4502 int last_level_to_check;
4503 tree parm_level;
4504 bool no_errors = true;
4505
4506 /* [temp.param]
4507
4508 A default template-argument shall not be specified in a
4509 function template declaration or a function template definition, nor
4510 in the template-parameter-list of the definition of a member of a
4511 class template. */
4512
4513 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4514 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4515 /* You can't have a function template declaration in a local
4516 scope, nor you can you define a member of a class template in a
4517 local scope. */
4518 return true;
4519
4520 if ((TREE_CODE (decl) == TYPE_DECL
4521 && TREE_TYPE (decl)
4522 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4523 || (TREE_CODE (decl) == FUNCTION_DECL
4524 && LAMBDA_FUNCTION_P (decl)))
4525 /* A lambda doesn't have an explicit declaration; don't complain
4526 about the parms of the enclosing class. */
4527 return true;
4528
4529 if (current_class_type
4530 && !TYPE_BEING_DEFINED (current_class_type)
4531 && DECL_LANG_SPECIFIC (decl)
4532 && DECL_DECLARES_FUNCTION_P (decl)
4533 /* If this is either a friend defined in the scope of the class
4534 or a member function. */
4535 && (DECL_FUNCTION_MEMBER_P (decl)
4536 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4537 : DECL_FRIEND_CONTEXT (decl)
4538 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4539 : false)
4540 /* And, if it was a member function, it really was defined in
4541 the scope of the class. */
4542 && (!DECL_FUNCTION_MEMBER_P (decl)
4543 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4544 /* We already checked these parameters when the template was
4545 declared, so there's no need to do it again now. This function
4546 was defined in class scope, but we're processing its body now
4547 that the class is complete. */
4548 return true;
4549
4550 /* Core issue 226 (C++0x only): the following only applies to class
4551 templates. */
4552 if (is_primary
4553 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4554 {
4555 /* [temp.param]
4556
4557 If a template-parameter has a default template-argument, all
4558 subsequent template-parameters shall have a default
4559 template-argument supplied. */
4560 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4561 {
4562 tree inner_parms = TREE_VALUE (parm_level);
4563 int ntparms = TREE_VEC_LENGTH (inner_parms);
4564 int seen_def_arg_p = 0;
4565 int i;
4566
4567 for (i = 0; i < ntparms; ++i)
4568 {
4569 tree parm = TREE_VEC_ELT (inner_parms, i);
4570
4571 if (parm == error_mark_node)
4572 continue;
4573
4574 if (TREE_PURPOSE (parm))
4575 seen_def_arg_p = 1;
4576 else if (seen_def_arg_p
4577 && !template_parameter_pack_p (TREE_VALUE (parm)))
4578 {
4579 error ("no default argument for %qD", TREE_VALUE (parm));
4580 /* For better subsequent error-recovery, we indicate that
4581 there should have been a default argument. */
4582 TREE_PURPOSE (parm) = error_mark_node;
4583 no_errors = false;
4584 }
4585 else if (!is_partial
4586 && !is_friend_decl
4587 /* Don't complain about an enclosing partial
4588 specialization. */
4589 && parm_level == parms
4590 && TREE_CODE (decl) == TYPE_DECL
4591 && i < ntparms - 1
4592 && template_parameter_pack_p (TREE_VALUE (parm))
4593 /* A fixed parameter pack will be partially
4594 instantiated into a fixed length list. */
4595 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4596 {
4597 /* A primary class template can only have one
4598 parameter pack, at the end of the template
4599 parameter list. */
4600
4601 error ("parameter pack %q+D must be at the end of the"
4602 " template parameter list", TREE_VALUE (parm));
4603
4604 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4605 = error_mark_node;
4606 no_errors = false;
4607 }
4608 }
4609 }
4610 }
4611
4612 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4613 || is_partial
4614 || !is_primary
4615 || is_friend_decl)
4616 /* For an ordinary class template, default template arguments are
4617 allowed at the innermost level, e.g.:
4618 template <class T = int>
4619 struct S {};
4620 but, in a partial specialization, they're not allowed even
4621 there, as we have in [temp.class.spec]:
4622
4623 The template parameter list of a specialization shall not
4624 contain default template argument values.
4625
4626 So, for a partial specialization, or for a function template
4627 (in C++98/C++03), we look at all of them. */
4628 ;
4629 else
4630 /* But, for a primary class template that is not a partial
4631 specialization we look at all template parameters except the
4632 innermost ones. */
4633 parms = TREE_CHAIN (parms);
4634
4635 /* Figure out what error message to issue. */
4636 if (is_friend_decl == 2)
4637 msg = G_("default template arguments may not be used in function template "
4638 "friend re-declaration");
4639 else if (is_friend_decl)
4640 msg = G_("default template arguments may not be used in function template "
4641 "friend declarations");
4642 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4643 msg = G_("default template arguments may not be used in function templates "
4644 "without -std=c++11 or -std=gnu++11");
4645 else if (is_partial)
4646 msg = G_("default template arguments may not be used in "
4647 "partial specializations");
4648 else
4649 msg = G_("default argument for template parameter for class enclosing %qD");
4650
4651 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4652 /* If we're inside a class definition, there's no need to
4653 examine the parameters to the class itself. On the one
4654 hand, they will be checked when the class is defined, and,
4655 on the other, default arguments are valid in things like:
4656 template <class T = double>
4657 struct S { template <class U> void f(U); };
4658 Here the default argument for `S' has no bearing on the
4659 declaration of `f'. */
4660 last_level_to_check = template_class_depth (current_class_type) + 1;
4661 else
4662 /* Check everything. */
4663 last_level_to_check = 0;
4664
4665 for (parm_level = parms;
4666 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4667 parm_level = TREE_CHAIN (parm_level))
4668 {
4669 tree inner_parms = TREE_VALUE (parm_level);
4670 int i;
4671 int ntparms;
4672
4673 ntparms = TREE_VEC_LENGTH (inner_parms);
4674 for (i = 0; i < ntparms; ++i)
4675 {
4676 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4677 continue;
4678
4679 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4680 {
4681 if (msg)
4682 {
4683 no_errors = false;
4684 if (is_friend_decl == 2)
4685 return no_errors;
4686
4687 error (msg, decl);
4688 msg = 0;
4689 }
4690
4691 /* Clear out the default argument so that we are not
4692 confused later. */
4693 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4694 }
4695 }
4696
4697 /* At this point, if we're still interested in issuing messages,
4698 they must apply to classes surrounding the object declared. */
4699 if (msg)
4700 msg = G_("default argument for template parameter for class "
4701 "enclosing %qD");
4702 }
4703
4704 return no_errors;
4705 }
4706
4707 /* Worker for push_template_decl_real, called via
4708 for_each_template_parm. DATA is really an int, indicating the
4709 level of the parameters we are interested in. If T is a template
4710 parameter of that level, return nonzero. */
4711
4712 static int
4713 template_parm_this_level_p (tree t, void* data)
4714 {
4715 int this_level = *(int *)data;
4716 int level;
4717
4718 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4719 level = TEMPLATE_PARM_LEVEL (t);
4720 else
4721 level = TEMPLATE_TYPE_LEVEL (t);
4722 return level == this_level;
4723 }
4724
4725 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4726 parameters given by current_template_args, or reuses a
4727 previously existing one, if appropriate. Returns the DECL, or an
4728 equivalent one, if it is replaced via a call to duplicate_decls.
4729
4730 If IS_FRIEND is true, DECL is a friend declaration. */
4731
4732 tree
4733 push_template_decl_real (tree decl, bool is_friend)
4734 {
4735 tree tmpl;
4736 tree args;
4737 tree info;
4738 tree ctx;
4739 bool is_primary;
4740 bool is_partial;
4741 int new_template_p = 0;
4742 /* True if the template is a member template, in the sense of
4743 [temp.mem]. */
4744 bool member_template_p = false;
4745
4746 if (decl == error_mark_node || !current_template_parms)
4747 return error_mark_node;
4748
4749 /* See if this is a partial specialization. */
4750 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
4751 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4752 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4753 || (TREE_CODE (decl) == VAR_DECL
4754 && DECL_LANG_SPECIFIC (decl)
4755 && DECL_TEMPLATE_SPECIALIZATION (decl)
4756 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
4757
4758 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4759 is_friend = true;
4760
4761 if (is_friend)
4762 /* For a friend, we want the context of the friend function, not
4763 the type of which it is a friend. */
4764 ctx = CP_DECL_CONTEXT (decl);
4765 else if (CP_DECL_CONTEXT (decl)
4766 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4767 /* In the case of a virtual function, we want the class in which
4768 it is defined. */
4769 ctx = CP_DECL_CONTEXT (decl);
4770 else
4771 /* Otherwise, if we're currently defining some class, the DECL
4772 is assumed to be a member of the class. */
4773 ctx = current_scope ();
4774
4775 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4776 ctx = NULL_TREE;
4777
4778 if (!DECL_CONTEXT (decl))
4779 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4780
4781 /* See if this is a primary template. */
4782 if (is_friend && ctx
4783 && uses_template_parms_level (ctx, processing_template_decl))
4784 /* A friend template that specifies a class context, i.e.
4785 template <typename T> friend void A<T>::f();
4786 is not primary. */
4787 is_primary = false;
4788 else if (TREE_CODE (decl) == TYPE_DECL
4789 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4790 is_primary = false;
4791 else
4792 is_primary = template_parm_scope_p ();
4793
4794 if (is_primary)
4795 {
4796 if (DECL_CLASS_SCOPE_P (decl))
4797 member_template_p = true;
4798 if (TREE_CODE (decl) == TYPE_DECL
4799 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4800 {
4801 error ("template class without a name");
4802 return error_mark_node;
4803 }
4804 else if (TREE_CODE (decl) == FUNCTION_DECL)
4805 {
4806 if (member_template_p)
4807 {
4808 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
4809 error ("member template %qD may not have virt-specifiers", decl);
4810 }
4811 if (DECL_DESTRUCTOR_P (decl))
4812 {
4813 /* [temp.mem]
4814
4815 A destructor shall not be a member template. */
4816 error ("destructor %qD declared as member template", decl);
4817 return error_mark_node;
4818 }
4819 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4820 && (!prototype_p (TREE_TYPE (decl))
4821 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4822 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4823 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4824 == void_list_node)))
4825 {
4826 /* [basic.stc.dynamic.allocation]
4827
4828 An allocation function can be a function
4829 template. ... Template allocation functions shall
4830 have two or more parameters. */
4831 error ("invalid template declaration of %qD", decl);
4832 return error_mark_node;
4833 }
4834 }
4835 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4836 && CLASS_TYPE_P (TREE_TYPE (decl)))
4837 /* OK */;
4838 else if (TREE_CODE (decl) == TYPE_DECL
4839 && TYPE_DECL_ALIAS_P (decl))
4840 /* alias-declaration */
4841 gcc_assert (!DECL_ARTIFICIAL (decl));
4842 else if (VAR_P (decl))
4843 /* C++14 variable template. */;
4844 else
4845 {
4846 error ("template declaration of %q#D", decl);
4847 return error_mark_node;
4848 }
4849 }
4850
4851 /* Check to see that the rules regarding the use of default
4852 arguments are not being violated. */
4853 check_default_tmpl_args (decl, current_template_parms,
4854 is_primary, is_partial, /*is_friend_decl=*/0);
4855
4856 /* Ensure that there are no parameter packs in the type of this
4857 declaration that have not been expanded. */
4858 if (TREE_CODE (decl) == FUNCTION_DECL)
4859 {
4860 /* Check each of the arguments individually to see if there are
4861 any bare parameter packs. */
4862 tree type = TREE_TYPE (decl);
4863 tree arg = DECL_ARGUMENTS (decl);
4864 tree argtype = TYPE_ARG_TYPES (type);
4865
4866 while (arg && argtype)
4867 {
4868 if (!DECL_PACK_P (arg)
4869 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4870 {
4871 /* This is a PARM_DECL that contains unexpanded parameter
4872 packs. We have already complained about this in the
4873 check_for_bare_parameter_packs call, so just replace
4874 these types with ERROR_MARK_NODE. */
4875 TREE_TYPE (arg) = error_mark_node;
4876 TREE_VALUE (argtype) = error_mark_node;
4877 }
4878
4879 arg = DECL_CHAIN (arg);
4880 argtype = TREE_CHAIN (argtype);
4881 }
4882
4883 /* Check for bare parameter packs in the return type and the
4884 exception specifiers. */
4885 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4886 /* Errors were already issued, set return type to int
4887 as the frontend doesn't expect error_mark_node as
4888 the return type. */
4889 TREE_TYPE (type) = integer_type_node;
4890 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4891 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4892 }
4893 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4894 && TYPE_DECL_ALIAS_P (decl))
4895 ? DECL_ORIGINAL_TYPE (decl)
4896 : TREE_TYPE (decl)))
4897 {
4898 TREE_TYPE (decl) = error_mark_node;
4899 return error_mark_node;
4900 }
4901
4902 if (is_partial)
4903 return process_partial_specialization (decl);
4904
4905 args = current_template_args ();
4906
4907 if (!ctx
4908 || TREE_CODE (ctx) == FUNCTION_DECL
4909 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4910 || (TREE_CODE (decl) == TYPE_DECL
4911 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4912 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4913 {
4914 if (DECL_LANG_SPECIFIC (decl)
4915 && DECL_TEMPLATE_INFO (decl)
4916 && DECL_TI_TEMPLATE (decl))
4917 tmpl = DECL_TI_TEMPLATE (decl);
4918 /* If DECL is a TYPE_DECL for a class-template, then there won't
4919 be DECL_LANG_SPECIFIC. The information equivalent to
4920 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4921 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4922 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4923 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4924 {
4925 /* Since a template declaration already existed for this
4926 class-type, we must be redeclaring it here. Make sure
4927 that the redeclaration is valid. */
4928 redeclare_class_template (TREE_TYPE (decl),
4929 current_template_parms);
4930 /* We don't need to create a new TEMPLATE_DECL; just use the
4931 one we already had. */
4932 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4933 }
4934 else
4935 {
4936 tmpl = build_template_decl (decl, current_template_parms,
4937 member_template_p);
4938 new_template_p = 1;
4939
4940 if (DECL_LANG_SPECIFIC (decl)
4941 && DECL_TEMPLATE_SPECIALIZATION (decl))
4942 {
4943 /* A specialization of a member template of a template
4944 class. */
4945 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4946 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4947 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4948 }
4949 }
4950 }
4951 else
4952 {
4953 tree a, t, current, parms;
4954 int i;
4955 tree tinfo = get_template_info (decl);
4956
4957 if (!tinfo)
4958 {
4959 error ("template definition of non-template %q#D", decl);
4960 return error_mark_node;
4961 }
4962
4963 tmpl = TI_TEMPLATE (tinfo);
4964
4965 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4966 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4967 && DECL_TEMPLATE_SPECIALIZATION (decl)
4968 && DECL_MEMBER_TEMPLATE_P (tmpl))
4969 {
4970 tree new_tmpl;
4971
4972 /* The declaration is a specialization of a member
4973 template, declared outside the class. Therefore, the
4974 innermost template arguments will be NULL, so we
4975 replace them with the arguments determined by the
4976 earlier call to check_explicit_specialization. */
4977 args = DECL_TI_ARGS (decl);
4978
4979 new_tmpl
4980 = build_template_decl (decl, current_template_parms,
4981 member_template_p);
4982 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4983 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4984 DECL_TI_TEMPLATE (decl) = new_tmpl;
4985 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4986 DECL_TEMPLATE_INFO (new_tmpl)
4987 = build_template_info (tmpl, args);
4988
4989 register_specialization (new_tmpl,
4990 most_general_template (tmpl),
4991 args,
4992 is_friend, 0);
4993 return decl;
4994 }
4995
4996 /* Make sure the template headers we got make sense. */
4997
4998 parms = DECL_TEMPLATE_PARMS (tmpl);
4999 i = TMPL_PARMS_DEPTH (parms);
5000 if (TMPL_ARGS_DEPTH (args) != i)
5001 {
5002 error ("expected %d levels of template parms for %q#D, got %d",
5003 i, decl, TMPL_ARGS_DEPTH (args));
5004 DECL_INTERFACE_KNOWN (decl) = 1;
5005 return error_mark_node;
5006 }
5007 else
5008 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5009 {
5010 a = TMPL_ARGS_LEVEL (args, i);
5011 t = INNERMOST_TEMPLATE_PARMS (parms);
5012
5013 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5014 {
5015 if (current == decl)
5016 error ("got %d template parameters for %q#D",
5017 TREE_VEC_LENGTH (a), decl);
5018 else
5019 error ("got %d template parameters for %q#T",
5020 TREE_VEC_LENGTH (a), current);
5021 error (" but %d required", TREE_VEC_LENGTH (t));
5022 /* Avoid crash in import_export_decl. */
5023 DECL_INTERFACE_KNOWN (decl) = 1;
5024 return error_mark_node;
5025 }
5026
5027 if (current == decl)
5028 current = ctx;
5029 else if (current == NULL_TREE)
5030 /* Can happen in erroneous input. */
5031 break;
5032 else
5033 current = get_containing_scope (current);
5034 }
5035
5036 /* Check that the parms are used in the appropriate qualifying scopes
5037 in the declarator. */
5038 if (!comp_template_args
5039 (TI_ARGS (tinfo),
5040 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5041 {
5042 error ("\
5043 template arguments to %qD do not match original template %qD",
5044 decl, DECL_TEMPLATE_RESULT (tmpl));
5045 if (!uses_template_parms (TI_ARGS (tinfo)))
5046 inform (input_location, "use template<> for an explicit specialization");
5047 /* Avoid crash in import_export_decl. */
5048 DECL_INTERFACE_KNOWN (decl) = 1;
5049 return error_mark_node;
5050 }
5051 }
5052
5053 DECL_TEMPLATE_RESULT (tmpl) = decl;
5054 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5055
5056 /* Push template declarations for global functions and types. Note
5057 that we do not try to push a global template friend declared in a
5058 template class; such a thing may well depend on the template
5059 parameters of the class. */
5060 if (new_template_p && !ctx
5061 && !(is_friend && template_class_depth (current_class_type) > 0))
5062 {
5063 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5064 if (tmpl == error_mark_node)
5065 return error_mark_node;
5066
5067 /* Hide template friend classes that haven't been declared yet. */
5068 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5069 {
5070 DECL_ANTICIPATED (tmpl) = 1;
5071 DECL_FRIEND_P (tmpl) = 1;
5072 }
5073 }
5074
5075 if (is_primary)
5076 {
5077 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5078 int i;
5079
5080 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5081 if (DECL_CONV_FN_P (tmpl))
5082 {
5083 int depth = TMPL_PARMS_DEPTH (parms);
5084
5085 /* It is a conversion operator. See if the type converted to
5086 depends on innermost template operands. */
5087
5088 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5089 depth))
5090 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5091 }
5092
5093 /* Give template template parms a DECL_CONTEXT of the template
5094 for which they are a parameter. */
5095 parms = INNERMOST_TEMPLATE_PARMS (parms);
5096 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5097 {
5098 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5099 if (TREE_CODE (parm) == TEMPLATE_DECL)
5100 DECL_CONTEXT (parm) = tmpl;
5101 }
5102 }
5103
5104 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5105 back to its most general template. If TMPL is a specialization,
5106 ARGS may only have the innermost set of arguments. Add the missing
5107 argument levels if necessary. */
5108 if (DECL_TEMPLATE_INFO (tmpl))
5109 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5110
5111 info = build_template_info (tmpl, args);
5112
5113 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5114 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5115 else
5116 {
5117 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5118 retrofit_lang_decl (decl);
5119 if (DECL_LANG_SPECIFIC (decl))
5120 DECL_TEMPLATE_INFO (decl) = info;
5121 }
5122
5123 if (flag_implicit_templates
5124 && !is_friend
5125 && TREE_PUBLIC (decl)
5126 && VAR_OR_FUNCTION_DECL_P (decl))
5127 /* Set DECL_COMDAT on template instantiations; if we force
5128 them to be emitted by explicit instantiation or -frepo,
5129 mark_needed will tell cgraph to do the right thing. */
5130 DECL_COMDAT (decl) = true;
5131
5132 return DECL_TEMPLATE_RESULT (tmpl);
5133 }
5134
5135 tree
5136 push_template_decl (tree decl)
5137 {
5138 return push_template_decl_real (decl, false);
5139 }
5140
5141 /* FN is an inheriting constructor that inherits from the constructor
5142 template INHERITED; turn FN into a constructor template with a matching
5143 template header. */
5144
5145 tree
5146 add_inherited_template_parms (tree fn, tree inherited)
5147 {
5148 tree inner_parms
5149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5150 inner_parms = copy_node (inner_parms);
5151 tree parms
5152 = tree_cons (size_int (processing_template_decl + 1),
5153 inner_parms, current_template_parms);
5154 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5155 tree args = template_parms_to_args (parms);
5156 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5157 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5158 DECL_TEMPLATE_RESULT (tmpl) = fn;
5159 DECL_ARTIFICIAL (tmpl) = true;
5160 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5161 return tmpl;
5162 }
5163
5164 /* Called when a class template TYPE is redeclared with the indicated
5165 template PARMS, e.g.:
5166
5167 template <class T> struct S;
5168 template <class T> struct S {}; */
5169
5170 bool
5171 redeclare_class_template (tree type, tree parms)
5172 {
5173 tree tmpl;
5174 tree tmpl_parms;
5175 int i;
5176
5177 if (!TYPE_TEMPLATE_INFO (type))
5178 {
5179 error ("%qT is not a template type", type);
5180 return false;
5181 }
5182
5183 tmpl = TYPE_TI_TEMPLATE (type);
5184 if (!PRIMARY_TEMPLATE_P (tmpl))
5185 /* The type is nested in some template class. Nothing to worry
5186 about here; there are no new template parameters for the nested
5187 type. */
5188 return true;
5189
5190 if (!parms)
5191 {
5192 error ("template specifiers not specified in declaration of %qD",
5193 tmpl);
5194 return false;
5195 }
5196
5197 parms = INNERMOST_TEMPLATE_PARMS (parms);
5198 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5199
5200 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5201 {
5202 error_n (input_location, TREE_VEC_LENGTH (parms),
5203 "redeclared with %d template parameter",
5204 "redeclared with %d template parameters",
5205 TREE_VEC_LENGTH (parms));
5206 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5207 "previous declaration %q+D used %d template parameter",
5208 "previous declaration %q+D used %d template parameters",
5209 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5210 return false;
5211 }
5212
5213 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5214 {
5215 tree tmpl_parm;
5216 tree parm;
5217 tree tmpl_default;
5218 tree parm_default;
5219
5220 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5221 || TREE_VEC_ELT (parms, i) == error_mark_node)
5222 continue;
5223
5224 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5225 if (error_operand_p (tmpl_parm))
5226 return false;
5227
5228 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5229 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5230 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5231
5232 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5233 TEMPLATE_DECL. */
5234 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5235 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5236 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5237 || (TREE_CODE (tmpl_parm) != PARM_DECL
5238 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5239 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5240 || (TREE_CODE (tmpl_parm) == PARM_DECL
5241 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5242 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5243 {
5244 error ("template parameter %q+#D", tmpl_parm);
5245 error ("redeclared here as %q#D", parm);
5246 return false;
5247 }
5248
5249 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5250 {
5251 /* We have in [temp.param]:
5252
5253 A template-parameter may not be given default arguments
5254 by two different declarations in the same scope. */
5255 error_at (input_location, "redefinition of default argument for %q#D", parm);
5256 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5257 "original definition appeared here");
5258 return false;
5259 }
5260
5261 if (parm_default != NULL_TREE)
5262 /* Update the previous template parameters (which are the ones
5263 that will really count) with the new default value. */
5264 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5265 else if (tmpl_default != NULL_TREE)
5266 /* Update the new parameters, too; they'll be used as the
5267 parameters for any members. */
5268 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5269 }
5270
5271 return true;
5272 }
5273
5274 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5275 to be used when the caller has already checked
5276 (processing_template_decl
5277 && !instantiation_dependent_expression_p (expr)
5278 && potential_constant_expression (expr))
5279 and cleared processing_template_decl. */
5280
5281 tree
5282 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5283 {
5284 return tsubst_copy_and_build (expr,
5285 /*args=*/NULL_TREE,
5286 complain,
5287 /*in_decl=*/NULL_TREE,
5288 /*function_p=*/false,
5289 /*integral_constant_expression_p=*/true);
5290 }
5291
5292 /* Simplify EXPR if it is a non-dependent expression. Returns the
5293 (possibly simplified) expression. */
5294
5295 tree
5296 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5297 {
5298 if (expr == NULL_TREE)
5299 return NULL_TREE;
5300
5301 /* If we're in a template, but EXPR isn't value dependent, simplify
5302 it. We're supposed to treat:
5303
5304 template <typename T> void f(T[1 + 1]);
5305 template <typename T> void f(T[2]);
5306
5307 as two declarations of the same function, for example. */
5308 if (processing_template_decl
5309 && !instantiation_dependent_expression_p (expr)
5310 && potential_constant_expression (expr))
5311 {
5312 processing_template_decl_sentinel s;
5313 expr = instantiate_non_dependent_expr_internal (expr, complain);
5314 }
5315 return expr;
5316 }
5317
5318 tree
5319 instantiate_non_dependent_expr (tree expr)
5320 {
5321 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5322 }
5323
5324 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5325 template declaration, or a TYPE_DECL for an alias declaration. */
5326
5327 bool
5328 alias_type_or_template_p (tree t)
5329 {
5330 if (t == NULL_TREE)
5331 return false;
5332 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5333 || (TYPE_P (t)
5334 && TYPE_NAME (t)
5335 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5336 || DECL_ALIAS_TEMPLATE_P (t));
5337 }
5338
5339 /* Return TRUE iff T is a specialization of an alias template. */
5340
5341 bool
5342 alias_template_specialization_p (const_tree t)
5343 {
5344 /* It's an alias template specialization if it's an alias and its
5345 TYPE_NAME is a specialization of a primary template. */
5346 if (TYPE_ALIAS_P (t))
5347 {
5348 tree name = TYPE_NAME (t);
5349 if (DECL_LANG_SPECIFIC (name))
5350 if (tree ti = DECL_TEMPLATE_INFO (name))
5351 {
5352 tree tmpl = TI_TEMPLATE (ti);
5353 return PRIMARY_TEMPLATE_P (tmpl);
5354 }
5355 }
5356 return false;
5357 }
5358
5359 /* Return TRUE iff T is a specialization of an alias template with
5360 dependent template-arguments. */
5361
5362 bool
5363 dependent_alias_template_spec_p (const_tree t)
5364 {
5365 return (alias_template_specialization_p (t)
5366 && (any_dependent_template_arguments_p
5367 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5368 }
5369
5370 /* Return the number of innermost template parameters in TMPL. */
5371
5372 static int
5373 num_innermost_template_parms (tree tmpl)
5374 {
5375 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5376 return TREE_VEC_LENGTH (parms);
5377 }
5378
5379 /* Return either TMPL or another template that it is equivalent to under DR
5380 1286: An alias that just changes the name of a template is equivalent to
5381 the other template. */
5382
5383 static tree
5384 get_underlying_template (tree tmpl)
5385 {
5386 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5387 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5388 {
5389 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5390 if (TYPE_TEMPLATE_INFO (result))
5391 {
5392 tree sub = TYPE_TI_TEMPLATE (result);
5393 if (PRIMARY_TEMPLATE_P (sub)
5394 && (num_innermost_template_parms (tmpl)
5395 == num_innermost_template_parms (sub)))
5396 {
5397 tree alias_args = INNERMOST_TEMPLATE_ARGS
5398 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5399 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5400 break;
5401 /* The alias type is equivalent to the pattern of the
5402 underlying template, so strip the alias. */
5403 tmpl = sub;
5404 continue;
5405 }
5406 }
5407 break;
5408 }
5409 return tmpl;
5410 }
5411
5412 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5413 must be a function or a pointer-to-function type, as specified
5414 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5415 and check that the resulting function has external linkage. */
5416
5417 static tree
5418 convert_nontype_argument_function (tree type, tree expr,
5419 tsubst_flags_t complain)
5420 {
5421 tree fns = expr;
5422 tree fn, fn_no_ptr;
5423 linkage_kind linkage;
5424
5425 fn = instantiate_type (type, fns, tf_none);
5426 if (fn == error_mark_node)
5427 return error_mark_node;
5428
5429 fn_no_ptr = fn;
5430 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5431 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5432 if (BASELINK_P (fn_no_ptr))
5433 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5434
5435 /* [temp.arg.nontype]/1
5436
5437 A template-argument for a non-type, non-template template-parameter
5438 shall be one of:
5439 [...]
5440 -- the address of an object or function with external [C++11: or
5441 internal] linkage. */
5442
5443 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5444 {
5445 if (complain & tf_error)
5446 {
5447 error ("%qE is not a valid template argument for type %qT",
5448 expr, type);
5449 if (TYPE_PTR_P (type))
5450 error ("it must be the address of a function with "
5451 "external linkage");
5452 else
5453 error ("it must be the name of a function with "
5454 "external linkage");
5455 }
5456 return NULL_TREE;
5457 }
5458
5459 linkage = decl_linkage (fn_no_ptr);
5460 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5461 {
5462 if (complain & tf_error)
5463 {
5464 if (cxx_dialect >= cxx11)
5465 error ("%qE is not a valid template argument for type %qT "
5466 "because %qD has no linkage",
5467 expr, type, fn_no_ptr);
5468 else
5469 error ("%qE is not a valid template argument for type %qT "
5470 "because %qD does not have external linkage",
5471 expr, type, fn_no_ptr);
5472 }
5473 return NULL_TREE;
5474 }
5475
5476 return fn;
5477 }
5478
5479 /* Subroutine of convert_nontype_argument.
5480 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5481 Emit an error otherwise. */
5482
5483 static bool
5484 check_valid_ptrmem_cst_expr (tree type, tree expr,
5485 tsubst_flags_t complain)
5486 {
5487 STRIP_NOPS (expr);
5488 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5489 return true;
5490 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5491 return true;
5492 if (processing_template_decl
5493 && TREE_CODE (expr) == ADDR_EXPR
5494 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5495 return true;
5496 if (complain & tf_error)
5497 {
5498 error ("%qE is not a valid template argument for type %qT",
5499 expr, type);
5500 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5501 }
5502 return false;
5503 }
5504
5505 /* Returns TRUE iff the address of OP is value-dependent.
5506
5507 14.6.2.4 [temp.dep.temp]:
5508 A non-integral non-type template-argument is dependent if its type is
5509 dependent or it has either of the following forms
5510 qualified-id
5511 & qualified-id
5512 and contains a nested-name-specifier which specifies a class-name that
5513 names a dependent type.
5514
5515 We generalize this to just say that the address of a member of a
5516 dependent class is value-dependent; the above doesn't cover the
5517 address of a static data member named with an unqualified-id. */
5518
5519 static bool
5520 has_value_dependent_address (tree op)
5521 {
5522 /* We could use get_inner_reference here, but there's no need;
5523 this is only relevant for template non-type arguments, which
5524 can only be expressed as &id-expression. */
5525 if (DECL_P (op))
5526 {
5527 tree ctx = CP_DECL_CONTEXT (op);
5528 if (TYPE_P (ctx) && dependent_type_p (ctx))
5529 return true;
5530 }
5531
5532 return false;
5533 }
5534
5535 /* The next set of functions are used for providing helpful explanatory
5536 diagnostics for failed overload resolution. Their messages should be
5537 indented by two spaces for consistency with the messages in
5538 call.c */
5539
5540 static int
5541 unify_success (bool /*explain_p*/)
5542 {
5543 return 0;
5544 }
5545
5546 static int
5547 unify_parameter_deduction_failure (bool explain_p, tree parm)
5548 {
5549 if (explain_p)
5550 inform (input_location,
5551 " couldn't deduce template parameter %qD", parm);
5552 return 1;
5553 }
5554
5555 static int
5556 unify_invalid (bool /*explain_p*/)
5557 {
5558 return 1;
5559 }
5560
5561 static int
5562 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5563 {
5564 if (explain_p)
5565 inform (input_location,
5566 " types %qT and %qT have incompatible cv-qualifiers",
5567 parm, arg);
5568 return 1;
5569 }
5570
5571 static int
5572 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5573 {
5574 if (explain_p)
5575 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5576 return 1;
5577 }
5578
5579 static int
5580 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5581 {
5582 if (explain_p)
5583 inform (input_location,
5584 " template parameter %qD is not a parameter pack, but "
5585 "argument %qD is",
5586 parm, arg);
5587 return 1;
5588 }
5589
5590 static int
5591 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5592 {
5593 if (explain_p)
5594 inform (input_location,
5595 " template argument %qE does not match "
5596 "pointer-to-member constant %qE",
5597 arg, parm);
5598 return 1;
5599 }
5600
5601 static int
5602 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5603 {
5604 if (explain_p)
5605 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5606 return 1;
5607 }
5608
5609 static int
5610 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5611 {
5612 if (explain_p)
5613 inform (input_location,
5614 " inconsistent parameter pack deduction with %qT and %qT",
5615 old_arg, new_arg);
5616 return 1;
5617 }
5618
5619 static int
5620 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5621 {
5622 if (explain_p)
5623 {
5624 if (TYPE_P (parm))
5625 inform (input_location,
5626 " deduced conflicting types for parameter %qT (%qT and %qT)",
5627 parm, first, second);
5628 else
5629 inform (input_location,
5630 " deduced conflicting values for non-type parameter "
5631 "%qE (%qE and %qE)", parm, first, second);
5632 }
5633 return 1;
5634 }
5635
5636 static int
5637 unify_vla_arg (bool explain_p, tree arg)
5638 {
5639 if (explain_p)
5640 inform (input_location,
5641 " variable-sized array type %qT is not "
5642 "a valid template argument",
5643 arg);
5644 return 1;
5645 }
5646
5647 static int
5648 unify_method_type_error (bool explain_p, tree arg)
5649 {
5650 if (explain_p)
5651 inform (input_location,
5652 " member function type %qT is not a valid template argument",
5653 arg);
5654 return 1;
5655 }
5656
5657 static int
5658 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5659 {
5660 if (explain_p)
5661 {
5662 if (least_p)
5663 inform_n (input_location, wanted,
5664 " candidate expects at least %d argument, %d provided",
5665 " candidate expects at least %d arguments, %d provided",
5666 wanted, have);
5667 else
5668 inform_n (input_location, wanted,
5669 " candidate expects %d argument, %d provided",
5670 " candidate expects %d arguments, %d provided",
5671 wanted, have);
5672 }
5673 return 1;
5674 }
5675
5676 static int
5677 unify_too_many_arguments (bool explain_p, int have, int wanted)
5678 {
5679 return unify_arity (explain_p, have, wanted);
5680 }
5681
5682 static int
5683 unify_too_few_arguments (bool explain_p, int have, int wanted,
5684 bool least_p = false)
5685 {
5686 return unify_arity (explain_p, have, wanted, least_p);
5687 }
5688
5689 static int
5690 unify_arg_conversion (bool explain_p, tree to_type,
5691 tree from_type, tree arg)
5692 {
5693 if (explain_p)
5694 inform (EXPR_LOC_OR_LOC (arg, input_location),
5695 " cannot convert %qE (type %qT) to type %qT",
5696 arg, from_type, to_type);
5697 return 1;
5698 }
5699
5700 static int
5701 unify_no_common_base (bool explain_p, enum template_base_result r,
5702 tree parm, tree arg)
5703 {
5704 if (explain_p)
5705 switch (r)
5706 {
5707 case tbr_ambiguous_baseclass:
5708 inform (input_location, " %qT is an ambiguous base class of %qT",
5709 parm, arg);
5710 break;
5711 default:
5712 inform (input_location, " %qT is not derived from %qT", arg, parm);
5713 break;
5714 }
5715 return 1;
5716 }
5717
5718 static int
5719 unify_inconsistent_template_template_parameters (bool explain_p)
5720 {
5721 if (explain_p)
5722 inform (input_location,
5723 " template parameters of a template template argument are "
5724 "inconsistent with other deduced template arguments");
5725 return 1;
5726 }
5727
5728 static int
5729 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5730 {
5731 if (explain_p)
5732 inform (input_location,
5733 " can't deduce a template for %qT from non-template type %qT",
5734 parm, arg);
5735 return 1;
5736 }
5737
5738 static int
5739 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5740 {
5741 if (explain_p)
5742 inform (input_location,
5743 " template argument %qE does not match %qD", arg, parm);
5744 return 1;
5745 }
5746
5747 static int
5748 unify_overload_resolution_failure (bool explain_p, tree arg)
5749 {
5750 if (explain_p)
5751 inform (input_location,
5752 " could not resolve address from overloaded function %qE",
5753 arg);
5754 return 1;
5755 }
5756
5757 /* Attempt to convert the non-type template parameter EXPR to the
5758 indicated TYPE. If the conversion is successful, return the
5759 converted value. If the conversion is unsuccessful, return
5760 NULL_TREE if we issued an error message, or error_mark_node if we
5761 did not. We issue error messages for out-and-out bad template
5762 parameters, but not simply because the conversion failed, since we
5763 might be just trying to do argument deduction. Both TYPE and EXPR
5764 must be non-dependent.
5765
5766 The conversion follows the special rules described in
5767 [temp.arg.nontype], and it is much more strict than an implicit
5768 conversion.
5769
5770 This function is called twice for each template argument (see
5771 lookup_template_class for a more accurate description of this
5772 problem). This means that we need to handle expressions which
5773 are not valid in a C++ source, but can be created from the
5774 first call (for instance, casts to perform conversions). These
5775 hacks can go away after we fix the double coercion problem. */
5776
5777 static tree
5778 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5779 {
5780 tree expr_type;
5781
5782 /* Detect immediately string literals as invalid non-type argument.
5783 This special-case is not needed for correctness (we would easily
5784 catch this later), but only to provide better diagnostic for this
5785 common user mistake. As suggested by DR 100, we do not mention
5786 linkage issues in the diagnostic as this is not the point. */
5787 /* FIXME we're making this OK. */
5788 if (TREE_CODE (expr) == STRING_CST)
5789 {
5790 if (complain & tf_error)
5791 error ("%qE is not a valid template argument for type %qT "
5792 "because string literals can never be used in this context",
5793 expr, type);
5794 return NULL_TREE;
5795 }
5796
5797 /* Add the ADDR_EXPR now for the benefit of
5798 value_dependent_expression_p. */
5799 if (TYPE_PTROBV_P (type)
5800 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5801 {
5802 expr = decay_conversion (expr, complain);
5803 if (expr == error_mark_node)
5804 return error_mark_node;
5805 }
5806
5807 /* If we are in a template, EXPR may be non-dependent, but still
5808 have a syntactic, rather than semantic, form. For example, EXPR
5809 might be a SCOPE_REF, rather than the VAR_DECL to which the
5810 SCOPE_REF refers. Preserving the qualifying scope is necessary
5811 so that access checking can be performed when the template is
5812 instantiated -- but here we need the resolved form so that we can
5813 convert the argument. */
5814 bool non_dep = false;
5815 if (TYPE_REF_OBJ_P (type)
5816 && has_value_dependent_address (expr))
5817 /* If we want the address and it's value-dependent, don't fold. */;
5818 else if (!type_unknown_p (expr)
5819 && processing_template_decl
5820 && !instantiation_dependent_expression_p (expr)
5821 && potential_constant_expression (expr))
5822 non_dep = true;
5823 if (error_operand_p (expr))
5824 return error_mark_node;
5825 expr_type = TREE_TYPE (expr);
5826 if (TREE_CODE (type) == REFERENCE_TYPE)
5827 expr = mark_lvalue_use (expr);
5828 else
5829 expr = mark_rvalue_use (expr);
5830
5831 /* If the argument is non-dependent, perform any conversions in
5832 non-dependent context as well. */
5833 processing_template_decl_sentinel s (non_dep);
5834 if (non_dep)
5835 expr = instantiate_non_dependent_expr_internal (expr, complain);
5836
5837 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5838 to a non-type argument of "nullptr". */
5839 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5840 expr = convert (type, expr);
5841
5842 /* In C++11, integral or enumeration non-type template arguments can be
5843 arbitrary constant expressions. Pointer and pointer to
5844 member arguments can be general constant expressions that evaluate
5845 to a null value, but otherwise still need to be of a specific form. */
5846 if (cxx_dialect >= cxx11)
5847 {
5848 if (TREE_CODE (expr) == PTRMEM_CST)
5849 /* A PTRMEM_CST is already constant, and a valid template
5850 argument for a parameter of pointer to member type, we just want
5851 to leave it in that form rather than lower it to a
5852 CONSTRUCTOR. */;
5853 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5854 expr = maybe_constant_value (expr);
5855 else if (TYPE_PTR_OR_PTRMEM_P (type))
5856 {
5857 tree folded = maybe_constant_value (expr);
5858 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5859 : null_member_pointer_value_p (folded))
5860 expr = folded;
5861 }
5862 }
5863
5864 /* HACK: Due to double coercion, we can get a
5865 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5866 which is the tree that we built on the first call (see
5867 below when coercing to reference to object or to reference to
5868 function). We just strip everything and get to the arg.
5869 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5870 for examples. */
5871 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5872 {
5873 tree probe_type, probe = expr;
5874 if (REFERENCE_REF_P (probe))
5875 probe = TREE_OPERAND (probe, 0);
5876 probe_type = TREE_TYPE (probe);
5877 if (TREE_CODE (probe) == NOP_EXPR)
5878 {
5879 /* ??? Maybe we could use convert_from_reference here, but we
5880 would need to relax its constraints because the NOP_EXPR
5881 could actually change the type to something more cv-qualified,
5882 and this is not folded by convert_from_reference. */
5883 tree addr = TREE_OPERAND (probe, 0);
5884 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5885 && TREE_CODE (addr) == ADDR_EXPR
5886 && TYPE_PTR_P (TREE_TYPE (addr))
5887 && (same_type_ignoring_top_level_qualifiers_p
5888 (TREE_TYPE (probe_type),
5889 TREE_TYPE (TREE_TYPE (addr)))))
5890 {
5891 expr = TREE_OPERAND (addr, 0);
5892 expr_type = TREE_TYPE (probe_type);
5893 }
5894 }
5895 }
5896
5897 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5898 parameter is a pointer to object, through decay and
5899 qualification conversion. Let's strip everything. */
5900 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5901 {
5902 tree probe = expr;
5903 STRIP_NOPS (probe);
5904 if (TREE_CODE (probe) == ADDR_EXPR
5905 && TYPE_PTR_P (TREE_TYPE (probe)))
5906 {
5907 /* Skip the ADDR_EXPR only if it is part of the decay for
5908 an array. Otherwise, it is part of the original argument
5909 in the source code. */
5910 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5911 probe = TREE_OPERAND (probe, 0);
5912 expr = probe;
5913 expr_type = TREE_TYPE (expr);
5914 }
5915 }
5916
5917 /* [temp.arg.nontype]/5, bullet 1
5918
5919 For a non-type template-parameter of integral or enumeration type,
5920 integral promotions (_conv.prom_) and integral conversions
5921 (_conv.integral_) are applied. */
5922 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5923 {
5924 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5925 t = maybe_constant_value (t);
5926 if (t != error_mark_node)
5927 expr = t;
5928
5929 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5930 return error_mark_node;
5931
5932 /* Notice that there are constant expressions like '4 % 0' which
5933 do not fold into integer constants. */
5934 if (TREE_CODE (expr) != INTEGER_CST)
5935 {
5936 if (complain & tf_error)
5937 {
5938 int errs = errorcount, warns = warningcount + werrorcount;
5939 if (processing_template_decl
5940 && !require_potential_constant_expression (expr))
5941 return NULL_TREE;
5942 expr = cxx_constant_value (expr);
5943 if (errorcount > errs || warningcount + werrorcount > warns)
5944 inform (EXPR_LOC_OR_LOC (expr, input_location),
5945 "in template argument for type %qT ", type);
5946 if (expr == error_mark_node)
5947 return NULL_TREE;
5948 /* else cxx_constant_value complained but gave us
5949 a real constant, so go ahead. */
5950 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5951 }
5952 else
5953 return NULL_TREE;
5954 }
5955
5956 /* Avoid typedef problems. */
5957 if (TREE_TYPE (expr) != type)
5958 expr = fold_convert (type, expr);
5959 }
5960 /* [temp.arg.nontype]/5, bullet 2
5961
5962 For a non-type template-parameter of type pointer to object,
5963 qualification conversions (_conv.qual_) and the array-to-pointer
5964 conversion (_conv.array_) are applied. */
5965 else if (TYPE_PTROBV_P (type))
5966 {
5967 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5968
5969 A template-argument for a non-type, non-template template-parameter
5970 shall be one of: [...]
5971
5972 -- the name of a non-type template-parameter;
5973 -- the address of an object or function with external linkage, [...]
5974 expressed as "& id-expression" where the & is optional if the name
5975 refers to a function or array, or if the corresponding
5976 template-parameter is a reference.
5977
5978 Here, we do not care about functions, as they are invalid anyway
5979 for a parameter of type pointer-to-object. */
5980
5981 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5982 /* Non-type template parameters are OK. */
5983 ;
5984 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5985 /* Null pointer values are OK in C++11. */;
5986 else if (TREE_CODE (expr) != ADDR_EXPR
5987 && TREE_CODE (expr_type) != ARRAY_TYPE)
5988 {
5989 if (VAR_P (expr))
5990 {
5991 if (complain & tf_error)
5992 error ("%qD is not a valid template argument "
5993 "because %qD is a variable, not the address of "
5994 "a variable", expr, expr);
5995 return NULL_TREE;
5996 }
5997 if (POINTER_TYPE_P (expr_type))
5998 {
5999 if (complain & tf_error)
6000 error ("%qE is not a valid template argument for %qT "
6001 "because it is not the address of a variable",
6002 expr, type);
6003 return NULL_TREE;
6004 }
6005 /* Other values, like integer constants, might be valid
6006 non-type arguments of some other type. */
6007 return error_mark_node;
6008 }
6009 else
6010 {
6011 tree decl;
6012
6013 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6014 ? TREE_OPERAND (expr, 0) : expr);
6015 if (!VAR_P (decl))
6016 {
6017 if (complain & tf_error)
6018 error ("%qE is not a valid template argument of type %qT "
6019 "because %qE is not a variable", expr, type, decl);
6020 return NULL_TREE;
6021 }
6022 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6023 {
6024 if (complain & tf_error)
6025 error ("%qE is not a valid template argument of type %qT "
6026 "because %qD does not have external linkage",
6027 expr, type, decl);
6028 return NULL_TREE;
6029 }
6030 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6031 {
6032 if (complain & tf_error)
6033 error ("%qE is not a valid template argument of type %qT "
6034 "because %qD has no linkage", expr, type, decl);
6035 return NULL_TREE;
6036 }
6037 }
6038
6039 expr = decay_conversion (expr, complain);
6040 if (expr == error_mark_node)
6041 return error_mark_node;
6042
6043 expr = perform_qualification_conversions (type, expr);
6044 if (expr == error_mark_node)
6045 return error_mark_node;
6046 }
6047 /* [temp.arg.nontype]/5, bullet 3
6048
6049 For a non-type template-parameter of type reference to object, no
6050 conversions apply. The type referred to by the reference may be more
6051 cv-qualified than the (otherwise identical) type of the
6052 template-argument. The template-parameter is bound directly to the
6053 template-argument, which must be an lvalue. */
6054 else if (TYPE_REF_OBJ_P (type))
6055 {
6056 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6057 expr_type))
6058 return error_mark_node;
6059
6060 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6061 {
6062 if (complain & tf_error)
6063 error ("%qE is not a valid template argument for type %qT "
6064 "because of conflicts in cv-qualification", expr, type);
6065 return NULL_TREE;
6066 }
6067
6068 if (!real_lvalue_p (expr))
6069 {
6070 if (complain & tf_error)
6071 error ("%qE is not a valid template argument for type %qT "
6072 "because it is not an lvalue", expr, type);
6073 return NULL_TREE;
6074 }
6075
6076 /* [temp.arg.nontype]/1
6077
6078 A template-argument for a non-type, non-template template-parameter
6079 shall be one of: [...]
6080
6081 -- the address of an object or function with external linkage. */
6082 if (INDIRECT_REF_P (expr)
6083 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6084 {
6085 expr = TREE_OPERAND (expr, 0);
6086 if (DECL_P (expr))
6087 {
6088 if (complain & tf_error)
6089 error ("%q#D is not a valid template argument for type %qT "
6090 "because a reference variable does not have a constant "
6091 "address", expr, type);
6092 return NULL_TREE;
6093 }
6094 }
6095
6096 if (!DECL_P (expr))
6097 {
6098 if (complain & tf_error)
6099 error ("%qE is not a valid template argument for type %qT "
6100 "because it is not an object with external linkage",
6101 expr, type);
6102 return NULL_TREE;
6103 }
6104
6105 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6106 {
6107 if (complain & tf_error)
6108 error ("%qE is not a valid template argument for type %qT "
6109 "because object %qD has not external linkage",
6110 expr, type, expr);
6111 return NULL_TREE;
6112 }
6113
6114 expr = build_nop (type, build_address (expr));
6115 }
6116 /* [temp.arg.nontype]/5, bullet 4
6117
6118 For a non-type template-parameter of type pointer to function, only
6119 the function-to-pointer conversion (_conv.func_) is applied. If the
6120 template-argument represents a set of overloaded functions (or a
6121 pointer to such), the matching function is selected from the set
6122 (_over.over_). */
6123 else if (TYPE_PTRFN_P (type))
6124 {
6125 /* If the argument is a template-id, we might not have enough
6126 context information to decay the pointer. */
6127 if (!type_unknown_p (expr_type))
6128 {
6129 expr = decay_conversion (expr, complain);
6130 if (expr == error_mark_node)
6131 return error_mark_node;
6132 }
6133
6134 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6135 /* Null pointer values are OK in C++11. */
6136 return perform_qualification_conversions (type, expr);
6137
6138 expr = convert_nontype_argument_function (type, expr, complain);
6139 if (!expr || expr == error_mark_node)
6140 return expr;
6141 }
6142 /* [temp.arg.nontype]/5, bullet 5
6143
6144 For a non-type template-parameter of type reference to function, no
6145 conversions apply. If the template-argument represents a set of
6146 overloaded functions, the matching function is selected from the set
6147 (_over.over_). */
6148 else if (TYPE_REFFN_P (type))
6149 {
6150 if (TREE_CODE (expr) == ADDR_EXPR)
6151 {
6152 if (complain & tf_error)
6153 {
6154 error ("%qE is not a valid template argument for type %qT "
6155 "because it is a pointer", expr, type);
6156 inform (input_location, "try using %qE instead",
6157 TREE_OPERAND (expr, 0));
6158 }
6159 return NULL_TREE;
6160 }
6161
6162 expr = convert_nontype_argument_function (type, expr, complain);
6163 if (!expr || expr == error_mark_node)
6164 return expr;
6165
6166 expr = build_nop (type, build_address (expr));
6167 }
6168 /* [temp.arg.nontype]/5, bullet 6
6169
6170 For a non-type template-parameter of type pointer to member function,
6171 no conversions apply. If the template-argument represents a set of
6172 overloaded member functions, the matching member function is selected
6173 from the set (_over.over_). */
6174 else if (TYPE_PTRMEMFUNC_P (type))
6175 {
6176 expr = instantiate_type (type, expr, tf_none);
6177 if (expr == error_mark_node)
6178 return error_mark_node;
6179
6180 /* [temp.arg.nontype] bullet 1 says the pointer to member
6181 expression must be a pointer-to-member constant. */
6182 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6183 return error_mark_node;
6184
6185 /* There is no way to disable standard conversions in
6186 resolve_address_of_overloaded_function (called by
6187 instantiate_type). It is possible that the call succeeded by
6188 converting &B::I to &D::I (where B is a base of D), so we need
6189 to reject this conversion here.
6190
6191 Actually, even if there was a way to disable standard conversions,
6192 it would still be better to reject them here so that we can
6193 provide a superior diagnostic. */
6194 if (!same_type_p (TREE_TYPE (expr), type))
6195 {
6196 if (complain & tf_error)
6197 {
6198 error ("%qE is not a valid template argument for type %qT "
6199 "because it is of type %qT", expr, type,
6200 TREE_TYPE (expr));
6201 /* If we are just one standard conversion off, explain. */
6202 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6203 inform (input_location,
6204 "standard conversions are not allowed in this context");
6205 }
6206 return NULL_TREE;
6207 }
6208 }
6209 /* [temp.arg.nontype]/5, bullet 7
6210
6211 For a non-type template-parameter of type pointer to data member,
6212 qualification conversions (_conv.qual_) are applied. */
6213 else if (TYPE_PTRDATAMEM_P (type))
6214 {
6215 /* [temp.arg.nontype] bullet 1 says the pointer to member
6216 expression must be a pointer-to-member constant. */
6217 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6218 return error_mark_node;
6219
6220 expr = perform_qualification_conversions (type, expr);
6221 if (expr == error_mark_node)
6222 return expr;
6223 }
6224 else if (NULLPTR_TYPE_P (type))
6225 {
6226 if (expr != nullptr_node)
6227 {
6228 if (complain & tf_error)
6229 error ("%qE is not a valid template argument for type %qT "
6230 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6231 return NULL_TREE;
6232 }
6233 return expr;
6234 }
6235 /* A template non-type parameter must be one of the above. */
6236 else
6237 gcc_unreachable ();
6238
6239 /* Sanity check: did we actually convert the argument to the
6240 right type? */
6241 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6242 (type, TREE_TYPE (expr)));
6243 return convert_from_reference (expr);
6244 }
6245
6246 /* Subroutine of coerce_template_template_parms, which returns 1 if
6247 PARM_PARM and ARG_PARM match using the rule for the template
6248 parameters of template template parameters. Both PARM and ARG are
6249 template parameters; the rest of the arguments are the same as for
6250 coerce_template_template_parms.
6251 */
6252 static int
6253 coerce_template_template_parm (tree parm,
6254 tree arg,
6255 tsubst_flags_t complain,
6256 tree in_decl,
6257 tree outer_args)
6258 {
6259 if (arg == NULL_TREE || error_operand_p (arg)
6260 || parm == NULL_TREE || error_operand_p (parm))
6261 return 0;
6262
6263 if (TREE_CODE (arg) != TREE_CODE (parm))
6264 return 0;
6265
6266 switch (TREE_CODE (parm))
6267 {
6268 case TEMPLATE_DECL:
6269 /* We encounter instantiations of templates like
6270 template <template <template <class> class> class TT>
6271 class C; */
6272 {
6273 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6274 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6275
6276 if (!coerce_template_template_parms
6277 (parmparm, argparm, complain, in_decl, outer_args))
6278 return 0;
6279 }
6280 /* Fall through. */
6281
6282 case TYPE_DECL:
6283 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6284 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6285 /* Argument is a parameter pack but parameter is not. */
6286 return 0;
6287 break;
6288
6289 case PARM_DECL:
6290 /* The tsubst call is used to handle cases such as
6291
6292 template <int> class C {};
6293 template <class T, template <T> class TT> class D {};
6294 D<int, C> d;
6295
6296 i.e. the parameter list of TT depends on earlier parameters. */
6297 if (!uses_template_parms (TREE_TYPE (arg))
6298 && !same_type_p
6299 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6300 TREE_TYPE (arg)))
6301 return 0;
6302
6303 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6304 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6305 /* Argument is a parameter pack but parameter is not. */
6306 return 0;
6307
6308 break;
6309
6310 default:
6311 gcc_unreachable ();
6312 }
6313
6314 return 1;
6315 }
6316
6317
6318 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6319 template template parameters. Both PARM_PARMS and ARG_PARMS are
6320 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6321 or PARM_DECL.
6322
6323 Consider the example:
6324 template <class T> class A;
6325 template<template <class U> class TT> class B;
6326
6327 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6328 the parameters to A, and OUTER_ARGS contains A. */
6329
6330 static int
6331 coerce_template_template_parms (tree parm_parms,
6332 tree arg_parms,
6333 tsubst_flags_t complain,
6334 tree in_decl,
6335 tree outer_args)
6336 {
6337 int nparms, nargs, i;
6338 tree parm, arg;
6339 int variadic_p = 0;
6340
6341 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6342 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6343
6344 nparms = TREE_VEC_LENGTH (parm_parms);
6345 nargs = TREE_VEC_LENGTH (arg_parms);
6346
6347 /* Determine whether we have a parameter pack at the end of the
6348 template template parameter's template parameter list. */
6349 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6350 {
6351 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6352
6353 if (error_operand_p (parm))
6354 return 0;
6355
6356 switch (TREE_CODE (parm))
6357 {
6358 case TEMPLATE_DECL:
6359 case TYPE_DECL:
6360 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6361 variadic_p = 1;
6362 break;
6363
6364 case PARM_DECL:
6365 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6366 variadic_p = 1;
6367 break;
6368
6369 default:
6370 gcc_unreachable ();
6371 }
6372 }
6373
6374 if (nargs != nparms
6375 && !(variadic_p && nargs >= nparms - 1))
6376 return 0;
6377
6378 /* Check all of the template parameters except the parameter pack at
6379 the end (if any). */
6380 for (i = 0; i < nparms - variadic_p; ++i)
6381 {
6382 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6383 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6384 continue;
6385
6386 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6387 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6388
6389 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6390 outer_args))
6391 return 0;
6392
6393 }
6394
6395 if (variadic_p)
6396 {
6397 /* Check each of the template parameters in the template
6398 argument against the template parameter pack at the end of
6399 the template template parameter. */
6400 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6401 return 0;
6402
6403 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6404
6405 for (; i < nargs; ++i)
6406 {
6407 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6408 continue;
6409
6410 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6411
6412 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6413 outer_args))
6414 return 0;
6415 }
6416 }
6417
6418 return 1;
6419 }
6420
6421 /* Verifies that the deduced template arguments (in TARGS) for the
6422 template template parameters (in TPARMS) represent valid bindings,
6423 by comparing the template parameter list of each template argument
6424 to the template parameter list of its corresponding template
6425 template parameter, in accordance with DR150. This
6426 routine can only be called after all template arguments have been
6427 deduced. It will return TRUE if all of the template template
6428 parameter bindings are okay, FALSE otherwise. */
6429 bool
6430 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6431 {
6432 int i, ntparms = TREE_VEC_LENGTH (tparms);
6433 bool ret = true;
6434
6435 /* We're dealing with template parms in this process. */
6436 ++processing_template_decl;
6437
6438 targs = INNERMOST_TEMPLATE_ARGS (targs);
6439
6440 for (i = 0; i < ntparms; ++i)
6441 {
6442 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6443 tree targ = TREE_VEC_ELT (targs, i);
6444
6445 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6446 {
6447 tree packed_args = NULL_TREE;
6448 int idx, len = 1;
6449
6450 if (ARGUMENT_PACK_P (targ))
6451 {
6452 /* Look inside the argument pack. */
6453 packed_args = ARGUMENT_PACK_ARGS (targ);
6454 len = TREE_VEC_LENGTH (packed_args);
6455 }
6456
6457 for (idx = 0; idx < len; ++idx)
6458 {
6459 tree targ_parms = NULL_TREE;
6460
6461 if (packed_args)
6462 /* Extract the next argument from the argument
6463 pack. */
6464 targ = TREE_VEC_ELT (packed_args, idx);
6465
6466 if (PACK_EXPANSION_P (targ))
6467 /* Look at the pattern of the pack expansion. */
6468 targ = PACK_EXPANSION_PATTERN (targ);
6469
6470 /* Extract the template parameters from the template
6471 argument. */
6472 if (TREE_CODE (targ) == TEMPLATE_DECL)
6473 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6474 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6475 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6476
6477 /* Verify that we can coerce the template template
6478 parameters from the template argument to the template
6479 parameter. This requires an exact match. */
6480 if (targ_parms
6481 && !coerce_template_template_parms
6482 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6483 targ_parms,
6484 tf_none,
6485 tparm,
6486 targs))
6487 {
6488 ret = false;
6489 goto out;
6490 }
6491 }
6492 }
6493 }
6494
6495 out:
6496
6497 --processing_template_decl;
6498 return ret;
6499 }
6500
6501 /* Since type attributes aren't mangled, we need to strip them from
6502 template type arguments. */
6503
6504 static tree
6505 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6506 {
6507 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6508 return arg;
6509 bool removed_attributes = false;
6510 tree canon = strip_typedefs (arg, &removed_attributes);
6511 if (removed_attributes
6512 && (complain & tf_warning))
6513 warning (0, "ignoring attributes on template argument %qT", arg);
6514 return canon;
6515 }
6516
6517 /* Convert the indicated template ARG as necessary to match the
6518 indicated template PARM. Returns the converted ARG, or
6519 error_mark_node if the conversion was unsuccessful. Error and
6520 warning messages are issued under control of COMPLAIN. This
6521 conversion is for the Ith parameter in the parameter list. ARGS is
6522 the full set of template arguments deduced so far. */
6523
6524 static tree
6525 convert_template_argument (tree parm,
6526 tree arg,
6527 tree args,
6528 tsubst_flags_t complain,
6529 int i,
6530 tree in_decl)
6531 {
6532 tree orig_arg;
6533 tree val;
6534 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6535
6536 if (parm == error_mark_node)
6537 return error_mark_node;
6538
6539 if (TREE_CODE (arg) == TREE_LIST
6540 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6541 {
6542 /* The template argument was the name of some
6543 member function. That's usually
6544 invalid, but static members are OK. In any
6545 case, grab the underlying fields/functions
6546 and issue an error later if required. */
6547 orig_arg = TREE_VALUE (arg);
6548 TREE_TYPE (arg) = unknown_type_node;
6549 }
6550
6551 orig_arg = arg;
6552
6553 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6554 requires_type = (TREE_CODE (parm) == TYPE_DECL
6555 || requires_tmpl_type);
6556
6557 /* When determining whether an argument pack expansion is a template,
6558 look at the pattern. */
6559 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6560 arg = PACK_EXPANSION_PATTERN (arg);
6561
6562 /* Deal with an injected-class-name used as a template template arg. */
6563 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6564 {
6565 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6566 if (TREE_CODE (t) == TEMPLATE_DECL)
6567 {
6568 if (cxx_dialect >= cxx11)
6569 /* OK under DR 1004. */;
6570 else if (complain & tf_warning_or_error)
6571 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6572 " used as template template argument", TYPE_NAME (arg));
6573 else if (flag_pedantic_errors)
6574 t = arg;
6575
6576 arg = t;
6577 }
6578 }
6579
6580 is_tmpl_type =
6581 ((TREE_CODE (arg) == TEMPLATE_DECL
6582 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6583 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6584 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6585 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6586
6587 if (is_tmpl_type
6588 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6589 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6590 arg = TYPE_STUB_DECL (arg);
6591
6592 is_type = TYPE_P (arg) || is_tmpl_type;
6593
6594 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6595 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6596 {
6597 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6598 {
6599 if (complain & tf_error)
6600 error ("invalid use of destructor %qE as a type", orig_arg);
6601 return error_mark_node;
6602 }
6603
6604 permerror (input_location,
6605 "to refer to a type member of a template parameter, "
6606 "use %<typename %E%>", orig_arg);
6607
6608 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6609 TREE_OPERAND (arg, 1),
6610 typename_type,
6611 complain);
6612 arg = orig_arg;
6613 is_type = 1;
6614 }
6615 if (is_type != requires_type)
6616 {
6617 if (in_decl)
6618 {
6619 if (complain & tf_error)
6620 {
6621 error ("type/value mismatch at argument %d in template "
6622 "parameter list for %qD",
6623 i + 1, in_decl);
6624 if (is_type)
6625 inform (input_location,
6626 " expected a constant of type %qT, got %qT",
6627 TREE_TYPE (parm),
6628 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6629 else if (requires_tmpl_type)
6630 inform (input_location,
6631 " expected a class template, got %qE", orig_arg);
6632 else
6633 inform (input_location,
6634 " expected a type, got %qE", orig_arg);
6635 }
6636 }
6637 return error_mark_node;
6638 }
6639 if (is_tmpl_type ^ requires_tmpl_type)
6640 {
6641 if (in_decl && (complain & tf_error))
6642 {
6643 error ("type/value mismatch at argument %d in template "
6644 "parameter list for %qD",
6645 i + 1, in_decl);
6646 if (is_tmpl_type)
6647 inform (input_location,
6648 " expected a type, got %qT", DECL_NAME (arg));
6649 else
6650 inform (input_location,
6651 " expected a class template, got %qT", orig_arg);
6652 }
6653 return error_mark_node;
6654 }
6655
6656 if (is_type)
6657 {
6658 if (requires_tmpl_type)
6659 {
6660 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6661 val = orig_arg;
6662 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6663 /* The number of argument required is not known yet.
6664 Just accept it for now. */
6665 val = TREE_TYPE (arg);
6666 else
6667 {
6668 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6669 tree argparm;
6670
6671 /* Strip alias templates that are equivalent to another
6672 template. */
6673 arg = get_underlying_template (arg);
6674 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6675
6676 if (coerce_template_template_parms (parmparm, argparm,
6677 complain, in_decl,
6678 args))
6679 {
6680 val = arg;
6681
6682 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6683 TEMPLATE_DECL. */
6684 if (val != error_mark_node)
6685 {
6686 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6687 val = TREE_TYPE (val);
6688 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6689 val = make_pack_expansion (val);
6690 }
6691 }
6692 else
6693 {
6694 if (in_decl && (complain & tf_error))
6695 {
6696 error ("type/value mismatch at argument %d in "
6697 "template parameter list for %qD",
6698 i + 1, in_decl);
6699 inform (input_location,
6700 " expected a template of type %qD, got %qT",
6701 parm, orig_arg);
6702 }
6703
6704 val = error_mark_node;
6705 }
6706 }
6707 }
6708 else
6709 val = orig_arg;
6710 /* We only form one instance of each template specialization.
6711 Therefore, if we use a non-canonical variant (i.e., a
6712 typedef), any future messages referring to the type will use
6713 the typedef, which is confusing if those future uses do not
6714 themselves also use the typedef. */
6715 if (TYPE_P (val))
6716 val = canonicalize_type_argument (val, complain);
6717 }
6718 else
6719 {
6720 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6721
6722 if (invalid_nontype_parm_type_p (t, complain))
6723 return error_mark_node;
6724
6725 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6726 {
6727 if (same_type_p (t, TREE_TYPE (orig_arg)))
6728 val = orig_arg;
6729 else
6730 {
6731 /* Not sure if this is reachable, but it doesn't hurt
6732 to be robust. */
6733 error ("type mismatch in nontype parameter pack");
6734 val = error_mark_node;
6735 }
6736 }
6737 else if (!dependent_template_arg_p (orig_arg)
6738 && !uses_template_parms (t))
6739 /* We used to call digest_init here. However, digest_init
6740 will report errors, which we don't want when complain
6741 is zero. More importantly, digest_init will try too
6742 hard to convert things: for example, `0' should not be
6743 converted to pointer type at this point according to
6744 the standard. Accepting this is not merely an
6745 extension, since deciding whether or not these
6746 conversions can occur is part of determining which
6747 function template to call, or whether a given explicit
6748 argument specification is valid. */
6749 val = convert_nontype_argument (t, orig_arg, complain);
6750 else
6751 {
6752 bool removed_attr = false;
6753 val = strip_typedefs_expr (orig_arg, &removed_attr);
6754 }
6755
6756 if (val == NULL_TREE)
6757 val = error_mark_node;
6758 else if (val == error_mark_node && (complain & tf_error))
6759 error ("could not convert template argument %qE to %qT", orig_arg, t);
6760
6761 if (TREE_CODE (val) == SCOPE_REF)
6762 {
6763 /* Strip typedefs from the SCOPE_REF. */
6764 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6765 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6766 complain);
6767 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6768 QUALIFIED_NAME_IS_TEMPLATE (val));
6769 }
6770 }
6771
6772 return val;
6773 }
6774
6775 /* Coerces the remaining template arguments in INNER_ARGS (from
6776 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6777 Returns the coerced argument pack. PARM_IDX is the position of this
6778 parameter in the template parameter list. ARGS is the original
6779 template argument list. */
6780 static tree
6781 coerce_template_parameter_pack (tree parms,
6782 int parm_idx,
6783 tree args,
6784 tree inner_args,
6785 int arg_idx,
6786 tree new_args,
6787 int* lost,
6788 tree in_decl,
6789 tsubst_flags_t complain)
6790 {
6791 tree parm = TREE_VEC_ELT (parms, parm_idx);
6792 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6793 tree packed_args;
6794 tree argument_pack;
6795 tree packed_parms = NULL_TREE;
6796
6797 if (arg_idx > nargs)
6798 arg_idx = nargs;
6799
6800 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6801 {
6802 /* When the template parameter is a non-type template parameter pack
6803 or template template parameter pack whose type or template
6804 parameters use parameter packs, we know exactly how many arguments
6805 we are looking for. Build a vector of the instantiated decls for
6806 these template parameters in PACKED_PARMS. */
6807 /* We can't use make_pack_expansion here because it would interpret a
6808 _DECL as a use rather than a declaration. */
6809 tree decl = TREE_VALUE (parm);
6810 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6811 SET_PACK_EXPANSION_PATTERN (exp, decl);
6812 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6813 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6814
6815 TREE_VEC_LENGTH (args)--;
6816 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6817 TREE_VEC_LENGTH (args)++;
6818
6819 if (packed_parms == error_mark_node)
6820 return error_mark_node;
6821
6822 /* If we're doing a partial instantiation of a member template,
6823 verify that all of the types used for the non-type
6824 template parameter pack are, in fact, valid for non-type
6825 template parameters. */
6826 if (arg_idx < nargs
6827 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6828 {
6829 int j, len = TREE_VEC_LENGTH (packed_parms);
6830 for (j = 0; j < len; ++j)
6831 {
6832 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6833 if (invalid_nontype_parm_type_p (t, complain))
6834 return error_mark_node;
6835 }
6836 /* We don't know how many args we have yet, just
6837 use the unconverted ones for now. */
6838 return NULL_TREE;
6839 }
6840
6841 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6842 }
6843 else
6844 packed_args = make_tree_vec (nargs - arg_idx);
6845
6846 /* Convert the remaining arguments, which will be a part of the
6847 parameter pack "parm". */
6848 for (; arg_idx < nargs; ++arg_idx)
6849 {
6850 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6851 tree actual_parm = TREE_VALUE (parm);
6852 int pack_idx = arg_idx - parm_idx;
6853
6854 if (packed_parms)
6855 {
6856 /* Once we've packed as many args as we have types, stop. */
6857 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6858 break;
6859 else if (PACK_EXPANSION_P (arg))
6860 /* We don't know how many args we have yet, just
6861 use the unconverted ones for now. */
6862 return NULL_TREE;
6863 else
6864 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6865 }
6866
6867 if (arg == error_mark_node)
6868 {
6869 if (complain & tf_error)
6870 error ("template argument %d is invalid", arg_idx + 1);
6871 }
6872 else
6873 arg = convert_template_argument (actual_parm,
6874 arg, new_args, complain, parm_idx,
6875 in_decl);
6876 if (arg == error_mark_node)
6877 (*lost)++;
6878 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6879 }
6880
6881 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6882 && TREE_VEC_LENGTH (packed_args) > 0)
6883 {
6884 if (complain & tf_error)
6885 error ("wrong number of template arguments (%d, should be %d)",
6886 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6887 return error_mark_node;
6888 }
6889
6890 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6891 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6892 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6893 else
6894 {
6895 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6896 TREE_TYPE (argument_pack)
6897 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6898 TREE_CONSTANT (argument_pack) = 1;
6899 }
6900
6901 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6902 #ifdef ENABLE_CHECKING
6903 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6904 TREE_VEC_LENGTH (packed_args));
6905 #endif
6906 return argument_pack;
6907 }
6908
6909 /* Returns the number of pack expansions in the template argument vector
6910 ARGS. */
6911
6912 static int
6913 pack_expansion_args_count (tree args)
6914 {
6915 int i;
6916 int count = 0;
6917 if (args)
6918 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6919 {
6920 tree elt = TREE_VEC_ELT (args, i);
6921 if (elt && PACK_EXPANSION_P (elt))
6922 ++count;
6923 }
6924 return count;
6925 }
6926
6927 /* Convert all template arguments to their appropriate types, and
6928 return a vector containing the innermost resulting template
6929 arguments. If any error occurs, return error_mark_node. Error and
6930 warning messages are issued under control of COMPLAIN.
6931
6932 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6933 for arguments not specified in ARGS. Otherwise, if
6934 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6935 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6936 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6937 ARGS. */
6938
6939 static tree
6940 coerce_template_parms (tree parms,
6941 tree args,
6942 tree in_decl,
6943 tsubst_flags_t complain,
6944 bool require_all_args,
6945 bool use_default_args)
6946 {
6947 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6948 tree orig_inner_args;
6949 tree inner_args;
6950 tree new_args;
6951 tree new_inner_args;
6952 int saved_unevaluated_operand;
6953 int saved_inhibit_evaluation_warnings;
6954
6955 /* When used as a boolean value, indicates whether this is a
6956 variadic template parameter list. Since it's an int, we can also
6957 subtract it from nparms to get the number of non-variadic
6958 parameters. */
6959 int variadic_p = 0;
6960 int variadic_args_p = 0;
6961 int post_variadic_parms = 0;
6962
6963 /* Likewise for parameters with default arguments. */
6964 int default_p = 0;
6965
6966 if (args == error_mark_node)
6967 return error_mark_node;
6968
6969 nparms = TREE_VEC_LENGTH (parms);
6970
6971 /* Determine if there are any parameter packs or default arguments. */
6972 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6973 {
6974 tree parm = TREE_VEC_ELT (parms, parm_idx);
6975 if (variadic_p)
6976 ++post_variadic_parms;
6977 if (template_parameter_pack_p (TREE_VALUE (parm)))
6978 ++variadic_p;
6979 if (TREE_PURPOSE (parm))
6980 ++default_p;
6981 }
6982
6983 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6984 /* If there are no parameters that follow a parameter pack, we need to
6985 expand any argument packs so that we can deduce a parameter pack from
6986 some non-packed args followed by an argument pack, as in variadic85.C.
6987 If there are such parameters, we need to leave argument packs intact
6988 so the arguments are assigned properly. This can happen when dealing
6989 with a nested class inside a partial specialization of a class
6990 template, as in variadic92.C, or when deducing a template parameter pack
6991 from a sub-declarator, as in variadic114.C. */
6992 if (!post_variadic_parms)
6993 inner_args = expand_template_argument_pack (inner_args);
6994
6995 /* Count any pack expansion args. */
6996 variadic_args_p = pack_expansion_args_count (inner_args);
6997
6998 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6999 if ((nargs > nparms && !variadic_p)
7000 || (nargs < nparms - variadic_p
7001 && require_all_args
7002 && !variadic_args_p
7003 && (!use_default_args
7004 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7005 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7006 {
7007 if (complain & tf_error)
7008 {
7009 if (variadic_p || default_p)
7010 {
7011 nparms -= variadic_p + default_p;
7012 error ("wrong number of template arguments "
7013 "(%d, should be at least %d)", nargs, nparms);
7014 }
7015 else
7016 error ("wrong number of template arguments "
7017 "(%d, should be %d)", nargs, nparms);
7018
7019 if (in_decl)
7020 inform (input_location, "provided for %q+D", in_decl);
7021 }
7022
7023 return error_mark_node;
7024 }
7025 /* We can't pass a pack expansion to a non-pack parameter of an alias
7026 template (DR 1430). */
7027 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7028 && variadic_args_p
7029 && nargs - variadic_args_p < nparms - variadic_p)
7030 {
7031 if (complain & tf_error)
7032 {
7033 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7034 {
7035 tree arg = TREE_VEC_ELT (inner_args, i);
7036 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7037
7038 if (PACK_EXPANSION_P (arg)
7039 && !template_parameter_pack_p (parm))
7040 {
7041 error ("pack expansion argument for non-pack parameter "
7042 "%qD of alias template %qD", parm, in_decl);
7043 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7044 goto found;
7045 }
7046 }
7047 gcc_unreachable ();
7048 found:;
7049 }
7050 return error_mark_node;
7051 }
7052
7053 /* We need to evaluate the template arguments, even though this
7054 template-id may be nested within a "sizeof". */
7055 saved_unevaluated_operand = cp_unevaluated_operand;
7056 cp_unevaluated_operand = 0;
7057 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7058 c_inhibit_evaluation_warnings = 0;
7059 new_inner_args = make_tree_vec (nparms);
7060 new_args = add_outermost_template_args (args, new_inner_args);
7061 int pack_adjust = 0;
7062 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7063 {
7064 tree arg;
7065 tree parm;
7066
7067 /* Get the Ith template parameter. */
7068 parm = TREE_VEC_ELT (parms, parm_idx);
7069
7070 if (parm == error_mark_node)
7071 {
7072 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7073 continue;
7074 }
7075
7076 /* Calculate the next argument. */
7077 if (arg_idx < nargs)
7078 arg = TREE_VEC_ELT (inner_args, arg_idx);
7079 else
7080 arg = NULL_TREE;
7081
7082 if (template_parameter_pack_p (TREE_VALUE (parm))
7083 && !(arg && ARGUMENT_PACK_P (arg)))
7084 {
7085 /* Some arguments will be placed in the
7086 template parameter pack PARM. */
7087 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7088 inner_args, arg_idx,
7089 new_args, &lost,
7090 in_decl, complain);
7091
7092 if (arg == NULL_TREE)
7093 {
7094 /* We don't know how many args we have yet, just use the
7095 unconverted (and still packed) ones for now. */
7096 new_inner_args = orig_inner_args;
7097 arg_idx = nargs;
7098 break;
7099 }
7100
7101 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7102
7103 /* Store this argument. */
7104 if (arg == error_mark_node)
7105 {
7106 lost++;
7107 /* We are done with all of the arguments. */
7108 arg_idx = nargs;
7109 }
7110 else
7111 {
7112 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7113 arg_idx += pack_adjust;
7114 }
7115
7116 continue;
7117 }
7118 else if (arg)
7119 {
7120 if (PACK_EXPANSION_P (arg))
7121 {
7122 /* "If every valid specialization of a variadic template
7123 requires an empty template parameter pack, the template is
7124 ill-formed, no diagnostic required." So check that the
7125 pattern works with this parameter. */
7126 tree pattern = PACK_EXPANSION_PATTERN (arg);
7127 tree conv = convert_template_argument (TREE_VALUE (parm),
7128 pattern, new_args,
7129 complain, parm_idx,
7130 in_decl);
7131 if (conv == error_mark_node)
7132 {
7133 inform (input_location, "so any instantiation with a "
7134 "non-empty parameter pack would be ill-formed");
7135 ++lost;
7136 }
7137 else if (TYPE_P (conv) && !TYPE_P (pattern))
7138 /* Recover from missing typename. */
7139 TREE_VEC_ELT (inner_args, arg_idx)
7140 = make_pack_expansion (conv);
7141
7142 /* We don't know how many args we have yet, just
7143 use the unconverted ones for now. */
7144 new_inner_args = inner_args;
7145 arg_idx = nargs;
7146 break;
7147 }
7148 }
7149 else if (require_all_args)
7150 {
7151 /* There must be a default arg in this case. */
7152 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7153 complain, in_decl);
7154 /* The position of the first default template argument,
7155 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7156 Record that. */
7157 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7158 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7159 arg_idx - pack_adjust);
7160 }
7161 else
7162 break;
7163
7164 if (arg == error_mark_node)
7165 {
7166 if (complain & tf_error)
7167 error ("template argument %d is invalid", arg_idx + 1);
7168 }
7169 else if (!arg)
7170 /* This only occurs if there was an error in the template
7171 parameter list itself (which we would already have
7172 reported) that we are trying to recover from, e.g., a class
7173 template with a parameter list such as
7174 template<typename..., typename>. */
7175 ++lost;
7176 else
7177 arg = convert_template_argument (TREE_VALUE (parm),
7178 arg, new_args, complain,
7179 parm_idx, in_decl);
7180
7181 if (arg == error_mark_node)
7182 lost++;
7183 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7184 }
7185 cp_unevaluated_operand = saved_unevaluated_operand;
7186 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7187
7188 if (variadic_p && arg_idx < nargs)
7189 {
7190 if (complain & tf_error)
7191 {
7192 error ("wrong number of template arguments "
7193 "(%d, should be %d)", nargs, arg_idx);
7194 if (in_decl)
7195 error ("provided for %q+D", in_decl);
7196 }
7197 return error_mark_node;
7198 }
7199
7200 if (lost)
7201 return error_mark_node;
7202
7203 #ifdef ENABLE_CHECKING
7204 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7205 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7206 TREE_VEC_LENGTH (new_inner_args));
7207 #endif
7208
7209 return new_inner_args;
7210 }
7211
7212 /* Like coerce_template_parms. If PARMS represents all template
7213 parameters levels, this function returns a vector of vectors
7214 representing all the resulting argument levels. Note that in this
7215 case, only the innermost arguments are coerced because the
7216 outermost ones are supposed to have been coerced already.
7217
7218 Otherwise, if PARMS represents only (the innermost) vector of
7219 parameters, this function returns a vector containing just the
7220 innermost resulting arguments. */
7221
7222 static tree
7223 coerce_innermost_template_parms (tree parms,
7224 tree args,
7225 tree in_decl,
7226 tsubst_flags_t complain,
7227 bool require_all_args,
7228 bool use_default_args)
7229 {
7230 int parms_depth = TMPL_PARMS_DEPTH (parms);
7231 int args_depth = TMPL_ARGS_DEPTH (args);
7232 tree coerced_args;
7233
7234 if (parms_depth > 1)
7235 {
7236 coerced_args = make_tree_vec (parms_depth);
7237 tree level;
7238 int cur_depth;
7239
7240 for (level = parms, cur_depth = parms_depth;
7241 parms_depth > 0 && level != NULL_TREE;
7242 level = TREE_CHAIN (level), --cur_depth)
7243 {
7244 tree l;
7245 if (cur_depth == args_depth)
7246 l = coerce_template_parms (TREE_VALUE (level),
7247 args, in_decl, complain,
7248 require_all_args,
7249 use_default_args);
7250 else
7251 l = TMPL_ARGS_LEVEL (args, cur_depth);
7252
7253 if (l == error_mark_node)
7254 return error_mark_node;
7255
7256 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7257 }
7258 }
7259 else
7260 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7261 args, in_decl, complain,
7262 require_all_args,
7263 use_default_args);
7264 return coerced_args;
7265 }
7266
7267 /* Returns 1 if template args OT and NT are equivalent. */
7268
7269 static int
7270 template_args_equal (tree ot, tree nt)
7271 {
7272 if (nt == ot)
7273 return 1;
7274 if (nt == NULL_TREE || ot == NULL_TREE)
7275 return false;
7276
7277 if (TREE_CODE (nt) == TREE_VEC)
7278 /* For member templates */
7279 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7280 else if (PACK_EXPANSION_P (ot))
7281 return (PACK_EXPANSION_P (nt)
7282 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7283 PACK_EXPANSION_PATTERN (nt))
7284 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7285 PACK_EXPANSION_EXTRA_ARGS (nt)));
7286 else if (ARGUMENT_PACK_P (ot))
7287 {
7288 int i, len;
7289 tree opack, npack;
7290
7291 if (!ARGUMENT_PACK_P (nt))
7292 return 0;
7293
7294 opack = ARGUMENT_PACK_ARGS (ot);
7295 npack = ARGUMENT_PACK_ARGS (nt);
7296 len = TREE_VEC_LENGTH (opack);
7297 if (TREE_VEC_LENGTH (npack) != len)
7298 return 0;
7299 for (i = 0; i < len; ++i)
7300 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7301 TREE_VEC_ELT (npack, i)))
7302 return 0;
7303 return 1;
7304 }
7305 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7306 {
7307 /* We get here probably because we are in the middle of substituting
7308 into the pattern of a pack expansion. In that case the
7309 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7310 interested in. So we want to use the initial pack argument for
7311 the comparison. */
7312 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7313 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7314 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7315 return template_args_equal (ot, nt);
7316 }
7317 else if (TYPE_P (nt))
7318 {
7319 if (!TYPE_P (ot))
7320 return false;
7321 /* Don't treat an alias template specialization with dependent
7322 arguments as equivalent to its underlying type when used as a
7323 template argument; we need them to be distinct so that we
7324 substitute into the specialization arguments at instantiation
7325 time. And aliases can't be equivalent without being ==, so
7326 we don't need to look any deeper. */
7327 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7328 return false;
7329 else
7330 return same_type_p (ot, nt);
7331 }
7332 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7333 return 0;
7334 else
7335 {
7336 /* Try to treat a template non-type argument that has been converted
7337 to the parameter type as equivalent to one that hasn't yet. */
7338 for (enum tree_code code1 = TREE_CODE (ot);
7339 CONVERT_EXPR_CODE_P (code1)
7340 || code1 == NON_LVALUE_EXPR;
7341 code1 = TREE_CODE (ot))
7342 ot = TREE_OPERAND (ot, 0);
7343 for (enum tree_code code2 = TREE_CODE (nt);
7344 CONVERT_EXPR_CODE_P (code2)
7345 || code2 == NON_LVALUE_EXPR;
7346 code2 = TREE_CODE (nt))
7347 nt = TREE_OPERAND (nt, 0);
7348
7349 return cp_tree_equal (ot, nt);
7350 }
7351 }
7352
7353 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7354 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7355 NEWARG_PTR with the offending arguments if they are non-NULL. */
7356
7357 static int
7358 comp_template_args_with_info (tree oldargs, tree newargs,
7359 tree *oldarg_ptr, tree *newarg_ptr)
7360 {
7361 int i;
7362
7363 if (oldargs == newargs)
7364 return 1;
7365
7366 if (!oldargs || !newargs)
7367 return 0;
7368
7369 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7370 return 0;
7371
7372 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7373 {
7374 tree nt = TREE_VEC_ELT (newargs, i);
7375 tree ot = TREE_VEC_ELT (oldargs, i);
7376
7377 if (! template_args_equal (ot, nt))
7378 {
7379 if (oldarg_ptr != NULL)
7380 *oldarg_ptr = ot;
7381 if (newarg_ptr != NULL)
7382 *newarg_ptr = nt;
7383 return 0;
7384 }
7385 }
7386 return 1;
7387 }
7388
7389 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7390 of template arguments. Returns 0 otherwise. */
7391
7392 int
7393 comp_template_args (tree oldargs, tree newargs)
7394 {
7395 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7396 }
7397
7398 static void
7399 add_pending_template (tree d)
7400 {
7401 tree ti = (TYPE_P (d)
7402 ? CLASSTYPE_TEMPLATE_INFO (d)
7403 : DECL_TEMPLATE_INFO (d));
7404 struct pending_template *pt;
7405 int level;
7406
7407 if (TI_PENDING_TEMPLATE_FLAG (ti))
7408 return;
7409
7410 /* We are called both from instantiate_decl, where we've already had a
7411 tinst_level pushed, and instantiate_template, where we haven't.
7412 Compensate. */
7413 level = !current_tinst_level || current_tinst_level->decl != d;
7414
7415 if (level)
7416 push_tinst_level (d);
7417
7418 pt = ggc_alloc<pending_template> ();
7419 pt->next = NULL;
7420 pt->tinst = current_tinst_level;
7421 if (last_pending_template)
7422 last_pending_template->next = pt;
7423 else
7424 pending_templates = pt;
7425
7426 last_pending_template = pt;
7427
7428 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7429
7430 if (level)
7431 pop_tinst_level ();
7432 }
7433
7434
7435 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7436 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7437 documentation for TEMPLATE_ID_EXPR. */
7438
7439 tree
7440 lookup_template_function (tree fns, tree arglist)
7441 {
7442 tree type;
7443
7444 if (fns == error_mark_node || arglist == error_mark_node)
7445 return error_mark_node;
7446
7447 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7448
7449 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7450 {
7451 error ("%q#D is not a function template", fns);
7452 return error_mark_node;
7453 }
7454
7455 if (BASELINK_P (fns))
7456 {
7457 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7458 unknown_type_node,
7459 BASELINK_FUNCTIONS (fns),
7460 arglist);
7461 return fns;
7462 }
7463
7464 type = TREE_TYPE (fns);
7465 if (TREE_CODE (fns) == OVERLOAD || !type)
7466 type = unknown_type_node;
7467
7468 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7469 }
7470
7471 /* Within the scope of a template class S<T>, the name S gets bound
7472 (in build_self_reference) to a TYPE_DECL for the class, not a
7473 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7474 or one of its enclosing classes, and that type is a template,
7475 return the associated TEMPLATE_DECL. Otherwise, the original
7476 DECL is returned.
7477
7478 Also handle the case when DECL is a TREE_LIST of ambiguous
7479 injected-class-names from different bases. */
7480
7481 tree
7482 maybe_get_template_decl_from_type_decl (tree decl)
7483 {
7484 if (decl == NULL_TREE)
7485 return decl;
7486
7487 /* DR 176: A lookup that finds an injected-class-name (10.2
7488 [class.member.lookup]) can result in an ambiguity in certain cases
7489 (for example, if it is found in more than one base class). If all of
7490 the injected-class-names that are found refer to specializations of
7491 the same class template, and if the name is followed by a
7492 template-argument-list, the reference refers to the class template
7493 itself and not a specialization thereof, and is not ambiguous. */
7494 if (TREE_CODE (decl) == TREE_LIST)
7495 {
7496 tree t, tmpl = NULL_TREE;
7497 for (t = decl; t; t = TREE_CHAIN (t))
7498 {
7499 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7500 if (!tmpl)
7501 tmpl = elt;
7502 else if (tmpl != elt)
7503 break;
7504 }
7505 if (tmpl && t == NULL_TREE)
7506 return tmpl;
7507 else
7508 return decl;
7509 }
7510
7511 return (decl != NULL_TREE
7512 && DECL_SELF_REFERENCE_P (decl)
7513 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7514 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7515 }
7516
7517 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7518 parameters, find the desired type.
7519
7520 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7521
7522 IN_DECL, if non-NULL, is the template declaration we are trying to
7523 instantiate.
7524
7525 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7526 the class we are looking up.
7527
7528 Issue error and warning messages under control of COMPLAIN.
7529
7530 If the template class is really a local class in a template
7531 function, then the FUNCTION_CONTEXT is the function in which it is
7532 being instantiated.
7533
7534 ??? Note that this function is currently called *twice* for each
7535 template-id: the first time from the parser, while creating the
7536 incomplete type (finish_template_type), and the second type during the
7537 real instantiation (instantiate_template_class). This is surely something
7538 that we want to avoid. It also causes some problems with argument
7539 coercion (see convert_nontype_argument for more information on this). */
7540
7541 static tree
7542 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7543 int entering_scope, tsubst_flags_t complain)
7544 {
7545 tree templ = NULL_TREE, parmlist;
7546 tree t;
7547 spec_entry **slot;
7548 spec_entry *entry;
7549 spec_entry elt;
7550 hashval_t hash;
7551
7552 if (identifier_p (d1))
7553 {
7554 tree value = innermost_non_namespace_value (d1);
7555 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7556 templ = value;
7557 else
7558 {
7559 if (context)
7560 push_decl_namespace (context);
7561 templ = lookup_name (d1);
7562 templ = maybe_get_template_decl_from_type_decl (templ);
7563 if (context)
7564 pop_decl_namespace ();
7565 }
7566 if (templ)
7567 context = DECL_CONTEXT (templ);
7568 }
7569 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7570 {
7571 tree type = TREE_TYPE (d1);
7572
7573 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7574 an implicit typename for the second A. Deal with it. */
7575 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7576 type = TREE_TYPE (type);
7577
7578 if (CLASSTYPE_TEMPLATE_INFO (type))
7579 {
7580 templ = CLASSTYPE_TI_TEMPLATE (type);
7581 d1 = DECL_NAME (templ);
7582 }
7583 }
7584 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7585 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7586 {
7587 templ = TYPE_TI_TEMPLATE (d1);
7588 d1 = DECL_NAME (templ);
7589 }
7590 else if (DECL_TYPE_TEMPLATE_P (d1))
7591 {
7592 templ = d1;
7593 d1 = DECL_NAME (templ);
7594 context = DECL_CONTEXT (templ);
7595 }
7596 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7597 {
7598 templ = d1;
7599 d1 = DECL_NAME (templ);
7600 }
7601
7602 /* Issue an error message if we didn't find a template. */
7603 if (! templ)
7604 {
7605 if (complain & tf_error)
7606 error ("%qT is not a template", d1);
7607 return error_mark_node;
7608 }
7609
7610 if (TREE_CODE (templ) != TEMPLATE_DECL
7611 /* Make sure it's a user visible template, if it was named by
7612 the user. */
7613 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7614 && !PRIMARY_TEMPLATE_P (templ)))
7615 {
7616 if (complain & tf_error)
7617 {
7618 error ("non-template type %qT used as a template", d1);
7619 if (in_decl)
7620 error ("for template declaration %q+D", in_decl);
7621 }
7622 return error_mark_node;
7623 }
7624
7625 complain &= ~tf_user;
7626
7627 /* An alias that just changes the name of a template is equivalent to the
7628 other template, so if any of the arguments are pack expansions, strip
7629 the alias to avoid problems with a pack expansion passed to a non-pack
7630 alias template parameter (DR 1430). */
7631 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7632 templ = get_underlying_template (templ);
7633
7634 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7635 {
7636 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7637 template arguments */
7638
7639 tree parm;
7640 tree arglist2;
7641 tree outer;
7642
7643 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7644
7645 /* Consider an example where a template template parameter declared as
7646
7647 template <class T, class U = std::allocator<T> > class TT
7648
7649 The template parameter level of T and U are one level larger than
7650 of TT. To proper process the default argument of U, say when an
7651 instantiation `TT<int>' is seen, we need to build the full
7652 arguments containing {int} as the innermost level. Outer levels,
7653 available when not appearing as default template argument, can be
7654 obtained from the arguments of the enclosing template.
7655
7656 Suppose that TT is later substituted with std::vector. The above
7657 instantiation is `TT<int, std::allocator<T> >' with TT at
7658 level 1, and T at level 2, while the template arguments at level 1
7659 becomes {std::vector} and the inner level 2 is {int}. */
7660
7661 outer = DECL_CONTEXT (templ);
7662 if (outer)
7663 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7664 else if (current_template_parms)
7665 /* This is an argument of the current template, so we haven't set
7666 DECL_CONTEXT yet. */
7667 outer = current_template_args ();
7668
7669 if (outer)
7670 arglist = add_to_template_args (outer, arglist);
7671
7672 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7673 complain,
7674 /*require_all_args=*/true,
7675 /*use_default_args=*/true);
7676 if (arglist2 == error_mark_node
7677 || (!uses_template_parms (arglist2)
7678 && check_instantiated_args (templ, arglist2, complain)))
7679 return error_mark_node;
7680
7681 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7682 return parm;
7683 }
7684 else
7685 {
7686 tree template_type = TREE_TYPE (templ);
7687 tree gen_tmpl;
7688 tree type_decl;
7689 tree found = NULL_TREE;
7690 int arg_depth;
7691 int parm_depth;
7692 int is_dependent_type;
7693 int use_partial_inst_tmpl = false;
7694
7695 if (template_type == error_mark_node)
7696 /* An error occurred while building the template TEMPL, and a
7697 diagnostic has most certainly been emitted for that
7698 already. Let's propagate that error. */
7699 return error_mark_node;
7700
7701 gen_tmpl = most_general_template (templ);
7702 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7703 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7704 arg_depth = TMPL_ARGS_DEPTH (arglist);
7705
7706 if (arg_depth == 1 && parm_depth > 1)
7707 {
7708 /* We've been given an incomplete set of template arguments.
7709 For example, given:
7710
7711 template <class T> struct S1 {
7712 template <class U> struct S2 {};
7713 template <class U> struct S2<U*> {};
7714 };
7715
7716 we will be called with an ARGLIST of `U*', but the
7717 TEMPLATE will be `template <class T> template
7718 <class U> struct S1<T>::S2'. We must fill in the missing
7719 arguments. */
7720 arglist
7721 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7722 arglist);
7723 arg_depth = TMPL_ARGS_DEPTH (arglist);
7724 }
7725
7726 /* Now we should have enough arguments. */
7727 gcc_assert (parm_depth == arg_depth);
7728
7729 /* From here on, we're only interested in the most general
7730 template. */
7731
7732 /* Calculate the BOUND_ARGS. These will be the args that are
7733 actually tsubst'd into the definition to create the
7734 instantiation. */
7735 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
7736 complain,
7737 /*require_all_args=*/true,
7738 /*use_default_args=*/true);
7739
7740 if (arglist == error_mark_node)
7741 /* We were unable to bind the arguments. */
7742 return error_mark_node;
7743
7744 /* In the scope of a template class, explicit references to the
7745 template class refer to the type of the template, not any
7746 instantiation of it. For example, in:
7747
7748 template <class T> class C { void f(C<T>); }
7749
7750 the `C<T>' is just the same as `C'. Outside of the
7751 class, however, such a reference is an instantiation. */
7752 if ((entering_scope
7753 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7754 || currently_open_class (template_type))
7755 /* comp_template_args is expensive, check it last. */
7756 && comp_template_args (TYPE_TI_ARGS (template_type),
7757 arglist))
7758 return template_type;
7759
7760 /* If we already have this specialization, return it. */
7761 elt.tmpl = gen_tmpl;
7762 elt.args = arglist;
7763 hash = spec_hasher::hash (&elt);
7764 entry = type_specializations->find_with_hash (&elt, hash);
7765
7766 if (entry)
7767 return entry->spec;
7768
7769 is_dependent_type = uses_template_parms (arglist);
7770
7771 /* If the deduced arguments are invalid, then the binding
7772 failed. */
7773 if (!is_dependent_type
7774 && check_instantiated_args (gen_tmpl,
7775 INNERMOST_TEMPLATE_ARGS (arglist),
7776 complain))
7777 return error_mark_node;
7778
7779 if (!is_dependent_type
7780 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7781 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7782 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7783 {
7784 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7785 DECL_NAME (gen_tmpl),
7786 /*tag_scope=*/ts_global);
7787 return found;
7788 }
7789
7790 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7791 complain, in_decl);
7792 if (context == error_mark_node)
7793 return error_mark_node;
7794
7795 if (!context)
7796 context = global_namespace;
7797
7798 /* Create the type. */
7799 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7800 {
7801 /* The user referred to a specialization of an alias
7802 template represented by GEN_TMPL.
7803
7804 [temp.alias]/2 says:
7805
7806 When a template-id refers to the specialization of an
7807 alias template, it is equivalent to the associated
7808 type obtained by substitution of its
7809 template-arguments for the template-parameters in the
7810 type-id of the alias template. */
7811
7812 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7813 /* Note that the call above (by indirectly calling
7814 register_specialization in tsubst_decl) registers the
7815 TYPE_DECL representing the specialization of the alias
7816 template. So next time someone substitutes ARGLIST for
7817 the template parms into the alias template (GEN_TMPL),
7818 she'll get that TYPE_DECL back. */
7819
7820 if (t == error_mark_node)
7821 return t;
7822 }
7823 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7824 {
7825 if (!is_dependent_type)
7826 {
7827 set_current_access_from_decl (TYPE_NAME (template_type));
7828 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7829 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7830 arglist, complain, in_decl),
7831 SCOPED_ENUM_P (template_type), NULL);
7832
7833 if (t == error_mark_node)
7834 return t;
7835 }
7836 else
7837 {
7838 /* We don't want to call start_enum for this type, since
7839 the values for the enumeration constants may involve
7840 template parameters. And, no one should be interested
7841 in the enumeration constants for such a type. */
7842 t = cxx_make_type (ENUMERAL_TYPE);
7843 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7844 }
7845 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7846 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7847 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7848 }
7849 else if (CLASS_TYPE_P (template_type))
7850 {
7851 t = make_class_type (TREE_CODE (template_type));
7852 CLASSTYPE_DECLARED_CLASS (t)
7853 = CLASSTYPE_DECLARED_CLASS (template_type);
7854 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7855 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7856
7857 /* A local class. Make sure the decl gets registered properly. */
7858 if (context == current_function_decl)
7859 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7860
7861 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7862 /* This instantiation is another name for the primary
7863 template type. Set the TYPE_CANONICAL field
7864 appropriately. */
7865 TYPE_CANONICAL (t) = template_type;
7866 else if (any_template_arguments_need_structural_equality_p (arglist))
7867 /* Some of the template arguments require structural
7868 equality testing, so this template class requires
7869 structural equality testing. */
7870 SET_TYPE_STRUCTURAL_EQUALITY (t);
7871 }
7872 else
7873 gcc_unreachable ();
7874
7875 /* If we called start_enum or pushtag above, this information
7876 will already be set up. */
7877 if (!TYPE_NAME (t))
7878 {
7879 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7880
7881 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7882 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7883 DECL_SOURCE_LOCATION (type_decl)
7884 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7885 }
7886 else
7887 type_decl = TYPE_NAME (t);
7888
7889 if (CLASS_TYPE_P (template_type))
7890 {
7891 TREE_PRIVATE (type_decl)
7892 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7893 TREE_PROTECTED (type_decl)
7894 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7895 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7896 {
7897 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7898 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7899 }
7900 }
7901
7902 if (OVERLOAD_TYPE_P (t)
7903 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7904 {
7905 static const char *tags[] = {"abi_tag", "may_alias"};
7906
7907 for (unsigned ix = 0; ix != 2; ix++)
7908 {
7909 tree attributes
7910 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
7911
7912 if (!attributes)
7913 ;
7914 else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
7915 TYPE_ATTRIBUTES (t) = attributes;
7916 else
7917 TYPE_ATTRIBUTES (t)
7918 = tree_cons (TREE_PURPOSE (attributes),
7919 TREE_VALUE (attributes),
7920 TYPE_ATTRIBUTES (t));
7921 }
7922 }
7923
7924 /* Let's consider the explicit specialization of a member
7925 of a class template specialization that is implicitly instantiated,
7926 e.g.:
7927 template<class T>
7928 struct S
7929 {
7930 template<class U> struct M {}; //#0
7931 };
7932
7933 template<>
7934 template<>
7935 struct S<int>::M<char> //#1
7936 {
7937 int i;
7938 };
7939 [temp.expl.spec]/4 says this is valid.
7940
7941 In this case, when we write:
7942 S<int>::M<char> m;
7943
7944 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7945 the one of #0.
7946
7947 When we encounter #1, we want to store the partial instantiation
7948 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7949
7950 For all cases other than this "explicit specialization of member of a
7951 class template", we just want to store the most general template into
7952 the CLASSTYPE_TI_TEMPLATE of M.
7953
7954 This case of "explicit specialization of member of a class template"
7955 only happens when:
7956 1/ the enclosing class is an instantiation of, and therefore not
7957 the same as, the context of the most general template, and
7958 2/ we aren't looking at the partial instantiation itself, i.e.
7959 the innermost arguments are not the same as the innermost parms of
7960 the most general template.
7961
7962 So it's only when 1/ and 2/ happens that we want to use the partial
7963 instantiation of the member template in lieu of its most general
7964 template. */
7965
7966 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7967 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7968 /* the enclosing class must be an instantiation... */
7969 && CLASS_TYPE_P (context)
7970 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7971 {
7972 tree partial_inst_args;
7973 TREE_VEC_LENGTH (arglist)--;
7974 ++processing_template_decl;
7975 partial_inst_args =
7976 tsubst (INNERMOST_TEMPLATE_ARGS
7977 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7978 arglist, complain, NULL_TREE);
7979 --processing_template_decl;
7980 TREE_VEC_LENGTH (arglist)++;
7981 use_partial_inst_tmpl =
7982 /*...and we must not be looking at the partial instantiation
7983 itself. */
7984 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7985 partial_inst_args);
7986 }
7987
7988 if (!use_partial_inst_tmpl)
7989 /* This case is easy; there are no member templates involved. */
7990 found = gen_tmpl;
7991 else
7992 {
7993 /* This is a full instantiation of a member template. Find
7994 the partial instantiation of which this is an instance. */
7995
7996 /* Temporarily reduce by one the number of levels in the ARGLIST
7997 so as to avoid comparing the last set of arguments. */
7998 TREE_VEC_LENGTH (arglist)--;
7999 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8000 TREE_VEC_LENGTH (arglist)++;
8001 /* FOUND is either a proper class type, or an alias
8002 template specialization. In the later case, it's a
8003 TYPE_DECL, resulting from the substituting of arguments
8004 for parameters in the TYPE_DECL of the alias template
8005 done earlier. So be careful while getting the template
8006 of FOUND. */
8007 found = TREE_CODE (found) == TYPE_DECL
8008 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8009 : CLASSTYPE_TI_TEMPLATE (found);
8010 }
8011
8012 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8013
8014 elt.spec = t;
8015 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8016 entry = ggc_alloc<spec_entry> ();
8017 *entry = elt;
8018 *slot = entry;
8019
8020 /* Note this use of the partial instantiation so we can check it
8021 later in maybe_process_partial_specialization. */
8022 DECL_TEMPLATE_INSTANTIATIONS (found)
8023 = tree_cons (arglist, t,
8024 DECL_TEMPLATE_INSTANTIATIONS (found));
8025
8026 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8027 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8028 /* Now that the type has been registered on the instantiations
8029 list, we set up the enumerators. Because the enumeration
8030 constants may involve the enumeration type itself, we make
8031 sure to register the type first, and then create the
8032 constants. That way, doing tsubst_expr for the enumeration
8033 constants won't result in recursive calls here; we'll find
8034 the instantiation and exit above. */
8035 tsubst_enum (template_type, t, arglist);
8036
8037 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8038 /* If the type makes use of template parameters, the
8039 code that generates debugging information will crash. */
8040 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8041
8042 /* Possibly limit visibility based on template args. */
8043 TREE_PUBLIC (type_decl) = 1;
8044 determine_visibility (type_decl);
8045
8046 inherit_targ_abi_tags (t);
8047
8048 return t;
8049 }
8050 }
8051
8052 /* Wrapper for lookup_template_class_1. */
8053
8054 tree
8055 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8056 int entering_scope, tsubst_flags_t complain)
8057 {
8058 tree ret;
8059 timevar_push (TV_TEMPLATE_INST);
8060 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8061 entering_scope, complain);
8062 timevar_pop (TV_TEMPLATE_INST);
8063 return ret;
8064 }
8065
8066 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8067 The type of the expression is the unknown_type_node since the
8068 template-id could refer to an explicit or partial specialization. */
8069
8070 tree
8071 lookup_template_variable (tree templ, tree arglist)
8072 {
8073 tree type = unknown_type_node;
8074 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8075 }
8076
8077 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8078
8079 tree
8080 finish_template_variable (tree var)
8081 {
8082 tree templ = TREE_OPERAND (var, 0);
8083
8084 tree arglist = TREE_OPERAND (var, 1);
8085 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8086 arglist = add_outermost_template_args (tmpl_args, arglist);
8087
8088 tree parms = DECL_TEMPLATE_PARMS (templ);
8089 tsubst_flags_t complain = tf_warning_or_error;
8090 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8091 /*req_all*/true,
8092 /*use_default*/true);
8093
8094 return instantiate_template (templ, arglist, complain);
8095 }
8096 \f
8097 struct pair_fn_data
8098 {
8099 tree_fn_t fn;
8100 void *data;
8101 /* True when we should also visit template parameters that occur in
8102 non-deduced contexts. */
8103 bool include_nondeduced_p;
8104 hash_set<tree> *visited;
8105 };
8106
8107 /* Called from for_each_template_parm via walk_tree. */
8108
8109 static tree
8110 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8111 {
8112 tree t = *tp;
8113 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8114 tree_fn_t fn = pfd->fn;
8115 void *data = pfd->data;
8116
8117 if (TYPE_P (t)
8118 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8119 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8120 pfd->include_nondeduced_p))
8121 return error_mark_node;
8122
8123 switch (TREE_CODE (t))
8124 {
8125 case RECORD_TYPE:
8126 if (TYPE_PTRMEMFUNC_P (t))
8127 break;
8128 /* Fall through. */
8129
8130 case UNION_TYPE:
8131 case ENUMERAL_TYPE:
8132 if (!TYPE_TEMPLATE_INFO (t))
8133 *walk_subtrees = 0;
8134 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8135 fn, data, pfd->visited,
8136 pfd->include_nondeduced_p))
8137 return error_mark_node;
8138 break;
8139
8140 case INTEGER_TYPE:
8141 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8142 fn, data, pfd->visited,
8143 pfd->include_nondeduced_p)
8144 || for_each_template_parm (TYPE_MAX_VALUE (t),
8145 fn, data, pfd->visited,
8146 pfd->include_nondeduced_p))
8147 return error_mark_node;
8148 break;
8149
8150 case METHOD_TYPE:
8151 /* Since we're not going to walk subtrees, we have to do this
8152 explicitly here. */
8153 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8154 pfd->visited, pfd->include_nondeduced_p))
8155 return error_mark_node;
8156 /* Fall through. */
8157
8158 case FUNCTION_TYPE:
8159 /* Check the return type. */
8160 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8161 pfd->include_nondeduced_p))
8162 return error_mark_node;
8163
8164 /* Check the parameter types. Since default arguments are not
8165 instantiated until they are needed, the TYPE_ARG_TYPES may
8166 contain expressions that involve template parameters. But,
8167 no-one should be looking at them yet. And, once they're
8168 instantiated, they don't contain template parameters, so
8169 there's no point in looking at them then, either. */
8170 {
8171 tree parm;
8172
8173 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8174 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8175 pfd->visited, pfd->include_nondeduced_p))
8176 return error_mark_node;
8177
8178 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8179 want walk_tree walking into them itself. */
8180 *walk_subtrees = 0;
8181 }
8182 break;
8183
8184 case TYPEOF_TYPE:
8185 case UNDERLYING_TYPE:
8186 if (pfd->include_nondeduced_p
8187 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8188 pfd->visited,
8189 pfd->include_nondeduced_p))
8190 return error_mark_node;
8191 break;
8192
8193 case FUNCTION_DECL:
8194 case VAR_DECL:
8195 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8196 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8197 pfd->visited, pfd->include_nondeduced_p))
8198 return error_mark_node;
8199 /* Fall through. */
8200
8201 case PARM_DECL:
8202 case CONST_DECL:
8203 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8204 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8205 pfd->visited, pfd->include_nondeduced_p))
8206 return error_mark_node;
8207 if (DECL_CONTEXT (t)
8208 && pfd->include_nondeduced_p
8209 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8210 pfd->visited, pfd->include_nondeduced_p))
8211 return error_mark_node;
8212 break;
8213
8214 case BOUND_TEMPLATE_TEMPLATE_PARM:
8215 /* Record template parameters such as `T' inside `TT<T>'. */
8216 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8217 pfd->include_nondeduced_p))
8218 return error_mark_node;
8219 /* Fall through. */
8220
8221 case TEMPLATE_TEMPLATE_PARM:
8222 case TEMPLATE_TYPE_PARM:
8223 case TEMPLATE_PARM_INDEX:
8224 if (fn && (*fn)(t, data))
8225 return error_mark_node;
8226 else if (!fn)
8227 return error_mark_node;
8228 break;
8229
8230 case TEMPLATE_DECL:
8231 /* A template template parameter is encountered. */
8232 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8233 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8234 pfd->include_nondeduced_p))
8235 return error_mark_node;
8236
8237 /* Already substituted template template parameter */
8238 *walk_subtrees = 0;
8239 break;
8240
8241 case TYPENAME_TYPE:
8242 if (!fn
8243 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8244 data, pfd->visited,
8245 pfd->include_nondeduced_p))
8246 return error_mark_node;
8247 break;
8248
8249 case CONSTRUCTOR:
8250 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8251 && pfd->include_nondeduced_p
8252 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8253 (TREE_TYPE (t)), fn, data,
8254 pfd->visited, pfd->include_nondeduced_p))
8255 return error_mark_node;
8256 break;
8257
8258 case INDIRECT_REF:
8259 case COMPONENT_REF:
8260 /* If there's no type, then this thing must be some expression
8261 involving template parameters. */
8262 if (!fn && !TREE_TYPE (t))
8263 return error_mark_node;
8264 break;
8265
8266 case MODOP_EXPR:
8267 case CAST_EXPR:
8268 case IMPLICIT_CONV_EXPR:
8269 case REINTERPRET_CAST_EXPR:
8270 case CONST_CAST_EXPR:
8271 case STATIC_CAST_EXPR:
8272 case DYNAMIC_CAST_EXPR:
8273 case ARROW_EXPR:
8274 case DOTSTAR_EXPR:
8275 case TYPEID_EXPR:
8276 case PSEUDO_DTOR_EXPR:
8277 if (!fn)
8278 return error_mark_node;
8279 break;
8280
8281 default:
8282 break;
8283 }
8284
8285 /* We didn't find any template parameters we liked. */
8286 return NULL_TREE;
8287 }
8288
8289 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8290 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8291 call FN with the parameter and the DATA.
8292 If FN returns nonzero, the iteration is terminated, and
8293 for_each_template_parm returns 1. Otherwise, the iteration
8294 continues. If FN never returns a nonzero value, the value
8295 returned by for_each_template_parm is 0. If FN is NULL, it is
8296 considered to be the function which always returns 1.
8297
8298 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8299 parameters that occur in non-deduced contexts. When false, only
8300 visits those template parameters that can be deduced. */
8301
8302 static int
8303 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8304 hash_set<tree> *visited,
8305 bool include_nondeduced_p)
8306 {
8307 struct pair_fn_data pfd;
8308 int result;
8309
8310 /* Set up. */
8311 pfd.fn = fn;
8312 pfd.data = data;
8313 pfd.include_nondeduced_p = include_nondeduced_p;
8314
8315 /* Walk the tree. (Conceptually, we would like to walk without
8316 duplicates, but for_each_template_parm_r recursively calls
8317 for_each_template_parm, so we would need to reorganize a fair
8318 bit to use walk_tree_without_duplicates, so we keep our own
8319 visited list.) */
8320 if (visited)
8321 pfd.visited = visited;
8322 else
8323 pfd.visited = new hash_set<tree>;
8324 result = cp_walk_tree (&t,
8325 for_each_template_parm_r,
8326 &pfd,
8327 pfd.visited) != NULL_TREE;
8328
8329 /* Clean up. */
8330 if (!visited)
8331 {
8332 delete pfd.visited;
8333 pfd.visited = 0;
8334 }
8335
8336 return result;
8337 }
8338
8339 /* Returns true if T depends on any template parameter. */
8340
8341 int
8342 uses_template_parms (tree t)
8343 {
8344 if (t == NULL_TREE)
8345 return false;
8346
8347 bool dependent_p;
8348 int saved_processing_template_decl;
8349
8350 saved_processing_template_decl = processing_template_decl;
8351 if (!saved_processing_template_decl)
8352 processing_template_decl = 1;
8353 if (TYPE_P (t))
8354 dependent_p = dependent_type_p (t);
8355 else if (TREE_CODE (t) == TREE_VEC)
8356 dependent_p = any_dependent_template_arguments_p (t);
8357 else if (TREE_CODE (t) == TREE_LIST)
8358 dependent_p = (uses_template_parms (TREE_VALUE (t))
8359 || uses_template_parms (TREE_CHAIN (t)));
8360 else if (TREE_CODE (t) == TYPE_DECL)
8361 dependent_p = dependent_type_p (TREE_TYPE (t));
8362 else if (DECL_P (t)
8363 || EXPR_P (t)
8364 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8365 || TREE_CODE (t) == OVERLOAD
8366 || BASELINK_P (t)
8367 || identifier_p (t)
8368 || TREE_CODE (t) == TRAIT_EXPR
8369 || TREE_CODE (t) == CONSTRUCTOR
8370 || CONSTANT_CLASS_P (t))
8371 dependent_p = (type_dependent_expression_p (t)
8372 || value_dependent_expression_p (t));
8373 else
8374 {
8375 gcc_assert (t == error_mark_node);
8376 dependent_p = false;
8377 }
8378
8379 processing_template_decl = saved_processing_template_decl;
8380
8381 return dependent_p;
8382 }
8383
8384 /* Returns true iff current_function_decl is an incompletely instantiated
8385 template. Useful instead of processing_template_decl because the latter
8386 is set to 0 during instantiate_non_dependent_expr. */
8387
8388 bool
8389 in_template_function (void)
8390 {
8391 tree fn = current_function_decl;
8392 bool ret;
8393 ++processing_template_decl;
8394 ret = (fn && DECL_LANG_SPECIFIC (fn)
8395 && DECL_TEMPLATE_INFO (fn)
8396 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8397 --processing_template_decl;
8398 return ret;
8399 }
8400
8401 /* Returns true if T depends on any template parameter with level LEVEL. */
8402
8403 int
8404 uses_template_parms_level (tree t, int level)
8405 {
8406 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8407 /*include_nondeduced_p=*/true);
8408 }
8409
8410 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8411 ill-formed translation unit, i.e. a variable or function that isn't
8412 usable in a constant expression. */
8413
8414 static inline bool
8415 neglectable_inst_p (tree d)
8416 {
8417 return (DECL_P (d)
8418 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8419 : decl_maybe_constant_var_p (d)));
8420 }
8421
8422 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8423 neglectable and instantiated from within an erroneous instantiation. */
8424
8425 static bool
8426 limit_bad_template_recursion (tree decl)
8427 {
8428 struct tinst_level *lev = current_tinst_level;
8429 int errs = errorcount + sorrycount;
8430 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8431 return false;
8432
8433 for (; lev; lev = lev->next)
8434 if (neglectable_inst_p (lev->decl))
8435 break;
8436
8437 return (lev && errs > lev->errors);
8438 }
8439
8440 static int tinst_depth;
8441 extern int max_tinst_depth;
8442 int depth_reached;
8443
8444 static GTY(()) struct tinst_level *last_error_tinst_level;
8445
8446 /* We're starting to instantiate D; record the template instantiation context
8447 for diagnostics and to restore it later. */
8448
8449 bool
8450 push_tinst_level (tree d)
8451 {
8452 return push_tinst_level_loc (d, input_location);
8453 }
8454
8455 /* We're starting to instantiate D; record the template instantiation context
8456 at LOC for diagnostics and to restore it later. */
8457
8458 bool
8459 push_tinst_level_loc (tree d, location_t loc)
8460 {
8461 struct tinst_level *new_level;
8462
8463 if (tinst_depth >= max_tinst_depth)
8464 {
8465 fatal_error (input_location,
8466 "template instantiation depth exceeds maximum of %d"
8467 " (use -ftemplate-depth= to increase the maximum)",
8468 max_tinst_depth);
8469 return false;
8470 }
8471
8472 /* If the current instantiation caused problems, don't let it instantiate
8473 anything else. Do allow deduction substitution and decls usable in
8474 constant expressions. */
8475 if (limit_bad_template_recursion (d))
8476 return false;
8477
8478 new_level = ggc_alloc<tinst_level> ();
8479 new_level->decl = d;
8480 new_level->locus = loc;
8481 new_level->errors = errorcount+sorrycount;
8482 new_level->in_system_header_p = in_system_header_at (input_location);
8483 new_level->next = current_tinst_level;
8484 current_tinst_level = new_level;
8485
8486 ++tinst_depth;
8487 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8488 depth_reached = tinst_depth;
8489
8490 return true;
8491 }
8492
8493 /* We're done instantiating this template; return to the instantiation
8494 context. */
8495
8496 void
8497 pop_tinst_level (void)
8498 {
8499 /* Restore the filename and line number stashed away when we started
8500 this instantiation. */
8501 input_location = current_tinst_level->locus;
8502 current_tinst_level = current_tinst_level->next;
8503 --tinst_depth;
8504 }
8505
8506 /* We're instantiating a deferred template; restore the template
8507 instantiation context in which the instantiation was requested, which
8508 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8509
8510 static tree
8511 reopen_tinst_level (struct tinst_level *level)
8512 {
8513 struct tinst_level *t;
8514
8515 tinst_depth = 0;
8516 for (t = level; t; t = t->next)
8517 ++tinst_depth;
8518
8519 current_tinst_level = level;
8520 pop_tinst_level ();
8521 if (current_tinst_level)
8522 current_tinst_level->errors = errorcount+sorrycount;
8523 return level->decl;
8524 }
8525
8526 /* Returns the TINST_LEVEL which gives the original instantiation
8527 context. */
8528
8529 struct tinst_level *
8530 outermost_tinst_level (void)
8531 {
8532 struct tinst_level *level = current_tinst_level;
8533 if (level)
8534 while (level->next)
8535 level = level->next;
8536 return level;
8537 }
8538
8539 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8540 vector of template arguments, as for tsubst.
8541
8542 Returns an appropriate tsubst'd friend declaration. */
8543
8544 static tree
8545 tsubst_friend_function (tree decl, tree args)
8546 {
8547 tree new_friend;
8548
8549 if (TREE_CODE (decl) == FUNCTION_DECL
8550 && DECL_TEMPLATE_INSTANTIATION (decl)
8551 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8552 /* This was a friend declared with an explicit template
8553 argument list, e.g.:
8554
8555 friend void f<>(T);
8556
8557 to indicate that f was a template instantiation, not a new
8558 function declaration. Now, we have to figure out what
8559 instantiation of what template. */
8560 {
8561 tree template_id, arglist, fns;
8562 tree new_args;
8563 tree tmpl;
8564 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8565
8566 /* Friend functions are looked up in the containing namespace scope.
8567 We must enter that scope, to avoid finding member functions of the
8568 current class with same name. */
8569 push_nested_namespace (ns);
8570 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8571 tf_warning_or_error, NULL_TREE,
8572 /*integral_constant_expression_p=*/false);
8573 pop_nested_namespace (ns);
8574 arglist = tsubst (DECL_TI_ARGS (decl), args,
8575 tf_warning_or_error, NULL_TREE);
8576 template_id = lookup_template_function (fns, arglist);
8577
8578 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8579 tmpl = determine_specialization (template_id, new_friend,
8580 &new_args,
8581 /*need_member_template=*/0,
8582 TREE_VEC_LENGTH (args),
8583 tsk_none);
8584 return instantiate_template (tmpl, new_args, tf_error);
8585 }
8586
8587 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8588
8589 /* The NEW_FRIEND will look like an instantiation, to the
8590 compiler, but is not an instantiation from the point of view of
8591 the language. For example, we might have had:
8592
8593 template <class T> struct S {
8594 template <class U> friend void f(T, U);
8595 };
8596
8597 Then, in S<int>, template <class U> void f(int, U) is not an
8598 instantiation of anything. */
8599 if (new_friend == error_mark_node)
8600 return error_mark_node;
8601
8602 DECL_USE_TEMPLATE (new_friend) = 0;
8603 if (TREE_CODE (decl) == TEMPLATE_DECL)
8604 {
8605 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8606 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8607 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8608 }
8609
8610 /* The mangled name for the NEW_FRIEND is incorrect. The function
8611 is not a template instantiation and should not be mangled like
8612 one. Therefore, we forget the mangling here; we'll recompute it
8613 later if we need it. */
8614 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8615 {
8616 SET_DECL_RTL (new_friend, NULL);
8617 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8618 }
8619
8620 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8621 {
8622 tree old_decl;
8623 tree new_friend_template_info;
8624 tree new_friend_result_template_info;
8625 tree ns;
8626 int new_friend_is_defn;
8627
8628 /* We must save some information from NEW_FRIEND before calling
8629 duplicate decls since that function will free NEW_FRIEND if
8630 possible. */
8631 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8632 new_friend_is_defn =
8633 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8634 (template_for_substitution (new_friend)))
8635 != NULL_TREE);
8636 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8637 {
8638 /* This declaration is a `primary' template. */
8639 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8640
8641 new_friend_result_template_info
8642 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8643 }
8644 else
8645 new_friend_result_template_info = NULL_TREE;
8646
8647 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8648 if (new_friend_is_defn)
8649 DECL_INITIAL (new_friend) = error_mark_node;
8650
8651 /* Inside pushdecl_namespace_level, we will push into the
8652 current namespace. However, the friend function should go
8653 into the namespace of the template. */
8654 ns = decl_namespace_context (new_friend);
8655 push_nested_namespace (ns);
8656 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8657 pop_nested_namespace (ns);
8658
8659 if (old_decl == error_mark_node)
8660 return error_mark_node;
8661
8662 if (old_decl != new_friend)
8663 {
8664 /* This new friend declaration matched an existing
8665 declaration. For example, given:
8666
8667 template <class T> void f(T);
8668 template <class U> class C {
8669 template <class T> friend void f(T) {}
8670 };
8671
8672 the friend declaration actually provides the definition
8673 of `f', once C has been instantiated for some type. So,
8674 old_decl will be the out-of-class template declaration,
8675 while new_friend is the in-class definition.
8676
8677 But, if `f' was called before this point, the
8678 instantiation of `f' will have DECL_TI_ARGS corresponding
8679 to `T' but not to `U', references to which might appear
8680 in the definition of `f'. Previously, the most general
8681 template for an instantiation of `f' was the out-of-class
8682 version; now it is the in-class version. Therefore, we
8683 run through all specialization of `f', adding to their
8684 DECL_TI_ARGS appropriately. In particular, they need a
8685 new set of outer arguments, corresponding to the
8686 arguments for this class instantiation.
8687
8688 The same situation can arise with something like this:
8689
8690 friend void f(int);
8691 template <class T> class C {
8692 friend void f(T) {}
8693 };
8694
8695 when `C<int>' is instantiated. Now, `f(int)' is defined
8696 in the class. */
8697
8698 if (!new_friend_is_defn)
8699 /* On the other hand, if the in-class declaration does
8700 *not* provide a definition, then we don't want to alter
8701 existing definitions. We can just leave everything
8702 alone. */
8703 ;
8704 else
8705 {
8706 tree new_template = TI_TEMPLATE (new_friend_template_info);
8707 tree new_args = TI_ARGS (new_friend_template_info);
8708
8709 /* Overwrite whatever template info was there before, if
8710 any, with the new template information pertaining to
8711 the declaration. */
8712 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8713
8714 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8715 {
8716 /* We should have called reregister_specialization in
8717 duplicate_decls. */
8718 gcc_assert (retrieve_specialization (new_template,
8719 new_args, 0)
8720 == old_decl);
8721
8722 /* Instantiate it if the global has already been used. */
8723 if (DECL_ODR_USED (old_decl))
8724 instantiate_decl (old_decl, /*defer_ok=*/true,
8725 /*expl_inst_class_mem_p=*/false);
8726 }
8727 else
8728 {
8729 tree t;
8730
8731 /* Indicate that the old function template is a partial
8732 instantiation. */
8733 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8734 = new_friend_result_template_info;
8735
8736 gcc_assert (new_template
8737 == most_general_template (new_template));
8738 gcc_assert (new_template != old_decl);
8739
8740 /* Reassign any specializations already in the hash table
8741 to the new more general template, and add the
8742 additional template args. */
8743 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8744 t != NULL_TREE;
8745 t = TREE_CHAIN (t))
8746 {
8747 tree spec = TREE_VALUE (t);
8748 spec_entry elt;
8749
8750 elt.tmpl = old_decl;
8751 elt.args = DECL_TI_ARGS (spec);
8752 elt.spec = NULL_TREE;
8753
8754 decl_specializations->remove_elt (&elt);
8755
8756 DECL_TI_ARGS (spec)
8757 = add_outermost_template_args (new_args,
8758 DECL_TI_ARGS (spec));
8759
8760 register_specialization
8761 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8762
8763 }
8764 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8765 }
8766 }
8767
8768 /* The information from NEW_FRIEND has been merged into OLD_DECL
8769 by duplicate_decls. */
8770 new_friend = old_decl;
8771 }
8772 }
8773 else
8774 {
8775 tree context = DECL_CONTEXT (new_friend);
8776 bool dependent_p;
8777
8778 /* In the code
8779 template <class T> class C {
8780 template <class U> friend void C1<U>::f (); // case 1
8781 friend void C2<T>::f (); // case 2
8782 };
8783 we only need to make sure CONTEXT is a complete type for
8784 case 2. To distinguish between the two cases, we note that
8785 CONTEXT of case 1 remains dependent type after tsubst while
8786 this isn't true for case 2. */
8787 ++processing_template_decl;
8788 dependent_p = dependent_type_p (context);
8789 --processing_template_decl;
8790
8791 if (!dependent_p
8792 && !complete_type_or_else (context, NULL_TREE))
8793 return error_mark_node;
8794
8795 if (COMPLETE_TYPE_P (context))
8796 {
8797 tree fn = new_friend;
8798 /* do_friend adds the TEMPLATE_DECL for any member friend
8799 template even if it isn't a member template, i.e.
8800 template <class T> friend A<T>::f();
8801 Look through it in that case. */
8802 if (TREE_CODE (fn) == TEMPLATE_DECL
8803 && !PRIMARY_TEMPLATE_P (fn))
8804 fn = DECL_TEMPLATE_RESULT (fn);
8805 /* Check to see that the declaration is really present, and,
8806 possibly obtain an improved declaration. */
8807 fn = check_classfn (context, fn, NULL_TREE);
8808
8809 if (fn)
8810 new_friend = fn;
8811 }
8812 }
8813
8814 return new_friend;
8815 }
8816
8817 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8818 template arguments, as for tsubst.
8819
8820 Returns an appropriate tsubst'd friend type or error_mark_node on
8821 failure. */
8822
8823 static tree
8824 tsubst_friend_class (tree friend_tmpl, tree args)
8825 {
8826 tree friend_type;
8827 tree tmpl;
8828 tree context;
8829
8830 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8831 {
8832 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8833 return TREE_TYPE (t);
8834 }
8835
8836 context = CP_DECL_CONTEXT (friend_tmpl);
8837
8838 if (context != global_namespace)
8839 {
8840 if (TREE_CODE (context) == NAMESPACE_DECL)
8841 push_nested_namespace (context);
8842 else
8843 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8844 }
8845
8846 /* Look for a class template declaration. We look for hidden names
8847 because two friend declarations of the same template are the
8848 same. For example, in:
8849
8850 struct A {
8851 template <typename> friend class F;
8852 };
8853 template <typename> struct B {
8854 template <typename> friend class F;
8855 };
8856
8857 both F templates are the same. */
8858 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8859 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8860
8861 /* But, if we don't find one, it might be because we're in a
8862 situation like this:
8863
8864 template <class T>
8865 struct S {
8866 template <class U>
8867 friend struct S;
8868 };
8869
8870 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8871 for `S<int>', not the TEMPLATE_DECL. */
8872 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8873 {
8874 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8875 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8876 }
8877
8878 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8879 {
8880 /* The friend template has already been declared. Just
8881 check to see that the declarations match, and install any new
8882 default parameters. We must tsubst the default parameters,
8883 of course. We only need the innermost template parameters
8884 because that is all that redeclare_class_template will look
8885 at. */
8886 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8887 > TMPL_ARGS_DEPTH (args))
8888 {
8889 tree parms;
8890 location_t saved_input_location;
8891 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8892 args, tf_warning_or_error);
8893
8894 saved_input_location = input_location;
8895 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8896 redeclare_class_template (TREE_TYPE (tmpl), parms);
8897 input_location = saved_input_location;
8898
8899 }
8900
8901 friend_type = TREE_TYPE (tmpl);
8902 }
8903 else
8904 {
8905 /* The friend template has not already been declared. In this
8906 case, the instantiation of the template class will cause the
8907 injection of this template into the global scope. */
8908 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8909 if (tmpl == error_mark_node)
8910 return error_mark_node;
8911
8912 /* The new TMPL is not an instantiation of anything, so we
8913 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8914 the new type because that is supposed to be the corresponding
8915 template decl, i.e., TMPL. */
8916 DECL_USE_TEMPLATE (tmpl) = 0;
8917 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8918 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8919 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8920 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8921
8922 /* Inject this template into the global scope. */
8923 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8924 }
8925
8926 if (context != global_namespace)
8927 {
8928 if (TREE_CODE (context) == NAMESPACE_DECL)
8929 pop_nested_namespace (context);
8930 else
8931 pop_nested_class ();
8932 }
8933
8934 return friend_type;
8935 }
8936
8937 /* Returns zero if TYPE cannot be completed later due to circularity.
8938 Otherwise returns one. */
8939
8940 static int
8941 can_complete_type_without_circularity (tree type)
8942 {
8943 if (type == NULL_TREE || type == error_mark_node)
8944 return 0;
8945 else if (COMPLETE_TYPE_P (type))
8946 return 1;
8947 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8948 return can_complete_type_without_circularity (TREE_TYPE (type));
8949 else if (CLASS_TYPE_P (type)
8950 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8951 return 0;
8952 else
8953 return 1;
8954 }
8955
8956 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8957
8958 /* Apply any attributes which had to be deferred until instantiation
8959 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8960 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8961
8962 static void
8963 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8964 tree args, tsubst_flags_t complain, tree in_decl)
8965 {
8966 tree last_dep = NULL_TREE;
8967 tree t;
8968 tree *p;
8969
8970 for (t = attributes; t; t = TREE_CHAIN (t))
8971 if (ATTR_IS_DEPENDENT (t))
8972 {
8973 last_dep = t;
8974 attributes = copy_list (attributes);
8975 break;
8976 }
8977
8978 if (DECL_P (*decl_p))
8979 {
8980 if (TREE_TYPE (*decl_p) == error_mark_node)
8981 return;
8982 p = &DECL_ATTRIBUTES (*decl_p);
8983 }
8984 else
8985 p = &TYPE_ATTRIBUTES (*decl_p);
8986
8987 if (last_dep)
8988 {
8989 tree late_attrs = NULL_TREE;
8990 tree *q = &late_attrs;
8991
8992 for (*p = attributes; *p; )
8993 {
8994 t = *p;
8995 if (ATTR_IS_DEPENDENT (t))
8996 {
8997 *p = TREE_CHAIN (t);
8998 TREE_CHAIN (t) = NULL_TREE;
8999 if ((flag_openmp || flag_cilkplus)
9000 && is_attribute_p ("omp declare simd",
9001 get_attribute_name (t))
9002 && TREE_VALUE (t))
9003 {
9004 tree clauses = TREE_VALUE (TREE_VALUE (t));
9005 clauses = tsubst_omp_clauses (clauses, true, args,
9006 complain, in_decl);
9007 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9008 clauses = finish_omp_clauses (clauses);
9009 tree parms = DECL_ARGUMENTS (*decl_p);
9010 clauses
9011 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9012 if (clauses)
9013 TREE_VALUE (TREE_VALUE (t)) = clauses;
9014 else
9015 TREE_VALUE (t) = NULL_TREE;
9016 }
9017 /* If the first attribute argument is an identifier, don't
9018 pass it through tsubst. Attributes like mode, format,
9019 cleanup and several target specific attributes expect it
9020 unmodified. */
9021 else if (attribute_takes_identifier_p (get_attribute_name (t))
9022 && TREE_VALUE (t))
9023 {
9024 tree chain
9025 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9026 in_decl,
9027 /*integral_constant_expression_p=*/false);
9028 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9029 TREE_VALUE (t)
9030 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9031 chain);
9032 }
9033 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9034 {
9035 /* An attribute pack expansion. */
9036 tree purp = TREE_PURPOSE (t);
9037 tree pack = (tsubst_pack_expansion
9038 (TREE_VALUE (t), args, complain, in_decl));
9039 int len = TREE_VEC_LENGTH (pack);
9040 for (int i = 0; i < len; ++i)
9041 {
9042 tree elt = TREE_VEC_ELT (pack, i);
9043 *q = build_tree_list (purp, elt);
9044 q = &TREE_CHAIN (*q);
9045 }
9046 continue;
9047 }
9048 else
9049 TREE_VALUE (t)
9050 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9051 /*integral_constant_expression_p=*/false);
9052 *q = t;
9053 q = &TREE_CHAIN (t);
9054 }
9055 else
9056 p = &TREE_CHAIN (t);
9057 }
9058
9059 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9060 }
9061 }
9062
9063 /* Perform (or defer) access check for typedefs that were referenced
9064 from within the template TMPL code.
9065 This is a subroutine of instantiate_decl and instantiate_class_template.
9066 TMPL is the template to consider and TARGS is the list of arguments of
9067 that template. */
9068
9069 static void
9070 perform_typedefs_access_check (tree tmpl, tree targs)
9071 {
9072 location_t saved_location;
9073 unsigned i;
9074 qualified_typedef_usage_t *iter;
9075
9076 if (!tmpl
9077 || (!CLASS_TYPE_P (tmpl)
9078 && TREE_CODE (tmpl) != FUNCTION_DECL))
9079 return;
9080
9081 saved_location = input_location;
9082 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9083 {
9084 tree type_decl = iter->typedef_decl;
9085 tree type_scope = iter->context;
9086
9087 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9088 continue;
9089
9090 if (uses_template_parms (type_decl))
9091 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9092 if (uses_template_parms (type_scope))
9093 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9094
9095 /* Make access check error messages point to the location
9096 of the use of the typedef. */
9097 input_location = iter->locus;
9098 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9099 type_decl, type_decl,
9100 tf_warning_or_error);
9101 }
9102 input_location = saved_location;
9103 }
9104
9105 static tree
9106 instantiate_class_template_1 (tree type)
9107 {
9108 tree templ, args, pattern, t, member;
9109 tree typedecl;
9110 tree pbinfo;
9111 tree base_list;
9112 unsigned int saved_maximum_field_alignment;
9113 tree fn_context;
9114
9115 if (type == error_mark_node)
9116 return error_mark_node;
9117
9118 if (COMPLETE_OR_OPEN_TYPE_P (type)
9119 || uses_template_parms (type))
9120 return type;
9121
9122 /* Figure out which template is being instantiated. */
9123 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9124 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9125
9126 /* Determine what specialization of the original template to
9127 instantiate. */
9128 t = most_specialized_partial_spec (type, tf_warning_or_error);
9129 if (t == error_mark_node)
9130 {
9131 TYPE_BEING_DEFINED (type) = 1;
9132 return error_mark_node;
9133 }
9134 else if (t)
9135 {
9136 /* This TYPE is actually an instantiation of a partial
9137 specialization. We replace the innermost set of ARGS with
9138 the arguments appropriate for substitution. For example,
9139 given:
9140
9141 template <class T> struct S {};
9142 template <class T> struct S<T*> {};
9143
9144 and supposing that we are instantiating S<int*>, ARGS will
9145 presently be {int*} -- but we need {int}. */
9146 pattern = TREE_TYPE (t);
9147 args = TREE_PURPOSE (t);
9148 }
9149 else
9150 {
9151 pattern = TREE_TYPE (templ);
9152 args = CLASSTYPE_TI_ARGS (type);
9153 }
9154
9155 /* If the template we're instantiating is incomplete, then clearly
9156 there's nothing we can do. */
9157 if (!COMPLETE_TYPE_P (pattern))
9158 return type;
9159
9160 /* If we've recursively instantiated too many templates, stop. */
9161 if (! push_tinst_level (type))
9162 return type;
9163
9164 /* Now we're really doing the instantiation. Mark the type as in
9165 the process of being defined. */
9166 TYPE_BEING_DEFINED (type) = 1;
9167
9168 /* We may be in the middle of deferred access check. Disable
9169 it now. */
9170 push_deferring_access_checks (dk_no_deferred);
9171
9172 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9173 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9174 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9175 fn_context = error_mark_node;
9176 if (!fn_context)
9177 push_to_top_level ();
9178 /* Use #pragma pack from the template context. */
9179 saved_maximum_field_alignment = maximum_field_alignment;
9180 maximum_field_alignment = TYPE_PRECISION (pattern);
9181
9182 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9183
9184 /* Set the input location to the most specialized template definition.
9185 This is needed if tsubsting causes an error. */
9186 typedecl = TYPE_MAIN_DECL (pattern);
9187 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9188 DECL_SOURCE_LOCATION (typedecl);
9189
9190 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9191 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9192 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9193 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9194 if (ANON_AGGR_TYPE_P (pattern))
9195 SET_ANON_AGGR_TYPE_P (type);
9196 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9197 {
9198 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9199 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9200 /* Adjust visibility for template arguments. */
9201 determine_visibility (TYPE_MAIN_DECL (type));
9202 }
9203 if (CLASS_TYPE_P (type))
9204 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9205
9206 pbinfo = TYPE_BINFO (pattern);
9207
9208 /* We should never instantiate a nested class before its enclosing
9209 class; we need to look up the nested class by name before we can
9210 instantiate it, and that lookup should instantiate the enclosing
9211 class. */
9212 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9213 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9214
9215 base_list = NULL_TREE;
9216 if (BINFO_N_BASE_BINFOS (pbinfo))
9217 {
9218 tree pbase_binfo;
9219 tree pushed_scope;
9220 int i;
9221
9222 /* We must enter the scope containing the type, as that is where
9223 the accessibility of types named in dependent bases are
9224 looked up from. */
9225 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9226
9227 /* Substitute into each of the bases to determine the actual
9228 basetypes. */
9229 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9230 {
9231 tree base;
9232 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9233 tree expanded_bases = NULL_TREE;
9234 int idx, len = 1;
9235
9236 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9237 {
9238 expanded_bases =
9239 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9240 args, tf_error, NULL_TREE);
9241 if (expanded_bases == error_mark_node)
9242 continue;
9243
9244 len = TREE_VEC_LENGTH (expanded_bases);
9245 }
9246
9247 for (idx = 0; idx < len; idx++)
9248 {
9249 if (expanded_bases)
9250 /* Extract the already-expanded base class. */
9251 base = TREE_VEC_ELT (expanded_bases, idx);
9252 else
9253 /* Substitute to figure out the base class. */
9254 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9255 NULL_TREE);
9256
9257 if (base == error_mark_node)
9258 continue;
9259
9260 base_list = tree_cons (access, base, base_list);
9261 if (BINFO_VIRTUAL_P (pbase_binfo))
9262 TREE_TYPE (base_list) = integer_type_node;
9263 }
9264 }
9265
9266 /* The list is now in reverse order; correct that. */
9267 base_list = nreverse (base_list);
9268
9269 if (pushed_scope)
9270 pop_scope (pushed_scope);
9271 }
9272 /* Now call xref_basetypes to set up all the base-class
9273 information. */
9274 xref_basetypes (type, base_list);
9275
9276 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9277 (int) ATTR_FLAG_TYPE_IN_PLACE,
9278 args, tf_error, NULL_TREE);
9279 fixup_attribute_variants (type);
9280
9281 /* Now that our base classes are set up, enter the scope of the
9282 class, so that name lookups into base classes, etc. will work
9283 correctly. This is precisely analogous to what we do in
9284 begin_class_definition when defining an ordinary non-template
9285 class, except we also need to push the enclosing classes. */
9286 push_nested_class (type);
9287
9288 /* Now members are processed in the order of declaration. */
9289 for (member = CLASSTYPE_DECL_LIST (pattern);
9290 member; member = TREE_CHAIN (member))
9291 {
9292 tree t = TREE_VALUE (member);
9293
9294 if (TREE_PURPOSE (member))
9295 {
9296 if (TYPE_P (t))
9297 {
9298 /* Build new CLASSTYPE_NESTED_UTDS. */
9299
9300 tree newtag;
9301 bool class_template_p;
9302
9303 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9304 && TYPE_LANG_SPECIFIC (t)
9305 && CLASSTYPE_IS_TEMPLATE (t));
9306 /* If the member is a class template, then -- even after
9307 substitution -- there may be dependent types in the
9308 template argument list for the class. We increment
9309 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9310 that function will assume that no types are dependent
9311 when outside of a template. */
9312 if (class_template_p)
9313 ++processing_template_decl;
9314 newtag = tsubst (t, args, tf_error, NULL_TREE);
9315 if (class_template_p)
9316 --processing_template_decl;
9317 if (newtag == error_mark_node)
9318 continue;
9319
9320 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9321 {
9322 tree name = TYPE_IDENTIFIER (t);
9323
9324 if (class_template_p)
9325 /* Unfortunately, lookup_template_class sets
9326 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9327 instantiation (i.e., for the type of a member
9328 template class nested within a template class.)
9329 This behavior is required for
9330 maybe_process_partial_specialization to work
9331 correctly, but is not accurate in this case;
9332 the TAG is not an instantiation of anything.
9333 (The corresponding TEMPLATE_DECL is an
9334 instantiation, but the TYPE is not.) */
9335 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9336
9337 /* Now, we call pushtag to put this NEWTAG into the scope of
9338 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9339 pushtag calling push_template_decl. We don't have to do
9340 this for enums because it will already have been done in
9341 tsubst_enum. */
9342 if (name)
9343 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9344 pushtag (name, newtag, /*tag_scope=*/ts_current);
9345 }
9346 }
9347 else if (DECL_DECLARES_FUNCTION_P (t))
9348 {
9349 /* Build new TYPE_METHODS. */
9350 tree r;
9351
9352 if (TREE_CODE (t) == TEMPLATE_DECL)
9353 ++processing_template_decl;
9354 r = tsubst (t, args, tf_error, NULL_TREE);
9355 if (TREE_CODE (t) == TEMPLATE_DECL)
9356 --processing_template_decl;
9357 set_current_access_from_decl (r);
9358 finish_member_declaration (r);
9359 /* Instantiate members marked with attribute used. */
9360 if (r != error_mark_node && DECL_PRESERVE_P (r))
9361 mark_used (r);
9362 if (TREE_CODE (r) == FUNCTION_DECL
9363 && DECL_OMP_DECLARE_REDUCTION_P (r))
9364 cp_check_omp_declare_reduction (r);
9365 }
9366 else if (DECL_CLASS_TEMPLATE_P (t)
9367 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9368 /* A closure type for a lambda in a default argument for a
9369 member template. Ignore it; it will be instantiated with
9370 the default argument. */;
9371 else
9372 {
9373 /* Build new TYPE_FIELDS. */
9374 if (TREE_CODE (t) == STATIC_ASSERT)
9375 {
9376 tree condition;
9377
9378 ++c_inhibit_evaluation_warnings;
9379 condition =
9380 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9381 tf_warning_or_error, NULL_TREE,
9382 /*integral_constant_expression_p=*/true);
9383 --c_inhibit_evaluation_warnings;
9384
9385 finish_static_assert (condition,
9386 STATIC_ASSERT_MESSAGE (t),
9387 STATIC_ASSERT_SOURCE_LOCATION (t),
9388 /*member_p=*/true);
9389 }
9390 else if (TREE_CODE (t) != CONST_DECL)
9391 {
9392 tree r;
9393 tree vec = NULL_TREE;
9394 int len = 1;
9395
9396 /* The file and line for this declaration, to
9397 assist in error message reporting. Since we
9398 called push_tinst_level above, we don't need to
9399 restore these. */
9400 input_location = DECL_SOURCE_LOCATION (t);
9401
9402 if (TREE_CODE (t) == TEMPLATE_DECL)
9403 ++processing_template_decl;
9404 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9405 if (TREE_CODE (t) == TEMPLATE_DECL)
9406 --processing_template_decl;
9407
9408 if (TREE_CODE (r) == TREE_VEC)
9409 {
9410 /* A capture pack became multiple fields. */
9411 vec = r;
9412 len = TREE_VEC_LENGTH (vec);
9413 }
9414
9415 for (int i = 0; i < len; ++i)
9416 {
9417 if (vec)
9418 r = TREE_VEC_ELT (vec, i);
9419 if (VAR_P (r))
9420 {
9421 /* In [temp.inst]:
9422
9423 [t]he initialization (and any associated
9424 side-effects) of a static data member does
9425 not occur unless the static data member is
9426 itself used in a way that requires the
9427 definition of the static data member to
9428 exist.
9429
9430 Therefore, we do not substitute into the
9431 initialized for the static data member here. */
9432 finish_static_data_member_decl
9433 (r,
9434 /*init=*/NULL_TREE,
9435 /*init_const_expr_p=*/false,
9436 /*asmspec_tree=*/NULL_TREE,
9437 /*flags=*/0);
9438 /* Instantiate members marked with attribute used. */
9439 if (r != error_mark_node && DECL_PRESERVE_P (r))
9440 mark_used (r);
9441 }
9442 else if (TREE_CODE (r) == FIELD_DECL)
9443 {
9444 /* Determine whether R has a valid type and can be
9445 completed later. If R is invalid, then its type
9446 is replaced by error_mark_node. */
9447 tree rtype = TREE_TYPE (r);
9448 if (can_complete_type_without_circularity (rtype))
9449 complete_type (rtype);
9450
9451 if (!COMPLETE_TYPE_P (rtype))
9452 {
9453 cxx_incomplete_type_error (r, rtype);
9454 TREE_TYPE (r) = error_mark_node;
9455 }
9456 }
9457
9458 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9459 such a thing will already have been added to the field
9460 list by tsubst_enum in finish_member_declaration in the
9461 CLASSTYPE_NESTED_UTDS case above. */
9462 if (!(TREE_CODE (r) == TYPE_DECL
9463 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9464 && DECL_ARTIFICIAL (r)))
9465 {
9466 set_current_access_from_decl (r);
9467 finish_member_declaration (r);
9468 }
9469 }
9470 }
9471 }
9472 }
9473 else
9474 {
9475 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9476 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9477 {
9478 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9479
9480 tree friend_type = t;
9481 bool adjust_processing_template_decl = false;
9482
9483 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9484 {
9485 /* template <class T> friend class C; */
9486 friend_type = tsubst_friend_class (friend_type, args);
9487 adjust_processing_template_decl = true;
9488 }
9489 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9490 {
9491 /* template <class T> friend class C::D; */
9492 friend_type = tsubst (friend_type, args,
9493 tf_warning_or_error, NULL_TREE);
9494 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9495 friend_type = TREE_TYPE (friend_type);
9496 adjust_processing_template_decl = true;
9497 }
9498 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9499 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9500 {
9501 /* This could be either
9502
9503 friend class T::C;
9504
9505 when dependent_type_p is false or
9506
9507 template <class U> friend class T::C;
9508
9509 otherwise. */
9510 friend_type = tsubst (friend_type, args,
9511 tf_warning_or_error, NULL_TREE);
9512 /* Bump processing_template_decl for correct
9513 dependent_type_p calculation. */
9514 ++processing_template_decl;
9515 if (dependent_type_p (friend_type))
9516 adjust_processing_template_decl = true;
9517 --processing_template_decl;
9518 }
9519 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9520 && hidden_name_p (TYPE_NAME (friend_type)))
9521 {
9522 /* friend class C;
9523
9524 where C hasn't been declared yet. Let's lookup name
9525 from namespace scope directly, bypassing any name that
9526 come from dependent base class. */
9527 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9528
9529 /* The call to xref_tag_from_type does injection for friend
9530 classes. */
9531 push_nested_namespace (ns);
9532 friend_type =
9533 xref_tag_from_type (friend_type, NULL_TREE,
9534 /*tag_scope=*/ts_current);
9535 pop_nested_namespace (ns);
9536 }
9537 else if (uses_template_parms (friend_type))
9538 /* friend class C<T>; */
9539 friend_type = tsubst (friend_type, args,
9540 tf_warning_or_error, NULL_TREE);
9541 /* Otherwise it's
9542
9543 friend class C;
9544
9545 where C is already declared or
9546
9547 friend class C<int>;
9548
9549 We don't have to do anything in these cases. */
9550
9551 if (adjust_processing_template_decl)
9552 /* Trick make_friend_class into realizing that the friend
9553 we're adding is a template, not an ordinary class. It's
9554 important that we use make_friend_class since it will
9555 perform some error-checking and output cross-reference
9556 information. */
9557 ++processing_template_decl;
9558
9559 if (friend_type != error_mark_node)
9560 make_friend_class (type, friend_type, /*complain=*/false);
9561
9562 if (adjust_processing_template_decl)
9563 --processing_template_decl;
9564 }
9565 else
9566 {
9567 /* Build new DECL_FRIENDLIST. */
9568 tree r;
9569
9570 /* The file and line for this declaration, to
9571 assist in error message reporting. Since we
9572 called push_tinst_level above, we don't need to
9573 restore these. */
9574 input_location = DECL_SOURCE_LOCATION (t);
9575
9576 if (TREE_CODE (t) == TEMPLATE_DECL)
9577 {
9578 ++processing_template_decl;
9579 push_deferring_access_checks (dk_no_check);
9580 }
9581
9582 r = tsubst_friend_function (t, args);
9583 add_friend (type, r, /*complain=*/false);
9584 if (TREE_CODE (t) == TEMPLATE_DECL)
9585 {
9586 pop_deferring_access_checks ();
9587 --processing_template_decl;
9588 }
9589 }
9590 }
9591 }
9592
9593 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9594 {
9595 tree decl = lambda_function (type);
9596 if (decl)
9597 {
9598 if (!DECL_TEMPLATE_INFO (decl)
9599 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9600 instantiate_decl (decl, false, false);
9601
9602 /* We need to instantiate the capture list from the template
9603 after we've instantiated the closure members, but before we
9604 consider adding the conversion op. Also keep any captures
9605 that may have been added during instantiation of the op(). */
9606 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9607 tree tmpl_cap
9608 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9609 args, tf_warning_or_error, NULL_TREE,
9610 false, false);
9611
9612 LAMBDA_EXPR_CAPTURE_LIST (expr)
9613 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9614
9615 maybe_add_lambda_conv_op (type);
9616 }
9617 else
9618 gcc_assert (errorcount);
9619 }
9620
9621 /* Set the file and line number information to whatever is given for
9622 the class itself. This puts error messages involving generated
9623 implicit functions at a predictable point, and the same point
9624 that would be used for non-template classes. */
9625 input_location = DECL_SOURCE_LOCATION (typedecl);
9626
9627 unreverse_member_declarations (type);
9628 finish_struct_1 (type);
9629 TYPE_BEING_DEFINED (type) = 0;
9630
9631 /* We don't instantiate default arguments for member functions. 14.7.1:
9632
9633 The implicit instantiation of a class template specialization causes
9634 the implicit instantiation of the declarations, but not of the
9635 definitions or default arguments, of the class member functions,
9636 member classes, static data members and member templates.... */
9637
9638 /* Some typedefs referenced from within the template code need to be access
9639 checked at template instantiation time, i.e now. These types were
9640 added to the template at parsing time. Let's get those and perform
9641 the access checks then. */
9642 perform_typedefs_access_check (pattern, args);
9643 perform_deferred_access_checks (tf_warning_or_error);
9644 pop_nested_class ();
9645 maximum_field_alignment = saved_maximum_field_alignment;
9646 if (!fn_context)
9647 pop_from_top_level ();
9648 pop_deferring_access_checks ();
9649 pop_tinst_level ();
9650
9651 /* The vtable for a template class can be emitted in any translation
9652 unit in which the class is instantiated. When there is no key
9653 method, however, finish_struct_1 will already have added TYPE to
9654 the keyed_classes list. */
9655 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9656 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9657
9658 return type;
9659 }
9660
9661 /* Wrapper for instantiate_class_template_1. */
9662
9663 tree
9664 instantiate_class_template (tree type)
9665 {
9666 tree ret;
9667 timevar_push (TV_TEMPLATE_INST);
9668 ret = instantiate_class_template_1 (type);
9669 timevar_pop (TV_TEMPLATE_INST);
9670 return ret;
9671 }
9672
9673 static tree
9674 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9675 {
9676 tree r;
9677
9678 if (!t)
9679 r = t;
9680 else if (TYPE_P (t))
9681 r = tsubst (t, args, complain, in_decl);
9682 else
9683 {
9684 if (!(complain & tf_warning))
9685 ++c_inhibit_evaluation_warnings;
9686 r = tsubst_expr (t, args, complain, in_decl,
9687 /*integral_constant_expression_p=*/true);
9688 if (!(complain & tf_warning))
9689 --c_inhibit_evaluation_warnings;
9690 }
9691 return r;
9692 }
9693
9694 /* Given a function parameter pack TMPL_PARM and some function parameters
9695 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9696 and set *SPEC_P to point at the next point in the list. */
9697
9698 static tree
9699 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9700 {
9701 /* Collect all of the extra "packed" parameters into an
9702 argument pack. */
9703 tree parmvec;
9704 tree parmtypevec;
9705 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9706 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9707 tree spec_parm = *spec_p;
9708 int i, len;
9709
9710 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9711 if (tmpl_parm
9712 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9713 break;
9714
9715 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9716 parmvec = make_tree_vec (len);
9717 parmtypevec = make_tree_vec (len);
9718 spec_parm = *spec_p;
9719 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9720 {
9721 TREE_VEC_ELT (parmvec, i) = spec_parm;
9722 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9723 }
9724
9725 /* Build the argument packs. */
9726 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9727 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9728 TREE_TYPE (argpack) = argtypepack;
9729 *spec_p = spec_parm;
9730
9731 return argpack;
9732 }
9733
9734 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9735 NONTYPE_ARGUMENT_PACK. */
9736
9737 static tree
9738 make_fnparm_pack (tree spec_parm)
9739 {
9740 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9741 }
9742
9743 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9744 pack expansion. */
9745
9746 static bool
9747 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9748 {
9749 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9750 if (i >= TREE_VEC_LENGTH (vec))
9751 return false;
9752 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9753 }
9754
9755
9756 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9757
9758 static tree
9759 make_argument_pack_select (tree arg_pack, unsigned index)
9760 {
9761 tree aps = make_node (ARGUMENT_PACK_SELECT);
9762
9763 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9764 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9765
9766 return aps;
9767 }
9768
9769 /* This is a subroutine of tsubst_pack_expansion.
9770
9771 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9772 mechanism to store the (non complete list of) arguments of the
9773 substitution and return a non substituted pack expansion, in order
9774 to wait for when we have enough arguments to really perform the
9775 substitution. */
9776
9777 static bool
9778 use_pack_expansion_extra_args_p (tree parm_packs,
9779 int arg_pack_len,
9780 bool has_empty_arg)
9781 {
9782 /* If one pack has an expansion and another pack has a normal
9783 argument or if one pack has an empty argument and an another
9784 one hasn't then tsubst_pack_expansion cannot perform the
9785 substitution and need to fall back on the
9786 PACK_EXPANSION_EXTRA mechanism. */
9787 if (parm_packs == NULL_TREE)
9788 return false;
9789 else if (has_empty_arg)
9790 return true;
9791
9792 bool has_expansion_arg = false;
9793 for (int i = 0 ; i < arg_pack_len; ++i)
9794 {
9795 bool has_non_expansion_arg = false;
9796 for (tree parm_pack = parm_packs;
9797 parm_pack;
9798 parm_pack = TREE_CHAIN (parm_pack))
9799 {
9800 tree arg = TREE_VALUE (parm_pack);
9801
9802 if (argument_pack_element_is_expansion_p (arg, i))
9803 has_expansion_arg = true;
9804 else
9805 has_non_expansion_arg = true;
9806 }
9807
9808 if (has_expansion_arg && has_non_expansion_arg)
9809 return true;
9810 }
9811 return false;
9812 }
9813
9814 /* [temp.variadic]/6 says that:
9815
9816 The instantiation of a pack expansion [...]
9817 produces a list E1,E2, ..., En, where N is the number of elements
9818 in the pack expansion parameters.
9819
9820 This subroutine of tsubst_pack_expansion produces one of these Ei.
9821
9822 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9823 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9824 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9825 INDEX is the index 'i' of the element Ei to produce. ARGS,
9826 COMPLAIN, and IN_DECL are the same parameters as for the
9827 tsubst_pack_expansion function.
9828
9829 The function returns the resulting Ei upon successful completion,
9830 or error_mark_node.
9831
9832 Note that this function possibly modifies the ARGS parameter, so
9833 it's the responsibility of the caller to restore it. */
9834
9835 static tree
9836 gen_elem_of_pack_expansion_instantiation (tree pattern,
9837 tree parm_packs,
9838 unsigned index,
9839 tree args /* This parm gets
9840 modified. */,
9841 tsubst_flags_t complain,
9842 tree in_decl)
9843 {
9844 tree t;
9845 bool ith_elem_is_expansion = false;
9846
9847 /* For each parameter pack, change the substitution of the parameter
9848 pack to the ith argument in its argument pack, then expand the
9849 pattern. */
9850 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9851 {
9852 tree parm = TREE_PURPOSE (pack);
9853 tree arg_pack = TREE_VALUE (pack);
9854 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9855
9856 ith_elem_is_expansion |=
9857 argument_pack_element_is_expansion_p (arg_pack, index);
9858
9859 /* Select the Ith argument from the pack. */
9860 if (TREE_CODE (parm) == PARM_DECL
9861 || TREE_CODE (parm) == FIELD_DECL)
9862 {
9863 if (index == 0)
9864 {
9865 aps = make_argument_pack_select (arg_pack, index);
9866 if (!mark_used (parm, complain) && !(complain & tf_error))
9867 return error_mark_node;
9868 register_local_specialization (aps, parm);
9869 }
9870 else
9871 aps = retrieve_local_specialization (parm);
9872 }
9873 else
9874 {
9875 int idx, level;
9876 template_parm_level_and_index (parm, &level, &idx);
9877
9878 if (index == 0)
9879 {
9880 aps = make_argument_pack_select (arg_pack, index);
9881 /* Update the corresponding argument. */
9882 TMPL_ARG (args, level, idx) = aps;
9883 }
9884 else
9885 /* Re-use the ARGUMENT_PACK_SELECT. */
9886 aps = TMPL_ARG (args, level, idx);
9887 }
9888 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9889 }
9890
9891 /* Substitute into the PATTERN with the (possibly altered)
9892 arguments. */
9893 if (pattern == in_decl)
9894 /* Expanding a fixed parameter pack from
9895 coerce_template_parameter_pack. */
9896 t = tsubst_decl (pattern, args, complain);
9897 else if (!TYPE_P (pattern))
9898 t = tsubst_expr (pattern, args, complain, in_decl,
9899 /*integral_constant_expression_p=*/false);
9900 else
9901 t = tsubst (pattern, args, complain, in_decl);
9902
9903 /* If the Ith argument pack element is a pack expansion, then
9904 the Ith element resulting from the substituting is going to
9905 be a pack expansion as well. */
9906 if (ith_elem_is_expansion)
9907 t = make_pack_expansion (t);
9908
9909 return t;
9910 }
9911
9912 /* Substitute ARGS into T, which is an pack expansion
9913 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9914 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9915 (if only a partial substitution could be performed) or
9916 ERROR_MARK_NODE if there was an error. */
9917 tree
9918 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9919 tree in_decl)
9920 {
9921 tree pattern;
9922 tree pack, packs = NULL_TREE;
9923 bool unsubstituted_packs = false;
9924 int i, len = -1;
9925 tree result;
9926 hash_map<tree, tree> *saved_local_specializations = NULL;
9927 bool need_local_specializations = false;
9928 int levels;
9929
9930 gcc_assert (PACK_EXPANSION_P (t));
9931 pattern = PACK_EXPANSION_PATTERN (t);
9932
9933 /* Add in any args remembered from an earlier partial instantiation. */
9934 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9935
9936 levels = TMPL_ARGS_DEPTH (args);
9937
9938 /* Determine the argument packs that will instantiate the parameter
9939 packs used in the expansion expression. While we're at it,
9940 compute the number of arguments to be expanded and make sure it
9941 is consistent. */
9942 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9943 pack = TREE_CHAIN (pack))
9944 {
9945 tree parm_pack = TREE_VALUE (pack);
9946 tree arg_pack = NULL_TREE;
9947 tree orig_arg = NULL_TREE;
9948 int level = 0;
9949
9950 if (TREE_CODE (parm_pack) == BASES)
9951 {
9952 if (BASES_DIRECT (parm_pack))
9953 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9954 args, complain, in_decl, false));
9955 else
9956 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9957 args, complain, in_decl, false));
9958 }
9959 if (TREE_CODE (parm_pack) == PARM_DECL)
9960 {
9961 if (PACK_EXPANSION_LOCAL_P (t))
9962 arg_pack = retrieve_local_specialization (parm_pack);
9963 else
9964 {
9965 /* We can't rely on local_specializations for a parameter
9966 name used later in a function declaration (such as in a
9967 late-specified return type). Even if it exists, it might
9968 have the wrong value for a recursive call. Just make a
9969 dummy decl, since it's only used for its type. */
9970 arg_pack = tsubst_decl (parm_pack, args, complain);
9971 if (arg_pack && DECL_PACK_P (arg_pack))
9972 /* Partial instantiation of the parm_pack, we can't build
9973 up an argument pack yet. */
9974 arg_pack = NULL_TREE;
9975 else
9976 arg_pack = make_fnparm_pack (arg_pack);
9977 need_local_specializations = true;
9978 }
9979 }
9980 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9981 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9982 else
9983 {
9984 int idx;
9985 template_parm_level_and_index (parm_pack, &level, &idx);
9986
9987 if (level <= levels)
9988 arg_pack = TMPL_ARG (args, level, idx);
9989 }
9990
9991 orig_arg = arg_pack;
9992 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9993 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9994
9995 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9996 /* This can only happen if we forget to expand an argument
9997 pack somewhere else. Just return an error, silently. */
9998 {
9999 result = make_tree_vec (1);
10000 TREE_VEC_ELT (result, 0) = error_mark_node;
10001 return result;
10002 }
10003
10004 if (arg_pack)
10005 {
10006 int my_len =
10007 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10008
10009 /* Don't bother trying to do a partial substitution with
10010 incomplete packs; we'll try again after deduction. */
10011 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10012 return t;
10013
10014 if (len < 0)
10015 len = my_len;
10016 else if (len != my_len)
10017 {
10018 if (!(complain & tf_error))
10019 /* Fail quietly. */;
10020 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10021 error ("mismatched argument pack lengths while expanding "
10022 "%<%T%>",
10023 pattern);
10024 else
10025 error ("mismatched argument pack lengths while expanding "
10026 "%<%E%>",
10027 pattern);
10028 return error_mark_node;
10029 }
10030
10031 /* Keep track of the parameter packs and their corresponding
10032 argument packs. */
10033 packs = tree_cons (parm_pack, arg_pack, packs);
10034 TREE_TYPE (packs) = orig_arg;
10035 }
10036 else
10037 {
10038 /* We can't substitute for this parameter pack. We use a flag as
10039 well as the missing_level counter because function parameter
10040 packs don't have a level. */
10041 unsubstituted_packs = true;
10042 }
10043 }
10044
10045 /* If the expansion is just T..., return the matching argument pack. */
10046 if (!unsubstituted_packs
10047 && TREE_PURPOSE (packs) == pattern)
10048 {
10049 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10050 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10051 || pack_expansion_args_count (args))
10052 return args;
10053 /* Otherwise use the normal path so we get convert_from_reference. */
10054 }
10055
10056 /* We cannot expand this expansion expression, because we don't have
10057 all of the argument packs we need. */
10058 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10059 {
10060 /* We got some full packs, but we can't substitute them in until we
10061 have values for all the packs. So remember these until then. */
10062
10063 t = make_pack_expansion (pattern);
10064 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10065 return t;
10066 }
10067 else if (unsubstituted_packs)
10068 {
10069 /* There were no real arguments, we're just replacing a parameter
10070 pack with another version of itself. Substitute into the
10071 pattern and return a PACK_EXPANSION_*. The caller will need to
10072 deal with that. */
10073 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10074 t = tsubst_expr (pattern, args, complain, in_decl,
10075 /*integral_constant_expression_p=*/false);
10076 else
10077 t = tsubst (pattern, args, complain, in_decl);
10078 t = make_pack_expansion (t);
10079 return t;
10080 }
10081
10082 gcc_assert (len >= 0);
10083
10084 if (need_local_specializations)
10085 {
10086 /* We're in a late-specified return type, so create our own local
10087 specializations map; the current map is either NULL or (in the
10088 case of recursive unification) might have bindings that we don't
10089 want to use or alter. */
10090 saved_local_specializations = local_specializations;
10091 local_specializations = new hash_map<tree, tree>;
10092 }
10093
10094 /* For each argument in each argument pack, substitute into the
10095 pattern. */
10096 result = make_tree_vec (len);
10097 for (i = 0; i < len; ++i)
10098 {
10099 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10100 i,
10101 args, complain,
10102 in_decl);
10103 TREE_VEC_ELT (result, i) = t;
10104 if (t == error_mark_node)
10105 {
10106 result = error_mark_node;
10107 break;
10108 }
10109 }
10110
10111 /* Update ARGS to restore the substitution from parameter packs to
10112 their argument packs. */
10113 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10114 {
10115 tree parm = TREE_PURPOSE (pack);
10116
10117 if (TREE_CODE (parm) == PARM_DECL
10118 || TREE_CODE (parm) == FIELD_DECL)
10119 register_local_specialization (TREE_TYPE (pack), parm);
10120 else
10121 {
10122 int idx, level;
10123
10124 if (TREE_VALUE (pack) == NULL_TREE)
10125 continue;
10126
10127 template_parm_level_and_index (parm, &level, &idx);
10128
10129 /* Update the corresponding argument. */
10130 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10131 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10132 TREE_TYPE (pack);
10133 else
10134 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10135 }
10136 }
10137
10138 if (need_local_specializations)
10139 {
10140 delete local_specializations;
10141 local_specializations = saved_local_specializations;
10142 }
10143
10144 return result;
10145 }
10146
10147 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10148 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10149 parameter packs; all parms generated from a function parameter pack will
10150 have the same DECL_PARM_INDEX. */
10151
10152 tree
10153 get_pattern_parm (tree parm, tree tmpl)
10154 {
10155 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10156 tree patparm;
10157
10158 if (DECL_ARTIFICIAL (parm))
10159 {
10160 for (patparm = DECL_ARGUMENTS (pattern);
10161 patparm; patparm = DECL_CHAIN (patparm))
10162 if (DECL_ARTIFICIAL (patparm)
10163 && DECL_NAME (parm) == DECL_NAME (patparm))
10164 break;
10165 }
10166 else
10167 {
10168 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10169 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10170 gcc_assert (DECL_PARM_INDEX (patparm)
10171 == DECL_PARM_INDEX (parm));
10172 }
10173
10174 return patparm;
10175 }
10176
10177 /* Substitute ARGS into the vector or list of template arguments T. */
10178
10179 static tree
10180 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10181 {
10182 tree orig_t = t;
10183 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10184 tree *elts;
10185
10186 if (t == error_mark_node)
10187 return error_mark_node;
10188
10189 len = TREE_VEC_LENGTH (t);
10190 elts = XALLOCAVEC (tree, len);
10191
10192 for (i = 0; i < len; i++)
10193 {
10194 tree orig_arg = TREE_VEC_ELT (t, i);
10195 tree new_arg;
10196
10197 if (TREE_CODE (orig_arg) == TREE_VEC)
10198 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10199 else if (PACK_EXPANSION_P (orig_arg))
10200 {
10201 /* Substitute into an expansion expression. */
10202 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10203
10204 if (TREE_CODE (new_arg) == TREE_VEC)
10205 /* Add to the expanded length adjustment the number of
10206 expanded arguments. We subtract one from this
10207 measurement, because the argument pack expression
10208 itself is already counted as 1 in
10209 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10210 the argument pack is empty. */
10211 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10212 }
10213 else if (ARGUMENT_PACK_P (orig_arg))
10214 {
10215 /* Substitute into each of the arguments. */
10216 new_arg = TYPE_P (orig_arg)
10217 ? cxx_make_type (TREE_CODE (orig_arg))
10218 : make_node (TREE_CODE (orig_arg));
10219
10220 SET_ARGUMENT_PACK_ARGS (
10221 new_arg,
10222 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10223 args, complain, in_decl));
10224
10225 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10226 new_arg = error_mark_node;
10227
10228 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10229 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10230 complain, in_decl);
10231 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10232
10233 if (TREE_TYPE (new_arg) == error_mark_node)
10234 new_arg = error_mark_node;
10235 }
10236 }
10237 else
10238 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10239
10240 if (new_arg == error_mark_node)
10241 return error_mark_node;
10242
10243 elts[i] = new_arg;
10244 if (new_arg != orig_arg)
10245 need_new = 1;
10246 }
10247
10248 if (!need_new)
10249 return t;
10250
10251 /* Make space for the expanded arguments coming from template
10252 argument packs. */
10253 t = make_tree_vec (len + expanded_len_adjust);
10254 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10255 arguments for a member template.
10256 In that case each TREE_VEC in ORIG_T represents a level of template
10257 arguments, and ORIG_T won't carry any non defaulted argument count.
10258 It will rather be the nested TREE_VECs that will carry one.
10259 In other words, ORIG_T carries a non defaulted argument count only
10260 if it doesn't contain any nested TREE_VEC. */
10261 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10262 {
10263 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10264 count += expanded_len_adjust;
10265 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10266 }
10267 for (i = 0, out = 0; i < len; i++)
10268 {
10269 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10270 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10271 && TREE_CODE (elts[i]) == TREE_VEC)
10272 {
10273 int idx;
10274
10275 /* Now expand the template argument pack "in place". */
10276 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10277 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10278 }
10279 else
10280 {
10281 TREE_VEC_ELT (t, out) = elts[i];
10282 out++;
10283 }
10284 }
10285
10286 return t;
10287 }
10288
10289 /* Return the result of substituting ARGS into the template parameters
10290 given by PARMS. If there are m levels of ARGS and m + n levels of
10291 PARMS, then the result will contain n levels of PARMS. For
10292 example, if PARMS is `template <class T> template <class U>
10293 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10294 result will be `template <int*, double, class V>'. */
10295
10296 static tree
10297 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10298 {
10299 tree r = NULL_TREE;
10300 tree* new_parms;
10301
10302 /* When substituting into a template, we must set
10303 PROCESSING_TEMPLATE_DECL as the template parameters may be
10304 dependent if they are based on one-another, and the dependency
10305 predicates are short-circuit outside of templates. */
10306 ++processing_template_decl;
10307
10308 for (new_parms = &r;
10309 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10310 new_parms = &(TREE_CHAIN (*new_parms)),
10311 parms = TREE_CHAIN (parms))
10312 {
10313 tree new_vec =
10314 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10315 int i;
10316
10317 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10318 {
10319 tree tuple;
10320
10321 if (parms == error_mark_node)
10322 continue;
10323
10324 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10325
10326 if (tuple == error_mark_node)
10327 continue;
10328
10329 TREE_VEC_ELT (new_vec, i) =
10330 tsubst_template_parm (tuple, args, complain);
10331 }
10332
10333 *new_parms =
10334 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10335 - TMPL_ARGS_DEPTH (args)),
10336 new_vec, NULL_TREE);
10337 }
10338
10339 --processing_template_decl;
10340
10341 return r;
10342 }
10343
10344 /* Return the result of substituting ARGS into one template parameter
10345 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10346 parameter and which TREE_PURPOSE is the default argument of the
10347 template parameter. */
10348
10349 static tree
10350 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10351 {
10352 tree default_value, parm_decl;
10353
10354 if (args == NULL_TREE
10355 || t == NULL_TREE
10356 || t == error_mark_node)
10357 return t;
10358
10359 gcc_assert (TREE_CODE (t) == TREE_LIST);
10360
10361 default_value = TREE_PURPOSE (t);
10362 parm_decl = TREE_VALUE (t);
10363
10364 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10365 if (TREE_CODE (parm_decl) == PARM_DECL
10366 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10367 parm_decl = error_mark_node;
10368 default_value = tsubst_template_arg (default_value, args,
10369 complain, NULL_TREE);
10370
10371 return build_tree_list (default_value, parm_decl);
10372 }
10373
10374 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10375 type T. If T is not an aggregate or enumeration type, it is
10376 handled as if by tsubst. IN_DECL is as for tsubst. If
10377 ENTERING_SCOPE is nonzero, T is the context for a template which
10378 we are presently tsubst'ing. Return the substituted value. */
10379
10380 static tree
10381 tsubst_aggr_type (tree t,
10382 tree args,
10383 tsubst_flags_t complain,
10384 tree in_decl,
10385 int entering_scope)
10386 {
10387 if (t == NULL_TREE)
10388 return NULL_TREE;
10389
10390 switch (TREE_CODE (t))
10391 {
10392 case RECORD_TYPE:
10393 if (TYPE_PTRMEMFUNC_P (t))
10394 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10395
10396 /* Else fall through. */
10397 case ENUMERAL_TYPE:
10398 case UNION_TYPE:
10399 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10400 {
10401 tree argvec;
10402 tree context;
10403 tree r;
10404 int saved_unevaluated_operand;
10405 int saved_inhibit_evaluation_warnings;
10406
10407 /* In "sizeof(X<I>)" we need to evaluate "I". */
10408 saved_unevaluated_operand = cp_unevaluated_operand;
10409 cp_unevaluated_operand = 0;
10410 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10411 c_inhibit_evaluation_warnings = 0;
10412
10413 /* First, determine the context for the type we are looking
10414 up. */
10415 context = TYPE_CONTEXT (t);
10416 if (context && TYPE_P (context))
10417 {
10418 context = tsubst_aggr_type (context, args, complain,
10419 in_decl, /*entering_scope=*/1);
10420 /* If context is a nested class inside a class template,
10421 it may still need to be instantiated (c++/33959). */
10422 context = complete_type (context);
10423 }
10424
10425 /* Then, figure out what arguments are appropriate for the
10426 type we are trying to find. For example, given:
10427
10428 template <class T> struct S;
10429 template <class T, class U> void f(T, U) { S<U> su; }
10430
10431 and supposing that we are instantiating f<int, double>,
10432 then our ARGS will be {int, double}, but, when looking up
10433 S we only want {double}. */
10434 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10435 complain, in_decl);
10436 if (argvec == error_mark_node)
10437 r = error_mark_node;
10438 else
10439 {
10440 r = lookup_template_class (t, argvec, in_decl, context,
10441 entering_scope, complain);
10442 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10443 }
10444
10445 cp_unevaluated_operand = saved_unevaluated_operand;
10446 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10447
10448 return r;
10449 }
10450 else
10451 /* This is not a template type, so there's nothing to do. */
10452 return t;
10453
10454 default:
10455 return tsubst (t, args, complain, in_decl);
10456 }
10457 }
10458
10459 /* Substitute into the default argument ARG (a default argument for
10460 FN), which has the indicated TYPE. */
10461
10462 tree
10463 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10464 {
10465 tree saved_class_ptr = NULL_TREE;
10466 tree saved_class_ref = NULL_TREE;
10467 int errs = errorcount + sorrycount;
10468
10469 /* This can happen in invalid code. */
10470 if (TREE_CODE (arg) == DEFAULT_ARG)
10471 return arg;
10472
10473 /* This default argument came from a template. Instantiate the
10474 default argument here, not in tsubst. In the case of
10475 something like:
10476
10477 template <class T>
10478 struct S {
10479 static T t();
10480 void f(T = t());
10481 };
10482
10483 we must be careful to do name lookup in the scope of S<T>,
10484 rather than in the current class. */
10485 push_access_scope (fn);
10486 /* The "this" pointer is not valid in a default argument. */
10487 if (cfun)
10488 {
10489 saved_class_ptr = current_class_ptr;
10490 cp_function_chain->x_current_class_ptr = NULL_TREE;
10491 saved_class_ref = current_class_ref;
10492 cp_function_chain->x_current_class_ref = NULL_TREE;
10493 }
10494
10495 push_deferring_access_checks(dk_no_deferred);
10496 /* The default argument expression may cause implicitly defined
10497 member functions to be synthesized, which will result in garbage
10498 collection. We must treat this situation as if we were within
10499 the body of function so as to avoid collecting live data on the
10500 stack. */
10501 ++function_depth;
10502 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10503 complain, NULL_TREE,
10504 /*integral_constant_expression_p=*/false);
10505 --function_depth;
10506 pop_deferring_access_checks();
10507
10508 /* Restore the "this" pointer. */
10509 if (cfun)
10510 {
10511 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10512 cp_function_chain->x_current_class_ref = saved_class_ref;
10513 }
10514
10515 if (errorcount+sorrycount > errs
10516 && (complain & tf_warning_or_error))
10517 inform (input_location,
10518 " when instantiating default argument for call to %D", fn);
10519
10520 /* Make sure the default argument is reasonable. */
10521 arg = check_default_argument (type, arg, complain);
10522
10523 pop_access_scope (fn);
10524
10525 return arg;
10526 }
10527
10528 /* Substitute into all the default arguments for FN. */
10529
10530 static void
10531 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10532 {
10533 tree arg;
10534 tree tmpl_args;
10535
10536 tmpl_args = DECL_TI_ARGS (fn);
10537
10538 /* If this function is not yet instantiated, we certainly don't need
10539 its default arguments. */
10540 if (uses_template_parms (tmpl_args))
10541 return;
10542 /* Don't do this again for clones. */
10543 if (DECL_CLONED_FUNCTION_P (fn))
10544 return;
10545
10546 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10547 arg;
10548 arg = TREE_CHAIN (arg))
10549 if (TREE_PURPOSE (arg))
10550 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10551 TREE_VALUE (arg),
10552 TREE_PURPOSE (arg),
10553 complain);
10554 }
10555
10556 /* Substitute the ARGS into the T, which is a _DECL. Return the
10557 result of the substitution. Issue error and warning messages under
10558 control of COMPLAIN. */
10559
10560 static tree
10561 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10562 {
10563 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10564 location_t saved_loc;
10565 tree r = NULL_TREE;
10566 tree in_decl = t;
10567 hashval_t hash = 0;
10568
10569 /* Set the filename and linenumber to improve error-reporting. */
10570 saved_loc = input_location;
10571 input_location = DECL_SOURCE_LOCATION (t);
10572
10573 switch (TREE_CODE (t))
10574 {
10575 case TEMPLATE_DECL:
10576 {
10577 /* We can get here when processing a member function template,
10578 member class template, or template template parameter. */
10579 tree decl = DECL_TEMPLATE_RESULT (t);
10580 tree spec;
10581 tree tmpl_args;
10582 tree full_args;
10583
10584 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10585 {
10586 /* Template template parameter is treated here. */
10587 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10588 if (new_type == error_mark_node)
10589 RETURN (error_mark_node);
10590 /* If we get a real template back, return it. This can happen in
10591 the context of most_specialized_partial_spec. */
10592 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10593 return new_type;
10594
10595 r = copy_decl (t);
10596 DECL_CHAIN (r) = NULL_TREE;
10597 TREE_TYPE (r) = new_type;
10598 DECL_TEMPLATE_RESULT (r)
10599 = build_decl (DECL_SOURCE_LOCATION (decl),
10600 TYPE_DECL, DECL_NAME (decl), new_type);
10601 DECL_TEMPLATE_PARMS (r)
10602 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10603 complain);
10604 TYPE_NAME (new_type) = r;
10605 break;
10606 }
10607
10608 /* We might already have an instance of this template.
10609 The ARGS are for the surrounding class type, so the
10610 full args contain the tsubst'd args for the context,
10611 plus the innermost args from the template decl. */
10612 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10613 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10614 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10615 /* Because this is a template, the arguments will still be
10616 dependent, even after substitution. If
10617 PROCESSING_TEMPLATE_DECL is not set, the dependency
10618 predicates will short-circuit. */
10619 ++processing_template_decl;
10620 full_args = tsubst_template_args (tmpl_args, args,
10621 complain, in_decl);
10622 --processing_template_decl;
10623 if (full_args == error_mark_node)
10624 RETURN (error_mark_node);
10625
10626 /* If this is a default template template argument,
10627 tsubst might not have changed anything. */
10628 if (full_args == tmpl_args)
10629 RETURN (t);
10630
10631 hash = hash_tmpl_and_args (t, full_args);
10632 spec = retrieve_specialization (t, full_args, hash);
10633 if (spec != NULL_TREE)
10634 {
10635 r = spec;
10636 break;
10637 }
10638
10639 /* Make a new template decl. It will be similar to the
10640 original, but will record the current template arguments.
10641 We also create a new function declaration, which is just
10642 like the old one, but points to this new template, rather
10643 than the old one. */
10644 r = copy_decl (t);
10645 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10646 DECL_CHAIN (r) = NULL_TREE;
10647
10648 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10649
10650 if (TREE_CODE (decl) == TYPE_DECL
10651 && !TYPE_DECL_ALIAS_P (decl))
10652 {
10653 tree new_type;
10654 ++processing_template_decl;
10655 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10656 --processing_template_decl;
10657 if (new_type == error_mark_node)
10658 RETURN (error_mark_node);
10659
10660 TREE_TYPE (r) = new_type;
10661 /* For a partial specialization, we need to keep pointing to
10662 the primary template. */
10663 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10664 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10665 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10666 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10667 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10668 }
10669 else
10670 {
10671 tree new_decl;
10672 ++processing_template_decl;
10673 new_decl = tsubst (decl, args, complain, in_decl);
10674 --processing_template_decl;
10675 if (new_decl == error_mark_node)
10676 RETURN (error_mark_node);
10677
10678 DECL_TEMPLATE_RESULT (r) = new_decl;
10679 DECL_TI_TEMPLATE (new_decl) = r;
10680 TREE_TYPE (r) = TREE_TYPE (new_decl);
10681 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10682 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10683 }
10684
10685 SET_DECL_IMPLICIT_INSTANTIATION (r);
10686 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10687 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10688
10689 /* The template parameters for this new template are all the
10690 template parameters for the old template, except the
10691 outermost level of parameters. */
10692 DECL_TEMPLATE_PARMS (r)
10693 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10694 complain);
10695
10696 if (PRIMARY_TEMPLATE_P (t))
10697 DECL_PRIMARY_TEMPLATE (r) = r;
10698
10699 if (TREE_CODE (decl) != TYPE_DECL && TREE_CODE (decl) != VAR_DECL)
10700 /* Record this non-type partial instantiation. */
10701 register_specialization (r, t,
10702 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10703 false, hash);
10704 }
10705 break;
10706
10707 case FUNCTION_DECL:
10708 {
10709 tree ctx;
10710 tree argvec = NULL_TREE;
10711 tree *friends;
10712 tree gen_tmpl;
10713 tree type;
10714 int member;
10715 int args_depth;
10716 int parms_depth;
10717
10718 /* Nobody should be tsubst'ing into non-template functions. */
10719 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10720
10721 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10722 {
10723 tree spec;
10724 bool dependent_p;
10725
10726 /* If T is not dependent, just return it. We have to
10727 increment PROCESSING_TEMPLATE_DECL because
10728 value_dependent_expression_p assumes that nothing is
10729 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10730 ++processing_template_decl;
10731 dependent_p = value_dependent_expression_p (t);
10732 --processing_template_decl;
10733 if (!dependent_p)
10734 RETURN (t);
10735
10736 /* Calculate the most general template of which R is a
10737 specialization, and the complete set of arguments used to
10738 specialize R. */
10739 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10740 argvec = tsubst_template_args (DECL_TI_ARGS
10741 (DECL_TEMPLATE_RESULT
10742 (DECL_TI_TEMPLATE (t))),
10743 args, complain, in_decl);
10744 if (argvec == error_mark_node)
10745 RETURN (error_mark_node);
10746
10747 /* Check to see if we already have this specialization. */
10748 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10749 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10750
10751 if (spec)
10752 {
10753 r = spec;
10754 break;
10755 }
10756
10757 /* We can see more levels of arguments than parameters if
10758 there was a specialization of a member template, like
10759 this:
10760
10761 template <class T> struct S { template <class U> void f(); }
10762 template <> template <class U> void S<int>::f(U);
10763
10764 Here, we'll be substituting into the specialization,
10765 because that's where we can find the code we actually
10766 want to generate, but we'll have enough arguments for
10767 the most general template.
10768
10769 We also deal with the peculiar case:
10770
10771 template <class T> struct S {
10772 template <class U> friend void f();
10773 };
10774 template <class U> void f() {}
10775 template S<int>;
10776 template void f<double>();
10777
10778 Here, the ARGS for the instantiation of will be {int,
10779 double}. But, we only need as many ARGS as there are
10780 levels of template parameters in CODE_PATTERN. We are
10781 careful not to get fooled into reducing the ARGS in
10782 situations like:
10783
10784 template <class T> struct S { template <class U> void f(U); }
10785 template <class T> template <> void S<T>::f(int) {}
10786
10787 which we can spot because the pattern will be a
10788 specialization in this case. */
10789 args_depth = TMPL_ARGS_DEPTH (args);
10790 parms_depth =
10791 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10792 if (args_depth > parms_depth
10793 && !DECL_TEMPLATE_SPECIALIZATION (t))
10794 args = get_innermost_template_args (args, parms_depth);
10795 }
10796 else
10797 {
10798 /* This special case arises when we have something like this:
10799
10800 template <class T> struct S {
10801 friend void f<int>(int, double);
10802 };
10803
10804 Here, the DECL_TI_TEMPLATE for the friend declaration
10805 will be an IDENTIFIER_NODE. We are being called from
10806 tsubst_friend_function, and we want only to create a
10807 new decl (R) with appropriate types so that we can call
10808 determine_specialization. */
10809 gen_tmpl = NULL_TREE;
10810 }
10811
10812 if (DECL_CLASS_SCOPE_P (t))
10813 {
10814 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10815 member = 2;
10816 else
10817 member = 1;
10818 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10819 complain, t, /*entering_scope=*/1);
10820 }
10821 else
10822 {
10823 member = 0;
10824 ctx = DECL_CONTEXT (t);
10825 }
10826 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10827 if (type == error_mark_node)
10828 RETURN (error_mark_node);
10829
10830 /* If we hit excessive deduction depth, the type is bogus even if
10831 it isn't error_mark_node, so don't build a decl. */
10832 if (excessive_deduction_depth)
10833 RETURN (error_mark_node);
10834
10835 /* We do NOT check for matching decls pushed separately at this
10836 point, as they may not represent instantiations of this
10837 template, and in any case are considered separate under the
10838 discrete model. */
10839 r = copy_decl (t);
10840 DECL_USE_TEMPLATE (r) = 0;
10841 TREE_TYPE (r) = type;
10842 /* Clear out the mangled name and RTL for the instantiation. */
10843 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10844 SET_DECL_RTL (r, NULL);
10845 /* Leave DECL_INITIAL set on deleted instantiations. */
10846 if (!DECL_DELETED_FN (r))
10847 DECL_INITIAL (r) = NULL_TREE;
10848 DECL_CONTEXT (r) = ctx;
10849
10850 /* OpenMP UDRs have the only argument a reference to the declared
10851 type. We want to diagnose if the declared type is a reference,
10852 which is invalid, but as references to references are usually
10853 quietly merged, diagnose it here. */
10854 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10855 {
10856 tree argtype
10857 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10858 argtype = tsubst (argtype, args, complain, in_decl);
10859 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10860 error_at (DECL_SOURCE_LOCATION (t),
10861 "reference type %qT in "
10862 "%<#pragma omp declare reduction%>", argtype);
10863 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10864 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10865 argtype);
10866 }
10867
10868 if (member && DECL_CONV_FN_P (r))
10869 /* Type-conversion operator. Reconstruct the name, in
10870 case it's the name of one of the template's parameters. */
10871 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10872
10873 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10874 complain, t);
10875 DECL_RESULT (r) = NULL_TREE;
10876
10877 TREE_STATIC (r) = 0;
10878 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10879 DECL_EXTERNAL (r) = 1;
10880 /* If this is an instantiation of a function with internal
10881 linkage, we already know what object file linkage will be
10882 assigned to the instantiation. */
10883 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10884 DECL_DEFER_OUTPUT (r) = 0;
10885 DECL_CHAIN (r) = NULL_TREE;
10886 DECL_PENDING_INLINE_INFO (r) = 0;
10887 DECL_PENDING_INLINE_P (r) = 0;
10888 DECL_SAVED_TREE (r) = NULL_TREE;
10889 DECL_STRUCT_FUNCTION (r) = NULL;
10890 TREE_USED (r) = 0;
10891 /* We'll re-clone as appropriate in instantiate_template. */
10892 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10893
10894 /* If we aren't complaining now, return on error before we register
10895 the specialization so that we'll complain eventually. */
10896 if ((complain & tf_error) == 0
10897 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10898 && !grok_op_properties (r, /*complain=*/false))
10899 RETURN (error_mark_node);
10900
10901 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10902 this in the special friend case mentioned above where
10903 GEN_TMPL is NULL. */
10904 if (gen_tmpl)
10905 {
10906 DECL_TEMPLATE_INFO (r)
10907 = build_template_info (gen_tmpl, argvec);
10908 SET_DECL_IMPLICIT_INSTANTIATION (r);
10909
10910 tree new_r
10911 = register_specialization (r, gen_tmpl, argvec, false, hash);
10912 if (new_r != r)
10913 /* We instantiated this while substituting into
10914 the type earlier (template/friend54.C). */
10915 RETURN (new_r);
10916
10917 /* We're not supposed to instantiate default arguments
10918 until they are called, for a template. But, for a
10919 declaration like:
10920
10921 template <class T> void f ()
10922 { extern void g(int i = T()); }
10923
10924 we should do the substitution when the template is
10925 instantiated. We handle the member function case in
10926 instantiate_class_template since the default arguments
10927 might refer to other members of the class. */
10928 if (!member
10929 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10930 && !uses_template_parms (argvec))
10931 tsubst_default_arguments (r, complain);
10932 }
10933 else
10934 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10935
10936 /* Copy the list of befriending classes. */
10937 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10938 *friends;
10939 friends = &TREE_CHAIN (*friends))
10940 {
10941 *friends = copy_node (*friends);
10942 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10943 args, complain,
10944 in_decl);
10945 }
10946
10947 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10948 {
10949 maybe_retrofit_in_chrg (r);
10950 if (DECL_CONSTRUCTOR_P (r))
10951 grok_ctor_properties (ctx, r);
10952 if (DECL_INHERITED_CTOR_BASE (r))
10953 deduce_inheriting_ctor (r);
10954 /* If this is an instantiation of a member template, clone it.
10955 If it isn't, that'll be handled by
10956 clone_constructors_and_destructors. */
10957 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10958 clone_function_decl (r, /*update_method_vec_p=*/0);
10959 }
10960 else if ((complain & tf_error) != 0
10961 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10962 && !grok_op_properties (r, /*complain=*/true))
10963 RETURN (error_mark_node);
10964
10965 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10966 SET_DECL_FRIEND_CONTEXT (r,
10967 tsubst (DECL_FRIEND_CONTEXT (t),
10968 args, complain, in_decl));
10969
10970 /* Possibly limit visibility based on template args. */
10971 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10972 if (DECL_VISIBILITY_SPECIFIED (t))
10973 {
10974 DECL_VISIBILITY_SPECIFIED (r) = 0;
10975 DECL_ATTRIBUTES (r)
10976 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10977 }
10978 determine_visibility (r);
10979 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10980 && !processing_template_decl)
10981 defaulted_late_check (r);
10982
10983 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10984 args, complain, in_decl);
10985 }
10986 break;
10987
10988 case PARM_DECL:
10989 {
10990 tree type = NULL_TREE;
10991 int i, len = 1;
10992 tree expanded_types = NULL_TREE;
10993 tree prev_r = NULL_TREE;
10994 tree first_r = NULL_TREE;
10995
10996 if (DECL_PACK_P (t))
10997 {
10998 /* If there is a local specialization that isn't a
10999 parameter pack, it means that we're doing a "simple"
11000 substitution from inside tsubst_pack_expansion. Just
11001 return the local specialization (which will be a single
11002 parm). */
11003 tree spec = retrieve_local_specialization (t);
11004 if (spec
11005 && TREE_CODE (spec) == PARM_DECL
11006 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11007 RETURN (spec);
11008
11009 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11010 the parameters in this function parameter pack. */
11011 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11012 complain, in_decl);
11013 if (TREE_CODE (expanded_types) == TREE_VEC)
11014 {
11015 len = TREE_VEC_LENGTH (expanded_types);
11016
11017 /* Zero-length parameter packs are boring. Just substitute
11018 into the chain. */
11019 if (len == 0)
11020 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11021 TREE_CHAIN (t)));
11022 }
11023 else
11024 {
11025 /* All we did was update the type. Make a note of that. */
11026 type = expanded_types;
11027 expanded_types = NULL_TREE;
11028 }
11029 }
11030
11031 /* Loop through all of the parameters we'll build. When T is
11032 a function parameter pack, LEN is the number of expanded
11033 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11034 r = NULL_TREE;
11035 for (i = 0; i < len; ++i)
11036 {
11037 prev_r = r;
11038 r = copy_node (t);
11039 if (DECL_TEMPLATE_PARM_P (t))
11040 SET_DECL_TEMPLATE_PARM_P (r);
11041
11042 if (expanded_types)
11043 /* We're on the Ith parameter of the function parameter
11044 pack. */
11045 {
11046 /* Get the Ith type. */
11047 type = TREE_VEC_ELT (expanded_types, i);
11048
11049 /* Rename the parameter to include the index. */
11050 DECL_NAME (r)
11051 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11052 }
11053 else if (!type)
11054 /* We're dealing with a normal parameter. */
11055 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11056
11057 type = type_decays_to (type);
11058 TREE_TYPE (r) = type;
11059 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11060
11061 if (DECL_INITIAL (r))
11062 {
11063 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11064 DECL_INITIAL (r) = TREE_TYPE (r);
11065 else
11066 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11067 complain, in_decl);
11068 }
11069
11070 DECL_CONTEXT (r) = NULL_TREE;
11071
11072 if (!DECL_TEMPLATE_PARM_P (r))
11073 DECL_ARG_TYPE (r) = type_passed_as (type);
11074
11075 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11076 args, complain, in_decl);
11077
11078 /* Keep track of the first new parameter we
11079 generate. That's what will be returned to the
11080 caller. */
11081 if (!first_r)
11082 first_r = r;
11083
11084 /* Build a proper chain of parameters when substituting
11085 into a function parameter pack. */
11086 if (prev_r)
11087 DECL_CHAIN (prev_r) = r;
11088 }
11089
11090 /* If cp_unevaluated_operand is set, we're just looking for a
11091 single dummy parameter, so don't keep going. */
11092 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11093 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11094 complain, DECL_CHAIN (t));
11095
11096 /* FIRST_R contains the start of the chain we've built. */
11097 r = first_r;
11098 }
11099 break;
11100
11101 case FIELD_DECL:
11102 {
11103 tree type = NULL_TREE;
11104 tree vec = NULL_TREE;
11105 tree expanded_types = NULL_TREE;
11106 int len = 1;
11107
11108 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11109 {
11110 /* This field is a lambda capture pack. Return a TREE_VEC of
11111 the expanded fields to instantiate_class_template_1 and
11112 store them in the specializations hash table as a
11113 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11114 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11115 complain, in_decl);
11116 if (TREE_CODE (expanded_types) == TREE_VEC)
11117 {
11118 len = TREE_VEC_LENGTH (expanded_types);
11119 vec = make_tree_vec (len);
11120 }
11121 else
11122 {
11123 /* All we did was update the type. Make a note of that. */
11124 type = expanded_types;
11125 expanded_types = NULL_TREE;
11126 }
11127 }
11128
11129 for (int i = 0; i < len; ++i)
11130 {
11131 r = copy_decl (t);
11132 if (expanded_types)
11133 {
11134 type = TREE_VEC_ELT (expanded_types, i);
11135 DECL_NAME (r)
11136 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11137 }
11138 else if (!type)
11139 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11140
11141 if (type == error_mark_node)
11142 RETURN (error_mark_node);
11143 TREE_TYPE (r) = type;
11144 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11145
11146 if (DECL_C_BIT_FIELD (r))
11147 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11148 non-bit-fields DECL_INITIAL is a non-static data member
11149 initializer, which gets deferred instantiation. */
11150 DECL_INITIAL (r)
11151 = tsubst_expr (DECL_INITIAL (t), args,
11152 complain, in_decl,
11153 /*integral_constant_expression_p=*/true);
11154 else if (DECL_INITIAL (t))
11155 {
11156 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11157 NSDMI in perform_member_init. Still set DECL_INITIAL
11158 so that we know there is one. */
11159 DECL_INITIAL (r) = void_node;
11160 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11161 retrofit_lang_decl (r);
11162 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11163 }
11164 /* We don't have to set DECL_CONTEXT here; it is set by
11165 finish_member_declaration. */
11166 DECL_CHAIN (r) = NULL_TREE;
11167
11168 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11169 args, complain, in_decl);
11170
11171 if (vec)
11172 TREE_VEC_ELT (vec, i) = r;
11173 }
11174
11175 if (vec)
11176 {
11177 r = vec;
11178 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11179 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11180 SET_ARGUMENT_PACK_ARGS (pack, vec);
11181 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11182 TREE_TYPE (pack) = tpack;
11183 register_specialization (pack, t, args, false, 0);
11184 }
11185 }
11186 break;
11187
11188 case USING_DECL:
11189 /* We reach here only for member using decls. We also need to check
11190 uses_template_parms because DECL_DEPENDENT_P is not set for a
11191 using-declaration that designates a member of the current
11192 instantiation (c++/53549). */
11193 if (DECL_DEPENDENT_P (t)
11194 || uses_template_parms (USING_DECL_SCOPE (t)))
11195 {
11196 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11197 complain, in_decl);
11198 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11199 r = do_class_using_decl (inst_scope, name);
11200 if (!r)
11201 r = error_mark_node;
11202 else
11203 {
11204 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11205 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11206 }
11207 }
11208 else
11209 {
11210 r = copy_node (t);
11211 DECL_CHAIN (r) = NULL_TREE;
11212 }
11213 break;
11214
11215 case TYPE_DECL:
11216 case VAR_DECL:
11217 {
11218 tree argvec = NULL_TREE;
11219 tree gen_tmpl = NULL_TREE;
11220 tree spec;
11221 tree tmpl = NULL_TREE;
11222 tree ctx;
11223 tree type = NULL_TREE;
11224 bool local_p;
11225
11226 if (TREE_TYPE (t) == error_mark_node)
11227 RETURN (error_mark_node);
11228
11229 if (TREE_CODE (t) == TYPE_DECL
11230 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11231 {
11232 /* If this is the canonical decl, we don't have to
11233 mess with instantiations, and often we can't (for
11234 typename, template type parms and such). Note that
11235 TYPE_NAME is not correct for the above test if
11236 we've copied the type for a typedef. */
11237 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11238 if (type == error_mark_node)
11239 RETURN (error_mark_node);
11240 r = TYPE_NAME (type);
11241 break;
11242 }
11243
11244 /* Check to see if we already have the specialization we
11245 need. */
11246 spec = NULL_TREE;
11247 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11248 {
11249 /* T is a static data member or namespace-scope entity.
11250 We have to substitute into namespace-scope variables
11251 (even though such entities are never templates) because
11252 of cases like:
11253
11254 template <class T> void f() { extern T t; }
11255
11256 where the entity referenced is not known until
11257 instantiation time. */
11258 local_p = false;
11259 ctx = DECL_CONTEXT (t);
11260 if (DECL_CLASS_SCOPE_P (t))
11261 {
11262 ctx = tsubst_aggr_type (ctx, args,
11263 complain,
11264 in_decl, /*entering_scope=*/1);
11265 /* If CTX is unchanged, then T is in fact the
11266 specialization we want. That situation occurs when
11267 referencing a static data member within in its own
11268 class. We can use pointer equality, rather than
11269 same_type_p, because DECL_CONTEXT is always
11270 canonical... */
11271 if (ctx == DECL_CONTEXT (t)
11272 /* ... unless T is a member template; in which
11273 case our caller can be willing to create a
11274 specialization of that template represented
11275 by T. */
11276 && !(DECL_TI_TEMPLATE (t)
11277 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11278 spec = t;
11279 }
11280
11281 if (!spec)
11282 {
11283 tmpl = DECL_TI_TEMPLATE (t);
11284 gen_tmpl = most_general_template (tmpl);
11285 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11286 if (argvec != error_mark_node)
11287 argvec = (coerce_innermost_template_parms
11288 (DECL_TEMPLATE_PARMS (gen_tmpl),
11289 argvec, t, complain,
11290 /*all*/true, /*defarg*/true));
11291 if (argvec == error_mark_node)
11292 RETURN (error_mark_node);
11293 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11294 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11295 }
11296 }
11297 else
11298 {
11299 /* A local variable. */
11300 local_p = true;
11301 /* Subsequent calls to pushdecl will fill this in. */
11302 ctx = NULL_TREE;
11303 spec = retrieve_local_specialization (t);
11304 }
11305 /* If we already have the specialization we need, there is
11306 nothing more to do. */
11307 if (spec)
11308 {
11309 r = spec;
11310 break;
11311 }
11312
11313 /* Create a new node for the specialization we need. */
11314 r = copy_decl (t);
11315 if (type == NULL_TREE)
11316 {
11317 if (is_typedef_decl (t))
11318 type = DECL_ORIGINAL_TYPE (t);
11319 else
11320 type = TREE_TYPE (t);
11321 if (VAR_P (t)
11322 && VAR_HAD_UNKNOWN_BOUND (t)
11323 && type != error_mark_node)
11324 type = strip_array_domain (type);
11325 type = tsubst (type, args, complain, in_decl);
11326 }
11327 if (VAR_P (r))
11328 {
11329 /* Even if the original location is out of scope, the
11330 newly substituted one is not. */
11331 DECL_DEAD_FOR_LOCAL (r) = 0;
11332 DECL_INITIALIZED_P (r) = 0;
11333 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11334 if (type == error_mark_node)
11335 RETURN (error_mark_node);
11336 if (TREE_CODE (type) == FUNCTION_TYPE)
11337 {
11338 /* It may seem that this case cannot occur, since:
11339
11340 typedef void f();
11341 void g() { f x; }
11342
11343 declares a function, not a variable. However:
11344
11345 typedef void f();
11346 template <typename T> void g() { T t; }
11347 template void g<f>();
11348
11349 is an attempt to declare a variable with function
11350 type. */
11351 error ("variable %qD has function type",
11352 /* R is not yet sufficiently initialized, so we
11353 just use its name. */
11354 DECL_NAME (r));
11355 RETURN (error_mark_node);
11356 }
11357 type = complete_type (type);
11358 /* Wait until cp_finish_decl to set this again, to handle
11359 circular dependency (template/instantiate6.C). */
11360 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11361 type = check_var_type (DECL_NAME (r), type);
11362
11363 if (DECL_HAS_VALUE_EXPR_P (t))
11364 {
11365 tree ve = DECL_VALUE_EXPR (t);
11366 ve = tsubst_expr (ve, args, complain, in_decl,
11367 /*constant_expression_p=*/false);
11368 if (REFERENCE_REF_P (ve))
11369 {
11370 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11371 ve = TREE_OPERAND (ve, 0);
11372 }
11373 SET_DECL_VALUE_EXPR (r, ve);
11374 }
11375 if (TREE_STATIC (r) || DECL_EXTERNAL (r))
11376 set_decl_tls_model (r, decl_tls_model (t));
11377 }
11378 else if (DECL_SELF_REFERENCE_P (t))
11379 SET_DECL_SELF_REFERENCE_P (r);
11380 TREE_TYPE (r) = type;
11381 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11382 DECL_CONTEXT (r) = ctx;
11383 /* Clear out the mangled name and RTL for the instantiation. */
11384 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11385 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11386 SET_DECL_RTL (r, NULL);
11387 /* The initializer must not be expanded until it is required;
11388 see [temp.inst]. */
11389 DECL_INITIAL (r) = NULL_TREE;
11390 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11391 SET_DECL_RTL (r, NULL);
11392 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11393 if (VAR_P (r))
11394 {
11395 /* Possibly limit visibility based on template args. */
11396 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11397 if (DECL_VISIBILITY_SPECIFIED (t))
11398 {
11399 DECL_VISIBILITY_SPECIFIED (r) = 0;
11400 DECL_ATTRIBUTES (r)
11401 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11402 }
11403 determine_visibility (r);
11404 }
11405
11406 if (!local_p)
11407 {
11408 /* A static data member declaration is always marked
11409 external when it is declared in-class, even if an
11410 initializer is present. We mimic the non-template
11411 processing here. */
11412 DECL_EXTERNAL (r) = 1;
11413
11414 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11415 SET_DECL_IMPLICIT_INSTANTIATION (r);
11416 register_specialization (r, gen_tmpl, argvec, false, hash);
11417 }
11418 else if (!cp_unevaluated_operand)
11419 register_local_specialization (r, t);
11420
11421 DECL_CHAIN (r) = NULL_TREE;
11422
11423 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11424 /*flags=*/0,
11425 args, complain, in_decl);
11426
11427 /* Preserve a typedef that names a type. */
11428 if (is_typedef_decl (r))
11429 {
11430 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11431 set_underlying_type (r);
11432 }
11433
11434 layout_decl (r, 0);
11435 }
11436 break;
11437
11438 default:
11439 gcc_unreachable ();
11440 }
11441 #undef RETURN
11442
11443 out:
11444 /* Restore the file and line information. */
11445 input_location = saved_loc;
11446
11447 return r;
11448 }
11449
11450 /* Substitute into the ARG_TYPES of a function type.
11451 If END is a TREE_CHAIN, leave it and any following types
11452 un-substituted. */
11453
11454 static tree
11455 tsubst_arg_types (tree arg_types,
11456 tree args,
11457 tree end,
11458 tsubst_flags_t complain,
11459 tree in_decl)
11460 {
11461 tree remaining_arg_types;
11462 tree type = NULL_TREE;
11463 int i = 1;
11464 tree expanded_args = NULL_TREE;
11465 tree default_arg;
11466
11467 if (!arg_types || arg_types == void_list_node || arg_types == end)
11468 return arg_types;
11469
11470 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11471 args, end, complain, in_decl);
11472 if (remaining_arg_types == error_mark_node)
11473 return error_mark_node;
11474
11475 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11476 {
11477 /* For a pack expansion, perform substitution on the
11478 entire expression. Later on, we'll handle the arguments
11479 one-by-one. */
11480 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11481 args, complain, in_decl);
11482
11483 if (TREE_CODE (expanded_args) == TREE_VEC)
11484 /* So that we'll spin through the parameters, one by one. */
11485 i = TREE_VEC_LENGTH (expanded_args);
11486 else
11487 {
11488 /* We only partially substituted into the parameter
11489 pack. Our type is TYPE_PACK_EXPANSION. */
11490 type = expanded_args;
11491 expanded_args = NULL_TREE;
11492 }
11493 }
11494
11495 while (i > 0) {
11496 --i;
11497
11498 if (expanded_args)
11499 type = TREE_VEC_ELT (expanded_args, i);
11500 else if (!type)
11501 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11502
11503 if (type == error_mark_node)
11504 return error_mark_node;
11505 if (VOID_TYPE_P (type))
11506 {
11507 if (complain & tf_error)
11508 {
11509 error ("invalid parameter type %qT", type);
11510 if (in_decl)
11511 error ("in declaration %q+D", in_decl);
11512 }
11513 return error_mark_node;
11514 }
11515 /* DR 657. */
11516 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11517 return error_mark_node;
11518
11519 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11520 top-level qualifiers as required. */
11521 type = cv_unqualified (type_decays_to (type));
11522
11523 /* We do not substitute into default arguments here. The standard
11524 mandates that they be instantiated only when needed, which is
11525 done in build_over_call. */
11526 default_arg = TREE_PURPOSE (arg_types);
11527
11528 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11529 {
11530 /* We've instantiated a template before its default arguments
11531 have been parsed. This can happen for a nested template
11532 class, and is not an error unless we require the default
11533 argument in a call of this function. */
11534 remaining_arg_types =
11535 tree_cons (default_arg, type, remaining_arg_types);
11536 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11537 }
11538 else
11539 remaining_arg_types =
11540 hash_tree_cons (default_arg, type, remaining_arg_types);
11541 }
11542
11543 return remaining_arg_types;
11544 }
11545
11546 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11547 *not* handle the exception-specification for FNTYPE, because the
11548 initial substitution of explicitly provided template parameters
11549 during argument deduction forbids substitution into the
11550 exception-specification:
11551
11552 [temp.deduct]
11553
11554 All references in the function type of the function template to the
11555 corresponding template parameters are replaced by the specified tem-
11556 plate argument values. If a substitution in a template parameter or
11557 in the function type of the function template results in an invalid
11558 type, type deduction fails. [Note: The equivalent substitution in
11559 exception specifications is done only when the function is instanti-
11560 ated, at which point a program is ill-formed if the substitution
11561 results in an invalid type.] */
11562
11563 static tree
11564 tsubst_function_type (tree t,
11565 tree args,
11566 tsubst_flags_t complain,
11567 tree in_decl)
11568 {
11569 tree return_type;
11570 tree arg_types = NULL_TREE;
11571 tree fntype;
11572
11573 /* The TYPE_CONTEXT is not used for function/method types. */
11574 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11575
11576 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11577 failure. */
11578 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
11579
11580 if (late_return_type_p)
11581 {
11582 /* Substitute the argument types. */
11583 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11584 complain, in_decl);
11585 if (arg_types == error_mark_node)
11586 return error_mark_node;
11587
11588 tree save_ccp = current_class_ptr;
11589 tree save_ccr = current_class_ref;
11590 tree this_type = (TREE_CODE (t) == METHOD_TYPE
11591 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
11592 bool do_inject = this_type && CLASS_TYPE_P (this_type);
11593 if (do_inject)
11594 {
11595 /* DR 1207: 'this' is in scope in the trailing return type. */
11596 inject_this_parameter (this_type, cp_type_quals (this_type));
11597 }
11598
11599 /* Substitute the return type. */
11600 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11601
11602 if (do_inject)
11603 {
11604 current_class_ptr = save_ccp;
11605 current_class_ref = save_ccr;
11606 }
11607 }
11608 else
11609 /* Substitute the return type. */
11610 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11611
11612 if (return_type == error_mark_node)
11613 return error_mark_node;
11614 /* DR 486 clarifies that creation of a function type with an
11615 invalid return type is a deduction failure. */
11616 if (TREE_CODE (return_type) == ARRAY_TYPE
11617 || TREE_CODE (return_type) == FUNCTION_TYPE)
11618 {
11619 if (complain & tf_error)
11620 {
11621 if (TREE_CODE (return_type) == ARRAY_TYPE)
11622 error ("function returning an array");
11623 else
11624 error ("function returning a function");
11625 }
11626 return error_mark_node;
11627 }
11628 /* And DR 657. */
11629 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11630 return error_mark_node;
11631
11632 if (!late_return_type_p)
11633 {
11634 /* Substitute the argument types. */
11635 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11636 complain, in_decl);
11637 if (arg_types == error_mark_node)
11638 return error_mark_node;
11639 }
11640
11641 /* Construct a new type node and return it. */
11642 if (TREE_CODE (t) == FUNCTION_TYPE)
11643 {
11644 fntype = build_function_type (return_type, arg_types);
11645 fntype = apply_memfn_quals (fntype,
11646 type_memfn_quals (t),
11647 type_memfn_rqual (t));
11648 }
11649 else
11650 {
11651 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11652 /* Don't pick up extra function qualifiers from the basetype. */
11653 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11654 if (! MAYBE_CLASS_TYPE_P (r))
11655 {
11656 /* [temp.deduct]
11657
11658 Type deduction may fail for any of the following
11659 reasons:
11660
11661 -- Attempting to create "pointer to member of T" when T
11662 is not a class type. */
11663 if (complain & tf_error)
11664 error ("creating pointer to member function of non-class type %qT",
11665 r);
11666 return error_mark_node;
11667 }
11668
11669 fntype = build_method_type_directly (r, return_type,
11670 TREE_CHAIN (arg_types));
11671 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11672 }
11673 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11674
11675 if (late_return_type_p)
11676 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
11677
11678 return fntype;
11679 }
11680
11681 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11682 ARGS into that specification, and return the substituted
11683 specification. If there is no specification, return NULL_TREE. */
11684
11685 static tree
11686 tsubst_exception_specification (tree fntype,
11687 tree args,
11688 tsubst_flags_t complain,
11689 tree in_decl,
11690 bool defer_ok)
11691 {
11692 tree specs;
11693 tree new_specs;
11694
11695 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11696 new_specs = NULL_TREE;
11697 if (specs && TREE_PURPOSE (specs))
11698 {
11699 /* A noexcept-specifier. */
11700 tree expr = TREE_PURPOSE (specs);
11701 if (TREE_CODE (expr) == INTEGER_CST)
11702 new_specs = expr;
11703 else if (defer_ok)
11704 {
11705 /* Defer instantiation of noexcept-specifiers to avoid
11706 excessive instantiations (c++/49107). */
11707 new_specs = make_node (DEFERRED_NOEXCEPT);
11708 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11709 {
11710 /* We already partially instantiated this member template,
11711 so combine the new args with the old. */
11712 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11713 = DEFERRED_NOEXCEPT_PATTERN (expr);
11714 DEFERRED_NOEXCEPT_ARGS (new_specs)
11715 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11716 }
11717 else
11718 {
11719 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11720 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11721 }
11722 }
11723 else
11724 new_specs = tsubst_copy_and_build
11725 (expr, args, complain, in_decl, /*function_p=*/false,
11726 /*integral_constant_expression_p=*/true);
11727 new_specs = build_noexcept_spec (new_specs, complain);
11728 }
11729 else if (specs)
11730 {
11731 if (! TREE_VALUE (specs))
11732 new_specs = specs;
11733 else
11734 while (specs)
11735 {
11736 tree spec;
11737 int i, len = 1;
11738 tree expanded_specs = NULL_TREE;
11739
11740 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11741 {
11742 /* Expand the pack expansion type. */
11743 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11744 args, complain,
11745 in_decl);
11746
11747 if (expanded_specs == error_mark_node)
11748 return error_mark_node;
11749 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11750 len = TREE_VEC_LENGTH (expanded_specs);
11751 else
11752 {
11753 /* We're substituting into a member template, so
11754 we got a TYPE_PACK_EXPANSION back. Add that
11755 expansion and move on. */
11756 gcc_assert (TREE_CODE (expanded_specs)
11757 == TYPE_PACK_EXPANSION);
11758 new_specs = add_exception_specifier (new_specs,
11759 expanded_specs,
11760 complain);
11761 specs = TREE_CHAIN (specs);
11762 continue;
11763 }
11764 }
11765
11766 for (i = 0; i < len; ++i)
11767 {
11768 if (expanded_specs)
11769 spec = TREE_VEC_ELT (expanded_specs, i);
11770 else
11771 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11772 if (spec == error_mark_node)
11773 return spec;
11774 new_specs = add_exception_specifier (new_specs, spec,
11775 complain);
11776 }
11777
11778 specs = TREE_CHAIN (specs);
11779 }
11780 }
11781 return new_specs;
11782 }
11783
11784 /* Take the tree structure T and replace template parameters used
11785 therein with the argument vector ARGS. IN_DECL is an associated
11786 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11787 Issue error and warning messages under control of COMPLAIN. Note
11788 that we must be relatively non-tolerant of extensions here, in
11789 order to preserve conformance; if we allow substitutions that
11790 should not be allowed, we may allow argument deductions that should
11791 not succeed, and therefore report ambiguous overload situations
11792 where there are none. In theory, we could allow the substitution,
11793 but indicate that it should have failed, and allow our caller to
11794 make sure that the right thing happens, but we don't try to do this
11795 yet.
11796
11797 This function is used for dealing with types, decls and the like;
11798 for expressions, use tsubst_expr or tsubst_copy. */
11799
11800 tree
11801 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11802 {
11803 enum tree_code code;
11804 tree type, r = NULL_TREE;
11805
11806 if (t == NULL_TREE || t == error_mark_node
11807 || t == integer_type_node
11808 || t == void_type_node
11809 || t == char_type_node
11810 || t == unknown_type_node
11811 || TREE_CODE (t) == NAMESPACE_DECL
11812 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11813 return t;
11814
11815 if (DECL_P (t))
11816 return tsubst_decl (t, args, complain);
11817
11818 if (args == NULL_TREE)
11819 return t;
11820
11821 code = TREE_CODE (t);
11822
11823 if (code == IDENTIFIER_NODE)
11824 type = IDENTIFIER_TYPE_VALUE (t);
11825 else
11826 type = TREE_TYPE (t);
11827
11828 gcc_assert (type != unknown_type_node);
11829
11830 /* Reuse typedefs. We need to do this to handle dependent attributes,
11831 such as attribute aligned. */
11832 if (TYPE_P (t)
11833 && typedef_variant_p (t))
11834 {
11835 tree decl = TYPE_NAME (t);
11836
11837 if (alias_template_specialization_p (t))
11838 {
11839 /* DECL represents an alias template and we want to
11840 instantiate it. */
11841 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11842 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11843 r = instantiate_alias_template (tmpl, gen_args, complain);
11844 }
11845 else if (DECL_CLASS_SCOPE_P (decl)
11846 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11847 && uses_template_parms (DECL_CONTEXT (decl)))
11848 {
11849 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11850 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11851 r = retrieve_specialization (tmpl, gen_args, 0);
11852 }
11853 else if (DECL_FUNCTION_SCOPE_P (decl)
11854 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11855 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11856 r = retrieve_local_specialization (decl);
11857 else
11858 /* The typedef is from a non-template context. */
11859 return t;
11860
11861 if (r)
11862 {
11863 r = TREE_TYPE (r);
11864 r = cp_build_qualified_type_real
11865 (r, cp_type_quals (t) | cp_type_quals (r),
11866 complain | tf_ignore_bad_quals);
11867 return r;
11868 }
11869 else
11870 {
11871 /* We don't have an instantiation yet, so drop the typedef. */
11872 int quals = cp_type_quals (t);
11873 t = DECL_ORIGINAL_TYPE (decl);
11874 t = cp_build_qualified_type_real (t, quals,
11875 complain | tf_ignore_bad_quals);
11876 }
11877 }
11878
11879 if (type
11880 && code != TYPENAME_TYPE
11881 && code != TEMPLATE_TYPE_PARM
11882 && code != IDENTIFIER_NODE
11883 && code != FUNCTION_TYPE
11884 && code != METHOD_TYPE)
11885 type = tsubst (type, args, complain, in_decl);
11886 if (type == error_mark_node)
11887 return error_mark_node;
11888
11889 switch (code)
11890 {
11891 case RECORD_TYPE:
11892 case UNION_TYPE:
11893 case ENUMERAL_TYPE:
11894 return tsubst_aggr_type (t, args, complain, in_decl,
11895 /*entering_scope=*/0);
11896
11897 case ERROR_MARK:
11898 case IDENTIFIER_NODE:
11899 case VOID_TYPE:
11900 case REAL_TYPE:
11901 case COMPLEX_TYPE:
11902 case VECTOR_TYPE:
11903 case BOOLEAN_TYPE:
11904 case NULLPTR_TYPE:
11905 case LANG_TYPE:
11906 return t;
11907
11908 case INTEGER_TYPE:
11909 if (t == integer_type_node)
11910 return t;
11911
11912 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11913 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11914 return t;
11915
11916 {
11917 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11918
11919 max = tsubst_expr (omax, args, complain, in_decl,
11920 /*integral_constant_expression_p=*/false);
11921
11922 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11923 needed. */
11924 if (TREE_CODE (max) == NOP_EXPR
11925 && TREE_SIDE_EFFECTS (omax)
11926 && !TREE_TYPE (max))
11927 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11928
11929 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11930 with TREE_SIDE_EFFECTS that indicates this is not an integral
11931 constant expression. */
11932 if (processing_template_decl
11933 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11934 {
11935 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11936 TREE_SIDE_EFFECTS (max) = 1;
11937 }
11938
11939 return compute_array_index_type (NULL_TREE, max, complain);
11940 }
11941
11942 case TEMPLATE_TYPE_PARM:
11943 case TEMPLATE_TEMPLATE_PARM:
11944 case BOUND_TEMPLATE_TEMPLATE_PARM:
11945 case TEMPLATE_PARM_INDEX:
11946 {
11947 int idx;
11948 int level;
11949 int levels;
11950 tree arg = NULL_TREE;
11951
11952 r = NULL_TREE;
11953
11954 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11955 template_parm_level_and_index (t, &level, &idx);
11956
11957 levels = TMPL_ARGS_DEPTH (args);
11958 if (level <= levels)
11959 {
11960 arg = TMPL_ARG (args, level, idx);
11961
11962 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11963 {
11964 /* See through ARGUMENT_PACK_SELECT arguments. */
11965 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11966 /* If the selected argument is an expansion E, that most
11967 likely means we were called from
11968 gen_elem_of_pack_expansion_instantiation during the
11969 substituting of pack an argument pack (which Ith
11970 element is a pack expansion, where I is
11971 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11972 In this case, the Ith element resulting from this
11973 substituting is going to be a pack expansion, which
11974 pattern is the pattern of E. Let's return the
11975 pattern of E, and
11976 gen_elem_of_pack_expansion_instantiation will
11977 build the resulting pack expansion from it. */
11978 if (PACK_EXPANSION_P (arg))
11979 {
11980 /* Make sure we aren't throwing away arg info. */
11981 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
11982 arg = PACK_EXPANSION_PATTERN (arg);
11983 }
11984 }
11985 }
11986
11987 if (arg == error_mark_node)
11988 return error_mark_node;
11989 else if (arg != NULL_TREE)
11990 {
11991 if (ARGUMENT_PACK_P (arg))
11992 /* If ARG is an argument pack, we don't actually want to
11993 perform a substitution here, because substitutions
11994 for argument packs are only done
11995 element-by-element. We can get to this point when
11996 substituting the type of a non-type template
11997 parameter pack, when that type actually contains
11998 template parameter packs from an outer template, e.g.,
11999
12000 template<typename... Types> struct A {
12001 template<Types... Values> struct B { };
12002 }; */
12003 return t;
12004
12005 if (code == TEMPLATE_TYPE_PARM)
12006 {
12007 int quals;
12008 gcc_assert (TYPE_P (arg));
12009
12010 quals = cp_type_quals (arg) | cp_type_quals (t);
12011
12012 return cp_build_qualified_type_real
12013 (arg, quals, complain | tf_ignore_bad_quals);
12014 }
12015 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12016 {
12017 /* We are processing a type constructed from a
12018 template template parameter. */
12019 tree argvec = tsubst (TYPE_TI_ARGS (t),
12020 args, complain, in_decl);
12021 if (argvec == error_mark_node)
12022 return error_mark_node;
12023
12024 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12025 || TREE_CODE (arg) == TEMPLATE_DECL
12026 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12027
12028 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12029 /* Consider this code:
12030
12031 template <template <class> class Template>
12032 struct Internal {
12033 template <class Arg> using Bind = Template<Arg>;
12034 };
12035
12036 template <template <class> class Template, class Arg>
12037 using Instantiate = Template<Arg>; //#0
12038
12039 template <template <class> class Template,
12040 class Argument>
12041 using Bind =
12042 Instantiate<Internal<Template>::template Bind,
12043 Argument>; //#1
12044
12045 When #1 is parsed, the
12046 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12047 parameter `Template' in #0 matches the
12048 UNBOUND_CLASS_TEMPLATE representing the argument
12049 `Internal<Template>::template Bind'; We then want
12050 to assemble the type `Bind<Argument>' that can't
12051 be fully created right now, because
12052 `Internal<Template>' not being complete, the Bind
12053 template cannot be looked up in that context. So
12054 we need to "store" `Bind<Argument>' for later
12055 when the context of Bind becomes complete. Let's
12056 store that in a TYPENAME_TYPE. */
12057 return make_typename_type (TYPE_CONTEXT (arg),
12058 build_nt (TEMPLATE_ID_EXPR,
12059 TYPE_IDENTIFIER (arg),
12060 argvec),
12061 typename_type,
12062 complain);
12063
12064 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12065 are resolving nested-types in the signature of a
12066 member function templates. Otherwise ARG is a
12067 TEMPLATE_DECL and is the real template to be
12068 instantiated. */
12069 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12070 arg = TYPE_NAME (arg);
12071
12072 r = lookup_template_class (arg,
12073 argvec, in_decl,
12074 DECL_CONTEXT (arg),
12075 /*entering_scope=*/0,
12076 complain);
12077 return cp_build_qualified_type_real
12078 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12079 }
12080 else
12081 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12082 return convert_from_reference (unshare_expr (arg));
12083 }
12084
12085 if (level == 1)
12086 /* This can happen during the attempted tsubst'ing in
12087 unify. This means that we don't yet have any information
12088 about the template parameter in question. */
12089 return t;
12090
12091 /* Early in template argument deduction substitution, we don't
12092 want to reduce the level of 'auto', or it will be confused
12093 with a normal template parm in subsequent deduction. */
12094 if (is_auto (t) && (complain & tf_partial))
12095 return t;
12096
12097 /* If we get here, we must have been looking at a parm for a
12098 more deeply nested template. Make a new version of this
12099 template parameter, but with a lower level. */
12100 switch (code)
12101 {
12102 case TEMPLATE_TYPE_PARM:
12103 case TEMPLATE_TEMPLATE_PARM:
12104 case BOUND_TEMPLATE_TEMPLATE_PARM:
12105 if (cp_type_quals (t))
12106 {
12107 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12108 r = cp_build_qualified_type_real
12109 (r, cp_type_quals (t),
12110 complain | (code == TEMPLATE_TYPE_PARM
12111 ? tf_ignore_bad_quals : 0));
12112 }
12113 else
12114 {
12115 r = copy_type (t);
12116 TEMPLATE_TYPE_PARM_INDEX (r)
12117 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12118 r, levels, args, complain);
12119 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12120 TYPE_MAIN_VARIANT (r) = r;
12121 TYPE_POINTER_TO (r) = NULL_TREE;
12122 TYPE_REFERENCE_TO (r) = NULL_TREE;
12123
12124 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12125 /* We have reduced the level of the template
12126 template parameter, but not the levels of its
12127 template parameters, so canonical_type_parameter
12128 will not be able to find the canonical template
12129 template parameter for this level. Thus, we
12130 require structural equality checking to compare
12131 TEMPLATE_TEMPLATE_PARMs. */
12132 SET_TYPE_STRUCTURAL_EQUALITY (r);
12133 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12134 SET_TYPE_STRUCTURAL_EQUALITY (r);
12135 else
12136 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12137
12138 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12139 {
12140 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12141 complain, in_decl);
12142 if (argvec == error_mark_node)
12143 return error_mark_node;
12144
12145 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12146 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12147 }
12148 }
12149 break;
12150
12151 case TEMPLATE_PARM_INDEX:
12152 r = reduce_template_parm_level (t, type, levels, args, complain);
12153 break;
12154
12155 default:
12156 gcc_unreachable ();
12157 }
12158
12159 return r;
12160 }
12161
12162 case TREE_LIST:
12163 {
12164 tree purpose, value, chain;
12165
12166 if (t == void_list_node)
12167 return t;
12168
12169 purpose = TREE_PURPOSE (t);
12170 if (purpose)
12171 {
12172 purpose = tsubst (purpose, args, complain, in_decl);
12173 if (purpose == error_mark_node)
12174 return error_mark_node;
12175 }
12176 value = TREE_VALUE (t);
12177 if (value)
12178 {
12179 value = tsubst (value, args, complain, in_decl);
12180 if (value == error_mark_node)
12181 return error_mark_node;
12182 }
12183 chain = TREE_CHAIN (t);
12184 if (chain && chain != void_type_node)
12185 {
12186 chain = tsubst (chain, args, complain, in_decl);
12187 if (chain == error_mark_node)
12188 return error_mark_node;
12189 }
12190 if (purpose == TREE_PURPOSE (t)
12191 && value == TREE_VALUE (t)
12192 && chain == TREE_CHAIN (t))
12193 return t;
12194 return hash_tree_cons (purpose, value, chain);
12195 }
12196
12197 case TREE_BINFO:
12198 /* We should never be tsubsting a binfo. */
12199 gcc_unreachable ();
12200
12201 case TREE_VEC:
12202 /* A vector of template arguments. */
12203 gcc_assert (!type);
12204 return tsubst_template_args (t, args, complain, in_decl);
12205
12206 case POINTER_TYPE:
12207 case REFERENCE_TYPE:
12208 {
12209 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12210 return t;
12211
12212 /* [temp.deduct]
12213
12214 Type deduction may fail for any of the following
12215 reasons:
12216
12217 -- Attempting to create a pointer to reference type.
12218 -- Attempting to create a reference to a reference type or
12219 a reference to void.
12220
12221 Core issue 106 says that creating a reference to a reference
12222 during instantiation is no longer a cause for failure. We
12223 only enforce this check in strict C++98 mode. */
12224 if ((TREE_CODE (type) == REFERENCE_TYPE
12225 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12226 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12227 {
12228 static location_t last_loc;
12229
12230 /* We keep track of the last time we issued this error
12231 message to avoid spewing a ton of messages during a
12232 single bad template instantiation. */
12233 if (complain & tf_error
12234 && last_loc != input_location)
12235 {
12236 if (VOID_TYPE_P (type))
12237 error ("forming reference to void");
12238 else if (code == POINTER_TYPE)
12239 error ("forming pointer to reference type %qT", type);
12240 else
12241 error ("forming reference to reference type %qT", type);
12242 last_loc = input_location;
12243 }
12244
12245 return error_mark_node;
12246 }
12247 else if (TREE_CODE (type) == FUNCTION_TYPE
12248 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12249 || type_memfn_rqual (type) != REF_QUAL_NONE))
12250 {
12251 if (complain & tf_error)
12252 {
12253 if (code == POINTER_TYPE)
12254 error ("forming pointer to qualified function type %qT",
12255 type);
12256 else
12257 error ("forming reference to qualified function type %qT",
12258 type);
12259 }
12260 return error_mark_node;
12261 }
12262 else if (code == POINTER_TYPE)
12263 {
12264 r = build_pointer_type (type);
12265 if (TREE_CODE (type) == METHOD_TYPE)
12266 r = build_ptrmemfunc_type (r);
12267 }
12268 else if (TREE_CODE (type) == REFERENCE_TYPE)
12269 /* In C++0x, during template argument substitution, when there is an
12270 attempt to create a reference to a reference type, reference
12271 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12272
12273 "If a template-argument for a template-parameter T names a type
12274 that is a reference to a type A, an attempt to create the type
12275 'lvalue reference to cv T' creates the type 'lvalue reference to
12276 A,' while an attempt to create the type type rvalue reference to
12277 cv T' creates the type T"
12278 */
12279 r = cp_build_reference_type
12280 (TREE_TYPE (type),
12281 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12282 else
12283 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12284 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12285
12286 if (r != error_mark_node)
12287 /* Will this ever be needed for TYPE_..._TO values? */
12288 layout_type (r);
12289
12290 return r;
12291 }
12292 case OFFSET_TYPE:
12293 {
12294 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12295 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12296 {
12297 /* [temp.deduct]
12298
12299 Type deduction may fail for any of the following
12300 reasons:
12301
12302 -- Attempting to create "pointer to member of T" when T
12303 is not a class type. */
12304 if (complain & tf_error)
12305 error ("creating pointer to member of non-class type %qT", r);
12306 return error_mark_node;
12307 }
12308 if (TREE_CODE (type) == REFERENCE_TYPE)
12309 {
12310 if (complain & tf_error)
12311 error ("creating pointer to member reference type %qT", type);
12312 return error_mark_node;
12313 }
12314 if (VOID_TYPE_P (type))
12315 {
12316 if (complain & tf_error)
12317 error ("creating pointer to member of type void");
12318 return error_mark_node;
12319 }
12320 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12321 if (TREE_CODE (type) == FUNCTION_TYPE)
12322 {
12323 /* The type of the implicit object parameter gets its
12324 cv-qualifiers from the FUNCTION_TYPE. */
12325 tree memptr;
12326 tree method_type
12327 = build_memfn_type (type, r, type_memfn_quals (type),
12328 type_memfn_rqual (type));
12329 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12330 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12331 complain);
12332 }
12333 else
12334 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12335 cp_type_quals (t),
12336 complain);
12337 }
12338 case FUNCTION_TYPE:
12339 case METHOD_TYPE:
12340 {
12341 tree fntype;
12342 tree specs;
12343 fntype = tsubst_function_type (t, args, complain, in_decl);
12344 if (fntype == error_mark_node)
12345 return error_mark_node;
12346
12347 /* Substitute the exception specification. */
12348 specs = tsubst_exception_specification (t, args, complain,
12349 in_decl, /*defer_ok*/true);
12350 if (specs == error_mark_node)
12351 return error_mark_node;
12352 if (specs)
12353 fntype = build_exception_variant (fntype, specs);
12354 return fntype;
12355 }
12356 case ARRAY_TYPE:
12357 {
12358 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12359 if (domain == error_mark_node)
12360 return error_mark_node;
12361
12362 /* As an optimization, we avoid regenerating the array type if
12363 it will obviously be the same as T. */
12364 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12365 return t;
12366
12367 /* These checks should match the ones in create_array_type_for_decl.
12368
12369 [temp.deduct]
12370
12371 The deduction may fail for any of the following reasons:
12372
12373 -- Attempting to create an array with an element type that
12374 is void, a function type, or a reference type, or [DR337]
12375 an abstract class type. */
12376 if (VOID_TYPE_P (type)
12377 || TREE_CODE (type) == FUNCTION_TYPE
12378 || (TREE_CODE (type) == ARRAY_TYPE
12379 && TYPE_DOMAIN (type) == NULL_TREE)
12380 || TREE_CODE (type) == REFERENCE_TYPE)
12381 {
12382 if (complain & tf_error)
12383 error ("creating array of %qT", type);
12384 return error_mark_node;
12385 }
12386
12387 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12388 return error_mark_node;
12389
12390 r = build_cplus_array_type (type, domain);
12391
12392 if (TYPE_USER_ALIGN (t))
12393 {
12394 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12395 TYPE_USER_ALIGN (r) = 1;
12396 }
12397
12398 return r;
12399 }
12400
12401 case TYPENAME_TYPE:
12402 {
12403 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12404 in_decl, /*entering_scope=*/1);
12405 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12406 complain, in_decl);
12407
12408 if (ctx == error_mark_node || f == error_mark_node)
12409 return error_mark_node;
12410
12411 if (!MAYBE_CLASS_TYPE_P (ctx))
12412 {
12413 if (complain & tf_error)
12414 error ("%qT is not a class, struct, or union type", ctx);
12415 return error_mark_node;
12416 }
12417 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12418 {
12419 /* Normally, make_typename_type does not require that the CTX
12420 have complete type in order to allow things like:
12421
12422 template <class T> struct S { typename S<T>::X Y; };
12423
12424 But, such constructs have already been resolved by this
12425 point, so here CTX really should have complete type, unless
12426 it's a partial instantiation. */
12427 ctx = complete_type (ctx);
12428 if (!COMPLETE_TYPE_P (ctx))
12429 {
12430 if (complain & tf_error)
12431 cxx_incomplete_type_error (NULL_TREE, ctx);
12432 return error_mark_node;
12433 }
12434 }
12435
12436 f = make_typename_type (ctx, f, typename_type,
12437 complain | tf_keep_type_decl);
12438 if (f == error_mark_node)
12439 return f;
12440 if (TREE_CODE (f) == TYPE_DECL)
12441 {
12442 complain |= tf_ignore_bad_quals;
12443 f = TREE_TYPE (f);
12444 }
12445
12446 if (TREE_CODE (f) != TYPENAME_TYPE)
12447 {
12448 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12449 {
12450 if (complain & tf_error)
12451 error ("%qT resolves to %qT, which is not an enumeration type",
12452 t, f);
12453 else
12454 return error_mark_node;
12455 }
12456 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12457 {
12458 if (complain & tf_error)
12459 error ("%qT resolves to %qT, which is is not a class type",
12460 t, f);
12461 else
12462 return error_mark_node;
12463 }
12464 }
12465
12466 return cp_build_qualified_type_real
12467 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12468 }
12469
12470 case UNBOUND_CLASS_TEMPLATE:
12471 {
12472 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12473 in_decl, /*entering_scope=*/1);
12474 tree name = TYPE_IDENTIFIER (t);
12475 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12476
12477 if (ctx == error_mark_node || name == error_mark_node)
12478 return error_mark_node;
12479
12480 if (parm_list)
12481 parm_list = tsubst_template_parms (parm_list, args, complain);
12482 return make_unbound_class_template (ctx, name, parm_list, complain);
12483 }
12484
12485 case TYPEOF_TYPE:
12486 {
12487 tree type;
12488
12489 ++cp_unevaluated_operand;
12490 ++c_inhibit_evaluation_warnings;
12491
12492 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12493 complain, in_decl,
12494 /*integral_constant_expression_p=*/false);
12495
12496 --cp_unevaluated_operand;
12497 --c_inhibit_evaluation_warnings;
12498
12499 type = finish_typeof (type);
12500 return cp_build_qualified_type_real (type,
12501 cp_type_quals (t)
12502 | cp_type_quals (type),
12503 complain);
12504 }
12505
12506 case DECLTYPE_TYPE:
12507 {
12508 tree type;
12509
12510 ++cp_unevaluated_operand;
12511 ++c_inhibit_evaluation_warnings;
12512
12513 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12514 complain|tf_decltype, in_decl,
12515 /*function_p*/false,
12516 /*integral_constant_expression*/false);
12517
12518 --cp_unevaluated_operand;
12519 --c_inhibit_evaluation_warnings;
12520
12521 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12522 type = lambda_capture_field_type (type,
12523 DECLTYPE_FOR_INIT_CAPTURE (t));
12524 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12525 type = lambda_proxy_type (type);
12526 else
12527 {
12528 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12529 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12530 && EXPR_P (type))
12531 /* In a template ~id could be either a complement expression
12532 or an unqualified-id naming a destructor; if instantiating
12533 it produces an expression, it's not an id-expression or
12534 member access. */
12535 id = false;
12536 type = finish_decltype_type (type, id, complain);
12537 }
12538 return cp_build_qualified_type_real (type,
12539 cp_type_quals (t)
12540 | cp_type_quals (type),
12541 complain | tf_ignore_bad_quals);
12542 }
12543
12544 case UNDERLYING_TYPE:
12545 {
12546 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12547 complain, in_decl);
12548 return finish_underlying_type (type);
12549 }
12550
12551 case TYPE_ARGUMENT_PACK:
12552 case NONTYPE_ARGUMENT_PACK:
12553 {
12554 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12555 tree packed_out =
12556 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12557 args,
12558 complain,
12559 in_decl);
12560 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12561
12562 /* For template nontype argument packs, also substitute into
12563 the type. */
12564 if (code == NONTYPE_ARGUMENT_PACK)
12565 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12566
12567 return r;
12568 }
12569 break;
12570
12571 case VOID_CST:
12572 case INTEGER_CST:
12573 case REAL_CST:
12574 case STRING_CST:
12575 case PLUS_EXPR:
12576 case MINUS_EXPR:
12577 case NEGATE_EXPR:
12578 case NOP_EXPR:
12579 case INDIRECT_REF:
12580 case ADDR_EXPR:
12581 case CALL_EXPR:
12582 case ARRAY_REF:
12583 case SCOPE_REF:
12584 /* We should use one of the expression tsubsts for these codes. */
12585 gcc_unreachable ();
12586
12587 default:
12588 sorry ("use of %qs in template", get_tree_code_name (code));
12589 return error_mark_node;
12590 }
12591 }
12592
12593 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12594 type of the expression on the left-hand side of the "." or "->"
12595 operator. */
12596
12597 static tree
12598 tsubst_baselink (tree baselink, tree object_type,
12599 tree args, tsubst_flags_t complain, tree in_decl)
12600 {
12601 tree name;
12602 tree qualifying_scope;
12603 tree fns;
12604 tree optype;
12605 tree template_args = 0;
12606 bool template_id_p = false;
12607 bool qualified = BASELINK_QUALIFIED_P (baselink);
12608
12609 /* A baselink indicates a function from a base class. Both the
12610 BASELINK_ACCESS_BINFO and the base class referenced may
12611 indicate bases of the template class, rather than the
12612 instantiated class. In addition, lookups that were not
12613 ambiguous before may be ambiguous now. Therefore, we perform
12614 the lookup again. */
12615 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12616 qualifying_scope = tsubst (qualifying_scope, args,
12617 complain, in_decl);
12618 fns = BASELINK_FUNCTIONS (baselink);
12619 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12620 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12621 {
12622 template_id_p = true;
12623 template_args = TREE_OPERAND (fns, 1);
12624 fns = TREE_OPERAND (fns, 0);
12625 if (template_args)
12626 template_args = tsubst_template_args (template_args, args,
12627 complain, in_decl);
12628 }
12629 name = DECL_NAME (get_first_fn (fns));
12630 if (IDENTIFIER_TYPENAME_P (name))
12631 name = mangle_conv_op_name_for_type (optype);
12632 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12633 if (!baselink)
12634 return error_mark_node;
12635
12636 /* If lookup found a single function, mark it as used at this
12637 point. (If it lookup found multiple functions the one selected
12638 later by overload resolution will be marked as used at that
12639 point.) */
12640 if (BASELINK_P (baselink))
12641 fns = BASELINK_FUNCTIONS (baselink);
12642 if (!template_id_p && !really_overloaded_fn (fns)
12643 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
12644 return error_mark_node;
12645
12646 /* Add back the template arguments, if present. */
12647 if (BASELINK_P (baselink) && template_id_p)
12648 BASELINK_FUNCTIONS (baselink)
12649 = build_nt (TEMPLATE_ID_EXPR,
12650 BASELINK_FUNCTIONS (baselink),
12651 template_args);
12652 /* Update the conversion operator type. */
12653 BASELINK_OPTYPE (baselink) = optype;
12654
12655 if (!object_type)
12656 object_type = current_class_type;
12657
12658 if (qualified)
12659 baselink = adjust_result_of_qualified_name_lookup (baselink,
12660 qualifying_scope,
12661 object_type);
12662 return baselink;
12663 }
12664
12665 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12666 true if the qualified-id will be a postfix-expression in-and-of
12667 itself; false if more of the postfix-expression follows the
12668 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12669 of "&". */
12670
12671 static tree
12672 tsubst_qualified_id (tree qualified_id, tree args,
12673 tsubst_flags_t complain, tree in_decl,
12674 bool done, bool address_p)
12675 {
12676 tree expr;
12677 tree scope;
12678 tree name;
12679 bool is_template;
12680 tree template_args;
12681 location_t loc = UNKNOWN_LOCATION;
12682
12683 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12684
12685 /* Figure out what name to look up. */
12686 name = TREE_OPERAND (qualified_id, 1);
12687 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12688 {
12689 is_template = true;
12690 loc = EXPR_LOCATION (name);
12691 template_args = TREE_OPERAND (name, 1);
12692 if (template_args)
12693 template_args = tsubst_template_args (template_args, args,
12694 complain, in_decl);
12695 name = TREE_OPERAND (name, 0);
12696 }
12697 else
12698 {
12699 is_template = false;
12700 template_args = NULL_TREE;
12701 }
12702
12703 /* Substitute into the qualifying scope. When there are no ARGS, we
12704 are just trying to simplify a non-dependent expression. In that
12705 case the qualifying scope may be dependent, and, in any case,
12706 substituting will not help. */
12707 scope = TREE_OPERAND (qualified_id, 0);
12708 if (args)
12709 {
12710 scope = tsubst (scope, args, complain, in_decl);
12711 expr = tsubst_copy (name, args, complain, in_decl);
12712 }
12713 else
12714 expr = name;
12715
12716 if (dependent_scope_p (scope))
12717 {
12718 if (is_template)
12719 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12720 return build_qualified_name (NULL_TREE, scope, expr,
12721 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12722 }
12723
12724 if (!BASELINK_P (name) && !DECL_P (expr))
12725 {
12726 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12727 {
12728 /* A BIT_NOT_EXPR is used to represent a destructor. */
12729 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12730 {
12731 error ("qualifying type %qT does not match destructor name ~%qT",
12732 scope, TREE_OPERAND (expr, 0));
12733 expr = error_mark_node;
12734 }
12735 else
12736 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12737 /*is_type_p=*/0, false);
12738 }
12739 else
12740 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12741 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12742 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12743 {
12744 if (complain & tf_error)
12745 {
12746 error ("dependent-name %qE is parsed as a non-type, but "
12747 "instantiation yields a type", qualified_id);
12748 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12749 }
12750 return error_mark_node;
12751 }
12752 }
12753
12754 if (DECL_P (expr))
12755 {
12756 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12757 scope);
12758 /* Remember that there was a reference to this entity. */
12759 if (!mark_used (expr, complain) && !(complain & tf_error))
12760 return error_mark_node;
12761 }
12762
12763 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12764 {
12765 if (complain & tf_error)
12766 qualified_name_lookup_error (scope,
12767 TREE_OPERAND (qualified_id, 1),
12768 expr, input_location);
12769 return error_mark_node;
12770 }
12771
12772 if (is_template)
12773 expr = lookup_template_function (expr, template_args);
12774
12775 if (expr == error_mark_node && complain & tf_error)
12776 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12777 expr, input_location);
12778 else if (TYPE_P (scope))
12779 {
12780 expr = (adjust_result_of_qualified_name_lookup
12781 (expr, scope, current_nonlambda_class_type ()));
12782 expr = (finish_qualified_id_expr
12783 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12784 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12785 /*template_arg_p=*/false, complain));
12786 }
12787
12788 /* Expressions do not generally have reference type. */
12789 if (TREE_CODE (expr) != SCOPE_REF
12790 /* However, if we're about to form a pointer-to-member, we just
12791 want the referenced member referenced. */
12792 && TREE_CODE (expr) != OFFSET_REF)
12793 expr = convert_from_reference (expr);
12794
12795 return expr;
12796 }
12797
12798 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12799 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12800 for tsubst. */
12801
12802 static tree
12803 tsubst_init (tree init, tree decl, tree args,
12804 tsubst_flags_t complain, tree in_decl)
12805 {
12806 if (!init)
12807 return NULL_TREE;
12808
12809 init = tsubst_expr (init, args, complain, in_decl, false);
12810
12811 if (!init)
12812 {
12813 /* If we had an initializer but it
12814 instantiated to nothing,
12815 value-initialize the object. This will
12816 only occur when the initializer was a
12817 pack expansion where the parameter packs
12818 used in that expansion were of length
12819 zero. */
12820 init = build_value_init (TREE_TYPE (decl),
12821 complain);
12822 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12823 init = get_target_expr_sfinae (init, complain);
12824 }
12825
12826 return init;
12827 }
12828
12829 /* Like tsubst, but deals with expressions. This function just replaces
12830 template parms; to finish processing the resultant expression, use
12831 tsubst_copy_and_build or tsubst_expr. */
12832
12833 static tree
12834 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12835 {
12836 enum tree_code code;
12837 tree r;
12838
12839 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12840 return t;
12841
12842 code = TREE_CODE (t);
12843
12844 switch (code)
12845 {
12846 case PARM_DECL:
12847 r = retrieve_local_specialization (t);
12848
12849 if (r == NULL_TREE)
12850 {
12851 /* We get here for a use of 'this' in an NSDMI. */
12852 if (DECL_NAME (t) == this_identifier
12853 && current_function_decl
12854 && DECL_CONSTRUCTOR_P (current_function_decl))
12855 return current_class_ptr;
12856
12857 /* This can happen for a parameter name used later in a function
12858 declaration (such as in a late-specified return type). Just
12859 make a dummy decl, since it's only used for its type. */
12860 gcc_assert (cp_unevaluated_operand != 0);
12861 r = tsubst_decl (t, args, complain);
12862 /* Give it the template pattern as its context; its true context
12863 hasn't been instantiated yet and this is good enough for
12864 mangling. */
12865 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12866 }
12867
12868 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12869 r = ARGUMENT_PACK_SELECT_ARG (r);
12870 if (!mark_used (r, complain) && !(complain & tf_error))
12871 return error_mark_node;
12872 return r;
12873
12874 case CONST_DECL:
12875 {
12876 tree enum_type;
12877 tree v;
12878
12879 if (DECL_TEMPLATE_PARM_P (t))
12880 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12881 /* There is no need to substitute into namespace-scope
12882 enumerators. */
12883 if (DECL_NAMESPACE_SCOPE_P (t))
12884 return t;
12885 /* If ARGS is NULL, then T is known to be non-dependent. */
12886 if (args == NULL_TREE)
12887 return scalar_constant_value (t);
12888
12889 /* Unfortunately, we cannot just call lookup_name here.
12890 Consider:
12891
12892 template <int I> int f() {
12893 enum E { a = I };
12894 struct S { void g() { E e = a; } };
12895 };
12896
12897 When we instantiate f<7>::S::g(), say, lookup_name is not
12898 clever enough to find f<7>::a. */
12899 enum_type
12900 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12901 /*entering_scope=*/0);
12902
12903 for (v = TYPE_VALUES (enum_type);
12904 v != NULL_TREE;
12905 v = TREE_CHAIN (v))
12906 if (TREE_PURPOSE (v) == DECL_NAME (t))
12907 return TREE_VALUE (v);
12908
12909 /* We didn't find the name. That should never happen; if
12910 name-lookup found it during preliminary parsing, we
12911 should find it again here during instantiation. */
12912 gcc_unreachable ();
12913 }
12914 return t;
12915
12916 case FIELD_DECL:
12917 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12918 {
12919 /* Check for a local specialization set up by
12920 tsubst_pack_expansion. */
12921 if (tree r = retrieve_local_specialization (t))
12922 {
12923 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12924 r = ARGUMENT_PACK_SELECT_ARG (r);
12925 return r;
12926 }
12927
12928 /* When retrieving a capture pack from a generic lambda, remove the
12929 lambda call op's own template argument list from ARGS. Only the
12930 template arguments active for the closure type should be used to
12931 retrieve the pack specialization. */
12932 if (LAMBDA_FUNCTION_P (current_function_decl)
12933 && (template_class_depth (DECL_CONTEXT (t))
12934 != TMPL_ARGS_DEPTH (args)))
12935 args = strip_innermost_template_args (args, 1);
12936
12937 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12938 tsubst_decl put in the hash table. */
12939 return retrieve_specialization (t, args, 0);
12940 }
12941
12942 if (DECL_CONTEXT (t))
12943 {
12944 tree ctx;
12945
12946 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12947 /*entering_scope=*/1);
12948 if (ctx != DECL_CONTEXT (t))
12949 {
12950 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12951 if (!r)
12952 {
12953 if (complain & tf_error)
12954 error ("using invalid field %qD", t);
12955 return error_mark_node;
12956 }
12957 return r;
12958 }
12959 }
12960
12961 return t;
12962
12963 case VAR_DECL:
12964 case FUNCTION_DECL:
12965 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12966 r = tsubst (t, args, complain, in_decl);
12967 else if (local_variable_p (t))
12968 {
12969 r = retrieve_local_specialization (t);
12970 if (r == NULL_TREE)
12971 {
12972 /* First try name lookup to find the instantiation. */
12973 r = lookup_name (DECL_NAME (t));
12974 if (r)
12975 {
12976 /* Make sure that the one we found is the one we want. */
12977 tree ctx = tsubst (DECL_CONTEXT (t), args,
12978 complain, in_decl);
12979 if (ctx != DECL_CONTEXT (r))
12980 r = NULL_TREE;
12981 }
12982
12983 if (r)
12984 /* OK */;
12985 else
12986 {
12987 /* This can happen for a variable used in a
12988 late-specified return type of a local lambda, or for a
12989 local static or constant. Building a new VAR_DECL
12990 should be OK in all those cases. */
12991 r = tsubst_decl (t, args, complain);
12992 if (decl_maybe_constant_var_p (r))
12993 {
12994 /* We can't call cp_finish_decl, so handle the
12995 initializer by hand. */
12996 tree init = tsubst_init (DECL_INITIAL (t), r, args,
12997 complain, in_decl);
12998 if (!processing_template_decl)
12999 init = maybe_constant_init (init);
13000 if (processing_template_decl
13001 ? potential_constant_expression (init)
13002 : reduced_constant_expression_p (init))
13003 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13004 = TREE_CONSTANT (r) = true;
13005 DECL_INITIAL (r) = init;
13006 }
13007 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13008 || decl_constant_var_p (r)
13009 || errorcount || sorrycount);
13010 if (!processing_template_decl)
13011 {
13012 if (TREE_STATIC (r))
13013 rest_of_decl_compilation (r, toplevel_bindings_p (),
13014 at_eof);
13015 else if (decl_constant_var_p (r))
13016 /* A use of a local constant decays to its value.
13017 FIXME update for core DR 696. */
13018 r = scalar_constant_value (r);
13019 }
13020 }
13021 /* Remember this for subsequent uses. */
13022 if (local_specializations)
13023 register_local_specialization (r, t);
13024 }
13025 }
13026 else
13027 r = t;
13028 if (!mark_used (r, complain) && !(complain & tf_error))
13029 return error_mark_node;
13030 return r;
13031
13032 case NAMESPACE_DECL:
13033 return t;
13034
13035 case OVERLOAD:
13036 /* An OVERLOAD will always be a non-dependent overload set; an
13037 overload set from function scope will just be represented with an
13038 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13039 gcc_assert (!uses_template_parms (t));
13040 return t;
13041
13042 case BASELINK:
13043 return tsubst_baselink (t, current_nonlambda_class_type (),
13044 args, complain, in_decl);
13045
13046 case TEMPLATE_DECL:
13047 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13048 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13049 args, complain, in_decl);
13050 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13051 return tsubst (t, args, complain, in_decl);
13052 else if (DECL_CLASS_SCOPE_P (t)
13053 && uses_template_parms (DECL_CONTEXT (t)))
13054 {
13055 /* Template template argument like the following example need
13056 special treatment:
13057
13058 template <template <class> class TT> struct C {};
13059 template <class T> struct D {
13060 template <class U> struct E {};
13061 C<E> c; // #1
13062 };
13063 D<int> d; // #2
13064
13065 We are processing the template argument `E' in #1 for
13066 the template instantiation #2. Originally, `E' is a
13067 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13068 have to substitute this with one having context `D<int>'. */
13069
13070 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13071 return lookup_field (context, DECL_NAME(t), 0, false);
13072 }
13073 else
13074 /* Ordinary template template argument. */
13075 return t;
13076
13077 case CAST_EXPR:
13078 case REINTERPRET_CAST_EXPR:
13079 case CONST_CAST_EXPR:
13080 case STATIC_CAST_EXPR:
13081 case DYNAMIC_CAST_EXPR:
13082 case IMPLICIT_CONV_EXPR:
13083 case CONVERT_EXPR:
13084 case NOP_EXPR:
13085 {
13086 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13087 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13088 return build1 (code, type, op0);
13089 }
13090
13091 case SIZEOF_EXPR:
13092 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13093 {
13094
13095 tree expanded, op = TREE_OPERAND (t, 0);
13096 int len = 0;
13097
13098 if (SIZEOF_EXPR_TYPE_P (t))
13099 op = TREE_TYPE (op);
13100
13101 ++cp_unevaluated_operand;
13102 ++c_inhibit_evaluation_warnings;
13103 /* We only want to compute the number of arguments. */
13104 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13105 --cp_unevaluated_operand;
13106 --c_inhibit_evaluation_warnings;
13107
13108 if (TREE_CODE (expanded) == TREE_VEC)
13109 len = TREE_VEC_LENGTH (expanded);
13110
13111 if (expanded == error_mark_node)
13112 return error_mark_node;
13113 else if (PACK_EXPANSION_P (expanded)
13114 || (TREE_CODE (expanded) == TREE_VEC
13115 && len > 0
13116 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13117 {
13118 if (TREE_CODE (expanded) == TREE_VEC)
13119 expanded = TREE_VEC_ELT (expanded, len - 1);
13120
13121 if (TYPE_P (expanded))
13122 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13123 complain & tf_error);
13124 else
13125 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13126 complain & tf_error);
13127 }
13128 else
13129 return build_int_cst (size_type_node, len);
13130 }
13131 if (SIZEOF_EXPR_TYPE_P (t))
13132 {
13133 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13134 args, complain, in_decl);
13135 r = build1 (NOP_EXPR, r, error_mark_node);
13136 r = build1 (SIZEOF_EXPR,
13137 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13138 SIZEOF_EXPR_TYPE_P (r) = 1;
13139 return r;
13140 }
13141 /* Fall through */
13142
13143 case INDIRECT_REF:
13144 case NEGATE_EXPR:
13145 case TRUTH_NOT_EXPR:
13146 case BIT_NOT_EXPR:
13147 case ADDR_EXPR:
13148 case UNARY_PLUS_EXPR: /* Unary + */
13149 case ALIGNOF_EXPR:
13150 case AT_ENCODE_EXPR:
13151 case ARROW_EXPR:
13152 case THROW_EXPR:
13153 case TYPEID_EXPR:
13154 case REALPART_EXPR:
13155 case IMAGPART_EXPR:
13156 case PAREN_EXPR:
13157 {
13158 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13159 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13160 return build1 (code, type, op0);
13161 }
13162
13163 case COMPONENT_REF:
13164 {
13165 tree object;
13166 tree name;
13167
13168 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13169 name = TREE_OPERAND (t, 1);
13170 if (TREE_CODE (name) == BIT_NOT_EXPR)
13171 {
13172 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13173 complain, in_decl);
13174 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13175 }
13176 else if (TREE_CODE (name) == SCOPE_REF
13177 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13178 {
13179 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13180 complain, in_decl);
13181 name = TREE_OPERAND (name, 1);
13182 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13183 complain, in_decl);
13184 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13185 name = build_qualified_name (/*type=*/NULL_TREE,
13186 base, name,
13187 /*template_p=*/false);
13188 }
13189 else if (BASELINK_P (name))
13190 name = tsubst_baselink (name,
13191 non_reference (TREE_TYPE (object)),
13192 args, complain,
13193 in_decl);
13194 else
13195 name = tsubst_copy (name, args, complain, in_decl);
13196 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13197 }
13198
13199 case PLUS_EXPR:
13200 case MINUS_EXPR:
13201 case MULT_EXPR:
13202 case TRUNC_DIV_EXPR:
13203 case CEIL_DIV_EXPR:
13204 case FLOOR_DIV_EXPR:
13205 case ROUND_DIV_EXPR:
13206 case EXACT_DIV_EXPR:
13207 case BIT_AND_EXPR:
13208 case BIT_IOR_EXPR:
13209 case BIT_XOR_EXPR:
13210 case TRUNC_MOD_EXPR:
13211 case FLOOR_MOD_EXPR:
13212 case TRUTH_ANDIF_EXPR:
13213 case TRUTH_ORIF_EXPR:
13214 case TRUTH_AND_EXPR:
13215 case TRUTH_OR_EXPR:
13216 case RSHIFT_EXPR:
13217 case LSHIFT_EXPR:
13218 case RROTATE_EXPR:
13219 case LROTATE_EXPR:
13220 case EQ_EXPR:
13221 case NE_EXPR:
13222 case MAX_EXPR:
13223 case MIN_EXPR:
13224 case LE_EXPR:
13225 case GE_EXPR:
13226 case LT_EXPR:
13227 case GT_EXPR:
13228 case COMPOUND_EXPR:
13229 case DOTSTAR_EXPR:
13230 case MEMBER_REF:
13231 case PREDECREMENT_EXPR:
13232 case PREINCREMENT_EXPR:
13233 case POSTDECREMENT_EXPR:
13234 case POSTINCREMENT_EXPR:
13235 {
13236 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13237 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13238 return build_nt (code, op0, op1);
13239 }
13240
13241 case SCOPE_REF:
13242 {
13243 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13244 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13245 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13246 QUALIFIED_NAME_IS_TEMPLATE (t));
13247 }
13248
13249 case ARRAY_REF:
13250 {
13251 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13252 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13253 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13254 }
13255
13256 case CALL_EXPR:
13257 {
13258 int n = VL_EXP_OPERAND_LENGTH (t);
13259 tree result = build_vl_exp (CALL_EXPR, n);
13260 int i;
13261 for (i = 0; i < n; i++)
13262 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13263 complain, in_decl);
13264 return result;
13265 }
13266
13267 case COND_EXPR:
13268 case MODOP_EXPR:
13269 case PSEUDO_DTOR_EXPR:
13270 case VEC_PERM_EXPR:
13271 {
13272 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13273 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13274 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13275 r = build_nt (code, op0, op1, op2);
13276 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13277 return r;
13278 }
13279
13280 case NEW_EXPR:
13281 {
13282 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13283 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13284 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13285 r = build_nt (code, op0, op1, op2);
13286 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13287 return r;
13288 }
13289
13290 case DELETE_EXPR:
13291 {
13292 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13293 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13294 r = build_nt (code, op0, op1);
13295 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13296 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13297 return r;
13298 }
13299
13300 case TEMPLATE_ID_EXPR:
13301 {
13302 /* Substituted template arguments */
13303 tree fn = TREE_OPERAND (t, 0);
13304 tree targs = TREE_OPERAND (t, 1);
13305
13306 fn = tsubst_copy (fn, args, complain, in_decl);
13307 if (targs)
13308 targs = tsubst_template_args (targs, args, complain, in_decl);
13309
13310 return lookup_template_function (fn, targs);
13311 }
13312
13313 case TREE_LIST:
13314 {
13315 tree purpose, value, chain;
13316
13317 if (t == void_list_node)
13318 return t;
13319
13320 purpose = TREE_PURPOSE (t);
13321 if (purpose)
13322 purpose = tsubst_copy (purpose, args, complain, in_decl);
13323 value = TREE_VALUE (t);
13324 if (value)
13325 value = tsubst_copy (value, args, complain, in_decl);
13326 chain = TREE_CHAIN (t);
13327 if (chain && chain != void_type_node)
13328 chain = tsubst_copy (chain, args, complain, in_decl);
13329 if (purpose == TREE_PURPOSE (t)
13330 && value == TREE_VALUE (t)
13331 && chain == TREE_CHAIN (t))
13332 return t;
13333 return tree_cons (purpose, value, chain);
13334 }
13335
13336 case RECORD_TYPE:
13337 case UNION_TYPE:
13338 case ENUMERAL_TYPE:
13339 case INTEGER_TYPE:
13340 case TEMPLATE_TYPE_PARM:
13341 case TEMPLATE_TEMPLATE_PARM:
13342 case BOUND_TEMPLATE_TEMPLATE_PARM:
13343 case TEMPLATE_PARM_INDEX:
13344 case POINTER_TYPE:
13345 case REFERENCE_TYPE:
13346 case OFFSET_TYPE:
13347 case FUNCTION_TYPE:
13348 case METHOD_TYPE:
13349 case ARRAY_TYPE:
13350 case TYPENAME_TYPE:
13351 case UNBOUND_CLASS_TEMPLATE:
13352 case TYPEOF_TYPE:
13353 case DECLTYPE_TYPE:
13354 case TYPE_DECL:
13355 return tsubst (t, args, complain, in_decl);
13356
13357 case USING_DECL:
13358 t = DECL_NAME (t);
13359 /* Fall through. */
13360 case IDENTIFIER_NODE:
13361 if (IDENTIFIER_TYPENAME_P (t))
13362 {
13363 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13364 return mangle_conv_op_name_for_type (new_type);
13365 }
13366 else
13367 return t;
13368
13369 case CONSTRUCTOR:
13370 /* This is handled by tsubst_copy_and_build. */
13371 gcc_unreachable ();
13372
13373 case VA_ARG_EXPR:
13374 {
13375 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13376 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13377 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13378 }
13379
13380 case CLEANUP_POINT_EXPR:
13381 /* We shouldn't have built any of these during initial template
13382 generation. Instead, they should be built during instantiation
13383 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13384 gcc_unreachable ();
13385
13386 case OFFSET_REF:
13387 {
13388 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13389 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13390 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13391 r = build2 (code, type, op0, op1);
13392 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13393 if (!mark_used (TREE_OPERAND (r, 1), complain)
13394 && !(complain & tf_error))
13395 return error_mark_node;
13396 return r;
13397 }
13398
13399 case EXPR_PACK_EXPANSION:
13400 error ("invalid use of pack expansion expression");
13401 return error_mark_node;
13402
13403 case NONTYPE_ARGUMENT_PACK:
13404 error ("use %<...%> to expand argument pack");
13405 return error_mark_node;
13406
13407 case VOID_CST:
13408 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
13409 return t;
13410
13411 case INTEGER_CST:
13412 case REAL_CST:
13413 case STRING_CST:
13414 case COMPLEX_CST:
13415 {
13416 /* Instantiate any typedefs in the type. */
13417 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13418 r = fold_convert (type, t);
13419 gcc_assert (TREE_CODE (r) == code);
13420 return r;
13421 }
13422
13423 case PTRMEM_CST:
13424 /* These can sometimes show up in a partial instantiation, but never
13425 involve template parms. */
13426 gcc_assert (!uses_template_parms (t));
13427 return t;
13428
13429 default:
13430 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13431 gcc_checking_assert (false);
13432 return t;
13433 }
13434 }
13435
13436 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13437
13438 static tree
13439 tsubst_omp_clauses (tree clauses, bool declare_simd,
13440 tree args, tsubst_flags_t complain, tree in_decl)
13441 {
13442 tree new_clauses = NULL, nc, oc;
13443
13444 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13445 {
13446 nc = copy_node (oc);
13447 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13448 new_clauses = nc;
13449
13450 switch (OMP_CLAUSE_CODE (nc))
13451 {
13452 case OMP_CLAUSE_LASTPRIVATE:
13453 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13454 {
13455 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13456 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13457 in_decl, /*integral_constant_expression_p=*/false);
13458 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13459 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13460 }
13461 /* FALLTHRU */
13462 case OMP_CLAUSE_PRIVATE:
13463 case OMP_CLAUSE_SHARED:
13464 case OMP_CLAUSE_FIRSTPRIVATE:
13465 case OMP_CLAUSE_COPYIN:
13466 case OMP_CLAUSE_COPYPRIVATE:
13467 case OMP_CLAUSE_IF:
13468 case OMP_CLAUSE_NUM_THREADS:
13469 case OMP_CLAUSE_SCHEDULE:
13470 case OMP_CLAUSE_COLLAPSE:
13471 case OMP_CLAUSE_FINAL:
13472 case OMP_CLAUSE_DEPEND:
13473 case OMP_CLAUSE_FROM:
13474 case OMP_CLAUSE_TO:
13475 case OMP_CLAUSE_UNIFORM:
13476 case OMP_CLAUSE_MAP:
13477 case OMP_CLAUSE_DEVICE:
13478 case OMP_CLAUSE_DIST_SCHEDULE:
13479 case OMP_CLAUSE_NUM_TEAMS:
13480 case OMP_CLAUSE_THREAD_LIMIT:
13481 case OMP_CLAUSE_SAFELEN:
13482 case OMP_CLAUSE_SIMDLEN:
13483 OMP_CLAUSE_OPERAND (nc, 0)
13484 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13485 in_decl, /*integral_constant_expression_p=*/false);
13486 break;
13487 case OMP_CLAUSE_REDUCTION:
13488 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13489 {
13490 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13491 if (TREE_CODE (placeholder) == SCOPE_REF)
13492 {
13493 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13494 complain, in_decl);
13495 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13496 = build_qualified_name (NULL_TREE, scope,
13497 TREE_OPERAND (placeholder, 1),
13498 false);
13499 }
13500 else
13501 gcc_assert (identifier_p (placeholder));
13502 }
13503 OMP_CLAUSE_OPERAND (nc, 0)
13504 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13505 in_decl, /*integral_constant_expression_p=*/false);
13506 break;
13507 case OMP_CLAUSE_LINEAR:
13508 case OMP_CLAUSE_ALIGNED:
13509 OMP_CLAUSE_OPERAND (nc, 0)
13510 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13511 in_decl, /*integral_constant_expression_p=*/false);
13512 OMP_CLAUSE_OPERAND (nc, 1)
13513 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13514 in_decl, /*integral_constant_expression_p=*/false);
13515 break;
13516
13517 case OMP_CLAUSE_NOWAIT:
13518 case OMP_CLAUSE_ORDERED:
13519 case OMP_CLAUSE_DEFAULT:
13520 case OMP_CLAUSE_UNTIED:
13521 case OMP_CLAUSE_MERGEABLE:
13522 case OMP_CLAUSE_INBRANCH:
13523 case OMP_CLAUSE_NOTINBRANCH:
13524 case OMP_CLAUSE_PROC_BIND:
13525 case OMP_CLAUSE_FOR:
13526 case OMP_CLAUSE_PARALLEL:
13527 case OMP_CLAUSE_SECTIONS:
13528 case OMP_CLAUSE_TASKGROUP:
13529 break;
13530 default:
13531 gcc_unreachable ();
13532 }
13533 }
13534
13535 new_clauses = nreverse (new_clauses);
13536 if (!declare_simd)
13537 new_clauses = finish_omp_clauses (new_clauses);
13538 return new_clauses;
13539 }
13540
13541 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13542
13543 static tree
13544 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13545 tree in_decl)
13546 {
13547 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13548
13549 tree purpose, value, chain;
13550
13551 if (t == NULL)
13552 return t;
13553
13554 if (TREE_CODE (t) != TREE_LIST)
13555 return tsubst_copy_and_build (t, args, complain, in_decl,
13556 /*function_p=*/false,
13557 /*integral_constant_expression_p=*/false);
13558
13559 if (t == void_list_node)
13560 return t;
13561
13562 purpose = TREE_PURPOSE (t);
13563 if (purpose)
13564 purpose = RECUR (purpose);
13565 value = TREE_VALUE (t);
13566 if (value)
13567 {
13568 if (TREE_CODE (value) != LABEL_DECL)
13569 value = RECUR (value);
13570 else
13571 {
13572 value = lookup_label (DECL_NAME (value));
13573 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13574 TREE_USED (value) = 1;
13575 }
13576 }
13577 chain = TREE_CHAIN (t);
13578 if (chain && chain != void_type_node)
13579 chain = RECUR (chain);
13580 return tree_cons (purpose, value, chain);
13581 #undef RECUR
13582 }
13583
13584 /* Substitute one OMP_FOR iterator. */
13585
13586 static void
13587 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13588 tree condv, tree incrv, tree *clauses,
13589 tree args, tsubst_flags_t complain, tree in_decl,
13590 bool integral_constant_expression_p)
13591 {
13592 #define RECUR(NODE) \
13593 tsubst_expr ((NODE), args, complain, in_decl, \
13594 integral_constant_expression_p)
13595 tree decl, init, cond, incr;
13596
13597 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13598 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13599 decl = TREE_OPERAND (init, 0);
13600 init = TREE_OPERAND (init, 1);
13601 tree decl_expr = NULL_TREE;
13602 if (init && TREE_CODE (init) == DECL_EXPR)
13603 {
13604 /* We need to jump through some hoops to handle declarations in the
13605 for-init-statement, since we might need to handle auto deduction,
13606 but we need to keep control of initialization. */
13607 decl_expr = init;
13608 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13609 decl = tsubst_decl (decl, args, complain);
13610 }
13611 else
13612 decl = RECUR (decl);
13613 init = RECUR (init);
13614
13615 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13616 if (auto_node && init)
13617 TREE_TYPE (decl)
13618 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13619
13620 gcc_assert (!type_dependent_expression_p (decl));
13621
13622 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13623 {
13624 if (decl_expr)
13625 {
13626 /* Declare the variable, but don't let that initialize it. */
13627 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13628 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13629 RECUR (decl_expr);
13630 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13631 }
13632
13633 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13634 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13635 if (TREE_CODE (incr) == MODIFY_EXPR)
13636 {
13637 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13638 tree rhs = RECUR (TREE_OPERAND (incr, 1));
13639 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
13640 NOP_EXPR, rhs, complain);
13641 }
13642 else
13643 incr = RECUR (incr);
13644 TREE_VEC_ELT (declv, i) = decl;
13645 TREE_VEC_ELT (initv, i) = init;
13646 TREE_VEC_ELT (condv, i) = cond;
13647 TREE_VEC_ELT (incrv, i) = incr;
13648 return;
13649 }
13650
13651 if (decl_expr)
13652 {
13653 /* Declare and initialize the variable. */
13654 RECUR (decl_expr);
13655 init = NULL_TREE;
13656 }
13657 else if (init)
13658 {
13659 tree c;
13660 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13661 {
13662 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13663 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13664 && OMP_CLAUSE_DECL (c) == decl)
13665 break;
13666 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13667 && OMP_CLAUSE_DECL (c) == decl)
13668 error ("iteration variable %qD should not be firstprivate", decl);
13669 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13670 && OMP_CLAUSE_DECL (c) == decl)
13671 error ("iteration variable %qD should not be reduction", decl);
13672 }
13673 if (c == NULL)
13674 {
13675 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13676 OMP_CLAUSE_DECL (c) = decl;
13677 c = finish_omp_clauses (c);
13678 if (c)
13679 {
13680 OMP_CLAUSE_CHAIN (c) = *clauses;
13681 *clauses = c;
13682 }
13683 }
13684 }
13685 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13686 if (COMPARISON_CLASS_P (cond))
13687 {
13688 tree op0 = RECUR (TREE_OPERAND (cond, 0));
13689 tree op1 = RECUR (TREE_OPERAND (cond, 1));
13690 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
13691 }
13692 else
13693 cond = RECUR (cond);
13694 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13695 switch (TREE_CODE (incr))
13696 {
13697 case PREINCREMENT_EXPR:
13698 case PREDECREMENT_EXPR:
13699 case POSTINCREMENT_EXPR:
13700 case POSTDECREMENT_EXPR:
13701 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13702 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13703 break;
13704 case MODIFY_EXPR:
13705 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13706 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13707 {
13708 tree rhs = TREE_OPERAND (incr, 1);
13709 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13710 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13711 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13712 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13713 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13714 rhs0, rhs1));
13715 }
13716 else
13717 incr = RECUR (incr);
13718 break;
13719 case MODOP_EXPR:
13720 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13721 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13722 {
13723 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13724 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13725 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13726 TREE_TYPE (decl), lhs,
13727 RECUR (TREE_OPERAND (incr, 2))));
13728 }
13729 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13730 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13731 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13732 {
13733 tree rhs = TREE_OPERAND (incr, 2);
13734 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13735 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13736 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13737 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13738 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13739 rhs0, rhs1));
13740 }
13741 else
13742 incr = RECUR (incr);
13743 break;
13744 default:
13745 incr = RECUR (incr);
13746 break;
13747 }
13748
13749 TREE_VEC_ELT (declv, i) = decl;
13750 TREE_VEC_ELT (initv, i) = init;
13751 TREE_VEC_ELT (condv, i) = cond;
13752 TREE_VEC_ELT (incrv, i) = incr;
13753 #undef RECUR
13754 }
13755
13756 /* Like tsubst_copy for expressions, etc. but also does semantic
13757 processing. */
13758
13759 static tree
13760 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13761 bool integral_constant_expression_p)
13762 {
13763 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13764 #define RECUR(NODE) \
13765 tsubst_expr ((NODE), args, complain, in_decl, \
13766 integral_constant_expression_p)
13767
13768 tree stmt, tmp;
13769 tree r;
13770 location_t loc;
13771
13772 if (t == NULL_TREE || t == error_mark_node)
13773 return t;
13774
13775 loc = input_location;
13776 if (EXPR_HAS_LOCATION (t))
13777 input_location = EXPR_LOCATION (t);
13778 if (STATEMENT_CODE_P (TREE_CODE (t)))
13779 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13780
13781 switch (TREE_CODE (t))
13782 {
13783 case STATEMENT_LIST:
13784 {
13785 tree_stmt_iterator i;
13786 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13787 RECUR (tsi_stmt (i));
13788 break;
13789 }
13790
13791 case CTOR_INITIALIZER:
13792 finish_mem_initializers (tsubst_initializer_list
13793 (TREE_OPERAND (t, 0), args));
13794 break;
13795
13796 case RETURN_EXPR:
13797 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13798 break;
13799
13800 case EXPR_STMT:
13801 tmp = RECUR (EXPR_STMT_EXPR (t));
13802 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13803 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13804 else
13805 finish_expr_stmt (tmp);
13806 break;
13807
13808 case USING_STMT:
13809 do_using_directive (USING_STMT_NAMESPACE (t));
13810 break;
13811
13812 case DECL_EXPR:
13813 {
13814 tree decl, pattern_decl;
13815 tree init;
13816
13817 pattern_decl = decl = DECL_EXPR_DECL (t);
13818 if (TREE_CODE (decl) == LABEL_DECL)
13819 finish_label_decl (DECL_NAME (decl));
13820 else if (TREE_CODE (decl) == USING_DECL)
13821 {
13822 tree scope = USING_DECL_SCOPE (decl);
13823 tree name = DECL_NAME (decl);
13824 tree decl;
13825
13826 scope = tsubst (scope, args, complain, in_decl);
13827 decl = lookup_qualified_name (scope, name,
13828 /*is_type_p=*/false,
13829 /*complain=*/false);
13830 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13831 qualified_name_lookup_error (scope, name, decl, input_location);
13832 else
13833 do_local_using_decl (decl, scope, name);
13834 }
13835 else if (DECL_PACK_P (decl))
13836 {
13837 /* Don't build up decls for a variadic capture proxy, we'll
13838 instantiate the elements directly as needed. */
13839 break;
13840 }
13841 else
13842 {
13843 init = DECL_INITIAL (decl);
13844 decl = tsubst (decl, args, complain, in_decl);
13845 if (decl != error_mark_node)
13846 {
13847 /* By marking the declaration as instantiated, we avoid
13848 trying to instantiate it. Since instantiate_decl can't
13849 handle local variables, and since we've already done
13850 all that needs to be done, that's the right thing to
13851 do. */
13852 if (VAR_P (decl))
13853 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13854 if (VAR_P (decl)
13855 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13856 /* Anonymous aggregates are a special case. */
13857 finish_anon_union (decl);
13858 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13859 {
13860 DECL_CONTEXT (decl) = current_function_decl;
13861 if (DECL_NAME (decl) == this_identifier)
13862 {
13863 tree lam = DECL_CONTEXT (current_function_decl);
13864 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13865 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13866 }
13867 insert_capture_proxy (decl);
13868 }
13869 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13870 /* We already did a pushtag. */;
13871 else if (TREE_CODE (decl) == FUNCTION_DECL
13872 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13873 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13874 {
13875 DECL_CONTEXT (decl) = NULL_TREE;
13876 pushdecl (decl);
13877 DECL_CONTEXT (decl) = current_function_decl;
13878 cp_check_omp_declare_reduction (decl);
13879 }
13880 else
13881 {
13882 int const_init = false;
13883 maybe_push_decl (decl);
13884 if (VAR_P (decl)
13885 && DECL_PRETTY_FUNCTION_P (decl))
13886 {
13887 /* For __PRETTY_FUNCTION__ we have to adjust the
13888 initializer. */
13889 const char *const name
13890 = cxx_printable_name (current_function_decl, 2);
13891 init = cp_fname_init (name, &TREE_TYPE (decl));
13892 }
13893 else
13894 init = tsubst_init (init, decl, args, complain, in_decl);
13895
13896 if (VAR_P (decl))
13897 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13898 (pattern_decl));
13899 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13900 }
13901 }
13902 }
13903
13904 break;
13905 }
13906
13907 case FOR_STMT:
13908 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13909 RECUR (FOR_INIT_STMT (t));
13910 finish_for_init_stmt (stmt);
13911 tmp = RECUR (FOR_COND (t));
13912 finish_for_cond (tmp, stmt, false);
13913 tmp = RECUR (FOR_EXPR (t));
13914 finish_for_expr (tmp, stmt);
13915 RECUR (FOR_BODY (t));
13916 finish_for_stmt (stmt);
13917 break;
13918
13919 case RANGE_FOR_STMT:
13920 {
13921 tree decl, expr;
13922 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13923 decl = RANGE_FOR_DECL (t);
13924 decl = tsubst (decl, args, complain, in_decl);
13925 maybe_push_decl (decl);
13926 expr = RECUR (RANGE_FOR_EXPR (t));
13927 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13928 RECUR (RANGE_FOR_BODY (t));
13929 finish_for_stmt (stmt);
13930 }
13931 break;
13932
13933 case WHILE_STMT:
13934 stmt = begin_while_stmt ();
13935 tmp = RECUR (WHILE_COND (t));
13936 finish_while_stmt_cond (tmp, stmt, false);
13937 RECUR (WHILE_BODY (t));
13938 finish_while_stmt (stmt);
13939 break;
13940
13941 case DO_STMT:
13942 stmt = begin_do_stmt ();
13943 RECUR (DO_BODY (t));
13944 finish_do_body (stmt);
13945 tmp = RECUR (DO_COND (t));
13946 finish_do_stmt (tmp, stmt, false);
13947 break;
13948
13949 case IF_STMT:
13950 stmt = begin_if_stmt ();
13951 tmp = RECUR (IF_COND (t));
13952 finish_if_stmt_cond (tmp, stmt);
13953 RECUR (THEN_CLAUSE (t));
13954 finish_then_clause (stmt);
13955
13956 if (ELSE_CLAUSE (t))
13957 {
13958 begin_else_clause (stmt);
13959 RECUR (ELSE_CLAUSE (t));
13960 finish_else_clause (stmt);
13961 }
13962
13963 finish_if_stmt (stmt);
13964 break;
13965
13966 case BIND_EXPR:
13967 if (BIND_EXPR_BODY_BLOCK (t))
13968 stmt = begin_function_body ();
13969 else
13970 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13971 ? BCS_TRY_BLOCK : 0);
13972
13973 RECUR (BIND_EXPR_BODY (t));
13974
13975 if (BIND_EXPR_BODY_BLOCK (t))
13976 finish_function_body (stmt);
13977 else
13978 finish_compound_stmt (stmt);
13979 break;
13980
13981 case BREAK_STMT:
13982 finish_break_stmt ();
13983 break;
13984
13985 case CONTINUE_STMT:
13986 finish_continue_stmt ();
13987 break;
13988
13989 case SWITCH_STMT:
13990 stmt = begin_switch_stmt ();
13991 tmp = RECUR (SWITCH_STMT_COND (t));
13992 finish_switch_cond (tmp, stmt);
13993 RECUR (SWITCH_STMT_BODY (t));
13994 finish_switch_stmt (stmt);
13995 break;
13996
13997 case CASE_LABEL_EXPR:
13998 {
13999 tree low = RECUR (CASE_LOW (t));
14000 tree high = RECUR (CASE_HIGH (t));
14001 finish_case_label (EXPR_LOCATION (t), low, high);
14002 }
14003 break;
14004
14005 case LABEL_EXPR:
14006 {
14007 tree decl = LABEL_EXPR_LABEL (t);
14008 tree label;
14009
14010 label = finish_label_stmt (DECL_NAME (decl));
14011 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
14012 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
14013 }
14014 break;
14015
14016 case GOTO_EXPR:
14017 tmp = GOTO_DESTINATION (t);
14018 if (TREE_CODE (tmp) != LABEL_DECL)
14019 /* Computed goto's must be tsubst'd into. On the other hand,
14020 non-computed gotos must not be; the identifier in question
14021 will have no binding. */
14022 tmp = RECUR (tmp);
14023 else
14024 tmp = DECL_NAME (tmp);
14025 finish_goto_stmt (tmp);
14026 break;
14027
14028 case ASM_EXPR:
14029 {
14030 tree string = RECUR (ASM_STRING (t));
14031 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
14032 complain, in_decl);
14033 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14034 complain, in_decl);
14035 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14036 complain, in_decl);
14037 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14038 complain, in_decl);
14039 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14040 clobbers, labels);
14041 tree asm_expr = tmp;
14042 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14043 asm_expr = TREE_OPERAND (asm_expr, 0);
14044 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14045 }
14046 break;
14047
14048 case TRY_BLOCK:
14049 if (CLEANUP_P (t))
14050 {
14051 stmt = begin_try_block ();
14052 RECUR (TRY_STMTS (t));
14053 finish_cleanup_try_block (stmt);
14054 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14055 }
14056 else
14057 {
14058 tree compound_stmt = NULL_TREE;
14059
14060 if (FN_TRY_BLOCK_P (t))
14061 stmt = begin_function_try_block (&compound_stmt);
14062 else
14063 stmt = begin_try_block ();
14064
14065 RECUR (TRY_STMTS (t));
14066
14067 if (FN_TRY_BLOCK_P (t))
14068 finish_function_try_block (stmt);
14069 else
14070 finish_try_block (stmt);
14071
14072 RECUR (TRY_HANDLERS (t));
14073 if (FN_TRY_BLOCK_P (t))
14074 finish_function_handler_sequence (stmt, compound_stmt);
14075 else
14076 finish_handler_sequence (stmt);
14077 }
14078 break;
14079
14080 case HANDLER:
14081 {
14082 tree decl = HANDLER_PARMS (t);
14083
14084 if (decl)
14085 {
14086 decl = tsubst (decl, args, complain, in_decl);
14087 /* Prevent instantiate_decl from trying to instantiate
14088 this variable. We've already done all that needs to be
14089 done. */
14090 if (decl != error_mark_node)
14091 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14092 }
14093 stmt = begin_handler ();
14094 finish_handler_parms (decl, stmt);
14095 RECUR (HANDLER_BODY (t));
14096 finish_handler (stmt);
14097 }
14098 break;
14099
14100 case TAG_DEFN:
14101 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14102 if (CLASS_TYPE_P (tmp))
14103 {
14104 /* Local classes are not independent templates; they are
14105 instantiated along with their containing function. And this
14106 way we don't have to deal with pushing out of one local class
14107 to instantiate a member of another local class. */
14108 tree fn;
14109 /* Closures are handled by the LAMBDA_EXPR. */
14110 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14111 complete_type (tmp);
14112 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14113 if (!DECL_ARTIFICIAL (fn))
14114 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14115 }
14116 break;
14117
14118 case STATIC_ASSERT:
14119 {
14120 tree condition;
14121
14122 ++c_inhibit_evaluation_warnings;
14123 condition =
14124 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14125 args,
14126 complain, in_decl,
14127 /*integral_constant_expression_p=*/true);
14128 --c_inhibit_evaluation_warnings;
14129
14130 finish_static_assert (condition,
14131 STATIC_ASSERT_MESSAGE (t),
14132 STATIC_ASSERT_SOURCE_LOCATION (t),
14133 /*member_p=*/false);
14134 }
14135 break;
14136
14137 case OMP_PARALLEL:
14138 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14139 args, complain, in_decl);
14140 stmt = begin_omp_parallel ();
14141 RECUR (OMP_PARALLEL_BODY (t));
14142 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14143 = OMP_PARALLEL_COMBINED (t);
14144 break;
14145
14146 case OMP_TASK:
14147 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14148 args, complain, in_decl);
14149 stmt = begin_omp_task ();
14150 RECUR (OMP_TASK_BODY (t));
14151 finish_omp_task (tmp, stmt);
14152 break;
14153
14154 case OMP_FOR:
14155 case OMP_SIMD:
14156 case CILK_SIMD:
14157 case CILK_FOR:
14158 case OMP_DISTRIBUTE:
14159 {
14160 tree clauses, body, pre_body;
14161 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14162 tree incrv = NULL_TREE;
14163 int i;
14164
14165 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14166 args, complain, in_decl);
14167 if (OMP_FOR_INIT (t) != NULL_TREE)
14168 {
14169 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14170 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14171 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14172 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14173 }
14174
14175 stmt = begin_omp_structured_block ();
14176
14177 pre_body = push_stmt_list ();
14178 RECUR (OMP_FOR_PRE_BODY (t));
14179 pre_body = pop_stmt_list (pre_body);
14180
14181 if (OMP_FOR_INIT (t) != NULL_TREE)
14182 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14183 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14184 &clauses, args, complain, in_decl,
14185 integral_constant_expression_p);
14186
14187 body = push_stmt_list ();
14188 RECUR (OMP_FOR_BODY (t));
14189 body = pop_stmt_list (body);
14190
14191 if (OMP_FOR_INIT (t) != NULL_TREE)
14192 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14193 condv, incrv, body, pre_body, clauses);
14194 else
14195 {
14196 t = make_node (TREE_CODE (t));
14197 TREE_TYPE (t) = void_type_node;
14198 OMP_FOR_BODY (t) = body;
14199 OMP_FOR_PRE_BODY (t) = pre_body;
14200 OMP_FOR_CLAUSES (t) = clauses;
14201 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14202 add_stmt (t);
14203 }
14204
14205 add_stmt (finish_omp_structured_block (stmt));
14206 }
14207 break;
14208
14209 case OMP_SECTIONS:
14210 case OMP_SINGLE:
14211 case OMP_TEAMS:
14212 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14213 args, complain, in_decl);
14214 stmt = push_stmt_list ();
14215 RECUR (OMP_BODY (t));
14216 stmt = pop_stmt_list (stmt);
14217
14218 t = copy_node (t);
14219 OMP_BODY (t) = stmt;
14220 OMP_CLAUSES (t) = tmp;
14221 add_stmt (t);
14222 break;
14223
14224 case OMP_TARGET_DATA:
14225 case OMP_TARGET:
14226 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14227 args, complain, in_decl);
14228 keep_next_level (true);
14229 stmt = begin_omp_structured_block ();
14230
14231 RECUR (OMP_BODY (t));
14232 stmt = finish_omp_structured_block (stmt);
14233
14234 t = copy_node (t);
14235 OMP_BODY (t) = stmt;
14236 OMP_CLAUSES (t) = tmp;
14237 add_stmt (t);
14238 break;
14239
14240 case OMP_TARGET_UPDATE:
14241 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14242 args, complain, in_decl);
14243 t = copy_node (t);
14244 OMP_TARGET_UPDATE_CLAUSES (t) = tmp;
14245 add_stmt (t);
14246 break;
14247
14248 case OMP_SECTION:
14249 case OMP_CRITICAL:
14250 case OMP_MASTER:
14251 case OMP_TASKGROUP:
14252 case OMP_ORDERED:
14253 stmt = push_stmt_list ();
14254 RECUR (OMP_BODY (t));
14255 stmt = pop_stmt_list (stmt);
14256
14257 t = copy_node (t);
14258 OMP_BODY (t) = stmt;
14259 add_stmt (t);
14260 break;
14261
14262 case OMP_ATOMIC:
14263 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14264 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14265 {
14266 tree op1 = TREE_OPERAND (t, 1);
14267 tree rhs1 = NULL_TREE;
14268 tree lhs, rhs;
14269 if (TREE_CODE (op1) == COMPOUND_EXPR)
14270 {
14271 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14272 op1 = TREE_OPERAND (op1, 1);
14273 }
14274 lhs = RECUR (TREE_OPERAND (op1, 0));
14275 rhs = RECUR (TREE_OPERAND (op1, 1));
14276 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14277 NULL_TREE, NULL_TREE, rhs1,
14278 OMP_ATOMIC_SEQ_CST (t));
14279 }
14280 else
14281 {
14282 tree op1 = TREE_OPERAND (t, 1);
14283 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14284 tree rhs1 = NULL_TREE;
14285 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14286 enum tree_code opcode = NOP_EXPR;
14287 if (code == OMP_ATOMIC_READ)
14288 {
14289 v = RECUR (TREE_OPERAND (op1, 0));
14290 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14291 }
14292 else if (code == OMP_ATOMIC_CAPTURE_OLD
14293 || code == OMP_ATOMIC_CAPTURE_NEW)
14294 {
14295 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14296 v = RECUR (TREE_OPERAND (op1, 0));
14297 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14298 if (TREE_CODE (op11) == COMPOUND_EXPR)
14299 {
14300 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14301 op11 = TREE_OPERAND (op11, 1);
14302 }
14303 lhs = RECUR (TREE_OPERAND (op11, 0));
14304 rhs = RECUR (TREE_OPERAND (op11, 1));
14305 opcode = TREE_CODE (op11);
14306 if (opcode == MODIFY_EXPR)
14307 opcode = NOP_EXPR;
14308 }
14309 else
14310 {
14311 code = OMP_ATOMIC;
14312 lhs = RECUR (TREE_OPERAND (op1, 0));
14313 rhs = RECUR (TREE_OPERAND (op1, 1));
14314 }
14315 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14316 OMP_ATOMIC_SEQ_CST (t));
14317 }
14318 break;
14319
14320 case TRANSACTION_EXPR:
14321 {
14322 int flags = 0;
14323 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14324 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14325
14326 if (TRANSACTION_EXPR_IS_STMT (t))
14327 {
14328 tree body = TRANSACTION_EXPR_BODY (t);
14329 tree noex = NULL_TREE;
14330 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14331 {
14332 noex = MUST_NOT_THROW_COND (body);
14333 if (noex == NULL_TREE)
14334 noex = boolean_true_node;
14335 body = TREE_OPERAND (body, 0);
14336 }
14337 stmt = begin_transaction_stmt (input_location, NULL, flags);
14338 RECUR (body);
14339 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14340 }
14341 else
14342 {
14343 stmt = build_transaction_expr (EXPR_LOCATION (t),
14344 RECUR (TRANSACTION_EXPR_BODY (t)),
14345 flags, NULL_TREE);
14346 RETURN (stmt);
14347 }
14348 }
14349 break;
14350
14351 case MUST_NOT_THROW_EXPR:
14352 {
14353 tree op0 = RECUR (TREE_OPERAND (t, 0));
14354 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14355 RETURN (build_must_not_throw_expr (op0, cond));
14356 }
14357
14358 case EXPR_PACK_EXPANSION:
14359 error ("invalid use of pack expansion expression");
14360 RETURN (error_mark_node);
14361
14362 case NONTYPE_ARGUMENT_PACK:
14363 error ("use %<...%> to expand argument pack");
14364 RETURN (error_mark_node);
14365
14366 case CILK_SPAWN_STMT:
14367 cfun->calls_cilk_spawn = 1;
14368 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14369
14370 case CILK_SYNC_STMT:
14371 RETURN (build_cilk_sync ());
14372
14373 case COMPOUND_EXPR:
14374 tmp = RECUR (TREE_OPERAND (t, 0));
14375 if (tmp == NULL_TREE)
14376 /* If the first operand was a statement, we're done with it. */
14377 RETURN (RECUR (TREE_OPERAND (t, 1)));
14378 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14379 RECUR (TREE_OPERAND (t, 1)),
14380 complain));
14381
14382 case ANNOTATE_EXPR:
14383 tmp = RECUR (TREE_OPERAND (t, 0));
14384 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14385 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14386
14387 default:
14388 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14389
14390 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14391 /*function_p=*/false,
14392 integral_constant_expression_p));
14393 }
14394
14395 RETURN (NULL_TREE);
14396 out:
14397 input_location = loc;
14398 return r;
14399 #undef RECUR
14400 #undef RETURN
14401 }
14402
14403 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14404 function. For description of the body see comment above
14405 cp_parser_omp_declare_reduction_exprs. */
14406
14407 static void
14408 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14409 {
14410 if (t == NULL_TREE || t == error_mark_node)
14411 return;
14412
14413 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14414
14415 tree_stmt_iterator tsi;
14416 int i;
14417 tree stmts[7];
14418 memset (stmts, 0, sizeof stmts);
14419 for (i = 0, tsi = tsi_start (t);
14420 i < 7 && !tsi_end_p (tsi);
14421 i++, tsi_next (&tsi))
14422 stmts[i] = tsi_stmt (tsi);
14423 gcc_assert (tsi_end_p (tsi));
14424
14425 if (i >= 3)
14426 {
14427 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14428 && TREE_CODE (stmts[1]) == DECL_EXPR);
14429 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14430 args, complain, in_decl);
14431 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14432 args, complain, in_decl);
14433 DECL_CONTEXT (omp_out) = current_function_decl;
14434 DECL_CONTEXT (omp_in) = current_function_decl;
14435 keep_next_level (true);
14436 tree block = begin_omp_structured_block ();
14437 tsubst_expr (stmts[2], args, complain, in_decl, false);
14438 block = finish_omp_structured_block (block);
14439 block = maybe_cleanup_point_expr_void (block);
14440 add_decl_expr (omp_out);
14441 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14442 TREE_NO_WARNING (omp_out) = 1;
14443 add_decl_expr (omp_in);
14444 finish_expr_stmt (block);
14445 }
14446 if (i >= 6)
14447 {
14448 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14449 && TREE_CODE (stmts[4]) == DECL_EXPR);
14450 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14451 args, complain, in_decl);
14452 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14453 args, complain, in_decl);
14454 DECL_CONTEXT (omp_priv) = current_function_decl;
14455 DECL_CONTEXT (omp_orig) = current_function_decl;
14456 keep_next_level (true);
14457 tree block = begin_omp_structured_block ();
14458 tsubst_expr (stmts[5], args, complain, in_decl, false);
14459 block = finish_omp_structured_block (block);
14460 block = maybe_cleanup_point_expr_void (block);
14461 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14462 add_decl_expr (omp_priv);
14463 add_decl_expr (omp_orig);
14464 finish_expr_stmt (block);
14465 if (i == 7)
14466 add_decl_expr (omp_orig);
14467 }
14468 }
14469
14470 /* T is a postfix-expression that is not being used in a function
14471 call. Return the substituted version of T. */
14472
14473 static tree
14474 tsubst_non_call_postfix_expression (tree t, tree args,
14475 tsubst_flags_t complain,
14476 tree in_decl)
14477 {
14478 if (TREE_CODE (t) == SCOPE_REF)
14479 t = tsubst_qualified_id (t, args, complain, in_decl,
14480 /*done=*/false, /*address_p=*/false);
14481 else
14482 t = tsubst_copy_and_build (t, args, complain, in_decl,
14483 /*function_p=*/false,
14484 /*integral_constant_expression_p=*/false);
14485
14486 return t;
14487 }
14488
14489 /* Like tsubst but deals with expressions and performs semantic
14490 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14491
14492 tree
14493 tsubst_copy_and_build (tree t,
14494 tree args,
14495 tsubst_flags_t complain,
14496 tree in_decl,
14497 bool function_p,
14498 bool integral_constant_expression_p)
14499 {
14500 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14501 #define RECUR(NODE) \
14502 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14503 /*function_p=*/false, \
14504 integral_constant_expression_p)
14505
14506 tree retval, op1;
14507 location_t loc;
14508
14509 if (t == NULL_TREE || t == error_mark_node)
14510 return t;
14511
14512 loc = input_location;
14513 if (EXPR_HAS_LOCATION (t))
14514 input_location = EXPR_LOCATION (t);
14515
14516 /* N3276 decltype magic only applies to calls at the top level or on the
14517 right side of a comma. */
14518 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14519 complain &= ~tf_decltype;
14520
14521 switch (TREE_CODE (t))
14522 {
14523 case USING_DECL:
14524 t = DECL_NAME (t);
14525 /* Fall through. */
14526 case IDENTIFIER_NODE:
14527 {
14528 tree decl;
14529 cp_id_kind idk;
14530 bool non_integral_constant_expression_p;
14531 const char *error_msg;
14532
14533 if (IDENTIFIER_TYPENAME_P (t))
14534 {
14535 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14536 t = mangle_conv_op_name_for_type (new_type);
14537 }
14538
14539 /* Look up the name. */
14540 decl = lookup_name (t);
14541
14542 /* By convention, expressions use ERROR_MARK_NODE to indicate
14543 failure, not NULL_TREE. */
14544 if (decl == NULL_TREE)
14545 decl = error_mark_node;
14546
14547 decl = finish_id_expression (t, decl, NULL_TREE,
14548 &idk,
14549 integral_constant_expression_p,
14550 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14551 &non_integral_constant_expression_p,
14552 /*template_p=*/false,
14553 /*done=*/true,
14554 /*address_p=*/false,
14555 /*template_arg_p=*/false,
14556 &error_msg,
14557 input_location);
14558 if (error_msg)
14559 error (error_msg);
14560 if (!function_p && identifier_p (decl))
14561 {
14562 if (complain & tf_error)
14563 unqualified_name_lookup_error (decl);
14564 decl = error_mark_node;
14565 }
14566 RETURN (decl);
14567 }
14568
14569 case TEMPLATE_ID_EXPR:
14570 {
14571 tree object;
14572 tree templ = RECUR (TREE_OPERAND (t, 0));
14573 tree targs = TREE_OPERAND (t, 1);
14574
14575 if (targs)
14576 targs = tsubst_template_args (targs, args, complain, in_decl);
14577
14578 if (TREE_CODE (templ) == COMPONENT_REF)
14579 {
14580 object = TREE_OPERAND (templ, 0);
14581 templ = TREE_OPERAND (templ, 1);
14582 }
14583 else
14584 object = NULL_TREE;
14585 templ = lookup_template_function (templ, targs);
14586
14587 if (object)
14588 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14589 object, templ, NULL_TREE));
14590 else
14591 RETURN (baselink_for_fns (templ));
14592 }
14593
14594 case INDIRECT_REF:
14595 {
14596 tree r = RECUR (TREE_OPERAND (t, 0));
14597
14598 if (REFERENCE_REF_P (t))
14599 {
14600 /* A type conversion to reference type will be enclosed in
14601 such an indirect ref, but the substitution of the cast
14602 will have also added such an indirect ref. */
14603 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14604 r = convert_from_reference (r);
14605 }
14606 else
14607 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14608 complain|decltype_flag);
14609 RETURN (r);
14610 }
14611
14612 case NOP_EXPR:
14613 {
14614 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14615 tree op0 = RECUR (TREE_OPERAND (t, 0));
14616 RETURN (build_nop (type, op0));
14617 }
14618
14619 case IMPLICIT_CONV_EXPR:
14620 {
14621 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14622 tree expr = RECUR (TREE_OPERAND (t, 0));
14623 int flags = LOOKUP_IMPLICIT;
14624 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14625 flags = LOOKUP_NORMAL;
14626 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14627 flags));
14628 }
14629
14630 case CONVERT_EXPR:
14631 {
14632 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14633 tree op0 = RECUR (TREE_OPERAND (t, 0));
14634 RETURN (build1 (CONVERT_EXPR, type, op0));
14635 }
14636
14637 case CAST_EXPR:
14638 case REINTERPRET_CAST_EXPR:
14639 case CONST_CAST_EXPR:
14640 case DYNAMIC_CAST_EXPR:
14641 case STATIC_CAST_EXPR:
14642 {
14643 tree type;
14644 tree op, r = NULL_TREE;
14645
14646 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14647 if (integral_constant_expression_p
14648 && !cast_valid_in_integral_constant_expression_p (type))
14649 {
14650 if (complain & tf_error)
14651 error ("a cast to a type other than an integral or "
14652 "enumeration type cannot appear in a constant-expression");
14653 RETURN (error_mark_node);
14654 }
14655
14656 op = RECUR (TREE_OPERAND (t, 0));
14657
14658 warning_sentinel s(warn_useless_cast);
14659 switch (TREE_CODE (t))
14660 {
14661 case CAST_EXPR:
14662 r = build_functional_cast (type, op, complain);
14663 break;
14664 case REINTERPRET_CAST_EXPR:
14665 r = build_reinterpret_cast (type, op, complain);
14666 break;
14667 case CONST_CAST_EXPR:
14668 r = build_const_cast (type, op, complain);
14669 break;
14670 case DYNAMIC_CAST_EXPR:
14671 r = build_dynamic_cast (type, op, complain);
14672 break;
14673 case STATIC_CAST_EXPR:
14674 r = build_static_cast (type, op, complain);
14675 break;
14676 default:
14677 gcc_unreachable ();
14678 }
14679
14680 RETURN (r);
14681 }
14682
14683 case POSTDECREMENT_EXPR:
14684 case POSTINCREMENT_EXPR:
14685 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14686 args, complain, in_decl);
14687 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14688 complain|decltype_flag));
14689
14690 case PREDECREMENT_EXPR:
14691 case PREINCREMENT_EXPR:
14692 case NEGATE_EXPR:
14693 case BIT_NOT_EXPR:
14694 case ABS_EXPR:
14695 case TRUTH_NOT_EXPR:
14696 case UNARY_PLUS_EXPR: /* Unary + */
14697 case REALPART_EXPR:
14698 case IMAGPART_EXPR:
14699 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14700 RECUR (TREE_OPERAND (t, 0)),
14701 complain|decltype_flag));
14702
14703 case FIX_TRUNC_EXPR:
14704 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14705 0, complain));
14706
14707 case ADDR_EXPR:
14708 op1 = TREE_OPERAND (t, 0);
14709 if (TREE_CODE (op1) == LABEL_DECL)
14710 RETURN (finish_label_address_expr (DECL_NAME (op1),
14711 EXPR_LOCATION (op1)));
14712 if (TREE_CODE (op1) == SCOPE_REF)
14713 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14714 /*done=*/true, /*address_p=*/true);
14715 else
14716 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14717 in_decl);
14718 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14719 complain|decltype_flag));
14720
14721 case PLUS_EXPR:
14722 case MINUS_EXPR:
14723 case MULT_EXPR:
14724 case TRUNC_DIV_EXPR:
14725 case CEIL_DIV_EXPR:
14726 case FLOOR_DIV_EXPR:
14727 case ROUND_DIV_EXPR:
14728 case EXACT_DIV_EXPR:
14729 case BIT_AND_EXPR:
14730 case BIT_IOR_EXPR:
14731 case BIT_XOR_EXPR:
14732 case TRUNC_MOD_EXPR:
14733 case FLOOR_MOD_EXPR:
14734 case TRUTH_ANDIF_EXPR:
14735 case TRUTH_ORIF_EXPR:
14736 case TRUTH_AND_EXPR:
14737 case TRUTH_OR_EXPR:
14738 case RSHIFT_EXPR:
14739 case LSHIFT_EXPR:
14740 case RROTATE_EXPR:
14741 case LROTATE_EXPR:
14742 case EQ_EXPR:
14743 case NE_EXPR:
14744 case MAX_EXPR:
14745 case MIN_EXPR:
14746 case LE_EXPR:
14747 case GE_EXPR:
14748 case LT_EXPR:
14749 case GT_EXPR:
14750 case MEMBER_REF:
14751 case DOTSTAR_EXPR:
14752 {
14753 warning_sentinel s1(warn_type_limits);
14754 warning_sentinel s2(warn_div_by_zero);
14755 tree op0 = RECUR (TREE_OPERAND (t, 0));
14756 tree op1 = RECUR (TREE_OPERAND (t, 1));
14757 tree r = build_x_binary_op
14758 (input_location, TREE_CODE (t),
14759 op0,
14760 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14761 ? ERROR_MARK
14762 : TREE_CODE (TREE_OPERAND (t, 0))),
14763 op1,
14764 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14765 ? ERROR_MARK
14766 : TREE_CODE (TREE_OPERAND (t, 1))),
14767 /*overload=*/NULL,
14768 complain|decltype_flag);
14769 if (EXPR_P (r) && TREE_NO_WARNING (t))
14770 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14771
14772 RETURN (r);
14773 }
14774
14775 case POINTER_PLUS_EXPR:
14776 {
14777 tree op0 = RECUR (TREE_OPERAND (t, 0));
14778 tree op1 = RECUR (TREE_OPERAND (t, 1));
14779 return fold_build_pointer_plus (op0, op1);
14780 }
14781
14782 case SCOPE_REF:
14783 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14784 /*address_p=*/false));
14785 case ARRAY_REF:
14786 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14787 args, complain, in_decl);
14788 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14789 RECUR (TREE_OPERAND (t, 1)),
14790 complain|decltype_flag));
14791
14792 case ARRAY_NOTATION_REF:
14793 {
14794 tree start_index, length, stride;
14795 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14796 args, complain, in_decl);
14797 start_index = RECUR (ARRAY_NOTATION_START (t));
14798 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14799 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14800 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14801 length, stride, TREE_TYPE (op1)));
14802 }
14803 case SIZEOF_EXPR:
14804 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14805 RETURN (tsubst_copy (t, args, complain, in_decl));
14806 /* Fall through */
14807
14808 case ALIGNOF_EXPR:
14809 {
14810 tree r;
14811
14812 op1 = TREE_OPERAND (t, 0);
14813 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14814 op1 = TREE_TYPE (op1);
14815 if (!args)
14816 {
14817 /* When there are no ARGS, we are trying to evaluate a
14818 non-dependent expression from the parser. Trying to do
14819 the substitutions may not work. */
14820 if (!TYPE_P (op1))
14821 op1 = TREE_TYPE (op1);
14822 }
14823 else
14824 {
14825 ++cp_unevaluated_operand;
14826 ++c_inhibit_evaluation_warnings;
14827 if (TYPE_P (op1))
14828 op1 = tsubst (op1, args, complain, in_decl);
14829 else
14830 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14831 /*function_p=*/false,
14832 /*integral_constant_expression_p=*/
14833 false);
14834 --cp_unevaluated_operand;
14835 --c_inhibit_evaluation_warnings;
14836 }
14837 if (TYPE_P (op1))
14838 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14839 complain & tf_error);
14840 else
14841 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14842 complain & tf_error);
14843 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14844 {
14845 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14846 {
14847 if (!processing_template_decl && TYPE_P (op1))
14848 {
14849 r = build_min (SIZEOF_EXPR, size_type_node,
14850 build1 (NOP_EXPR, op1, error_mark_node));
14851 SIZEOF_EXPR_TYPE_P (r) = 1;
14852 }
14853 else
14854 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14855 TREE_SIDE_EFFECTS (r) = 0;
14856 TREE_READONLY (r) = 1;
14857 }
14858 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14859 }
14860 RETURN (r);
14861 }
14862
14863 case AT_ENCODE_EXPR:
14864 {
14865 op1 = TREE_OPERAND (t, 0);
14866 ++cp_unevaluated_operand;
14867 ++c_inhibit_evaluation_warnings;
14868 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14869 /*function_p=*/false,
14870 /*integral_constant_expression_p=*/false);
14871 --cp_unevaluated_operand;
14872 --c_inhibit_evaluation_warnings;
14873 RETURN (objc_build_encode_expr (op1));
14874 }
14875
14876 case NOEXCEPT_EXPR:
14877 op1 = TREE_OPERAND (t, 0);
14878 ++cp_unevaluated_operand;
14879 ++c_inhibit_evaluation_warnings;
14880 ++cp_noexcept_operand;
14881 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14882 /*function_p=*/false,
14883 /*integral_constant_expression_p=*/false);
14884 --cp_unevaluated_operand;
14885 --c_inhibit_evaluation_warnings;
14886 --cp_noexcept_operand;
14887 RETURN (finish_noexcept_expr (op1, complain));
14888
14889 case MODOP_EXPR:
14890 {
14891 warning_sentinel s(warn_div_by_zero);
14892 tree lhs = RECUR (TREE_OPERAND (t, 0));
14893 tree rhs = RECUR (TREE_OPERAND (t, 2));
14894 tree r = build_x_modify_expr
14895 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
14896 complain|decltype_flag);
14897 /* TREE_NO_WARNING must be set if either the expression was
14898 parenthesized or it uses an operator such as >>= rather
14899 than plain assignment. In the former case, it was already
14900 set and must be copied. In the latter case,
14901 build_x_modify_expr sets it and it must not be reset
14902 here. */
14903 if (TREE_NO_WARNING (t))
14904 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14905
14906 RETURN (r);
14907 }
14908
14909 case ARROW_EXPR:
14910 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14911 args, complain, in_decl);
14912 /* Remember that there was a reference to this entity. */
14913 if (DECL_P (op1)
14914 && !mark_used (op1, complain) && !(complain & tf_error))
14915 RETURN (error_mark_node);
14916 RETURN (build_x_arrow (input_location, op1, complain));
14917
14918 case NEW_EXPR:
14919 {
14920 tree placement = RECUR (TREE_OPERAND (t, 0));
14921 tree init = RECUR (TREE_OPERAND (t, 3));
14922 vec<tree, va_gc> *placement_vec;
14923 vec<tree, va_gc> *init_vec;
14924 tree ret;
14925
14926 if (placement == NULL_TREE)
14927 placement_vec = NULL;
14928 else
14929 {
14930 placement_vec = make_tree_vector ();
14931 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14932 vec_safe_push (placement_vec, TREE_VALUE (placement));
14933 }
14934
14935 /* If there was an initializer in the original tree, but it
14936 instantiated to an empty list, then we should pass a
14937 non-NULL empty vector to tell build_new that it was an
14938 empty initializer() rather than no initializer. This can
14939 only happen when the initializer is a pack expansion whose
14940 parameter packs are of length zero. */
14941 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14942 init_vec = NULL;
14943 else
14944 {
14945 init_vec = make_tree_vector ();
14946 if (init == void_node)
14947 gcc_assert (init_vec != NULL);
14948 else
14949 {
14950 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14951 vec_safe_push (init_vec, TREE_VALUE (init));
14952 }
14953 }
14954
14955 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
14956 tree op2 = RECUR (TREE_OPERAND (t, 2));
14957 ret = build_new (&placement_vec, op1, op2, &init_vec,
14958 NEW_EXPR_USE_GLOBAL (t),
14959 complain);
14960
14961 if (placement_vec != NULL)
14962 release_tree_vector (placement_vec);
14963 if (init_vec != NULL)
14964 release_tree_vector (init_vec);
14965
14966 RETURN (ret);
14967 }
14968
14969 case DELETE_EXPR:
14970 {
14971 tree op0 = RECUR (TREE_OPERAND (t, 0));
14972 tree op1 = RECUR (TREE_OPERAND (t, 1));
14973 RETURN (delete_sanity (op0, op1,
14974 DELETE_EXPR_USE_VEC (t),
14975 DELETE_EXPR_USE_GLOBAL (t),
14976 complain));
14977 }
14978
14979 case COMPOUND_EXPR:
14980 {
14981 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14982 complain & ~tf_decltype, in_decl,
14983 /*function_p=*/false,
14984 integral_constant_expression_p);
14985 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14986 op0,
14987 RECUR (TREE_OPERAND (t, 1)),
14988 complain|decltype_flag));
14989 }
14990
14991 case CALL_EXPR:
14992 {
14993 tree function;
14994 vec<tree, va_gc> *call_args;
14995 unsigned int nargs, i;
14996 bool qualified_p;
14997 bool koenig_p;
14998 tree ret;
14999
15000 function = CALL_EXPR_FN (t);
15001 /* When we parsed the expression, we determined whether or
15002 not Koenig lookup should be performed. */
15003 koenig_p = KOENIG_LOOKUP_P (t);
15004 if (TREE_CODE (function) == SCOPE_REF)
15005 {
15006 qualified_p = true;
15007 function = tsubst_qualified_id (function, args, complain, in_decl,
15008 /*done=*/false,
15009 /*address_p=*/false);
15010 }
15011 else if (koenig_p && identifier_p (function))
15012 {
15013 /* Do nothing; calling tsubst_copy_and_build on an identifier
15014 would incorrectly perform unqualified lookup again.
15015
15016 Note that we can also have an IDENTIFIER_NODE if the earlier
15017 unqualified lookup found a member function; in that case
15018 koenig_p will be false and we do want to do the lookup
15019 again to find the instantiated member function.
15020
15021 FIXME but doing that causes c++/15272, so we need to stop
15022 using IDENTIFIER_NODE in that situation. */
15023 qualified_p = false;
15024 }
15025 else
15026 {
15027 if (TREE_CODE (function) == COMPONENT_REF)
15028 {
15029 tree op = TREE_OPERAND (function, 1);
15030
15031 qualified_p = (TREE_CODE (op) == SCOPE_REF
15032 || (BASELINK_P (op)
15033 && BASELINK_QUALIFIED_P (op)));
15034 }
15035 else
15036 qualified_p = false;
15037
15038 if (TREE_CODE (function) == ADDR_EXPR
15039 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15040 /* Avoid error about taking the address of a constructor. */
15041 function = TREE_OPERAND (function, 0);
15042
15043 function = tsubst_copy_and_build (function, args, complain,
15044 in_decl,
15045 !qualified_p,
15046 integral_constant_expression_p);
15047
15048 if (BASELINK_P (function))
15049 qualified_p = true;
15050 }
15051
15052 nargs = call_expr_nargs (t);
15053 call_args = make_tree_vector ();
15054 for (i = 0; i < nargs; ++i)
15055 {
15056 tree arg = CALL_EXPR_ARG (t, i);
15057
15058 if (!PACK_EXPANSION_P (arg))
15059 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15060 else
15061 {
15062 /* Expand the pack expansion and push each entry onto
15063 CALL_ARGS. */
15064 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15065 if (TREE_CODE (arg) == TREE_VEC)
15066 {
15067 unsigned int len, j;
15068
15069 len = TREE_VEC_LENGTH (arg);
15070 for (j = 0; j < len; ++j)
15071 {
15072 tree value = TREE_VEC_ELT (arg, j);
15073 if (value != NULL_TREE)
15074 value = convert_from_reference (value);
15075 vec_safe_push (call_args, value);
15076 }
15077 }
15078 else
15079 {
15080 /* A partial substitution. Add one entry. */
15081 vec_safe_push (call_args, arg);
15082 }
15083 }
15084 }
15085
15086 /* We do not perform argument-dependent lookup if normal
15087 lookup finds a non-function, in accordance with the
15088 expected resolution of DR 218. */
15089 if (koenig_p
15090 && ((is_overloaded_fn (function)
15091 /* If lookup found a member function, the Koenig lookup is
15092 not appropriate, even if an unqualified-name was used
15093 to denote the function. */
15094 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15095 || identifier_p (function))
15096 /* Only do this when substitution turns a dependent call
15097 into a non-dependent call. */
15098 && type_dependent_expression_p_push (t)
15099 && !any_type_dependent_arguments_p (call_args))
15100 function = perform_koenig_lookup (function, call_args, tf_none);
15101
15102 if (identifier_p (function)
15103 && !any_type_dependent_arguments_p (call_args))
15104 {
15105 if (koenig_p && (complain & tf_warning_or_error))
15106 {
15107 /* For backwards compatibility and good diagnostics, try
15108 the unqualified lookup again if we aren't in SFINAE
15109 context. */
15110 tree unq = (tsubst_copy_and_build
15111 (function, args, complain, in_decl, true,
15112 integral_constant_expression_p));
15113 if (unq == error_mark_node)
15114 RETURN (error_mark_node);
15115
15116 if (unq != function)
15117 {
15118 tree fn = unq;
15119 if (INDIRECT_REF_P (fn))
15120 fn = TREE_OPERAND (fn, 0);
15121 if (TREE_CODE (fn) == COMPONENT_REF)
15122 fn = TREE_OPERAND (fn, 1);
15123 if (is_overloaded_fn (fn))
15124 fn = get_first_fn (fn);
15125 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15126 "%qD was not declared in this scope, "
15127 "and no declarations were found by "
15128 "argument-dependent lookup at the point "
15129 "of instantiation", function))
15130 {
15131 if (!DECL_P (fn))
15132 /* Can't say anything more. */;
15133 else if (DECL_CLASS_SCOPE_P (fn))
15134 {
15135 location_t loc = EXPR_LOC_OR_LOC (t,
15136 input_location);
15137 inform (loc,
15138 "declarations in dependent base %qT are "
15139 "not found by unqualified lookup",
15140 DECL_CLASS_CONTEXT (fn));
15141 if (current_class_ptr)
15142 inform (loc,
15143 "use %<this->%D%> instead", function);
15144 else
15145 inform (loc,
15146 "use %<%T::%D%> instead",
15147 current_class_name, function);
15148 }
15149 else
15150 inform (0, "%q+D declared here, later in the "
15151 "translation unit", fn);
15152 }
15153 function = unq;
15154 }
15155 }
15156 if (identifier_p (function))
15157 {
15158 if (complain & tf_error)
15159 unqualified_name_lookup_error (function);
15160 release_tree_vector (call_args);
15161 RETURN (error_mark_node);
15162 }
15163 }
15164
15165 /* Remember that there was a reference to this entity. */
15166 if (DECL_P (function)
15167 && !mark_used (function, complain) && !(complain & tf_error))
15168 RETURN (error_mark_node);
15169
15170 /* Put back tf_decltype for the actual call. */
15171 complain |= decltype_flag;
15172
15173 if (TREE_CODE (function) == OFFSET_REF)
15174 ret = build_offset_ref_call_from_tree (function, &call_args,
15175 complain);
15176 else if (TREE_CODE (function) == COMPONENT_REF)
15177 {
15178 tree instance = TREE_OPERAND (function, 0);
15179 tree fn = TREE_OPERAND (function, 1);
15180
15181 if (processing_template_decl
15182 && (type_dependent_expression_p (instance)
15183 || (!BASELINK_P (fn)
15184 && TREE_CODE (fn) != FIELD_DECL)
15185 || type_dependent_expression_p (fn)
15186 || any_type_dependent_arguments_p (call_args)))
15187 ret = build_nt_call_vec (function, call_args);
15188 else if (!BASELINK_P (fn))
15189 ret = finish_call_expr (function, &call_args,
15190 /*disallow_virtual=*/false,
15191 /*koenig_p=*/false,
15192 complain);
15193 else
15194 ret = (build_new_method_call
15195 (instance, fn,
15196 &call_args, NULL_TREE,
15197 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15198 /*fn_p=*/NULL,
15199 complain));
15200 }
15201 else
15202 ret = finish_call_expr (function, &call_args,
15203 /*disallow_virtual=*/qualified_p,
15204 koenig_p,
15205 complain);
15206
15207 release_tree_vector (call_args);
15208
15209 RETURN (ret);
15210 }
15211
15212 case COND_EXPR:
15213 {
15214 tree cond = RECUR (TREE_OPERAND (t, 0));
15215 tree folded_cond = fold_non_dependent_expr (cond);
15216 tree exp1, exp2;
15217
15218 if (TREE_CODE (folded_cond) == INTEGER_CST)
15219 {
15220 if (integer_zerop (folded_cond))
15221 {
15222 ++c_inhibit_evaluation_warnings;
15223 exp1 = RECUR (TREE_OPERAND (t, 1));
15224 --c_inhibit_evaluation_warnings;
15225 exp2 = RECUR (TREE_OPERAND (t, 2));
15226 }
15227 else
15228 {
15229 exp1 = RECUR (TREE_OPERAND (t, 1));
15230 ++c_inhibit_evaluation_warnings;
15231 exp2 = RECUR (TREE_OPERAND (t, 2));
15232 --c_inhibit_evaluation_warnings;
15233 }
15234 cond = folded_cond;
15235 }
15236 else
15237 {
15238 exp1 = RECUR (TREE_OPERAND (t, 1));
15239 exp2 = RECUR (TREE_OPERAND (t, 2));
15240 }
15241
15242 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15243 cond, exp1, exp2, complain));
15244 }
15245
15246 case PSEUDO_DTOR_EXPR:
15247 {
15248 tree op0 = RECUR (TREE_OPERAND (t, 0));
15249 tree op1 = RECUR (TREE_OPERAND (t, 1));
15250 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15251 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15252 input_location));
15253 }
15254
15255 case TREE_LIST:
15256 {
15257 tree purpose, value, chain;
15258
15259 if (t == void_list_node)
15260 RETURN (t);
15261
15262 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15263 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15264 {
15265 /* We have pack expansions, so expand those and
15266 create a new list out of it. */
15267 tree purposevec = NULL_TREE;
15268 tree valuevec = NULL_TREE;
15269 tree chain;
15270 int i, len = -1;
15271
15272 /* Expand the argument expressions. */
15273 if (TREE_PURPOSE (t))
15274 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15275 complain, in_decl);
15276 if (TREE_VALUE (t))
15277 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15278 complain, in_decl);
15279
15280 /* Build the rest of the list. */
15281 chain = TREE_CHAIN (t);
15282 if (chain && chain != void_type_node)
15283 chain = RECUR (chain);
15284
15285 /* Determine the number of arguments. */
15286 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15287 {
15288 len = TREE_VEC_LENGTH (purposevec);
15289 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15290 }
15291 else if (TREE_CODE (valuevec) == TREE_VEC)
15292 len = TREE_VEC_LENGTH (valuevec);
15293 else
15294 {
15295 /* Since we only performed a partial substitution into
15296 the argument pack, we only RETURN (a single list
15297 node. */
15298 if (purposevec == TREE_PURPOSE (t)
15299 && valuevec == TREE_VALUE (t)
15300 && chain == TREE_CHAIN (t))
15301 RETURN (t);
15302
15303 RETURN (tree_cons (purposevec, valuevec, chain));
15304 }
15305
15306 /* Convert the argument vectors into a TREE_LIST */
15307 i = len;
15308 while (i > 0)
15309 {
15310 /* Grab the Ith values. */
15311 i--;
15312 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15313 : NULL_TREE;
15314 value
15315 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15316 : NULL_TREE;
15317
15318 /* Build the list (backwards). */
15319 chain = tree_cons (purpose, value, chain);
15320 }
15321
15322 RETURN (chain);
15323 }
15324
15325 purpose = TREE_PURPOSE (t);
15326 if (purpose)
15327 purpose = RECUR (purpose);
15328 value = TREE_VALUE (t);
15329 if (value)
15330 value = RECUR (value);
15331 chain = TREE_CHAIN (t);
15332 if (chain && chain != void_type_node)
15333 chain = RECUR (chain);
15334 if (purpose == TREE_PURPOSE (t)
15335 && value == TREE_VALUE (t)
15336 && chain == TREE_CHAIN (t))
15337 RETURN (t);
15338 RETURN (tree_cons (purpose, value, chain));
15339 }
15340
15341 case COMPONENT_REF:
15342 {
15343 tree object;
15344 tree object_type;
15345 tree member;
15346 tree r;
15347
15348 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15349 args, complain, in_decl);
15350 /* Remember that there was a reference to this entity. */
15351 if (DECL_P (object)
15352 && !mark_used (object, complain) && !(complain & tf_error))
15353 RETURN (error_mark_node);
15354 object_type = TREE_TYPE (object);
15355
15356 member = TREE_OPERAND (t, 1);
15357 if (BASELINK_P (member))
15358 member = tsubst_baselink (member,
15359 non_reference (TREE_TYPE (object)),
15360 args, complain, in_decl);
15361 else
15362 member = tsubst_copy (member, args, complain, in_decl);
15363 if (member == error_mark_node)
15364 RETURN (error_mark_node);
15365
15366 if (type_dependent_expression_p (object))
15367 /* We can't do much here. */;
15368 else if (!CLASS_TYPE_P (object_type))
15369 {
15370 if (scalarish_type_p (object_type))
15371 {
15372 tree s = NULL_TREE;
15373 tree dtor = member;
15374
15375 if (TREE_CODE (dtor) == SCOPE_REF)
15376 {
15377 s = TREE_OPERAND (dtor, 0);
15378 dtor = TREE_OPERAND (dtor, 1);
15379 }
15380 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15381 {
15382 dtor = TREE_OPERAND (dtor, 0);
15383 if (TYPE_P (dtor))
15384 RETURN (finish_pseudo_destructor_expr
15385 (object, s, dtor, input_location));
15386 }
15387 }
15388 }
15389 else if (TREE_CODE (member) == SCOPE_REF
15390 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15391 {
15392 /* Lookup the template functions now that we know what the
15393 scope is. */
15394 tree scope = TREE_OPERAND (member, 0);
15395 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15396 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15397 member = lookup_qualified_name (scope, tmpl,
15398 /*is_type_p=*/false,
15399 /*complain=*/false);
15400 if (BASELINK_P (member))
15401 {
15402 BASELINK_FUNCTIONS (member)
15403 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15404 args);
15405 member = (adjust_result_of_qualified_name_lookup
15406 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15407 object_type));
15408 }
15409 else
15410 {
15411 qualified_name_lookup_error (scope, tmpl, member,
15412 input_location);
15413 RETURN (error_mark_node);
15414 }
15415 }
15416 else if (TREE_CODE (member) == SCOPE_REF
15417 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15418 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15419 {
15420 if (complain & tf_error)
15421 {
15422 if (TYPE_P (TREE_OPERAND (member, 0)))
15423 error ("%qT is not a class or namespace",
15424 TREE_OPERAND (member, 0));
15425 else
15426 error ("%qD is not a class or namespace",
15427 TREE_OPERAND (member, 0));
15428 }
15429 RETURN (error_mark_node);
15430 }
15431 else if (TREE_CODE (member) == FIELD_DECL)
15432 {
15433 r = finish_non_static_data_member (member, object, NULL_TREE);
15434 if (TREE_CODE (r) == COMPONENT_REF)
15435 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15436 RETURN (r);
15437 }
15438
15439 r = finish_class_member_access_expr (object, member,
15440 /*template_p=*/false,
15441 complain);
15442 if (TREE_CODE (r) == COMPONENT_REF)
15443 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15444 RETURN (r);
15445 }
15446
15447 case THROW_EXPR:
15448 RETURN (build_throw
15449 (RECUR (TREE_OPERAND (t, 0))));
15450
15451 case CONSTRUCTOR:
15452 {
15453 vec<constructor_elt, va_gc> *n;
15454 constructor_elt *ce;
15455 unsigned HOST_WIDE_INT idx;
15456 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15457 bool process_index_p;
15458 int newlen;
15459 bool need_copy_p = false;
15460 tree r;
15461
15462 if (type == error_mark_node)
15463 RETURN (error_mark_node);
15464
15465 /* digest_init will do the wrong thing if we let it. */
15466 if (type && TYPE_PTRMEMFUNC_P (type))
15467 RETURN (t);
15468
15469 /* We do not want to process the index of aggregate
15470 initializers as they are identifier nodes which will be
15471 looked up by digest_init. */
15472 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15473
15474 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15475 newlen = vec_safe_length (n);
15476 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15477 {
15478 if (ce->index && process_index_p
15479 /* An identifier index is looked up in the type
15480 being initialized, not the current scope. */
15481 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15482 ce->index = RECUR (ce->index);
15483
15484 if (PACK_EXPANSION_P (ce->value))
15485 {
15486 /* Substitute into the pack expansion. */
15487 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15488 in_decl);
15489
15490 if (ce->value == error_mark_node
15491 || PACK_EXPANSION_P (ce->value))
15492 ;
15493 else if (TREE_VEC_LENGTH (ce->value) == 1)
15494 /* Just move the argument into place. */
15495 ce->value = TREE_VEC_ELT (ce->value, 0);
15496 else
15497 {
15498 /* Update the length of the final CONSTRUCTOR
15499 arguments vector, and note that we will need to
15500 copy.*/
15501 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15502 need_copy_p = true;
15503 }
15504 }
15505 else
15506 ce->value = RECUR (ce->value);
15507 }
15508
15509 if (need_copy_p)
15510 {
15511 vec<constructor_elt, va_gc> *old_n = n;
15512
15513 vec_alloc (n, newlen);
15514 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15515 {
15516 if (TREE_CODE (ce->value) == TREE_VEC)
15517 {
15518 int i, len = TREE_VEC_LENGTH (ce->value);
15519 for (i = 0; i < len; ++i)
15520 CONSTRUCTOR_APPEND_ELT (n, 0,
15521 TREE_VEC_ELT (ce->value, i));
15522 }
15523 else
15524 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15525 }
15526 }
15527
15528 r = build_constructor (init_list_type_node, n);
15529 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15530
15531 if (TREE_HAS_CONSTRUCTOR (t))
15532 RETURN (finish_compound_literal (type, r, complain));
15533
15534 TREE_TYPE (r) = type;
15535 RETURN (r);
15536 }
15537
15538 case TYPEID_EXPR:
15539 {
15540 tree operand_0 = TREE_OPERAND (t, 0);
15541 if (TYPE_P (operand_0))
15542 {
15543 operand_0 = tsubst (operand_0, args, complain, in_decl);
15544 RETURN (get_typeid (operand_0, complain));
15545 }
15546 else
15547 {
15548 operand_0 = RECUR (operand_0);
15549 RETURN (build_typeid (operand_0, complain));
15550 }
15551 }
15552
15553 case VAR_DECL:
15554 if (!args)
15555 RETURN (t);
15556 else if (DECL_PACK_P (t))
15557 {
15558 /* We don't build decls for an instantiation of a
15559 variadic capture proxy, we instantiate the elements
15560 when needed. */
15561 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15562 return RECUR (DECL_VALUE_EXPR (t));
15563 }
15564 /* Fall through */
15565
15566 case PARM_DECL:
15567 {
15568 tree r = tsubst_copy (t, args, complain, in_decl);
15569 /* ??? We're doing a subset of finish_id_expression here. */
15570 if (VAR_P (r)
15571 && !processing_template_decl
15572 && !cp_unevaluated_operand
15573 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
15574 && DECL_THREAD_LOCAL_P (r))
15575 {
15576 if (tree wrap = get_tls_wrapper_fn (r))
15577 /* Replace an evaluated use of the thread_local variable with
15578 a call to its wrapper. */
15579 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15580 }
15581 else if (outer_automatic_var_p (r))
15582 r = process_outer_var_ref (r, complain);
15583
15584 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15585 /* If the original type was a reference, we'll be wrapped in
15586 the appropriate INDIRECT_REF. */
15587 r = convert_from_reference (r);
15588 RETURN (r);
15589 }
15590
15591 case VA_ARG_EXPR:
15592 {
15593 tree op0 = RECUR (TREE_OPERAND (t, 0));
15594 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15595 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
15596 }
15597
15598 case OFFSETOF_EXPR:
15599 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
15600 EXPR_LOCATION (t)));
15601
15602 case TRAIT_EXPR:
15603 {
15604 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15605 complain, in_decl);
15606
15607 tree type2 = TRAIT_EXPR_TYPE2 (t);
15608 if (type2 && TREE_CODE (type2) == TREE_LIST)
15609 type2 = RECUR (type2);
15610 else if (type2)
15611 type2 = tsubst (type2, args, complain, in_decl);
15612
15613 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15614 }
15615
15616 case STMT_EXPR:
15617 {
15618 tree old_stmt_expr = cur_stmt_expr;
15619 tree stmt_expr = begin_stmt_expr ();
15620
15621 cur_stmt_expr = stmt_expr;
15622 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15623 integral_constant_expression_p);
15624 stmt_expr = finish_stmt_expr (stmt_expr, false);
15625 cur_stmt_expr = old_stmt_expr;
15626
15627 /* If the resulting list of expression statement is empty,
15628 fold it further into void_node. */
15629 if (empty_expr_stmt_p (stmt_expr))
15630 stmt_expr = void_node;
15631
15632 RETURN (stmt_expr);
15633 }
15634
15635 case LAMBDA_EXPR:
15636 {
15637 tree r = build_lambda_expr ();
15638
15639 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15640 LAMBDA_EXPR_CLOSURE (r) = type;
15641 CLASSTYPE_LAMBDA_EXPR (type) = r;
15642
15643 LAMBDA_EXPR_LOCATION (r)
15644 = LAMBDA_EXPR_LOCATION (t);
15645 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15646 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15647 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15648 LAMBDA_EXPR_DISCRIMINATOR (r)
15649 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15650 /* For a function scope, we want to use tsubst so that we don't
15651 complain about referring to an auto function before its return
15652 type has been deduced. Otherwise, we want to use tsubst_copy so
15653 that we look up the existing field/parameter/variable rather
15654 than build a new one. */
15655 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15656 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15657 scope = tsubst (scope, args, complain, in_decl);
15658 else if (scope && TREE_CODE (scope) == PARM_DECL)
15659 {
15660 /* Look up the parameter we want directly, as tsubst_copy
15661 doesn't do what we need. */
15662 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15663 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15664 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15665 parm = DECL_CHAIN (parm);
15666 scope = parm;
15667 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15668 if (DECL_CONTEXT (scope) == NULL_TREE)
15669 DECL_CONTEXT (scope) = fn;
15670 }
15671 else
15672 scope = RECUR (scope);
15673 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15674 LAMBDA_EXPR_RETURN_TYPE (r)
15675 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15676
15677 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15678 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15679
15680 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15681 determine_visibility (TYPE_NAME (type));
15682 /* Now that we know visibility, instantiate the type so we have a
15683 declaration of the op() for later calls to lambda_function. */
15684 complete_type (type);
15685
15686 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15687
15688 RETURN (build_lambda_object (r));
15689 }
15690
15691 case TARGET_EXPR:
15692 /* We can get here for a constant initializer of non-dependent type.
15693 FIXME stop folding in cp_parser_initializer_clause. */
15694 {
15695 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15696 complain);
15697 RETURN (r);
15698 }
15699
15700 case TRANSACTION_EXPR:
15701 RETURN (tsubst_expr(t, args, complain, in_decl,
15702 integral_constant_expression_p));
15703
15704 case PAREN_EXPR:
15705 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15706
15707 case VEC_PERM_EXPR:
15708 {
15709 tree op0 = RECUR (TREE_OPERAND (t, 0));
15710 tree op1 = RECUR (TREE_OPERAND (t, 1));
15711 tree op2 = RECUR (TREE_OPERAND (t, 2));
15712 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
15713 complain));
15714 }
15715
15716 default:
15717 /* Handle Objective-C++ constructs, if appropriate. */
15718 {
15719 tree subst
15720 = objcp_tsubst_copy_and_build (t, args, complain,
15721 in_decl, /*function_p=*/false);
15722 if (subst)
15723 RETURN (subst);
15724 }
15725 RETURN (tsubst_copy (t, args, complain, in_decl));
15726 }
15727
15728 #undef RECUR
15729 #undef RETURN
15730 out:
15731 input_location = loc;
15732 return retval;
15733 }
15734
15735 /* Verify that the instantiated ARGS are valid. For type arguments,
15736 make sure that the type's linkage is ok. For non-type arguments,
15737 make sure they are constants if they are integral or enumerations.
15738 Emit an error under control of COMPLAIN, and return TRUE on error. */
15739
15740 static bool
15741 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15742 {
15743 if (dependent_template_arg_p (t))
15744 return false;
15745 if (ARGUMENT_PACK_P (t))
15746 {
15747 tree vec = ARGUMENT_PACK_ARGS (t);
15748 int len = TREE_VEC_LENGTH (vec);
15749 bool result = false;
15750 int i;
15751
15752 for (i = 0; i < len; ++i)
15753 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15754 result = true;
15755 return result;
15756 }
15757 else if (TYPE_P (t))
15758 {
15759 /* [basic.link]: A name with no linkage (notably, the name
15760 of a class or enumeration declared in a local scope)
15761 shall not be used to declare an entity with linkage.
15762 This implies that names with no linkage cannot be used as
15763 template arguments
15764
15765 DR 757 relaxes this restriction for C++0x. */
15766 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15767 : no_linkage_check (t, /*relaxed_p=*/false));
15768
15769 if (nt)
15770 {
15771 /* DR 488 makes use of a type with no linkage cause
15772 type deduction to fail. */
15773 if (complain & tf_error)
15774 {
15775 if (TYPE_ANONYMOUS_P (nt))
15776 error ("%qT is/uses anonymous type", t);
15777 else
15778 error ("template argument for %qD uses local type %qT",
15779 tmpl, t);
15780 }
15781 return true;
15782 }
15783 /* In order to avoid all sorts of complications, we do not
15784 allow variably-modified types as template arguments. */
15785 else if (variably_modified_type_p (t, NULL_TREE))
15786 {
15787 if (complain & tf_error)
15788 error ("%qT is a variably modified type", t);
15789 return true;
15790 }
15791 }
15792 /* Class template and alias template arguments should be OK. */
15793 else if (DECL_TYPE_TEMPLATE_P (t))
15794 ;
15795 /* A non-type argument of integral or enumerated type must be a
15796 constant. */
15797 else if (TREE_TYPE (t)
15798 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15799 && !REFERENCE_REF_P (t)
15800 && !TREE_CONSTANT (t))
15801 {
15802 if (complain & tf_error)
15803 error ("integral expression %qE is not constant", t);
15804 return true;
15805 }
15806 return false;
15807 }
15808
15809 static bool
15810 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15811 {
15812 int ix, len = DECL_NTPARMS (tmpl);
15813 bool result = false;
15814
15815 for (ix = 0; ix != len; ix++)
15816 {
15817 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15818 result = true;
15819 }
15820 if (result && (complain & tf_error))
15821 error (" trying to instantiate %qD", tmpl);
15822 return result;
15823 }
15824
15825 /* We're out of SFINAE context now, so generate diagnostics for the access
15826 errors we saw earlier when instantiating D from TMPL and ARGS. */
15827
15828 static void
15829 recheck_decl_substitution (tree d, tree tmpl, tree args)
15830 {
15831 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15832 tree type = TREE_TYPE (pattern);
15833 location_t loc = input_location;
15834
15835 push_access_scope (d);
15836 push_deferring_access_checks (dk_no_deferred);
15837 input_location = DECL_SOURCE_LOCATION (pattern);
15838 tsubst (type, args, tf_warning_or_error, d);
15839 input_location = loc;
15840 pop_deferring_access_checks ();
15841 pop_access_scope (d);
15842 }
15843
15844 /* Instantiate the indicated variable, function, or alias template TMPL with
15845 the template arguments in TARG_PTR. */
15846
15847 static tree
15848 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15849 {
15850 tree targ_ptr = orig_args;
15851 tree fndecl;
15852 tree gen_tmpl;
15853 tree spec;
15854 bool access_ok = true;
15855
15856 if (tmpl == error_mark_node)
15857 return error_mark_node;
15858
15859 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15860
15861 /* If this function is a clone, handle it specially. */
15862 if (DECL_CLONED_FUNCTION_P (tmpl))
15863 {
15864 tree spec;
15865 tree clone;
15866
15867 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15868 DECL_CLONED_FUNCTION. */
15869 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15870 targ_ptr, complain);
15871 if (spec == error_mark_node)
15872 return error_mark_node;
15873
15874 /* Look for the clone. */
15875 FOR_EACH_CLONE (clone, spec)
15876 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15877 return clone;
15878 /* We should always have found the clone by now. */
15879 gcc_unreachable ();
15880 return NULL_TREE;
15881 }
15882
15883 if (targ_ptr == error_mark_node)
15884 return error_mark_node;
15885
15886 /* Check to see if we already have this specialization. */
15887 gen_tmpl = most_general_template (tmpl);
15888 if (tmpl != gen_tmpl)
15889 /* The TMPL is a partial instantiation. To get a full set of
15890 arguments we must add the arguments used to perform the
15891 partial instantiation. */
15892 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15893 targ_ptr);
15894
15895 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15896 but it doesn't seem to be on the hot path. */
15897 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15898
15899 gcc_assert (tmpl == gen_tmpl
15900 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15901 == spec)
15902 || fndecl == NULL_TREE);
15903
15904 if (spec != NULL_TREE)
15905 {
15906 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15907 {
15908 if (complain & tf_error)
15909 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15910 return error_mark_node;
15911 }
15912 return spec;
15913 }
15914
15915 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15916 complain))
15917 return error_mark_node;
15918
15919 /* We are building a FUNCTION_DECL, during which the access of its
15920 parameters and return types have to be checked. However this
15921 FUNCTION_DECL which is the desired context for access checking
15922 is not built yet. We solve this chicken-and-egg problem by
15923 deferring all checks until we have the FUNCTION_DECL. */
15924 push_deferring_access_checks (dk_deferred);
15925
15926 /* Instantiation of the function happens in the context of the function
15927 template, not the context of the overload resolution we're doing. */
15928 push_to_top_level ();
15929 /* If there are dependent arguments, e.g. because we're doing partial
15930 ordering, make sure processing_template_decl stays set. */
15931 if (uses_template_parms (targ_ptr))
15932 ++processing_template_decl;
15933 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15934 {
15935 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
15936 complain, gen_tmpl, true);
15937 push_nested_class (ctx);
15938 }
15939
15940 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
15941
15942 if (VAR_P (pattern))
15943 {
15944 /* We need to determine if we're using a partial or explicit
15945 specialization now, because the type of the variable could be
15946 different. */
15947 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
15948 tree elt = most_specialized_partial_spec (tid, complain);
15949 if (elt == error_mark_node)
15950 pattern = error_mark_node;
15951 else if (elt)
15952 {
15953 tmpl = TREE_VALUE (elt);
15954 pattern = DECL_TEMPLATE_RESULT (tmpl);
15955 targ_ptr = TREE_PURPOSE (elt);
15956 }
15957 }
15958
15959 /* Substitute template parameters to obtain the specialization. */
15960 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
15961 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15962 pop_nested_class ();
15963 pop_from_top_level ();
15964
15965 if (fndecl == error_mark_node)
15966 {
15967 pop_deferring_access_checks ();
15968 return error_mark_node;
15969 }
15970
15971 /* The DECL_TI_TEMPLATE should always be the immediate parent
15972 template, not the most general template. */
15973 DECL_TI_TEMPLATE (fndecl) = tmpl;
15974
15975 /* Now we know the specialization, compute access previously
15976 deferred. */
15977 push_access_scope (fndecl);
15978 if (!perform_deferred_access_checks (complain))
15979 access_ok = false;
15980 pop_access_scope (fndecl);
15981 pop_deferring_access_checks ();
15982
15983 /* If we've just instantiated the main entry point for a function,
15984 instantiate all the alternate entry points as well. We do this
15985 by cloning the instantiation of the main entry point, not by
15986 instantiating the template clones. */
15987 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15988 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15989
15990 if (!access_ok)
15991 {
15992 if (!(complain & tf_error))
15993 {
15994 /* Remember to reinstantiate when we're out of SFINAE so the user
15995 can see the errors. */
15996 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15997 }
15998 return error_mark_node;
15999 }
16000 return fndecl;
16001 }
16002
16003 /* Wrapper for instantiate_template_1. */
16004
16005 tree
16006 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
16007 {
16008 tree ret;
16009 timevar_push (TV_TEMPLATE_INST);
16010 ret = instantiate_template_1 (tmpl, orig_args, complain);
16011 timevar_pop (TV_TEMPLATE_INST);
16012 return ret;
16013 }
16014
16015 /* Instantiate the alias template TMPL with ARGS. Also push a template
16016 instantiation level, which instantiate_template doesn't do because
16017 functions and variables have sufficient context established by the
16018 callers. */
16019
16020 static tree
16021 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
16022 {
16023 struct pending_template *old_last_pend = last_pending_template;
16024 struct tinst_level *old_error_tinst = last_error_tinst_level;
16025 if (tmpl == error_mark_node || args == error_mark_node)
16026 return error_mark_node;
16027 tree tinst = build_tree_list (tmpl, args);
16028 if (!push_tinst_level (tinst))
16029 {
16030 ggc_free (tinst);
16031 return error_mark_node;
16032 }
16033
16034 args =
16035 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16036 args, tmpl, complain,
16037 /*require_all_args=*/true,
16038 /*use_default_args=*/true);
16039
16040 tree r = instantiate_template (tmpl, args, complain);
16041 pop_tinst_level ();
16042 /* We can't free this if a pending_template entry or last_error_tinst_level
16043 is pointing at it. */
16044 if (last_pending_template == old_last_pend
16045 && last_error_tinst_level == old_error_tinst)
16046 ggc_free (tinst);
16047
16048 return r;
16049 }
16050
16051 /* PARM is a template parameter pack for FN. Returns true iff
16052 PARM is used in a deducible way in the argument list of FN. */
16053
16054 static bool
16055 pack_deducible_p (tree parm, tree fn)
16056 {
16057 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16058 for (; t; t = TREE_CHAIN (t))
16059 {
16060 tree type = TREE_VALUE (t);
16061 tree packs;
16062 if (!PACK_EXPANSION_P (type))
16063 continue;
16064 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16065 packs; packs = TREE_CHAIN (packs))
16066 if (template_args_equal (TREE_VALUE (packs), parm))
16067 {
16068 /* The template parameter pack is used in a function parameter
16069 pack. If this is the end of the parameter list, the
16070 template parameter pack is deducible. */
16071 if (TREE_CHAIN (t) == void_list_node)
16072 return true;
16073 else
16074 /* Otherwise, not. Well, it could be deduced from
16075 a non-pack parameter, but doing so would end up with
16076 a deduction mismatch, so don't bother. */
16077 return false;
16078 }
16079 }
16080 /* The template parameter pack isn't used in any function parameter
16081 packs, but it might be used deeper, e.g. tuple<Args...>. */
16082 return true;
16083 }
16084
16085 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16086 NARGS elements of the arguments that are being used when calling
16087 it. TARGS is a vector into which the deduced template arguments
16088 are placed.
16089
16090 Returns either a FUNCTION_DECL for the matching specialization of FN or
16091 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16092 true, diagnostics will be printed to explain why it failed.
16093
16094 If FN is a conversion operator, or we are trying to produce a specific
16095 specialization, RETURN_TYPE is the return type desired.
16096
16097 The EXPLICIT_TARGS are explicit template arguments provided via a
16098 template-id.
16099
16100 The parameter STRICT is one of:
16101
16102 DEDUCE_CALL:
16103 We are deducing arguments for a function call, as in
16104 [temp.deduct.call].
16105
16106 DEDUCE_CONV:
16107 We are deducing arguments for a conversion function, as in
16108 [temp.deduct.conv].
16109
16110 DEDUCE_EXACT:
16111 We are deducing arguments when doing an explicit instantiation
16112 as in [temp.explicit], when determining an explicit specialization
16113 as in [temp.expl.spec], or when taking the address of a function
16114 template, as in [temp.deduct.funcaddr]. */
16115
16116 tree
16117 fn_type_unification (tree fn,
16118 tree explicit_targs,
16119 tree targs,
16120 const tree *args,
16121 unsigned int nargs,
16122 tree return_type,
16123 unification_kind_t strict,
16124 int flags,
16125 bool explain_p,
16126 bool decltype_p)
16127 {
16128 tree parms;
16129 tree fntype;
16130 tree decl = NULL_TREE;
16131 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16132 bool ok;
16133 static int deduction_depth;
16134 struct pending_template *old_last_pend = last_pending_template;
16135 struct tinst_level *old_error_tinst = last_error_tinst_level;
16136 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16137 tree tinst;
16138 tree r = error_mark_node;
16139
16140 if (decltype_p)
16141 complain |= tf_decltype;
16142
16143 /* In C++0x, it's possible to have a function template whose type depends
16144 on itself recursively. This is most obvious with decltype, but can also
16145 occur with enumeration scope (c++/48969). So we need to catch infinite
16146 recursion and reject the substitution at deduction time; this function
16147 will return error_mark_node for any repeated substitution.
16148
16149 This also catches excessive recursion such as when f<N> depends on
16150 f<N-1> across all integers, and returns error_mark_node for all the
16151 substitutions back up to the initial one.
16152
16153 This is, of course, not reentrant. */
16154 if (excessive_deduction_depth)
16155 return error_mark_node;
16156 tinst = build_tree_list (fn, NULL_TREE);
16157 ++deduction_depth;
16158
16159 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16160
16161 fntype = TREE_TYPE (fn);
16162 if (explicit_targs)
16163 {
16164 /* [temp.deduct]
16165
16166 The specified template arguments must match the template
16167 parameters in kind (i.e., type, nontype, template), and there
16168 must not be more arguments than there are parameters;
16169 otherwise type deduction fails.
16170
16171 Nontype arguments must match the types of the corresponding
16172 nontype template parameters, or must be convertible to the
16173 types of the corresponding nontype parameters as specified in
16174 _temp.arg.nontype_, otherwise type deduction fails.
16175
16176 All references in the function type of the function template
16177 to the corresponding template parameters are replaced by the
16178 specified template argument values. If a substitution in a
16179 template parameter or in the function type of the function
16180 template results in an invalid type, type deduction fails. */
16181 int i, len = TREE_VEC_LENGTH (tparms);
16182 location_t loc = input_location;
16183 bool incomplete = false;
16184
16185 /* Adjust any explicit template arguments before entering the
16186 substitution context. */
16187 explicit_targs
16188 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16189 complain,
16190 /*require_all_args=*/false,
16191 /*use_default_args=*/false));
16192 if (explicit_targs == error_mark_node)
16193 goto fail;
16194
16195 /* Substitute the explicit args into the function type. This is
16196 necessary so that, for instance, explicitly declared function
16197 arguments can match null pointed constants. If we were given
16198 an incomplete set of explicit args, we must not do semantic
16199 processing during substitution as we could create partial
16200 instantiations. */
16201 for (i = 0; i < len; i++)
16202 {
16203 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16204 bool parameter_pack = false;
16205 tree targ = TREE_VEC_ELT (explicit_targs, i);
16206
16207 /* Dig out the actual parm. */
16208 if (TREE_CODE (parm) == TYPE_DECL
16209 || TREE_CODE (parm) == TEMPLATE_DECL)
16210 {
16211 parm = TREE_TYPE (parm);
16212 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16213 }
16214 else if (TREE_CODE (parm) == PARM_DECL)
16215 {
16216 parm = DECL_INITIAL (parm);
16217 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16218 }
16219
16220 if (!parameter_pack && targ == NULL_TREE)
16221 /* No explicit argument for this template parameter. */
16222 incomplete = true;
16223
16224 if (parameter_pack && pack_deducible_p (parm, fn))
16225 {
16226 /* Mark the argument pack as "incomplete". We could
16227 still deduce more arguments during unification.
16228 We remove this mark in type_unification_real. */
16229 if (targ)
16230 {
16231 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16232 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16233 = ARGUMENT_PACK_ARGS (targ);
16234 }
16235
16236 /* We have some incomplete argument packs. */
16237 incomplete = true;
16238 }
16239 }
16240
16241 TREE_VALUE (tinst) = explicit_targs;
16242 if (!push_tinst_level (tinst))
16243 {
16244 excessive_deduction_depth = true;
16245 goto fail;
16246 }
16247 processing_template_decl += incomplete;
16248 input_location = DECL_SOURCE_LOCATION (fn);
16249 /* Ignore any access checks; we'll see them again in
16250 instantiate_template and they might have the wrong
16251 access path at this point. */
16252 push_deferring_access_checks (dk_deferred);
16253 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16254 complain | tf_partial, NULL_TREE);
16255 pop_deferring_access_checks ();
16256 input_location = loc;
16257 processing_template_decl -= incomplete;
16258 pop_tinst_level ();
16259
16260 if (fntype == error_mark_node)
16261 goto fail;
16262
16263 /* Place the explicitly specified arguments in TARGS. */
16264 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16265 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16266 }
16267
16268 /* Never do unification on the 'this' parameter. */
16269 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16270
16271 if (return_type)
16272 {
16273 tree *new_args;
16274
16275 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16276 new_args = XALLOCAVEC (tree, nargs + 1);
16277 new_args[0] = return_type;
16278 memcpy (new_args + 1, args, nargs * sizeof (tree));
16279 args = new_args;
16280 ++nargs;
16281 }
16282
16283 /* We allow incomplete unification without an error message here
16284 because the standard doesn't seem to explicitly prohibit it. Our
16285 callers must be ready to deal with unification failures in any
16286 event. */
16287
16288 TREE_VALUE (tinst) = targs;
16289 /* If we aren't explaining yet, push tinst context so we can see where
16290 any errors (e.g. from class instantiations triggered by instantiation
16291 of default template arguments) come from. If we are explaining, this
16292 context is redundant. */
16293 if (!explain_p && !push_tinst_level (tinst))
16294 {
16295 excessive_deduction_depth = true;
16296 goto fail;
16297 }
16298
16299 /* type_unification_real will pass back any access checks from default
16300 template argument substitution. */
16301 vec<deferred_access_check, va_gc> *checks;
16302 checks = NULL;
16303
16304 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16305 targs, parms, args, nargs, /*subr=*/0,
16306 strict, flags, &checks, explain_p);
16307 if (!explain_p)
16308 pop_tinst_level ();
16309 if (!ok)
16310 goto fail;
16311
16312 /* Now that we have bindings for all of the template arguments,
16313 ensure that the arguments deduced for the template template
16314 parameters have compatible template parameter lists. We cannot
16315 check this property before we have deduced all template
16316 arguments, because the template parameter types of a template
16317 template parameter might depend on prior template parameters
16318 deduced after the template template parameter. The following
16319 ill-formed example illustrates this issue:
16320
16321 template<typename T, template<T> class C> void f(C<5>, T);
16322
16323 template<int N> struct X {};
16324
16325 void g() {
16326 f(X<5>(), 5l); // error: template argument deduction fails
16327 }
16328
16329 The template parameter list of 'C' depends on the template type
16330 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16331 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16332 time that we deduce 'C'. */
16333 if (!template_template_parm_bindings_ok_p
16334 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16335 {
16336 unify_inconsistent_template_template_parameters (explain_p);
16337 goto fail;
16338 }
16339
16340 /* All is well so far. Now, check:
16341
16342 [temp.deduct]
16343
16344 When all template arguments have been deduced, all uses of
16345 template parameters in nondeduced contexts are replaced with
16346 the corresponding deduced argument values. If the
16347 substitution results in an invalid type, as described above,
16348 type deduction fails. */
16349 TREE_VALUE (tinst) = targs;
16350 if (!push_tinst_level (tinst))
16351 {
16352 excessive_deduction_depth = true;
16353 goto fail;
16354 }
16355
16356 /* Also collect access checks from the instantiation. */
16357 reopen_deferring_access_checks (checks);
16358
16359 decl = instantiate_template (fn, targs, complain);
16360
16361 checks = get_deferred_access_checks ();
16362 pop_deferring_access_checks ();
16363
16364 pop_tinst_level ();
16365
16366 if (decl == error_mark_node)
16367 goto fail;
16368
16369 /* Now perform any access checks encountered during substitution. */
16370 push_access_scope (decl);
16371 ok = perform_access_checks (checks, complain);
16372 pop_access_scope (decl);
16373 if (!ok)
16374 goto fail;
16375
16376 /* If we're looking for an exact match, check that what we got
16377 is indeed an exact match. It might not be if some template
16378 parameters are used in non-deduced contexts. But don't check
16379 for an exact match if we have dependent template arguments;
16380 in that case we're doing partial ordering, and we already know
16381 that we have two candidates that will provide the actual type. */
16382 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16383 {
16384 tree substed = TREE_TYPE (decl);
16385 unsigned int i;
16386
16387 tree sarg
16388 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16389 if (return_type)
16390 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16391 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16392 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16393 {
16394 unify_type_mismatch (explain_p, args[i],
16395 TREE_VALUE (sarg));
16396 goto fail;
16397 }
16398 }
16399
16400 r = decl;
16401
16402 fail:
16403 --deduction_depth;
16404 if (excessive_deduction_depth)
16405 {
16406 if (deduction_depth == 0)
16407 /* Reset once we're all the way out. */
16408 excessive_deduction_depth = false;
16409 }
16410
16411 /* We can't free this if a pending_template entry or last_error_tinst_level
16412 is pointing at it. */
16413 if (last_pending_template == old_last_pend
16414 && last_error_tinst_level == old_error_tinst)
16415 ggc_free (tinst);
16416
16417 return r;
16418 }
16419
16420 /* Adjust types before performing type deduction, as described in
16421 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16422 sections are symmetric. PARM is the type of a function parameter
16423 or the return type of the conversion function. ARG is the type of
16424 the argument passed to the call, or the type of the value
16425 initialized with the result of the conversion function.
16426 ARG_EXPR is the original argument expression, which may be null. */
16427
16428 static int
16429 maybe_adjust_types_for_deduction (unification_kind_t strict,
16430 tree* parm,
16431 tree* arg,
16432 tree arg_expr)
16433 {
16434 int result = 0;
16435
16436 switch (strict)
16437 {
16438 case DEDUCE_CALL:
16439 break;
16440
16441 case DEDUCE_CONV:
16442 {
16443 /* Swap PARM and ARG throughout the remainder of this
16444 function; the handling is precisely symmetric since PARM
16445 will initialize ARG rather than vice versa. */
16446 tree* temp = parm;
16447 parm = arg;
16448 arg = temp;
16449 break;
16450 }
16451
16452 case DEDUCE_EXACT:
16453 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16454 too, but here handle it by stripping the reference from PARM
16455 rather than by adding it to ARG. */
16456 if (TREE_CODE (*parm) == REFERENCE_TYPE
16457 && TYPE_REF_IS_RVALUE (*parm)
16458 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16459 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16460 && TREE_CODE (*arg) == REFERENCE_TYPE
16461 && !TYPE_REF_IS_RVALUE (*arg))
16462 *parm = TREE_TYPE (*parm);
16463 /* Nothing else to do in this case. */
16464 return 0;
16465
16466 default:
16467 gcc_unreachable ();
16468 }
16469
16470 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16471 {
16472 /* [temp.deduct.call]
16473
16474 If P is not a reference type:
16475
16476 --If A is an array type, the pointer type produced by the
16477 array-to-pointer standard conversion (_conv.array_) is
16478 used in place of A for type deduction; otherwise,
16479
16480 --If A is a function type, the pointer type produced by
16481 the function-to-pointer standard conversion
16482 (_conv.func_) is used in place of A for type deduction;
16483 otherwise,
16484
16485 --If A is a cv-qualified type, the top level
16486 cv-qualifiers of A's type are ignored for type
16487 deduction. */
16488 if (TREE_CODE (*arg) == ARRAY_TYPE)
16489 *arg = build_pointer_type (TREE_TYPE (*arg));
16490 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16491 *arg = build_pointer_type (*arg);
16492 else
16493 *arg = TYPE_MAIN_VARIANT (*arg);
16494 }
16495
16496 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16497 of the form T&&, where T is a template parameter, and the argument
16498 is an lvalue, T is deduced as A& */
16499 if (TREE_CODE (*parm) == REFERENCE_TYPE
16500 && TYPE_REF_IS_RVALUE (*parm)
16501 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16502 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16503 && (arg_expr ? real_lvalue_p (arg_expr)
16504 /* try_one_overload doesn't provide an arg_expr, but
16505 functions are always lvalues. */
16506 : TREE_CODE (*arg) == FUNCTION_TYPE))
16507 *arg = build_reference_type (*arg);
16508
16509 /* [temp.deduct.call]
16510
16511 If P is a cv-qualified type, the top level cv-qualifiers
16512 of P's type are ignored for type deduction. If P is a
16513 reference type, the type referred to by P is used for
16514 type deduction. */
16515 *parm = TYPE_MAIN_VARIANT (*parm);
16516 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16517 {
16518 *parm = TREE_TYPE (*parm);
16519 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16520 }
16521
16522 /* DR 322. For conversion deduction, remove a reference type on parm
16523 too (which has been swapped into ARG). */
16524 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16525 *arg = TREE_TYPE (*arg);
16526
16527 return result;
16528 }
16529
16530 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16531 template which does contain any deducible template parameters; check if
16532 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16533 unify_one_argument. */
16534
16535 static int
16536 check_non_deducible_conversion (tree parm, tree arg, int strict,
16537 int flags, bool explain_p)
16538 {
16539 tree type;
16540
16541 if (!TYPE_P (arg))
16542 type = TREE_TYPE (arg);
16543 else
16544 type = arg;
16545
16546 if (same_type_p (parm, type))
16547 return unify_success (explain_p);
16548
16549 if (strict == DEDUCE_CONV)
16550 {
16551 if (can_convert_arg (type, parm, NULL_TREE, flags,
16552 explain_p ? tf_warning_or_error : tf_none))
16553 return unify_success (explain_p);
16554 }
16555 else if (strict != DEDUCE_EXACT)
16556 {
16557 if (can_convert_arg (parm, type,
16558 TYPE_P (arg) ? NULL_TREE : arg,
16559 flags, explain_p ? tf_warning_or_error : tf_none))
16560 return unify_success (explain_p);
16561 }
16562
16563 if (strict == DEDUCE_EXACT)
16564 return unify_type_mismatch (explain_p, parm, arg);
16565 else
16566 return unify_arg_conversion (explain_p, parm, type, arg);
16567 }
16568
16569 static bool uses_deducible_template_parms (tree type);
16570
16571 /* Returns true iff the expression EXPR is one from which a template
16572 argument can be deduced. In other words, if it's an undecorated
16573 use of a template non-type parameter. */
16574
16575 static bool
16576 deducible_expression (tree expr)
16577 {
16578 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16579 }
16580
16581 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16582 deducible way; that is, if it has a max value of <PARM> - 1. */
16583
16584 static bool
16585 deducible_array_bound (tree domain)
16586 {
16587 if (domain == NULL_TREE)
16588 return false;
16589
16590 tree max = TYPE_MAX_VALUE (domain);
16591 if (TREE_CODE (max) != MINUS_EXPR)
16592 return false;
16593
16594 return deducible_expression (TREE_OPERAND (max, 0));
16595 }
16596
16597 /* Returns true iff the template arguments ARGS use a template parameter
16598 in a deducible way. */
16599
16600 static bool
16601 deducible_template_args (tree args)
16602 {
16603 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16604 {
16605 bool deducible;
16606 tree elt = TREE_VEC_ELT (args, i);
16607 if (ARGUMENT_PACK_P (elt))
16608 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16609 else
16610 {
16611 if (PACK_EXPANSION_P (elt))
16612 elt = PACK_EXPANSION_PATTERN (elt);
16613 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16614 deducible = true;
16615 else if (TYPE_P (elt))
16616 deducible = uses_deducible_template_parms (elt);
16617 else
16618 deducible = deducible_expression (elt);
16619 }
16620 if (deducible)
16621 return true;
16622 }
16623 return false;
16624 }
16625
16626 /* Returns true iff TYPE contains any deducible references to template
16627 parameters, as per 14.8.2.5. */
16628
16629 static bool
16630 uses_deducible_template_parms (tree type)
16631 {
16632 if (PACK_EXPANSION_P (type))
16633 type = PACK_EXPANSION_PATTERN (type);
16634
16635 /* T
16636 cv-list T
16637 TT<T>
16638 TT<i>
16639 TT<> */
16640 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16641 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16642 return true;
16643
16644 /* T*
16645 T&
16646 T&& */
16647 if (POINTER_TYPE_P (type))
16648 return uses_deducible_template_parms (TREE_TYPE (type));
16649
16650 /* T[integer-constant ]
16651 type [i] */
16652 if (TREE_CODE (type) == ARRAY_TYPE)
16653 return (uses_deducible_template_parms (TREE_TYPE (type))
16654 || deducible_array_bound (TYPE_DOMAIN (type)));
16655
16656 /* T type ::*
16657 type T::*
16658 T T::*
16659 T (type ::*)()
16660 type (T::*)()
16661 type (type ::*)(T)
16662 type (T::*)(T)
16663 T (type ::*)(T)
16664 T (T::*)()
16665 T (T::*)(T) */
16666 if (TYPE_PTRMEM_P (type))
16667 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16668 || (uses_deducible_template_parms
16669 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16670
16671 /* template-name <T> (where template-name refers to a class template)
16672 template-name <i> (where template-name refers to a class template) */
16673 if (CLASS_TYPE_P (type)
16674 && CLASSTYPE_TEMPLATE_INFO (type)
16675 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16676 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16677 (CLASSTYPE_TI_ARGS (type)));
16678
16679 /* type (T)
16680 T()
16681 T(T) */
16682 if (TREE_CODE (type) == FUNCTION_TYPE
16683 || TREE_CODE (type) == METHOD_TYPE)
16684 {
16685 if (uses_deducible_template_parms (TREE_TYPE (type)))
16686 return true;
16687 tree parm = TYPE_ARG_TYPES (type);
16688 if (TREE_CODE (type) == METHOD_TYPE)
16689 parm = TREE_CHAIN (parm);
16690 for (; parm; parm = TREE_CHAIN (parm))
16691 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16692 return true;
16693 }
16694
16695 return false;
16696 }
16697
16698 /* Subroutine of type_unification_real and unify_pack_expansion to
16699 handle unification of a single P/A pair. Parameters are as
16700 for those functions. */
16701
16702 static int
16703 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16704 int subr, unification_kind_t strict,
16705 bool explain_p)
16706 {
16707 tree arg_expr = NULL_TREE;
16708 int arg_strict;
16709
16710 if (arg == error_mark_node || parm == error_mark_node)
16711 return unify_invalid (explain_p);
16712 if (arg == unknown_type_node)
16713 /* We can't deduce anything from this, but we might get all the
16714 template args from other function args. */
16715 return unify_success (explain_p);
16716
16717 /* Implicit conversions (Clause 4) will be performed on a function
16718 argument to convert it to the type of the corresponding function
16719 parameter if the parameter type contains no template-parameters that
16720 participate in template argument deduction. */
16721 if (strict != DEDUCE_EXACT
16722 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16723 /* For function parameters with no deducible template parameters,
16724 just return. We'll check non-dependent conversions later. */
16725 return unify_success (explain_p);
16726
16727 switch (strict)
16728 {
16729 case DEDUCE_CALL:
16730 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16731 | UNIFY_ALLOW_MORE_CV_QUAL
16732 | UNIFY_ALLOW_DERIVED);
16733 break;
16734
16735 case DEDUCE_CONV:
16736 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16737 break;
16738
16739 case DEDUCE_EXACT:
16740 arg_strict = UNIFY_ALLOW_NONE;
16741 break;
16742
16743 default:
16744 gcc_unreachable ();
16745 }
16746
16747 /* We only do these transformations if this is the top-level
16748 parameter_type_list in a call or declaration matching; in other
16749 situations (nested function declarators, template argument lists) we
16750 won't be comparing a type to an expression, and we don't do any type
16751 adjustments. */
16752 if (!subr)
16753 {
16754 if (!TYPE_P (arg))
16755 {
16756 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16757 if (type_unknown_p (arg))
16758 {
16759 /* [temp.deduct.type] A template-argument can be
16760 deduced from a pointer to function or pointer
16761 to member function argument if the set of
16762 overloaded functions does not contain function
16763 templates and at most one of a set of
16764 overloaded functions provides a unique
16765 match. */
16766
16767 if (resolve_overloaded_unification
16768 (tparms, targs, parm, arg, strict,
16769 arg_strict, explain_p))
16770 return unify_success (explain_p);
16771 return unify_overload_resolution_failure (explain_p, arg);
16772 }
16773
16774 arg_expr = arg;
16775 arg = unlowered_expr_type (arg);
16776 if (arg == error_mark_node)
16777 return unify_invalid (explain_p);
16778 }
16779
16780 arg_strict |=
16781 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16782 }
16783 else
16784 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16785 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16786 return unify_template_argument_mismatch (explain_p, parm, arg);
16787
16788 /* For deduction from an init-list we need the actual list. */
16789 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16790 arg = arg_expr;
16791 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16792 }
16793
16794 /* Most parms like fn_type_unification.
16795
16796 If SUBR is 1, we're being called recursively (to unify the
16797 arguments of a function or method parameter of a function
16798 template).
16799
16800 CHECKS is a pointer to a vector of access checks encountered while
16801 substituting default template arguments. */
16802
16803 static int
16804 type_unification_real (tree tparms,
16805 tree targs,
16806 tree xparms,
16807 const tree *xargs,
16808 unsigned int xnargs,
16809 int subr,
16810 unification_kind_t strict,
16811 int flags,
16812 vec<deferred_access_check, va_gc> **checks,
16813 bool explain_p)
16814 {
16815 tree parm, arg;
16816 int i;
16817 int ntparms = TREE_VEC_LENGTH (tparms);
16818 int saw_undeduced = 0;
16819 tree parms;
16820 const tree *args;
16821 unsigned int nargs;
16822 unsigned int ia;
16823
16824 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16825 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16826 gcc_assert (ntparms > 0);
16827
16828 /* Reset the number of non-defaulted template arguments contained
16829 in TARGS. */
16830 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16831
16832 again:
16833 parms = xparms;
16834 args = xargs;
16835 nargs = xnargs;
16836
16837 ia = 0;
16838 while (parms && parms != void_list_node
16839 && ia < nargs)
16840 {
16841 parm = TREE_VALUE (parms);
16842
16843 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16844 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16845 /* For a function parameter pack that occurs at the end of the
16846 parameter-declaration-list, the type A of each remaining
16847 argument of the call is compared with the type P of the
16848 declarator-id of the function parameter pack. */
16849 break;
16850
16851 parms = TREE_CHAIN (parms);
16852
16853 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16854 /* For a function parameter pack that does not occur at the
16855 end of the parameter-declaration-list, the type of the
16856 parameter pack is a non-deduced context. */
16857 continue;
16858
16859 arg = args[ia];
16860 ++ia;
16861
16862 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16863 explain_p))
16864 return 1;
16865 }
16866
16867 if (parms
16868 && parms != void_list_node
16869 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16870 {
16871 /* Unify the remaining arguments with the pack expansion type. */
16872 tree argvec;
16873 tree parmvec = make_tree_vec (1);
16874
16875 /* Allocate a TREE_VEC and copy in all of the arguments */
16876 argvec = make_tree_vec (nargs - ia);
16877 for (i = 0; ia < nargs; ++ia, ++i)
16878 TREE_VEC_ELT (argvec, i) = args[ia];
16879
16880 /* Copy the parameter into parmvec. */
16881 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16882 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16883 /*subr=*/subr, explain_p))
16884 return 1;
16885
16886 /* Advance to the end of the list of parameters. */
16887 parms = TREE_CHAIN (parms);
16888 }
16889
16890 /* Fail if we've reached the end of the parm list, and more args
16891 are present, and the parm list isn't variadic. */
16892 if (ia < nargs && parms == void_list_node)
16893 return unify_too_many_arguments (explain_p, nargs, ia);
16894 /* Fail if parms are left and they don't have default values and
16895 they aren't all deduced as empty packs (c++/57397). This is
16896 consistent with sufficient_parms_p. */
16897 if (parms && parms != void_list_node
16898 && TREE_PURPOSE (parms) == NULL_TREE)
16899 {
16900 unsigned int count = nargs;
16901 tree p = parms;
16902 bool type_pack_p;
16903 do
16904 {
16905 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
16906 if (!type_pack_p)
16907 count++;
16908 p = TREE_CHAIN (p);
16909 }
16910 while (p && p != void_list_node);
16911 if (count != nargs)
16912 return unify_too_few_arguments (explain_p, ia, count,
16913 type_pack_p);
16914 }
16915
16916 if (!subr)
16917 {
16918 tsubst_flags_t complain = (explain_p
16919 ? tf_warning_or_error
16920 : tf_none);
16921
16922 for (i = 0; i < ntparms; i++)
16923 {
16924 tree targ = TREE_VEC_ELT (targs, i);
16925 tree tparm = TREE_VEC_ELT (tparms, i);
16926
16927 /* Clear the "incomplete" flags on all argument packs now so that
16928 substituting them into later default arguments works. */
16929 if (targ && ARGUMENT_PACK_P (targ))
16930 {
16931 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16932 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16933 }
16934
16935 if (targ || tparm == error_mark_node)
16936 continue;
16937 tparm = TREE_VALUE (tparm);
16938
16939 /* If this is an undeduced nontype parameter that depends on
16940 a type parameter, try another pass; its type may have been
16941 deduced from a later argument than the one from which
16942 this parameter can be deduced. */
16943 if (TREE_CODE (tparm) == PARM_DECL
16944 && uses_template_parms (TREE_TYPE (tparm))
16945 && saw_undeduced < 2)
16946 {
16947 saw_undeduced = 1;
16948 continue;
16949 }
16950
16951 /* Core issue #226 (C++0x) [temp.deduct]:
16952
16953 If a template argument has not been deduced, its
16954 default template argument, if any, is used.
16955
16956 When we are in C++98 mode, TREE_PURPOSE will either
16957 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16958 to explicitly check cxx_dialect here. */
16959 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16960 /* OK, there is a default argument. Wait until after the
16961 conversion check to do substitution. */
16962 continue;
16963
16964 /* If the type parameter is a parameter pack, then it will
16965 be deduced to an empty parameter pack. */
16966 if (template_parameter_pack_p (tparm))
16967 {
16968 tree arg;
16969
16970 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16971 {
16972 arg = make_node (NONTYPE_ARGUMENT_PACK);
16973 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16974 TREE_CONSTANT (arg) = 1;
16975 }
16976 else
16977 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16978
16979 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16980
16981 TREE_VEC_ELT (targs, i) = arg;
16982 continue;
16983 }
16984
16985 return unify_parameter_deduction_failure (explain_p, tparm);
16986 }
16987
16988 /* DR 1391: All parameters have args, now check non-dependent parms for
16989 convertibility. */
16990 if (saw_undeduced < 2)
16991 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
16992 parms && parms != void_list_node && ia < nargs; )
16993 {
16994 parm = TREE_VALUE (parms);
16995
16996 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16997 && (!TREE_CHAIN (parms)
16998 || TREE_CHAIN (parms) == void_list_node))
16999 /* For a function parameter pack that occurs at the end of the
17000 parameter-declaration-list, the type A of each remaining
17001 argument of the call is compared with the type P of the
17002 declarator-id of the function parameter pack. */
17003 break;
17004
17005 parms = TREE_CHAIN (parms);
17006
17007 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17008 /* For a function parameter pack that does not occur at the
17009 end of the parameter-declaration-list, the type of the
17010 parameter pack is a non-deduced context. */
17011 continue;
17012
17013 arg = args[ia];
17014 ++ia;
17015
17016 if (uses_template_parms (parm))
17017 continue;
17018 if (check_non_deducible_conversion (parm, arg, strict, flags,
17019 explain_p))
17020 return 1;
17021 }
17022
17023 /* Now substitute into the default template arguments. */
17024 for (i = 0; i < ntparms; i++)
17025 {
17026 tree targ = TREE_VEC_ELT (targs, i);
17027 tree tparm = TREE_VEC_ELT (tparms, i);
17028
17029 if (targ || tparm == error_mark_node)
17030 continue;
17031 tree parm = TREE_VALUE (tparm);
17032
17033 if (TREE_CODE (parm) == PARM_DECL
17034 && uses_template_parms (TREE_TYPE (parm))
17035 && saw_undeduced < 2)
17036 continue;
17037
17038 tree arg = TREE_PURPOSE (tparm);
17039 reopen_deferring_access_checks (*checks);
17040 location_t save_loc = input_location;
17041 if (DECL_P (parm))
17042 input_location = DECL_SOURCE_LOCATION (parm);
17043 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
17044 arg = convert_template_argument (parm, arg, targs, complain,
17045 i, NULL_TREE);
17046 input_location = save_loc;
17047 *checks = get_deferred_access_checks ();
17048 pop_deferring_access_checks ();
17049 if (arg == error_mark_node)
17050 return 1;
17051 else
17052 {
17053 TREE_VEC_ELT (targs, i) = arg;
17054 /* The position of the first default template argument,
17055 is also the number of non-defaulted arguments in TARGS.
17056 Record that. */
17057 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17058 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
17059 continue;
17060 }
17061 }
17062
17063 if (saw_undeduced++ == 1)
17064 goto again;
17065 }
17066 #ifdef ENABLE_CHECKING
17067 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17068 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
17069 #endif
17070
17071 return unify_success (explain_p);
17072 }
17073
17074 /* Subroutine of type_unification_real. Args are like the variables
17075 at the call site. ARG is an overloaded function (or template-id);
17076 we try deducing template args from each of the overloads, and if
17077 only one succeeds, we go with that. Modifies TARGS and returns
17078 true on success. */
17079
17080 static bool
17081 resolve_overloaded_unification (tree tparms,
17082 tree targs,
17083 tree parm,
17084 tree arg,
17085 unification_kind_t strict,
17086 int sub_strict,
17087 bool explain_p)
17088 {
17089 tree tempargs = copy_node (targs);
17090 int good = 0;
17091 tree goodfn = NULL_TREE;
17092 bool addr_p;
17093
17094 if (TREE_CODE (arg) == ADDR_EXPR)
17095 {
17096 arg = TREE_OPERAND (arg, 0);
17097 addr_p = true;
17098 }
17099 else
17100 addr_p = false;
17101
17102 if (TREE_CODE (arg) == COMPONENT_REF)
17103 /* Handle `&x' where `x' is some static or non-static member
17104 function name. */
17105 arg = TREE_OPERAND (arg, 1);
17106
17107 if (TREE_CODE (arg) == OFFSET_REF)
17108 arg = TREE_OPERAND (arg, 1);
17109
17110 /* Strip baselink information. */
17111 if (BASELINK_P (arg))
17112 arg = BASELINK_FUNCTIONS (arg);
17113
17114 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17115 {
17116 /* If we got some explicit template args, we need to plug them into
17117 the affected templates before we try to unify, in case the
17118 explicit args will completely resolve the templates in question. */
17119
17120 int ok = 0;
17121 tree expl_subargs = TREE_OPERAND (arg, 1);
17122 arg = TREE_OPERAND (arg, 0);
17123
17124 for (; arg; arg = OVL_NEXT (arg))
17125 {
17126 tree fn = OVL_CURRENT (arg);
17127 tree subargs, elem;
17128
17129 if (TREE_CODE (fn) != TEMPLATE_DECL)
17130 continue;
17131
17132 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17133 expl_subargs, NULL_TREE, tf_none,
17134 /*require_all_args=*/true,
17135 /*use_default_args=*/true);
17136 if (subargs != error_mark_node
17137 && !any_dependent_template_arguments_p (subargs))
17138 {
17139 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17140 if (try_one_overload (tparms, targs, tempargs, parm,
17141 elem, strict, sub_strict, addr_p, explain_p)
17142 && (!goodfn || !same_type_p (goodfn, elem)))
17143 {
17144 goodfn = elem;
17145 ++good;
17146 }
17147 }
17148 else if (subargs)
17149 ++ok;
17150 }
17151 /* If no templates (or more than one) are fully resolved by the
17152 explicit arguments, this template-id is a non-deduced context; it
17153 could still be OK if we deduce all template arguments for the
17154 enclosing call through other arguments. */
17155 if (good != 1)
17156 good = ok;
17157 }
17158 else if (TREE_CODE (arg) != OVERLOAD
17159 && TREE_CODE (arg) != FUNCTION_DECL)
17160 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17161 -- but the deduction does not succeed because the expression is
17162 not just the function on its own. */
17163 return false;
17164 else
17165 for (; arg; arg = OVL_NEXT (arg))
17166 if (try_one_overload (tparms, targs, tempargs, parm,
17167 TREE_TYPE (OVL_CURRENT (arg)),
17168 strict, sub_strict, addr_p, explain_p)
17169 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17170 {
17171 goodfn = OVL_CURRENT (arg);
17172 ++good;
17173 }
17174
17175 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17176 to function or pointer to member function argument if the set of
17177 overloaded functions does not contain function templates and at most
17178 one of a set of overloaded functions provides a unique match.
17179
17180 So if we found multiple possibilities, we return success but don't
17181 deduce anything. */
17182
17183 if (good == 1)
17184 {
17185 int i = TREE_VEC_LENGTH (targs);
17186 for (; i--; )
17187 if (TREE_VEC_ELT (tempargs, i))
17188 {
17189 tree old = TREE_VEC_ELT (targs, i);
17190 tree new_ = TREE_VEC_ELT (tempargs, i);
17191 if (new_ && old && ARGUMENT_PACK_P (old)
17192 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17193 /* Don't forget explicit template arguments in a pack. */
17194 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17195 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17196 TREE_VEC_ELT (targs, i) = new_;
17197 }
17198 }
17199 if (good)
17200 return true;
17201
17202 return false;
17203 }
17204
17205 /* Core DR 115: In contexts where deduction is done and fails, or in
17206 contexts where deduction is not done, if a template argument list is
17207 specified and it, along with any default template arguments, identifies
17208 a single function template specialization, then the template-id is an
17209 lvalue for the function template specialization. */
17210
17211 tree
17212 resolve_nondeduced_context (tree orig_expr)
17213 {
17214 tree expr, offset, baselink;
17215 bool addr;
17216
17217 if (!type_unknown_p (orig_expr))
17218 return orig_expr;
17219
17220 expr = orig_expr;
17221 addr = false;
17222 offset = NULL_TREE;
17223 baselink = NULL_TREE;
17224
17225 if (TREE_CODE (expr) == ADDR_EXPR)
17226 {
17227 expr = TREE_OPERAND (expr, 0);
17228 addr = true;
17229 }
17230 if (TREE_CODE (expr) == OFFSET_REF)
17231 {
17232 offset = expr;
17233 expr = TREE_OPERAND (expr, 1);
17234 }
17235 if (BASELINK_P (expr))
17236 {
17237 baselink = expr;
17238 expr = BASELINK_FUNCTIONS (expr);
17239 }
17240
17241 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17242 {
17243 int good = 0;
17244 tree goodfn = NULL_TREE;
17245
17246 /* If we got some explicit template args, we need to plug them into
17247 the affected templates before we try to unify, in case the
17248 explicit args will completely resolve the templates in question. */
17249
17250 tree expl_subargs = TREE_OPERAND (expr, 1);
17251 tree arg = TREE_OPERAND (expr, 0);
17252 tree badfn = NULL_TREE;
17253 tree badargs = NULL_TREE;
17254
17255 for (; arg; arg = OVL_NEXT (arg))
17256 {
17257 tree fn = OVL_CURRENT (arg);
17258 tree subargs, elem;
17259
17260 if (TREE_CODE (fn) != TEMPLATE_DECL)
17261 continue;
17262
17263 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17264 expl_subargs, NULL_TREE, tf_none,
17265 /*require_all_args=*/true,
17266 /*use_default_args=*/true);
17267 if (subargs != error_mark_node
17268 && !any_dependent_template_arguments_p (subargs))
17269 {
17270 elem = instantiate_template (fn, subargs, tf_none);
17271 if (elem == error_mark_node)
17272 {
17273 badfn = fn;
17274 badargs = subargs;
17275 }
17276 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17277 {
17278 goodfn = elem;
17279 ++good;
17280 }
17281 }
17282 }
17283 if (good == 1)
17284 {
17285 mark_used (goodfn);
17286 expr = goodfn;
17287 if (baselink)
17288 expr = build_baselink (BASELINK_BINFO (baselink),
17289 BASELINK_ACCESS_BINFO (baselink),
17290 expr, BASELINK_OPTYPE (baselink));
17291 if (offset)
17292 {
17293 tree base
17294 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17295 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17296 }
17297 if (addr)
17298 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17299 return expr;
17300 }
17301 else if (good == 0 && badargs)
17302 /* There were no good options and at least one bad one, so let the
17303 user know what the problem is. */
17304 instantiate_template (badfn, badargs, tf_warning_or_error);
17305 }
17306 return orig_expr;
17307 }
17308
17309 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17310 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17311 different overloads deduce different arguments for a given parm.
17312 ADDR_P is true if the expression for which deduction is being
17313 performed was of the form "& fn" rather than simply "fn".
17314
17315 Returns 1 on success. */
17316
17317 static int
17318 try_one_overload (tree tparms,
17319 tree orig_targs,
17320 tree targs,
17321 tree parm,
17322 tree arg,
17323 unification_kind_t strict,
17324 int sub_strict,
17325 bool addr_p,
17326 bool explain_p)
17327 {
17328 int nargs;
17329 tree tempargs;
17330 int i;
17331
17332 if (arg == error_mark_node)
17333 return 0;
17334
17335 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17336 to function or pointer to member function argument if the set of
17337 overloaded functions does not contain function templates and at most
17338 one of a set of overloaded functions provides a unique match.
17339
17340 So if this is a template, just return success. */
17341
17342 if (uses_template_parms (arg))
17343 return 1;
17344
17345 if (TREE_CODE (arg) == METHOD_TYPE)
17346 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17347 else if (addr_p)
17348 arg = build_pointer_type (arg);
17349
17350 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17351
17352 /* We don't copy orig_targs for this because if we have already deduced
17353 some template args from previous args, unify would complain when we
17354 try to deduce a template parameter for the same argument, even though
17355 there isn't really a conflict. */
17356 nargs = TREE_VEC_LENGTH (targs);
17357 tempargs = make_tree_vec (nargs);
17358
17359 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17360 return 0;
17361
17362 /* First make sure we didn't deduce anything that conflicts with
17363 explicitly specified args. */
17364 for (i = nargs; i--; )
17365 {
17366 tree elt = TREE_VEC_ELT (tempargs, i);
17367 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17368
17369 if (!elt)
17370 /*NOP*/;
17371 else if (uses_template_parms (elt))
17372 /* Since we're unifying against ourselves, we will fill in
17373 template args used in the function parm list with our own
17374 template parms. Discard them. */
17375 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17376 else if (oldelt && !template_args_equal (oldelt, elt))
17377 return 0;
17378 }
17379
17380 for (i = nargs; i--; )
17381 {
17382 tree elt = TREE_VEC_ELT (tempargs, i);
17383
17384 if (elt)
17385 TREE_VEC_ELT (targs, i) = elt;
17386 }
17387
17388 return 1;
17389 }
17390
17391 /* PARM is a template class (perhaps with unbound template
17392 parameters). ARG is a fully instantiated type. If ARG can be
17393 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17394 TARGS are as for unify. */
17395
17396 static tree
17397 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17398 bool explain_p)
17399 {
17400 tree copy_of_targs;
17401
17402 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17403 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17404 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17405 return NULL_TREE;
17406
17407 /* We need to make a new template argument vector for the call to
17408 unify. If we used TARGS, we'd clutter it up with the result of
17409 the attempted unification, even if this class didn't work out.
17410 We also don't want to commit ourselves to all the unifications
17411 we've already done, since unification is supposed to be done on
17412 an argument-by-argument basis. In other words, consider the
17413 following pathological case:
17414
17415 template <int I, int J, int K>
17416 struct S {};
17417
17418 template <int I, int J>
17419 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17420
17421 template <int I, int J, int K>
17422 void f(S<I, J, K>, S<I, I, I>);
17423
17424 void g() {
17425 S<0, 0, 0> s0;
17426 S<0, 1, 2> s2;
17427
17428 f(s0, s2);
17429 }
17430
17431 Now, by the time we consider the unification involving `s2', we
17432 already know that we must have `f<0, 0, 0>'. But, even though
17433 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17434 because there are two ways to unify base classes of S<0, 1, 2>
17435 with S<I, I, I>. If we kept the already deduced knowledge, we
17436 would reject the possibility I=1. */
17437 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17438
17439 /* If unification failed, we're done. */
17440 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17441 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17442 return NULL_TREE;
17443
17444 return arg;
17445 }
17446
17447 /* Given a template type PARM and a class type ARG, find the unique
17448 base type in ARG that is an instance of PARM. We do not examine
17449 ARG itself; only its base-classes. If there is not exactly one
17450 appropriate base class, return NULL_TREE. PARM may be the type of
17451 a partial specialization, as well as a plain template type. Used
17452 by unify. */
17453
17454 static enum template_base_result
17455 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17456 bool explain_p, tree *result)
17457 {
17458 tree rval = NULL_TREE;
17459 tree binfo;
17460
17461 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17462
17463 binfo = TYPE_BINFO (complete_type (arg));
17464 if (!binfo)
17465 {
17466 /* The type could not be completed. */
17467 *result = NULL_TREE;
17468 return tbr_incomplete_type;
17469 }
17470
17471 /* Walk in inheritance graph order. The search order is not
17472 important, and this avoids multiple walks of virtual bases. */
17473 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17474 {
17475 tree r = try_class_unification (tparms, targs, parm,
17476 BINFO_TYPE (binfo), explain_p);
17477
17478 if (r)
17479 {
17480 /* If there is more than one satisfactory baseclass, then:
17481
17482 [temp.deduct.call]
17483
17484 If they yield more than one possible deduced A, the type
17485 deduction fails.
17486
17487 applies. */
17488 if (rval && !same_type_p (r, rval))
17489 {
17490 *result = NULL_TREE;
17491 return tbr_ambiguous_baseclass;
17492 }
17493
17494 rval = r;
17495 }
17496 }
17497
17498 *result = rval;
17499 return tbr_success;
17500 }
17501
17502 /* Returns the level of DECL, which declares a template parameter. */
17503
17504 static int
17505 template_decl_level (tree decl)
17506 {
17507 switch (TREE_CODE (decl))
17508 {
17509 case TYPE_DECL:
17510 case TEMPLATE_DECL:
17511 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17512
17513 case PARM_DECL:
17514 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17515
17516 default:
17517 gcc_unreachable ();
17518 }
17519 return 0;
17520 }
17521
17522 /* Decide whether ARG can be unified with PARM, considering only the
17523 cv-qualifiers of each type, given STRICT as documented for unify.
17524 Returns nonzero iff the unification is OK on that basis. */
17525
17526 static int
17527 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17528 {
17529 int arg_quals = cp_type_quals (arg);
17530 int parm_quals = cp_type_quals (parm);
17531
17532 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17533 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17534 {
17535 /* Although a CVR qualifier is ignored when being applied to a
17536 substituted template parameter ([8.3.2]/1 for example), that
17537 does not allow us to unify "const T" with "int&" because both
17538 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17539 It is ok when we're allowing additional CV qualifiers
17540 at the outer level [14.8.2.1]/3,1st bullet. */
17541 if ((TREE_CODE (arg) == REFERENCE_TYPE
17542 || TREE_CODE (arg) == FUNCTION_TYPE
17543 || TREE_CODE (arg) == METHOD_TYPE)
17544 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17545 return 0;
17546
17547 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17548 && (parm_quals & TYPE_QUAL_RESTRICT))
17549 return 0;
17550 }
17551
17552 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17553 && (arg_quals & parm_quals) != parm_quals)
17554 return 0;
17555
17556 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17557 && (parm_quals & arg_quals) != arg_quals)
17558 return 0;
17559
17560 return 1;
17561 }
17562
17563 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17564 void
17565 template_parm_level_and_index (tree parm, int* level, int* index)
17566 {
17567 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17568 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17569 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17570 {
17571 *index = TEMPLATE_TYPE_IDX (parm);
17572 *level = TEMPLATE_TYPE_LEVEL (parm);
17573 }
17574 else
17575 {
17576 *index = TEMPLATE_PARM_IDX (parm);
17577 *level = TEMPLATE_PARM_LEVEL (parm);
17578 }
17579 }
17580
17581 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17582 do { \
17583 if (unify (TP, TA, P, A, S, EP)) \
17584 return 1; \
17585 } while (0);
17586
17587 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17588 expansion at the end of PACKED_PARMS. Returns 0 if the type
17589 deduction succeeds, 1 otherwise. STRICT is the same as in
17590 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17591 call argument list. We'll need to adjust the arguments to make them
17592 types. SUBR tells us if this is from a recursive call to
17593 type_unification_real, or for comparing two template argument
17594 lists. */
17595
17596 static int
17597 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17598 tree packed_args, unification_kind_t strict,
17599 bool subr, bool explain_p)
17600 {
17601 tree parm
17602 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17603 tree pattern = PACK_EXPANSION_PATTERN (parm);
17604 tree pack, packs = NULL_TREE;
17605 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17606
17607 packed_args = expand_template_argument_pack (packed_args);
17608
17609 int len = TREE_VEC_LENGTH (packed_args);
17610
17611 /* Determine the parameter packs we will be deducing from the
17612 pattern, and record their current deductions. */
17613 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17614 pack; pack = TREE_CHAIN (pack))
17615 {
17616 tree parm_pack = TREE_VALUE (pack);
17617 int idx, level;
17618
17619 /* Determine the index and level of this parameter pack. */
17620 template_parm_level_and_index (parm_pack, &level, &idx);
17621
17622 /* Keep track of the parameter packs and their corresponding
17623 argument packs. */
17624 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17625 TREE_TYPE (packs) = make_tree_vec (len - start);
17626 }
17627
17628 /* Loop through all of the arguments that have not yet been
17629 unified and unify each with the pattern. */
17630 for (i = start; i < len; i++)
17631 {
17632 tree parm;
17633 bool any_explicit = false;
17634 tree arg = TREE_VEC_ELT (packed_args, i);
17635
17636 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17637 or the element of its argument pack at the current index if
17638 this argument was explicitly specified. */
17639 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17640 {
17641 int idx, level;
17642 tree arg, pargs;
17643 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17644
17645 arg = NULL_TREE;
17646 if (TREE_VALUE (pack)
17647 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17648 && (i - start < TREE_VEC_LENGTH (pargs)))
17649 {
17650 any_explicit = true;
17651 arg = TREE_VEC_ELT (pargs, i - start);
17652 }
17653 TMPL_ARG (targs, level, idx) = arg;
17654 }
17655
17656 /* If we had explicit template arguments, substitute them into the
17657 pattern before deduction. */
17658 if (any_explicit)
17659 {
17660 /* Some arguments might still be unspecified or dependent. */
17661 bool dependent;
17662 ++processing_template_decl;
17663 dependent = any_dependent_template_arguments_p (targs);
17664 if (!dependent)
17665 --processing_template_decl;
17666 parm = tsubst (pattern, targs,
17667 explain_p ? tf_warning_or_error : tf_none,
17668 NULL_TREE);
17669 if (dependent)
17670 --processing_template_decl;
17671 if (parm == error_mark_node)
17672 return 1;
17673 }
17674 else
17675 parm = pattern;
17676
17677 /* Unify the pattern with the current argument. */
17678 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17679 explain_p))
17680 return 1;
17681
17682 /* For each parameter pack, collect the deduced value. */
17683 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17684 {
17685 int idx, level;
17686 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17687
17688 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17689 TMPL_ARG (targs, level, idx);
17690 }
17691 }
17692
17693 /* Verify that the results of unification with the parameter packs
17694 produce results consistent with what we've seen before, and make
17695 the deduced argument packs available. */
17696 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17697 {
17698 tree old_pack = TREE_VALUE (pack);
17699 tree new_args = TREE_TYPE (pack);
17700 int i, len = TREE_VEC_LENGTH (new_args);
17701 int idx, level;
17702 bool nondeduced_p = false;
17703
17704 /* By default keep the original deduced argument pack.
17705 If necessary, more specific code is going to update the
17706 resulting deduced argument later down in this function. */
17707 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17708 TMPL_ARG (targs, level, idx) = old_pack;
17709
17710 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17711 actually deduce anything. */
17712 for (i = 0; i < len && !nondeduced_p; ++i)
17713 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17714 nondeduced_p = true;
17715 if (nondeduced_p)
17716 continue;
17717
17718 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17719 {
17720 /* If we had fewer function args than explicit template args,
17721 just use the explicits. */
17722 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17723 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17724 if (len < explicit_len)
17725 new_args = explicit_args;
17726 }
17727
17728 if (!old_pack)
17729 {
17730 tree result;
17731 /* Build the deduced *_ARGUMENT_PACK. */
17732 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17733 {
17734 result = make_node (NONTYPE_ARGUMENT_PACK);
17735 TREE_TYPE (result) =
17736 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17737 TREE_CONSTANT (result) = 1;
17738 }
17739 else
17740 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17741
17742 SET_ARGUMENT_PACK_ARGS (result, new_args);
17743
17744 /* Note the deduced argument packs for this parameter
17745 pack. */
17746 TMPL_ARG (targs, level, idx) = result;
17747 }
17748 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17749 && (ARGUMENT_PACK_ARGS (old_pack)
17750 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17751 {
17752 /* We only had the explicitly-provided arguments before, but
17753 now we have a complete set of arguments. */
17754 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17755
17756 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17757 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17758 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17759 }
17760 else
17761 {
17762 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17763 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17764
17765 if (!comp_template_args_with_info (old_args, new_args,
17766 &bad_old_arg, &bad_new_arg))
17767 /* Inconsistent unification of this parameter pack. */
17768 return unify_parameter_pack_inconsistent (explain_p,
17769 bad_old_arg,
17770 bad_new_arg);
17771 }
17772 }
17773
17774 return unify_success (explain_p);
17775 }
17776
17777 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17778 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17779 parameters and return value are as for unify. */
17780
17781 static int
17782 unify_array_domain (tree tparms, tree targs,
17783 tree parm_dom, tree arg_dom,
17784 bool explain_p)
17785 {
17786 tree parm_max;
17787 tree arg_max;
17788 bool parm_cst;
17789 bool arg_cst;
17790
17791 /* Our representation of array types uses "N - 1" as the
17792 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17793 not an integer constant. We cannot unify arbitrarily
17794 complex expressions, so we eliminate the MINUS_EXPRs
17795 here. */
17796 parm_max = TYPE_MAX_VALUE (parm_dom);
17797 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17798 if (!parm_cst)
17799 {
17800 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17801 parm_max = TREE_OPERAND (parm_max, 0);
17802 }
17803 arg_max = TYPE_MAX_VALUE (arg_dom);
17804 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17805 if (!arg_cst)
17806 {
17807 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17808 trying to unify the type of a variable with the type
17809 of a template parameter. For example:
17810
17811 template <unsigned int N>
17812 void f (char (&) [N]);
17813 int g();
17814 void h(int i) {
17815 char a[g(i)];
17816 f(a);
17817 }
17818
17819 Here, the type of the ARG will be "int [g(i)]", and
17820 may be a SAVE_EXPR, etc. */
17821 if (TREE_CODE (arg_max) != MINUS_EXPR)
17822 return unify_vla_arg (explain_p, arg_dom);
17823 arg_max = TREE_OPERAND (arg_max, 0);
17824 }
17825
17826 /* If only one of the bounds used a MINUS_EXPR, compensate
17827 by adding one to the other bound. */
17828 if (parm_cst && !arg_cst)
17829 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17830 integer_type_node,
17831 parm_max,
17832 integer_one_node);
17833 else if (arg_cst && !parm_cst)
17834 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17835 integer_type_node,
17836 arg_max,
17837 integer_one_node);
17838
17839 return unify (tparms, targs, parm_max, arg_max,
17840 UNIFY_ALLOW_INTEGER, explain_p);
17841 }
17842
17843 /* Deduce the value of template parameters. TPARMS is the (innermost)
17844 set of template parameters to a template. TARGS is the bindings
17845 for those template parameters, as determined thus far; TARGS may
17846 include template arguments for outer levels of template parameters
17847 as well. PARM is a parameter to a template function, or a
17848 subcomponent of that parameter; ARG is the corresponding argument.
17849 This function attempts to match PARM with ARG in a manner
17850 consistent with the existing assignments in TARGS. If more values
17851 are deduced, then TARGS is updated.
17852
17853 Returns 0 if the type deduction succeeds, 1 otherwise. The
17854 parameter STRICT is a bitwise or of the following flags:
17855
17856 UNIFY_ALLOW_NONE:
17857 Require an exact match between PARM and ARG.
17858 UNIFY_ALLOW_MORE_CV_QUAL:
17859 Allow the deduced ARG to be more cv-qualified (by qualification
17860 conversion) than ARG.
17861 UNIFY_ALLOW_LESS_CV_QUAL:
17862 Allow the deduced ARG to be less cv-qualified than ARG.
17863 UNIFY_ALLOW_DERIVED:
17864 Allow the deduced ARG to be a template base class of ARG,
17865 or a pointer to a template base class of the type pointed to by
17866 ARG.
17867 UNIFY_ALLOW_INTEGER:
17868 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17869 case for more information.
17870 UNIFY_ALLOW_OUTER_LEVEL:
17871 This is the outermost level of a deduction. Used to determine validity
17872 of qualification conversions. A valid qualification conversion must
17873 have const qualified pointers leading up to the inner type which
17874 requires additional CV quals, except at the outer level, where const
17875 is not required [conv.qual]. It would be normal to set this flag in
17876 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17877 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17878 This is the outermost level of a deduction, and PARM can be more CV
17879 qualified at this point.
17880 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17881 This is the outermost level of a deduction, and PARM can be less CV
17882 qualified at this point. */
17883
17884 static int
17885 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17886 bool explain_p)
17887 {
17888 int idx;
17889 tree targ;
17890 tree tparm;
17891 int strict_in = strict;
17892
17893 /* I don't think this will do the right thing with respect to types.
17894 But the only case I've seen it in so far has been array bounds, where
17895 signedness is the only information lost, and I think that will be
17896 okay. */
17897 while (TREE_CODE (parm) == NOP_EXPR)
17898 parm = TREE_OPERAND (parm, 0);
17899
17900 if (arg == error_mark_node)
17901 return unify_invalid (explain_p);
17902 if (arg == unknown_type_node
17903 || arg == init_list_type_node)
17904 /* We can't deduce anything from this, but we might get all the
17905 template args from other function args. */
17906 return unify_success (explain_p);
17907
17908 /* If PARM uses template parameters, then we can't bail out here,
17909 even if ARG == PARM, since we won't record unifications for the
17910 template parameters. We might need them if we're trying to
17911 figure out which of two things is more specialized. */
17912 if (arg == parm && !uses_template_parms (parm))
17913 return unify_success (explain_p);
17914
17915 /* Handle init lists early, so the rest of the function can assume
17916 we're dealing with a type. */
17917 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17918 {
17919 tree elt, elttype;
17920 unsigned i;
17921 tree orig_parm = parm;
17922
17923 /* Replace T with std::initializer_list<T> for deduction. */
17924 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17925 && flag_deduce_init_list)
17926 parm = listify (parm);
17927
17928 if (!is_std_init_list (parm)
17929 && TREE_CODE (parm) != ARRAY_TYPE)
17930 /* We can only deduce from an initializer list argument if the
17931 parameter is std::initializer_list or an array; otherwise this
17932 is a non-deduced context. */
17933 return unify_success (explain_p);
17934
17935 if (TREE_CODE (parm) == ARRAY_TYPE)
17936 elttype = TREE_TYPE (parm);
17937 else
17938 {
17939 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17940 /* Deduction is defined in terms of a single type, so just punt
17941 on the (bizarre) std::initializer_list<T...>. */
17942 if (PACK_EXPANSION_P (elttype))
17943 return unify_success (explain_p);
17944 }
17945
17946 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17947 {
17948 int elt_strict = strict;
17949
17950 if (elt == error_mark_node)
17951 return unify_invalid (explain_p);
17952
17953 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17954 {
17955 tree type = TREE_TYPE (elt);
17956 if (type == error_mark_node)
17957 return unify_invalid (explain_p);
17958 /* It should only be possible to get here for a call. */
17959 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17960 elt_strict |= maybe_adjust_types_for_deduction
17961 (DEDUCE_CALL, &elttype, &type, elt);
17962 elt = type;
17963 }
17964
17965 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17966 explain_p);
17967 }
17968
17969 if (TREE_CODE (parm) == ARRAY_TYPE
17970 && deducible_array_bound (TYPE_DOMAIN (parm)))
17971 {
17972 /* Also deduce from the length of the initializer list. */
17973 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17974 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17975 if (idx == error_mark_node)
17976 return unify_invalid (explain_p);
17977 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17978 idx, explain_p);
17979 }
17980
17981 /* If the std::initializer_list<T> deduction worked, replace the
17982 deduced A with std::initializer_list<A>. */
17983 if (orig_parm != parm)
17984 {
17985 idx = TEMPLATE_TYPE_IDX (orig_parm);
17986 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17987 targ = listify (targ);
17988 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17989 }
17990 return unify_success (explain_p);
17991 }
17992
17993 /* Immediately reject some pairs that won't unify because of
17994 cv-qualification mismatches. */
17995 if (TREE_CODE (arg) == TREE_CODE (parm)
17996 && TYPE_P (arg)
17997 /* It is the elements of the array which hold the cv quals of an array
17998 type, and the elements might be template type parms. We'll check
17999 when we recurse. */
18000 && TREE_CODE (arg) != ARRAY_TYPE
18001 /* We check the cv-qualifiers when unifying with template type
18002 parameters below. We want to allow ARG `const T' to unify with
18003 PARM `T' for example, when computing which of two templates
18004 is more specialized, for example. */
18005 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
18006 && !check_cv_quals_for_unify (strict_in, arg, parm))
18007 return unify_cv_qual_mismatch (explain_p, parm, arg);
18008
18009 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
18010 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
18011 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
18012 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
18013 strict &= ~UNIFY_ALLOW_DERIVED;
18014 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18015 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
18016
18017 switch (TREE_CODE (parm))
18018 {
18019 case TYPENAME_TYPE:
18020 case SCOPE_REF:
18021 case UNBOUND_CLASS_TEMPLATE:
18022 /* In a type which contains a nested-name-specifier, template
18023 argument values cannot be deduced for template parameters used
18024 within the nested-name-specifier. */
18025 return unify_success (explain_p);
18026
18027 case TEMPLATE_TYPE_PARM:
18028 case TEMPLATE_TEMPLATE_PARM:
18029 case BOUND_TEMPLATE_TEMPLATE_PARM:
18030 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18031 if (error_operand_p (tparm))
18032 return unify_invalid (explain_p);
18033
18034 if (TEMPLATE_TYPE_LEVEL (parm)
18035 != template_decl_level (tparm))
18036 /* The PARM is not one we're trying to unify. Just check
18037 to see if it matches ARG. */
18038 {
18039 if (TREE_CODE (arg) == TREE_CODE (parm)
18040 && (is_auto (parm) ? is_auto (arg)
18041 : same_type_p (parm, arg)))
18042 return unify_success (explain_p);
18043 else
18044 return unify_type_mismatch (explain_p, parm, arg);
18045 }
18046 idx = TEMPLATE_TYPE_IDX (parm);
18047 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18048 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
18049 if (error_operand_p (tparm))
18050 return unify_invalid (explain_p);
18051
18052 /* Check for mixed types and values. */
18053 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18054 && TREE_CODE (tparm) != TYPE_DECL)
18055 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18056 && TREE_CODE (tparm) != TEMPLATE_DECL))
18057 gcc_unreachable ();
18058
18059 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18060 {
18061 /* ARG must be constructed from a template class or a template
18062 template parameter. */
18063 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
18064 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
18065 return unify_template_deduction_failure (explain_p, parm, arg);
18066 {
18067 tree parmvec = TYPE_TI_ARGS (parm);
18068 /* An alias template name is never deduced. */
18069 if (TYPE_ALIAS_P (arg))
18070 arg = strip_typedefs (arg);
18071 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
18072 tree full_argvec = add_to_template_args (targs, argvec);
18073 tree parm_parms
18074 = DECL_INNERMOST_TEMPLATE_PARMS
18075 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
18076 int i, len;
18077 int parm_variadic_p = 0;
18078
18079 /* The resolution to DR150 makes clear that default
18080 arguments for an N-argument may not be used to bind T
18081 to a template template parameter with fewer than N
18082 parameters. It is not safe to permit the binding of
18083 default arguments as an extension, as that may change
18084 the meaning of a conforming program. Consider:
18085
18086 struct Dense { static const unsigned int dim = 1; };
18087
18088 template <template <typename> class View,
18089 typename Block>
18090 void operator+(float, View<Block> const&);
18091
18092 template <typename Block,
18093 unsigned int Dim = Block::dim>
18094 struct Lvalue_proxy { operator float() const; };
18095
18096 void
18097 test_1d (void) {
18098 Lvalue_proxy<Dense> p;
18099 float b;
18100 b + p;
18101 }
18102
18103 Here, if Lvalue_proxy is permitted to bind to View, then
18104 the global operator+ will be used; if they are not, the
18105 Lvalue_proxy will be converted to float. */
18106 if (coerce_template_parms (parm_parms,
18107 full_argvec,
18108 TYPE_TI_TEMPLATE (parm),
18109 (explain_p
18110 ? tf_warning_or_error
18111 : tf_none),
18112 /*require_all_args=*/true,
18113 /*use_default_args=*/false)
18114 == error_mark_node)
18115 return 1;
18116
18117 /* Deduce arguments T, i from TT<T> or TT<i>.
18118 We check each element of PARMVEC and ARGVEC individually
18119 rather than the whole TREE_VEC since they can have
18120 different number of elements. */
18121
18122 parmvec = expand_template_argument_pack (parmvec);
18123 argvec = expand_template_argument_pack (argvec);
18124
18125 len = TREE_VEC_LENGTH (parmvec);
18126
18127 /* Check if the parameters end in a pack, making them
18128 variadic. */
18129 if (len > 0
18130 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18131 parm_variadic_p = 1;
18132
18133 for (i = 0; i < len - parm_variadic_p; ++i)
18134 /* If the template argument list of P contains a pack
18135 expansion that is not the last template argument, the
18136 entire template argument list is a non-deduced
18137 context. */
18138 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18139 return unify_success (explain_p);
18140
18141 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18142 return unify_too_few_arguments (explain_p,
18143 TREE_VEC_LENGTH (argvec), len);
18144
18145 for (i = 0; i < len - parm_variadic_p; ++i)
18146 {
18147 RECUR_AND_CHECK_FAILURE (tparms, targs,
18148 TREE_VEC_ELT (parmvec, i),
18149 TREE_VEC_ELT (argvec, i),
18150 UNIFY_ALLOW_NONE, explain_p);
18151 }
18152
18153 if (parm_variadic_p
18154 && unify_pack_expansion (tparms, targs,
18155 parmvec, argvec,
18156 DEDUCE_EXACT,
18157 /*subr=*/true, explain_p))
18158 return 1;
18159 }
18160 arg = TYPE_TI_TEMPLATE (arg);
18161
18162 /* Fall through to deduce template name. */
18163 }
18164
18165 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18166 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18167 {
18168 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18169
18170 /* Simple cases: Value already set, does match or doesn't. */
18171 if (targ != NULL_TREE && template_args_equal (targ, arg))
18172 return unify_success (explain_p);
18173 else if (targ)
18174 return unify_inconsistency (explain_p, parm, targ, arg);
18175 }
18176 else
18177 {
18178 /* If PARM is `const T' and ARG is only `int', we don't have
18179 a match unless we are allowing additional qualification.
18180 If ARG is `const int' and PARM is just `T' that's OK;
18181 that binds `const int' to `T'. */
18182 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18183 arg, parm))
18184 return unify_cv_qual_mismatch (explain_p, parm, arg);
18185
18186 /* Consider the case where ARG is `const volatile int' and
18187 PARM is `const T'. Then, T should be `volatile int'. */
18188 arg = cp_build_qualified_type_real
18189 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18190 if (arg == error_mark_node)
18191 return unify_invalid (explain_p);
18192
18193 /* Simple cases: Value already set, does match or doesn't. */
18194 if (targ != NULL_TREE && same_type_p (targ, arg))
18195 return unify_success (explain_p);
18196 else if (targ)
18197 return unify_inconsistency (explain_p, parm, targ, arg);
18198
18199 /* Make sure that ARG is not a variable-sized array. (Note
18200 that were talking about variable-sized arrays (like
18201 `int[n]'), rather than arrays of unknown size (like
18202 `int[]').) We'll get very confused by such a type since
18203 the bound of the array is not constant, and therefore
18204 not mangleable. Besides, such types are not allowed in
18205 ISO C++, so we can do as we please here. We do allow
18206 them for 'auto' deduction, since that isn't ABI-exposed. */
18207 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18208 return unify_vla_arg (explain_p, arg);
18209
18210 /* Strip typedefs as in convert_template_argument. */
18211 arg = canonicalize_type_argument (arg, tf_none);
18212 }
18213
18214 /* If ARG is a parameter pack or an expansion, we cannot unify
18215 against it unless PARM is also a parameter pack. */
18216 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18217 && !template_parameter_pack_p (parm))
18218 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18219
18220 /* If the argument deduction results is a METHOD_TYPE,
18221 then there is a problem.
18222 METHOD_TYPE doesn't map to any real C++ type the result of
18223 the deduction can not be of that type. */
18224 if (TREE_CODE (arg) == METHOD_TYPE)
18225 return unify_method_type_error (explain_p, arg);
18226
18227 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18228 return unify_success (explain_p);
18229
18230 case TEMPLATE_PARM_INDEX:
18231 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18232 if (error_operand_p (tparm))
18233 return unify_invalid (explain_p);
18234
18235 if (TEMPLATE_PARM_LEVEL (parm)
18236 != template_decl_level (tparm))
18237 {
18238 /* The PARM is not one we're trying to unify. Just check
18239 to see if it matches ARG. */
18240 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18241 && cp_tree_equal (parm, arg));
18242 if (result)
18243 unify_expression_unequal (explain_p, parm, arg);
18244 return result;
18245 }
18246
18247 idx = TEMPLATE_PARM_IDX (parm);
18248 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18249
18250 if (targ)
18251 {
18252 int x = !cp_tree_equal (targ, arg);
18253 if (x)
18254 unify_inconsistency (explain_p, parm, targ, arg);
18255 return x;
18256 }
18257
18258 /* [temp.deduct.type] If, in the declaration of a function template
18259 with a non-type template-parameter, the non-type
18260 template-parameter is used in an expression in the function
18261 parameter-list and, if the corresponding template-argument is
18262 deduced, the template-argument type shall match the type of the
18263 template-parameter exactly, except that a template-argument
18264 deduced from an array bound may be of any integral type.
18265 The non-type parameter might use already deduced type parameters. */
18266 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18267 if (!TREE_TYPE (arg))
18268 /* Template-parameter dependent expression. Just accept it for now.
18269 It will later be processed in convert_template_argument. */
18270 ;
18271 else if (same_type_p (TREE_TYPE (arg), tparm))
18272 /* OK */;
18273 else if ((strict & UNIFY_ALLOW_INTEGER)
18274 && CP_INTEGRAL_TYPE_P (tparm))
18275 /* Convert the ARG to the type of PARM; the deduced non-type
18276 template argument must exactly match the types of the
18277 corresponding parameter. */
18278 arg = fold (build_nop (tparm, arg));
18279 else if (uses_template_parms (tparm))
18280 /* We haven't deduced the type of this parameter yet. Try again
18281 later. */
18282 return unify_success (explain_p);
18283 else
18284 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18285
18286 /* If ARG is a parameter pack or an expansion, we cannot unify
18287 against it unless PARM is also a parameter pack. */
18288 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18289 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18290 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18291
18292 {
18293 bool removed_attr = false;
18294 arg = strip_typedefs_expr (arg, &removed_attr);
18295 }
18296 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18297 return unify_success (explain_p);
18298
18299 case PTRMEM_CST:
18300 {
18301 /* A pointer-to-member constant can be unified only with
18302 another constant. */
18303 if (TREE_CODE (arg) != PTRMEM_CST)
18304 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18305
18306 /* Just unify the class member. It would be useless (and possibly
18307 wrong, depending on the strict flags) to unify also
18308 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18309 arg refer to the same variable, even if through different
18310 classes. For instance:
18311
18312 struct A { int x; };
18313 struct B : A { };
18314
18315 Unification of &A::x and &B::x must succeed. */
18316 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18317 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18318 }
18319
18320 case POINTER_TYPE:
18321 {
18322 if (!TYPE_PTR_P (arg))
18323 return unify_type_mismatch (explain_p, parm, arg);
18324
18325 /* [temp.deduct.call]
18326
18327 A can be another pointer or pointer to member type that can
18328 be converted to the deduced A via a qualification
18329 conversion (_conv.qual_).
18330
18331 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18332 This will allow for additional cv-qualification of the
18333 pointed-to types if appropriate. */
18334
18335 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18336 /* The derived-to-base conversion only persists through one
18337 level of pointers. */
18338 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18339
18340 return unify (tparms, targs, TREE_TYPE (parm),
18341 TREE_TYPE (arg), strict, explain_p);
18342 }
18343
18344 case REFERENCE_TYPE:
18345 if (TREE_CODE (arg) != REFERENCE_TYPE)
18346 return unify_type_mismatch (explain_p, parm, arg);
18347 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18348 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18349
18350 case ARRAY_TYPE:
18351 if (TREE_CODE (arg) != ARRAY_TYPE)
18352 return unify_type_mismatch (explain_p, parm, arg);
18353 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18354 != (TYPE_DOMAIN (arg) == NULL_TREE))
18355 return unify_type_mismatch (explain_p, parm, arg);
18356 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18357 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18358 if (TYPE_DOMAIN (parm) != NULL_TREE)
18359 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18360 TYPE_DOMAIN (arg), explain_p);
18361 return unify_success (explain_p);
18362
18363 case REAL_TYPE:
18364 case COMPLEX_TYPE:
18365 case VECTOR_TYPE:
18366 case INTEGER_TYPE:
18367 case BOOLEAN_TYPE:
18368 case ENUMERAL_TYPE:
18369 case VOID_TYPE:
18370 case NULLPTR_TYPE:
18371 if (TREE_CODE (arg) != TREE_CODE (parm))
18372 return unify_type_mismatch (explain_p, parm, arg);
18373
18374 /* We have already checked cv-qualification at the top of the
18375 function. */
18376 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18377 return unify_type_mismatch (explain_p, parm, arg);
18378
18379 /* As far as unification is concerned, this wins. Later checks
18380 will invalidate it if necessary. */
18381 return unify_success (explain_p);
18382
18383 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18384 /* Type INTEGER_CST can come from ordinary constant template args. */
18385 case INTEGER_CST:
18386 while (TREE_CODE (arg) == NOP_EXPR)
18387 arg = TREE_OPERAND (arg, 0);
18388
18389 if (TREE_CODE (arg) != INTEGER_CST)
18390 return unify_template_argument_mismatch (explain_p, parm, arg);
18391 return (tree_int_cst_equal (parm, arg)
18392 ? unify_success (explain_p)
18393 : unify_template_argument_mismatch (explain_p, parm, arg));
18394
18395 case TREE_VEC:
18396 {
18397 int i, len, argslen;
18398 int parm_variadic_p = 0;
18399
18400 if (TREE_CODE (arg) != TREE_VEC)
18401 return unify_template_argument_mismatch (explain_p, parm, arg);
18402
18403 len = TREE_VEC_LENGTH (parm);
18404 argslen = TREE_VEC_LENGTH (arg);
18405
18406 /* Check for pack expansions in the parameters. */
18407 for (i = 0; i < len; ++i)
18408 {
18409 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18410 {
18411 if (i == len - 1)
18412 /* We can unify against something with a trailing
18413 parameter pack. */
18414 parm_variadic_p = 1;
18415 else
18416 /* [temp.deduct.type]/9: If the template argument list of
18417 P contains a pack expansion that is not the last
18418 template argument, the entire template argument list
18419 is a non-deduced context. */
18420 return unify_success (explain_p);
18421 }
18422 }
18423
18424 /* If we don't have enough arguments to satisfy the parameters
18425 (not counting the pack expression at the end), or we have
18426 too many arguments for a parameter list that doesn't end in
18427 a pack expression, we can't unify. */
18428 if (parm_variadic_p
18429 ? argslen < len - parm_variadic_p
18430 : argslen != len)
18431 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18432
18433 /* Unify all of the parameters that precede the (optional)
18434 pack expression. */
18435 for (i = 0; i < len - parm_variadic_p; ++i)
18436 {
18437 RECUR_AND_CHECK_FAILURE (tparms, targs,
18438 TREE_VEC_ELT (parm, i),
18439 TREE_VEC_ELT (arg, i),
18440 UNIFY_ALLOW_NONE, explain_p);
18441 }
18442 if (parm_variadic_p)
18443 return unify_pack_expansion (tparms, targs, parm, arg,
18444 DEDUCE_EXACT,
18445 /*subr=*/true, explain_p);
18446 return unify_success (explain_p);
18447 }
18448
18449 case RECORD_TYPE:
18450 case UNION_TYPE:
18451 if (TREE_CODE (arg) != TREE_CODE (parm))
18452 return unify_type_mismatch (explain_p, parm, arg);
18453
18454 if (TYPE_PTRMEMFUNC_P (parm))
18455 {
18456 if (!TYPE_PTRMEMFUNC_P (arg))
18457 return unify_type_mismatch (explain_p, parm, arg);
18458
18459 return unify (tparms, targs,
18460 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18461 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18462 strict, explain_p);
18463 }
18464 else if (TYPE_PTRMEMFUNC_P (arg))
18465 return unify_type_mismatch (explain_p, parm, arg);
18466
18467 if (CLASSTYPE_TEMPLATE_INFO (parm))
18468 {
18469 tree t = NULL_TREE;
18470
18471 if (strict_in & UNIFY_ALLOW_DERIVED)
18472 {
18473 /* First, we try to unify the PARM and ARG directly. */
18474 t = try_class_unification (tparms, targs,
18475 parm, arg, explain_p);
18476
18477 if (!t)
18478 {
18479 /* Fallback to the special case allowed in
18480 [temp.deduct.call]:
18481
18482 If P is a class, and P has the form
18483 template-id, then A can be a derived class of
18484 the deduced A. Likewise, if P is a pointer to
18485 a class of the form template-id, A can be a
18486 pointer to a derived class pointed to by the
18487 deduced A. */
18488 enum template_base_result r;
18489 r = get_template_base (tparms, targs, parm, arg,
18490 explain_p, &t);
18491
18492 if (!t)
18493 return unify_no_common_base (explain_p, r, parm, arg);
18494 }
18495 }
18496 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18497 && (CLASSTYPE_TI_TEMPLATE (parm)
18498 == CLASSTYPE_TI_TEMPLATE (arg)))
18499 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18500 Then, we should unify `int' and `U'. */
18501 t = arg;
18502 else
18503 /* There's no chance of unification succeeding. */
18504 return unify_type_mismatch (explain_p, parm, arg);
18505
18506 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18507 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18508 }
18509 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18510 return unify_type_mismatch (explain_p, parm, arg);
18511 return unify_success (explain_p);
18512
18513 case METHOD_TYPE:
18514 case FUNCTION_TYPE:
18515 {
18516 unsigned int nargs;
18517 tree *args;
18518 tree a;
18519 unsigned int i;
18520
18521 if (TREE_CODE (arg) != TREE_CODE (parm))
18522 return unify_type_mismatch (explain_p, parm, arg);
18523
18524 /* CV qualifications for methods can never be deduced, they must
18525 match exactly. We need to check them explicitly here,
18526 because type_unification_real treats them as any other
18527 cv-qualified parameter. */
18528 if (TREE_CODE (parm) == METHOD_TYPE
18529 && (!check_cv_quals_for_unify
18530 (UNIFY_ALLOW_NONE,
18531 class_of_this_parm (arg),
18532 class_of_this_parm (parm))))
18533 return unify_cv_qual_mismatch (explain_p, parm, arg);
18534
18535 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18536 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18537
18538 nargs = list_length (TYPE_ARG_TYPES (arg));
18539 args = XALLOCAVEC (tree, nargs);
18540 for (a = TYPE_ARG_TYPES (arg), i = 0;
18541 a != NULL_TREE && a != void_list_node;
18542 a = TREE_CHAIN (a), ++i)
18543 args[i] = TREE_VALUE (a);
18544 nargs = i;
18545
18546 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18547 args, nargs, 1, DEDUCE_EXACT,
18548 LOOKUP_NORMAL, NULL, explain_p);
18549 }
18550
18551 case OFFSET_TYPE:
18552 /* Unify a pointer to member with a pointer to member function, which
18553 deduces the type of the member as a function type. */
18554 if (TYPE_PTRMEMFUNC_P (arg))
18555 {
18556 /* Check top-level cv qualifiers */
18557 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18558 return unify_cv_qual_mismatch (explain_p, parm, arg);
18559
18560 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18561 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18562 UNIFY_ALLOW_NONE, explain_p);
18563
18564 /* Determine the type of the function we are unifying against. */
18565 tree fntype = static_fn_type (arg);
18566
18567 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18568 }
18569
18570 if (TREE_CODE (arg) != OFFSET_TYPE)
18571 return unify_type_mismatch (explain_p, parm, arg);
18572 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18573 TYPE_OFFSET_BASETYPE (arg),
18574 UNIFY_ALLOW_NONE, explain_p);
18575 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18576 strict, explain_p);
18577
18578 case CONST_DECL:
18579 if (DECL_TEMPLATE_PARM_P (parm))
18580 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18581 if (arg != scalar_constant_value (parm))
18582 return unify_template_argument_mismatch (explain_p, parm, arg);
18583 return unify_success (explain_p);
18584
18585 case FIELD_DECL:
18586 case TEMPLATE_DECL:
18587 /* Matched cases are handled by the ARG == PARM test above. */
18588 return unify_template_argument_mismatch (explain_p, parm, arg);
18589
18590 case VAR_DECL:
18591 /* A non-type template parameter that is a variable should be a
18592 an integral constant, in which case, it whould have been
18593 folded into its (constant) value. So we should not be getting
18594 a variable here. */
18595 gcc_unreachable ();
18596
18597 case TYPE_ARGUMENT_PACK:
18598 case NONTYPE_ARGUMENT_PACK:
18599 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18600 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18601
18602 case TYPEOF_TYPE:
18603 case DECLTYPE_TYPE:
18604 case UNDERLYING_TYPE:
18605 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18606 or UNDERLYING_TYPE nodes. */
18607 return unify_success (explain_p);
18608
18609 case ERROR_MARK:
18610 /* Unification fails if we hit an error node. */
18611 return unify_invalid (explain_p);
18612
18613 case INDIRECT_REF:
18614 if (REFERENCE_REF_P (parm))
18615 {
18616 if (REFERENCE_REF_P (arg))
18617 arg = TREE_OPERAND (arg, 0);
18618 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18619 strict, explain_p);
18620 }
18621 /* FALLTHRU */
18622
18623 default:
18624 /* An unresolved overload is a nondeduced context. */
18625 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18626 return unify_success (explain_p);
18627 gcc_assert (EXPR_P (parm));
18628
18629 /* We must be looking at an expression. This can happen with
18630 something like:
18631
18632 template <int I>
18633 void foo(S<I>, S<I + 2>);
18634
18635 This is a "nondeduced context":
18636
18637 [deduct.type]
18638
18639 The nondeduced contexts are:
18640
18641 --A type that is a template-id in which one or more of
18642 the template-arguments is an expression that references
18643 a template-parameter.
18644
18645 In these cases, we assume deduction succeeded, but don't
18646 actually infer any unifications. */
18647
18648 if (!uses_template_parms (parm)
18649 && !template_args_equal (parm, arg))
18650 return unify_expression_unequal (explain_p, parm, arg);
18651 else
18652 return unify_success (explain_p);
18653 }
18654 }
18655 #undef RECUR_AND_CHECK_FAILURE
18656 \f
18657 /* Note that DECL can be defined in this translation unit, if
18658 required. */
18659
18660 static void
18661 mark_definable (tree decl)
18662 {
18663 tree clone;
18664 DECL_NOT_REALLY_EXTERN (decl) = 1;
18665 FOR_EACH_CLONE (clone, decl)
18666 DECL_NOT_REALLY_EXTERN (clone) = 1;
18667 }
18668
18669 /* Called if RESULT is explicitly instantiated, or is a member of an
18670 explicitly instantiated class. */
18671
18672 void
18673 mark_decl_instantiated (tree result, int extern_p)
18674 {
18675 SET_DECL_EXPLICIT_INSTANTIATION (result);
18676
18677 /* If this entity has already been written out, it's too late to
18678 make any modifications. */
18679 if (TREE_ASM_WRITTEN (result))
18680 return;
18681
18682 /* For anonymous namespace we don't need to do anything. */
18683 if (decl_anon_ns_mem_p (result))
18684 {
18685 gcc_assert (!TREE_PUBLIC (result));
18686 return;
18687 }
18688
18689 if (TREE_CODE (result) != FUNCTION_DECL)
18690 /* The TREE_PUBLIC flag for function declarations will have been
18691 set correctly by tsubst. */
18692 TREE_PUBLIC (result) = 1;
18693
18694 /* This might have been set by an earlier implicit instantiation. */
18695 DECL_COMDAT (result) = 0;
18696
18697 if (extern_p)
18698 DECL_NOT_REALLY_EXTERN (result) = 0;
18699 else
18700 {
18701 mark_definable (result);
18702 mark_needed (result);
18703 /* Always make artificials weak. */
18704 if (DECL_ARTIFICIAL (result) && flag_weak)
18705 comdat_linkage (result);
18706 /* For WIN32 we also want to put explicit instantiations in
18707 linkonce sections. */
18708 else if (TREE_PUBLIC (result))
18709 maybe_make_one_only (result);
18710 }
18711
18712 /* If EXTERN_P, then this function will not be emitted -- unless
18713 followed by an explicit instantiation, at which point its linkage
18714 will be adjusted. If !EXTERN_P, then this function will be
18715 emitted here. In neither circumstance do we want
18716 import_export_decl to adjust the linkage. */
18717 DECL_INTERFACE_KNOWN (result) = 1;
18718 }
18719
18720 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18721 important template arguments. If any are missing, we check whether
18722 they're important by using error_mark_node for substituting into any
18723 args that were used for partial ordering (the ones between ARGS and END)
18724 and seeing if it bubbles up. */
18725
18726 static bool
18727 check_undeduced_parms (tree targs, tree args, tree end)
18728 {
18729 bool found = false;
18730 int i;
18731 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18732 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18733 {
18734 found = true;
18735 TREE_VEC_ELT (targs, i) = error_mark_node;
18736 }
18737 if (found)
18738 {
18739 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18740 if (substed == error_mark_node)
18741 return true;
18742 }
18743 return false;
18744 }
18745
18746 /* Given two function templates PAT1 and PAT2, return:
18747
18748 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18749 -1 if PAT2 is more specialized than PAT1.
18750 0 if neither is more specialized.
18751
18752 LEN indicates the number of parameters we should consider
18753 (defaulted parameters should not be considered).
18754
18755 The 1998 std underspecified function template partial ordering, and
18756 DR214 addresses the issue. We take pairs of arguments, one from
18757 each of the templates, and deduce them against each other. One of
18758 the templates will be more specialized if all the *other*
18759 template's arguments deduce against its arguments and at least one
18760 of its arguments *does* *not* deduce against the other template's
18761 corresponding argument. Deduction is done as for class templates.
18762 The arguments used in deduction have reference and top level cv
18763 qualifiers removed. Iff both arguments were originally reference
18764 types *and* deduction succeeds in both directions, an lvalue reference
18765 wins against an rvalue reference and otherwise the template
18766 with the more cv-qualified argument wins for that pairing (if
18767 neither is more cv-qualified, they both are equal). Unlike regular
18768 deduction, after all the arguments have been deduced in this way,
18769 we do *not* verify the deduced template argument values can be
18770 substituted into non-deduced contexts.
18771
18772 The logic can be a bit confusing here, because we look at deduce1 and
18773 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18774 can find template arguments for pat1 to make arg1 look like arg2, that
18775 means that arg2 is at least as specialized as arg1. */
18776
18777 int
18778 more_specialized_fn (tree pat1, tree pat2, int len)
18779 {
18780 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18781 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18782 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18783 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18784 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18785 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18786 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18787 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18788 tree origs1, origs2;
18789 bool lose1 = false;
18790 bool lose2 = false;
18791
18792 /* Remove the this parameter from non-static member functions. If
18793 one is a non-static member function and the other is not a static
18794 member function, remove the first parameter from that function
18795 also. This situation occurs for operator functions where we
18796 locate both a member function (with this pointer) and non-member
18797 operator (with explicit first operand). */
18798 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18799 {
18800 len--; /* LEN is the number of significant arguments for DECL1 */
18801 args1 = TREE_CHAIN (args1);
18802 if (!DECL_STATIC_FUNCTION_P (decl2))
18803 args2 = TREE_CHAIN (args2);
18804 }
18805 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18806 {
18807 args2 = TREE_CHAIN (args2);
18808 if (!DECL_STATIC_FUNCTION_P (decl1))
18809 {
18810 len--;
18811 args1 = TREE_CHAIN (args1);
18812 }
18813 }
18814
18815 /* If only one is a conversion operator, they are unordered. */
18816 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18817 return 0;
18818
18819 /* Consider the return type for a conversion function */
18820 if (DECL_CONV_FN_P (decl1))
18821 {
18822 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18823 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18824 len++;
18825 }
18826
18827 processing_template_decl++;
18828
18829 origs1 = args1;
18830 origs2 = args2;
18831
18832 while (len--
18833 /* Stop when an ellipsis is seen. */
18834 && args1 != NULL_TREE && args2 != NULL_TREE)
18835 {
18836 tree arg1 = TREE_VALUE (args1);
18837 tree arg2 = TREE_VALUE (args2);
18838 int deduce1, deduce2;
18839 int quals1 = -1;
18840 int quals2 = -1;
18841 int ref1 = 0;
18842 int ref2 = 0;
18843
18844 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18845 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18846 {
18847 /* When both arguments are pack expansions, we need only
18848 unify the patterns themselves. */
18849 arg1 = PACK_EXPANSION_PATTERN (arg1);
18850 arg2 = PACK_EXPANSION_PATTERN (arg2);
18851
18852 /* This is the last comparison we need to do. */
18853 len = 0;
18854 }
18855
18856 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18857 {
18858 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18859 arg1 = TREE_TYPE (arg1);
18860 quals1 = cp_type_quals (arg1);
18861 }
18862
18863 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18864 {
18865 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18866 arg2 = TREE_TYPE (arg2);
18867 quals2 = cp_type_quals (arg2);
18868 }
18869
18870 arg1 = TYPE_MAIN_VARIANT (arg1);
18871 arg2 = TYPE_MAIN_VARIANT (arg2);
18872
18873 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18874 {
18875 int i, len2 = list_length (args2);
18876 tree parmvec = make_tree_vec (1);
18877 tree argvec = make_tree_vec (len2);
18878 tree ta = args2;
18879
18880 /* Setup the parameter vector, which contains only ARG1. */
18881 TREE_VEC_ELT (parmvec, 0) = arg1;
18882
18883 /* Setup the argument vector, which contains the remaining
18884 arguments. */
18885 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18886 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18887
18888 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18889 argvec, DEDUCE_EXACT,
18890 /*subr=*/true, /*explain_p=*/false)
18891 == 0);
18892
18893 /* We cannot deduce in the other direction, because ARG1 is
18894 a pack expansion but ARG2 is not. */
18895 deduce2 = 0;
18896 }
18897 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18898 {
18899 int i, len1 = list_length (args1);
18900 tree parmvec = make_tree_vec (1);
18901 tree argvec = make_tree_vec (len1);
18902 tree ta = args1;
18903
18904 /* Setup the parameter vector, which contains only ARG1. */
18905 TREE_VEC_ELT (parmvec, 0) = arg2;
18906
18907 /* Setup the argument vector, which contains the remaining
18908 arguments. */
18909 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18910 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18911
18912 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18913 argvec, DEDUCE_EXACT,
18914 /*subr=*/true, /*explain_p=*/false)
18915 == 0);
18916
18917 /* We cannot deduce in the other direction, because ARG2 is
18918 a pack expansion but ARG1 is not.*/
18919 deduce1 = 0;
18920 }
18921
18922 else
18923 {
18924 /* The normal case, where neither argument is a pack
18925 expansion. */
18926 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18927 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18928 == 0);
18929 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18930 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18931 == 0);
18932 }
18933
18934 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18935 arg2, then arg2 is not as specialized as arg1. */
18936 if (!deduce1)
18937 lose2 = true;
18938 if (!deduce2)
18939 lose1 = true;
18940
18941 /* "If, for a given type, deduction succeeds in both directions
18942 (i.e., the types are identical after the transformations above)
18943 and both P and A were reference types (before being replaced with
18944 the type referred to above):
18945 - if the type from the argument template was an lvalue reference and
18946 the type from the parameter template was not, the argument type is
18947 considered to be more specialized than the other; otherwise,
18948 - if the type from the argument template is more cv-qualified
18949 than the type from the parameter template (as described above),
18950 the argument type is considered to be more specialized than the other;
18951 otherwise,
18952 - neither type is more specialized than the other." */
18953
18954 if (deduce1 && deduce2)
18955 {
18956 if (ref1 && ref2 && ref1 != ref2)
18957 {
18958 if (ref1 > ref2)
18959 lose1 = true;
18960 else
18961 lose2 = true;
18962 }
18963 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18964 {
18965 if ((quals1 & quals2) == quals2)
18966 lose2 = true;
18967 if ((quals1 & quals2) == quals1)
18968 lose1 = true;
18969 }
18970 }
18971
18972 if (lose1 && lose2)
18973 /* We've failed to deduce something in either direction.
18974 These must be unordered. */
18975 break;
18976
18977 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18978 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18979 /* We have already processed all of the arguments in our
18980 handing of the pack expansion type. */
18981 len = 0;
18982
18983 args1 = TREE_CHAIN (args1);
18984 args2 = TREE_CHAIN (args2);
18985 }
18986
18987 /* "In most cases, all template parameters must have values in order for
18988 deduction to succeed, but for partial ordering purposes a template
18989 parameter may remain without a value provided it is not used in the
18990 types being used for partial ordering."
18991
18992 Thus, if we are missing any of the targs1 we need to substitute into
18993 origs1, then pat2 is not as specialized as pat1. This can happen when
18994 there is a nondeduced context. */
18995 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18996 lose2 = true;
18997 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18998 lose1 = true;
18999
19000 processing_template_decl--;
19001
19002 /* All things being equal, if the next argument is a pack expansion
19003 for one function but not for the other, prefer the
19004 non-variadic function. FIXME this is bogus; see c++/41958. */
19005 if (lose1 == lose2
19006 && args1 && TREE_VALUE (args1)
19007 && args2 && TREE_VALUE (args2))
19008 {
19009 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
19010 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
19011 }
19012
19013 if (lose1 == lose2)
19014 return 0;
19015 else if (!lose1)
19016 return 1;
19017 else
19018 return -1;
19019 }
19020
19021 /* Determine which of two partial specializations of TMPL is more
19022 specialized.
19023
19024 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19025 to the first partial specialization. The TREE_PURPOSE is the
19026 innermost set of template parameters for the partial
19027 specialization. PAT2 is similar, but for the second template.
19028
19029 Return 1 if the first partial specialization is more specialized;
19030 -1 if the second is more specialized; 0 if neither is more
19031 specialized.
19032
19033 See [temp.class.order] for information about determining which of
19034 two templates is more specialized. */
19035
19036 static int
19037 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
19038 {
19039 tree targs;
19040 int winner = 0;
19041 bool any_deductions = false;
19042
19043 tree tmpl1 = TREE_VALUE (pat1);
19044 tree tmpl2 = TREE_VALUE (pat2);
19045 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
19046 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
19047 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
19048 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
19049
19050 /* Just like what happens for functions, if we are ordering between
19051 different template specializations, we may encounter dependent
19052 types in the arguments, and we need our dependency check functions
19053 to behave correctly. */
19054 ++processing_template_decl;
19055 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
19056 if (targs)
19057 {
19058 --winner;
19059 any_deductions = true;
19060 }
19061
19062 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
19063 if (targs)
19064 {
19065 ++winner;
19066 any_deductions = true;
19067 }
19068 --processing_template_decl;
19069
19070 /* In the case of a tie where at least one of the templates
19071 has a parameter pack at the end, the template with the most
19072 non-packed parameters wins. */
19073 if (winner == 0
19074 && any_deductions
19075 && (template_args_variadic_p (TREE_PURPOSE (pat1))
19076 || template_args_variadic_p (TREE_PURPOSE (pat2))))
19077 {
19078 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
19079 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
19080 int len1 = TREE_VEC_LENGTH (args1);
19081 int len2 = TREE_VEC_LENGTH (args2);
19082
19083 /* We don't count the pack expansion at the end. */
19084 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
19085 --len1;
19086 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
19087 --len2;
19088
19089 if (len1 > len2)
19090 return 1;
19091 else if (len1 < len2)
19092 return -1;
19093 }
19094
19095 return winner;
19096 }
19097
19098 /* Return the template arguments that will produce the function signature
19099 DECL from the function template FN, with the explicit template
19100 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19101 also match. Return NULL_TREE if no satisfactory arguments could be
19102 found. */
19103
19104 static tree
19105 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19106 {
19107 int ntparms = DECL_NTPARMS (fn);
19108 tree targs = make_tree_vec (ntparms);
19109 tree decl_type = TREE_TYPE (decl);
19110 tree decl_arg_types;
19111 tree *args;
19112 unsigned int nargs, ix;
19113 tree arg;
19114
19115 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19116
19117 /* Never do unification on the 'this' parameter. */
19118 decl_arg_types = skip_artificial_parms_for (decl,
19119 TYPE_ARG_TYPES (decl_type));
19120
19121 nargs = list_length (decl_arg_types);
19122 args = XALLOCAVEC (tree, nargs);
19123 for (arg = decl_arg_types, ix = 0;
19124 arg != NULL_TREE && arg != void_list_node;
19125 arg = TREE_CHAIN (arg), ++ix)
19126 args[ix] = TREE_VALUE (arg);
19127
19128 if (fn_type_unification (fn, explicit_args, targs,
19129 args, ix,
19130 (check_rettype || DECL_CONV_FN_P (fn)
19131 ? TREE_TYPE (decl_type) : NULL_TREE),
19132 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19133 /*decltype*/false)
19134 == error_mark_node)
19135 return NULL_TREE;
19136
19137 return targs;
19138 }
19139
19140 /* Return the innermost template arguments that, when applied to a partial
19141 specialization of TMPL whose innermost template parameters are
19142 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19143 ARGS.
19144
19145 For example, suppose we have:
19146
19147 template <class T, class U> struct S {};
19148 template <class T> struct S<T*, int> {};
19149
19150 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19151 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19152 int}. The resulting vector will be {double}, indicating that `T'
19153 is bound to `double'. */
19154
19155 static tree
19156 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19157 {
19158 int i, ntparms = TREE_VEC_LENGTH (tparms);
19159 tree deduced_args;
19160 tree innermost_deduced_args;
19161
19162 innermost_deduced_args = make_tree_vec (ntparms);
19163 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19164 {
19165 deduced_args = copy_node (args);
19166 SET_TMPL_ARGS_LEVEL (deduced_args,
19167 TMPL_ARGS_DEPTH (deduced_args),
19168 innermost_deduced_args);
19169 }
19170 else
19171 deduced_args = innermost_deduced_args;
19172
19173 if (unify (tparms, deduced_args,
19174 INNERMOST_TEMPLATE_ARGS (spec_args),
19175 INNERMOST_TEMPLATE_ARGS (args),
19176 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19177 return NULL_TREE;
19178
19179 for (i = 0; i < ntparms; ++i)
19180 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19181 return NULL_TREE;
19182
19183 /* Verify that nondeduced template arguments agree with the type
19184 obtained from argument deduction.
19185
19186 For example:
19187
19188 struct A { typedef int X; };
19189 template <class T, class U> struct C {};
19190 template <class T> struct C<T, typename T::X> {};
19191
19192 Then with the instantiation `C<A, int>', we can deduce that
19193 `T' is `A' but unify () does not check whether `typename T::X'
19194 is `int'. */
19195 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19196 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19197 spec_args, tmpl,
19198 tf_none, false, false);
19199 if (spec_args == error_mark_node
19200 /* We only need to check the innermost arguments; the other
19201 arguments will always agree. */
19202 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19203 INNERMOST_TEMPLATE_ARGS (args)))
19204 return NULL_TREE;
19205
19206 /* Now that we have bindings for all of the template arguments,
19207 ensure that the arguments deduced for the template template
19208 parameters have compatible template parameter lists. See the use
19209 of template_template_parm_bindings_ok_p in fn_type_unification
19210 for more information. */
19211 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19212 return NULL_TREE;
19213
19214 return deduced_args;
19215 }
19216
19217 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19218 Return the TREE_LIST node with the most specialized template, if
19219 any. If there is no most specialized template, the error_mark_node
19220 is returned.
19221
19222 Note that this function does not look at, or modify, the
19223 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19224 returned is one of the elements of INSTANTIATIONS, callers may
19225 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19226 and retrieve it from the value returned. */
19227
19228 tree
19229 most_specialized_instantiation (tree templates)
19230 {
19231 tree fn, champ;
19232
19233 ++processing_template_decl;
19234
19235 champ = templates;
19236 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19237 {
19238 int fate = 0;
19239
19240 if (get_bindings (TREE_VALUE (champ),
19241 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19242 NULL_TREE, /*check_ret=*/true))
19243 fate--;
19244
19245 if (get_bindings (TREE_VALUE (fn),
19246 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19247 NULL_TREE, /*check_ret=*/true))
19248 fate++;
19249
19250 if (fate == -1)
19251 champ = fn;
19252 else if (!fate)
19253 {
19254 /* Equally specialized, move to next function. If there
19255 is no next function, nothing's most specialized. */
19256 fn = TREE_CHAIN (fn);
19257 champ = fn;
19258 if (!fn)
19259 break;
19260 }
19261 }
19262
19263 if (champ)
19264 /* Now verify that champ is better than everything earlier in the
19265 instantiation list. */
19266 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19267 if (get_bindings (TREE_VALUE (champ),
19268 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19269 NULL_TREE, /*check_ret=*/true)
19270 || !get_bindings (TREE_VALUE (fn),
19271 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19272 NULL_TREE, /*check_ret=*/true))
19273 {
19274 champ = NULL_TREE;
19275 break;
19276 }
19277
19278 processing_template_decl--;
19279
19280 if (!champ)
19281 return error_mark_node;
19282
19283 return champ;
19284 }
19285
19286 /* If DECL is a specialization of some template, return the most
19287 general such template. Otherwise, returns NULL_TREE.
19288
19289 For example, given:
19290
19291 template <class T> struct S { template <class U> void f(U); };
19292
19293 if TMPL is `template <class U> void S<int>::f(U)' this will return
19294 the full template. This function will not trace past partial
19295 specializations, however. For example, given in addition:
19296
19297 template <class T> struct S<T*> { template <class U> void f(U); };
19298
19299 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19300 `template <class T> template <class U> S<T*>::f(U)'. */
19301
19302 tree
19303 most_general_template (tree decl)
19304 {
19305 if (TREE_CODE (decl) != TEMPLATE_DECL)
19306 {
19307 if (tree tinfo = get_template_info (decl))
19308 decl = TI_TEMPLATE (tinfo);
19309 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19310 template friend, or a FIELD_DECL for a capture pack. */
19311 if (TREE_CODE (decl) != TEMPLATE_DECL)
19312 return NULL_TREE;
19313 }
19314
19315 /* Look for more and more general templates. */
19316 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19317 {
19318 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19319 (See cp-tree.h for details.) */
19320 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19321 break;
19322
19323 if (CLASS_TYPE_P (TREE_TYPE (decl))
19324 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19325 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19326 break;
19327
19328 /* Stop if we run into an explicitly specialized class template. */
19329 if (!DECL_NAMESPACE_SCOPE_P (decl)
19330 && DECL_CONTEXT (decl)
19331 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19332 break;
19333
19334 decl = DECL_TI_TEMPLATE (decl);
19335 }
19336
19337 return decl;
19338 }
19339
19340 /* Return the most specialized of the template partial specializations
19341 which can produce TARGET, a specialization of some class or variable
19342 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19343 a TEMPLATE_DECL node corresponding to the partial specialization, while
19344 the TREE_PURPOSE is the set of template arguments that must be
19345 substituted into the template pattern in order to generate TARGET.
19346
19347 If the choice of partial specialization is ambiguous, a diagnostic
19348 is issued, and the error_mark_node is returned. If there are no
19349 partial specializations matching TARGET, then NULL_TREE is
19350 returned, indicating that the primary template should be used. */
19351
19352 static tree
19353 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19354 {
19355 tree list = NULL_TREE;
19356 tree t;
19357 tree champ;
19358 int fate;
19359 bool ambiguous_p;
19360 tree outer_args = NULL_TREE;
19361 tree tmpl, args;
19362
19363 if (TYPE_P (target))
19364 {
19365 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19366 tmpl = TI_TEMPLATE (tinfo);
19367 args = TI_ARGS (tinfo);
19368 }
19369 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19370 {
19371 tmpl = TREE_OPERAND (target, 0);
19372 args = TREE_OPERAND (target, 1);
19373 }
19374 else if (VAR_P (target))
19375 {
19376 tree tinfo = DECL_TEMPLATE_INFO (target);
19377 tmpl = TI_TEMPLATE (tinfo);
19378 args = TI_ARGS (tinfo);
19379 }
19380 else
19381 gcc_unreachable ();
19382
19383 tree main_tmpl = most_general_template (tmpl);
19384
19385 /* For determining which partial specialization to use, only the
19386 innermost args are interesting. */
19387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19388 {
19389 outer_args = strip_innermost_template_args (args, 1);
19390 args = INNERMOST_TEMPLATE_ARGS (args);
19391 }
19392
19393 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19394 {
19395 tree partial_spec_args;
19396 tree spec_args;
19397 tree spec_tmpl = TREE_VALUE (t);
19398
19399 partial_spec_args = TREE_PURPOSE (t);
19400
19401 ++processing_template_decl;
19402
19403 if (outer_args)
19404 {
19405 /* Discard the outer levels of args, and then substitute in the
19406 template args from the enclosing class. */
19407 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19408 partial_spec_args = tsubst_template_args
19409 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19410
19411 /* And the same for the partial specialization TEMPLATE_DECL. */
19412 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19413 }
19414
19415 partial_spec_args =
19416 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19417 partial_spec_args,
19418 tmpl, tf_none,
19419 /*require_all_args=*/true,
19420 /*use_default_args=*/true);
19421
19422 --processing_template_decl;
19423
19424 if (partial_spec_args == error_mark_node)
19425 return error_mark_node;
19426 if (spec_tmpl == error_mark_node)
19427 return error_mark_node;
19428
19429 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19430 spec_args = get_partial_spec_bindings (tmpl, parms,
19431 partial_spec_args,
19432 args);
19433 if (spec_args)
19434 {
19435 if (outer_args)
19436 spec_args = add_to_template_args (outer_args, spec_args);
19437 list = tree_cons (spec_args, TREE_VALUE (t), list);
19438 TREE_TYPE (list) = TREE_TYPE (t);
19439 }
19440 }
19441
19442 if (! list)
19443 return NULL_TREE;
19444
19445 ambiguous_p = false;
19446 t = list;
19447 champ = t;
19448 t = TREE_CHAIN (t);
19449 for (; t; t = TREE_CHAIN (t))
19450 {
19451 fate = more_specialized_partial_spec (tmpl, champ, t);
19452 if (fate == 1)
19453 ;
19454 else
19455 {
19456 if (fate == 0)
19457 {
19458 t = TREE_CHAIN (t);
19459 if (! t)
19460 {
19461 ambiguous_p = true;
19462 break;
19463 }
19464 }
19465 champ = t;
19466 }
19467 }
19468
19469 if (!ambiguous_p)
19470 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19471 {
19472 fate = more_specialized_partial_spec (tmpl, champ, t);
19473 if (fate != 1)
19474 {
19475 ambiguous_p = true;
19476 break;
19477 }
19478 }
19479
19480 if (ambiguous_p)
19481 {
19482 const char *str;
19483 char *spaces = NULL;
19484 if (!(complain & tf_error))
19485 return error_mark_node;
19486 if (TYPE_P (target))
19487 error ("ambiguous template instantiation for %q#T", target);
19488 else
19489 error ("ambiguous template instantiation for %q#D", target);
19490 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19491 for (t = list; t; t = TREE_CHAIN (t))
19492 {
19493 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
19494 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
19495 "%s %#S", spaces ? spaces : str, subst);
19496 spaces = spaces ? spaces : get_spaces (str);
19497 }
19498 free (spaces);
19499 return error_mark_node;
19500 }
19501
19502 return champ;
19503 }
19504
19505 /* Explicitly instantiate DECL. */
19506
19507 void
19508 do_decl_instantiation (tree decl, tree storage)
19509 {
19510 tree result = NULL_TREE;
19511 int extern_p = 0;
19512
19513 if (!decl || decl == error_mark_node)
19514 /* An error occurred, for which grokdeclarator has already issued
19515 an appropriate message. */
19516 return;
19517 else if (! DECL_LANG_SPECIFIC (decl))
19518 {
19519 error ("explicit instantiation of non-template %q#D", decl);
19520 return;
19521 }
19522
19523 bool var_templ = (DECL_TEMPLATE_INFO (decl)
19524 && variable_template_p (DECL_TI_TEMPLATE (decl)));
19525
19526 if (VAR_P (decl) && !var_templ)
19527 {
19528 /* There is an asymmetry here in the way VAR_DECLs and
19529 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19530 the latter, the DECL we get back will be marked as a
19531 template instantiation, and the appropriate
19532 DECL_TEMPLATE_INFO will be set up. This does not happen for
19533 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19534 should handle VAR_DECLs as it currently handles
19535 FUNCTION_DECLs. */
19536 if (!DECL_CLASS_SCOPE_P (decl))
19537 {
19538 error ("%qD is not a static data member of a class template", decl);
19539 return;
19540 }
19541 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19542 if (!result || !VAR_P (result))
19543 {
19544 error ("no matching template for %qD found", decl);
19545 return;
19546 }
19547 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19548 {
19549 error ("type %qT for explicit instantiation %qD does not match "
19550 "declared type %qT", TREE_TYPE (result), decl,
19551 TREE_TYPE (decl));
19552 return;
19553 }
19554 }
19555 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
19556 {
19557 error ("explicit instantiation of %q#D", decl);
19558 return;
19559 }
19560 else
19561 result = decl;
19562
19563 /* Check for various error cases. Note that if the explicit
19564 instantiation is valid the RESULT will currently be marked as an
19565 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19566 until we get here. */
19567
19568 if (DECL_TEMPLATE_SPECIALIZATION (result))
19569 {
19570 /* DR 259 [temp.spec].
19571
19572 Both an explicit instantiation and a declaration of an explicit
19573 specialization shall not appear in a program unless the explicit
19574 instantiation follows a declaration of the explicit specialization.
19575
19576 For a given set of template parameters, if an explicit
19577 instantiation of a template appears after a declaration of an
19578 explicit specialization for that template, the explicit
19579 instantiation has no effect. */
19580 return;
19581 }
19582 else if (DECL_EXPLICIT_INSTANTIATION (result))
19583 {
19584 /* [temp.spec]
19585
19586 No program shall explicitly instantiate any template more
19587 than once.
19588
19589 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19590 the first instantiation was `extern' and the second is not,
19591 and EXTERN_P for the opposite case. */
19592 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19593 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19594 /* If an "extern" explicit instantiation follows an ordinary
19595 explicit instantiation, the template is instantiated. */
19596 if (extern_p)
19597 return;
19598 }
19599 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19600 {
19601 error ("no matching template for %qD found", result);
19602 return;
19603 }
19604 else if (!DECL_TEMPLATE_INFO (result))
19605 {
19606 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19607 return;
19608 }
19609
19610 if (storage == NULL_TREE)
19611 ;
19612 else if (storage == ridpointers[(int) RID_EXTERN])
19613 {
19614 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19615 pedwarn (input_location, OPT_Wpedantic,
19616 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19617 "instantiations");
19618 extern_p = 1;
19619 }
19620 else
19621 error ("storage class %qD applied to template instantiation", storage);
19622
19623 check_explicit_instantiation_namespace (result);
19624 mark_decl_instantiated (result, extern_p);
19625 if (! extern_p)
19626 instantiate_decl (result, /*defer_ok=*/1,
19627 /*expl_inst_class_mem_p=*/false);
19628 }
19629
19630 static void
19631 mark_class_instantiated (tree t, int extern_p)
19632 {
19633 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19634 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19635 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19636 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19637 if (! extern_p)
19638 {
19639 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19640 rest_of_type_compilation (t, 1);
19641 }
19642 }
19643
19644 /* Called from do_type_instantiation through binding_table_foreach to
19645 do recursive instantiation for the type bound in ENTRY. */
19646 static void
19647 bt_instantiate_type_proc (binding_entry entry, void *data)
19648 {
19649 tree storage = *(tree *) data;
19650
19651 if (MAYBE_CLASS_TYPE_P (entry->type)
19652 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19653 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19654 }
19655
19656 /* Called from do_type_instantiation to instantiate a member
19657 (a member function or a static member variable) of an
19658 explicitly instantiated class template. */
19659 static void
19660 instantiate_class_member (tree decl, int extern_p)
19661 {
19662 mark_decl_instantiated (decl, extern_p);
19663 if (! extern_p)
19664 instantiate_decl (decl, /*defer_ok=*/1,
19665 /*expl_inst_class_mem_p=*/true);
19666 }
19667
19668 /* Perform an explicit instantiation of template class T. STORAGE, if
19669 non-null, is the RID for extern, inline or static. COMPLAIN is
19670 nonzero if this is called from the parser, zero if called recursively,
19671 since the standard is unclear (as detailed below). */
19672
19673 void
19674 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19675 {
19676 int extern_p = 0;
19677 int nomem_p = 0;
19678 int static_p = 0;
19679 int previous_instantiation_extern_p = 0;
19680
19681 if (TREE_CODE (t) == TYPE_DECL)
19682 t = TREE_TYPE (t);
19683
19684 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19685 {
19686 tree tmpl =
19687 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19688 if (tmpl)
19689 error ("explicit instantiation of non-class template %qD", tmpl);
19690 else
19691 error ("explicit instantiation of non-template type %qT", t);
19692 return;
19693 }
19694
19695 complete_type (t);
19696
19697 if (!COMPLETE_TYPE_P (t))
19698 {
19699 if (complain & tf_error)
19700 error ("explicit instantiation of %q#T before definition of template",
19701 t);
19702 return;
19703 }
19704
19705 if (storage != NULL_TREE)
19706 {
19707 if (!in_system_header_at (input_location))
19708 {
19709 if (storage == ridpointers[(int) RID_EXTERN])
19710 {
19711 if (cxx_dialect == cxx98)
19712 pedwarn (input_location, OPT_Wpedantic,
19713 "ISO C++ 1998 forbids the use of %<extern%> on "
19714 "explicit instantiations");
19715 }
19716 else
19717 pedwarn (input_location, OPT_Wpedantic,
19718 "ISO C++ forbids the use of %qE"
19719 " on explicit instantiations", storage);
19720 }
19721
19722 if (storage == ridpointers[(int) RID_INLINE])
19723 nomem_p = 1;
19724 else if (storage == ridpointers[(int) RID_EXTERN])
19725 extern_p = 1;
19726 else if (storage == ridpointers[(int) RID_STATIC])
19727 static_p = 1;
19728 else
19729 {
19730 error ("storage class %qD applied to template instantiation",
19731 storage);
19732 extern_p = 0;
19733 }
19734 }
19735
19736 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19737 {
19738 /* DR 259 [temp.spec].
19739
19740 Both an explicit instantiation and a declaration of an explicit
19741 specialization shall not appear in a program unless the explicit
19742 instantiation follows a declaration of the explicit specialization.
19743
19744 For a given set of template parameters, if an explicit
19745 instantiation of a template appears after a declaration of an
19746 explicit specialization for that template, the explicit
19747 instantiation has no effect. */
19748 return;
19749 }
19750 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19751 {
19752 /* [temp.spec]
19753
19754 No program shall explicitly instantiate any template more
19755 than once.
19756
19757 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19758 instantiation was `extern'. If EXTERN_P then the second is.
19759 These cases are OK. */
19760 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19761
19762 if (!previous_instantiation_extern_p && !extern_p
19763 && (complain & tf_error))
19764 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19765
19766 /* If we've already instantiated the template, just return now. */
19767 if (!CLASSTYPE_INTERFACE_ONLY (t))
19768 return;
19769 }
19770
19771 check_explicit_instantiation_namespace (TYPE_NAME (t));
19772 mark_class_instantiated (t, extern_p);
19773
19774 if (nomem_p)
19775 return;
19776
19777 {
19778 tree tmp;
19779
19780 /* In contrast to implicit instantiation, where only the
19781 declarations, and not the definitions, of members are
19782 instantiated, we have here:
19783
19784 [temp.explicit]
19785
19786 The explicit instantiation of a class template specialization
19787 implies the instantiation of all of its members not
19788 previously explicitly specialized in the translation unit
19789 containing the explicit instantiation.
19790
19791 Of course, we can't instantiate member template classes, since
19792 we don't have any arguments for them. Note that the standard
19793 is unclear on whether the instantiation of the members are
19794 *explicit* instantiations or not. However, the most natural
19795 interpretation is that it should be an explicit instantiation. */
19796
19797 if (! static_p)
19798 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19799 if (TREE_CODE (tmp) == FUNCTION_DECL
19800 && DECL_TEMPLATE_INSTANTIATION (tmp))
19801 instantiate_class_member (tmp, extern_p);
19802
19803 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19804 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19805 instantiate_class_member (tmp, extern_p);
19806
19807 if (CLASSTYPE_NESTED_UTDS (t))
19808 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19809 bt_instantiate_type_proc, &storage);
19810 }
19811 }
19812
19813 /* Given a function DECL, which is a specialization of TMPL, modify
19814 DECL to be a re-instantiation of TMPL with the same template
19815 arguments. TMPL should be the template into which tsubst'ing
19816 should occur for DECL, not the most general template.
19817
19818 One reason for doing this is a scenario like this:
19819
19820 template <class T>
19821 void f(const T&, int i);
19822
19823 void g() { f(3, 7); }
19824
19825 template <class T>
19826 void f(const T& t, const int i) { }
19827
19828 Note that when the template is first instantiated, with
19829 instantiate_template, the resulting DECL will have no name for the
19830 first parameter, and the wrong type for the second. So, when we go
19831 to instantiate the DECL, we regenerate it. */
19832
19833 static void
19834 regenerate_decl_from_template (tree decl, tree tmpl)
19835 {
19836 /* The arguments used to instantiate DECL, from the most general
19837 template. */
19838 tree args;
19839 tree code_pattern;
19840
19841 args = DECL_TI_ARGS (decl);
19842 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19843
19844 /* Make sure that we can see identifiers, and compute access
19845 correctly. */
19846 push_access_scope (decl);
19847
19848 if (TREE_CODE (decl) == FUNCTION_DECL)
19849 {
19850 tree decl_parm;
19851 tree pattern_parm;
19852 tree specs;
19853 int args_depth;
19854 int parms_depth;
19855
19856 args_depth = TMPL_ARGS_DEPTH (args);
19857 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19858 if (args_depth > parms_depth)
19859 args = get_innermost_template_args (args, parms_depth);
19860
19861 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19862 args, tf_error, NULL_TREE,
19863 /*defer_ok*/false);
19864 if (specs && specs != error_mark_node)
19865 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19866 specs);
19867
19868 /* Merge parameter declarations. */
19869 decl_parm = skip_artificial_parms_for (decl,
19870 DECL_ARGUMENTS (decl));
19871 pattern_parm
19872 = skip_artificial_parms_for (code_pattern,
19873 DECL_ARGUMENTS (code_pattern));
19874 while (decl_parm && !DECL_PACK_P (pattern_parm))
19875 {
19876 tree parm_type;
19877 tree attributes;
19878
19879 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19880 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19881 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19882 NULL_TREE);
19883 parm_type = type_decays_to (parm_type);
19884 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19885 TREE_TYPE (decl_parm) = parm_type;
19886 attributes = DECL_ATTRIBUTES (pattern_parm);
19887 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19888 {
19889 DECL_ATTRIBUTES (decl_parm) = attributes;
19890 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19891 }
19892 decl_parm = DECL_CHAIN (decl_parm);
19893 pattern_parm = DECL_CHAIN (pattern_parm);
19894 }
19895 /* Merge any parameters that match with the function parameter
19896 pack. */
19897 if (pattern_parm && DECL_PACK_P (pattern_parm))
19898 {
19899 int i, len;
19900 tree expanded_types;
19901 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19902 the parameters in this function parameter pack. */
19903 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19904 args, tf_error, NULL_TREE);
19905 len = TREE_VEC_LENGTH (expanded_types);
19906 for (i = 0; i < len; i++)
19907 {
19908 tree parm_type;
19909 tree attributes;
19910
19911 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19912 /* Rename the parameter to include the index. */
19913 DECL_NAME (decl_parm) =
19914 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19915 parm_type = TREE_VEC_ELT (expanded_types, i);
19916 parm_type = type_decays_to (parm_type);
19917 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19918 TREE_TYPE (decl_parm) = parm_type;
19919 attributes = DECL_ATTRIBUTES (pattern_parm);
19920 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19921 {
19922 DECL_ATTRIBUTES (decl_parm) = attributes;
19923 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19924 }
19925 decl_parm = DECL_CHAIN (decl_parm);
19926 }
19927 }
19928 /* Merge additional specifiers from the CODE_PATTERN. */
19929 if (DECL_DECLARED_INLINE_P (code_pattern)
19930 && !DECL_DECLARED_INLINE_P (decl))
19931 DECL_DECLARED_INLINE_P (decl) = 1;
19932 }
19933 else if (VAR_P (decl))
19934 {
19935 DECL_INITIAL (decl) =
19936 tsubst_expr (DECL_INITIAL (code_pattern), args,
19937 tf_error, DECL_TI_TEMPLATE (decl),
19938 /*integral_constant_expression_p=*/false);
19939 if (VAR_HAD_UNKNOWN_BOUND (decl))
19940 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19941 tf_error, DECL_TI_TEMPLATE (decl));
19942 }
19943 else
19944 gcc_unreachable ();
19945
19946 pop_access_scope (decl);
19947 }
19948
19949 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19950 substituted to get DECL. */
19951
19952 tree
19953 template_for_substitution (tree decl)
19954 {
19955 tree tmpl = DECL_TI_TEMPLATE (decl);
19956
19957 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19958 for the instantiation. This is not always the most general
19959 template. Consider, for example:
19960
19961 template <class T>
19962 struct S { template <class U> void f();
19963 template <> void f<int>(); };
19964
19965 and an instantiation of S<double>::f<int>. We want TD to be the
19966 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19967 while (/* An instantiation cannot have a definition, so we need a
19968 more general template. */
19969 DECL_TEMPLATE_INSTANTIATION (tmpl)
19970 /* We must also deal with friend templates. Given:
19971
19972 template <class T> struct S {
19973 template <class U> friend void f() {};
19974 };
19975
19976 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19977 so far as the language is concerned, but that's still
19978 where we get the pattern for the instantiation from. On
19979 other hand, if the definition comes outside the class, say:
19980
19981 template <class T> struct S {
19982 template <class U> friend void f();
19983 };
19984 template <class U> friend void f() {}
19985
19986 we don't need to look any further. That's what the check for
19987 DECL_INITIAL is for. */
19988 || (TREE_CODE (decl) == FUNCTION_DECL
19989 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19990 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19991 {
19992 /* The present template, TD, should not be a definition. If it
19993 were a definition, we should be using it! Note that we
19994 cannot restructure the loop to just keep going until we find
19995 a template with a definition, since that might go too far if
19996 a specialization was declared, but not defined. */
19997
19998 /* Fetch the more general template. */
19999 tmpl = DECL_TI_TEMPLATE (tmpl);
20000 }
20001
20002 return tmpl;
20003 }
20004
20005 /* Returns true if we need to instantiate this template instance even if we
20006 know we aren't going to emit it.. */
20007
20008 bool
20009 always_instantiate_p (tree decl)
20010 {
20011 /* We always instantiate inline functions so that we can inline them. An
20012 explicit instantiation declaration prohibits implicit instantiation of
20013 non-inline functions. With high levels of optimization, we would
20014 normally inline non-inline functions -- but we're not allowed to do
20015 that for "extern template" functions. Therefore, we check
20016 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20017 return ((TREE_CODE (decl) == FUNCTION_DECL
20018 && (DECL_DECLARED_INLINE_P (decl)
20019 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
20020 /* And we need to instantiate static data members so that
20021 their initializers are available in integral constant
20022 expressions. */
20023 || (VAR_P (decl)
20024 && decl_maybe_constant_var_p (decl)));
20025 }
20026
20027 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20028 instantiate it now, modifying TREE_TYPE (fn). */
20029
20030 void
20031 maybe_instantiate_noexcept (tree fn)
20032 {
20033 tree fntype, spec, noex, clone;
20034
20035 /* Don't instantiate a noexcept-specification from template context. */
20036 if (processing_template_decl)
20037 return;
20038
20039 if (DECL_CLONED_FUNCTION_P (fn))
20040 fn = DECL_CLONED_FUNCTION (fn);
20041 fntype = TREE_TYPE (fn);
20042 spec = TYPE_RAISES_EXCEPTIONS (fntype);
20043
20044 if (!spec || !TREE_PURPOSE (spec))
20045 return;
20046
20047 noex = TREE_PURPOSE (spec);
20048
20049 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
20050 {
20051 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
20052 spec = get_defaulted_eh_spec (fn);
20053 else if (push_tinst_level (fn))
20054 {
20055 push_access_scope (fn);
20056 push_deferring_access_checks (dk_no_deferred);
20057 input_location = DECL_SOURCE_LOCATION (fn);
20058 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
20059 DEFERRED_NOEXCEPT_ARGS (noex),
20060 tf_warning_or_error, fn,
20061 /*function_p=*/false,
20062 /*integral_constant_expression_p=*/true);
20063 pop_deferring_access_checks ();
20064 pop_access_scope (fn);
20065 pop_tinst_level ();
20066 spec = build_noexcept_spec (noex, tf_warning_or_error);
20067 if (spec == error_mark_node)
20068 spec = noexcept_false_spec;
20069 }
20070 else
20071 spec = noexcept_false_spec;
20072
20073 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
20074 }
20075
20076 FOR_EACH_CLONE (clone, fn)
20077 {
20078 if (TREE_TYPE (clone) == fntype)
20079 TREE_TYPE (clone) = TREE_TYPE (fn);
20080 else
20081 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
20082 }
20083 }
20084
20085 /* Produce the definition of D, a _DECL generated from a template. If
20086 DEFER_OK is nonzero, then we don't have to actually do the
20087 instantiation now; we just have to do it sometime. Normally it is
20088 an error if this is an explicit instantiation but D is undefined.
20089 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20090 explicitly instantiated class template. */
20091
20092 tree
20093 instantiate_decl (tree d, int defer_ok,
20094 bool expl_inst_class_mem_p)
20095 {
20096 tree tmpl = DECL_TI_TEMPLATE (d);
20097 tree gen_args;
20098 tree args;
20099 tree td;
20100 tree code_pattern;
20101 tree spec;
20102 tree gen_tmpl;
20103 bool pattern_defined;
20104 location_t saved_loc = input_location;
20105 int saved_unevaluated_operand = cp_unevaluated_operand;
20106 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20107 bool external_p;
20108 bool deleted_p;
20109 tree fn_context;
20110 bool nested;
20111
20112 /* This function should only be used to instantiate templates for
20113 functions and static member variables. */
20114 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20115
20116 /* Variables are never deferred; if instantiation is required, they
20117 are instantiated right away. That allows for better code in the
20118 case that an expression refers to the value of the variable --
20119 if the variable has a constant value the referring expression can
20120 take advantage of that fact. */
20121 if (VAR_P (d)
20122 || DECL_DECLARED_CONSTEXPR_P (d))
20123 defer_ok = 0;
20124
20125 /* Don't instantiate cloned functions. Instead, instantiate the
20126 functions they cloned. */
20127 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20128 d = DECL_CLONED_FUNCTION (d);
20129
20130 if (DECL_TEMPLATE_INSTANTIATED (d)
20131 || (TREE_CODE (d) == FUNCTION_DECL
20132 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20133 || DECL_TEMPLATE_SPECIALIZATION (d))
20134 /* D has already been instantiated or explicitly specialized, so
20135 there's nothing for us to do here.
20136
20137 It might seem reasonable to check whether or not D is an explicit
20138 instantiation, and, if so, stop here. But when an explicit
20139 instantiation is deferred until the end of the compilation,
20140 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20141 the instantiation. */
20142 return d;
20143
20144 /* Check to see whether we know that this template will be
20145 instantiated in some other file, as with "extern template"
20146 extension. */
20147 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20148
20149 /* In general, we do not instantiate such templates. */
20150 if (external_p && !always_instantiate_p (d))
20151 return d;
20152
20153 gen_tmpl = most_general_template (tmpl);
20154 gen_args = DECL_TI_ARGS (d);
20155
20156 if (tmpl != gen_tmpl)
20157 /* We should already have the extra args. */
20158 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20159 == TMPL_ARGS_DEPTH (gen_args));
20160 /* And what's in the hash table should match D. */
20161 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20162 || spec == NULL_TREE);
20163
20164 /* This needs to happen before any tsubsting. */
20165 if (! push_tinst_level (d))
20166 return d;
20167
20168 timevar_push (TV_TEMPLATE_INST);
20169
20170 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20171 for the instantiation. */
20172 td = template_for_substitution (d);
20173 code_pattern = DECL_TEMPLATE_RESULT (td);
20174
20175 /* We should never be trying to instantiate a member of a class
20176 template or partial specialization. */
20177 gcc_assert (d != code_pattern);
20178
20179 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20180 || DECL_TEMPLATE_SPECIALIZATION (td))
20181 /* In the case of a friend template whose definition is provided
20182 outside the class, we may have too many arguments. Drop the
20183 ones we don't need. The same is true for specializations. */
20184 args = get_innermost_template_args
20185 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20186 else
20187 args = gen_args;
20188
20189 if (TREE_CODE (d) == FUNCTION_DECL)
20190 {
20191 deleted_p = DECL_DELETED_FN (code_pattern);
20192 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20193 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20194 || deleted_p);
20195 }
20196 else
20197 {
20198 deleted_p = false;
20199 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20200 }
20201
20202 /* We may be in the middle of deferred access check. Disable it now. */
20203 push_deferring_access_checks (dk_no_deferred);
20204
20205 /* Unless an explicit instantiation directive has already determined
20206 the linkage of D, remember that a definition is available for
20207 this entity. */
20208 if (pattern_defined
20209 && !DECL_INTERFACE_KNOWN (d)
20210 && !DECL_NOT_REALLY_EXTERN (d))
20211 mark_definable (d);
20212
20213 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20214 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20215 input_location = DECL_SOURCE_LOCATION (d);
20216
20217 /* If D is a member of an explicitly instantiated class template,
20218 and no definition is available, treat it like an implicit
20219 instantiation. */
20220 if (!pattern_defined && expl_inst_class_mem_p
20221 && DECL_EXPLICIT_INSTANTIATION (d))
20222 {
20223 /* Leave linkage flags alone on instantiations with anonymous
20224 visibility. */
20225 if (TREE_PUBLIC (d))
20226 {
20227 DECL_NOT_REALLY_EXTERN (d) = 0;
20228 DECL_INTERFACE_KNOWN (d) = 0;
20229 }
20230 SET_DECL_IMPLICIT_INSTANTIATION (d);
20231 }
20232
20233 /* Defer all other templates, unless we have been explicitly
20234 forbidden from doing so. */
20235 if (/* If there is no definition, we cannot instantiate the
20236 template. */
20237 ! pattern_defined
20238 /* If it's OK to postpone instantiation, do so. */
20239 || defer_ok
20240 /* If this is a static data member that will be defined
20241 elsewhere, we don't want to instantiate the entire data
20242 member, but we do want to instantiate the initializer so that
20243 we can substitute that elsewhere. */
20244 || (external_p && VAR_P (d))
20245 /* Handle here a deleted function too, avoid generating
20246 its body (c++/61080). */
20247 || deleted_p)
20248 {
20249 /* The definition of the static data member is now required so
20250 we must substitute the initializer. */
20251 if (VAR_P (d)
20252 && !DECL_INITIAL (d)
20253 && DECL_INITIAL (code_pattern))
20254 {
20255 tree ns;
20256 tree init;
20257 bool const_init = false;
20258 bool enter_context = DECL_CLASS_SCOPE_P (d);
20259
20260 ns = decl_namespace_context (d);
20261 push_nested_namespace (ns);
20262 if (enter_context)
20263 push_nested_class (DECL_CONTEXT (d));
20264 init = tsubst_expr (DECL_INITIAL (code_pattern),
20265 args,
20266 tf_warning_or_error, NULL_TREE,
20267 /*integral_constant_expression_p=*/false);
20268 /* If instantiating the initializer involved instantiating this
20269 again, don't call cp_finish_decl twice. */
20270 if (!DECL_INITIAL (d))
20271 {
20272 /* Make sure the initializer is still constant, in case of
20273 circular dependency (template/instantiate6.C). */
20274 const_init
20275 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20276 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20277 /*asmspec_tree=*/NULL_TREE,
20278 LOOKUP_ONLYCONVERTING);
20279 }
20280 if (enter_context)
20281 pop_nested_class ();
20282 pop_nested_namespace (ns);
20283 }
20284
20285 /* We restore the source position here because it's used by
20286 add_pending_template. */
20287 input_location = saved_loc;
20288
20289 if (at_eof && !pattern_defined
20290 && DECL_EXPLICIT_INSTANTIATION (d)
20291 && DECL_NOT_REALLY_EXTERN (d))
20292 /* [temp.explicit]
20293
20294 The definition of a non-exported function template, a
20295 non-exported member function template, or a non-exported
20296 member function or static data member of a class template
20297 shall be present in every translation unit in which it is
20298 explicitly instantiated. */
20299 permerror (input_location, "explicit instantiation of %qD "
20300 "but no definition available", d);
20301
20302 /* If we're in unevaluated context, we just wanted to get the
20303 constant value; this isn't an odr use, so don't queue
20304 a full instantiation. */
20305 if (cp_unevaluated_operand != 0)
20306 goto out;
20307 /* ??? Historically, we have instantiated inline functions, even
20308 when marked as "extern template". */
20309 if (!(external_p && VAR_P (d)))
20310 add_pending_template (d);
20311 goto out;
20312 }
20313 /* Tell the repository that D is available in this translation unit
20314 -- and see if it is supposed to be instantiated here. */
20315 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20316 {
20317 /* In a PCH file, despite the fact that the repository hasn't
20318 requested instantiation in the PCH it is still possible that
20319 an instantiation will be required in a file that includes the
20320 PCH. */
20321 if (pch_file)
20322 add_pending_template (d);
20323 /* Instantiate inline functions so that the inliner can do its
20324 job, even though we'll not be emitting a copy of this
20325 function. */
20326 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20327 goto out;
20328 }
20329
20330 fn_context = decl_function_context (d);
20331 nested = (current_function_decl != NULL_TREE);
20332 if (!fn_context)
20333 push_to_top_level ();
20334 else
20335 {
20336 if (nested)
20337 push_function_context ();
20338 cp_unevaluated_operand = 0;
20339 c_inhibit_evaluation_warnings = 0;
20340 }
20341
20342 /* Mark D as instantiated so that recursive calls to
20343 instantiate_decl do not try to instantiate it again. */
20344 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20345
20346 /* Regenerate the declaration in case the template has been modified
20347 by a subsequent redeclaration. */
20348 regenerate_decl_from_template (d, td);
20349
20350 /* We already set the file and line above. Reset them now in case
20351 they changed as a result of calling regenerate_decl_from_template. */
20352 input_location = DECL_SOURCE_LOCATION (d);
20353
20354 if (VAR_P (d))
20355 {
20356 tree init;
20357 bool const_init = false;
20358
20359 /* Clear out DECL_RTL; whatever was there before may not be right
20360 since we've reset the type of the declaration. */
20361 SET_DECL_RTL (d, NULL);
20362 DECL_IN_AGGR_P (d) = 0;
20363
20364 /* The initializer is placed in DECL_INITIAL by
20365 regenerate_decl_from_template so we don't need to
20366 push/pop_access_scope again here. Pull it out so that
20367 cp_finish_decl can process it. */
20368 init = DECL_INITIAL (d);
20369 DECL_INITIAL (d) = NULL_TREE;
20370 DECL_INITIALIZED_P (d) = 0;
20371
20372 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20373 initializer. That function will defer actual emission until
20374 we have a chance to determine linkage. */
20375 DECL_EXTERNAL (d) = 0;
20376
20377 /* Enter the scope of D so that access-checking works correctly. */
20378 bool enter_context = DECL_CLASS_SCOPE_P (d);
20379 if (enter_context)
20380 push_nested_class (DECL_CONTEXT (d));
20381
20382 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20383 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20384
20385 if (enter_context)
20386 pop_nested_class ();
20387
20388 if (variable_template_p (td))
20389 note_variable_template_instantiation (d);
20390 }
20391 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20392 synthesize_method (d);
20393 else if (TREE_CODE (d) == FUNCTION_DECL)
20394 {
20395 hash_map<tree, tree> *saved_local_specializations;
20396 tree subst_decl;
20397 tree tmpl_parm;
20398 tree spec_parm;
20399 tree block = NULL_TREE;
20400
20401 /* Save away the current list, in case we are instantiating one
20402 template from within the body of another. */
20403 saved_local_specializations = local_specializations;
20404
20405 /* Set up the list of local specializations. */
20406 local_specializations = new hash_map<tree, tree>;
20407
20408 /* Set up context. */
20409 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20410 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20411 block = push_stmt_list ();
20412 else
20413 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20414
20415 /* Some typedefs referenced from within the template code need to be
20416 access checked at template instantiation time, i.e now. These
20417 types were added to the template at parsing time. Let's get those
20418 and perform the access checks then. */
20419 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20420 gen_args);
20421
20422 /* Create substitution entries for the parameters. */
20423 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20424 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20425 spec_parm = DECL_ARGUMENTS (d);
20426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20427 {
20428 register_local_specialization (spec_parm, tmpl_parm);
20429 spec_parm = skip_artificial_parms_for (d, spec_parm);
20430 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20431 }
20432 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20433 {
20434 if (!DECL_PACK_P (tmpl_parm))
20435 {
20436 register_local_specialization (spec_parm, tmpl_parm);
20437 spec_parm = DECL_CHAIN (spec_parm);
20438 }
20439 else
20440 {
20441 /* Register the (value) argument pack as a specialization of
20442 TMPL_PARM, then move on. */
20443 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20444 register_local_specialization (argpack, tmpl_parm);
20445 }
20446 }
20447 gcc_assert (!spec_parm);
20448
20449 /* Substitute into the body of the function. */
20450 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20451 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20452 tf_warning_or_error, tmpl);
20453 else
20454 {
20455 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20456 tf_warning_or_error, tmpl,
20457 /*integral_constant_expression_p=*/false);
20458
20459 /* Set the current input_location to the end of the function
20460 so that finish_function knows where we are. */
20461 input_location
20462 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20463
20464 /* Remember if we saw an infinite loop in the template. */
20465 current_function_infinite_loop
20466 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20467 }
20468
20469 /* We don't need the local specializations any more. */
20470 delete local_specializations;
20471 local_specializations = saved_local_specializations;
20472
20473 /* Finish the function. */
20474 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20475 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20476 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20477 else
20478 {
20479 d = finish_function (0);
20480 expand_or_defer_fn (d);
20481 }
20482
20483 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20484 cp_check_omp_declare_reduction (d);
20485 }
20486
20487 /* We're not deferring instantiation any more. */
20488 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20489
20490 if (!fn_context)
20491 pop_from_top_level ();
20492 else if (nested)
20493 pop_function_context ();
20494
20495 out:
20496 input_location = saved_loc;
20497 cp_unevaluated_operand = saved_unevaluated_operand;
20498 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20499 pop_deferring_access_checks ();
20500 pop_tinst_level ();
20501
20502 timevar_pop (TV_TEMPLATE_INST);
20503
20504 return d;
20505 }
20506
20507 /* Run through the list of templates that we wish we could
20508 instantiate, and instantiate any we can. RETRIES is the
20509 number of times we retry pending template instantiation. */
20510
20511 void
20512 instantiate_pending_templates (int retries)
20513 {
20514 int reconsider;
20515 location_t saved_loc = input_location;
20516
20517 /* Instantiating templates may trigger vtable generation. This in turn
20518 may require further template instantiations. We place a limit here
20519 to avoid infinite loop. */
20520 if (pending_templates && retries >= max_tinst_depth)
20521 {
20522 tree decl = pending_templates->tinst->decl;
20523
20524 fatal_error (input_location,
20525 "template instantiation depth exceeds maximum of %d"
20526 " instantiating %q+D, possibly from virtual table generation"
20527 " (use -ftemplate-depth= to increase the maximum)",
20528 max_tinst_depth, decl);
20529 if (TREE_CODE (decl) == FUNCTION_DECL)
20530 /* Pretend that we defined it. */
20531 DECL_INITIAL (decl) = error_mark_node;
20532 return;
20533 }
20534
20535 do
20536 {
20537 struct pending_template **t = &pending_templates;
20538 struct pending_template *last = NULL;
20539 reconsider = 0;
20540 while (*t)
20541 {
20542 tree instantiation = reopen_tinst_level ((*t)->tinst);
20543 bool complete = false;
20544
20545 if (TYPE_P (instantiation))
20546 {
20547 tree fn;
20548
20549 if (!COMPLETE_TYPE_P (instantiation))
20550 {
20551 instantiate_class_template (instantiation);
20552 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20553 for (fn = TYPE_METHODS (instantiation);
20554 fn;
20555 fn = TREE_CHAIN (fn))
20556 if (! DECL_ARTIFICIAL (fn))
20557 instantiate_decl (fn,
20558 /*defer_ok=*/0,
20559 /*expl_inst_class_mem_p=*/false);
20560 if (COMPLETE_TYPE_P (instantiation))
20561 reconsider = 1;
20562 }
20563
20564 complete = COMPLETE_TYPE_P (instantiation);
20565 }
20566 else
20567 {
20568 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20569 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20570 {
20571 instantiation
20572 = instantiate_decl (instantiation,
20573 /*defer_ok=*/0,
20574 /*expl_inst_class_mem_p=*/false);
20575 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20576 reconsider = 1;
20577 }
20578
20579 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20580 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20581 }
20582
20583 if (complete)
20584 /* If INSTANTIATION has been instantiated, then we don't
20585 need to consider it again in the future. */
20586 *t = (*t)->next;
20587 else
20588 {
20589 last = *t;
20590 t = &(*t)->next;
20591 }
20592 tinst_depth = 0;
20593 current_tinst_level = NULL;
20594 }
20595 last_pending_template = last;
20596 }
20597 while (reconsider);
20598
20599 input_location = saved_loc;
20600 }
20601
20602 /* Substitute ARGVEC into T, which is a list of initializers for
20603 either base class or a non-static data member. The TREE_PURPOSEs
20604 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20605 instantiate_decl. */
20606
20607 static tree
20608 tsubst_initializer_list (tree t, tree argvec)
20609 {
20610 tree inits = NULL_TREE;
20611
20612 for (; t; t = TREE_CHAIN (t))
20613 {
20614 tree decl;
20615 tree init;
20616 tree expanded_bases = NULL_TREE;
20617 tree expanded_arguments = NULL_TREE;
20618 int i, len = 1;
20619
20620 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20621 {
20622 tree expr;
20623 tree arg;
20624
20625 /* Expand the base class expansion type into separate base
20626 classes. */
20627 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20628 tf_warning_or_error,
20629 NULL_TREE);
20630 if (expanded_bases == error_mark_node)
20631 continue;
20632
20633 /* We'll be building separate TREE_LISTs of arguments for
20634 each base. */
20635 len = TREE_VEC_LENGTH (expanded_bases);
20636 expanded_arguments = make_tree_vec (len);
20637 for (i = 0; i < len; i++)
20638 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20639
20640 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20641 expand each argument in the TREE_VALUE of t. */
20642 expr = make_node (EXPR_PACK_EXPANSION);
20643 PACK_EXPANSION_LOCAL_P (expr) = true;
20644 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20645 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20646
20647 if (TREE_VALUE (t) == void_type_node)
20648 /* VOID_TYPE_NODE is used to indicate
20649 value-initialization. */
20650 {
20651 for (i = 0; i < len; i++)
20652 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20653 }
20654 else
20655 {
20656 /* Substitute parameter packs into each argument in the
20657 TREE_LIST. */
20658 in_base_initializer = 1;
20659 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20660 {
20661 tree expanded_exprs;
20662
20663 /* Expand the argument. */
20664 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20665 expanded_exprs
20666 = tsubst_pack_expansion (expr, argvec,
20667 tf_warning_or_error,
20668 NULL_TREE);
20669 if (expanded_exprs == error_mark_node)
20670 continue;
20671
20672 /* Prepend each of the expanded expressions to the
20673 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20674 for (i = 0; i < len; i++)
20675 {
20676 TREE_VEC_ELT (expanded_arguments, i) =
20677 tree_cons (NULL_TREE,
20678 TREE_VEC_ELT (expanded_exprs, i),
20679 TREE_VEC_ELT (expanded_arguments, i));
20680 }
20681 }
20682 in_base_initializer = 0;
20683
20684 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20685 since we built them backwards. */
20686 for (i = 0; i < len; i++)
20687 {
20688 TREE_VEC_ELT (expanded_arguments, i) =
20689 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20690 }
20691 }
20692 }
20693
20694 for (i = 0; i < len; ++i)
20695 {
20696 if (expanded_bases)
20697 {
20698 decl = TREE_VEC_ELT (expanded_bases, i);
20699 decl = expand_member_init (decl);
20700 init = TREE_VEC_ELT (expanded_arguments, i);
20701 }
20702 else
20703 {
20704 tree tmp;
20705 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20706 tf_warning_or_error, NULL_TREE);
20707
20708 decl = expand_member_init (decl);
20709 if (decl && !DECL_P (decl))
20710 in_base_initializer = 1;
20711
20712 init = TREE_VALUE (t);
20713 tmp = init;
20714 if (init != void_type_node)
20715 init = tsubst_expr (init, argvec,
20716 tf_warning_or_error, NULL_TREE,
20717 /*integral_constant_expression_p=*/false);
20718 if (init == NULL_TREE && tmp != NULL_TREE)
20719 /* If we had an initializer but it instantiated to nothing,
20720 value-initialize the object. This will only occur when
20721 the initializer was a pack expansion where the parameter
20722 packs used in that expansion were of length zero. */
20723 init = void_type_node;
20724 in_base_initializer = 0;
20725 }
20726
20727 if (decl)
20728 {
20729 init = build_tree_list (decl, init);
20730 TREE_CHAIN (init) = inits;
20731 inits = init;
20732 }
20733 }
20734 }
20735 return inits;
20736 }
20737
20738 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20739
20740 static void
20741 set_current_access_from_decl (tree decl)
20742 {
20743 if (TREE_PRIVATE (decl))
20744 current_access_specifier = access_private_node;
20745 else if (TREE_PROTECTED (decl))
20746 current_access_specifier = access_protected_node;
20747 else
20748 current_access_specifier = access_public_node;
20749 }
20750
20751 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20752 is the instantiation (which should have been created with
20753 start_enum) and ARGS are the template arguments to use. */
20754
20755 static void
20756 tsubst_enum (tree tag, tree newtag, tree args)
20757 {
20758 tree e;
20759
20760 if (SCOPED_ENUM_P (newtag))
20761 begin_scope (sk_scoped_enum, newtag);
20762
20763 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20764 {
20765 tree value;
20766 tree decl;
20767
20768 decl = TREE_VALUE (e);
20769 /* Note that in a template enum, the TREE_VALUE is the
20770 CONST_DECL, not the corresponding INTEGER_CST. */
20771 value = tsubst_expr (DECL_INITIAL (decl),
20772 args, tf_warning_or_error, NULL_TREE,
20773 /*integral_constant_expression_p=*/true);
20774
20775 /* Give this enumeration constant the correct access. */
20776 set_current_access_from_decl (decl);
20777
20778 /* Actually build the enumerator itself. Here we're assuming that
20779 enumerators can't have dependent attributes. */
20780 build_enumerator (DECL_NAME (decl), value, newtag,
20781 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
20782 }
20783
20784 if (SCOPED_ENUM_P (newtag))
20785 finish_scope ();
20786
20787 finish_enum_value_list (newtag);
20788 finish_enum (newtag);
20789
20790 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20791 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20792 }
20793
20794 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20795 its type -- but without substituting the innermost set of template
20796 arguments. So, innermost set of template parameters will appear in
20797 the type. */
20798
20799 tree
20800 get_mostly_instantiated_function_type (tree decl)
20801 {
20802 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20803 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
20804 }
20805
20806 /* Return truthvalue if we're processing a template different from
20807 the last one involved in diagnostics. */
20808 bool
20809 problematic_instantiation_changed (void)
20810 {
20811 return current_tinst_level != last_error_tinst_level;
20812 }
20813
20814 /* Remember current template involved in diagnostics. */
20815 void
20816 record_last_problematic_instantiation (void)
20817 {
20818 last_error_tinst_level = current_tinst_level;
20819 }
20820
20821 struct tinst_level *
20822 current_instantiation (void)
20823 {
20824 return current_tinst_level;
20825 }
20826
20827 /* Return TRUE if current_function_decl is being instantiated, false
20828 otherwise. */
20829
20830 bool
20831 instantiating_current_function_p (void)
20832 {
20833 return (current_instantiation ()
20834 && current_instantiation ()->decl == current_function_decl);
20835 }
20836
20837 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20838 type. Return zero for ok, nonzero for disallowed. Issue error and
20839 warning messages under control of COMPLAIN. */
20840
20841 static int
20842 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20843 {
20844 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20845 return 0;
20846 else if (POINTER_TYPE_P (type))
20847 return 0;
20848 else if (TYPE_PTRMEM_P (type))
20849 return 0;
20850 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20851 return 0;
20852 else if (TREE_CODE (type) == TYPENAME_TYPE)
20853 return 0;
20854 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20855 return 0;
20856 else if (TREE_CODE (type) == NULLPTR_TYPE)
20857 return 0;
20858
20859 if (complain & tf_error)
20860 {
20861 if (type == error_mark_node)
20862 inform (input_location, "invalid template non-type parameter");
20863 else
20864 error ("%q#T is not a valid type for a template non-type parameter",
20865 type);
20866 }
20867 return 1;
20868 }
20869
20870 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20871 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20872
20873 static bool
20874 dependent_type_p_r (tree type)
20875 {
20876 tree scope;
20877
20878 /* [temp.dep.type]
20879
20880 A type is dependent if it is:
20881
20882 -- a template parameter. Template template parameters are types
20883 for us (since TYPE_P holds true for them) so we handle
20884 them here. */
20885 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20886 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20887 return true;
20888 /* -- a qualified-id with a nested-name-specifier which contains a
20889 class-name that names a dependent type or whose unqualified-id
20890 names a dependent type. */
20891 if (TREE_CODE (type) == TYPENAME_TYPE)
20892 return true;
20893 /* -- a cv-qualified type where the cv-unqualified type is
20894 dependent.
20895 No code is necessary for this bullet; the code below handles
20896 cv-qualified types, and we don't want to strip aliases with
20897 TYPE_MAIN_VARIANT because of DR 1558. */
20898 /* -- a compound type constructed from any dependent type. */
20899 if (TYPE_PTRMEM_P (type))
20900 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20901 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20902 (type)));
20903 else if (TYPE_PTR_P (type)
20904 || TREE_CODE (type) == REFERENCE_TYPE)
20905 return dependent_type_p (TREE_TYPE (type));
20906 else if (TREE_CODE (type) == FUNCTION_TYPE
20907 || TREE_CODE (type) == METHOD_TYPE)
20908 {
20909 tree arg_type;
20910
20911 if (dependent_type_p (TREE_TYPE (type)))
20912 return true;
20913 for (arg_type = TYPE_ARG_TYPES (type);
20914 arg_type;
20915 arg_type = TREE_CHAIN (arg_type))
20916 if (dependent_type_p (TREE_VALUE (arg_type)))
20917 return true;
20918 return false;
20919 }
20920 /* -- an array type constructed from any dependent type or whose
20921 size is specified by a constant expression that is
20922 value-dependent.
20923
20924 We checked for type- and value-dependence of the bounds in
20925 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20926 if (TREE_CODE (type) == ARRAY_TYPE)
20927 {
20928 if (TYPE_DOMAIN (type)
20929 && dependent_type_p (TYPE_DOMAIN (type)))
20930 return true;
20931 return dependent_type_p (TREE_TYPE (type));
20932 }
20933
20934 /* -- a template-id in which either the template name is a template
20935 parameter ... */
20936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20937 return true;
20938 /* ... or any of the template arguments is a dependent type or
20939 an expression that is type-dependent or value-dependent. */
20940 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20941 && (any_dependent_template_arguments_p
20942 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20943 return true;
20944 /* For an alias template specialization, check the arguments both to the
20945 class template and the alias template. */
20946 else if (alias_template_specialization_p (type)
20947 && (any_dependent_template_arguments_p
20948 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type)))))
20949 return true;
20950
20951 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20952 dependent; if the argument of the `typeof' expression is not
20953 type-dependent, then it should already been have resolved. */
20954 if (TREE_CODE (type) == TYPEOF_TYPE
20955 || TREE_CODE (type) == DECLTYPE_TYPE
20956 || TREE_CODE (type) == UNDERLYING_TYPE)
20957 return true;
20958
20959 /* A template argument pack is dependent if any of its packed
20960 arguments are. */
20961 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20962 {
20963 tree args = ARGUMENT_PACK_ARGS (type);
20964 int i, len = TREE_VEC_LENGTH (args);
20965 for (i = 0; i < len; ++i)
20966 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20967 return true;
20968 }
20969
20970 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20971 be template parameters. */
20972 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20973 return true;
20974
20975 /* The standard does not specifically mention types that are local
20976 to template functions or local classes, but they should be
20977 considered dependent too. For example:
20978
20979 template <int I> void f() {
20980 enum E { a = I };
20981 S<sizeof (E)> s;
20982 }
20983
20984 The size of `E' cannot be known until the value of `I' has been
20985 determined. Therefore, `E' must be considered dependent. */
20986 scope = TYPE_CONTEXT (type);
20987 if (scope && TYPE_P (scope))
20988 return dependent_type_p (scope);
20989 /* Don't use type_dependent_expression_p here, as it can lead
20990 to infinite recursion trying to determine whether a lambda
20991 nested in a lambda is dependent (c++/47687). */
20992 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20993 && DECL_LANG_SPECIFIC (scope)
20994 && DECL_TEMPLATE_INFO (scope)
20995 && (any_dependent_template_arguments_p
20996 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20997 return true;
20998
20999 /* Other types are non-dependent. */
21000 return false;
21001 }
21002
21003 /* Returns TRUE if TYPE is dependent, in the sense of
21004 [temp.dep.type]. Note that a NULL type is considered dependent. */
21005
21006 bool
21007 dependent_type_p (tree type)
21008 {
21009 /* If there are no template parameters in scope, then there can't be
21010 any dependent types. */
21011 if (!processing_template_decl)
21012 {
21013 /* If we are not processing a template, then nobody should be
21014 providing us with a dependent type. */
21015 gcc_assert (type);
21016 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
21017 return false;
21018 }
21019
21020 /* If the type is NULL, we have not computed a type for the entity
21021 in question; in that case, the type is dependent. */
21022 if (!type)
21023 return true;
21024
21025 /* Erroneous types can be considered non-dependent. */
21026 if (type == error_mark_node)
21027 return false;
21028
21029 /* If we have not already computed the appropriate value for TYPE,
21030 do so now. */
21031 if (!TYPE_DEPENDENT_P_VALID (type))
21032 {
21033 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
21034 TYPE_DEPENDENT_P_VALID (type) = 1;
21035 }
21036
21037 return TYPE_DEPENDENT_P (type);
21038 }
21039
21040 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21041 lookup. In other words, a dependent type that is not the current
21042 instantiation. */
21043
21044 bool
21045 dependent_scope_p (tree scope)
21046 {
21047 return (scope && TYPE_P (scope) && dependent_type_p (scope)
21048 && !currently_open_class (scope));
21049 }
21050
21051 /* T is a SCOPE_REF; return whether we need to consider it
21052 instantiation-dependent so that we can check access at instantiation
21053 time even though we know which member it resolves to. */
21054
21055 static bool
21056 instantiation_dependent_scope_ref_p (tree t)
21057 {
21058 if (DECL_P (TREE_OPERAND (t, 1))
21059 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
21060 && accessible_in_template_p (TREE_OPERAND (t, 0),
21061 TREE_OPERAND (t, 1)))
21062 return false;
21063 else
21064 return true;
21065 }
21066
21067 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21068 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21069 expression. */
21070
21071 /* Note that this predicate is not appropriate for general expressions;
21072 only constant expressions (that satisfy potential_constant_expression)
21073 can be tested for value dependence. */
21074
21075 bool
21076 value_dependent_expression_p (tree expression)
21077 {
21078 if (!processing_template_decl)
21079 return false;
21080
21081 /* A name declared with a dependent type. */
21082 if (DECL_P (expression) && type_dependent_expression_p (expression))
21083 return true;
21084
21085 switch (TREE_CODE (expression))
21086 {
21087 case IDENTIFIER_NODE:
21088 /* A name that has not been looked up -- must be dependent. */
21089 return true;
21090
21091 case TEMPLATE_PARM_INDEX:
21092 /* A non-type template parm. */
21093 return true;
21094
21095 case CONST_DECL:
21096 /* A non-type template parm. */
21097 if (DECL_TEMPLATE_PARM_P (expression))
21098 return true;
21099 return value_dependent_expression_p (DECL_INITIAL (expression));
21100
21101 case VAR_DECL:
21102 /* A constant with literal type and is initialized
21103 with an expression that is value-dependent.
21104
21105 Note that a non-dependent parenthesized initializer will have
21106 already been replaced with its constant value, so if we see
21107 a TREE_LIST it must be dependent. */
21108 if (DECL_INITIAL (expression)
21109 && decl_constant_var_p (expression)
21110 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21111 /* cp_finish_decl doesn't fold reference initializers. */
21112 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21113 || value_dependent_expression_p (DECL_INITIAL (expression))))
21114 return true;
21115 return false;
21116
21117 case DYNAMIC_CAST_EXPR:
21118 case STATIC_CAST_EXPR:
21119 case CONST_CAST_EXPR:
21120 case REINTERPRET_CAST_EXPR:
21121 case CAST_EXPR:
21122 /* These expressions are value-dependent if the type to which
21123 the cast occurs is dependent or the expression being casted
21124 is value-dependent. */
21125 {
21126 tree type = TREE_TYPE (expression);
21127
21128 if (dependent_type_p (type))
21129 return true;
21130
21131 /* A functional cast has a list of operands. */
21132 expression = TREE_OPERAND (expression, 0);
21133 if (!expression)
21134 {
21135 /* If there are no operands, it must be an expression such
21136 as "int()". This should not happen for aggregate types
21137 because it would form non-constant expressions. */
21138 gcc_assert (cxx_dialect >= cxx11
21139 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21140
21141 return false;
21142 }
21143
21144 if (TREE_CODE (expression) == TREE_LIST)
21145 return any_value_dependent_elements_p (expression);
21146
21147 return value_dependent_expression_p (expression);
21148 }
21149
21150 case SIZEOF_EXPR:
21151 if (SIZEOF_EXPR_TYPE_P (expression))
21152 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21153 /* FALLTHRU */
21154 case ALIGNOF_EXPR:
21155 case TYPEID_EXPR:
21156 /* A `sizeof' expression is value-dependent if the operand is
21157 type-dependent or is a pack expansion. */
21158 expression = TREE_OPERAND (expression, 0);
21159 if (PACK_EXPANSION_P (expression))
21160 return true;
21161 else if (TYPE_P (expression))
21162 return dependent_type_p (expression);
21163 return instantiation_dependent_expression_p (expression);
21164
21165 case AT_ENCODE_EXPR:
21166 /* An 'encode' expression is value-dependent if the operand is
21167 type-dependent. */
21168 expression = TREE_OPERAND (expression, 0);
21169 return dependent_type_p (expression);
21170
21171 case NOEXCEPT_EXPR:
21172 expression = TREE_OPERAND (expression, 0);
21173 return instantiation_dependent_expression_p (expression);
21174
21175 case SCOPE_REF:
21176 /* All instantiation-dependent expressions should also be considered
21177 value-dependent. */
21178 return instantiation_dependent_scope_ref_p (expression);
21179
21180 case COMPONENT_REF:
21181 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21182 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21183
21184 case NONTYPE_ARGUMENT_PACK:
21185 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21186 is value-dependent. */
21187 {
21188 tree values = ARGUMENT_PACK_ARGS (expression);
21189 int i, len = TREE_VEC_LENGTH (values);
21190
21191 for (i = 0; i < len; ++i)
21192 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21193 return true;
21194
21195 return false;
21196 }
21197
21198 case TRAIT_EXPR:
21199 {
21200 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21201 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21202 || (type2 ? dependent_type_p (type2) : false));
21203 }
21204
21205 case MODOP_EXPR:
21206 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21207 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21208
21209 case ARRAY_REF:
21210 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21211 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21212
21213 case ADDR_EXPR:
21214 {
21215 tree op = TREE_OPERAND (expression, 0);
21216 return (value_dependent_expression_p (op)
21217 || has_value_dependent_address (op));
21218 }
21219
21220 case CALL_EXPR:
21221 {
21222 tree fn = get_callee_fndecl (expression);
21223 int i, nargs;
21224 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21225 return true;
21226 nargs = call_expr_nargs (expression);
21227 for (i = 0; i < nargs; ++i)
21228 {
21229 tree op = CALL_EXPR_ARG (expression, i);
21230 /* In a call to a constexpr member function, look through the
21231 implicit ADDR_EXPR on the object argument so that it doesn't
21232 cause the call to be considered value-dependent. We also
21233 look through it in potential_constant_expression. */
21234 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21235 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21236 && TREE_CODE (op) == ADDR_EXPR)
21237 op = TREE_OPERAND (op, 0);
21238 if (value_dependent_expression_p (op))
21239 return true;
21240 }
21241 return false;
21242 }
21243
21244 case TEMPLATE_ID_EXPR:
21245 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21246 type-dependent. */
21247 return type_dependent_expression_p (expression);
21248
21249 case CONSTRUCTOR:
21250 {
21251 unsigned ix;
21252 tree val;
21253 if (dependent_type_p (TREE_TYPE (expression)))
21254 return true;
21255 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21256 if (value_dependent_expression_p (val))
21257 return true;
21258 return false;
21259 }
21260
21261 case STMT_EXPR:
21262 /* Treat a GNU statement expression as dependent to avoid crashing
21263 under instantiate_non_dependent_expr; it can't be constant. */
21264 return true;
21265
21266 default:
21267 /* A constant expression is value-dependent if any subexpression is
21268 value-dependent. */
21269 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21270 {
21271 case tcc_reference:
21272 case tcc_unary:
21273 case tcc_comparison:
21274 case tcc_binary:
21275 case tcc_expression:
21276 case tcc_vl_exp:
21277 {
21278 int i, len = cp_tree_operand_length (expression);
21279
21280 for (i = 0; i < len; i++)
21281 {
21282 tree t = TREE_OPERAND (expression, i);
21283
21284 /* In some cases, some of the operands may be missing.l
21285 (For example, in the case of PREDECREMENT_EXPR, the
21286 amount to increment by may be missing.) That doesn't
21287 make the expression dependent. */
21288 if (t && value_dependent_expression_p (t))
21289 return true;
21290 }
21291 }
21292 break;
21293 default:
21294 break;
21295 }
21296 break;
21297 }
21298
21299 /* The expression is not value-dependent. */
21300 return false;
21301 }
21302
21303 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21304 [temp.dep.expr]. Note that an expression with no type is
21305 considered dependent. Other parts of the compiler arrange for an
21306 expression with type-dependent subexpressions to have no type, so
21307 this function doesn't have to be fully recursive. */
21308
21309 bool
21310 type_dependent_expression_p (tree expression)
21311 {
21312 if (!processing_template_decl)
21313 return false;
21314
21315 if (expression == NULL_TREE || expression == error_mark_node)
21316 return false;
21317
21318 /* An unresolved name is always dependent. */
21319 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
21320 return true;
21321
21322 /* Some expression forms are never type-dependent. */
21323 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21324 || TREE_CODE (expression) == SIZEOF_EXPR
21325 || TREE_CODE (expression) == ALIGNOF_EXPR
21326 || TREE_CODE (expression) == AT_ENCODE_EXPR
21327 || TREE_CODE (expression) == NOEXCEPT_EXPR
21328 || TREE_CODE (expression) == TRAIT_EXPR
21329 || TREE_CODE (expression) == TYPEID_EXPR
21330 || TREE_CODE (expression) == DELETE_EXPR
21331 || TREE_CODE (expression) == VEC_DELETE_EXPR
21332 || TREE_CODE (expression) == THROW_EXPR)
21333 return false;
21334
21335 /* The types of these expressions depends only on the type to which
21336 the cast occurs. */
21337 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21338 || TREE_CODE (expression) == STATIC_CAST_EXPR
21339 || TREE_CODE (expression) == CONST_CAST_EXPR
21340 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21341 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21342 || TREE_CODE (expression) == CAST_EXPR)
21343 return dependent_type_p (TREE_TYPE (expression));
21344
21345 /* The types of these expressions depends only on the type created
21346 by the expression. */
21347 if (TREE_CODE (expression) == NEW_EXPR
21348 || TREE_CODE (expression) == VEC_NEW_EXPR)
21349 {
21350 /* For NEW_EXPR tree nodes created inside a template, either
21351 the object type itself or a TREE_LIST may appear as the
21352 operand 1. */
21353 tree type = TREE_OPERAND (expression, 1);
21354 if (TREE_CODE (type) == TREE_LIST)
21355 /* This is an array type. We need to check array dimensions
21356 as well. */
21357 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21358 || value_dependent_expression_p
21359 (TREE_OPERAND (TREE_VALUE (type), 1));
21360 else
21361 return dependent_type_p (type);
21362 }
21363
21364 if (TREE_CODE (expression) == SCOPE_REF)
21365 {
21366 tree scope = TREE_OPERAND (expression, 0);
21367 tree name = TREE_OPERAND (expression, 1);
21368
21369 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21370 contains an identifier associated by name lookup with one or more
21371 declarations declared with a dependent type, or...a
21372 nested-name-specifier or qualified-id that names a member of an
21373 unknown specialization. */
21374 return (type_dependent_expression_p (name)
21375 || dependent_scope_p (scope));
21376 }
21377
21378 if (TREE_CODE (expression) == FUNCTION_DECL
21379 && DECL_LANG_SPECIFIC (expression)
21380 && DECL_TEMPLATE_INFO (expression)
21381 && (any_dependent_template_arguments_p
21382 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21383 return true;
21384
21385 if (TREE_CODE (expression) == TEMPLATE_DECL
21386 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21387 return false;
21388
21389 if (TREE_CODE (expression) == STMT_EXPR)
21390 expression = stmt_expr_value_expr (expression);
21391
21392 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21393 {
21394 tree elt;
21395 unsigned i;
21396
21397 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21398 {
21399 if (type_dependent_expression_p (elt))
21400 return true;
21401 }
21402 return false;
21403 }
21404
21405 /* A static data member of the current instantiation with incomplete
21406 array type is type-dependent, as the definition and specializations
21407 can have different bounds. */
21408 if (VAR_P (expression)
21409 && DECL_CLASS_SCOPE_P (expression)
21410 && dependent_type_p (DECL_CONTEXT (expression))
21411 && VAR_HAD_UNKNOWN_BOUND (expression))
21412 return true;
21413
21414 /* An array of unknown bound depending on a variadic parameter, eg:
21415
21416 template<typename... Args>
21417 void foo (Args... args)
21418 {
21419 int arr[] = { args... };
21420 }
21421
21422 template<int... vals>
21423 void bar ()
21424 {
21425 int arr[] = { vals... };
21426 }
21427
21428 If the array has no length and has an initializer, it must be that
21429 we couldn't determine its length in cp_complete_array_type because
21430 it is dependent. */
21431 if (VAR_P (expression)
21432 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21433 && !TYPE_DOMAIN (TREE_TYPE (expression))
21434 && DECL_INITIAL (expression))
21435 return true;
21436
21437 /* A variable template specialization is type-dependent if it has any
21438 dependent template arguments. */
21439 if (VAR_P (expression)
21440 && DECL_LANG_SPECIFIC (expression)
21441 && DECL_TEMPLATE_INFO (expression)
21442 && variable_template_p (DECL_TI_TEMPLATE (expression)))
21443 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
21444
21445 /* Always dependent, on the number of arguments if nothing else. */
21446 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21447 return true;
21448
21449 if (TREE_TYPE (expression) == unknown_type_node)
21450 {
21451 if (TREE_CODE (expression) == ADDR_EXPR)
21452 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21453 if (TREE_CODE (expression) == COMPONENT_REF
21454 || TREE_CODE (expression) == OFFSET_REF)
21455 {
21456 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21457 return true;
21458 expression = TREE_OPERAND (expression, 1);
21459 if (identifier_p (expression))
21460 return false;
21461 }
21462 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21463 if (TREE_CODE (expression) == SCOPE_REF)
21464 return false;
21465
21466 if (BASELINK_P (expression))
21467 {
21468 if (BASELINK_OPTYPE (expression)
21469 && dependent_type_p (BASELINK_OPTYPE (expression)))
21470 return true;
21471 expression = BASELINK_FUNCTIONS (expression);
21472 }
21473
21474 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21475 {
21476 if (any_dependent_template_arguments_p
21477 (TREE_OPERAND (expression, 1)))
21478 return true;
21479 expression = TREE_OPERAND (expression, 0);
21480 }
21481 gcc_assert (TREE_CODE (expression) == OVERLOAD
21482 || TREE_CODE (expression) == FUNCTION_DECL);
21483
21484 while (expression)
21485 {
21486 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21487 return true;
21488 expression = OVL_NEXT (expression);
21489 }
21490 return false;
21491 }
21492
21493 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21494
21495 return (dependent_type_p (TREE_TYPE (expression)));
21496 }
21497
21498 /* walk_tree callback function for instantiation_dependent_expression_p,
21499 below. Returns non-zero if a dependent subexpression is found. */
21500
21501 static tree
21502 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21503 void * /*data*/)
21504 {
21505 if (TYPE_P (*tp))
21506 {
21507 /* We don't have to worry about decltype currently because decltype
21508 of an instantiation-dependent expr is a dependent type. This
21509 might change depending on the resolution of DR 1172. */
21510 *walk_subtrees = false;
21511 return NULL_TREE;
21512 }
21513 enum tree_code code = TREE_CODE (*tp);
21514 switch (code)
21515 {
21516 /* Don't treat an argument list as dependent just because it has no
21517 TREE_TYPE. */
21518 case TREE_LIST:
21519 case TREE_VEC:
21520 return NULL_TREE;
21521
21522 case VAR_DECL:
21523 case CONST_DECL:
21524 /* A constant with a dependent initializer is dependent. */
21525 if (value_dependent_expression_p (*tp))
21526 return *tp;
21527 break;
21528
21529 case TEMPLATE_PARM_INDEX:
21530 return *tp;
21531
21532 /* Handle expressions with type operands. */
21533 case SIZEOF_EXPR:
21534 case ALIGNOF_EXPR:
21535 case TYPEID_EXPR:
21536 case AT_ENCODE_EXPR:
21537 {
21538 tree op = TREE_OPERAND (*tp, 0);
21539 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21540 op = TREE_TYPE (op);
21541 if (TYPE_P (op))
21542 {
21543 if (dependent_type_p (op))
21544 return *tp;
21545 else
21546 {
21547 *walk_subtrees = false;
21548 return NULL_TREE;
21549 }
21550 }
21551 break;
21552 }
21553
21554 case TRAIT_EXPR:
21555 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21556 || (TRAIT_EXPR_TYPE2 (*tp)
21557 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21558 return *tp;
21559 *walk_subtrees = false;
21560 return NULL_TREE;
21561
21562 case COMPONENT_REF:
21563 if (identifier_p (TREE_OPERAND (*tp, 1)))
21564 /* In a template, finish_class_member_access_expr creates a
21565 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21566 type-dependent, so that we can check access control at
21567 instantiation time (PR 42277). See also Core issue 1273. */
21568 return *tp;
21569 break;
21570
21571 case SCOPE_REF:
21572 if (instantiation_dependent_scope_ref_p (*tp))
21573 return *tp;
21574 else
21575 break;
21576
21577 /* Treat statement-expressions as dependent. */
21578 case BIND_EXPR:
21579 return *tp;
21580
21581 default:
21582 break;
21583 }
21584
21585 if (type_dependent_expression_p (*tp))
21586 return *tp;
21587 else
21588 return NULL_TREE;
21589 }
21590
21591 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21592 sense defined by the ABI:
21593
21594 "An expression is instantiation-dependent if it is type-dependent
21595 or value-dependent, or it has a subexpression that is type-dependent
21596 or value-dependent." */
21597
21598 bool
21599 instantiation_dependent_expression_p (tree expression)
21600 {
21601 tree result;
21602
21603 if (!processing_template_decl)
21604 return false;
21605
21606 if (expression == error_mark_node)
21607 return false;
21608
21609 result = cp_walk_tree_without_duplicates (&expression,
21610 instantiation_dependent_r, NULL);
21611 return result != NULL_TREE;
21612 }
21613
21614 /* Like type_dependent_expression_p, but it also works while not processing
21615 a template definition, i.e. during substitution or mangling. */
21616
21617 bool
21618 type_dependent_expression_p_push (tree expr)
21619 {
21620 bool b;
21621 ++processing_template_decl;
21622 b = type_dependent_expression_p (expr);
21623 --processing_template_decl;
21624 return b;
21625 }
21626
21627 /* Returns TRUE if ARGS contains a type-dependent expression. */
21628
21629 bool
21630 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21631 {
21632 unsigned int i;
21633 tree arg;
21634
21635 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21636 {
21637 if (type_dependent_expression_p (arg))
21638 return true;
21639 }
21640 return false;
21641 }
21642
21643 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21644 expressions) contains any type-dependent expressions. */
21645
21646 bool
21647 any_type_dependent_elements_p (const_tree list)
21648 {
21649 for (; list; list = TREE_CHAIN (list))
21650 if (type_dependent_expression_p (TREE_VALUE (list)))
21651 return true;
21652
21653 return false;
21654 }
21655
21656 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21657 expressions) contains any value-dependent expressions. */
21658
21659 bool
21660 any_value_dependent_elements_p (const_tree list)
21661 {
21662 for (; list; list = TREE_CHAIN (list))
21663 if (value_dependent_expression_p (TREE_VALUE (list)))
21664 return true;
21665
21666 return false;
21667 }
21668
21669 /* Returns TRUE if the ARG (a template argument) is dependent. */
21670
21671 bool
21672 dependent_template_arg_p (tree arg)
21673 {
21674 if (!processing_template_decl)
21675 return false;
21676
21677 /* Assume a template argument that was wrongly written by the user
21678 is dependent. This is consistent with what
21679 any_dependent_template_arguments_p [that calls this function]
21680 does. */
21681 if (!arg || arg == error_mark_node)
21682 return true;
21683
21684 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21685 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21686
21687 if (TREE_CODE (arg) == TEMPLATE_DECL
21688 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21689 return dependent_template_p (arg);
21690 else if (ARGUMENT_PACK_P (arg))
21691 {
21692 tree args = ARGUMENT_PACK_ARGS (arg);
21693 int i, len = TREE_VEC_LENGTH (args);
21694 for (i = 0; i < len; ++i)
21695 {
21696 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21697 return true;
21698 }
21699
21700 return false;
21701 }
21702 else if (TYPE_P (arg))
21703 return dependent_type_p (arg);
21704 else
21705 return (type_dependent_expression_p (arg)
21706 || value_dependent_expression_p (arg));
21707 }
21708
21709 /* Returns true if ARGS (a collection of template arguments) contains
21710 any types that require structural equality testing. */
21711
21712 bool
21713 any_template_arguments_need_structural_equality_p (tree args)
21714 {
21715 int i;
21716 int j;
21717
21718 if (!args)
21719 return false;
21720 if (args == error_mark_node)
21721 return true;
21722
21723 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21724 {
21725 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21726 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21727 {
21728 tree arg = TREE_VEC_ELT (level, j);
21729 tree packed_args = NULL_TREE;
21730 int k, len = 1;
21731
21732 if (ARGUMENT_PACK_P (arg))
21733 {
21734 /* Look inside the argument pack. */
21735 packed_args = ARGUMENT_PACK_ARGS (arg);
21736 len = TREE_VEC_LENGTH (packed_args);
21737 }
21738
21739 for (k = 0; k < len; ++k)
21740 {
21741 if (packed_args)
21742 arg = TREE_VEC_ELT (packed_args, k);
21743
21744 if (error_operand_p (arg))
21745 return true;
21746 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21747 continue;
21748 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21749 return true;
21750 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21751 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21752 return true;
21753 }
21754 }
21755 }
21756
21757 return false;
21758 }
21759
21760 /* Returns true if ARGS (a collection of template arguments) contains
21761 any dependent arguments. */
21762
21763 bool
21764 any_dependent_template_arguments_p (const_tree args)
21765 {
21766 int i;
21767 int j;
21768
21769 if (!args)
21770 return false;
21771 if (args == error_mark_node)
21772 return true;
21773
21774 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21775 {
21776 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21777 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21778 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21779 return true;
21780 }
21781
21782 return false;
21783 }
21784
21785 /* Returns TRUE if the template TMPL is dependent. */
21786
21787 bool
21788 dependent_template_p (tree tmpl)
21789 {
21790 if (TREE_CODE (tmpl) == OVERLOAD)
21791 {
21792 while (tmpl)
21793 {
21794 if (dependent_template_p (OVL_CURRENT (tmpl)))
21795 return true;
21796 tmpl = OVL_NEXT (tmpl);
21797 }
21798 return false;
21799 }
21800
21801 /* Template template parameters are dependent. */
21802 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21803 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21804 return true;
21805 /* So are names that have not been looked up. */
21806 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21807 return true;
21808 /* So are member templates of dependent classes. */
21809 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21810 return dependent_type_p (DECL_CONTEXT (tmpl));
21811 return false;
21812 }
21813
21814 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21815
21816 bool
21817 dependent_template_id_p (tree tmpl, tree args)
21818 {
21819 return (dependent_template_p (tmpl)
21820 || any_dependent_template_arguments_p (args));
21821 }
21822
21823 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21824 is dependent. */
21825
21826 bool
21827 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21828 {
21829 int i;
21830
21831 if (!processing_template_decl)
21832 return false;
21833
21834 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21835 {
21836 tree decl = TREE_VEC_ELT (declv, i);
21837 tree init = TREE_VEC_ELT (initv, i);
21838 tree cond = TREE_VEC_ELT (condv, i);
21839 tree incr = TREE_VEC_ELT (incrv, i);
21840
21841 if (type_dependent_expression_p (decl))
21842 return true;
21843
21844 if (init && type_dependent_expression_p (init))
21845 return true;
21846
21847 if (type_dependent_expression_p (cond))
21848 return true;
21849
21850 if (COMPARISON_CLASS_P (cond)
21851 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21852 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21853 return true;
21854
21855 if (TREE_CODE (incr) == MODOP_EXPR)
21856 {
21857 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21858 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21859 return true;
21860 }
21861 else if (type_dependent_expression_p (incr))
21862 return true;
21863 else if (TREE_CODE (incr) == MODIFY_EXPR)
21864 {
21865 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21866 return true;
21867 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21868 {
21869 tree t = TREE_OPERAND (incr, 1);
21870 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21871 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21872 return true;
21873 }
21874 }
21875 }
21876
21877 return false;
21878 }
21879
21880 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21881 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21882 no such TYPE can be found. Note that this function peers inside
21883 uninstantiated templates and therefore should be used only in
21884 extremely limited situations. ONLY_CURRENT_P restricts this
21885 peering to the currently open classes hierarchy (which is required
21886 when comparing types). */
21887
21888 tree
21889 resolve_typename_type (tree type, bool only_current_p)
21890 {
21891 tree scope;
21892 tree name;
21893 tree decl;
21894 int quals;
21895 tree pushed_scope;
21896 tree result;
21897
21898 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21899
21900 scope = TYPE_CONTEXT (type);
21901 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21902 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21903 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21904 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21905 identifier of the TYPENAME_TYPE anymore.
21906 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21907 TYPENAME_TYPE instead, we avoid messing up with a possible
21908 typedef variant case. */
21909 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21910
21911 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21912 it first before we can figure out what NAME refers to. */
21913 if (TREE_CODE (scope) == TYPENAME_TYPE)
21914 {
21915 if (TYPENAME_IS_RESOLVING_P (scope))
21916 /* Given a class template A with a dependent base with nested type C,
21917 typedef typename A::C::C C will land us here, as trying to resolve
21918 the initial A::C leads to the local C typedef, which leads back to
21919 A::C::C. So we break the recursion now. */
21920 return type;
21921 else
21922 scope = resolve_typename_type (scope, only_current_p);
21923 }
21924 /* If we don't know what SCOPE refers to, then we cannot resolve the
21925 TYPENAME_TYPE. */
21926 if (TREE_CODE (scope) == TYPENAME_TYPE)
21927 return type;
21928 /* If the SCOPE is a template type parameter, we have no way of
21929 resolving the name. */
21930 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21931 return type;
21932 /* If the SCOPE is not the current instantiation, there's no reason
21933 to look inside it. */
21934 if (only_current_p && !currently_open_class (scope))
21935 return type;
21936 /* If this is a typedef, we don't want to look inside (c++/11987). */
21937 if (typedef_variant_p (type))
21938 return type;
21939 /* If SCOPE isn't the template itself, it will not have a valid
21940 TYPE_FIELDS list. */
21941 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21942 /* scope is either the template itself or a compatible instantiation
21943 like X<T>, so look up the name in the original template. */
21944 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21945 else
21946 /* scope is a partial instantiation, so we can't do the lookup or we
21947 will lose the template arguments. */
21948 return type;
21949 /* Enter the SCOPE so that name lookup will be resolved as if we
21950 were in the class definition. In particular, SCOPE will no
21951 longer be considered a dependent type. */
21952 pushed_scope = push_scope (scope);
21953 /* Look up the declaration. */
21954 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21955 tf_warning_or_error);
21956
21957 result = NULL_TREE;
21958
21959 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21960 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21961 if (!decl)
21962 /*nop*/;
21963 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21964 && TREE_CODE (decl) == TYPE_DECL)
21965 {
21966 result = TREE_TYPE (decl);
21967 if (result == error_mark_node)
21968 result = NULL_TREE;
21969 }
21970 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21971 && DECL_CLASS_TEMPLATE_P (decl))
21972 {
21973 tree tmpl;
21974 tree args;
21975 /* Obtain the template and the arguments. */
21976 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21977 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21978 /* Instantiate the template. */
21979 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21980 /*entering_scope=*/0,
21981 tf_error | tf_user);
21982 if (result == error_mark_node)
21983 result = NULL_TREE;
21984 }
21985
21986 /* Leave the SCOPE. */
21987 if (pushed_scope)
21988 pop_scope (pushed_scope);
21989
21990 /* If we failed to resolve it, return the original typename. */
21991 if (!result)
21992 return type;
21993
21994 /* If lookup found a typename type, resolve that too. */
21995 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21996 {
21997 /* Ill-formed programs can cause infinite recursion here, so we
21998 must catch that. */
21999 TYPENAME_IS_RESOLVING_P (type) = 1;
22000 result = resolve_typename_type (result, only_current_p);
22001 TYPENAME_IS_RESOLVING_P (type) = 0;
22002 }
22003
22004 /* Qualify the resulting type. */
22005 quals = cp_type_quals (type);
22006 if (quals)
22007 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
22008
22009 return result;
22010 }
22011
22012 /* EXPR is an expression which is not type-dependent. Return a proxy
22013 for EXPR that can be used to compute the types of larger
22014 expressions containing EXPR. */
22015
22016 tree
22017 build_non_dependent_expr (tree expr)
22018 {
22019 tree inner_expr;
22020
22021 #ifdef ENABLE_CHECKING
22022 /* Try to get a constant value for all non-dependent expressions in
22023 order to expose bugs in *_dependent_expression_p and constexpr. */
22024 if (cxx_dialect >= cxx11)
22025 fold_non_dependent_expr (expr);
22026 #endif
22027
22028 /* Preserve OVERLOADs; the functions must be available to resolve
22029 types. */
22030 inner_expr = expr;
22031 if (TREE_CODE (inner_expr) == STMT_EXPR)
22032 inner_expr = stmt_expr_value_expr (inner_expr);
22033 if (TREE_CODE (inner_expr) == ADDR_EXPR)
22034 inner_expr = TREE_OPERAND (inner_expr, 0);
22035 if (TREE_CODE (inner_expr) == COMPONENT_REF)
22036 inner_expr = TREE_OPERAND (inner_expr, 1);
22037 if (is_overloaded_fn (inner_expr)
22038 || TREE_CODE (inner_expr) == OFFSET_REF)
22039 return expr;
22040 /* There is no need to return a proxy for a variable. */
22041 if (VAR_P (expr))
22042 return expr;
22043 /* Preserve string constants; conversions from string constants to
22044 "char *" are allowed, even though normally a "const char *"
22045 cannot be used to initialize a "char *". */
22046 if (TREE_CODE (expr) == STRING_CST)
22047 return expr;
22048 /* Preserve void and arithmetic constants, as an optimization -- there is no
22049 reason to create a new node. */
22050 if (TREE_CODE (expr) == VOID_CST
22051 || TREE_CODE (expr) == INTEGER_CST
22052 || TREE_CODE (expr) == REAL_CST)
22053 return expr;
22054 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22055 There is at least one place where we want to know that a
22056 particular expression is a throw-expression: when checking a ?:
22057 expression, there are special rules if the second or third
22058 argument is a throw-expression. */
22059 if (TREE_CODE (expr) == THROW_EXPR)
22060 return expr;
22061
22062 /* Don't wrap an initializer list, we need to be able to look inside. */
22063 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
22064 return expr;
22065
22066 /* Don't wrap a dummy object, we need to be able to test for it. */
22067 if (is_dummy_object (expr))
22068 return expr;
22069
22070 if (TREE_CODE (expr) == COND_EXPR)
22071 return build3 (COND_EXPR,
22072 TREE_TYPE (expr),
22073 TREE_OPERAND (expr, 0),
22074 (TREE_OPERAND (expr, 1)
22075 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22076 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22077 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22078 if (TREE_CODE (expr) == COMPOUND_EXPR
22079 && !COMPOUND_EXPR_OVERLOADED (expr))
22080 return build2 (COMPOUND_EXPR,
22081 TREE_TYPE (expr),
22082 TREE_OPERAND (expr, 0),
22083 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22084
22085 /* If the type is unknown, it can't really be non-dependent */
22086 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22087
22088 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22089 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22090 }
22091
22092 /* ARGS is a vector of expressions as arguments to a function call.
22093 Replace the arguments with equivalent non-dependent expressions.
22094 This modifies ARGS in place. */
22095
22096 void
22097 make_args_non_dependent (vec<tree, va_gc> *args)
22098 {
22099 unsigned int ix;
22100 tree arg;
22101
22102 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22103 {
22104 tree newarg = build_non_dependent_expr (arg);
22105 if (newarg != arg)
22106 (*args)[ix] = newarg;
22107 }
22108 }
22109
22110 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22111 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22112 parms. */
22113
22114 static tree
22115 make_auto_1 (tree name)
22116 {
22117 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22118 TYPE_NAME (au) = build_decl (input_location,
22119 TYPE_DECL, name, au);
22120 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22121 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22122 (0, processing_template_decl + 1, processing_template_decl + 1,
22123 TYPE_NAME (au), NULL_TREE);
22124 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22125 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22126 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22127
22128 return au;
22129 }
22130
22131 tree
22132 make_decltype_auto (void)
22133 {
22134 return make_auto_1 (get_identifier ("decltype(auto)"));
22135 }
22136
22137 tree
22138 make_auto (void)
22139 {
22140 return make_auto_1 (get_identifier ("auto"));
22141 }
22142
22143 /* Given type ARG, return std::initializer_list<ARG>. */
22144
22145 static tree
22146 listify (tree arg)
22147 {
22148 tree std_init_list = namespace_binding
22149 (get_identifier ("initializer_list"), std_node);
22150 tree argvec;
22151 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22152 {
22153 error ("deducing from brace-enclosed initializer list requires "
22154 "#include <initializer_list>");
22155 return error_mark_node;
22156 }
22157 argvec = make_tree_vec (1);
22158 TREE_VEC_ELT (argvec, 0) = arg;
22159 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22160 NULL_TREE, 0, tf_warning_or_error);
22161 }
22162
22163 /* Replace auto in TYPE with std::initializer_list<auto>. */
22164
22165 static tree
22166 listify_autos (tree type, tree auto_node)
22167 {
22168 tree init_auto = listify (auto_node);
22169 tree argvec = make_tree_vec (1);
22170 TREE_VEC_ELT (argvec, 0) = init_auto;
22171 if (processing_template_decl)
22172 argvec = add_to_template_args (current_template_args (), argvec);
22173 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22174 }
22175
22176 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22177 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22178
22179 tree
22180 do_auto_deduction (tree type, tree init, tree auto_node)
22181 {
22182 tree targs;
22183
22184 if (init == error_mark_node)
22185 return error_mark_node;
22186
22187 if (type_dependent_expression_p (init))
22188 /* Defining a subset of type-dependent expressions that we can deduce
22189 from ahead of time isn't worth the trouble. */
22190 return type;
22191
22192 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22193 with either a new invented type template parameter U or, if the
22194 initializer is a braced-init-list (8.5.4), with
22195 std::initializer_list<U>. */
22196 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22197 {
22198 if (!DIRECT_LIST_INIT_P (init))
22199 type = listify_autos (type, auto_node);
22200 else if (CONSTRUCTOR_NELTS (init) == 1)
22201 init = CONSTRUCTOR_ELT (init, 0)->value;
22202 else
22203 {
22204 if (permerror (input_location, "direct-list-initialization of "
22205 "%<auto%> requires exactly one element"))
22206 inform (input_location,
22207 "for deduction to %<std::initializer_list%>, use copy-"
22208 "list-initialization (i.e. add %<=%> before the %<{%>)");
22209 type = listify_autos (type, auto_node);
22210 }
22211 }
22212
22213 init = resolve_nondeduced_context (init);
22214
22215 targs = make_tree_vec (1);
22216 if (AUTO_IS_DECLTYPE (auto_node))
22217 {
22218 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22219 && !REF_PARENTHESIZED_P (init)));
22220 TREE_VEC_ELT (targs, 0)
22221 = finish_decltype_type (init, id, tf_warning_or_error);
22222 if (type != auto_node)
22223 {
22224 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22225 return error_mark_node;
22226 }
22227 }
22228 else
22229 {
22230 tree parms = build_tree_list (NULL_TREE, type);
22231 tree tparms = make_tree_vec (1);
22232 int val;
22233
22234 TREE_VEC_ELT (tparms, 0)
22235 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22236 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22237 DEDUCE_CALL, LOOKUP_NORMAL,
22238 NULL, /*explain_p=*/false);
22239 if (val > 0)
22240 {
22241 if (processing_template_decl)
22242 /* Try again at instantiation time. */
22243 return type;
22244 if (type && type != error_mark_node)
22245 /* If type is error_mark_node a diagnostic must have been
22246 emitted by now. Also, having a mention to '<type error>'
22247 in the diagnostic is not really useful to the user. */
22248 {
22249 if (cfun && auto_node == current_function_auto_return_pattern
22250 && LAMBDA_FUNCTION_P (current_function_decl))
22251 error ("unable to deduce lambda return type from %qE", init);
22252 else
22253 error ("unable to deduce %qT from %qE", type, init);
22254 }
22255 return error_mark_node;
22256 }
22257 }
22258
22259 /* If the list of declarators contains more than one declarator, the type
22260 of each declared variable is determined as described above. If the
22261 type deduced for the template parameter U is not the same in each
22262 deduction, the program is ill-formed. */
22263 if (TREE_TYPE (auto_node)
22264 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22265 {
22266 if (cfun && auto_node == current_function_auto_return_pattern
22267 && LAMBDA_FUNCTION_P (current_function_decl))
22268 error ("inconsistent types %qT and %qT deduced for "
22269 "lambda return type", TREE_TYPE (auto_node),
22270 TREE_VEC_ELT (targs, 0));
22271 else
22272 error ("inconsistent deduction for %qT: %qT and then %qT",
22273 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22274 return error_mark_node;
22275 }
22276 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22277
22278 if (processing_template_decl)
22279 targs = add_to_template_args (current_template_args (), targs);
22280 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22281 }
22282
22283 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22284 result. */
22285
22286 tree
22287 splice_late_return_type (tree type, tree late_return_type)
22288 {
22289 tree argvec;
22290
22291 if (late_return_type == NULL_TREE)
22292 return type;
22293 argvec = make_tree_vec (1);
22294 TREE_VEC_ELT (argvec, 0) = late_return_type;
22295 if (processing_template_parmlist)
22296 /* For a late-specified return type in a template type-parameter, we
22297 need to add a dummy argument level for its parmlist. */
22298 argvec = add_to_template_args
22299 (make_tree_vec (processing_template_parmlist), argvec);
22300 if (current_template_parms)
22301 argvec = add_to_template_args (current_template_args (), argvec);
22302 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22303 }
22304
22305 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22306 'decltype(auto)'. */
22307
22308 bool
22309 is_auto (const_tree type)
22310 {
22311 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22312 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22313 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22314 return true;
22315 else
22316 return false;
22317 }
22318
22319 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22320 a use of `auto'. Returns NULL_TREE otherwise. */
22321
22322 tree
22323 type_uses_auto (tree type)
22324 {
22325 return find_type_usage (type, is_auto);
22326 }
22327
22328 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22329 'decltype(auto)' or a concept. */
22330
22331 bool
22332 is_auto_or_concept (const_tree type)
22333 {
22334 return is_auto (type); // or concept
22335 }
22336
22337 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22338 a concept identifier) iff TYPE contains a use of a generic type. Returns
22339 NULL_TREE otherwise. */
22340
22341 tree
22342 type_uses_auto_or_concept (tree type)
22343 {
22344 return find_type_usage (type, is_auto_or_concept);
22345 }
22346
22347
22348 /* For a given template T, return the vector of typedefs referenced
22349 in T for which access check is needed at T instantiation time.
22350 T is either a FUNCTION_DECL or a RECORD_TYPE.
22351 Those typedefs were added to T by the function
22352 append_type_to_template_for_access_check. */
22353
22354 vec<qualified_typedef_usage_t, va_gc> *
22355 get_types_needing_access_check (tree t)
22356 {
22357 tree ti;
22358 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22359
22360 if (!t || t == error_mark_node)
22361 return NULL;
22362
22363 if (!(ti = get_template_info (t)))
22364 return NULL;
22365
22366 if (CLASS_TYPE_P (t)
22367 || TREE_CODE (t) == FUNCTION_DECL)
22368 {
22369 if (!TI_TEMPLATE (ti))
22370 return NULL;
22371
22372 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22373 }
22374
22375 return result;
22376 }
22377
22378 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22379 tied to T. That list of typedefs will be access checked at
22380 T instantiation time.
22381 T is either a FUNCTION_DECL or a RECORD_TYPE.
22382 TYPE_DECL is a TYPE_DECL node representing a typedef.
22383 SCOPE is the scope through which TYPE_DECL is accessed.
22384 LOCATION is the location of the usage point of TYPE_DECL.
22385
22386 This function is a subroutine of
22387 append_type_to_template_for_access_check. */
22388
22389 static void
22390 append_type_to_template_for_access_check_1 (tree t,
22391 tree type_decl,
22392 tree scope,
22393 location_t location)
22394 {
22395 qualified_typedef_usage_t typedef_usage;
22396 tree ti;
22397
22398 if (!t || t == error_mark_node)
22399 return;
22400
22401 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22402 || CLASS_TYPE_P (t))
22403 && type_decl
22404 && TREE_CODE (type_decl) == TYPE_DECL
22405 && scope);
22406
22407 if (!(ti = get_template_info (t)))
22408 return;
22409
22410 gcc_assert (TI_TEMPLATE (ti));
22411
22412 typedef_usage.typedef_decl = type_decl;
22413 typedef_usage.context = scope;
22414 typedef_usage.locus = location;
22415
22416 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22417 }
22418
22419 /* Append TYPE_DECL to the template TEMPL.
22420 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22421 At TEMPL instanciation time, TYPE_DECL will be checked to see
22422 if it can be accessed through SCOPE.
22423 LOCATION is the location of the usage point of TYPE_DECL.
22424
22425 e.g. consider the following code snippet:
22426
22427 class C
22428 {
22429 typedef int myint;
22430 };
22431
22432 template<class U> struct S
22433 {
22434 C::myint mi; // <-- usage point of the typedef C::myint
22435 };
22436
22437 S<char> s;
22438
22439 At S<char> instantiation time, we need to check the access of C::myint
22440 In other words, we need to check the access of the myint typedef through
22441 the C scope. For that purpose, this function will add the myint typedef
22442 and the scope C through which its being accessed to a list of typedefs
22443 tied to the template S. That list will be walked at template instantiation
22444 time and access check performed on each typedefs it contains.
22445 Note that this particular code snippet should yield an error because
22446 myint is private to C. */
22447
22448 void
22449 append_type_to_template_for_access_check (tree templ,
22450 tree type_decl,
22451 tree scope,
22452 location_t location)
22453 {
22454 qualified_typedef_usage_t *iter;
22455 unsigned i;
22456
22457 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22458
22459 /* Make sure we don't append the type to the template twice. */
22460 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22461 if (iter->typedef_decl == type_decl && scope == iter->context)
22462 return;
22463
22464 append_type_to_template_for_access_check_1 (templ, type_decl,
22465 scope, location);
22466 }
22467
22468 /* Convert the generic type parameters in PARM that match the types given in the
22469 range [START_IDX, END_IDX) from the current_template_parms into generic type
22470 packs. */
22471
22472 tree
22473 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22474 {
22475 tree current = current_template_parms;
22476 int depth = TMPL_PARMS_DEPTH (current);
22477 current = INNERMOST_TEMPLATE_PARMS (current);
22478 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22479
22480 for (int i = 0; i < start_idx; ++i)
22481 TREE_VEC_ELT (replacement, i)
22482 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22483
22484 for (int i = start_idx; i < end_idx; ++i)
22485 {
22486 /* Create a distinct parameter pack type from the current parm and add it
22487 to the replacement args to tsubst below into the generic function
22488 parameter. */
22489
22490 tree o = TREE_TYPE (TREE_VALUE
22491 (TREE_VEC_ELT (current, i)));
22492 tree t = copy_type (o);
22493 TEMPLATE_TYPE_PARM_INDEX (t)
22494 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22495 o, 0, 0, tf_none);
22496 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22497 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22498 TYPE_MAIN_VARIANT (t) = t;
22499 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22500 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22501 TREE_VEC_ELT (replacement, i) = t;
22502 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22503 }
22504
22505 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22506 TREE_VEC_ELT (replacement, i)
22507 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22508
22509 /* If there are more levels then build up the replacement with the outer
22510 template parms. */
22511 if (depth > 1)
22512 replacement = add_to_template_args (template_parms_to_args
22513 (TREE_CHAIN (current_template_parms)),
22514 replacement);
22515
22516 return tsubst (parm, replacement, tf_none, NULL_TREE);
22517 }
22518
22519
22520 /* Set up the hash tables for template instantiations. */
22521
22522 void
22523 init_template_processing (void)
22524 {
22525 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
22526 type_specializations = hash_table<spec_hasher>::create_ggc (37);
22527 }
22528
22529 /* Print stats about the template hash tables for -fstats. */
22530
22531 void
22532 print_template_statistics (void)
22533 {
22534 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22535 "%f collisions\n", (long) decl_specializations->size (),
22536 (long) decl_specializations->elements (),
22537 decl_specializations->collisions ());
22538 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22539 "%f collisions\n", (long) type_specializations->size (),
22540 (long) type_specializations->elements (),
22541 type_specializations->collisions ());
22542 }
22543
22544 #include "gt-cp-pt.h"