DR 1351 Represent the unevaluated exception specification of an implicitly declared...
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
50
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
54
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
71
72 static GTY(()) struct tinst_level *current_tinst_level;
73
74 static GTY(()) tree saved_access_scope;
75
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
80
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth;
83
84 typedef struct GTY(()) spec_entry
85 {
86 tree tmpl;
87 tree args;
88 tree spec;
89 } spec_entry;
90
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
93
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
96
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
102
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111
112 enum template_base_result {
113 tbr_incomplete_type,
114 tbr_ambiguous_baseclass,
115 tbr_success
116 };
117
118 static void push_access_scope (tree);
119 static void pop_access_scope (tree);
120 static bool resolve_overloaded_unification (tree, tree, tree, tree,
121 unification_kind_t, int,
122 bool);
123 static int try_one_overload (tree, tree, tree, tree, tree,
124 unification_kind_t, int, bool, bool);
125 static int unify (tree, tree, tree, tree, int, bool);
126 static void add_pending_template (tree);
127 static tree reopen_tinst_level (struct tinst_level *);
128 static tree tsubst_initializer_list (tree, tree);
129 static tree get_class_bindings (tree, tree, tree, tree);
130 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
131 bool, bool);
132 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
139 tree);
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
142 vec<deferred_access_check, va_gc> **,
143 bool);
144 static void note_template_header (int);
145 static tree convert_nontype_argument_function (tree, tree);
146 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
147 static tree convert_template_argument (tree, tree, tree,
148 tsubst_flags_t, int, tree);
149 static int for_each_template_parm (tree, tree_fn_t, void*,
150 struct pointer_set_t*, bool);
151 static tree expand_template_argument_pack (tree);
152 static tree build_template_parm_index (int, int, int, tree, tree);
153 static bool inline_needs_template_parms (tree, bool);
154 static void push_inline_template_parms_recursive (tree, int);
155 static tree retrieve_local_specialization (tree);
156 static void register_local_specialization (tree, tree);
157 static hashval_t hash_specialization (const void *p);
158 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
159 static int mark_template_parm (tree, void *);
160 static int template_parm_this_level_p (tree, void *);
161 static tree tsubst_friend_function (tree, tree);
162 static tree tsubst_friend_class (tree, tree);
163 static int can_complete_type_without_circularity (tree);
164 static tree get_bindings (tree, tree, tree, bool);
165 static int template_decl_level (tree);
166 static int check_cv_quals_for_unify (int, tree, tree);
167 static void template_parm_level_and_index (tree, int*, int*);
168 static int unify_pack_expansion (tree, tree, tree,
169 tree, unification_kind_t, bool, bool);
170 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
173 static void regenerate_decl_from_template (tree, tree);
174 static tree most_specialized_class (tree, tsubst_flags_t);
175 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
176 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
178 static bool check_specialization_scope (void);
179 static tree process_partial_specialization (tree);
180 static void set_current_access_from_decl (tree);
181 static enum template_base_result get_template_base (tree, tree, tree, tree,
182 bool , tree *);
183 static tree try_class_unification (tree, tree, tree, tree, bool);
184 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
185 tree, tree);
186 static bool template_template_parm_bindings_ok_p (tree, tree);
187 static int template_args_equal (tree, tree);
188 static void tsubst_default_arguments (tree, tsubst_flags_t);
189 static tree for_each_template_parm_r (tree *, int *, void *);
190 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
191 static void copy_default_args_to_explicit_spec (tree);
192 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
193 static bool dependent_template_arg_p (tree);
194 static bool any_template_arguments_need_structural_equality_p (tree);
195 static bool dependent_type_p_r (tree);
196 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
197 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_decl (tree, tree, tsubst_flags_t);
200 static void perform_typedefs_access_check (tree tmpl, tree targs);
201 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
202 location_t);
203 static tree listify (tree);
204 static tree listify_autos (tree, tree);
205 static tree template_parm_to_arg (tree t);
206 static tree current_template_args (void);
207 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
208 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
209
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
214
215 static void
216 push_access_scope (tree t)
217 {
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
219 || TREE_CODE (t) == TYPE_DECL);
220
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
225 else
226 push_to_top_level ();
227
228 if (TREE_CODE (t) == FUNCTION_DECL)
229 {
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
233 }
234 }
235
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
238
239 static void
240 pop_access_scope (tree t)
241 {
242 if (TREE_CODE (t) == FUNCTION_DECL)
243 {
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
246 }
247
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
252 }
253
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
258
259 tree
260 finish_member_template_decl (tree decl)
261 {
262 if (decl == error_mark_node)
263 return error_mark_node;
264
265 gcc_assert (DECL_P (decl));
266
267 if (TREE_CODE (decl) == TYPE_DECL)
268 {
269 tree type;
270
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
277 {
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
280 return tmpl;
281 }
282 return NULL_TREE;
283 }
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
287 {
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
289 {
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
292 }
293 else
294 return decl;
295 }
296 else
297 error ("invalid member template declaration %qD", decl);
298
299 return error_mark_node;
300 }
301
302 /* Create a template info node. */
303
304 tree
305 build_template_info (tree template_decl, tree template_args)
306 {
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
310 return result;
311 }
312
313 /* Return the template info node corresponding to T, whatever T is. */
314
315 tree
316 get_template_info (const_tree t)
317 {
318 tree tinfo = NULL_TREE;
319
320 if (!t || t == error_mark_node)
321 return NULL;
322
323 if (TREE_CODE (t) == NAMESPACE_DECL)
324 return NULL;
325
326 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
327 tinfo = DECL_TEMPLATE_INFO (t);
328
329 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
330 t = TREE_TYPE (t);
331
332 if (OVERLOAD_TYPE_P (t))
333 tinfo = TYPE_TEMPLATE_INFO (t);
334 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
335 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
336
337 return tinfo;
338 }
339
340 /* Returns the template nesting level of the indicated class TYPE.
341
342 For example, in:
343 template <class T>
344 struct A
345 {
346 template <class U>
347 struct B {};
348 };
349
350 A<T>::B<U> has depth two, while A<T> has depth one.
351 Both A<T>::B<int> and A<int>::B<U> have depth one, if
352 they are instantiations, not specializations.
353
354 This function is guaranteed to return 0 if passed NULL_TREE so
355 that, for example, `template_class_depth (current_class_type)' is
356 always safe. */
357
358 int
359 template_class_depth (tree type)
360 {
361 int depth;
362
363 for (depth = 0;
364 type && TREE_CODE (type) != NAMESPACE_DECL;
365 type = (TREE_CODE (type) == FUNCTION_DECL)
366 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
367 {
368 tree tinfo = get_template_info (type);
369
370 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
371 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
372 ++depth;
373 }
374
375 return depth;
376 }
377
378 /* Subroutine of maybe_begin_member_template_processing.
379 Returns true if processing DECL needs us to push template parms. */
380
381 static bool
382 inline_needs_template_parms (tree decl, bool nsdmi)
383 {
384 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
385 return false;
386
387 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
388 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
389 }
390
391 /* Subroutine of maybe_begin_member_template_processing.
392 Push the template parms in PARMS, starting from LEVELS steps into the
393 chain, and ending at the beginning, since template parms are listed
394 innermost first. */
395
396 static void
397 push_inline_template_parms_recursive (tree parmlist, int levels)
398 {
399 tree parms = TREE_VALUE (parmlist);
400 int i;
401
402 if (levels > 1)
403 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
404
405 ++processing_template_decl;
406 current_template_parms
407 = tree_cons (size_int (processing_template_decl),
408 parms, current_template_parms);
409 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
410
411 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
412 NULL);
413 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
414 {
415 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
416
417 if (error_operand_p (parm))
418 continue;
419
420 gcc_assert (DECL_P (parm));
421
422 switch (TREE_CODE (parm))
423 {
424 case TYPE_DECL:
425 case TEMPLATE_DECL:
426 pushdecl (parm);
427 break;
428
429 case PARM_DECL:
430 {
431 /* Make a CONST_DECL as is done in process_template_parm.
432 It is ugly that we recreate this here; the original
433 version built in process_template_parm is no longer
434 available. */
435 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
436 CONST_DECL, DECL_NAME (parm),
437 TREE_TYPE (parm));
438 DECL_ARTIFICIAL (decl) = 1;
439 TREE_CONSTANT (decl) = 1;
440 TREE_READONLY (decl) = 1;
441 DECL_INITIAL (decl) = DECL_INITIAL (parm);
442 SET_DECL_TEMPLATE_PARM_P (decl);
443 pushdecl (decl);
444 }
445 break;
446
447 default:
448 gcc_unreachable ();
449 }
450 }
451 }
452
453 /* Restore the template parameter context for a member template, a
454 friend template defined in a class definition, or a non-template
455 member of template class. */
456
457 void
458 maybe_begin_member_template_processing (tree decl)
459 {
460 tree parms;
461 int levels = 0;
462 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
463
464 if (nsdmi)
465 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
466 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
467 : NULL_TREE);
468
469 if (inline_needs_template_parms (decl, nsdmi))
470 {
471 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
472 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
473
474 if (DECL_TEMPLATE_SPECIALIZATION (decl))
475 {
476 --levels;
477 parms = TREE_CHAIN (parms);
478 }
479
480 push_inline_template_parms_recursive (parms, levels);
481 }
482
483 /* Remember how many levels of template parameters we pushed so that
484 we can pop them later. */
485 inline_parm_levels.safe_push (levels);
486 }
487
488 /* Undo the effects of maybe_begin_member_template_processing. */
489
490 void
491 maybe_end_member_template_processing (void)
492 {
493 int i;
494 int last;
495
496 if (inline_parm_levels.length () == 0)
497 return;
498
499 last = inline_parm_levels.pop ();
500 for (i = 0; i < last; ++i)
501 {
502 --processing_template_decl;
503 current_template_parms = TREE_CHAIN (current_template_parms);
504 poplevel (0, 0, 0);
505 }
506 }
507
508 /* Return a new template argument vector which contains all of ARGS,
509 but has as its innermost set of arguments the EXTRA_ARGS. */
510
511 static tree
512 add_to_template_args (tree args, tree extra_args)
513 {
514 tree new_args;
515 int extra_depth;
516 int i;
517 int j;
518
519 if (args == NULL_TREE || extra_args == error_mark_node)
520 return extra_args;
521
522 extra_depth = TMPL_ARGS_DEPTH (extra_args);
523 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
524
525 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
526 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
527
528 for (j = 1; j <= extra_depth; ++j, ++i)
529 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
530
531 return new_args;
532 }
533
534 /* Like add_to_template_args, but only the outermost ARGS are added to
535 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
536 (EXTRA_ARGS) levels are added. This function is used to combine
537 the template arguments from a partial instantiation with the
538 template arguments used to attain the full instantiation from the
539 partial instantiation. */
540
541 static tree
542 add_outermost_template_args (tree args, tree extra_args)
543 {
544 tree new_args;
545
546 /* If there are more levels of EXTRA_ARGS than there are ARGS,
547 something very fishy is going on. */
548 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
549
550 /* If *all* the new arguments will be the EXTRA_ARGS, just return
551 them. */
552 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
553 return extra_args;
554
555 /* For the moment, we make ARGS look like it contains fewer levels. */
556 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
557
558 new_args = add_to_template_args (args, extra_args);
559
560 /* Now, we restore ARGS to its full dimensions. */
561 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
562
563 return new_args;
564 }
565
566 /* Return the N levels of innermost template arguments from the ARGS. */
567
568 tree
569 get_innermost_template_args (tree args, int n)
570 {
571 tree new_args;
572 int extra_levels;
573 int i;
574
575 gcc_assert (n >= 0);
576
577 /* If N is 1, just return the innermost set of template arguments. */
578 if (n == 1)
579 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
580
581 /* If we're not removing anything, just return the arguments we were
582 given. */
583 extra_levels = TMPL_ARGS_DEPTH (args) - n;
584 gcc_assert (extra_levels >= 0);
585 if (extra_levels == 0)
586 return args;
587
588 /* Make a new set of arguments, not containing the outer arguments. */
589 new_args = make_tree_vec (n);
590 for (i = 1; i <= n; ++i)
591 SET_TMPL_ARGS_LEVEL (new_args, i,
592 TMPL_ARGS_LEVEL (args, i + extra_levels));
593
594 return new_args;
595 }
596
597 /* The inverse of get_innermost_template_args: Return all but the innermost
598 EXTRA_LEVELS levels of template arguments from the ARGS. */
599
600 static tree
601 strip_innermost_template_args (tree args, int extra_levels)
602 {
603 tree new_args;
604 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
605 int i;
606
607 gcc_assert (n >= 0);
608
609 /* If N is 1, just return the outermost set of template arguments. */
610 if (n == 1)
611 return TMPL_ARGS_LEVEL (args, 1);
612
613 /* If we're not removing anything, just return the arguments we were
614 given. */
615 gcc_assert (extra_levels >= 0);
616 if (extra_levels == 0)
617 return args;
618
619 /* Make a new set of arguments, not containing the inner arguments. */
620 new_args = make_tree_vec (n);
621 for (i = 1; i <= n; ++i)
622 SET_TMPL_ARGS_LEVEL (new_args, i,
623 TMPL_ARGS_LEVEL (args, i));
624
625 return new_args;
626 }
627
628 /* We've got a template header coming up; push to a new level for storing
629 the parms. */
630
631 void
632 begin_template_parm_list (void)
633 {
634 /* We use a non-tag-transparent scope here, which causes pushtag to
635 put tags in this scope, rather than in the enclosing class or
636 namespace scope. This is the right thing, since we want
637 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
638 global template class, push_template_decl handles putting the
639 TEMPLATE_DECL into top-level scope. For a nested template class,
640 e.g.:
641
642 template <class T> struct S1 {
643 template <class T> struct S2 {};
644 };
645
646 pushtag contains special code to call pushdecl_with_scope on the
647 TEMPLATE_DECL for S2. */
648 begin_scope (sk_template_parms, NULL);
649 ++processing_template_decl;
650 ++processing_template_parmlist;
651 note_template_header (0);
652 }
653
654 /* This routine is called when a specialization is declared. If it is
655 invalid to declare a specialization here, an error is reported and
656 false is returned, otherwise this routine will return true. */
657
658 static bool
659 check_specialization_scope (void)
660 {
661 tree scope = current_scope ();
662
663 /* [temp.expl.spec]
664
665 An explicit specialization shall be declared in the namespace of
666 which the template is a member, or, for member templates, in the
667 namespace of which the enclosing class or enclosing class
668 template is a member. An explicit specialization of a member
669 function, member class or static data member of a class template
670 shall be declared in the namespace of which the class template
671 is a member. */
672 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
673 {
674 error ("explicit specialization in non-namespace scope %qD", scope);
675 return false;
676 }
677
678 /* [temp.expl.spec]
679
680 In an explicit specialization declaration for a member of a class
681 template or a member template that appears in namespace scope,
682 the member template and some of its enclosing class templates may
683 remain unspecialized, except that the declaration shall not
684 explicitly specialize a class member template if its enclosing
685 class templates are not explicitly specialized as well. */
686 if (current_template_parms)
687 {
688 error ("enclosing class templates are not explicitly specialized");
689 return false;
690 }
691
692 return true;
693 }
694
695 /* We've just seen template <>. */
696
697 bool
698 begin_specialization (void)
699 {
700 begin_scope (sk_template_spec, NULL);
701 note_template_header (1);
702 return check_specialization_scope ();
703 }
704
705 /* Called at then end of processing a declaration preceded by
706 template<>. */
707
708 void
709 end_specialization (void)
710 {
711 finish_scope ();
712 reset_specialization ();
713 }
714
715 /* Any template <>'s that we have seen thus far are not referring to a
716 function specialization. */
717
718 void
719 reset_specialization (void)
720 {
721 processing_specialization = 0;
722 template_header_count = 0;
723 }
724
725 /* We've just seen a template header. If SPECIALIZATION is nonzero,
726 it was of the form template <>. */
727
728 static void
729 note_template_header (int specialization)
730 {
731 processing_specialization = specialization;
732 template_header_count++;
733 }
734
735 /* We're beginning an explicit instantiation. */
736
737 void
738 begin_explicit_instantiation (void)
739 {
740 gcc_assert (!processing_explicit_instantiation);
741 processing_explicit_instantiation = true;
742 }
743
744
745 void
746 end_explicit_instantiation (void)
747 {
748 gcc_assert (processing_explicit_instantiation);
749 processing_explicit_instantiation = false;
750 }
751
752 /* An explicit specialization or partial specialization of TMPL is being
753 declared. Check that the namespace in which the specialization is
754 occurring is permissible. Returns false iff it is invalid to
755 specialize TMPL in the current namespace. */
756
757 static bool
758 check_specialization_namespace (tree tmpl)
759 {
760 tree tpl_ns = decl_namespace_context (tmpl);
761
762 /* [tmpl.expl.spec]
763
764 An explicit specialization shall be declared in the namespace of
765 which the template is a member, or, for member templates, in the
766 namespace of which the enclosing class or enclosing class
767 template is a member. An explicit specialization of a member
768 function, member class or static data member of a class template
769 shall be declared in the namespace of which the class template is
770 a member. */
771 if (current_scope() != DECL_CONTEXT (tmpl)
772 && !at_namespace_scope_p ())
773 {
774 error ("specialization of %qD must appear at namespace scope", tmpl);
775 return false;
776 }
777 if (is_associated_namespace (current_namespace, tpl_ns))
778 /* Same or super-using namespace. */
779 return true;
780 else
781 {
782 permerror (input_location, "specialization of %qD in different namespace", tmpl);
783 permerror (input_location, " from definition of %q+#D", tmpl);
784 return false;
785 }
786 }
787
788 /* SPEC is an explicit instantiation. Check that it is valid to
789 perform this explicit instantiation in the current namespace. */
790
791 static void
792 check_explicit_instantiation_namespace (tree spec)
793 {
794 tree ns;
795
796 /* DR 275: An explicit instantiation shall appear in an enclosing
797 namespace of its template. */
798 ns = decl_namespace_context (spec);
799 if (!is_ancestor (current_namespace, ns))
800 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
801 "(which does not enclose namespace %qD)",
802 spec, current_namespace, ns);
803 }
804
805 /* The TYPE is being declared. If it is a template type, that means it
806 is a partial specialization. Do appropriate error-checking. */
807
808 tree
809 maybe_process_partial_specialization (tree type)
810 {
811 tree context;
812
813 if (type == error_mark_node)
814 return error_mark_node;
815
816 /* A lambda that appears in specialization context is not itself a
817 specialization. */
818 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
819 return type;
820
821 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
822 {
823 error ("name of class shadows template template parameter %qD",
824 TYPE_NAME (type));
825 return error_mark_node;
826 }
827
828 context = TYPE_CONTEXT (type);
829
830 if (TYPE_ALIAS_P (type))
831 {
832 if (TYPE_TEMPLATE_INFO (type)
833 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
834 error ("specialization of alias template %qD",
835 TYPE_TI_TEMPLATE (type));
836 else
837 error ("explicit specialization of non-template %qT", type);
838 return error_mark_node;
839 }
840 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
841 {
842 /* This is for ordinary explicit specialization and partial
843 specialization of a template class such as:
844
845 template <> class C<int>;
846
847 or:
848
849 template <class T> class C<T*>;
850
851 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
852
853 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
854 && !COMPLETE_TYPE_P (type))
855 {
856 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
857 && !at_namespace_scope_p ())
858 return error_mark_node;
859 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
860 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
861 if (processing_template_decl)
862 {
863 if (push_template_decl (TYPE_MAIN_DECL (type))
864 == error_mark_node)
865 return error_mark_node;
866 }
867 }
868 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
869 error ("specialization of %qT after instantiation", type);
870 else if (errorcount && !processing_specialization
871 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
872 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
873 /* Trying to define a specialization either without a template<> header
874 or in an inappropriate place. We've already given an error, so just
875 bail now so we don't actually define the specialization. */
876 return error_mark_node;
877 }
878 else if (CLASS_TYPE_P (type)
879 && !CLASSTYPE_USE_TEMPLATE (type)
880 && CLASSTYPE_TEMPLATE_INFO (type)
881 && context && CLASS_TYPE_P (context)
882 && CLASSTYPE_TEMPLATE_INFO (context))
883 {
884 /* This is for an explicit specialization of member class
885 template according to [temp.expl.spec/18]:
886
887 template <> template <class U> class C<int>::D;
888
889 The context `C<int>' must be an implicit instantiation.
890 Otherwise this is just a member class template declared
891 earlier like:
892
893 template <> class C<int> { template <class U> class D; };
894 template <> template <class U> class C<int>::D;
895
896 In the first case, `C<int>::D' is a specialization of `C<T>::D'
897 while in the second case, `C<int>::D' is a primary template
898 and `C<T>::D' may not exist. */
899
900 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
901 && !COMPLETE_TYPE_P (type))
902 {
903 tree t;
904 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
905
906 if (current_namespace
907 != decl_namespace_context (tmpl))
908 {
909 permerror (input_location, "specializing %q#T in different namespace", type);
910 permerror (input_location, " from definition of %q+#D", tmpl);
911 }
912
913 /* Check for invalid specialization after instantiation:
914
915 template <> template <> class C<int>::D<int>;
916 template <> template <class U> class C<int>::D; */
917
918 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
919 t; t = TREE_CHAIN (t))
920 {
921 tree inst = TREE_VALUE (t);
922 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
923 || !COMPLETE_OR_OPEN_TYPE_P (inst))
924 {
925 /* We already have a full specialization of this partial
926 instantiation, or a full specialization has been
927 looked up but not instantiated. Reassign it to the
928 new member specialization template. */
929 spec_entry elt;
930 spec_entry *entry;
931 void **slot;
932
933 elt.tmpl = most_general_template (tmpl);
934 elt.args = CLASSTYPE_TI_ARGS (inst);
935 elt.spec = inst;
936
937 htab_remove_elt (type_specializations, &elt);
938
939 elt.tmpl = tmpl;
940 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
941
942 slot = htab_find_slot (type_specializations, &elt, INSERT);
943 entry = ggc_alloc_spec_entry ();
944 *entry = elt;
945 *slot = entry;
946 }
947 else
948 /* But if we've had an implicit instantiation, that's a
949 problem ([temp.expl.spec]/6). */
950 error ("specialization %qT after instantiation %qT",
951 type, inst);
952 }
953
954 /* Mark TYPE as a specialization. And as a result, we only
955 have one level of template argument for the innermost
956 class template. */
957 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
958 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
959 CLASSTYPE_TI_ARGS (type)
960 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
961 }
962 }
963 else if (processing_specialization)
964 {
965 /* Someday C++0x may allow for enum template specialization. */
966 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
967 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
968 pedwarn (input_location, OPT_Wpedantic, "template specialization "
969 "of %qD not allowed by ISO C++", type);
970 else
971 {
972 error ("explicit specialization of non-template %qT", type);
973 return error_mark_node;
974 }
975 }
976
977 return type;
978 }
979
980 /* Returns nonzero if we can optimize the retrieval of specializations
981 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
982 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
983
984 static inline bool
985 optimize_specialization_lookup_p (tree tmpl)
986 {
987 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
988 && DECL_CLASS_SCOPE_P (tmpl)
989 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
990 parameter. */
991 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
992 /* The optimized lookup depends on the fact that the
993 template arguments for the member function template apply
994 purely to the containing class, which is not true if the
995 containing class is an explicit or partial
996 specialization. */
997 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
998 && !DECL_MEMBER_TEMPLATE_P (tmpl)
999 && !DECL_CONV_FN_P (tmpl)
1000 /* It is possible to have a template that is not a member
1001 template and is not a member of a template class:
1002
1003 template <typename T>
1004 struct S { friend A::f(); };
1005
1006 Here, the friend function is a template, but the context does
1007 not have template information. The optimized lookup relies
1008 on having ARGS be the template arguments for both the class
1009 and the function template. */
1010 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1011 }
1012
1013 /* Retrieve the specialization (in the sense of [temp.spec] - a
1014 specialization is either an instantiation or an explicit
1015 specialization) of TMPL for the given template ARGS. If there is
1016 no such specialization, return NULL_TREE. The ARGS are a vector of
1017 arguments, or a vector of vectors of arguments, in the case of
1018 templates with more than one level of parameters.
1019
1020 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1021 then we search for a partial specialization matching ARGS. This
1022 parameter is ignored if TMPL is not a class template.
1023
1024 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1025 result is a NONTYPE_ARGUMENT_PACK. */
1026
1027 static tree
1028 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1029 {
1030 if (tmpl == NULL_TREE)
1031 return NULL_TREE;
1032
1033 if (args == error_mark_node)
1034 return NULL_TREE;
1035
1036 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1037 || TREE_CODE (tmpl) == FIELD_DECL);
1038
1039 /* There should be as many levels of arguments as there are
1040 levels of parameters. */
1041 gcc_assert (TMPL_ARGS_DEPTH (args)
1042 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1043 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1044 : template_class_depth (DECL_CONTEXT (tmpl))));
1045
1046 if (optimize_specialization_lookup_p (tmpl))
1047 {
1048 tree class_template;
1049 tree class_specialization;
1050 vec<tree, va_gc> *methods;
1051 tree fns;
1052 int idx;
1053
1054 /* The template arguments actually apply to the containing
1055 class. Find the class specialization with those
1056 arguments. */
1057 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1058 class_specialization
1059 = retrieve_specialization (class_template, args, 0);
1060 if (!class_specialization)
1061 return NULL_TREE;
1062 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1063 for the specialization. */
1064 idx = class_method_index_for_fn (class_specialization, tmpl);
1065 if (idx == -1)
1066 return NULL_TREE;
1067 /* Iterate through the methods with the indicated name, looking
1068 for the one that has an instance of TMPL. */
1069 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1070 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1071 {
1072 tree fn = OVL_CURRENT (fns);
1073 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1074 /* using-declarations can add base methods to the method vec,
1075 and we don't want those here. */
1076 && DECL_CONTEXT (fn) == class_specialization)
1077 return fn;
1078 }
1079 return NULL_TREE;
1080 }
1081 else
1082 {
1083 spec_entry *found;
1084 spec_entry elt;
1085 htab_t specializations;
1086
1087 elt.tmpl = tmpl;
1088 elt.args = args;
1089 elt.spec = NULL_TREE;
1090
1091 if (DECL_CLASS_TEMPLATE_P (tmpl))
1092 specializations = type_specializations;
1093 else
1094 specializations = decl_specializations;
1095
1096 if (hash == 0)
1097 hash = hash_specialization (&elt);
1098 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1099 if (found)
1100 return found->spec;
1101 }
1102
1103 return NULL_TREE;
1104 }
1105
1106 /* Like retrieve_specialization, but for local declarations. */
1107
1108 static tree
1109 retrieve_local_specialization (tree tmpl)
1110 {
1111 void **slot;
1112
1113 if (local_specializations == NULL)
1114 return NULL_TREE;
1115
1116 slot = pointer_map_contains (local_specializations, tmpl);
1117 return slot ? (tree) *slot : NULL_TREE;
1118 }
1119
1120 /* Returns nonzero iff DECL is a specialization of TMPL. */
1121
1122 int
1123 is_specialization_of (tree decl, tree tmpl)
1124 {
1125 tree t;
1126
1127 if (TREE_CODE (decl) == FUNCTION_DECL)
1128 {
1129 for (t = decl;
1130 t != NULL_TREE;
1131 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1132 if (t == tmpl)
1133 return 1;
1134 }
1135 else
1136 {
1137 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1138
1139 for (t = TREE_TYPE (decl);
1140 t != NULL_TREE;
1141 t = CLASSTYPE_USE_TEMPLATE (t)
1142 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1143 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1144 return 1;
1145 }
1146
1147 return 0;
1148 }
1149
1150 /* Returns nonzero iff DECL is a specialization of friend declaration
1151 FRIEND_DECL according to [temp.friend]. */
1152
1153 bool
1154 is_specialization_of_friend (tree decl, tree friend_decl)
1155 {
1156 bool need_template = true;
1157 int template_depth;
1158
1159 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1160 || TREE_CODE (decl) == TYPE_DECL);
1161
1162 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1163 of a template class, we want to check if DECL is a specialization
1164 if this. */
1165 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1166 && DECL_TEMPLATE_INFO (friend_decl)
1167 && !DECL_USE_TEMPLATE (friend_decl))
1168 {
1169 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1170 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1171 need_template = false;
1172 }
1173 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1174 && !PRIMARY_TEMPLATE_P (friend_decl))
1175 need_template = false;
1176
1177 /* There is nothing to do if this is not a template friend. */
1178 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1179 return false;
1180
1181 if (is_specialization_of (decl, friend_decl))
1182 return true;
1183
1184 /* [temp.friend/6]
1185 A member of a class template may be declared to be a friend of a
1186 non-template class. In this case, the corresponding member of
1187 every specialization of the class template is a friend of the
1188 class granting friendship.
1189
1190 For example, given a template friend declaration
1191
1192 template <class T> friend void A<T>::f();
1193
1194 the member function below is considered a friend
1195
1196 template <> struct A<int> {
1197 void f();
1198 };
1199
1200 For this type of template friend, TEMPLATE_DEPTH below will be
1201 nonzero. To determine if DECL is a friend of FRIEND, we first
1202 check if the enclosing class is a specialization of another. */
1203
1204 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1205 if (template_depth
1206 && DECL_CLASS_SCOPE_P (decl)
1207 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1208 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1209 {
1210 /* Next, we check the members themselves. In order to handle
1211 a few tricky cases, such as when FRIEND_DECL's are
1212
1213 template <class T> friend void A<T>::g(T t);
1214 template <class T> template <T t> friend void A<T>::h();
1215
1216 and DECL's are
1217
1218 void A<int>::g(int);
1219 template <int> void A<int>::h();
1220
1221 we need to figure out ARGS, the template arguments from
1222 the context of DECL. This is required for template substitution
1223 of `T' in the function parameter of `g' and template parameter
1224 of `h' in the above examples. Here ARGS corresponds to `int'. */
1225
1226 tree context = DECL_CONTEXT (decl);
1227 tree args = NULL_TREE;
1228 int current_depth = 0;
1229
1230 while (current_depth < template_depth)
1231 {
1232 if (CLASSTYPE_TEMPLATE_INFO (context))
1233 {
1234 if (current_depth == 0)
1235 args = TYPE_TI_ARGS (context);
1236 else
1237 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1238 current_depth++;
1239 }
1240 context = TYPE_CONTEXT (context);
1241 }
1242
1243 if (TREE_CODE (decl) == FUNCTION_DECL)
1244 {
1245 bool is_template;
1246 tree friend_type;
1247 tree decl_type;
1248 tree friend_args_type;
1249 tree decl_args_type;
1250
1251 /* Make sure that both DECL and FRIEND_DECL are templates or
1252 non-templates. */
1253 is_template = DECL_TEMPLATE_INFO (decl)
1254 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1255 if (need_template ^ is_template)
1256 return false;
1257 else if (is_template)
1258 {
1259 /* If both are templates, check template parameter list. */
1260 tree friend_parms
1261 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1262 args, tf_none);
1263 if (!comp_template_parms
1264 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1265 friend_parms))
1266 return false;
1267
1268 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1269 }
1270 else
1271 decl_type = TREE_TYPE (decl);
1272
1273 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1274 tf_none, NULL_TREE);
1275 if (friend_type == error_mark_node)
1276 return false;
1277
1278 /* Check if return types match. */
1279 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1280 return false;
1281
1282 /* Check if function parameter types match, ignoring the
1283 `this' parameter. */
1284 friend_args_type = TYPE_ARG_TYPES (friend_type);
1285 decl_args_type = TYPE_ARG_TYPES (decl_type);
1286 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1287 friend_args_type = TREE_CHAIN (friend_args_type);
1288 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1289 decl_args_type = TREE_CHAIN (decl_args_type);
1290
1291 return compparms (decl_args_type, friend_args_type);
1292 }
1293 else
1294 {
1295 /* DECL is a TYPE_DECL */
1296 bool is_template;
1297 tree decl_type = TREE_TYPE (decl);
1298
1299 /* Make sure that both DECL and FRIEND_DECL are templates or
1300 non-templates. */
1301 is_template
1302 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1303 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1304
1305 if (need_template ^ is_template)
1306 return false;
1307 else if (is_template)
1308 {
1309 tree friend_parms;
1310 /* If both are templates, check the name of the two
1311 TEMPLATE_DECL's first because is_friend didn't. */
1312 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1313 != DECL_NAME (friend_decl))
1314 return false;
1315
1316 /* Now check template parameter list. */
1317 friend_parms
1318 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1319 args, tf_none);
1320 return comp_template_parms
1321 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1322 friend_parms);
1323 }
1324 else
1325 return (DECL_NAME (decl)
1326 == DECL_NAME (friend_decl));
1327 }
1328 }
1329 return false;
1330 }
1331
1332 /* Register the specialization SPEC as a specialization of TMPL with
1333 the indicated ARGS. IS_FRIEND indicates whether the specialization
1334 is actually just a friend declaration. Returns SPEC, or an
1335 equivalent prior declaration, if available.
1336
1337 We also store instantiations of field packs in the hash table, even
1338 though they are not themselves templates, to make lookup easier. */
1339
1340 static tree
1341 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1342 hashval_t hash)
1343 {
1344 tree fn;
1345 void **slot = NULL;
1346 spec_entry elt;
1347
1348 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1349 || (TREE_CODE (tmpl) == FIELD_DECL
1350 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1351
1352 if (TREE_CODE (spec) == FUNCTION_DECL
1353 && uses_template_parms (DECL_TI_ARGS (spec)))
1354 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1355 register it; we want the corresponding TEMPLATE_DECL instead.
1356 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1357 the more obvious `uses_template_parms (spec)' to avoid problems
1358 with default function arguments. In particular, given
1359 something like this:
1360
1361 template <class T> void f(T t1, T t = T())
1362
1363 the default argument expression is not substituted for in an
1364 instantiation unless and until it is actually needed. */
1365 return spec;
1366
1367 if (optimize_specialization_lookup_p (tmpl))
1368 /* We don't put these specializations in the hash table, but we might
1369 want to give an error about a mismatch. */
1370 fn = retrieve_specialization (tmpl, args, 0);
1371 else
1372 {
1373 elt.tmpl = tmpl;
1374 elt.args = args;
1375 elt.spec = spec;
1376
1377 if (hash == 0)
1378 hash = hash_specialization (&elt);
1379
1380 slot =
1381 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1382 if (*slot)
1383 fn = ((spec_entry *) *slot)->spec;
1384 else
1385 fn = NULL_TREE;
1386 }
1387
1388 /* We can sometimes try to re-register a specialization that we've
1389 already got. In particular, regenerate_decl_from_template calls
1390 duplicate_decls which will update the specialization list. But,
1391 we'll still get called again here anyhow. It's more convenient
1392 to simply allow this than to try to prevent it. */
1393 if (fn == spec)
1394 return spec;
1395 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1396 {
1397 if (DECL_TEMPLATE_INSTANTIATION (fn))
1398 {
1399 if (DECL_ODR_USED (fn)
1400 || DECL_EXPLICIT_INSTANTIATION (fn))
1401 {
1402 error ("specialization of %qD after instantiation",
1403 fn);
1404 return error_mark_node;
1405 }
1406 else
1407 {
1408 tree clone;
1409 /* This situation should occur only if the first
1410 specialization is an implicit instantiation, the
1411 second is an explicit specialization, and the
1412 implicit instantiation has not yet been used. That
1413 situation can occur if we have implicitly
1414 instantiated a member function and then specialized
1415 it later.
1416
1417 We can also wind up here if a friend declaration that
1418 looked like an instantiation turns out to be a
1419 specialization:
1420
1421 template <class T> void foo(T);
1422 class S { friend void foo<>(int) };
1423 template <> void foo(int);
1424
1425 We transform the existing DECL in place so that any
1426 pointers to it become pointers to the updated
1427 declaration.
1428
1429 If there was a definition for the template, but not
1430 for the specialization, we want this to look as if
1431 there were no definition, and vice versa. */
1432 DECL_INITIAL (fn) = NULL_TREE;
1433 duplicate_decls (spec, fn, is_friend);
1434 /* The call to duplicate_decls will have applied
1435 [temp.expl.spec]:
1436
1437 An explicit specialization of a function template
1438 is inline only if it is explicitly declared to be,
1439 and independently of whether its function template
1440 is.
1441
1442 to the primary function; now copy the inline bits to
1443 the various clones. */
1444 FOR_EACH_CLONE (clone, fn)
1445 {
1446 DECL_DECLARED_INLINE_P (clone)
1447 = DECL_DECLARED_INLINE_P (fn);
1448 DECL_SOURCE_LOCATION (clone)
1449 = DECL_SOURCE_LOCATION (fn);
1450 DECL_DELETED_FN (clone)
1451 = DECL_DELETED_FN (fn);
1452 }
1453 check_specialization_namespace (tmpl);
1454
1455 return fn;
1456 }
1457 }
1458 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1459 {
1460 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1461 /* Dup decl failed, but this is a new definition. Set the
1462 line number so any errors match this new
1463 definition. */
1464 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1465
1466 return fn;
1467 }
1468 }
1469 else if (fn)
1470 return duplicate_decls (spec, fn, is_friend);
1471
1472 /* A specialization must be declared in the same namespace as the
1473 template it is specializing. */
1474 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1475 && !check_specialization_namespace (tmpl))
1476 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1477
1478 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1479 {
1480 spec_entry *entry = ggc_alloc_spec_entry ();
1481 gcc_assert (tmpl && args && spec);
1482 *entry = elt;
1483 *slot = entry;
1484 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1485 && PRIMARY_TEMPLATE_P (tmpl)
1486 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1487 /* TMPL is a forward declaration of a template function; keep a list
1488 of all specializations in case we need to reassign them to a friend
1489 template later in tsubst_friend_function. */
1490 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1491 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1492 }
1493
1494 return spec;
1495 }
1496
1497 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1498 TMPL and ARGS members, ignores SPEC. */
1499
1500 int comparing_specializations;
1501
1502 static int
1503 eq_specializations (const void *p1, const void *p2)
1504 {
1505 const spec_entry *e1 = (const spec_entry *)p1;
1506 const spec_entry *e2 = (const spec_entry *)p2;
1507 int equal;
1508
1509 ++comparing_specializations;
1510 equal = (e1->tmpl == e2->tmpl
1511 && comp_template_args (e1->args, e2->args));
1512 --comparing_specializations;
1513
1514 return equal;
1515 }
1516
1517 /* Returns a hash for a template TMPL and template arguments ARGS. */
1518
1519 static hashval_t
1520 hash_tmpl_and_args (tree tmpl, tree args)
1521 {
1522 hashval_t val = DECL_UID (tmpl);
1523 return iterative_hash_template_arg (args, val);
1524 }
1525
1526 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1527 ignoring SPEC. */
1528
1529 static hashval_t
1530 hash_specialization (const void *p)
1531 {
1532 const spec_entry *e = (const spec_entry *)p;
1533 return hash_tmpl_and_args (e->tmpl, e->args);
1534 }
1535
1536 /* Recursively calculate a hash value for a template argument ARG, for use
1537 in the hash tables of template specializations. */
1538
1539 hashval_t
1540 iterative_hash_template_arg (tree arg, hashval_t val)
1541 {
1542 unsigned HOST_WIDE_INT i;
1543 enum tree_code code;
1544 char tclass;
1545
1546 if (arg == NULL_TREE)
1547 return iterative_hash_object (arg, val);
1548
1549 if (!TYPE_P (arg))
1550 STRIP_NOPS (arg);
1551
1552 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1553 /* We can get one of these when re-hashing a previous entry in the middle
1554 of substituting into a pack expansion. Just look through it. */
1555 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1556
1557 code = TREE_CODE (arg);
1558 tclass = TREE_CODE_CLASS (code);
1559
1560 val = iterative_hash_object (code, val);
1561
1562 switch (code)
1563 {
1564 case ERROR_MARK:
1565 return val;
1566
1567 case IDENTIFIER_NODE:
1568 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1569
1570 case TREE_VEC:
1571 {
1572 int i, len = TREE_VEC_LENGTH (arg);
1573 for (i = 0; i < len; ++i)
1574 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1575 return val;
1576 }
1577
1578 case TYPE_PACK_EXPANSION:
1579 case EXPR_PACK_EXPANSION:
1580 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1581 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1582
1583 case TYPE_ARGUMENT_PACK:
1584 case NONTYPE_ARGUMENT_PACK:
1585 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1586
1587 case TREE_LIST:
1588 for (; arg; arg = TREE_CHAIN (arg))
1589 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1590 return val;
1591
1592 case OVERLOAD:
1593 for (; arg; arg = OVL_NEXT (arg))
1594 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1595 return val;
1596
1597 case CONSTRUCTOR:
1598 {
1599 tree field, value;
1600 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1601 {
1602 val = iterative_hash_template_arg (field, val);
1603 val = iterative_hash_template_arg (value, val);
1604 }
1605 return val;
1606 }
1607
1608 case PARM_DECL:
1609 if (!DECL_ARTIFICIAL (arg))
1610 {
1611 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1612 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1613 }
1614 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1615
1616 case TARGET_EXPR:
1617 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1618
1619 case PTRMEM_CST:
1620 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1621 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1622
1623 case TEMPLATE_PARM_INDEX:
1624 val = iterative_hash_template_arg
1625 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1626 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1627 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1628
1629 case TRAIT_EXPR:
1630 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1631 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1632 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1633
1634 case BASELINK:
1635 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1636 val);
1637 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1638 val);
1639
1640 case MODOP_EXPR:
1641 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1642 code = TREE_CODE (TREE_OPERAND (arg, 1));
1643 val = iterative_hash_object (code, val);
1644 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1645
1646 case LAMBDA_EXPR:
1647 /* A lambda can't appear in a template arg, but don't crash on
1648 erroneous input. */
1649 gcc_assert (seen_error ());
1650 return val;
1651
1652 case CAST_EXPR:
1653 case IMPLICIT_CONV_EXPR:
1654 case STATIC_CAST_EXPR:
1655 case REINTERPRET_CAST_EXPR:
1656 case CONST_CAST_EXPR:
1657 case DYNAMIC_CAST_EXPR:
1658 case NEW_EXPR:
1659 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1660 /* Now hash operands as usual. */
1661 break;
1662
1663 default:
1664 break;
1665 }
1666
1667 switch (tclass)
1668 {
1669 case tcc_type:
1670 if (TYPE_CANONICAL (arg))
1671 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1672 val);
1673 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1674 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1675 /* Otherwise just compare the types during lookup. */
1676 return val;
1677
1678 case tcc_declaration:
1679 case tcc_constant:
1680 return iterative_hash_expr (arg, val);
1681
1682 default:
1683 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1684 {
1685 unsigned n = cp_tree_operand_length (arg);
1686 for (i = 0; i < n; ++i)
1687 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1688 return val;
1689 }
1690 }
1691 gcc_unreachable ();
1692 return 0;
1693 }
1694
1695 /* Unregister the specialization SPEC as a specialization of TMPL.
1696 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1697 if the SPEC was listed as a specialization of TMPL.
1698
1699 Note that SPEC has been ggc_freed, so we can't look inside it. */
1700
1701 bool
1702 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1703 {
1704 spec_entry *entry;
1705 spec_entry elt;
1706
1707 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1708 elt.args = TI_ARGS (tinfo);
1709 elt.spec = NULL_TREE;
1710
1711 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1712 if (entry != NULL)
1713 {
1714 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1715 gcc_assert (new_spec != NULL_TREE);
1716 entry->spec = new_spec;
1717 return 1;
1718 }
1719
1720 return 0;
1721 }
1722
1723 /* Like register_specialization, but for local declarations. We are
1724 registering SPEC, an instantiation of TMPL. */
1725
1726 static void
1727 register_local_specialization (tree spec, tree tmpl)
1728 {
1729 void **slot;
1730
1731 slot = pointer_map_insert (local_specializations, tmpl);
1732 *slot = spec;
1733 }
1734
1735 /* TYPE is a class type. Returns true if TYPE is an explicitly
1736 specialized class. */
1737
1738 bool
1739 explicit_class_specialization_p (tree type)
1740 {
1741 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1742 return false;
1743 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1744 }
1745
1746 /* Print the list of functions at FNS, going through all the overloads
1747 for each element of the list. Alternatively, FNS can not be a
1748 TREE_LIST, in which case it will be printed together with all the
1749 overloads.
1750
1751 MORE and *STR should respectively be FALSE and NULL when the function
1752 is called from the outside. They are used internally on recursive
1753 calls. print_candidates manages the two parameters and leaves NULL
1754 in *STR when it ends. */
1755
1756 static void
1757 print_candidates_1 (tree fns, bool more, const char **str)
1758 {
1759 tree fn, fn2;
1760 char *spaces = NULL;
1761
1762 for (fn = fns; fn; fn = OVL_NEXT (fn))
1763 if (TREE_CODE (fn) == TREE_LIST)
1764 {
1765 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1766 print_candidates_1 (TREE_VALUE (fn2),
1767 TREE_CHAIN (fn2) || more, str);
1768 }
1769 else
1770 {
1771 tree cand = OVL_CURRENT (fn);
1772 if (!*str)
1773 {
1774 /* Pick the prefix string. */
1775 if (!more && !OVL_NEXT (fns))
1776 {
1777 inform (DECL_SOURCE_LOCATION (cand),
1778 "candidate is: %#D", cand);
1779 continue;
1780 }
1781
1782 *str = _("candidates are:");
1783 spaces = get_spaces (*str);
1784 }
1785 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1786 *str = spaces ? spaces : *str;
1787 }
1788
1789 if (!more)
1790 {
1791 free (spaces);
1792 *str = NULL;
1793 }
1794 }
1795
1796 /* Print the list of candidate FNS in an error message. FNS can also
1797 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1798
1799 void
1800 print_candidates (tree fns)
1801 {
1802 const char *str = NULL;
1803 print_candidates_1 (fns, false, &str);
1804 gcc_assert (str == NULL);
1805 }
1806
1807 /* Returns the template (one of the functions given by TEMPLATE_ID)
1808 which can be specialized to match the indicated DECL with the
1809 explicit template args given in TEMPLATE_ID. The DECL may be
1810 NULL_TREE if none is available. In that case, the functions in
1811 TEMPLATE_ID are non-members.
1812
1813 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1814 specialization of a member template.
1815
1816 The TEMPLATE_COUNT is the number of references to qualifying
1817 template classes that appeared in the name of the function. See
1818 check_explicit_specialization for a more accurate description.
1819
1820 TSK indicates what kind of template declaration (if any) is being
1821 declared. TSK_TEMPLATE indicates that the declaration given by
1822 DECL, though a FUNCTION_DECL, has template parameters, and is
1823 therefore a template function.
1824
1825 The template args (those explicitly specified and those deduced)
1826 are output in a newly created vector *TARGS_OUT.
1827
1828 If it is impossible to determine the result, an error message is
1829 issued. The error_mark_node is returned to indicate failure. */
1830
1831 static tree
1832 determine_specialization (tree template_id,
1833 tree decl,
1834 tree* targs_out,
1835 int need_member_template,
1836 int template_count,
1837 tmpl_spec_kind tsk)
1838 {
1839 tree fns;
1840 tree targs;
1841 tree explicit_targs;
1842 tree candidates = NULL_TREE;
1843 /* A TREE_LIST of templates of which DECL may be a specialization.
1844 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1845 corresponding TREE_PURPOSE is the set of template arguments that,
1846 when used to instantiate the template, would produce a function
1847 with the signature of DECL. */
1848 tree templates = NULL_TREE;
1849 int header_count;
1850 cp_binding_level *b;
1851
1852 *targs_out = NULL_TREE;
1853
1854 if (template_id == error_mark_node || decl == error_mark_node)
1855 return error_mark_node;
1856
1857 /* We shouldn't be specializing a member template of an
1858 unspecialized class template; we already gave an error in
1859 check_specialization_scope, now avoid crashing. */
1860 if (template_count && DECL_CLASS_SCOPE_P (decl)
1861 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1862 {
1863 gcc_assert (errorcount);
1864 return error_mark_node;
1865 }
1866
1867 fns = TREE_OPERAND (template_id, 0);
1868 explicit_targs = TREE_OPERAND (template_id, 1);
1869
1870 if (fns == error_mark_node)
1871 return error_mark_node;
1872
1873 /* Check for baselinks. */
1874 if (BASELINK_P (fns))
1875 fns = BASELINK_FUNCTIONS (fns);
1876
1877 if (!is_overloaded_fn (fns))
1878 {
1879 error ("%qD is not a function template", fns);
1880 return error_mark_node;
1881 }
1882
1883 /* Count the number of template headers specified for this
1884 specialization. */
1885 header_count = 0;
1886 for (b = current_binding_level;
1887 b->kind == sk_template_parms;
1888 b = b->level_chain)
1889 ++header_count;
1890
1891 for (; fns; fns = OVL_NEXT (fns))
1892 {
1893 tree fn = OVL_CURRENT (fns);
1894
1895 if (TREE_CODE (fn) == TEMPLATE_DECL)
1896 {
1897 tree decl_arg_types;
1898 tree fn_arg_types;
1899 tree insttype;
1900
1901 /* In case of explicit specialization, we need to check if
1902 the number of template headers appearing in the specialization
1903 is correct. This is usually done in check_explicit_specialization,
1904 but the check done there cannot be exhaustive when specializing
1905 member functions. Consider the following code:
1906
1907 template <> void A<int>::f(int);
1908 template <> template <> void A<int>::f(int);
1909
1910 Assuming that A<int> is not itself an explicit specialization
1911 already, the first line specializes "f" which is a non-template
1912 member function, whilst the second line specializes "f" which
1913 is a template member function. So both lines are syntactically
1914 correct, and check_explicit_specialization does not reject
1915 them.
1916
1917 Here, we can do better, as we are matching the specialization
1918 against the declarations. We count the number of template
1919 headers, and we check if they match TEMPLATE_COUNT + 1
1920 (TEMPLATE_COUNT is the number of qualifying template classes,
1921 plus there must be another header for the member template
1922 itself).
1923
1924 Notice that if header_count is zero, this is not a
1925 specialization but rather a template instantiation, so there
1926 is no check we can perform here. */
1927 if (header_count && header_count != template_count + 1)
1928 continue;
1929
1930 /* Check that the number of template arguments at the
1931 innermost level for DECL is the same as for FN. */
1932 if (current_binding_level->kind == sk_template_parms
1933 && !current_binding_level->explicit_spec_p
1934 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1935 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1936 (current_template_parms))))
1937 continue;
1938
1939 /* DECL might be a specialization of FN. */
1940 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1941 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1942
1943 /* For a non-static member function, we need to make sure
1944 that the const qualification is the same. Since
1945 get_bindings does not try to merge the "this" parameter,
1946 we must do the comparison explicitly. */
1947 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1948 && !same_type_p (TREE_VALUE (fn_arg_types),
1949 TREE_VALUE (decl_arg_types)))
1950 continue;
1951
1952 /* Skip the "this" parameter and, for constructors of
1953 classes with virtual bases, the VTT parameter. A
1954 full specialization of a constructor will have a VTT
1955 parameter, but a template never will. */
1956 decl_arg_types
1957 = skip_artificial_parms_for (decl, decl_arg_types);
1958 fn_arg_types
1959 = skip_artificial_parms_for (fn, fn_arg_types);
1960
1961 /* Function templates cannot be specializations; there are
1962 no partial specializations of functions. Therefore, if
1963 the type of DECL does not match FN, there is no
1964 match. */
1965 if (tsk == tsk_template)
1966 {
1967 if (compparms (fn_arg_types, decl_arg_types))
1968 candidates = tree_cons (NULL_TREE, fn, candidates);
1969 continue;
1970 }
1971
1972 /* See whether this function might be a specialization of this
1973 template. Suppress access control because we might be trying
1974 to make this specialization a friend, and we have already done
1975 access control for the declaration of the specialization. */
1976 push_deferring_access_checks (dk_no_check);
1977 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1978 pop_deferring_access_checks ();
1979
1980 if (!targs)
1981 /* We cannot deduce template arguments that when used to
1982 specialize TMPL will produce DECL. */
1983 continue;
1984
1985 /* Make sure that the deduced arguments actually work. */
1986 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1987 if (insttype == error_mark_node)
1988 continue;
1989 fn_arg_types
1990 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1991 if (!compparms (fn_arg_types, decl_arg_types))
1992 continue;
1993
1994 /* Save this template, and the arguments deduced. */
1995 templates = tree_cons (targs, fn, templates);
1996 }
1997 else if (need_member_template)
1998 /* FN is an ordinary member function, and we need a
1999 specialization of a member template. */
2000 ;
2001 else if (TREE_CODE (fn) != FUNCTION_DECL)
2002 /* We can get IDENTIFIER_NODEs here in certain erroneous
2003 cases. */
2004 ;
2005 else if (!DECL_FUNCTION_MEMBER_P (fn))
2006 /* This is just an ordinary non-member function. Nothing can
2007 be a specialization of that. */
2008 ;
2009 else if (DECL_ARTIFICIAL (fn))
2010 /* Cannot specialize functions that are created implicitly. */
2011 ;
2012 else
2013 {
2014 tree decl_arg_types;
2015
2016 /* This is an ordinary member function. However, since
2017 we're here, we can assume its enclosing class is a
2018 template class. For example,
2019
2020 template <typename T> struct S { void f(); };
2021 template <> void S<int>::f() {}
2022
2023 Here, S<int>::f is a non-template, but S<int> is a
2024 template class. If FN has the same type as DECL, we
2025 might be in business. */
2026
2027 if (!DECL_TEMPLATE_INFO (fn))
2028 /* Its enclosing class is an explicit specialization
2029 of a template class. This is not a candidate. */
2030 continue;
2031
2032 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2033 TREE_TYPE (TREE_TYPE (fn))))
2034 /* The return types differ. */
2035 continue;
2036
2037 /* Adjust the type of DECL in case FN is a static member. */
2038 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2039 if (DECL_STATIC_FUNCTION_P (fn)
2040 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2041 decl_arg_types = TREE_CHAIN (decl_arg_types);
2042
2043 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2044 decl_arg_types))
2045 /* They match! */
2046 candidates = tree_cons (NULL_TREE, fn, candidates);
2047 }
2048 }
2049
2050 if (templates && TREE_CHAIN (templates))
2051 {
2052 /* We have:
2053
2054 [temp.expl.spec]
2055
2056 It is possible for a specialization with a given function
2057 signature to be instantiated from more than one function
2058 template. In such cases, explicit specification of the
2059 template arguments must be used to uniquely identify the
2060 function template specialization being specialized.
2061
2062 Note that here, there's no suggestion that we're supposed to
2063 determine which of the candidate templates is most
2064 specialized. However, we, also have:
2065
2066 [temp.func.order]
2067
2068 Partial ordering of overloaded function template
2069 declarations is used in the following contexts to select
2070 the function template to which a function template
2071 specialization refers:
2072
2073 -- when an explicit specialization refers to a function
2074 template.
2075
2076 So, we do use the partial ordering rules, at least for now.
2077 This extension can only serve to make invalid programs valid,
2078 so it's safe. And, there is strong anecdotal evidence that
2079 the committee intended the partial ordering rules to apply;
2080 the EDG front end has that behavior, and John Spicer claims
2081 that the committee simply forgot to delete the wording in
2082 [temp.expl.spec]. */
2083 tree tmpl = most_specialized_instantiation (templates);
2084 if (tmpl != error_mark_node)
2085 {
2086 templates = tmpl;
2087 TREE_CHAIN (templates) = NULL_TREE;
2088 }
2089 }
2090
2091 if (templates == NULL_TREE && candidates == NULL_TREE)
2092 {
2093 error ("template-id %qD for %q+D does not match any template "
2094 "declaration", template_id, decl);
2095 if (header_count && header_count != template_count + 1)
2096 inform (input_location, "saw %d %<template<>%>, need %d for "
2097 "specializing a member function template",
2098 header_count, template_count + 1);
2099 return error_mark_node;
2100 }
2101 else if ((templates && TREE_CHAIN (templates))
2102 || (candidates && TREE_CHAIN (candidates))
2103 || (templates && candidates))
2104 {
2105 error ("ambiguous template specialization %qD for %q+D",
2106 template_id, decl);
2107 candidates = chainon (candidates, templates);
2108 print_candidates (candidates);
2109 return error_mark_node;
2110 }
2111
2112 /* We have one, and exactly one, match. */
2113 if (candidates)
2114 {
2115 tree fn = TREE_VALUE (candidates);
2116 *targs_out = copy_node (DECL_TI_ARGS (fn));
2117 /* DECL is a re-declaration or partial instantiation of a template
2118 function. */
2119 if (TREE_CODE (fn) == TEMPLATE_DECL)
2120 return fn;
2121 /* It was a specialization of an ordinary member function in a
2122 template class. */
2123 return DECL_TI_TEMPLATE (fn);
2124 }
2125
2126 /* It was a specialization of a template. */
2127 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2128 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2129 {
2130 *targs_out = copy_node (targs);
2131 SET_TMPL_ARGS_LEVEL (*targs_out,
2132 TMPL_ARGS_DEPTH (*targs_out),
2133 TREE_PURPOSE (templates));
2134 }
2135 else
2136 *targs_out = TREE_PURPOSE (templates);
2137 return TREE_VALUE (templates);
2138 }
2139
2140 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2141 but with the default argument values filled in from those in the
2142 TMPL_TYPES. */
2143
2144 static tree
2145 copy_default_args_to_explicit_spec_1 (tree spec_types,
2146 tree tmpl_types)
2147 {
2148 tree new_spec_types;
2149
2150 if (!spec_types)
2151 return NULL_TREE;
2152
2153 if (spec_types == void_list_node)
2154 return void_list_node;
2155
2156 /* Substitute into the rest of the list. */
2157 new_spec_types =
2158 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2159 TREE_CHAIN (tmpl_types));
2160
2161 /* Add the default argument for this parameter. */
2162 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2163 TREE_VALUE (spec_types),
2164 new_spec_types);
2165 }
2166
2167 /* DECL is an explicit specialization. Replicate default arguments
2168 from the template it specializes. (That way, code like:
2169
2170 template <class T> void f(T = 3);
2171 template <> void f(double);
2172 void g () { f (); }
2173
2174 works, as required.) An alternative approach would be to look up
2175 the correct default arguments at the call-site, but this approach
2176 is consistent with how implicit instantiations are handled. */
2177
2178 static void
2179 copy_default_args_to_explicit_spec (tree decl)
2180 {
2181 tree tmpl;
2182 tree spec_types;
2183 tree tmpl_types;
2184 tree new_spec_types;
2185 tree old_type;
2186 tree new_type;
2187 tree t;
2188 tree object_type = NULL_TREE;
2189 tree in_charge = NULL_TREE;
2190 tree vtt = NULL_TREE;
2191
2192 /* See if there's anything we need to do. */
2193 tmpl = DECL_TI_TEMPLATE (decl);
2194 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2195 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2196 if (TREE_PURPOSE (t))
2197 break;
2198 if (!t)
2199 return;
2200
2201 old_type = TREE_TYPE (decl);
2202 spec_types = TYPE_ARG_TYPES (old_type);
2203
2204 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2205 {
2206 /* Remove the this pointer, but remember the object's type for
2207 CV quals. */
2208 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2209 spec_types = TREE_CHAIN (spec_types);
2210 tmpl_types = TREE_CHAIN (tmpl_types);
2211
2212 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2213 {
2214 /* DECL may contain more parameters than TMPL due to the extra
2215 in-charge parameter in constructors and destructors. */
2216 in_charge = spec_types;
2217 spec_types = TREE_CHAIN (spec_types);
2218 }
2219 if (DECL_HAS_VTT_PARM_P (decl))
2220 {
2221 vtt = spec_types;
2222 spec_types = TREE_CHAIN (spec_types);
2223 }
2224 }
2225
2226 /* Compute the merged default arguments. */
2227 new_spec_types =
2228 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2229
2230 /* Compute the new FUNCTION_TYPE. */
2231 if (object_type)
2232 {
2233 if (vtt)
2234 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2235 TREE_VALUE (vtt),
2236 new_spec_types);
2237
2238 if (in_charge)
2239 /* Put the in-charge parameter back. */
2240 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2241 TREE_VALUE (in_charge),
2242 new_spec_types);
2243
2244 new_type = build_method_type_directly (object_type,
2245 TREE_TYPE (old_type),
2246 new_spec_types);
2247 }
2248 else
2249 new_type = build_function_type (TREE_TYPE (old_type),
2250 new_spec_types);
2251 new_type = cp_build_type_attribute_variant (new_type,
2252 TYPE_ATTRIBUTES (old_type));
2253 new_type = build_exception_variant (new_type,
2254 TYPE_RAISES_EXCEPTIONS (old_type));
2255 TREE_TYPE (decl) = new_type;
2256 }
2257
2258 /* Return the number of template headers we expect to see for a definition
2259 or specialization of CTYPE or one of its non-template members. */
2260
2261 int
2262 num_template_headers_for_class (tree ctype)
2263 {
2264 int num_templates = 0;
2265
2266 while (ctype && CLASS_TYPE_P (ctype))
2267 {
2268 /* You're supposed to have one `template <...>' for every
2269 template class, but you don't need one for a full
2270 specialization. For example:
2271
2272 template <class T> struct S{};
2273 template <> struct S<int> { void f(); };
2274 void S<int>::f () {}
2275
2276 is correct; there shouldn't be a `template <>' for the
2277 definition of `S<int>::f'. */
2278 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2279 /* If CTYPE does not have template information of any
2280 kind, then it is not a template, nor is it nested
2281 within a template. */
2282 break;
2283 if (explicit_class_specialization_p (ctype))
2284 break;
2285 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2286 ++num_templates;
2287
2288 ctype = TYPE_CONTEXT (ctype);
2289 }
2290
2291 return num_templates;
2292 }
2293
2294 /* Do a simple sanity check on the template headers that precede the
2295 variable declaration DECL. */
2296
2297 void
2298 check_template_variable (tree decl)
2299 {
2300 tree ctx = CP_DECL_CONTEXT (decl);
2301 int wanted = num_template_headers_for_class (ctx);
2302 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2303 permerror (DECL_SOURCE_LOCATION (decl),
2304 "%qD is not a static data member of a class template", decl);
2305 else if (template_header_count > wanted)
2306 {
2307 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2308 "too many template headers for %D (should be %d)",
2309 decl, wanted);
2310 if (warned && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2311 inform (DECL_SOURCE_LOCATION (decl),
2312 "members of an explicitly specialized class are defined "
2313 "without a template header");
2314 }
2315 }
2316
2317 /* Check to see if the function just declared, as indicated in
2318 DECLARATOR, and in DECL, is a specialization of a function
2319 template. We may also discover that the declaration is an explicit
2320 instantiation at this point.
2321
2322 Returns DECL, or an equivalent declaration that should be used
2323 instead if all goes well. Issues an error message if something is
2324 amiss. Returns error_mark_node if the error is not easily
2325 recoverable.
2326
2327 FLAGS is a bitmask consisting of the following flags:
2328
2329 2: The function has a definition.
2330 4: The function is a friend.
2331
2332 The TEMPLATE_COUNT is the number of references to qualifying
2333 template classes that appeared in the name of the function. For
2334 example, in
2335
2336 template <class T> struct S { void f(); };
2337 void S<int>::f();
2338
2339 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2340 classes are not counted in the TEMPLATE_COUNT, so that in
2341
2342 template <class T> struct S {};
2343 template <> struct S<int> { void f(); }
2344 template <> void S<int>::f();
2345
2346 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2347 invalid; there should be no template <>.)
2348
2349 If the function is a specialization, it is marked as such via
2350 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2351 is set up correctly, and it is added to the list of specializations
2352 for that template. */
2353
2354 tree
2355 check_explicit_specialization (tree declarator,
2356 tree decl,
2357 int template_count,
2358 int flags)
2359 {
2360 int have_def = flags & 2;
2361 int is_friend = flags & 4;
2362 int specialization = 0;
2363 int explicit_instantiation = 0;
2364 int member_specialization = 0;
2365 tree ctype = DECL_CLASS_CONTEXT (decl);
2366 tree dname = DECL_NAME (decl);
2367 tmpl_spec_kind tsk;
2368
2369 if (is_friend)
2370 {
2371 if (!processing_specialization)
2372 tsk = tsk_none;
2373 else
2374 tsk = tsk_excessive_parms;
2375 }
2376 else
2377 tsk = current_tmpl_spec_kind (template_count);
2378
2379 switch (tsk)
2380 {
2381 case tsk_none:
2382 if (processing_specialization)
2383 {
2384 specialization = 1;
2385 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2386 }
2387 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2388 {
2389 if (is_friend)
2390 /* This could be something like:
2391
2392 template <class T> void f(T);
2393 class S { friend void f<>(int); } */
2394 specialization = 1;
2395 else
2396 {
2397 /* This case handles bogus declarations like template <>
2398 template <class T> void f<int>(); */
2399
2400 error ("template-id %qD in declaration of primary template",
2401 declarator);
2402 return decl;
2403 }
2404 }
2405 break;
2406
2407 case tsk_invalid_member_spec:
2408 /* The error has already been reported in
2409 check_specialization_scope. */
2410 return error_mark_node;
2411
2412 case tsk_invalid_expl_inst:
2413 error ("template parameter list used in explicit instantiation");
2414
2415 /* Fall through. */
2416
2417 case tsk_expl_inst:
2418 if (have_def)
2419 error ("definition provided for explicit instantiation");
2420
2421 explicit_instantiation = 1;
2422 break;
2423
2424 case tsk_excessive_parms:
2425 case tsk_insufficient_parms:
2426 if (tsk == tsk_excessive_parms)
2427 error ("too many template parameter lists in declaration of %qD",
2428 decl);
2429 else if (template_header_count)
2430 error("too few template parameter lists in declaration of %qD", decl);
2431 else
2432 error("explicit specialization of %qD must be introduced by "
2433 "%<template <>%>", decl);
2434
2435 /* Fall through. */
2436 case tsk_expl_spec:
2437 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2438 if (ctype)
2439 member_specialization = 1;
2440 else
2441 specialization = 1;
2442 break;
2443
2444 case tsk_template:
2445 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2446 {
2447 /* This case handles bogus declarations like template <>
2448 template <class T> void f<int>(); */
2449
2450 if (uses_template_parms (declarator))
2451 error ("function template partial specialization %qD "
2452 "is not allowed", declarator);
2453 else
2454 error ("template-id %qD in declaration of primary template",
2455 declarator);
2456 return decl;
2457 }
2458
2459 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2460 /* This is a specialization of a member template, without
2461 specialization the containing class. Something like:
2462
2463 template <class T> struct S {
2464 template <class U> void f (U);
2465 };
2466 template <> template <class U> void S<int>::f(U) {}
2467
2468 That's a specialization -- but of the entire template. */
2469 specialization = 1;
2470 break;
2471
2472 default:
2473 gcc_unreachable ();
2474 }
2475
2476 if (specialization || member_specialization)
2477 {
2478 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2479 for (; t; t = TREE_CHAIN (t))
2480 if (TREE_PURPOSE (t))
2481 {
2482 permerror (input_location,
2483 "default argument specified in explicit specialization");
2484 break;
2485 }
2486 }
2487
2488 if (specialization || member_specialization || explicit_instantiation)
2489 {
2490 tree tmpl = NULL_TREE;
2491 tree targs = NULL_TREE;
2492
2493 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2494 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2495 {
2496 tree fns;
2497
2498 gcc_assert (identifier_p (declarator));
2499 if (ctype)
2500 fns = dname;
2501 else
2502 {
2503 /* If there is no class context, the explicit instantiation
2504 must be at namespace scope. */
2505 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2506
2507 /* Find the namespace binding, using the declaration
2508 context. */
2509 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2510 false, true);
2511 if (fns == error_mark_node || !is_overloaded_fn (fns))
2512 {
2513 error ("%qD is not a template function", dname);
2514 fns = error_mark_node;
2515 }
2516 else
2517 {
2518 tree fn = OVL_CURRENT (fns);
2519 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2520 CP_DECL_CONTEXT (fn)))
2521 error ("%qD is not declared in %qD",
2522 decl, current_namespace);
2523 }
2524 }
2525
2526 declarator = lookup_template_function (fns, NULL_TREE);
2527 }
2528
2529 if (declarator == error_mark_node)
2530 return error_mark_node;
2531
2532 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2533 {
2534 if (!explicit_instantiation)
2535 /* A specialization in class scope. This is invalid,
2536 but the error will already have been flagged by
2537 check_specialization_scope. */
2538 return error_mark_node;
2539 else
2540 {
2541 /* It's not valid to write an explicit instantiation in
2542 class scope, e.g.:
2543
2544 class C { template void f(); }
2545
2546 This case is caught by the parser. However, on
2547 something like:
2548
2549 template class C { void f(); };
2550
2551 (which is invalid) we can get here. The error will be
2552 issued later. */
2553 ;
2554 }
2555
2556 return decl;
2557 }
2558 else if (ctype != NULL_TREE
2559 && (identifier_p (TREE_OPERAND (declarator, 0))))
2560 {
2561 /* Find the list of functions in ctype that have the same
2562 name as the declared function. */
2563 tree name = TREE_OPERAND (declarator, 0);
2564 tree fns = NULL_TREE;
2565 int idx;
2566
2567 if (constructor_name_p (name, ctype))
2568 {
2569 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2570
2571 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2572 : !CLASSTYPE_DESTRUCTORS (ctype))
2573 {
2574 /* From [temp.expl.spec]:
2575
2576 If such an explicit specialization for the member
2577 of a class template names an implicitly-declared
2578 special member function (clause _special_), the
2579 program is ill-formed.
2580
2581 Similar language is found in [temp.explicit]. */
2582 error ("specialization of implicitly-declared special member function");
2583 return error_mark_node;
2584 }
2585
2586 name = is_constructor ? ctor_identifier : dtor_identifier;
2587 }
2588
2589 if (!DECL_CONV_FN_P (decl))
2590 {
2591 idx = lookup_fnfields_1 (ctype, name);
2592 if (idx >= 0)
2593 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2594 }
2595 else
2596 {
2597 vec<tree, va_gc> *methods;
2598 tree ovl;
2599
2600 /* For a type-conversion operator, we cannot do a
2601 name-based lookup. We might be looking for `operator
2602 int' which will be a specialization of `operator T'.
2603 So, we find *all* the conversion operators, and then
2604 select from them. */
2605 fns = NULL_TREE;
2606
2607 methods = CLASSTYPE_METHOD_VEC (ctype);
2608 if (methods)
2609 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2610 methods->iterate (idx, &ovl);
2611 ++idx)
2612 {
2613 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2614 /* There are no more conversion functions. */
2615 break;
2616
2617 /* Glue all these conversion functions together
2618 with those we already have. */
2619 for (; ovl; ovl = OVL_NEXT (ovl))
2620 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2621 }
2622 }
2623
2624 if (fns == NULL_TREE)
2625 {
2626 error ("no member function %qD declared in %qT", name, ctype);
2627 return error_mark_node;
2628 }
2629 else
2630 TREE_OPERAND (declarator, 0) = fns;
2631 }
2632
2633 /* Figure out what exactly is being specialized at this point.
2634 Note that for an explicit instantiation, even one for a
2635 member function, we cannot tell apriori whether the
2636 instantiation is for a member template, or just a member
2637 function of a template class. Even if a member template is
2638 being instantiated, the member template arguments may be
2639 elided if they can be deduced from the rest of the
2640 declaration. */
2641 tmpl = determine_specialization (declarator, decl,
2642 &targs,
2643 member_specialization,
2644 template_count,
2645 tsk);
2646
2647 if (!tmpl || tmpl == error_mark_node)
2648 /* We couldn't figure out what this declaration was
2649 specializing. */
2650 return error_mark_node;
2651 else
2652 {
2653 tree gen_tmpl = most_general_template (tmpl);
2654
2655 if (explicit_instantiation)
2656 {
2657 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2658 is done by do_decl_instantiation later. */
2659
2660 int arg_depth = TMPL_ARGS_DEPTH (targs);
2661 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2662
2663 if (arg_depth > parm_depth)
2664 {
2665 /* If TMPL is not the most general template (for
2666 example, if TMPL is a friend template that is
2667 injected into namespace scope), then there will
2668 be too many levels of TARGS. Remove some of them
2669 here. */
2670 int i;
2671 tree new_targs;
2672
2673 new_targs = make_tree_vec (parm_depth);
2674 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2675 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2676 = TREE_VEC_ELT (targs, i);
2677 targs = new_targs;
2678 }
2679
2680 return instantiate_template (tmpl, targs, tf_error);
2681 }
2682
2683 /* If we thought that the DECL was a member function, but it
2684 turns out to be specializing a static member function,
2685 make DECL a static member function as well. */
2686 if (DECL_STATIC_FUNCTION_P (tmpl)
2687 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2688 revert_static_member_fn (decl);
2689
2690 /* If this is a specialization of a member template of a
2691 template class, we want to return the TEMPLATE_DECL, not
2692 the specialization of it. */
2693 if (tsk == tsk_template)
2694 {
2695 tree result = DECL_TEMPLATE_RESULT (tmpl);
2696 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2697 DECL_INITIAL (result) = NULL_TREE;
2698 if (have_def)
2699 {
2700 tree parm;
2701 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2702 DECL_SOURCE_LOCATION (result)
2703 = DECL_SOURCE_LOCATION (decl);
2704 /* We want to use the argument list specified in the
2705 definition, not in the original declaration. */
2706 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2707 for (parm = DECL_ARGUMENTS (result); parm;
2708 parm = DECL_CHAIN (parm))
2709 DECL_CONTEXT (parm) = result;
2710 }
2711 return register_specialization (tmpl, gen_tmpl, targs,
2712 is_friend, 0);
2713 }
2714
2715 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2716 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2717
2718 /* Inherit default function arguments from the template
2719 DECL is specializing. */
2720 copy_default_args_to_explicit_spec (decl);
2721
2722 /* This specialization has the same protection as the
2723 template it specializes. */
2724 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2725 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2726
2727 /* 7.1.1-1 [dcl.stc]
2728
2729 A storage-class-specifier shall not be specified in an
2730 explicit specialization...
2731
2732 The parser rejects these, so unless action is taken here,
2733 explicit function specializations will always appear with
2734 global linkage.
2735
2736 The action recommended by the C++ CWG in response to C++
2737 defect report 605 is to make the storage class and linkage
2738 of the explicit specialization match the templated function:
2739
2740 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2741 */
2742 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2743 {
2744 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2745 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2746
2747 /* This specialization has the same linkage and visibility as
2748 the function template it specializes. */
2749 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2750 if (! TREE_PUBLIC (decl))
2751 {
2752 DECL_INTERFACE_KNOWN (decl) = 1;
2753 DECL_NOT_REALLY_EXTERN (decl) = 1;
2754 }
2755 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2756 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2757 {
2758 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2759 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2760 }
2761 }
2762
2763 /* If DECL is a friend declaration, declared using an
2764 unqualified name, the namespace associated with DECL may
2765 have been set incorrectly. For example, in:
2766
2767 template <typename T> void f(T);
2768 namespace N {
2769 struct S { friend void f<int>(int); }
2770 }
2771
2772 we will have set the DECL_CONTEXT for the friend
2773 declaration to N, rather than to the global namespace. */
2774 if (DECL_NAMESPACE_SCOPE_P (decl))
2775 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2776
2777 if (is_friend && !have_def)
2778 /* This is not really a declaration of a specialization.
2779 It's just the name of an instantiation. But, it's not
2780 a request for an instantiation, either. */
2781 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2782
2783 /* Register this specialization so that we can find it
2784 again. */
2785 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2786
2787 /* A 'structor should already have clones. */
2788 gcc_assert (decl == error_mark_node
2789 || !(DECL_CONSTRUCTOR_P (decl)
2790 || DECL_DESTRUCTOR_P (decl))
2791 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2792 }
2793 }
2794
2795 return decl;
2796 }
2797
2798 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2799 parameters. These are represented in the same format used for
2800 DECL_TEMPLATE_PARMS. */
2801
2802 int
2803 comp_template_parms (const_tree parms1, const_tree parms2)
2804 {
2805 const_tree p1;
2806 const_tree p2;
2807
2808 if (parms1 == parms2)
2809 return 1;
2810
2811 for (p1 = parms1, p2 = parms2;
2812 p1 != NULL_TREE && p2 != NULL_TREE;
2813 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2814 {
2815 tree t1 = TREE_VALUE (p1);
2816 tree t2 = TREE_VALUE (p2);
2817 int i;
2818
2819 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2820 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2821
2822 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2823 return 0;
2824
2825 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2826 {
2827 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2828 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2829
2830 /* If either of the template parameters are invalid, assume
2831 they match for the sake of error recovery. */
2832 if (error_operand_p (parm1) || error_operand_p (parm2))
2833 return 1;
2834
2835 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2836 return 0;
2837
2838 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2839 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2840 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2841 continue;
2842 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2843 return 0;
2844 }
2845 }
2846
2847 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2848 /* One set of parameters has more parameters lists than the
2849 other. */
2850 return 0;
2851
2852 return 1;
2853 }
2854
2855 /* Determine whether PARM is a parameter pack. */
2856
2857 bool
2858 template_parameter_pack_p (const_tree parm)
2859 {
2860 /* Determine if we have a non-type template parameter pack. */
2861 if (TREE_CODE (parm) == PARM_DECL)
2862 return (DECL_TEMPLATE_PARM_P (parm)
2863 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2864 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2865 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2866
2867 /* If this is a list of template parameters, we could get a
2868 TYPE_DECL or a TEMPLATE_DECL. */
2869 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2870 parm = TREE_TYPE (parm);
2871
2872 /* Otherwise it must be a type template parameter. */
2873 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2874 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2875 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2876 }
2877
2878 /* Determine if T is a function parameter pack. */
2879
2880 bool
2881 function_parameter_pack_p (const_tree t)
2882 {
2883 if (t && TREE_CODE (t) == PARM_DECL)
2884 return DECL_PACK_P (t);
2885 return false;
2886 }
2887
2888 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2889 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2890
2891 tree
2892 get_function_template_decl (const_tree primary_func_tmpl_inst)
2893 {
2894 if (! primary_func_tmpl_inst
2895 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2896 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2897 return NULL;
2898
2899 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2900 }
2901
2902 /* Return true iff the function parameter PARAM_DECL was expanded
2903 from the function parameter pack PACK. */
2904
2905 bool
2906 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2907 {
2908 if (DECL_ARTIFICIAL (param_decl)
2909 || !function_parameter_pack_p (pack))
2910 return false;
2911
2912 /* The parameter pack and its pack arguments have the same
2913 DECL_PARM_INDEX. */
2914 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2915 }
2916
2917 /* Determine whether ARGS describes a variadic template args list,
2918 i.e., one that is terminated by a template argument pack. */
2919
2920 static bool
2921 template_args_variadic_p (tree args)
2922 {
2923 int nargs;
2924 tree last_parm;
2925
2926 if (args == NULL_TREE)
2927 return false;
2928
2929 args = INNERMOST_TEMPLATE_ARGS (args);
2930 nargs = TREE_VEC_LENGTH (args);
2931
2932 if (nargs == 0)
2933 return false;
2934
2935 last_parm = TREE_VEC_ELT (args, nargs - 1);
2936
2937 return ARGUMENT_PACK_P (last_parm);
2938 }
2939
2940 /* Generate a new name for the parameter pack name NAME (an
2941 IDENTIFIER_NODE) that incorporates its */
2942
2943 static tree
2944 make_ith_pack_parameter_name (tree name, int i)
2945 {
2946 /* Munge the name to include the parameter index. */
2947 #define NUMBUF_LEN 128
2948 char numbuf[NUMBUF_LEN];
2949 char* newname;
2950 int newname_len;
2951
2952 if (name == NULL_TREE)
2953 return name;
2954 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2955 newname_len = IDENTIFIER_LENGTH (name)
2956 + strlen (numbuf) + 2;
2957 newname = (char*)alloca (newname_len);
2958 snprintf (newname, newname_len,
2959 "%s#%i", IDENTIFIER_POINTER (name), i);
2960 return get_identifier (newname);
2961 }
2962
2963 /* Return true if T is a primary function, class or alias template
2964 instantiation. */
2965
2966 bool
2967 primary_template_instantiation_p (const_tree t)
2968 {
2969 if (!t)
2970 return false;
2971
2972 if (TREE_CODE (t) == FUNCTION_DECL)
2973 return DECL_LANG_SPECIFIC (t)
2974 && DECL_TEMPLATE_INSTANTIATION (t)
2975 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2976 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2977 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2978 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2979 else if (alias_template_specialization_p (t))
2980 return true;
2981 return false;
2982 }
2983
2984 /* Return true if PARM is a template template parameter. */
2985
2986 bool
2987 template_template_parameter_p (const_tree parm)
2988 {
2989 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2990 }
2991
2992 /* Return true iff PARM is a DECL representing a type template
2993 parameter. */
2994
2995 bool
2996 template_type_parameter_p (const_tree parm)
2997 {
2998 return (parm
2999 && (TREE_CODE (parm) == TYPE_DECL
3000 || TREE_CODE (parm) == TEMPLATE_DECL)
3001 && DECL_TEMPLATE_PARM_P (parm));
3002 }
3003
3004 /* Return the template parameters of T if T is a
3005 primary template instantiation, NULL otherwise. */
3006
3007 tree
3008 get_primary_template_innermost_parameters (const_tree t)
3009 {
3010 tree parms = NULL, template_info = NULL;
3011
3012 if ((template_info = get_template_info (t))
3013 && primary_template_instantiation_p (t))
3014 parms = INNERMOST_TEMPLATE_PARMS
3015 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3016
3017 return parms;
3018 }
3019
3020 /* Return the template parameters of the LEVELth level from the full list
3021 of template parameters PARMS. */
3022
3023 tree
3024 get_template_parms_at_level (tree parms, int level)
3025 {
3026 tree p;
3027 if (!parms
3028 || TREE_CODE (parms) != TREE_LIST
3029 || level > TMPL_PARMS_DEPTH (parms))
3030 return NULL_TREE;
3031
3032 for (p = parms; p; p = TREE_CHAIN (p))
3033 if (TMPL_PARMS_DEPTH (p) == level)
3034 return p;
3035
3036 return NULL_TREE;
3037 }
3038
3039 /* Returns the template arguments of T if T is a template instantiation,
3040 NULL otherwise. */
3041
3042 tree
3043 get_template_innermost_arguments (const_tree t)
3044 {
3045 tree args = NULL, template_info = NULL;
3046
3047 if ((template_info = get_template_info (t))
3048 && TI_ARGS (template_info))
3049 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3050
3051 return args;
3052 }
3053
3054 /* Return the argument pack elements of T if T is a template argument pack,
3055 NULL otherwise. */
3056
3057 tree
3058 get_template_argument_pack_elems (const_tree t)
3059 {
3060 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3061 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3062 return NULL;
3063
3064 return ARGUMENT_PACK_ARGS (t);
3065 }
3066
3067 /* Structure used to track the progress of find_parameter_packs_r. */
3068 struct find_parameter_pack_data
3069 {
3070 /* TREE_LIST that will contain all of the parameter packs found by
3071 the traversal. */
3072 tree* parameter_packs;
3073
3074 /* Set of AST nodes that have been visited by the traversal. */
3075 struct pointer_set_t *visited;
3076 };
3077
3078 /* Identifies all of the argument packs that occur in a template
3079 argument and appends them to the TREE_LIST inside DATA, which is a
3080 find_parameter_pack_data structure. This is a subroutine of
3081 make_pack_expansion and uses_parameter_packs. */
3082 static tree
3083 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3084 {
3085 tree t = *tp;
3086 struct find_parameter_pack_data* ppd =
3087 (struct find_parameter_pack_data*)data;
3088 bool parameter_pack_p = false;
3089
3090 /* Handle type aliases/typedefs. */
3091 if (TYPE_ALIAS_P (t))
3092 {
3093 if (TYPE_TEMPLATE_INFO (t))
3094 cp_walk_tree (&TYPE_TI_ARGS (t),
3095 &find_parameter_packs_r,
3096 ppd, ppd->visited);
3097 *walk_subtrees = 0;
3098 return NULL_TREE;
3099 }
3100
3101 /* Identify whether this is a parameter pack or not. */
3102 switch (TREE_CODE (t))
3103 {
3104 case TEMPLATE_PARM_INDEX:
3105 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3106 parameter_pack_p = true;
3107 break;
3108
3109 case TEMPLATE_TYPE_PARM:
3110 t = TYPE_MAIN_VARIANT (t);
3111 case TEMPLATE_TEMPLATE_PARM:
3112 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3113 parameter_pack_p = true;
3114 break;
3115
3116 case FIELD_DECL:
3117 case PARM_DECL:
3118 if (DECL_PACK_P (t))
3119 {
3120 /* We don't want to walk into the type of a PARM_DECL,
3121 because we don't want to see the type parameter pack. */
3122 *walk_subtrees = 0;
3123 parameter_pack_p = true;
3124 }
3125 break;
3126
3127 /* Look through a lambda capture proxy to the field pack. */
3128 case VAR_DECL:
3129 if (DECL_HAS_VALUE_EXPR_P (t))
3130 {
3131 tree v = DECL_VALUE_EXPR (t);
3132 cp_walk_tree (&v,
3133 &find_parameter_packs_r,
3134 ppd, ppd->visited);
3135 *walk_subtrees = 0;
3136 }
3137 break;
3138
3139 case BASES:
3140 parameter_pack_p = true;
3141 break;
3142 default:
3143 /* Not a parameter pack. */
3144 break;
3145 }
3146
3147 if (parameter_pack_p)
3148 {
3149 /* Add this parameter pack to the list. */
3150 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3151 }
3152
3153 if (TYPE_P (t))
3154 cp_walk_tree (&TYPE_CONTEXT (t),
3155 &find_parameter_packs_r, ppd, ppd->visited);
3156
3157 /* This switch statement will return immediately if we don't find a
3158 parameter pack. */
3159 switch (TREE_CODE (t))
3160 {
3161 case TEMPLATE_PARM_INDEX:
3162 return NULL_TREE;
3163
3164 case BOUND_TEMPLATE_TEMPLATE_PARM:
3165 /* Check the template itself. */
3166 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3167 &find_parameter_packs_r, ppd, ppd->visited);
3168 /* Check the template arguments. */
3169 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3170 ppd->visited);
3171 *walk_subtrees = 0;
3172 return NULL_TREE;
3173
3174 case TEMPLATE_TYPE_PARM:
3175 case TEMPLATE_TEMPLATE_PARM:
3176 return NULL_TREE;
3177
3178 case PARM_DECL:
3179 return NULL_TREE;
3180
3181 case RECORD_TYPE:
3182 if (TYPE_PTRMEMFUNC_P (t))
3183 return NULL_TREE;
3184 /* Fall through. */
3185
3186 case UNION_TYPE:
3187 case ENUMERAL_TYPE:
3188 if (TYPE_TEMPLATE_INFO (t))
3189 cp_walk_tree (&TYPE_TI_ARGS (t),
3190 &find_parameter_packs_r, ppd, ppd->visited);
3191
3192 *walk_subtrees = 0;
3193 return NULL_TREE;
3194
3195 case CONSTRUCTOR:
3196 case TEMPLATE_DECL:
3197 cp_walk_tree (&TREE_TYPE (t),
3198 &find_parameter_packs_r, ppd, ppd->visited);
3199 return NULL_TREE;
3200
3201 case TYPENAME_TYPE:
3202 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3203 ppd, ppd->visited);
3204 *walk_subtrees = 0;
3205 return NULL_TREE;
3206
3207 case TYPE_PACK_EXPANSION:
3208 case EXPR_PACK_EXPANSION:
3209 *walk_subtrees = 0;
3210 return NULL_TREE;
3211
3212 case INTEGER_TYPE:
3213 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3214 ppd, ppd->visited);
3215 *walk_subtrees = 0;
3216 return NULL_TREE;
3217
3218 case IDENTIFIER_NODE:
3219 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3220 ppd->visited);
3221 *walk_subtrees = 0;
3222 return NULL_TREE;
3223
3224 default:
3225 return NULL_TREE;
3226 }
3227
3228 return NULL_TREE;
3229 }
3230
3231 /* Determines if the expression or type T uses any parameter packs. */
3232 bool
3233 uses_parameter_packs (tree t)
3234 {
3235 tree parameter_packs = NULL_TREE;
3236 struct find_parameter_pack_data ppd;
3237 ppd.parameter_packs = &parameter_packs;
3238 ppd.visited = pointer_set_create ();
3239 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3240 pointer_set_destroy (ppd.visited);
3241 return parameter_packs != NULL_TREE;
3242 }
3243
3244 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3245 representation a base-class initializer into a parameter pack
3246 expansion. If all goes well, the resulting node will be an
3247 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3248 respectively. */
3249 tree
3250 make_pack_expansion (tree arg)
3251 {
3252 tree result;
3253 tree parameter_packs = NULL_TREE;
3254 bool for_types = false;
3255 struct find_parameter_pack_data ppd;
3256
3257 if (!arg || arg == error_mark_node)
3258 return arg;
3259
3260 if (TREE_CODE (arg) == TREE_LIST)
3261 {
3262 /* The only time we will see a TREE_LIST here is for a base
3263 class initializer. In this case, the TREE_PURPOSE will be a
3264 _TYPE node (representing the base class expansion we're
3265 initializing) and the TREE_VALUE will be a TREE_LIST
3266 containing the initialization arguments.
3267
3268 The resulting expansion looks somewhat different from most
3269 expansions. Rather than returning just one _EXPANSION, we
3270 return a TREE_LIST whose TREE_PURPOSE is a
3271 TYPE_PACK_EXPANSION containing the bases that will be
3272 initialized. The TREE_VALUE will be identical to the
3273 original TREE_VALUE, which is a list of arguments that will
3274 be passed to each base. We do not introduce any new pack
3275 expansion nodes into the TREE_VALUE (although it is possible
3276 that some already exist), because the TREE_PURPOSE and
3277 TREE_VALUE all need to be expanded together with the same
3278 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3279 resulting TREE_PURPOSE will mention the parameter packs in
3280 both the bases and the arguments to the bases. */
3281 tree purpose;
3282 tree value;
3283 tree parameter_packs = NULL_TREE;
3284
3285 /* Determine which parameter packs will be used by the base
3286 class expansion. */
3287 ppd.visited = pointer_set_create ();
3288 ppd.parameter_packs = &parameter_packs;
3289 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3290 &ppd, ppd.visited);
3291
3292 if (parameter_packs == NULL_TREE)
3293 {
3294 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3295 pointer_set_destroy (ppd.visited);
3296 return error_mark_node;
3297 }
3298
3299 if (TREE_VALUE (arg) != void_type_node)
3300 {
3301 /* Collect the sets of parameter packs used in each of the
3302 initialization arguments. */
3303 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3304 {
3305 /* Determine which parameter packs will be expanded in this
3306 argument. */
3307 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3308 &ppd, ppd.visited);
3309 }
3310 }
3311
3312 pointer_set_destroy (ppd.visited);
3313
3314 /* Create the pack expansion type for the base type. */
3315 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3316 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3317 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3318
3319 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3320 they will rarely be compared to anything. */
3321 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3322
3323 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3324 }
3325
3326 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3327 for_types = true;
3328
3329 /* Build the PACK_EXPANSION_* node. */
3330 result = for_types
3331 ? cxx_make_type (TYPE_PACK_EXPANSION)
3332 : make_node (EXPR_PACK_EXPANSION);
3333 SET_PACK_EXPANSION_PATTERN (result, arg);
3334 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3335 {
3336 /* Propagate type and const-expression information. */
3337 TREE_TYPE (result) = TREE_TYPE (arg);
3338 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3339 }
3340 else
3341 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3342 they will rarely be compared to anything. */
3343 SET_TYPE_STRUCTURAL_EQUALITY (result);
3344
3345 /* Determine which parameter packs will be expanded. */
3346 ppd.parameter_packs = &parameter_packs;
3347 ppd.visited = pointer_set_create ();
3348 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3349 pointer_set_destroy (ppd.visited);
3350
3351 /* Make sure we found some parameter packs. */
3352 if (parameter_packs == NULL_TREE)
3353 {
3354 if (TYPE_P (arg))
3355 error ("expansion pattern %<%T%> contains no argument packs", arg);
3356 else
3357 error ("expansion pattern %<%E%> contains no argument packs", arg);
3358 return error_mark_node;
3359 }
3360 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3361
3362 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3363
3364 return result;
3365 }
3366
3367 /* Checks T for any "bare" parameter packs, which have not yet been
3368 expanded, and issues an error if any are found. This operation can
3369 only be done on full expressions or types (e.g., an expression
3370 statement, "if" condition, etc.), because we could have expressions like:
3371
3372 foo(f(g(h(args)))...)
3373
3374 where "args" is a parameter pack. check_for_bare_parameter_packs
3375 should not be called for the subexpressions args, h(args),
3376 g(h(args)), or f(g(h(args))), because we would produce erroneous
3377 error messages.
3378
3379 Returns TRUE and emits an error if there were bare parameter packs,
3380 returns FALSE otherwise. */
3381 bool
3382 check_for_bare_parameter_packs (tree t)
3383 {
3384 tree parameter_packs = NULL_TREE;
3385 struct find_parameter_pack_data ppd;
3386
3387 if (!processing_template_decl || !t || t == error_mark_node)
3388 return false;
3389
3390 if (TREE_CODE (t) == TYPE_DECL)
3391 t = TREE_TYPE (t);
3392
3393 ppd.parameter_packs = &parameter_packs;
3394 ppd.visited = pointer_set_create ();
3395 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3396 pointer_set_destroy (ppd.visited);
3397
3398 if (parameter_packs)
3399 {
3400 error ("parameter packs not expanded with %<...%>:");
3401 while (parameter_packs)
3402 {
3403 tree pack = TREE_VALUE (parameter_packs);
3404 tree name = NULL_TREE;
3405
3406 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3407 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3408 name = TYPE_NAME (pack);
3409 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3410 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3411 else
3412 name = DECL_NAME (pack);
3413
3414 if (name)
3415 inform (input_location, " %qD", name);
3416 else
3417 inform (input_location, " <anonymous>");
3418
3419 parameter_packs = TREE_CHAIN (parameter_packs);
3420 }
3421
3422 return true;
3423 }
3424
3425 return false;
3426 }
3427
3428 /* Expand any parameter packs that occur in the template arguments in
3429 ARGS. */
3430 tree
3431 expand_template_argument_pack (tree args)
3432 {
3433 tree result_args = NULL_TREE;
3434 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3435 int num_result_args = -1;
3436 int non_default_args_count = -1;
3437
3438 /* First, determine if we need to expand anything, and the number of
3439 slots we'll need. */
3440 for (in_arg = 0; in_arg < nargs; ++in_arg)
3441 {
3442 tree arg = TREE_VEC_ELT (args, in_arg);
3443 if (arg == NULL_TREE)
3444 return args;
3445 if (ARGUMENT_PACK_P (arg))
3446 {
3447 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3448 if (num_result_args < 0)
3449 num_result_args = in_arg + num_packed;
3450 else
3451 num_result_args += num_packed;
3452 }
3453 else
3454 {
3455 if (num_result_args >= 0)
3456 num_result_args++;
3457 }
3458 }
3459
3460 /* If no expansion is necessary, we're done. */
3461 if (num_result_args < 0)
3462 return args;
3463
3464 /* Expand arguments. */
3465 result_args = make_tree_vec (num_result_args);
3466 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3467 non_default_args_count =
3468 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3469 for (in_arg = 0; in_arg < nargs; ++in_arg)
3470 {
3471 tree arg = TREE_VEC_ELT (args, in_arg);
3472 if (ARGUMENT_PACK_P (arg))
3473 {
3474 tree packed = ARGUMENT_PACK_ARGS (arg);
3475 int i, num_packed = TREE_VEC_LENGTH (packed);
3476 for (i = 0; i < num_packed; ++i, ++out_arg)
3477 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3478 if (non_default_args_count > 0)
3479 non_default_args_count += num_packed - 1;
3480 }
3481 else
3482 {
3483 TREE_VEC_ELT (result_args, out_arg) = arg;
3484 ++out_arg;
3485 }
3486 }
3487 if (non_default_args_count >= 0)
3488 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3489 return result_args;
3490 }
3491
3492 /* Checks if DECL shadows a template parameter.
3493
3494 [temp.local]: A template-parameter shall not be redeclared within its
3495 scope (including nested scopes).
3496
3497 Emits an error and returns TRUE if the DECL shadows a parameter,
3498 returns FALSE otherwise. */
3499
3500 bool
3501 check_template_shadow (tree decl)
3502 {
3503 tree olddecl;
3504
3505 /* If we're not in a template, we can't possibly shadow a template
3506 parameter. */
3507 if (!current_template_parms)
3508 return true;
3509
3510 /* Figure out what we're shadowing. */
3511 if (TREE_CODE (decl) == OVERLOAD)
3512 decl = OVL_CURRENT (decl);
3513 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3514
3515 /* If there's no previous binding for this name, we're not shadowing
3516 anything, let alone a template parameter. */
3517 if (!olddecl)
3518 return true;
3519
3520 /* If we're not shadowing a template parameter, we're done. Note
3521 that OLDDECL might be an OVERLOAD (or perhaps even an
3522 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3523 node. */
3524 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3525 return true;
3526
3527 /* We check for decl != olddecl to avoid bogus errors for using a
3528 name inside a class. We check TPFI to avoid duplicate errors for
3529 inline member templates. */
3530 if (decl == olddecl
3531 || (DECL_TEMPLATE_PARM_P (decl)
3532 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3533 return true;
3534
3535 /* Don't complain about the injected class name, as we've already
3536 complained about the class itself. */
3537 if (DECL_SELF_REFERENCE_P (decl))
3538 return false;
3539
3540 error ("declaration of %q+#D", decl);
3541 error (" shadows template parm %q+#D", olddecl);
3542 return false;
3543 }
3544
3545 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3546 ORIG_LEVEL, DECL, and TYPE. */
3547
3548 static tree
3549 build_template_parm_index (int index,
3550 int level,
3551 int orig_level,
3552 tree decl,
3553 tree type)
3554 {
3555 tree t = make_node (TEMPLATE_PARM_INDEX);
3556 TEMPLATE_PARM_IDX (t) = index;
3557 TEMPLATE_PARM_LEVEL (t) = level;
3558 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3559 TEMPLATE_PARM_DECL (t) = decl;
3560 TREE_TYPE (t) = type;
3561 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3562 TREE_READONLY (t) = TREE_READONLY (decl);
3563
3564 return t;
3565 }
3566
3567 /* Find the canonical type parameter for the given template type
3568 parameter. Returns the canonical type parameter, which may be TYPE
3569 if no such parameter existed. */
3570
3571 static tree
3572 canonical_type_parameter (tree type)
3573 {
3574 tree list;
3575 int idx = TEMPLATE_TYPE_IDX (type);
3576 if (!canonical_template_parms)
3577 vec_alloc (canonical_template_parms, idx+1);
3578
3579 while (canonical_template_parms->length () <= (unsigned)idx)
3580 vec_safe_push (canonical_template_parms, NULL_TREE);
3581
3582 list = (*canonical_template_parms)[idx];
3583 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3584 list = TREE_CHAIN (list);
3585
3586 if (list)
3587 return TREE_VALUE (list);
3588 else
3589 {
3590 (*canonical_template_parms)[idx]
3591 = tree_cons (NULL_TREE, type,
3592 (*canonical_template_parms)[idx]);
3593 return type;
3594 }
3595 }
3596
3597 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3598 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3599 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3600 new one is created. */
3601
3602 static tree
3603 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3604 tsubst_flags_t complain)
3605 {
3606 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3607 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3608 != TEMPLATE_PARM_LEVEL (index) - levels)
3609 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3610 {
3611 tree orig_decl = TEMPLATE_PARM_DECL (index);
3612 tree decl, t;
3613
3614 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3615 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3616 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3617 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3618 DECL_ARTIFICIAL (decl) = 1;
3619 SET_DECL_TEMPLATE_PARM_P (decl);
3620
3621 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3622 TEMPLATE_PARM_LEVEL (index) - levels,
3623 TEMPLATE_PARM_ORIG_LEVEL (index),
3624 decl, type);
3625 TEMPLATE_PARM_DESCENDANTS (index) = t;
3626 TEMPLATE_PARM_PARAMETER_PACK (t)
3627 = TEMPLATE_PARM_PARAMETER_PACK (index);
3628
3629 /* Template template parameters need this. */
3630 if (TREE_CODE (decl) == TEMPLATE_DECL)
3631 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3632 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3633 args, complain);
3634 }
3635
3636 return TEMPLATE_PARM_DESCENDANTS (index);
3637 }
3638
3639 /* Process information from new template parameter PARM and append it
3640 to the LIST being built. This new parameter is a non-type
3641 parameter iff IS_NON_TYPE is true. This new parameter is a
3642 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3643 is in PARM_LOC. */
3644
3645 tree
3646 process_template_parm (tree list, location_t parm_loc, tree parm,
3647 bool is_non_type, bool is_parameter_pack)
3648 {
3649 tree decl = 0;
3650 tree defval;
3651 int idx = 0;
3652
3653 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3654 defval = TREE_PURPOSE (parm);
3655
3656 if (list)
3657 {
3658 tree p = tree_last (list);
3659
3660 if (p && TREE_VALUE (p) != error_mark_node)
3661 {
3662 p = TREE_VALUE (p);
3663 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3664 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3665 else
3666 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3667 }
3668
3669 ++idx;
3670 }
3671
3672 if (is_non_type)
3673 {
3674 parm = TREE_VALUE (parm);
3675
3676 SET_DECL_TEMPLATE_PARM_P (parm);
3677
3678 if (TREE_TYPE (parm) != error_mark_node)
3679 {
3680 /* [temp.param]
3681
3682 The top-level cv-qualifiers on the template-parameter are
3683 ignored when determining its type. */
3684 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3685 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3686 TREE_TYPE (parm) = error_mark_node;
3687 else if (uses_parameter_packs (TREE_TYPE (parm))
3688 && !is_parameter_pack
3689 /* If we're in a nested template parameter list, the template
3690 template parameter could be a parameter pack. */
3691 && processing_template_parmlist == 1)
3692 {
3693 /* This template parameter is not a parameter pack, but it
3694 should be. Complain about "bare" parameter packs. */
3695 check_for_bare_parameter_packs (TREE_TYPE (parm));
3696
3697 /* Recover by calling this a parameter pack. */
3698 is_parameter_pack = true;
3699 }
3700 }
3701
3702 /* A template parameter is not modifiable. */
3703 TREE_CONSTANT (parm) = 1;
3704 TREE_READONLY (parm) = 1;
3705 decl = build_decl (parm_loc,
3706 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3707 TREE_CONSTANT (decl) = 1;
3708 TREE_READONLY (decl) = 1;
3709 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3710 = build_template_parm_index (idx, processing_template_decl,
3711 processing_template_decl,
3712 decl, TREE_TYPE (parm));
3713
3714 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3715 = is_parameter_pack;
3716 }
3717 else
3718 {
3719 tree t;
3720 parm = TREE_VALUE (TREE_VALUE (parm));
3721
3722 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3723 {
3724 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3725 /* This is for distinguishing between real templates and template
3726 template parameters */
3727 TREE_TYPE (parm) = t;
3728 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3729 decl = parm;
3730 }
3731 else
3732 {
3733 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3734 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3735 decl = build_decl (parm_loc,
3736 TYPE_DECL, parm, t);
3737 }
3738
3739 TYPE_NAME (t) = decl;
3740 TYPE_STUB_DECL (t) = decl;
3741 parm = decl;
3742 TEMPLATE_TYPE_PARM_INDEX (t)
3743 = build_template_parm_index (idx, processing_template_decl,
3744 processing_template_decl,
3745 decl, TREE_TYPE (parm));
3746 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3747 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3748 }
3749 DECL_ARTIFICIAL (decl) = 1;
3750 SET_DECL_TEMPLATE_PARM_P (decl);
3751 pushdecl (decl);
3752 parm = build_tree_list (defval, parm);
3753 return chainon (list, parm);
3754 }
3755
3756 /* The end of a template parameter list has been reached. Process the
3757 tree list into a parameter vector, converting each parameter into a more
3758 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3759 as PARM_DECLs. */
3760
3761 tree
3762 end_template_parm_list (tree parms)
3763 {
3764 int nparms;
3765 tree parm, next;
3766 tree saved_parmlist = make_tree_vec (list_length (parms));
3767
3768 current_template_parms
3769 = tree_cons (size_int (processing_template_decl),
3770 saved_parmlist, current_template_parms);
3771
3772 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3773 {
3774 next = TREE_CHAIN (parm);
3775 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3776 TREE_CHAIN (parm) = NULL_TREE;
3777 }
3778
3779 --processing_template_parmlist;
3780
3781 return saved_parmlist;
3782 }
3783
3784 /* end_template_decl is called after a template declaration is seen. */
3785
3786 void
3787 end_template_decl (void)
3788 {
3789 reset_specialization ();
3790
3791 if (! processing_template_decl)
3792 return;
3793
3794 /* This matches the pushlevel in begin_template_parm_list. */
3795 finish_scope ();
3796
3797 --processing_template_decl;
3798 current_template_parms = TREE_CHAIN (current_template_parms);
3799 }
3800
3801 /* Takes a TREE_LIST representing a template parameter and convert it
3802 into an argument suitable to be passed to the type substitution
3803 functions. Note that If the TREE_LIST contains an error_mark
3804 node, the returned argument is error_mark_node. */
3805
3806 static tree
3807 template_parm_to_arg (tree t)
3808 {
3809
3810 if (t == NULL_TREE
3811 || TREE_CODE (t) != TREE_LIST)
3812 return t;
3813
3814 if (error_operand_p (TREE_VALUE (t)))
3815 return error_mark_node;
3816
3817 t = TREE_VALUE (t);
3818
3819 if (TREE_CODE (t) == TYPE_DECL
3820 || TREE_CODE (t) == TEMPLATE_DECL)
3821 {
3822 t = TREE_TYPE (t);
3823
3824 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3825 {
3826 /* Turn this argument into a TYPE_ARGUMENT_PACK
3827 with a single element, which expands T. */
3828 tree vec = make_tree_vec (1);
3829 #ifdef ENABLE_CHECKING
3830 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3831 (vec, TREE_VEC_LENGTH (vec));
3832 #endif
3833 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3834
3835 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3836 SET_ARGUMENT_PACK_ARGS (t, vec);
3837 }
3838 }
3839 else
3840 {
3841 t = DECL_INITIAL (t);
3842
3843 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3844 {
3845 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3846 with a single element, which expands T. */
3847 tree vec = make_tree_vec (1);
3848 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3849 #ifdef ENABLE_CHECKING
3850 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3851 (vec, TREE_VEC_LENGTH (vec));
3852 #endif
3853 t = convert_from_reference (t);
3854 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3855
3856 t = make_node (NONTYPE_ARGUMENT_PACK);
3857 SET_ARGUMENT_PACK_ARGS (t, vec);
3858 TREE_TYPE (t) = type;
3859 }
3860 else
3861 t = convert_from_reference (t);
3862 }
3863 return t;
3864 }
3865
3866 /* Given a set of template parameters, return them as a set of template
3867 arguments. The template parameters are represented as a TREE_VEC, in
3868 the form documented in cp-tree.h for template arguments. */
3869
3870 static tree
3871 template_parms_to_args (tree parms)
3872 {
3873 tree header;
3874 tree args = NULL_TREE;
3875 int length = TMPL_PARMS_DEPTH (parms);
3876 int l = length;
3877
3878 /* If there is only one level of template parameters, we do not
3879 create a TREE_VEC of TREE_VECs. Instead, we return a single
3880 TREE_VEC containing the arguments. */
3881 if (length > 1)
3882 args = make_tree_vec (length);
3883
3884 for (header = parms; header; header = TREE_CHAIN (header))
3885 {
3886 tree a = copy_node (TREE_VALUE (header));
3887 int i;
3888
3889 TREE_TYPE (a) = NULL_TREE;
3890 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3891 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3892
3893 #ifdef ENABLE_CHECKING
3894 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3895 #endif
3896
3897 if (length > 1)
3898 TREE_VEC_ELT (args, --l) = a;
3899 else
3900 args = a;
3901 }
3902
3903 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3904 /* This can happen for template parms of a template template
3905 parameter, e.g:
3906
3907 template<template<class T, class U> class TT> struct S;
3908
3909 Consider the level of the parms of TT; T and U both have
3910 level 2; TT has no template parm of level 1. So in this case
3911 the first element of full_template_args is NULL_TREE. If we
3912 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3913 of 2. This will make tsubst wrongly consider that T and U
3914 have level 1. Instead, let's create a dummy vector as the
3915 first element of full_template_args so that TMPL_ARGS_DEPTH
3916 returns the correct depth for args. */
3917 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3918 return args;
3919 }
3920
3921 /* Within the declaration of a template, return the currently active
3922 template parameters as an argument TREE_VEC. */
3923
3924 static tree
3925 current_template_args (void)
3926 {
3927 return template_parms_to_args (current_template_parms);
3928 }
3929
3930 /* Update the declared TYPE by doing any lookups which were thought to be
3931 dependent, but are not now that we know the SCOPE of the declarator. */
3932
3933 tree
3934 maybe_update_decl_type (tree orig_type, tree scope)
3935 {
3936 tree type = orig_type;
3937
3938 if (type == NULL_TREE)
3939 return type;
3940
3941 if (TREE_CODE (orig_type) == TYPE_DECL)
3942 type = TREE_TYPE (type);
3943
3944 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3945 && dependent_type_p (type)
3946 /* Don't bother building up the args in this case. */
3947 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3948 {
3949 /* tsubst in the args corresponding to the template parameters,
3950 including auto if present. Most things will be unchanged, but
3951 make_typename_type and tsubst_qualified_id will resolve
3952 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3953 tree args = current_template_args ();
3954 tree auto_node = type_uses_auto (type);
3955 tree pushed;
3956 if (auto_node)
3957 {
3958 tree auto_vec = make_tree_vec (1);
3959 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3960 args = add_to_template_args (args, auto_vec);
3961 }
3962 pushed = push_scope (scope);
3963 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3964 if (pushed)
3965 pop_scope (scope);
3966 }
3967
3968 if (type == error_mark_node)
3969 return orig_type;
3970
3971 if (TREE_CODE (orig_type) == TYPE_DECL)
3972 {
3973 if (same_type_p (type, TREE_TYPE (orig_type)))
3974 type = orig_type;
3975 else
3976 type = TYPE_NAME (type);
3977 }
3978 return type;
3979 }
3980
3981 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3982 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3983 a member template. Used by push_template_decl below. */
3984
3985 static tree
3986 build_template_decl (tree decl, tree parms, bool member_template_p)
3987 {
3988 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3989 DECL_TEMPLATE_PARMS (tmpl) = parms;
3990 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3991 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3992 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3993
3994 return tmpl;
3995 }
3996
3997 struct template_parm_data
3998 {
3999 /* The level of the template parameters we are currently
4000 processing. */
4001 int level;
4002
4003 /* The index of the specialization argument we are currently
4004 processing. */
4005 int current_arg;
4006
4007 /* An array whose size is the number of template parameters. The
4008 elements are nonzero if the parameter has been used in any one
4009 of the arguments processed so far. */
4010 int* parms;
4011
4012 /* An array whose size is the number of template arguments. The
4013 elements are nonzero if the argument makes use of template
4014 parameters of this level. */
4015 int* arg_uses_template_parms;
4016 };
4017
4018 /* Subroutine of push_template_decl used to see if each template
4019 parameter in a partial specialization is used in the explicit
4020 argument list. If T is of the LEVEL given in DATA (which is
4021 treated as a template_parm_data*), then DATA->PARMS is marked
4022 appropriately. */
4023
4024 static int
4025 mark_template_parm (tree t, void* data)
4026 {
4027 int level;
4028 int idx;
4029 struct template_parm_data* tpd = (struct template_parm_data*) data;
4030
4031 template_parm_level_and_index (t, &level, &idx);
4032
4033 if (level == tpd->level)
4034 {
4035 tpd->parms[idx] = 1;
4036 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4037 }
4038
4039 /* Return zero so that for_each_template_parm will continue the
4040 traversal of the tree; we want to mark *every* template parm. */
4041 return 0;
4042 }
4043
4044 /* Process the partial specialization DECL. */
4045
4046 static tree
4047 process_partial_specialization (tree decl)
4048 {
4049 tree type = TREE_TYPE (decl);
4050 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4051 tree specargs = CLASSTYPE_TI_ARGS (type);
4052 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4053 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4054 tree inner_parms;
4055 tree inst;
4056 int nargs = TREE_VEC_LENGTH (inner_args);
4057 int ntparms;
4058 int i;
4059 bool did_error_intro = false;
4060 struct template_parm_data tpd;
4061 struct template_parm_data tpd2;
4062
4063 gcc_assert (current_template_parms);
4064
4065 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4066 ntparms = TREE_VEC_LENGTH (inner_parms);
4067
4068 /* We check that each of the template parameters given in the
4069 partial specialization is used in the argument list to the
4070 specialization. For example:
4071
4072 template <class T> struct S;
4073 template <class T> struct S<T*>;
4074
4075 The second declaration is OK because `T*' uses the template
4076 parameter T, whereas
4077
4078 template <class T> struct S<int>;
4079
4080 is no good. Even trickier is:
4081
4082 template <class T>
4083 struct S1
4084 {
4085 template <class U>
4086 struct S2;
4087 template <class U>
4088 struct S2<T>;
4089 };
4090
4091 The S2<T> declaration is actually invalid; it is a
4092 full-specialization. Of course,
4093
4094 template <class U>
4095 struct S2<T (*)(U)>;
4096
4097 or some such would have been OK. */
4098 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4099 tpd.parms = XALLOCAVEC (int, ntparms);
4100 memset (tpd.parms, 0, sizeof (int) * ntparms);
4101
4102 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4103 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4104 for (i = 0; i < nargs; ++i)
4105 {
4106 tpd.current_arg = i;
4107 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4108 &mark_template_parm,
4109 &tpd,
4110 NULL,
4111 /*include_nondeduced_p=*/false);
4112 }
4113 for (i = 0; i < ntparms; ++i)
4114 if (tpd.parms[i] == 0)
4115 {
4116 /* One of the template parms was not used in a deduced context in the
4117 specialization. */
4118 if (!did_error_intro)
4119 {
4120 error ("template parameters not deducible in "
4121 "partial specialization:");
4122 did_error_intro = true;
4123 }
4124
4125 inform (input_location, " %qD",
4126 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4127 }
4128
4129 if (did_error_intro)
4130 return error_mark_node;
4131
4132 /* [temp.class.spec]
4133
4134 The argument list of the specialization shall not be identical to
4135 the implicit argument list of the primary template. */
4136 if (comp_template_args
4137 (inner_args,
4138 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4139 (maintmpl)))))
4140 error ("partial specialization %qT does not specialize any template arguments", type);
4141
4142 /* A partial specialization that replaces multiple parameters of the
4143 primary template with a pack expansion is less specialized for those
4144 parameters. */
4145 if (nargs < DECL_NTPARMS (maintmpl))
4146 {
4147 error ("partial specialization is not more specialized than the "
4148 "primary template because it replaces multiple parameters "
4149 "with a pack expansion");
4150 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4151 return decl;
4152 }
4153
4154 /* [temp.class.spec]
4155
4156 A partially specialized non-type argument expression shall not
4157 involve template parameters of the partial specialization except
4158 when the argument expression is a simple identifier.
4159
4160 The type of a template parameter corresponding to a specialized
4161 non-type argument shall not be dependent on a parameter of the
4162 specialization.
4163
4164 Also, we verify that pack expansions only occur at the
4165 end of the argument list. */
4166 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4167 tpd2.parms = 0;
4168 for (i = 0; i < nargs; ++i)
4169 {
4170 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4171 tree arg = TREE_VEC_ELT (inner_args, i);
4172 tree packed_args = NULL_TREE;
4173 int j, len = 1;
4174
4175 if (ARGUMENT_PACK_P (arg))
4176 {
4177 /* Extract the arguments from the argument pack. We'll be
4178 iterating over these in the following loop. */
4179 packed_args = ARGUMENT_PACK_ARGS (arg);
4180 len = TREE_VEC_LENGTH (packed_args);
4181 }
4182
4183 for (j = 0; j < len; j++)
4184 {
4185 if (packed_args)
4186 /* Get the Jth argument in the parameter pack. */
4187 arg = TREE_VEC_ELT (packed_args, j);
4188
4189 if (PACK_EXPANSION_P (arg))
4190 {
4191 /* Pack expansions must come at the end of the
4192 argument list. */
4193 if ((packed_args && j < len - 1)
4194 || (!packed_args && i < nargs - 1))
4195 {
4196 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4197 error ("parameter pack argument %qE must be at the "
4198 "end of the template argument list", arg);
4199 else
4200 error ("parameter pack argument %qT must be at the "
4201 "end of the template argument list", arg);
4202 }
4203 }
4204
4205 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4206 /* We only care about the pattern. */
4207 arg = PACK_EXPANSION_PATTERN (arg);
4208
4209 if (/* These first two lines are the `non-type' bit. */
4210 !TYPE_P (arg)
4211 && TREE_CODE (arg) != TEMPLATE_DECL
4212 /* This next two lines are the `argument expression is not just a
4213 simple identifier' condition and also the `specialized
4214 non-type argument' bit. */
4215 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4216 && !(REFERENCE_REF_P (arg)
4217 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4218 {
4219 if ((!packed_args && tpd.arg_uses_template_parms[i])
4220 || (packed_args && uses_template_parms (arg)))
4221 error ("template argument %qE involves template parameter(s)",
4222 arg);
4223 else
4224 {
4225 /* Look at the corresponding template parameter,
4226 marking which template parameters its type depends
4227 upon. */
4228 tree type = TREE_TYPE (parm);
4229
4230 if (!tpd2.parms)
4231 {
4232 /* We haven't yet initialized TPD2. Do so now. */
4233 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4234 /* The number of parameters here is the number in the
4235 main template, which, as checked in the assertion
4236 above, is NARGS. */
4237 tpd2.parms = XALLOCAVEC (int, nargs);
4238 tpd2.level =
4239 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4240 }
4241
4242 /* Mark the template parameters. But this time, we're
4243 looking for the template parameters of the main
4244 template, not in the specialization. */
4245 tpd2.current_arg = i;
4246 tpd2.arg_uses_template_parms[i] = 0;
4247 memset (tpd2.parms, 0, sizeof (int) * nargs);
4248 for_each_template_parm (type,
4249 &mark_template_parm,
4250 &tpd2,
4251 NULL,
4252 /*include_nondeduced_p=*/false);
4253
4254 if (tpd2.arg_uses_template_parms [i])
4255 {
4256 /* The type depended on some template parameters.
4257 If they are fully specialized in the
4258 specialization, that's OK. */
4259 int j;
4260 int count = 0;
4261 for (j = 0; j < nargs; ++j)
4262 if (tpd2.parms[j] != 0
4263 && tpd.arg_uses_template_parms [j])
4264 ++count;
4265 if (count != 0)
4266 error_n (input_location, count,
4267 "type %qT of template argument %qE depends "
4268 "on a template parameter",
4269 "type %qT of template argument %qE depends "
4270 "on template parameters",
4271 type,
4272 arg);
4273 }
4274 }
4275 }
4276 }
4277 }
4278
4279 /* We should only get here once. */
4280 gcc_assert (!COMPLETE_TYPE_P (type));
4281
4282 tree tmpl = build_template_decl (decl, current_template_parms,
4283 DECL_MEMBER_TEMPLATE_P (maintmpl));
4284 TREE_TYPE (tmpl) = type;
4285 DECL_TEMPLATE_RESULT (tmpl) = decl;
4286 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4287 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4288 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4289
4290 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4291 = tree_cons (specargs, tmpl,
4292 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4293 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4294
4295 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4296 inst = TREE_CHAIN (inst))
4297 {
4298 tree inst_type = TREE_VALUE (inst);
4299 if (COMPLETE_TYPE_P (inst_type)
4300 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4301 {
4302 tree spec = most_specialized_class (inst_type, tf_none);
4303 if (spec && TREE_TYPE (spec) == type)
4304 permerror (input_location,
4305 "partial specialization of %qT after instantiation "
4306 "of %qT", type, inst_type);
4307 }
4308 }
4309
4310 return decl;
4311 }
4312
4313 /* PARM is a template parameter of some form; return the corresponding
4314 TEMPLATE_PARM_INDEX. */
4315
4316 static tree
4317 get_template_parm_index (tree parm)
4318 {
4319 if (TREE_CODE (parm) == PARM_DECL
4320 || TREE_CODE (parm) == CONST_DECL)
4321 parm = DECL_INITIAL (parm);
4322 else if (TREE_CODE (parm) == TYPE_DECL
4323 || TREE_CODE (parm) == TEMPLATE_DECL)
4324 parm = TREE_TYPE (parm);
4325 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4326 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4327 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4328 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4329 return parm;
4330 }
4331
4332 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4333 parameter packs used by the template parameter PARM. */
4334
4335 static void
4336 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4337 {
4338 /* A type parm can't refer to another parm. */
4339 if (TREE_CODE (parm) == TYPE_DECL)
4340 return;
4341 else if (TREE_CODE (parm) == PARM_DECL)
4342 {
4343 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4344 ppd, ppd->visited);
4345 return;
4346 }
4347
4348 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4349
4350 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4351 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4352 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4353 }
4354
4355 /* PARM is a template parameter pack. Return any parameter packs used in
4356 its type or the type of any of its template parameters. If there are
4357 any such packs, it will be instantiated into a fixed template parameter
4358 list by partial instantiation rather than be fully deduced. */
4359
4360 tree
4361 fixed_parameter_pack_p (tree parm)
4362 {
4363 /* This can only be true in a member template. */
4364 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4365 return NULL_TREE;
4366 /* This can only be true for a parameter pack. */
4367 if (!template_parameter_pack_p (parm))
4368 return NULL_TREE;
4369 /* A type parm can't refer to another parm. */
4370 if (TREE_CODE (parm) == TYPE_DECL)
4371 return NULL_TREE;
4372
4373 tree parameter_packs = NULL_TREE;
4374 struct find_parameter_pack_data ppd;
4375 ppd.parameter_packs = &parameter_packs;
4376 ppd.visited = pointer_set_create ();
4377
4378 fixed_parameter_pack_p_1 (parm, &ppd);
4379
4380 pointer_set_destroy (ppd.visited);
4381 return parameter_packs;
4382 }
4383
4384 /* Check that a template declaration's use of default arguments and
4385 parameter packs is not invalid. Here, PARMS are the template
4386 parameters. IS_PRIMARY is true if DECL is the thing declared by
4387 a primary template. IS_PARTIAL is true if DECL is a partial
4388 specialization.
4389
4390 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4391 declaration (but not a definition); 1 indicates a declaration, 2
4392 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4393 emitted for extraneous default arguments.
4394
4395 Returns TRUE if there were no errors found, FALSE otherwise. */
4396
4397 bool
4398 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4399 bool is_partial, int is_friend_decl)
4400 {
4401 const char *msg;
4402 int last_level_to_check;
4403 tree parm_level;
4404 bool no_errors = true;
4405
4406 /* [temp.param]
4407
4408 A default template-argument shall not be specified in a
4409 function template declaration or a function template definition, nor
4410 in the template-parameter-list of the definition of a member of a
4411 class template. */
4412
4413 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4414 /* You can't have a function template declaration in a local
4415 scope, nor you can you define a member of a class template in a
4416 local scope. */
4417 return true;
4418
4419 if (TREE_CODE (decl) == TYPE_DECL
4420 && TREE_TYPE (decl)
4421 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4422 /* A lambda doesn't have an explicit declaration; don't complain
4423 about the parms of the enclosing class. */
4424 return true;
4425
4426 if (current_class_type
4427 && !TYPE_BEING_DEFINED (current_class_type)
4428 && DECL_LANG_SPECIFIC (decl)
4429 && DECL_DECLARES_FUNCTION_P (decl)
4430 /* If this is either a friend defined in the scope of the class
4431 or a member function. */
4432 && (DECL_FUNCTION_MEMBER_P (decl)
4433 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4434 : DECL_FRIEND_CONTEXT (decl)
4435 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4436 : false)
4437 /* And, if it was a member function, it really was defined in
4438 the scope of the class. */
4439 && (!DECL_FUNCTION_MEMBER_P (decl)
4440 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4441 /* We already checked these parameters when the template was
4442 declared, so there's no need to do it again now. This function
4443 was defined in class scope, but we're processing its body now
4444 that the class is complete. */
4445 return true;
4446
4447 /* Core issue 226 (C++0x only): the following only applies to class
4448 templates. */
4449 if (is_primary
4450 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4451 {
4452 /* [temp.param]
4453
4454 If a template-parameter has a default template-argument, all
4455 subsequent template-parameters shall have a default
4456 template-argument supplied. */
4457 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4458 {
4459 tree inner_parms = TREE_VALUE (parm_level);
4460 int ntparms = TREE_VEC_LENGTH (inner_parms);
4461 int seen_def_arg_p = 0;
4462 int i;
4463
4464 for (i = 0; i < ntparms; ++i)
4465 {
4466 tree parm = TREE_VEC_ELT (inner_parms, i);
4467
4468 if (parm == error_mark_node)
4469 continue;
4470
4471 if (TREE_PURPOSE (parm))
4472 seen_def_arg_p = 1;
4473 else if (seen_def_arg_p
4474 && !template_parameter_pack_p (TREE_VALUE (parm)))
4475 {
4476 error ("no default argument for %qD", TREE_VALUE (parm));
4477 /* For better subsequent error-recovery, we indicate that
4478 there should have been a default argument. */
4479 TREE_PURPOSE (parm) = error_mark_node;
4480 no_errors = false;
4481 }
4482 else if (!is_partial
4483 && !is_friend_decl
4484 /* Don't complain about an enclosing partial
4485 specialization. */
4486 && parm_level == parms
4487 && TREE_CODE (decl) == TYPE_DECL
4488 && i < ntparms - 1
4489 && template_parameter_pack_p (TREE_VALUE (parm))
4490 /* A fixed parameter pack will be partially
4491 instantiated into a fixed length list. */
4492 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4493 {
4494 /* A primary class template can only have one
4495 parameter pack, at the end of the template
4496 parameter list. */
4497
4498 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4499 error ("parameter pack %qE must be at the end of the"
4500 " template parameter list", TREE_VALUE (parm));
4501 else
4502 error ("parameter pack %qT must be at the end of the"
4503 " template parameter list",
4504 TREE_TYPE (TREE_VALUE (parm)));
4505
4506 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4507 = error_mark_node;
4508 no_errors = false;
4509 }
4510 }
4511 }
4512 }
4513
4514 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4515 || is_partial
4516 || !is_primary
4517 || is_friend_decl)
4518 /* For an ordinary class template, default template arguments are
4519 allowed at the innermost level, e.g.:
4520 template <class T = int>
4521 struct S {};
4522 but, in a partial specialization, they're not allowed even
4523 there, as we have in [temp.class.spec]:
4524
4525 The template parameter list of a specialization shall not
4526 contain default template argument values.
4527
4528 So, for a partial specialization, or for a function template
4529 (in C++98/C++03), we look at all of them. */
4530 ;
4531 else
4532 /* But, for a primary class template that is not a partial
4533 specialization we look at all template parameters except the
4534 innermost ones. */
4535 parms = TREE_CHAIN (parms);
4536
4537 /* Figure out what error message to issue. */
4538 if (is_friend_decl == 2)
4539 msg = G_("default template arguments may not be used in function template "
4540 "friend re-declaration");
4541 else if (is_friend_decl)
4542 msg = G_("default template arguments may not be used in function template "
4543 "friend declarations");
4544 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4545 msg = G_("default template arguments may not be used in function templates "
4546 "without -std=c++11 or -std=gnu++11");
4547 else if (is_partial)
4548 msg = G_("default template arguments may not be used in "
4549 "partial specializations");
4550 else
4551 msg = G_("default argument for template parameter for class enclosing %qD");
4552
4553 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4554 /* If we're inside a class definition, there's no need to
4555 examine the parameters to the class itself. On the one
4556 hand, they will be checked when the class is defined, and,
4557 on the other, default arguments are valid in things like:
4558 template <class T = double>
4559 struct S { template <class U> void f(U); };
4560 Here the default argument for `S' has no bearing on the
4561 declaration of `f'. */
4562 last_level_to_check = template_class_depth (current_class_type) + 1;
4563 else
4564 /* Check everything. */
4565 last_level_to_check = 0;
4566
4567 for (parm_level = parms;
4568 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4569 parm_level = TREE_CHAIN (parm_level))
4570 {
4571 tree inner_parms = TREE_VALUE (parm_level);
4572 int i;
4573 int ntparms;
4574
4575 ntparms = TREE_VEC_LENGTH (inner_parms);
4576 for (i = 0; i < ntparms; ++i)
4577 {
4578 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4579 continue;
4580
4581 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4582 {
4583 if (msg)
4584 {
4585 no_errors = false;
4586 if (is_friend_decl == 2)
4587 return no_errors;
4588
4589 error (msg, decl);
4590 msg = 0;
4591 }
4592
4593 /* Clear out the default argument so that we are not
4594 confused later. */
4595 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4596 }
4597 }
4598
4599 /* At this point, if we're still interested in issuing messages,
4600 they must apply to classes surrounding the object declared. */
4601 if (msg)
4602 msg = G_("default argument for template parameter for class "
4603 "enclosing %qD");
4604 }
4605
4606 return no_errors;
4607 }
4608
4609 /* Worker for push_template_decl_real, called via
4610 for_each_template_parm. DATA is really an int, indicating the
4611 level of the parameters we are interested in. If T is a template
4612 parameter of that level, return nonzero. */
4613
4614 static int
4615 template_parm_this_level_p (tree t, void* data)
4616 {
4617 int this_level = *(int *)data;
4618 int level;
4619
4620 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4621 level = TEMPLATE_PARM_LEVEL (t);
4622 else
4623 level = TEMPLATE_TYPE_LEVEL (t);
4624 return level == this_level;
4625 }
4626
4627 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4628 parameters given by current_template_args, or reuses a
4629 previously existing one, if appropriate. Returns the DECL, or an
4630 equivalent one, if it is replaced via a call to duplicate_decls.
4631
4632 If IS_FRIEND is true, DECL is a friend declaration. */
4633
4634 tree
4635 push_template_decl_real (tree decl, bool is_friend)
4636 {
4637 tree tmpl;
4638 tree args;
4639 tree info;
4640 tree ctx;
4641 bool is_primary;
4642 bool is_partial;
4643 int new_template_p = 0;
4644 /* True if the template is a member template, in the sense of
4645 [temp.mem]. */
4646 bool member_template_p = false;
4647
4648 if (decl == error_mark_node || !current_template_parms)
4649 return error_mark_node;
4650
4651 /* See if this is a partial specialization. */
4652 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4653 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4654 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4655
4656 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4657 is_friend = true;
4658
4659 if (is_friend)
4660 /* For a friend, we want the context of the friend function, not
4661 the type of which it is a friend. */
4662 ctx = CP_DECL_CONTEXT (decl);
4663 else if (CP_DECL_CONTEXT (decl)
4664 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4665 /* In the case of a virtual function, we want the class in which
4666 it is defined. */
4667 ctx = CP_DECL_CONTEXT (decl);
4668 else
4669 /* Otherwise, if we're currently defining some class, the DECL
4670 is assumed to be a member of the class. */
4671 ctx = current_scope ();
4672
4673 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4674 ctx = NULL_TREE;
4675
4676 if (!DECL_CONTEXT (decl))
4677 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4678
4679 /* See if this is a primary template. */
4680 if (is_friend && ctx
4681 && uses_template_parms_level (ctx, processing_template_decl))
4682 /* A friend template that specifies a class context, i.e.
4683 template <typename T> friend void A<T>::f();
4684 is not primary. */
4685 is_primary = false;
4686 else
4687 is_primary = template_parm_scope_p ();
4688
4689 if (is_primary)
4690 {
4691 if (DECL_CLASS_SCOPE_P (decl))
4692 member_template_p = true;
4693 if (TREE_CODE (decl) == TYPE_DECL
4694 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4695 {
4696 error ("template class without a name");
4697 return error_mark_node;
4698 }
4699 else if (TREE_CODE (decl) == FUNCTION_DECL)
4700 {
4701 if (DECL_DESTRUCTOR_P (decl))
4702 {
4703 /* [temp.mem]
4704
4705 A destructor shall not be a member template. */
4706 error ("destructor %qD declared as member template", decl);
4707 return error_mark_node;
4708 }
4709 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4710 && (!prototype_p (TREE_TYPE (decl))
4711 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4712 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4713 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4714 == void_list_node)))
4715 {
4716 /* [basic.stc.dynamic.allocation]
4717
4718 An allocation function can be a function
4719 template. ... Template allocation functions shall
4720 have two or more parameters. */
4721 error ("invalid template declaration of %qD", decl);
4722 return error_mark_node;
4723 }
4724 }
4725 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4726 && CLASS_TYPE_P (TREE_TYPE (decl)))
4727 /* OK */;
4728 else if (TREE_CODE (decl) == TYPE_DECL
4729 && TYPE_DECL_ALIAS_P (decl))
4730 /* alias-declaration */
4731 gcc_assert (!DECL_ARTIFICIAL (decl));
4732 else
4733 {
4734 error ("template declaration of %q#D", decl);
4735 return error_mark_node;
4736 }
4737 }
4738
4739 /* Check to see that the rules regarding the use of default
4740 arguments are not being violated. */
4741 check_default_tmpl_args (decl, current_template_parms,
4742 is_primary, is_partial, /*is_friend_decl=*/0);
4743
4744 /* Ensure that there are no parameter packs in the type of this
4745 declaration that have not been expanded. */
4746 if (TREE_CODE (decl) == FUNCTION_DECL)
4747 {
4748 /* Check each of the arguments individually to see if there are
4749 any bare parameter packs. */
4750 tree type = TREE_TYPE (decl);
4751 tree arg = DECL_ARGUMENTS (decl);
4752 tree argtype = TYPE_ARG_TYPES (type);
4753
4754 while (arg && argtype)
4755 {
4756 if (!DECL_PACK_P (arg)
4757 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4758 {
4759 /* This is a PARM_DECL that contains unexpanded parameter
4760 packs. We have already complained about this in the
4761 check_for_bare_parameter_packs call, so just replace
4762 these types with ERROR_MARK_NODE. */
4763 TREE_TYPE (arg) = error_mark_node;
4764 TREE_VALUE (argtype) = error_mark_node;
4765 }
4766
4767 arg = DECL_CHAIN (arg);
4768 argtype = TREE_CHAIN (argtype);
4769 }
4770
4771 /* Check for bare parameter packs in the return type and the
4772 exception specifiers. */
4773 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4774 /* Errors were already issued, set return type to int
4775 as the frontend doesn't expect error_mark_node as
4776 the return type. */
4777 TREE_TYPE (type) = integer_type_node;
4778 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4779 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4780 }
4781 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4782 && TYPE_DECL_ALIAS_P (decl))
4783 ? DECL_ORIGINAL_TYPE (decl)
4784 : TREE_TYPE (decl)))
4785 {
4786 TREE_TYPE (decl) = error_mark_node;
4787 return error_mark_node;
4788 }
4789
4790 if (is_partial)
4791 return process_partial_specialization (decl);
4792
4793 args = current_template_args ();
4794
4795 if (!ctx
4796 || TREE_CODE (ctx) == FUNCTION_DECL
4797 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4798 || (TREE_CODE (decl) == TYPE_DECL
4799 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4800 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4801 {
4802 if (DECL_LANG_SPECIFIC (decl)
4803 && DECL_TEMPLATE_INFO (decl)
4804 && DECL_TI_TEMPLATE (decl))
4805 tmpl = DECL_TI_TEMPLATE (decl);
4806 /* If DECL is a TYPE_DECL for a class-template, then there won't
4807 be DECL_LANG_SPECIFIC. The information equivalent to
4808 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4809 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4810 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4811 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4812 {
4813 /* Since a template declaration already existed for this
4814 class-type, we must be redeclaring it here. Make sure
4815 that the redeclaration is valid. */
4816 redeclare_class_template (TREE_TYPE (decl),
4817 current_template_parms);
4818 /* We don't need to create a new TEMPLATE_DECL; just use the
4819 one we already had. */
4820 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4821 }
4822 else
4823 {
4824 tmpl = build_template_decl (decl, current_template_parms,
4825 member_template_p);
4826 new_template_p = 1;
4827
4828 if (DECL_LANG_SPECIFIC (decl)
4829 && DECL_TEMPLATE_SPECIALIZATION (decl))
4830 {
4831 /* A specialization of a member template of a template
4832 class. */
4833 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4834 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4835 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4836 }
4837 }
4838 }
4839 else
4840 {
4841 tree a, t, current, parms;
4842 int i;
4843 tree tinfo = get_template_info (decl);
4844
4845 if (!tinfo)
4846 {
4847 error ("template definition of non-template %q#D", decl);
4848 return error_mark_node;
4849 }
4850
4851 tmpl = TI_TEMPLATE (tinfo);
4852
4853 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4854 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4855 && DECL_TEMPLATE_SPECIALIZATION (decl)
4856 && DECL_MEMBER_TEMPLATE_P (tmpl))
4857 {
4858 tree new_tmpl;
4859
4860 /* The declaration is a specialization of a member
4861 template, declared outside the class. Therefore, the
4862 innermost template arguments will be NULL, so we
4863 replace them with the arguments determined by the
4864 earlier call to check_explicit_specialization. */
4865 args = DECL_TI_ARGS (decl);
4866
4867 new_tmpl
4868 = build_template_decl (decl, current_template_parms,
4869 member_template_p);
4870 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4871 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4872 DECL_TI_TEMPLATE (decl) = new_tmpl;
4873 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4874 DECL_TEMPLATE_INFO (new_tmpl)
4875 = build_template_info (tmpl, args);
4876
4877 register_specialization (new_tmpl,
4878 most_general_template (tmpl),
4879 args,
4880 is_friend, 0);
4881 return decl;
4882 }
4883
4884 /* Make sure the template headers we got make sense. */
4885
4886 parms = DECL_TEMPLATE_PARMS (tmpl);
4887 i = TMPL_PARMS_DEPTH (parms);
4888 if (TMPL_ARGS_DEPTH (args) != i)
4889 {
4890 error ("expected %d levels of template parms for %q#D, got %d",
4891 i, decl, TMPL_ARGS_DEPTH (args));
4892 DECL_INTERFACE_KNOWN (decl) = 1;
4893 return error_mark_node;
4894 }
4895 else
4896 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4897 {
4898 a = TMPL_ARGS_LEVEL (args, i);
4899 t = INNERMOST_TEMPLATE_PARMS (parms);
4900
4901 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4902 {
4903 if (current == decl)
4904 error ("got %d template parameters for %q#D",
4905 TREE_VEC_LENGTH (a), decl);
4906 else
4907 error ("got %d template parameters for %q#T",
4908 TREE_VEC_LENGTH (a), current);
4909 error (" but %d required", TREE_VEC_LENGTH (t));
4910 /* Avoid crash in import_export_decl. */
4911 DECL_INTERFACE_KNOWN (decl) = 1;
4912 return error_mark_node;
4913 }
4914
4915 if (current == decl)
4916 current = ctx;
4917 else if (current == NULL_TREE)
4918 /* Can happen in erroneous input. */
4919 break;
4920 else
4921 current = get_containing_scope (current);
4922 }
4923
4924 /* Check that the parms are used in the appropriate qualifying scopes
4925 in the declarator. */
4926 if (!comp_template_args
4927 (TI_ARGS (tinfo),
4928 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4929 {
4930 error ("\
4931 template arguments to %qD do not match original template %qD",
4932 decl, DECL_TEMPLATE_RESULT (tmpl));
4933 if (!uses_template_parms (TI_ARGS (tinfo)))
4934 inform (input_location, "use template<> for an explicit specialization");
4935 /* Avoid crash in import_export_decl. */
4936 DECL_INTERFACE_KNOWN (decl) = 1;
4937 return error_mark_node;
4938 }
4939 }
4940
4941 DECL_TEMPLATE_RESULT (tmpl) = decl;
4942 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4943
4944 /* Push template declarations for global functions and types. Note
4945 that we do not try to push a global template friend declared in a
4946 template class; such a thing may well depend on the template
4947 parameters of the class. */
4948 if (new_template_p && !ctx
4949 && !(is_friend && template_class_depth (current_class_type) > 0))
4950 {
4951 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4952 if (tmpl == error_mark_node)
4953 return error_mark_node;
4954
4955 /* Hide template friend classes that haven't been declared yet. */
4956 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4957 {
4958 DECL_ANTICIPATED (tmpl) = 1;
4959 DECL_FRIEND_P (tmpl) = 1;
4960 }
4961 }
4962
4963 if (is_primary)
4964 {
4965 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4966 int i;
4967
4968 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4969 if (DECL_CONV_FN_P (tmpl))
4970 {
4971 int depth = TMPL_PARMS_DEPTH (parms);
4972
4973 /* It is a conversion operator. See if the type converted to
4974 depends on innermost template operands. */
4975
4976 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4977 depth))
4978 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4979 }
4980
4981 /* Give template template parms a DECL_CONTEXT of the template
4982 for which they are a parameter. */
4983 parms = INNERMOST_TEMPLATE_PARMS (parms);
4984 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4985 {
4986 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4987 if (TREE_CODE (parm) == TEMPLATE_DECL)
4988 DECL_CONTEXT (parm) = tmpl;
4989 }
4990 }
4991
4992 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4993 back to its most general template. If TMPL is a specialization,
4994 ARGS may only have the innermost set of arguments. Add the missing
4995 argument levels if necessary. */
4996 if (DECL_TEMPLATE_INFO (tmpl))
4997 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4998
4999 info = build_template_info (tmpl, args);
5000
5001 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5002 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5003 else
5004 {
5005 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5006 retrofit_lang_decl (decl);
5007 if (DECL_LANG_SPECIFIC (decl))
5008 DECL_TEMPLATE_INFO (decl) = info;
5009 }
5010
5011 return DECL_TEMPLATE_RESULT (tmpl);
5012 }
5013
5014 tree
5015 push_template_decl (tree decl)
5016 {
5017 return push_template_decl_real (decl, false);
5018 }
5019
5020 /* FN is an inheriting constructor that inherits from the constructor
5021 template INHERITED; turn FN into a constructor template with a matching
5022 template header. */
5023
5024 tree
5025 add_inherited_template_parms (tree fn, tree inherited)
5026 {
5027 tree inner_parms
5028 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5029 inner_parms = copy_node (inner_parms);
5030 tree parms
5031 = tree_cons (size_int (processing_template_decl + 1),
5032 inner_parms, current_template_parms);
5033 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5034 tree args = template_parms_to_args (parms);
5035 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5036 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5037 DECL_TEMPLATE_RESULT (tmpl) = fn;
5038 DECL_ARTIFICIAL (tmpl) = true;
5039 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5040 return tmpl;
5041 }
5042
5043 /* Called when a class template TYPE is redeclared with the indicated
5044 template PARMS, e.g.:
5045
5046 template <class T> struct S;
5047 template <class T> struct S {}; */
5048
5049 bool
5050 redeclare_class_template (tree type, tree parms)
5051 {
5052 tree tmpl;
5053 tree tmpl_parms;
5054 int i;
5055
5056 if (!TYPE_TEMPLATE_INFO (type))
5057 {
5058 error ("%qT is not a template type", type);
5059 return false;
5060 }
5061
5062 tmpl = TYPE_TI_TEMPLATE (type);
5063 if (!PRIMARY_TEMPLATE_P (tmpl))
5064 /* The type is nested in some template class. Nothing to worry
5065 about here; there are no new template parameters for the nested
5066 type. */
5067 return true;
5068
5069 if (!parms)
5070 {
5071 error ("template specifiers not specified in declaration of %qD",
5072 tmpl);
5073 return false;
5074 }
5075
5076 parms = INNERMOST_TEMPLATE_PARMS (parms);
5077 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5078
5079 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5080 {
5081 error_n (input_location, TREE_VEC_LENGTH (parms),
5082 "redeclared with %d template parameter",
5083 "redeclared with %d template parameters",
5084 TREE_VEC_LENGTH (parms));
5085 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5086 "previous declaration %q+D used %d template parameter",
5087 "previous declaration %q+D used %d template parameters",
5088 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5089 return false;
5090 }
5091
5092 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5093 {
5094 tree tmpl_parm;
5095 tree parm;
5096 tree tmpl_default;
5097 tree parm_default;
5098
5099 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5100 || TREE_VEC_ELT (parms, i) == error_mark_node)
5101 continue;
5102
5103 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5104 if (error_operand_p (tmpl_parm))
5105 return false;
5106
5107 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5108 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5109 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5110
5111 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5112 TEMPLATE_DECL. */
5113 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5114 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5115 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5116 || (TREE_CODE (tmpl_parm) != PARM_DECL
5117 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5118 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5119 || (TREE_CODE (tmpl_parm) == PARM_DECL
5120 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5121 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5122 {
5123 error ("template parameter %q+#D", tmpl_parm);
5124 error ("redeclared here as %q#D", parm);
5125 return false;
5126 }
5127
5128 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5129 {
5130 /* We have in [temp.param]:
5131
5132 A template-parameter may not be given default arguments
5133 by two different declarations in the same scope. */
5134 error_at (input_location, "redefinition of default argument for %q#D", parm);
5135 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5136 "original definition appeared here");
5137 return false;
5138 }
5139
5140 if (parm_default != NULL_TREE)
5141 /* Update the previous template parameters (which are the ones
5142 that will really count) with the new default value. */
5143 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5144 else if (tmpl_default != NULL_TREE)
5145 /* Update the new parameters, too; they'll be used as the
5146 parameters for any members. */
5147 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5148 }
5149
5150 return true;
5151 }
5152
5153 /* Simplify EXPR if it is a non-dependent expression. Returns the
5154 (possibly simplified) expression. */
5155
5156 tree
5157 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5158 {
5159 if (expr == NULL_TREE)
5160 return NULL_TREE;
5161
5162 /* If we're in a template, but EXPR isn't value dependent, simplify
5163 it. We're supposed to treat:
5164
5165 template <typename T> void f(T[1 + 1]);
5166 template <typename T> void f(T[2]);
5167
5168 as two declarations of the same function, for example. */
5169 if (processing_template_decl
5170 && !instantiation_dependent_expression_p (expr)
5171 && potential_constant_expression (expr))
5172 {
5173 HOST_WIDE_INT saved_processing_template_decl;
5174
5175 saved_processing_template_decl = processing_template_decl;
5176 processing_template_decl = 0;
5177 expr = tsubst_copy_and_build (expr,
5178 /*args=*/NULL_TREE,
5179 complain,
5180 /*in_decl=*/NULL_TREE,
5181 /*function_p=*/false,
5182 /*integral_constant_expression_p=*/true);
5183 processing_template_decl = saved_processing_template_decl;
5184 }
5185 return expr;
5186 }
5187
5188 tree
5189 fold_non_dependent_expr (tree expr)
5190 {
5191 return fold_non_dependent_expr_sfinae (expr, tf_error);
5192 }
5193
5194 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5195 template declaration, or a TYPE_DECL for an alias declaration. */
5196
5197 bool
5198 alias_type_or_template_p (tree t)
5199 {
5200 if (t == NULL_TREE)
5201 return false;
5202 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5203 || (TYPE_P (t)
5204 && TYPE_NAME (t)
5205 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5206 || DECL_ALIAS_TEMPLATE_P (t));
5207 }
5208
5209 /* Return TRUE iff is a specialization of an alias template. */
5210
5211 bool
5212 alias_template_specialization_p (const_tree t)
5213 {
5214 if (t == NULL_TREE)
5215 return false;
5216
5217 return (TYPE_P (t)
5218 && TYPE_TEMPLATE_INFO (t)
5219 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5220 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5221 }
5222
5223 /* Return the number of innermost template parameters in TMPL. */
5224
5225 static int
5226 num_innermost_template_parms (tree tmpl)
5227 {
5228 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5229 return TREE_VEC_LENGTH (parms);
5230 }
5231
5232 /* Return either TMPL or another template that it is equivalent to under DR
5233 1286: An alias that just changes the name of a template is equivalent to
5234 the other template. */
5235
5236 static tree
5237 get_underlying_template (tree tmpl)
5238 {
5239 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5240 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5241 {
5242 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5243 if (TYPE_TEMPLATE_INFO (result))
5244 {
5245 tree sub = TYPE_TI_TEMPLATE (result);
5246 if (PRIMARY_TEMPLATE_P (sub)
5247 && (num_innermost_template_parms (tmpl)
5248 == num_innermost_template_parms (sub)))
5249 {
5250 tree alias_args = INNERMOST_TEMPLATE_ARGS
5251 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5252 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5253 break;
5254 /* The alias type is equivalent to the pattern of the
5255 underlying template, so strip the alias. */
5256 tmpl = sub;
5257 continue;
5258 }
5259 }
5260 break;
5261 }
5262 return tmpl;
5263 }
5264
5265 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5266 must be a function or a pointer-to-function type, as specified
5267 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5268 and check that the resulting function has external linkage. */
5269
5270 static tree
5271 convert_nontype_argument_function (tree type, tree expr)
5272 {
5273 tree fns = expr;
5274 tree fn, fn_no_ptr;
5275 linkage_kind linkage;
5276
5277 fn = instantiate_type (type, fns, tf_none);
5278 if (fn == error_mark_node)
5279 return error_mark_node;
5280
5281 fn_no_ptr = fn;
5282 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5283 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5284 if (BASELINK_P (fn_no_ptr))
5285 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5286
5287 /* [temp.arg.nontype]/1
5288
5289 A template-argument for a non-type, non-template template-parameter
5290 shall be one of:
5291 [...]
5292 -- the address of an object or function with external [C++11: or
5293 internal] linkage. */
5294
5295 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5296 {
5297 error ("%qE is not a valid template argument for type %qT", expr, type);
5298 if (TYPE_PTR_P (type))
5299 error ("it must be the address of a function with external linkage");
5300 else
5301 error ("it must be the name of a function with external linkage");
5302 return NULL_TREE;
5303 }
5304
5305 linkage = decl_linkage (fn_no_ptr);
5306 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5307 {
5308 if (cxx_dialect >= cxx11)
5309 error ("%qE is not a valid template argument for type %qT "
5310 "because %qD has no linkage",
5311 expr, type, fn_no_ptr);
5312 else
5313 error ("%qE is not a valid template argument for type %qT "
5314 "because %qD does not have external linkage",
5315 expr, type, fn_no_ptr);
5316 return NULL_TREE;
5317 }
5318
5319 return fn;
5320 }
5321
5322 /* Subroutine of convert_nontype_argument.
5323 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5324 Emit an error otherwise. */
5325
5326 static bool
5327 check_valid_ptrmem_cst_expr (tree type, tree expr,
5328 tsubst_flags_t complain)
5329 {
5330 STRIP_NOPS (expr);
5331 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5332 return true;
5333 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5334 return true;
5335 if (complain & tf_error)
5336 {
5337 error ("%qE is not a valid template argument for type %qT",
5338 expr, type);
5339 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5340 }
5341 return false;
5342 }
5343
5344 /* Returns TRUE iff the address of OP is value-dependent.
5345
5346 14.6.2.4 [temp.dep.temp]:
5347 A non-integral non-type template-argument is dependent if its type is
5348 dependent or it has either of the following forms
5349 qualified-id
5350 & qualified-id
5351 and contains a nested-name-specifier which specifies a class-name that
5352 names a dependent type.
5353
5354 We generalize this to just say that the address of a member of a
5355 dependent class is value-dependent; the above doesn't cover the
5356 address of a static data member named with an unqualified-id. */
5357
5358 static bool
5359 has_value_dependent_address (tree op)
5360 {
5361 /* We could use get_inner_reference here, but there's no need;
5362 this is only relevant for template non-type arguments, which
5363 can only be expressed as &id-expression. */
5364 if (DECL_P (op))
5365 {
5366 tree ctx = CP_DECL_CONTEXT (op);
5367 if (TYPE_P (ctx) && dependent_type_p (ctx))
5368 return true;
5369 }
5370
5371 return false;
5372 }
5373
5374 /* The next set of functions are used for providing helpful explanatory
5375 diagnostics for failed overload resolution. Their messages should be
5376 indented by two spaces for consistency with the messages in
5377 call.c */
5378
5379 static int
5380 unify_success (bool /*explain_p*/)
5381 {
5382 return 0;
5383 }
5384
5385 static int
5386 unify_parameter_deduction_failure (bool explain_p, tree parm)
5387 {
5388 if (explain_p)
5389 inform (input_location,
5390 " couldn't deduce template parameter %qD", parm);
5391 return 1;
5392 }
5393
5394 static int
5395 unify_invalid (bool /*explain_p*/)
5396 {
5397 return 1;
5398 }
5399
5400 static int
5401 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5402 {
5403 if (explain_p)
5404 inform (input_location,
5405 " types %qT and %qT have incompatible cv-qualifiers",
5406 parm, arg);
5407 return 1;
5408 }
5409
5410 static int
5411 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5412 {
5413 if (explain_p)
5414 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5415 return 1;
5416 }
5417
5418 static int
5419 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5420 {
5421 if (explain_p)
5422 inform (input_location,
5423 " template parameter %qD is not a parameter pack, but "
5424 "argument %qD is",
5425 parm, arg);
5426 return 1;
5427 }
5428
5429 static int
5430 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5431 {
5432 if (explain_p)
5433 inform (input_location,
5434 " template argument %qE does not match "
5435 "pointer-to-member constant %qE",
5436 arg, parm);
5437 return 1;
5438 }
5439
5440 static int
5441 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5442 {
5443 if (explain_p)
5444 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5445 return 1;
5446 }
5447
5448 static int
5449 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5450 {
5451 if (explain_p)
5452 inform (input_location,
5453 " inconsistent parameter pack deduction with %qT and %qT",
5454 old_arg, new_arg);
5455 return 1;
5456 }
5457
5458 static int
5459 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5460 {
5461 if (explain_p)
5462 {
5463 if (TYPE_P (parm))
5464 inform (input_location,
5465 " deduced conflicting types for parameter %qT (%qT and %qT)",
5466 parm, first, second);
5467 else
5468 inform (input_location,
5469 " deduced conflicting values for non-type parameter "
5470 "%qE (%qE and %qE)", parm, first, second);
5471 }
5472 return 1;
5473 }
5474
5475 static int
5476 unify_vla_arg (bool explain_p, tree arg)
5477 {
5478 if (explain_p)
5479 inform (input_location,
5480 " variable-sized array type %qT is not "
5481 "a valid template argument",
5482 arg);
5483 return 1;
5484 }
5485
5486 static int
5487 unify_method_type_error (bool explain_p, tree arg)
5488 {
5489 if (explain_p)
5490 inform (input_location,
5491 " member function type %qT is not a valid template argument",
5492 arg);
5493 return 1;
5494 }
5495
5496 static int
5497 unify_arity (bool explain_p, int have, int wanted)
5498 {
5499 if (explain_p)
5500 inform_n (input_location, wanted,
5501 " candidate expects %d argument, %d provided",
5502 " candidate expects %d arguments, %d provided",
5503 wanted, have);
5504 return 1;
5505 }
5506
5507 static int
5508 unify_too_many_arguments (bool explain_p, int have, int wanted)
5509 {
5510 return unify_arity (explain_p, have, wanted);
5511 }
5512
5513 static int
5514 unify_too_few_arguments (bool explain_p, int have, int wanted)
5515 {
5516 return unify_arity (explain_p, have, wanted);
5517 }
5518
5519 static int
5520 unify_arg_conversion (bool explain_p, tree to_type,
5521 tree from_type, tree arg)
5522 {
5523 if (explain_p)
5524 inform (EXPR_LOC_OR_LOC (arg, input_location),
5525 " cannot convert %qE (type %qT) to type %qT",
5526 arg, from_type, to_type);
5527 return 1;
5528 }
5529
5530 static int
5531 unify_no_common_base (bool explain_p, enum template_base_result r,
5532 tree parm, tree arg)
5533 {
5534 if (explain_p)
5535 switch (r)
5536 {
5537 case tbr_ambiguous_baseclass:
5538 inform (input_location, " %qT is an ambiguous base class of %qT",
5539 parm, arg);
5540 break;
5541 default:
5542 inform (input_location, " %qT is not derived from %qT", arg, parm);
5543 break;
5544 }
5545 return 1;
5546 }
5547
5548 static int
5549 unify_inconsistent_template_template_parameters (bool explain_p)
5550 {
5551 if (explain_p)
5552 inform (input_location,
5553 " template parameters of a template template argument are "
5554 "inconsistent with other deduced template arguments");
5555 return 1;
5556 }
5557
5558 static int
5559 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5560 {
5561 if (explain_p)
5562 inform (input_location,
5563 " can't deduce a template for %qT from non-template type %qT",
5564 parm, arg);
5565 return 1;
5566 }
5567
5568 static int
5569 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5570 {
5571 if (explain_p)
5572 inform (input_location,
5573 " template argument %qE does not match %qD", arg, parm);
5574 return 1;
5575 }
5576
5577 static int
5578 unify_overload_resolution_failure (bool explain_p, tree arg)
5579 {
5580 if (explain_p)
5581 inform (input_location,
5582 " could not resolve address from overloaded function %qE",
5583 arg);
5584 return 1;
5585 }
5586
5587 /* Attempt to convert the non-type template parameter EXPR to the
5588 indicated TYPE. If the conversion is successful, return the
5589 converted value. If the conversion is unsuccessful, return
5590 NULL_TREE if we issued an error message, or error_mark_node if we
5591 did not. We issue error messages for out-and-out bad template
5592 parameters, but not simply because the conversion failed, since we
5593 might be just trying to do argument deduction. Both TYPE and EXPR
5594 must be non-dependent.
5595
5596 The conversion follows the special rules described in
5597 [temp.arg.nontype], and it is much more strict than an implicit
5598 conversion.
5599
5600 This function is called twice for each template argument (see
5601 lookup_template_class for a more accurate description of this
5602 problem). This means that we need to handle expressions which
5603 are not valid in a C++ source, but can be created from the
5604 first call (for instance, casts to perform conversions). These
5605 hacks can go away after we fix the double coercion problem. */
5606
5607 static tree
5608 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5609 {
5610 tree expr_type;
5611
5612 /* Detect immediately string literals as invalid non-type argument.
5613 This special-case is not needed for correctness (we would easily
5614 catch this later), but only to provide better diagnostic for this
5615 common user mistake. As suggested by DR 100, we do not mention
5616 linkage issues in the diagnostic as this is not the point. */
5617 /* FIXME we're making this OK. */
5618 if (TREE_CODE (expr) == STRING_CST)
5619 {
5620 if (complain & tf_error)
5621 error ("%qE is not a valid template argument for type %qT "
5622 "because string literals can never be used in this context",
5623 expr, type);
5624 return NULL_TREE;
5625 }
5626
5627 /* Add the ADDR_EXPR now for the benefit of
5628 value_dependent_expression_p. */
5629 if (TYPE_PTROBV_P (type)
5630 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5631 {
5632 expr = decay_conversion (expr, complain);
5633 if (expr == error_mark_node)
5634 return error_mark_node;
5635 }
5636
5637 /* If we are in a template, EXPR may be non-dependent, but still
5638 have a syntactic, rather than semantic, form. For example, EXPR
5639 might be a SCOPE_REF, rather than the VAR_DECL to which the
5640 SCOPE_REF refers. Preserving the qualifying scope is necessary
5641 so that access checking can be performed when the template is
5642 instantiated -- but here we need the resolved form so that we can
5643 convert the argument. */
5644 if (TYPE_REF_OBJ_P (type)
5645 && has_value_dependent_address (expr))
5646 /* If we want the address and it's value-dependent, don't fold. */;
5647 else if (!type_unknown_p (expr))
5648 expr = fold_non_dependent_expr_sfinae (expr, complain);
5649 if (error_operand_p (expr))
5650 return error_mark_node;
5651 expr_type = TREE_TYPE (expr);
5652 if (TREE_CODE (type) == REFERENCE_TYPE)
5653 expr = mark_lvalue_use (expr);
5654 else
5655 expr = mark_rvalue_use (expr);
5656
5657 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5658 to a non-type argument of "nullptr". */
5659 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5660 expr = convert (type, expr);
5661
5662 /* In C++11, integral or enumeration non-type template arguments can be
5663 arbitrary constant expressions. Pointer and pointer to
5664 member arguments can be general constant expressions that evaluate
5665 to a null value, but otherwise still need to be of a specific form. */
5666 if (cxx_dialect >= cxx11)
5667 {
5668 if (TREE_CODE (expr) == PTRMEM_CST)
5669 /* A PTRMEM_CST is already constant, and a valid template
5670 argument for a parameter of pointer to member type, we just want
5671 to leave it in that form rather than lower it to a
5672 CONSTRUCTOR. */;
5673 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5674 expr = maybe_constant_value (expr);
5675 else if (TYPE_PTR_OR_PTRMEM_P (type))
5676 {
5677 tree folded = maybe_constant_value (expr);
5678 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5679 : null_member_pointer_value_p (folded))
5680 expr = folded;
5681 }
5682 }
5683
5684 /* HACK: Due to double coercion, we can get a
5685 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5686 which is the tree that we built on the first call (see
5687 below when coercing to reference to object or to reference to
5688 function). We just strip everything and get to the arg.
5689 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5690 for examples. */
5691 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5692 {
5693 tree probe_type, probe = expr;
5694 if (REFERENCE_REF_P (probe))
5695 probe = TREE_OPERAND (probe, 0);
5696 probe_type = TREE_TYPE (probe);
5697 if (TREE_CODE (probe) == NOP_EXPR)
5698 {
5699 /* ??? Maybe we could use convert_from_reference here, but we
5700 would need to relax its constraints because the NOP_EXPR
5701 could actually change the type to something more cv-qualified,
5702 and this is not folded by convert_from_reference. */
5703 tree addr = TREE_OPERAND (probe, 0);
5704 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5705 && TREE_CODE (addr) == ADDR_EXPR
5706 && TYPE_PTR_P (TREE_TYPE (addr))
5707 && (same_type_ignoring_top_level_qualifiers_p
5708 (TREE_TYPE (probe_type),
5709 TREE_TYPE (TREE_TYPE (addr)))))
5710 {
5711 expr = TREE_OPERAND (addr, 0);
5712 expr_type = TREE_TYPE (probe_type);
5713 }
5714 }
5715 }
5716
5717 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5718 parameter is a pointer to object, through decay and
5719 qualification conversion. Let's strip everything. */
5720 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5721 {
5722 tree probe = expr;
5723 STRIP_NOPS (probe);
5724 if (TREE_CODE (probe) == ADDR_EXPR
5725 && TYPE_PTR_P (TREE_TYPE (probe)))
5726 {
5727 /* Skip the ADDR_EXPR only if it is part of the decay for
5728 an array. Otherwise, it is part of the original argument
5729 in the source code. */
5730 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5731 probe = TREE_OPERAND (probe, 0);
5732 expr = probe;
5733 expr_type = TREE_TYPE (expr);
5734 }
5735 }
5736
5737 /* [temp.arg.nontype]/5, bullet 1
5738
5739 For a non-type template-parameter of integral or enumeration type,
5740 integral promotions (_conv.prom_) and integral conversions
5741 (_conv.integral_) are applied. */
5742 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5743 {
5744 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5745 t = maybe_constant_value (t);
5746 if (t != error_mark_node)
5747 expr = t;
5748
5749 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5750 return error_mark_node;
5751
5752 /* Notice that there are constant expressions like '4 % 0' which
5753 do not fold into integer constants. */
5754 if (TREE_CODE (expr) != INTEGER_CST)
5755 {
5756 if (complain & tf_error)
5757 {
5758 int errs = errorcount, warns = warningcount + werrorcount;
5759 if (processing_template_decl
5760 && !require_potential_constant_expression (expr))
5761 return NULL_TREE;
5762 expr = cxx_constant_value (expr);
5763 if (errorcount > errs || warningcount + werrorcount > warns)
5764 inform (EXPR_LOC_OR_LOC (expr, input_location),
5765 "in template argument for type %qT ", type);
5766 if (expr == error_mark_node)
5767 return NULL_TREE;
5768 /* else cxx_constant_value complained but gave us
5769 a real constant, so go ahead. */
5770 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5771 }
5772 else
5773 return NULL_TREE;
5774 }
5775
5776 /* Avoid typedef problems. */
5777 if (TREE_TYPE (expr) != type)
5778 expr = fold_convert (type, expr);
5779 }
5780 /* [temp.arg.nontype]/5, bullet 2
5781
5782 For a non-type template-parameter of type pointer to object,
5783 qualification conversions (_conv.qual_) and the array-to-pointer
5784 conversion (_conv.array_) are applied. */
5785 else if (TYPE_PTROBV_P (type))
5786 {
5787 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5788
5789 A template-argument for a non-type, non-template template-parameter
5790 shall be one of: [...]
5791
5792 -- the name of a non-type template-parameter;
5793 -- the address of an object or function with external linkage, [...]
5794 expressed as "& id-expression" where the & is optional if the name
5795 refers to a function or array, or if the corresponding
5796 template-parameter is a reference.
5797
5798 Here, we do not care about functions, as they are invalid anyway
5799 for a parameter of type pointer-to-object. */
5800
5801 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5802 /* Non-type template parameters are OK. */
5803 ;
5804 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5805 /* Null pointer values are OK in C++11. */;
5806 else if (TREE_CODE (expr) != ADDR_EXPR
5807 && TREE_CODE (expr_type) != ARRAY_TYPE)
5808 {
5809 if (VAR_P (expr))
5810 {
5811 error ("%qD is not a valid template argument "
5812 "because %qD is a variable, not the address of "
5813 "a variable",
5814 expr, expr);
5815 return NULL_TREE;
5816 }
5817 if (POINTER_TYPE_P (expr_type))
5818 {
5819 error ("%qE is not a valid template argument for %qT "
5820 "because it is not the address of a variable",
5821 expr, type);
5822 return NULL_TREE;
5823 }
5824 /* Other values, like integer constants, might be valid
5825 non-type arguments of some other type. */
5826 return error_mark_node;
5827 }
5828 else
5829 {
5830 tree decl;
5831
5832 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5833 ? TREE_OPERAND (expr, 0) : expr);
5834 if (!VAR_P (decl))
5835 {
5836 error ("%qE is not a valid template argument of type %qT "
5837 "because %qE is not a variable",
5838 expr, type, decl);
5839 return NULL_TREE;
5840 }
5841 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5842 {
5843 error ("%qE is not a valid template argument of type %qT "
5844 "because %qD does not have external linkage",
5845 expr, type, decl);
5846 return NULL_TREE;
5847 }
5848 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5849 {
5850 error ("%qE is not a valid template argument of type %qT "
5851 "because %qD has no linkage",
5852 expr, type, decl);
5853 return NULL_TREE;
5854 }
5855 }
5856
5857 expr = decay_conversion (expr, complain);
5858 if (expr == error_mark_node)
5859 return error_mark_node;
5860
5861 expr = perform_qualification_conversions (type, expr);
5862 if (expr == error_mark_node)
5863 return error_mark_node;
5864 }
5865 /* [temp.arg.nontype]/5, bullet 3
5866
5867 For a non-type template-parameter of type reference to object, no
5868 conversions apply. The type referred to by the reference may be more
5869 cv-qualified than the (otherwise identical) type of the
5870 template-argument. The template-parameter is bound directly to the
5871 template-argument, which must be an lvalue. */
5872 else if (TYPE_REF_OBJ_P (type))
5873 {
5874 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5875 expr_type))
5876 return error_mark_node;
5877
5878 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5879 {
5880 error ("%qE is not a valid template argument for type %qT "
5881 "because of conflicts in cv-qualification", expr, type);
5882 return NULL_TREE;
5883 }
5884
5885 if (!real_lvalue_p (expr))
5886 {
5887 error ("%qE is not a valid template argument for type %qT "
5888 "because it is not an lvalue", expr, type);
5889 return NULL_TREE;
5890 }
5891
5892 /* [temp.arg.nontype]/1
5893
5894 A template-argument for a non-type, non-template template-parameter
5895 shall be one of: [...]
5896
5897 -- the address of an object or function with external linkage. */
5898 if (INDIRECT_REF_P (expr)
5899 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5900 {
5901 expr = TREE_OPERAND (expr, 0);
5902 if (DECL_P (expr))
5903 {
5904 error ("%q#D is not a valid template argument for type %qT "
5905 "because a reference variable does not have a constant "
5906 "address", expr, type);
5907 return NULL_TREE;
5908 }
5909 }
5910
5911 if (!DECL_P (expr))
5912 {
5913 error ("%qE is not a valid template argument for type %qT "
5914 "because it is not an object with external linkage",
5915 expr, type);
5916 return NULL_TREE;
5917 }
5918
5919 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5920 {
5921 error ("%qE is not a valid template argument for type %qT "
5922 "because object %qD has not external linkage",
5923 expr, type, expr);
5924 return NULL_TREE;
5925 }
5926
5927 expr = build_nop (type, build_address (expr));
5928 }
5929 /* [temp.arg.nontype]/5, bullet 4
5930
5931 For a non-type template-parameter of type pointer to function, only
5932 the function-to-pointer conversion (_conv.func_) is applied. If the
5933 template-argument represents a set of overloaded functions (or a
5934 pointer to such), the matching function is selected from the set
5935 (_over.over_). */
5936 else if (TYPE_PTRFN_P (type))
5937 {
5938 /* If the argument is a template-id, we might not have enough
5939 context information to decay the pointer. */
5940 if (!type_unknown_p (expr_type))
5941 {
5942 expr = decay_conversion (expr, complain);
5943 if (expr == error_mark_node)
5944 return error_mark_node;
5945 }
5946
5947 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5948 /* Null pointer values are OK in C++11. */
5949 return perform_qualification_conversions (type, expr);
5950
5951 expr = convert_nontype_argument_function (type, expr);
5952 if (!expr || expr == error_mark_node)
5953 return expr;
5954 }
5955 /* [temp.arg.nontype]/5, bullet 5
5956
5957 For a non-type template-parameter of type reference to function, no
5958 conversions apply. If the template-argument represents a set of
5959 overloaded functions, the matching function is selected from the set
5960 (_over.over_). */
5961 else if (TYPE_REFFN_P (type))
5962 {
5963 if (TREE_CODE (expr) == ADDR_EXPR)
5964 {
5965 error ("%qE is not a valid template argument for type %qT "
5966 "because it is a pointer", expr, type);
5967 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5968 return NULL_TREE;
5969 }
5970
5971 expr = convert_nontype_argument_function (type, expr);
5972 if (!expr || expr == error_mark_node)
5973 return expr;
5974
5975 expr = build_nop (type, build_address (expr));
5976 }
5977 /* [temp.arg.nontype]/5, bullet 6
5978
5979 For a non-type template-parameter of type pointer to member function,
5980 no conversions apply. If the template-argument represents a set of
5981 overloaded member functions, the matching member function is selected
5982 from the set (_over.over_). */
5983 else if (TYPE_PTRMEMFUNC_P (type))
5984 {
5985 expr = instantiate_type (type, expr, tf_none);
5986 if (expr == error_mark_node)
5987 return error_mark_node;
5988
5989 /* [temp.arg.nontype] bullet 1 says the pointer to member
5990 expression must be a pointer-to-member constant. */
5991 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5992 return error_mark_node;
5993
5994 /* There is no way to disable standard conversions in
5995 resolve_address_of_overloaded_function (called by
5996 instantiate_type). It is possible that the call succeeded by
5997 converting &B::I to &D::I (where B is a base of D), so we need
5998 to reject this conversion here.
5999
6000 Actually, even if there was a way to disable standard conversions,
6001 it would still be better to reject them here so that we can
6002 provide a superior diagnostic. */
6003 if (!same_type_p (TREE_TYPE (expr), type))
6004 {
6005 error ("%qE is not a valid template argument for type %qT "
6006 "because it is of type %qT", expr, type,
6007 TREE_TYPE (expr));
6008 /* If we are just one standard conversion off, explain. */
6009 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6010 inform (input_location,
6011 "standard conversions are not allowed in this context");
6012 return NULL_TREE;
6013 }
6014 }
6015 /* [temp.arg.nontype]/5, bullet 7
6016
6017 For a non-type template-parameter of type pointer to data member,
6018 qualification conversions (_conv.qual_) are applied. */
6019 else if (TYPE_PTRDATAMEM_P (type))
6020 {
6021 /* [temp.arg.nontype] bullet 1 says the pointer to member
6022 expression must be a pointer-to-member constant. */
6023 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6024 return error_mark_node;
6025
6026 expr = perform_qualification_conversions (type, expr);
6027 if (expr == error_mark_node)
6028 return expr;
6029 }
6030 else if (NULLPTR_TYPE_P (type))
6031 {
6032 if (expr != nullptr_node)
6033 {
6034 error ("%qE is not a valid template argument for type %qT "
6035 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6036 return NULL_TREE;
6037 }
6038 return expr;
6039 }
6040 /* A template non-type parameter must be one of the above. */
6041 else
6042 gcc_unreachable ();
6043
6044 /* Sanity check: did we actually convert the argument to the
6045 right type? */
6046 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6047 (type, TREE_TYPE (expr)));
6048 return expr;
6049 }
6050
6051 /* Subroutine of coerce_template_template_parms, which returns 1 if
6052 PARM_PARM and ARG_PARM match using the rule for the template
6053 parameters of template template parameters. Both PARM and ARG are
6054 template parameters; the rest of the arguments are the same as for
6055 coerce_template_template_parms.
6056 */
6057 static int
6058 coerce_template_template_parm (tree parm,
6059 tree arg,
6060 tsubst_flags_t complain,
6061 tree in_decl,
6062 tree outer_args)
6063 {
6064 if (arg == NULL_TREE || error_operand_p (arg)
6065 || parm == NULL_TREE || error_operand_p (parm))
6066 return 0;
6067
6068 if (TREE_CODE (arg) != TREE_CODE (parm))
6069 return 0;
6070
6071 switch (TREE_CODE (parm))
6072 {
6073 case TEMPLATE_DECL:
6074 /* We encounter instantiations of templates like
6075 template <template <template <class> class> class TT>
6076 class C; */
6077 {
6078 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6079 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6080
6081 if (!coerce_template_template_parms
6082 (parmparm, argparm, complain, in_decl, outer_args))
6083 return 0;
6084 }
6085 /* Fall through. */
6086
6087 case TYPE_DECL:
6088 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6089 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6090 /* Argument is a parameter pack but parameter is not. */
6091 return 0;
6092 break;
6093
6094 case PARM_DECL:
6095 /* The tsubst call is used to handle cases such as
6096
6097 template <int> class C {};
6098 template <class T, template <T> class TT> class D {};
6099 D<int, C> d;
6100
6101 i.e. the parameter list of TT depends on earlier parameters. */
6102 if (!uses_template_parms (TREE_TYPE (arg))
6103 && !same_type_p
6104 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6105 TREE_TYPE (arg)))
6106 return 0;
6107
6108 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6109 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6110 /* Argument is a parameter pack but parameter is not. */
6111 return 0;
6112
6113 break;
6114
6115 default:
6116 gcc_unreachable ();
6117 }
6118
6119 return 1;
6120 }
6121
6122
6123 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6124 template template parameters. Both PARM_PARMS and ARG_PARMS are
6125 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6126 or PARM_DECL.
6127
6128 Consider the example:
6129 template <class T> class A;
6130 template<template <class U> class TT> class B;
6131
6132 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6133 the parameters to A, and OUTER_ARGS contains A. */
6134
6135 static int
6136 coerce_template_template_parms (tree parm_parms,
6137 tree arg_parms,
6138 tsubst_flags_t complain,
6139 tree in_decl,
6140 tree outer_args)
6141 {
6142 int nparms, nargs, i;
6143 tree parm, arg;
6144 int variadic_p = 0;
6145
6146 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6147 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6148
6149 nparms = TREE_VEC_LENGTH (parm_parms);
6150 nargs = TREE_VEC_LENGTH (arg_parms);
6151
6152 /* Determine whether we have a parameter pack at the end of the
6153 template template parameter's template parameter list. */
6154 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6155 {
6156 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6157
6158 if (error_operand_p (parm))
6159 return 0;
6160
6161 switch (TREE_CODE (parm))
6162 {
6163 case TEMPLATE_DECL:
6164 case TYPE_DECL:
6165 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6166 variadic_p = 1;
6167 break;
6168
6169 case PARM_DECL:
6170 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6171 variadic_p = 1;
6172 break;
6173
6174 default:
6175 gcc_unreachable ();
6176 }
6177 }
6178
6179 if (nargs != nparms
6180 && !(variadic_p && nargs >= nparms - 1))
6181 return 0;
6182
6183 /* Check all of the template parameters except the parameter pack at
6184 the end (if any). */
6185 for (i = 0; i < nparms - variadic_p; ++i)
6186 {
6187 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6188 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6189 continue;
6190
6191 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6192 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6193
6194 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6195 outer_args))
6196 return 0;
6197
6198 }
6199
6200 if (variadic_p)
6201 {
6202 /* Check each of the template parameters in the template
6203 argument against the template parameter pack at the end of
6204 the template template parameter. */
6205 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6206 return 0;
6207
6208 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6209
6210 for (; i < nargs; ++i)
6211 {
6212 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6213 continue;
6214
6215 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6216
6217 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6218 outer_args))
6219 return 0;
6220 }
6221 }
6222
6223 return 1;
6224 }
6225
6226 /* Verifies that the deduced template arguments (in TARGS) for the
6227 template template parameters (in TPARMS) represent valid bindings,
6228 by comparing the template parameter list of each template argument
6229 to the template parameter list of its corresponding template
6230 template parameter, in accordance with DR150. This
6231 routine can only be called after all template arguments have been
6232 deduced. It will return TRUE if all of the template template
6233 parameter bindings are okay, FALSE otherwise. */
6234 bool
6235 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6236 {
6237 int i, ntparms = TREE_VEC_LENGTH (tparms);
6238 bool ret = true;
6239
6240 /* We're dealing with template parms in this process. */
6241 ++processing_template_decl;
6242
6243 targs = INNERMOST_TEMPLATE_ARGS (targs);
6244
6245 for (i = 0; i < ntparms; ++i)
6246 {
6247 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6248 tree targ = TREE_VEC_ELT (targs, i);
6249
6250 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6251 {
6252 tree packed_args = NULL_TREE;
6253 int idx, len = 1;
6254
6255 if (ARGUMENT_PACK_P (targ))
6256 {
6257 /* Look inside the argument pack. */
6258 packed_args = ARGUMENT_PACK_ARGS (targ);
6259 len = TREE_VEC_LENGTH (packed_args);
6260 }
6261
6262 for (idx = 0; idx < len; ++idx)
6263 {
6264 tree targ_parms = NULL_TREE;
6265
6266 if (packed_args)
6267 /* Extract the next argument from the argument
6268 pack. */
6269 targ = TREE_VEC_ELT (packed_args, idx);
6270
6271 if (PACK_EXPANSION_P (targ))
6272 /* Look at the pattern of the pack expansion. */
6273 targ = PACK_EXPANSION_PATTERN (targ);
6274
6275 /* Extract the template parameters from the template
6276 argument. */
6277 if (TREE_CODE (targ) == TEMPLATE_DECL)
6278 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6279 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6280 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6281
6282 /* Verify that we can coerce the template template
6283 parameters from the template argument to the template
6284 parameter. This requires an exact match. */
6285 if (targ_parms
6286 && !coerce_template_template_parms
6287 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6288 targ_parms,
6289 tf_none,
6290 tparm,
6291 targs))
6292 {
6293 ret = false;
6294 goto out;
6295 }
6296 }
6297 }
6298 }
6299
6300 out:
6301
6302 --processing_template_decl;
6303 return ret;
6304 }
6305
6306 /* Since type attributes aren't mangled, we need to strip them from
6307 template type arguments. */
6308
6309 static tree
6310 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6311 {
6312 tree mv;
6313 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6314 return arg;
6315 mv = TYPE_MAIN_VARIANT (arg);
6316 arg = strip_typedefs (arg);
6317 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6318 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6319 {
6320 if (complain & tf_warning)
6321 warning (0, "ignoring attributes on template argument %qT", arg);
6322 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6323 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6324 }
6325 return arg;
6326 }
6327
6328 /* Convert the indicated template ARG as necessary to match the
6329 indicated template PARM. Returns the converted ARG, or
6330 error_mark_node if the conversion was unsuccessful. Error and
6331 warning messages are issued under control of COMPLAIN. This
6332 conversion is for the Ith parameter in the parameter list. ARGS is
6333 the full set of template arguments deduced so far. */
6334
6335 static tree
6336 convert_template_argument (tree parm,
6337 tree arg,
6338 tree args,
6339 tsubst_flags_t complain,
6340 int i,
6341 tree in_decl)
6342 {
6343 tree orig_arg;
6344 tree val;
6345 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6346
6347 if (TREE_CODE (arg) == TREE_LIST
6348 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6349 {
6350 /* The template argument was the name of some
6351 member function. That's usually
6352 invalid, but static members are OK. In any
6353 case, grab the underlying fields/functions
6354 and issue an error later if required. */
6355 orig_arg = TREE_VALUE (arg);
6356 TREE_TYPE (arg) = unknown_type_node;
6357 }
6358
6359 orig_arg = arg;
6360
6361 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6362 requires_type = (TREE_CODE (parm) == TYPE_DECL
6363 || requires_tmpl_type);
6364
6365 /* When determining whether an argument pack expansion is a template,
6366 look at the pattern. */
6367 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6368 arg = PACK_EXPANSION_PATTERN (arg);
6369
6370 /* Deal with an injected-class-name used as a template template arg. */
6371 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6372 {
6373 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6374 if (TREE_CODE (t) == TEMPLATE_DECL)
6375 {
6376 if (cxx_dialect >= cxx11)
6377 /* OK under DR 1004. */;
6378 else if (complain & tf_warning_or_error)
6379 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6380 " used as template template argument", TYPE_NAME (arg));
6381 else if (flag_pedantic_errors)
6382 t = arg;
6383
6384 arg = t;
6385 }
6386 }
6387
6388 is_tmpl_type =
6389 ((TREE_CODE (arg) == TEMPLATE_DECL
6390 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6391 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6392 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6393 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6394
6395 if (is_tmpl_type
6396 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6397 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6398 arg = TYPE_STUB_DECL (arg);
6399
6400 is_type = TYPE_P (arg) || is_tmpl_type;
6401
6402 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6403 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6404 {
6405 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6406 {
6407 if (complain & tf_error)
6408 error ("invalid use of destructor %qE as a type", orig_arg);
6409 return error_mark_node;
6410 }
6411
6412 permerror (input_location,
6413 "to refer to a type member of a template parameter, "
6414 "use %<typename %E%>", orig_arg);
6415
6416 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6417 TREE_OPERAND (arg, 1),
6418 typename_type,
6419 complain);
6420 arg = orig_arg;
6421 is_type = 1;
6422 }
6423 if (is_type != requires_type)
6424 {
6425 if (in_decl)
6426 {
6427 if (complain & tf_error)
6428 {
6429 error ("type/value mismatch at argument %d in template "
6430 "parameter list for %qD",
6431 i + 1, in_decl);
6432 if (is_type)
6433 error (" expected a constant of type %qT, got %qT",
6434 TREE_TYPE (parm),
6435 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6436 else if (requires_tmpl_type)
6437 error (" expected a class template, got %qE", orig_arg);
6438 else
6439 error (" expected a type, got %qE", orig_arg);
6440 }
6441 }
6442 return error_mark_node;
6443 }
6444 if (is_tmpl_type ^ requires_tmpl_type)
6445 {
6446 if (in_decl && (complain & tf_error))
6447 {
6448 error ("type/value mismatch at argument %d in template "
6449 "parameter list for %qD",
6450 i + 1, in_decl);
6451 if (is_tmpl_type)
6452 error (" expected a type, got %qT", DECL_NAME (arg));
6453 else
6454 error (" expected a class template, got %qT", orig_arg);
6455 }
6456 return error_mark_node;
6457 }
6458
6459 if (is_type)
6460 {
6461 if (requires_tmpl_type)
6462 {
6463 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6464 val = orig_arg;
6465 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6466 /* The number of argument required is not known yet.
6467 Just accept it for now. */
6468 val = TREE_TYPE (arg);
6469 else
6470 {
6471 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6472 tree argparm;
6473
6474 /* Strip alias templates that are equivalent to another
6475 template. */
6476 arg = get_underlying_template (arg);
6477 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6478
6479 if (coerce_template_template_parms (parmparm, argparm,
6480 complain, in_decl,
6481 args))
6482 {
6483 val = arg;
6484
6485 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6486 TEMPLATE_DECL. */
6487 if (val != error_mark_node)
6488 {
6489 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6490 val = TREE_TYPE (val);
6491 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6492 val = make_pack_expansion (val);
6493 }
6494 }
6495 else
6496 {
6497 if (in_decl && (complain & tf_error))
6498 {
6499 error ("type/value mismatch at argument %d in "
6500 "template parameter list for %qD",
6501 i + 1, in_decl);
6502 error (" expected a template of type %qD, got %qT",
6503 parm, orig_arg);
6504 }
6505
6506 val = error_mark_node;
6507 }
6508 }
6509 }
6510 else
6511 val = orig_arg;
6512 /* We only form one instance of each template specialization.
6513 Therefore, if we use a non-canonical variant (i.e., a
6514 typedef), any future messages referring to the type will use
6515 the typedef, which is confusing if those future uses do not
6516 themselves also use the typedef. */
6517 if (TYPE_P (val))
6518 val = canonicalize_type_argument (val, complain);
6519 }
6520 else
6521 {
6522 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6523
6524 if (invalid_nontype_parm_type_p (t, complain))
6525 return error_mark_node;
6526
6527 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6528 {
6529 if (same_type_p (t, TREE_TYPE (orig_arg)))
6530 val = orig_arg;
6531 else
6532 {
6533 /* Not sure if this is reachable, but it doesn't hurt
6534 to be robust. */
6535 error ("type mismatch in nontype parameter pack");
6536 val = error_mark_node;
6537 }
6538 }
6539 else if (!dependent_template_arg_p (orig_arg)
6540 && !uses_template_parms (t))
6541 /* We used to call digest_init here. However, digest_init
6542 will report errors, which we don't want when complain
6543 is zero. More importantly, digest_init will try too
6544 hard to convert things: for example, `0' should not be
6545 converted to pointer type at this point according to
6546 the standard. Accepting this is not merely an
6547 extension, since deciding whether or not these
6548 conversions can occur is part of determining which
6549 function template to call, or whether a given explicit
6550 argument specification is valid. */
6551 val = convert_nontype_argument (t, orig_arg, complain);
6552 else
6553 val = strip_typedefs_expr (orig_arg);
6554
6555 if (val == NULL_TREE)
6556 val = error_mark_node;
6557 else if (val == error_mark_node && (complain & tf_error))
6558 error ("could not convert template argument %qE to %qT", orig_arg, t);
6559
6560 if (TREE_CODE (val) == SCOPE_REF)
6561 {
6562 /* Strip typedefs from the SCOPE_REF. */
6563 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6564 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6565 complain);
6566 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6567 QUALIFIED_NAME_IS_TEMPLATE (val));
6568 }
6569 }
6570
6571 return val;
6572 }
6573
6574 /* Coerces the remaining template arguments in INNER_ARGS (from
6575 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6576 Returns the coerced argument pack. PARM_IDX is the position of this
6577 parameter in the template parameter list. ARGS is the original
6578 template argument list. */
6579 static tree
6580 coerce_template_parameter_pack (tree parms,
6581 int parm_idx,
6582 tree args,
6583 tree inner_args,
6584 int arg_idx,
6585 tree new_args,
6586 int* lost,
6587 tree in_decl,
6588 tsubst_flags_t complain)
6589 {
6590 tree parm = TREE_VEC_ELT (parms, parm_idx);
6591 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6592 tree packed_args;
6593 tree argument_pack;
6594 tree packed_parms = NULL_TREE;
6595
6596 if (arg_idx > nargs)
6597 arg_idx = nargs;
6598
6599 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6600 {
6601 /* When the template parameter is a non-type template parameter pack
6602 or template template parameter pack whose type or template
6603 parameters use parameter packs, we know exactly how many arguments
6604 we are looking for. Build a vector of the instantiated decls for
6605 these template parameters in PACKED_PARMS. */
6606 /* We can't use make_pack_expansion here because it would interpret a
6607 _DECL as a use rather than a declaration. */
6608 tree decl = TREE_VALUE (parm);
6609 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6610 SET_PACK_EXPANSION_PATTERN (exp, decl);
6611 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6612 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6613
6614 TREE_VEC_LENGTH (args)--;
6615 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6616 TREE_VEC_LENGTH (args)++;
6617
6618 if (packed_parms == error_mark_node)
6619 return error_mark_node;
6620
6621 /* If we're doing a partial instantiation of a member template,
6622 verify that all of the types used for the non-type
6623 template parameter pack are, in fact, valid for non-type
6624 template parameters. */
6625 if (arg_idx < nargs
6626 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6627 {
6628 int j, len = TREE_VEC_LENGTH (packed_parms);
6629 for (j = 0; j < len; ++j)
6630 {
6631 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6632 if (invalid_nontype_parm_type_p (t, complain))
6633 return error_mark_node;
6634 }
6635 }
6636
6637 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6638 }
6639 else
6640 packed_args = make_tree_vec (nargs - arg_idx);
6641
6642 /* Convert the remaining arguments, which will be a part of the
6643 parameter pack "parm". */
6644 for (; arg_idx < nargs; ++arg_idx)
6645 {
6646 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6647 tree actual_parm = TREE_VALUE (parm);
6648 int pack_idx = arg_idx - parm_idx;
6649
6650 if (packed_parms)
6651 {
6652 /* Once we've packed as many args as we have types, stop. */
6653 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6654 break;
6655 else if (PACK_EXPANSION_P (arg))
6656 /* We don't know how many args we have yet, just
6657 use the unconverted ones for now. */
6658 return NULL_TREE;
6659 else
6660 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6661 }
6662
6663 if (arg == error_mark_node)
6664 {
6665 if (complain & tf_error)
6666 error ("template argument %d is invalid", arg_idx + 1);
6667 }
6668 else
6669 arg = convert_template_argument (actual_parm,
6670 arg, new_args, complain, parm_idx,
6671 in_decl);
6672 if (arg == error_mark_node)
6673 (*lost)++;
6674 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6675 }
6676
6677 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6678 && TREE_VEC_LENGTH (packed_args) > 0)
6679 {
6680 error ("wrong number of template arguments (%d, should be %d)",
6681 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6682 return error_mark_node;
6683 }
6684
6685 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6686 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6687 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6688 else
6689 {
6690 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6691 TREE_TYPE (argument_pack)
6692 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6693 TREE_CONSTANT (argument_pack) = 1;
6694 }
6695
6696 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6697 #ifdef ENABLE_CHECKING
6698 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6699 TREE_VEC_LENGTH (packed_args));
6700 #endif
6701 return argument_pack;
6702 }
6703
6704 /* Returns the number of pack expansions in the template argument vector
6705 ARGS. */
6706
6707 static int
6708 pack_expansion_args_count (tree args)
6709 {
6710 int i;
6711 int count = 0;
6712 if (args)
6713 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6714 {
6715 tree elt = TREE_VEC_ELT (args, i);
6716 if (elt && PACK_EXPANSION_P (elt))
6717 ++count;
6718 }
6719 return count;
6720 }
6721
6722 /* Convert all template arguments to their appropriate types, and
6723 return a vector containing the innermost resulting template
6724 arguments. If any error occurs, return error_mark_node. Error and
6725 warning messages are issued under control of COMPLAIN.
6726
6727 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6728 for arguments not specified in ARGS. Otherwise, if
6729 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6730 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6731 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6732 ARGS. */
6733
6734 static tree
6735 coerce_template_parms (tree parms,
6736 tree args,
6737 tree in_decl,
6738 tsubst_flags_t complain,
6739 bool require_all_args,
6740 bool use_default_args)
6741 {
6742 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6743 tree orig_inner_args;
6744 tree inner_args;
6745 tree new_args;
6746 tree new_inner_args;
6747 int saved_unevaluated_operand;
6748 int saved_inhibit_evaluation_warnings;
6749
6750 /* When used as a boolean value, indicates whether this is a
6751 variadic template parameter list. Since it's an int, we can also
6752 subtract it from nparms to get the number of non-variadic
6753 parameters. */
6754 int variadic_p = 0;
6755 int variadic_args_p = 0;
6756 int post_variadic_parms = 0;
6757
6758 if (args == error_mark_node)
6759 return error_mark_node;
6760
6761 nparms = TREE_VEC_LENGTH (parms);
6762
6763 /* Determine if there are any parameter packs. */
6764 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6765 {
6766 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6767 if (variadic_p)
6768 ++post_variadic_parms;
6769 if (template_parameter_pack_p (tparm))
6770 ++variadic_p;
6771 }
6772
6773 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6774 /* If there are no parameters that follow a parameter pack, we need to
6775 expand any argument packs so that we can deduce a parameter pack from
6776 some non-packed args followed by an argument pack, as in variadic85.C.
6777 If there are such parameters, we need to leave argument packs intact
6778 so the arguments are assigned properly. This can happen when dealing
6779 with a nested class inside a partial specialization of a class
6780 template, as in variadic92.C, or when deducing a template parameter pack
6781 from a sub-declarator, as in variadic114.C. */
6782 if (!post_variadic_parms)
6783 inner_args = expand_template_argument_pack (inner_args);
6784
6785 /* Count any pack expansion args. */
6786 variadic_args_p = pack_expansion_args_count (inner_args);
6787
6788 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6789 if ((nargs > nparms && !variadic_p)
6790 || (nargs < nparms - variadic_p
6791 && require_all_args
6792 && !variadic_args_p
6793 && (!use_default_args
6794 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6795 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6796 {
6797 if (complain & tf_error)
6798 {
6799 if (variadic_p)
6800 {
6801 nparms -= variadic_p;
6802 error ("wrong number of template arguments "
6803 "(%d, should be %d or more)", nargs, nparms);
6804 }
6805 else
6806 error ("wrong number of template arguments "
6807 "(%d, should be %d)", nargs, nparms);
6808
6809 if (in_decl)
6810 error ("provided for %q+D", in_decl);
6811 }
6812
6813 return error_mark_node;
6814 }
6815 /* We can't pass a pack expansion to a non-pack parameter of an alias
6816 template (DR 1430). */
6817 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6818 && variadic_args_p
6819 && nargs - variadic_args_p < nparms - variadic_p)
6820 {
6821 if (complain & tf_error)
6822 {
6823 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6824 {
6825 tree arg = TREE_VEC_ELT (inner_args, i);
6826 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6827
6828 if (PACK_EXPANSION_P (arg)
6829 && !template_parameter_pack_p (parm))
6830 {
6831 error ("pack expansion argument for non-pack parameter "
6832 "%qD of alias template %qD", parm, in_decl);
6833 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6834 goto found;
6835 }
6836 }
6837 gcc_unreachable ();
6838 found:;
6839 }
6840 return error_mark_node;
6841 }
6842
6843 /* We need to evaluate the template arguments, even though this
6844 template-id may be nested within a "sizeof". */
6845 saved_unevaluated_operand = cp_unevaluated_operand;
6846 cp_unevaluated_operand = 0;
6847 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6848 c_inhibit_evaluation_warnings = 0;
6849 new_inner_args = make_tree_vec (nparms);
6850 new_args = add_outermost_template_args (args, new_inner_args);
6851 int pack_adjust = 0;
6852 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6853 {
6854 tree arg;
6855 tree parm;
6856
6857 /* Get the Ith template parameter. */
6858 parm = TREE_VEC_ELT (parms, parm_idx);
6859
6860 if (parm == error_mark_node)
6861 {
6862 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6863 continue;
6864 }
6865
6866 /* Calculate the next argument. */
6867 if (arg_idx < nargs)
6868 arg = TREE_VEC_ELT (inner_args, arg_idx);
6869 else
6870 arg = NULL_TREE;
6871
6872 if (template_parameter_pack_p (TREE_VALUE (parm))
6873 && !(arg && ARGUMENT_PACK_P (arg)))
6874 {
6875 /* Some arguments will be placed in the
6876 template parameter pack PARM. */
6877 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6878 inner_args, arg_idx,
6879 new_args, &lost,
6880 in_decl, complain);
6881
6882 if (arg == NULL_TREE)
6883 {
6884 /* We don't know how many args we have yet, just use the
6885 unconverted (and still packed) ones for now. */
6886 new_inner_args = orig_inner_args;
6887 arg_idx = nargs;
6888 break;
6889 }
6890
6891 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6892
6893 /* Store this argument. */
6894 if (arg == error_mark_node)
6895 {
6896 lost++;
6897 /* We are done with all of the arguments. */
6898 arg_idx = nargs;
6899 }
6900 else
6901 {
6902 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
6903 arg_idx += pack_adjust;
6904 }
6905
6906 continue;
6907 }
6908 else if (arg)
6909 {
6910 if (PACK_EXPANSION_P (arg))
6911 {
6912 /* "If every valid specialization of a variadic template
6913 requires an empty template parameter pack, the template is
6914 ill-formed, no diagnostic required." So check that the
6915 pattern works with this parameter. */
6916 tree pattern = PACK_EXPANSION_PATTERN (arg);
6917 tree conv = convert_template_argument (TREE_VALUE (parm),
6918 pattern, new_args,
6919 complain, parm_idx,
6920 in_decl);
6921 if (conv == error_mark_node)
6922 {
6923 inform (input_location, "so any instantiation with a "
6924 "non-empty parameter pack would be ill-formed");
6925 ++lost;
6926 }
6927 else if (TYPE_P (conv) && !TYPE_P (pattern))
6928 /* Recover from missing typename. */
6929 TREE_VEC_ELT (inner_args, arg_idx)
6930 = make_pack_expansion (conv);
6931
6932 /* We don't know how many args we have yet, just
6933 use the unconverted ones for now. */
6934 new_inner_args = inner_args;
6935 arg_idx = nargs;
6936 break;
6937 }
6938 }
6939 else if (require_all_args)
6940 {
6941 /* There must be a default arg in this case. */
6942 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6943 complain, in_decl);
6944 /* The position of the first default template argument,
6945 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6946 Record that. */
6947 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6948 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6949 arg_idx - pack_adjust);
6950 }
6951 else
6952 break;
6953
6954 if (arg == error_mark_node)
6955 {
6956 if (complain & tf_error)
6957 error ("template argument %d is invalid", arg_idx + 1);
6958 }
6959 else if (!arg)
6960 /* This only occurs if there was an error in the template
6961 parameter list itself (which we would already have
6962 reported) that we are trying to recover from, e.g., a class
6963 template with a parameter list such as
6964 template<typename..., typename>. */
6965 ++lost;
6966 else
6967 arg = convert_template_argument (TREE_VALUE (parm),
6968 arg, new_args, complain,
6969 parm_idx, in_decl);
6970
6971 if (arg == error_mark_node)
6972 lost++;
6973 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
6974 }
6975 cp_unevaluated_operand = saved_unevaluated_operand;
6976 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6977
6978 if (variadic_p && arg_idx < nargs)
6979 {
6980 if (complain & tf_error)
6981 {
6982 error ("wrong number of template arguments "
6983 "(%d, should be %d)", nargs, arg_idx);
6984 if (in_decl)
6985 error ("provided for %q+D", in_decl);
6986 }
6987 return error_mark_node;
6988 }
6989
6990 if (lost)
6991 return error_mark_node;
6992
6993 #ifdef ENABLE_CHECKING
6994 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6996 TREE_VEC_LENGTH (new_inner_args));
6997 #endif
6998
6999 return new_inner_args;
7000 }
7001
7002 /* Like coerce_template_parms. If PARMS represents all template
7003 parameters levels, this function returns a vector of vectors
7004 representing all the resulting argument levels. Note that in this
7005 case, only the innermost arguments are coerced because the
7006 outermost ones are supposed to have been coerced already.
7007
7008 Otherwise, if PARMS represents only (the innermost) vector of
7009 parameters, this function returns a vector containing just the
7010 innermost resulting arguments. */
7011
7012 static tree
7013 coerce_innermost_template_parms (tree parms,
7014 tree args,
7015 tree in_decl,
7016 tsubst_flags_t complain,
7017 bool require_all_args,
7018 bool use_default_args)
7019 {
7020 int parms_depth = TMPL_PARMS_DEPTH (parms);
7021 int args_depth = TMPL_ARGS_DEPTH (args);
7022 tree coerced_args;
7023
7024 if (parms_depth > 1)
7025 {
7026 coerced_args = make_tree_vec (parms_depth);
7027 tree level;
7028 int cur_depth;
7029
7030 for (level = parms, cur_depth = parms_depth;
7031 parms_depth > 0 && level != NULL_TREE;
7032 level = TREE_CHAIN (level), --cur_depth)
7033 {
7034 tree l;
7035 if (cur_depth == args_depth)
7036 l = coerce_template_parms (TREE_VALUE (level),
7037 args, in_decl, complain,
7038 require_all_args,
7039 use_default_args);
7040 else
7041 l = TMPL_ARGS_LEVEL (args, cur_depth);
7042
7043 if (l == error_mark_node)
7044 return error_mark_node;
7045
7046 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7047 }
7048 }
7049 else
7050 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7051 args, in_decl, complain,
7052 require_all_args,
7053 use_default_args);
7054 return coerced_args;
7055 }
7056
7057 /* Returns 1 if template args OT and NT are equivalent. */
7058
7059 static int
7060 template_args_equal (tree ot, tree nt)
7061 {
7062 if (nt == ot)
7063 return 1;
7064 if (nt == NULL_TREE || ot == NULL_TREE)
7065 return false;
7066
7067 if (TREE_CODE (nt) == TREE_VEC)
7068 /* For member templates */
7069 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7070 else if (PACK_EXPANSION_P (ot))
7071 return (PACK_EXPANSION_P (nt)
7072 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7073 PACK_EXPANSION_PATTERN (nt))
7074 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7075 PACK_EXPANSION_EXTRA_ARGS (nt)));
7076 else if (ARGUMENT_PACK_P (ot))
7077 {
7078 int i, len;
7079 tree opack, npack;
7080
7081 if (!ARGUMENT_PACK_P (nt))
7082 return 0;
7083
7084 opack = ARGUMENT_PACK_ARGS (ot);
7085 npack = ARGUMENT_PACK_ARGS (nt);
7086 len = TREE_VEC_LENGTH (opack);
7087 if (TREE_VEC_LENGTH (npack) != len)
7088 return 0;
7089 for (i = 0; i < len; ++i)
7090 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7091 TREE_VEC_ELT (npack, i)))
7092 return 0;
7093 return 1;
7094 }
7095 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7096 {
7097 /* We get here probably because we are in the middle of substituting
7098 into the pattern of a pack expansion. In that case the
7099 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7100 interested in. So we want to use the initial pack argument for
7101 the comparison. */
7102 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7103 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7104 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7105 return template_args_equal (ot, nt);
7106 }
7107 else if (TYPE_P (nt))
7108 return TYPE_P (ot) && same_type_p (ot, nt);
7109 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7110 return 0;
7111 else
7112 return cp_tree_equal (ot, nt);
7113 }
7114
7115 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7116 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7117 NEWARG_PTR with the offending arguments if they are non-NULL. */
7118
7119 static int
7120 comp_template_args_with_info (tree oldargs, tree newargs,
7121 tree *oldarg_ptr, tree *newarg_ptr)
7122 {
7123 int i;
7124
7125 if (oldargs == newargs)
7126 return 1;
7127
7128 if (!oldargs || !newargs)
7129 return 0;
7130
7131 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7132 return 0;
7133
7134 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7135 {
7136 tree nt = TREE_VEC_ELT (newargs, i);
7137 tree ot = TREE_VEC_ELT (oldargs, i);
7138
7139 if (! template_args_equal (ot, nt))
7140 {
7141 if (oldarg_ptr != NULL)
7142 *oldarg_ptr = ot;
7143 if (newarg_ptr != NULL)
7144 *newarg_ptr = nt;
7145 return 0;
7146 }
7147 }
7148 return 1;
7149 }
7150
7151 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7152 of template arguments. Returns 0 otherwise. */
7153
7154 int
7155 comp_template_args (tree oldargs, tree newargs)
7156 {
7157 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7158 }
7159
7160 static void
7161 add_pending_template (tree d)
7162 {
7163 tree ti = (TYPE_P (d)
7164 ? CLASSTYPE_TEMPLATE_INFO (d)
7165 : DECL_TEMPLATE_INFO (d));
7166 struct pending_template *pt;
7167 int level;
7168
7169 if (TI_PENDING_TEMPLATE_FLAG (ti))
7170 return;
7171
7172 /* We are called both from instantiate_decl, where we've already had a
7173 tinst_level pushed, and instantiate_template, where we haven't.
7174 Compensate. */
7175 level = !current_tinst_level || current_tinst_level->decl != d;
7176
7177 if (level)
7178 push_tinst_level (d);
7179
7180 pt = ggc_alloc_pending_template ();
7181 pt->next = NULL;
7182 pt->tinst = current_tinst_level;
7183 if (last_pending_template)
7184 last_pending_template->next = pt;
7185 else
7186 pending_templates = pt;
7187
7188 last_pending_template = pt;
7189
7190 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7191
7192 if (level)
7193 pop_tinst_level ();
7194 }
7195
7196
7197 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7198 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7199 documentation for TEMPLATE_ID_EXPR. */
7200
7201 tree
7202 lookup_template_function (tree fns, tree arglist)
7203 {
7204 tree type;
7205
7206 if (fns == error_mark_node || arglist == error_mark_node)
7207 return error_mark_node;
7208
7209 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7210
7211 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7212 {
7213 error ("%q#D is not a function template", fns);
7214 return error_mark_node;
7215 }
7216
7217 if (BASELINK_P (fns))
7218 {
7219 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7220 unknown_type_node,
7221 BASELINK_FUNCTIONS (fns),
7222 arglist);
7223 return fns;
7224 }
7225
7226 type = TREE_TYPE (fns);
7227 if (TREE_CODE (fns) == OVERLOAD || !type)
7228 type = unknown_type_node;
7229
7230 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7231 }
7232
7233 /* Within the scope of a template class S<T>, the name S gets bound
7234 (in build_self_reference) to a TYPE_DECL for the class, not a
7235 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7236 or one of its enclosing classes, and that type is a template,
7237 return the associated TEMPLATE_DECL. Otherwise, the original
7238 DECL is returned.
7239
7240 Also handle the case when DECL is a TREE_LIST of ambiguous
7241 injected-class-names from different bases. */
7242
7243 tree
7244 maybe_get_template_decl_from_type_decl (tree decl)
7245 {
7246 if (decl == NULL_TREE)
7247 return decl;
7248
7249 /* DR 176: A lookup that finds an injected-class-name (10.2
7250 [class.member.lookup]) can result in an ambiguity in certain cases
7251 (for example, if it is found in more than one base class). If all of
7252 the injected-class-names that are found refer to specializations of
7253 the same class template, and if the name is followed by a
7254 template-argument-list, the reference refers to the class template
7255 itself and not a specialization thereof, and is not ambiguous. */
7256 if (TREE_CODE (decl) == TREE_LIST)
7257 {
7258 tree t, tmpl = NULL_TREE;
7259 for (t = decl; t; t = TREE_CHAIN (t))
7260 {
7261 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7262 if (!tmpl)
7263 tmpl = elt;
7264 else if (tmpl != elt)
7265 break;
7266 }
7267 if (tmpl && t == NULL_TREE)
7268 return tmpl;
7269 else
7270 return decl;
7271 }
7272
7273 return (decl != NULL_TREE
7274 && DECL_SELF_REFERENCE_P (decl)
7275 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7276 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7277 }
7278
7279 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7280 parameters, find the desired type.
7281
7282 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7283
7284 IN_DECL, if non-NULL, is the template declaration we are trying to
7285 instantiate.
7286
7287 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7288 the class we are looking up.
7289
7290 Issue error and warning messages under control of COMPLAIN.
7291
7292 If the template class is really a local class in a template
7293 function, then the FUNCTION_CONTEXT is the function in which it is
7294 being instantiated.
7295
7296 ??? Note that this function is currently called *twice* for each
7297 template-id: the first time from the parser, while creating the
7298 incomplete type (finish_template_type), and the second type during the
7299 real instantiation (instantiate_template_class). This is surely something
7300 that we want to avoid. It also causes some problems with argument
7301 coercion (see convert_nontype_argument for more information on this). */
7302
7303 static tree
7304 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7305 int entering_scope, tsubst_flags_t complain)
7306 {
7307 tree templ = NULL_TREE, parmlist;
7308 tree t;
7309 void **slot;
7310 spec_entry *entry;
7311 spec_entry elt;
7312 hashval_t hash;
7313
7314 if (identifier_p (d1))
7315 {
7316 tree value = innermost_non_namespace_value (d1);
7317 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7318 templ = value;
7319 else
7320 {
7321 if (context)
7322 push_decl_namespace (context);
7323 templ = lookup_name (d1);
7324 templ = maybe_get_template_decl_from_type_decl (templ);
7325 if (context)
7326 pop_decl_namespace ();
7327 }
7328 if (templ)
7329 context = DECL_CONTEXT (templ);
7330 }
7331 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7332 {
7333 tree type = TREE_TYPE (d1);
7334
7335 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7336 an implicit typename for the second A. Deal with it. */
7337 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7338 type = TREE_TYPE (type);
7339
7340 if (CLASSTYPE_TEMPLATE_INFO (type))
7341 {
7342 templ = CLASSTYPE_TI_TEMPLATE (type);
7343 d1 = DECL_NAME (templ);
7344 }
7345 }
7346 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7347 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7348 {
7349 templ = TYPE_TI_TEMPLATE (d1);
7350 d1 = DECL_NAME (templ);
7351 }
7352 else if (TREE_CODE (d1) == TEMPLATE_DECL
7353 && DECL_TEMPLATE_RESULT (d1)
7354 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7355 {
7356 templ = d1;
7357 d1 = DECL_NAME (templ);
7358 context = DECL_CONTEXT (templ);
7359 }
7360 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7361 {
7362 templ = d1;
7363 d1 = DECL_NAME (templ);
7364 }
7365
7366 /* Issue an error message if we didn't find a template. */
7367 if (! templ)
7368 {
7369 if (complain & tf_error)
7370 error ("%qT is not a template", d1);
7371 return error_mark_node;
7372 }
7373
7374 if (TREE_CODE (templ) != TEMPLATE_DECL
7375 /* Make sure it's a user visible template, if it was named by
7376 the user. */
7377 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7378 && !PRIMARY_TEMPLATE_P (templ)))
7379 {
7380 if (complain & tf_error)
7381 {
7382 error ("non-template type %qT used as a template", d1);
7383 if (in_decl)
7384 error ("for template declaration %q+D", in_decl);
7385 }
7386 return error_mark_node;
7387 }
7388
7389 complain &= ~tf_user;
7390
7391 /* An alias that just changes the name of a template is equivalent to the
7392 other template, so if any of the arguments are pack expansions, strip
7393 the alias to avoid problems with a pack expansion passed to a non-pack
7394 alias template parameter (DR 1430). */
7395 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7396 templ = get_underlying_template (templ);
7397
7398 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7399 {
7400 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7401 template arguments */
7402
7403 tree parm;
7404 tree arglist2;
7405 tree outer;
7406
7407 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7408
7409 /* Consider an example where a template template parameter declared as
7410
7411 template <class T, class U = std::allocator<T> > class TT
7412
7413 The template parameter level of T and U are one level larger than
7414 of TT. To proper process the default argument of U, say when an
7415 instantiation `TT<int>' is seen, we need to build the full
7416 arguments containing {int} as the innermost level. Outer levels,
7417 available when not appearing as default template argument, can be
7418 obtained from the arguments of the enclosing template.
7419
7420 Suppose that TT is later substituted with std::vector. The above
7421 instantiation is `TT<int, std::allocator<T> >' with TT at
7422 level 1, and T at level 2, while the template arguments at level 1
7423 becomes {std::vector} and the inner level 2 is {int}. */
7424
7425 outer = DECL_CONTEXT (templ);
7426 if (outer)
7427 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7428 else if (current_template_parms)
7429 /* This is an argument of the current template, so we haven't set
7430 DECL_CONTEXT yet. */
7431 outer = current_template_args ();
7432
7433 if (outer)
7434 arglist = add_to_template_args (outer, arglist);
7435
7436 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7437 complain,
7438 /*require_all_args=*/true,
7439 /*use_default_args=*/true);
7440 if (arglist2 == error_mark_node
7441 || (!uses_template_parms (arglist2)
7442 && check_instantiated_args (templ, arglist2, complain)))
7443 return error_mark_node;
7444
7445 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7446 return parm;
7447 }
7448 else
7449 {
7450 tree template_type = TREE_TYPE (templ);
7451 tree gen_tmpl;
7452 tree type_decl;
7453 tree found = NULL_TREE;
7454 int arg_depth;
7455 int parm_depth;
7456 int is_dependent_type;
7457 int use_partial_inst_tmpl = false;
7458
7459 if (template_type == error_mark_node)
7460 /* An error occurred while building the template TEMPL, and a
7461 diagnostic has most certainly been emitted for that
7462 already. Let's propagate that error. */
7463 return error_mark_node;
7464
7465 gen_tmpl = most_general_template (templ);
7466 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7467 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7468 arg_depth = TMPL_ARGS_DEPTH (arglist);
7469
7470 if (arg_depth == 1 && parm_depth > 1)
7471 {
7472 /* We've been given an incomplete set of template arguments.
7473 For example, given:
7474
7475 template <class T> struct S1 {
7476 template <class U> struct S2 {};
7477 template <class U> struct S2<U*> {};
7478 };
7479
7480 we will be called with an ARGLIST of `U*', but the
7481 TEMPLATE will be `template <class T> template
7482 <class U> struct S1<T>::S2'. We must fill in the missing
7483 arguments. */
7484 arglist
7485 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7486 arglist);
7487 arg_depth = TMPL_ARGS_DEPTH (arglist);
7488 }
7489
7490 /* Now we should have enough arguments. */
7491 gcc_assert (parm_depth == arg_depth);
7492
7493 /* From here on, we're only interested in the most general
7494 template. */
7495
7496 /* Calculate the BOUND_ARGS. These will be the args that are
7497 actually tsubst'd into the definition to create the
7498 instantiation. */
7499 if (parm_depth > 1)
7500 {
7501 /* We have multiple levels of arguments to coerce, at once. */
7502 int i;
7503 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7504
7505 tree bound_args = make_tree_vec (parm_depth);
7506
7507 for (i = saved_depth,
7508 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7509 i > 0 && t != NULL_TREE;
7510 --i, t = TREE_CHAIN (t))
7511 {
7512 tree a;
7513 if (i == saved_depth)
7514 a = coerce_template_parms (TREE_VALUE (t),
7515 arglist, gen_tmpl,
7516 complain,
7517 /*require_all_args=*/true,
7518 /*use_default_args=*/true);
7519 else
7520 /* Outer levels should have already been coerced. */
7521 a = TMPL_ARGS_LEVEL (arglist, i);
7522
7523 /* Don't process further if one of the levels fails. */
7524 if (a == error_mark_node)
7525 {
7526 /* Restore the ARGLIST to its full size. */
7527 TREE_VEC_LENGTH (arglist) = saved_depth;
7528 return error_mark_node;
7529 }
7530
7531 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7532
7533 /* We temporarily reduce the length of the ARGLIST so
7534 that coerce_template_parms will see only the arguments
7535 corresponding to the template parameters it is
7536 examining. */
7537 TREE_VEC_LENGTH (arglist)--;
7538 }
7539
7540 /* Restore the ARGLIST to its full size. */
7541 TREE_VEC_LENGTH (arglist) = saved_depth;
7542
7543 arglist = bound_args;
7544 }
7545 else
7546 arglist
7547 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7548 INNERMOST_TEMPLATE_ARGS (arglist),
7549 gen_tmpl,
7550 complain,
7551 /*require_all_args=*/true,
7552 /*use_default_args=*/true);
7553
7554 if (arglist == error_mark_node)
7555 /* We were unable to bind the arguments. */
7556 return error_mark_node;
7557
7558 /* In the scope of a template class, explicit references to the
7559 template class refer to the type of the template, not any
7560 instantiation of it. For example, in:
7561
7562 template <class T> class C { void f(C<T>); }
7563
7564 the `C<T>' is just the same as `C'. Outside of the
7565 class, however, such a reference is an instantiation. */
7566 if ((entering_scope
7567 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7568 || currently_open_class (template_type))
7569 /* comp_template_args is expensive, check it last. */
7570 && comp_template_args (TYPE_TI_ARGS (template_type),
7571 arglist))
7572 return template_type;
7573
7574 /* If we already have this specialization, return it. */
7575 elt.tmpl = gen_tmpl;
7576 elt.args = arglist;
7577 hash = hash_specialization (&elt);
7578 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7579 &elt, hash);
7580
7581 if (entry)
7582 return entry->spec;
7583
7584 is_dependent_type = uses_template_parms (arglist);
7585
7586 /* If the deduced arguments are invalid, then the binding
7587 failed. */
7588 if (!is_dependent_type
7589 && check_instantiated_args (gen_tmpl,
7590 INNERMOST_TEMPLATE_ARGS (arglist),
7591 complain))
7592 return error_mark_node;
7593
7594 if (!is_dependent_type
7595 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7596 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7597 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7598 {
7599 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7600 DECL_NAME (gen_tmpl),
7601 /*tag_scope=*/ts_global);
7602 return found;
7603 }
7604
7605 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7606 complain, in_decl);
7607 if (context == error_mark_node)
7608 return error_mark_node;
7609
7610 if (!context)
7611 context = global_namespace;
7612
7613 /* Create the type. */
7614 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7615 {
7616 /* The user referred to a specialization of an alias
7617 template represented by GEN_TMPL.
7618
7619 [temp.alias]/2 says:
7620
7621 When a template-id refers to the specialization of an
7622 alias template, it is equivalent to the associated
7623 type obtained by substitution of its
7624 template-arguments for the template-parameters in the
7625 type-id of the alias template. */
7626
7627 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7628 /* Note that the call above (by indirectly calling
7629 register_specialization in tsubst_decl) registers the
7630 TYPE_DECL representing the specialization of the alias
7631 template. So next time someone substitutes ARGLIST for
7632 the template parms into the alias template (GEN_TMPL),
7633 she'll get that TYPE_DECL back. */
7634
7635 if (t == error_mark_node)
7636 return t;
7637 }
7638 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7639 {
7640 if (!is_dependent_type)
7641 {
7642 set_current_access_from_decl (TYPE_NAME (template_type));
7643 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7644 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7645 arglist, complain, in_decl),
7646 SCOPED_ENUM_P (template_type), NULL);
7647
7648 if (t == error_mark_node)
7649 return t;
7650 }
7651 else
7652 {
7653 /* We don't want to call start_enum for this type, since
7654 the values for the enumeration constants may involve
7655 template parameters. And, no one should be interested
7656 in the enumeration constants for such a type. */
7657 t = cxx_make_type (ENUMERAL_TYPE);
7658 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7659 }
7660 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7661 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7662 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7663 }
7664 else if (CLASS_TYPE_P (template_type))
7665 {
7666 t = make_class_type (TREE_CODE (template_type));
7667 CLASSTYPE_DECLARED_CLASS (t)
7668 = CLASSTYPE_DECLARED_CLASS (template_type);
7669 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7670 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7671
7672 /* A local class. Make sure the decl gets registered properly. */
7673 if (context == current_function_decl)
7674 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7675
7676 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7677 /* This instantiation is another name for the primary
7678 template type. Set the TYPE_CANONICAL field
7679 appropriately. */
7680 TYPE_CANONICAL (t) = template_type;
7681 else if (any_template_arguments_need_structural_equality_p (arglist))
7682 /* Some of the template arguments require structural
7683 equality testing, so this template class requires
7684 structural equality testing. */
7685 SET_TYPE_STRUCTURAL_EQUALITY (t);
7686 }
7687 else
7688 gcc_unreachable ();
7689
7690 /* If we called start_enum or pushtag above, this information
7691 will already be set up. */
7692 if (!TYPE_NAME (t))
7693 {
7694 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7695
7696 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7697 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7698 DECL_SOURCE_LOCATION (type_decl)
7699 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7700 }
7701 else
7702 type_decl = TYPE_NAME (t);
7703
7704 if (CLASS_TYPE_P (template_type))
7705 {
7706 TREE_PRIVATE (type_decl)
7707 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7708 TREE_PROTECTED (type_decl)
7709 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7710 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7711 {
7712 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7713 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7714 }
7715 }
7716
7717 /* Let's consider the explicit specialization of a member
7718 of a class template specialization that is implicitly instantiated,
7719 e.g.:
7720 template<class T>
7721 struct S
7722 {
7723 template<class U> struct M {}; //#0
7724 };
7725
7726 template<>
7727 template<>
7728 struct S<int>::M<char> //#1
7729 {
7730 int i;
7731 };
7732 [temp.expl.spec]/4 says this is valid.
7733
7734 In this case, when we write:
7735 S<int>::M<char> m;
7736
7737 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7738 the one of #0.
7739
7740 When we encounter #1, we want to store the partial instantiation
7741 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7742
7743 For all cases other than this "explicit specialization of member of a
7744 class template", we just want to store the most general template into
7745 the CLASSTYPE_TI_TEMPLATE of M.
7746
7747 This case of "explicit specialization of member of a class template"
7748 only happens when:
7749 1/ the enclosing class is an instantiation of, and therefore not
7750 the same as, the context of the most general template, and
7751 2/ we aren't looking at the partial instantiation itself, i.e.
7752 the innermost arguments are not the same as the innermost parms of
7753 the most general template.
7754
7755 So it's only when 1/ and 2/ happens that we want to use the partial
7756 instantiation of the member template in lieu of its most general
7757 template. */
7758
7759 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7760 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7761 /* the enclosing class must be an instantiation... */
7762 && CLASS_TYPE_P (context)
7763 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7764 {
7765 tree partial_inst_args;
7766 TREE_VEC_LENGTH (arglist)--;
7767 ++processing_template_decl;
7768 partial_inst_args =
7769 tsubst (INNERMOST_TEMPLATE_ARGS
7770 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7771 arglist, complain, NULL_TREE);
7772 --processing_template_decl;
7773 TREE_VEC_LENGTH (arglist)++;
7774 use_partial_inst_tmpl =
7775 /*...and we must not be looking at the partial instantiation
7776 itself. */
7777 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7778 partial_inst_args);
7779 }
7780
7781 if (!use_partial_inst_tmpl)
7782 /* This case is easy; there are no member templates involved. */
7783 found = gen_tmpl;
7784 else
7785 {
7786 /* This is a full instantiation of a member template. Find
7787 the partial instantiation of which this is an instance. */
7788
7789 /* Temporarily reduce by one the number of levels in the ARGLIST
7790 so as to avoid comparing the last set of arguments. */
7791 TREE_VEC_LENGTH (arglist)--;
7792 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7793 TREE_VEC_LENGTH (arglist)++;
7794 /* FOUND is either a proper class type, or an alias
7795 template specialization. In the later case, it's a
7796 TYPE_DECL, resulting from the substituting of arguments
7797 for parameters in the TYPE_DECL of the alias template
7798 done earlier. So be careful while getting the template
7799 of FOUND. */
7800 found = TREE_CODE (found) == TYPE_DECL
7801 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7802 : CLASSTYPE_TI_TEMPLATE (found);
7803 }
7804
7805 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7806
7807 elt.spec = t;
7808 slot = htab_find_slot_with_hash (type_specializations,
7809 &elt, hash, INSERT);
7810 entry = ggc_alloc_spec_entry ();
7811 *entry = elt;
7812 *slot = entry;
7813
7814 /* Note this use of the partial instantiation so we can check it
7815 later in maybe_process_partial_specialization. */
7816 DECL_TEMPLATE_INSTANTIATIONS (found)
7817 = tree_cons (arglist, t,
7818 DECL_TEMPLATE_INSTANTIATIONS (found));
7819
7820 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7821 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7822 /* Now that the type has been registered on the instantiations
7823 list, we set up the enumerators. Because the enumeration
7824 constants may involve the enumeration type itself, we make
7825 sure to register the type first, and then create the
7826 constants. That way, doing tsubst_expr for the enumeration
7827 constants won't result in recursive calls here; we'll find
7828 the instantiation and exit above. */
7829 tsubst_enum (template_type, t, arglist);
7830
7831 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7832 /* If the type makes use of template parameters, the
7833 code that generates debugging information will crash. */
7834 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7835
7836 /* Possibly limit visibility based on template args. */
7837 TREE_PUBLIC (type_decl) = 1;
7838 determine_visibility (type_decl);
7839
7840 return t;
7841 }
7842 }
7843
7844 /* Wrapper for lookup_template_class_1. */
7845
7846 tree
7847 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7848 int entering_scope, tsubst_flags_t complain)
7849 {
7850 tree ret;
7851 timevar_push (TV_TEMPLATE_INST);
7852 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7853 entering_scope, complain);
7854 timevar_pop (TV_TEMPLATE_INST);
7855 return ret;
7856 }
7857 \f
7858 struct pair_fn_data
7859 {
7860 tree_fn_t fn;
7861 void *data;
7862 /* True when we should also visit template parameters that occur in
7863 non-deduced contexts. */
7864 bool include_nondeduced_p;
7865 struct pointer_set_t *visited;
7866 };
7867
7868 /* Called from for_each_template_parm via walk_tree. */
7869
7870 static tree
7871 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7872 {
7873 tree t = *tp;
7874 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7875 tree_fn_t fn = pfd->fn;
7876 void *data = pfd->data;
7877
7878 if (TYPE_P (t)
7879 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7880 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7881 pfd->include_nondeduced_p))
7882 return error_mark_node;
7883
7884 switch (TREE_CODE (t))
7885 {
7886 case RECORD_TYPE:
7887 if (TYPE_PTRMEMFUNC_P (t))
7888 break;
7889 /* Fall through. */
7890
7891 case UNION_TYPE:
7892 case ENUMERAL_TYPE:
7893 if (!TYPE_TEMPLATE_INFO (t))
7894 *walk_subtrees = 0;
7895 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7896 fn, data, pfd->visited,
7897 pfd->include_nondeduced_p))
7898 return error_mark_node;
7899 break;
7900
7901 case INTEGER_TYPE:
7902 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7903 fn, data, pfd->visited,
7904 pfd->include_nondeduced_p)
7905 || for_each_template_parm (TYPE_MAX_VALUE (t),
7906 fn, data, pfd->visited,
7907 pfd->include_nondeduced_p))
7908 return error_mark_node;
7909 break;
7910
7911 case METHOD_TYPE:
7912 /* Since we're not going to walk subtrees, we have to do this
7913 explicitly here. */
7914 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7915 pfd->visited, pfd->include_nondeduced_p))
7916 return error_mark_node;
7917 /* Fall through. */
7918
7919 case FUNCTION_TYPE:
7920 /* Check the return type. */
7921 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7922 pfd->include_nondeduced_p))
7923 return error_mark_node;
7924
7925 /* Check the parameter types. Since default arguments are not
7926 instantiated until they are needed, the TYPE_ARG_TYPES may
7927 contain expressions that involve template parameters. But,
7928 no-one should be looking at them yet. And, once they're
7929 instantiated, they don't contain template parameters, so
7930 there's no point in looking at them then, either. */
7931 {
7932 tree parm;
7933
7934 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7935 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7936 pfd->visited, pfd->include_nondeduced_p))
7937 return error_mark_node;
7938
7939 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7940 want walk_tree walking into them itself. */
7941 *walk_subtrees = 0;
7942 }
7943 break;
7944
7945 case TYPEOF_TYPE:
7946 case UNDERLYING_TYPE:
7947 if (pfd->include_nondeduced_p
7948 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7949 pfd->visited,
7950 pfd->include_nondeduced_p))
7951 return error_mark_node;
7952 break;
7953
7954 case FUNCTION_DECL:
7955 case VAR_DECL:
7956 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7957 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7958 pfd->visited, pfd->include_nondeduced_p))
7959 return error_mark_node;
7960 /* Fall through. */
7961
7962 case PARM_DECL:
7963 case CONST_DECL:
7964 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7965 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7966 pfd->visited, pfd->include_nondeduced_p))
7967 return error_mark_node;
7968 if (DECL_CONTEXT (t)
7969 && pfd->include_nondeduced_p
7970 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7971 pfd->visited, pfd->include_nondeduced_p))
7972 return error_mark_node;
7973 break;
7974
7975 case BOUND_TEMPLATE_TEMPLATE_PARM:
7976 /* Record template parameters such as `T' inside `TT<T>'. */
7977 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7978 pfd->include_nondeduced_p))
7979 return error_mark_node;
7980 /* Fall through. */
7981
7982 case TEMPLATE_TEMPLATE_PARM:
7983 case TEMPLATE_TYPE_PARM:
7984 case TEMPLATE_PARM_INDEX:
7985 if (fn && (*fn)(t, data))
7986 return error_mark_node;
7987 else if (!fn)
7988 return error_mark_node;
7989 break;
7990
7991 case TEMPLATE_DECL:
7992 /* A template template parameter is encountered. */
7993 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7994 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7995 pfd->include_nondeduced_p))
7996 return error_mark_node;
7997
7998 /* Already substituted template template parameter */
7999 *walk_subtrees = 0;
8000 break;
8001
8002 case TYPENAME_TYPE:
8003 if (!fn
8004 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8005 data, pfd->visited,
8006 pfd->include_nondeduced_p))
8007 return error_mark_node;
8008 break;
8009
8010 case CONSTRUCTOR:
8011 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8012 && pfd->include_nondeduced_p
8013 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8014 (TREE_TYPE (t)), fn, data,
8015 pfd->visited, pfd->include_nondeduced_p))
8016 return error_mark_node;
8017 break;
8018
8019 case INDIRECT_REF:
8020 case COMPONENT_REF:
8021 /* If there's no type, then this thing must be some expression
8022 involving template parameters. */
8023 if (!fn && !TREE_TYPE (t))
8024 return error_mark_node;
8025 break;
8026
8027 case MODOP_EXPR:
8028 case CAST_EXPR:
8029 case IMPLICIT_CONV_EXPR:
8030 case REINTERPRET_CAST_EXPR:
8031 case CONST_CAST_EXPR:
8032 case STATIC_CAST_EXPR:
8033 case DYNAMIC_CAST_EXPR:
8034 case ARROW_EXPR:
8035 case DOTSTAR_EXPR:
8036 case TYPEID_EXPR:
8037 case PSEUDO_DTOR_EXPR:
8038 if (!fn)
8039 return error_mark_node;
8040 break;
8041
8042 default:
8043 break;
8044 }
8045
8046 /* We didn't find any template parameters we liked. */
8047 return NULL_TREE;
8048 }
8049
8050 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8051 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8052 call FN with the parameter and the DATA.
8053 If FN returns nonzero, the iteration is terminated, and
8054 for_each_template_parm returns 1. Otherwise, the iteration
8055 continues. If FN never returns a nonzero value, the value
8056 returned by for_each_template_parm is 0. If FN is NULL, it is
8057 considered to be the function which always returns 1.
8058
8059 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8060 parameters that occur in non-deduced contexts. When false, only
8061 visits those template parameters that can be deduced. */
8062
8063 static int
8064 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8065 struct pointer_set_t *visited,
8066 bool include_nondeduced_p)
8067 {
8068 struct pair_fn_data pfd;
8069 int result;
8070
8071 /* Set up. */
8072 pfd.fn = fn;
8073 pfd.data = data;
8074 pfd.include_nondeduced_p = include_nondeduced_p;
8075
8076 /* Walk the tree. (Conceptually, we would like to walk without
8077 duplicates, but for_each_template_parm_r recursively calls
8078 for_each_template_parm, so we would need to reorganize a fair
8079 bit to use walk_tree_without_duplicates, so we keep our own
8080 visited list.) */
8081 if (visited)
8082 pfd.visited = visited;
8083 else
8084 pfd.visited = pointer_set_create ();
8085 result = cp_walk_tree (&t,
8086 for_each_template_parm_r,
8087 &pfd,
8088 pfd.visited) != NULL_TREE;
8089
8090 /* Clean up. */
8091 if (!visited)
8092 {
8093 pointer_set_destroy (pfd.visited);
8094 pfd.visited = 0;
8095 }
8096
8097 return result;
8098 }
8099
8100 /* Returns true if T depends on any template parameter. */
8101
8102 int
8103 uses_template_parms (tree t)
8104 {
8105 bool dependent_p;
8106 int saved_processing_template_decl;
8107
8108 saved_processing_template_decl = processing_template_decl;
8109 if (!saved_processing_template_decl)
8110 processing_template_decl = 1;
8111 if (TYPE_P (t))
8112 dependent_p = dependent_type_p (t);
8113 else if (TREE_CODE (t) == TREE_VEC)
8114 dependent_p = any_dependent_template_arguments_p (t);
8115 else if (TREE_CODE (t) == TREE_LIST)
8116 dependent_p = (uses_template_parms (TREE_VALUE (t))
8117 || uses_template_parms (TREE_CHAIN (t)));
8118 else if (TREE_CODE (t) == TYPE_DECL)
8119 dependent_p = dependent_type_p (TREE_TYPE (t));
8120 else if (DECL_P (t)
8121 || EXPR_P (t)
8122 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8123 || TREE_CODE (t) == OVERLOAD
8124 || BASELINK_P (t)
8125 || identifier_p (t)
8126 || TREE_CODE (t) == TRAIT_EXPR
8127 || TREE_CODE (t) == CONSTRUCTOR
8128 || CONSTANT_CLASS_P (t))
8129 dependent_p = (type_dependent_expression_p (t)
8130 || value_dependent_expression_p (t));
8131 else
8132 {
8133 gcc_assert (t == error_mark_node);
8134 dependent_p = false;
8135 }
8136
8137 processing_template_decl = saved_processing_template_decl;
8138
8139 return dependent_p;
8140 }
8141
8142 /* Returns true iff current_function_decl is an incompletely instantiated
8143 template. Useful instead of processing_template_decl because the latter
8144 is set to 0 during fold_non_dependent_expr. */
8145
8146 bool
8147 in_template_function (void)
8148 {
8149 tree fn = current_function_decl;
8150 bool ret;
8151 ++processing_template_decl;
8152 ret = (fn && DECL_LANG_SPECIFIC (fn)
8153 && DECL_TEMPLATE_INFO (fn)
8154 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8155 --processing_template_decl;
8156 return ret;
8157 }
8158
8159 /* Returns true if T depends on any template parameter with level LEVEL. */
8160
8161 int
8162 uses_template_parms_level (tree t, int level)
8163 {
8164 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8165 /*include_nondeduced_p=*/true);
8166 }
8167
8168 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8169 ill-formed translation unit, i.e. a variable or function that isn't
8170 usable in a constant expression. */
8171
8172 static inline bool
8173 neglectable_inst_p (tree d)
8174 {
8175 return (DECL_P (d)
8176 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8177 : decl_maybe_constant_var_p (d)));
8178 }
8179
8180 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8181 neglectable and instantiated from within an erroneous instantiation. */
8182
8183 static bool
8184 limit_bad_template_recursion (tree decl)
8185 {
8186 struct tinst_level *lev = current_tinst_level;
8187 int errs = errorcount + sorrycount;
8188 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8189 return false;
8190
8191 for (; lev; lev = lev->next)
8192 if (neglectable_inst_p (lev->decl))
8193 break;
8194
8195 return (lev && errs > lev->errors);
8196 }
8197
8198 static int tinst_depth;
8199 extern int max_tinst_depth;
8200 int depth_reached;
8201
8202 static GTY(()) struct tinst_level *last_error_tinst_level;
8203
8204 /* We're starting to instantiate D; record the template instantiation context
8205 for diagnostics and to restore it later. */
8206
8207 int
8208 push_tinst_level (tree d)
8209 {
8210 struct tinst_level *new_level;
8211
8212 if (tinst_depth >= max_tinst_depth)
8213 {
8214 last_error_tinst_level = current_tinst_level;
8215 if (TREE_CODE (d) == TREE_LIST)
8216 error ("template instantiation depth exceeds maximum of %d (use "
8217 "-ftemplate-depth= to increase the maximum) substituting %qS",
8218 max_tinst_depth, d);
8219 else
8220 error ("template instantiation depth exceeds maximum of %d (use "
8221 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8222 max_tinst_depth, d);
8223
8224 print_instantiation_context ();
8225
8226 return 0;
8227 }
8228
8229 /* If the current instantiation caused problems, don't let it instantiate
8230 anything else. Do allow deduction substitution and decls usable in
8231 constant expressions. */
8232 if (limit_bad_template_recursion (d))
8233 return 0;
8234
8235 new_level = ggc_alloc_tinst_level ();
8236 new_level->decl = d;
8237 new_level->locus = input_location;
8238 new_level->errors = errorcount+sorrycount;
8239 new_level->in_system_header_p = in_system_header_at (input_location);
8240 new_level->next = current_tinst_level;
8241 current_tinst_level = new_level;
8242
8243 ++tinst_depth;
8244 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8245 depth_reached = tinst_depth;
8246
8247 return 1;
8248 }
8249
8250 /* We're done instantiating this template; return to the instantiation
8251 context. */
8252
8253 void
8254 pop_tinst_level (void)
8255 {
8256 /* Restore the filename and line number stashed away when we started
8257 this instantiation. */
8258 input_location = current_tinst_level->locus;
8259 current_tinst_level = current_tinst_level->next;
8260 --tinst_depth;
8261 }
8262
8263 /* We're instantiating a deferred template; restore the template
8264 instantiation context in which the instantiation was requested, which
8265 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8266
8267 static tree
8268 reopen_tinst_level (struct tinst_level *level)
8269 {
8270 struct tinst_level *t;
8271
8272 tinst_depth = 0;
8273 for (t = level; t; t = t->next)
8274 ++tinst_depth;
8275
8276 current_tinst_level = level;
8277 pop_tinst_level ();
8278 if (current_tinst_level)
8279 current_tinst_level->errors = errorcount+sorrycount;
8280 return level->decl;
8281 }
8282
8283 /* Returns the TINST_LEVEL which gives the original instantiation
8284 context. */
8285
8286 struct tinst_level *
8287 outermost_tinst_level (void)
8288 {
8289 struct tinst_level *level = current_tinst_level;
8290 if (level)
8291 while (level->next)
8292 level = level->next;
8293 return level;
8294 }
8295
8296 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8297 vector of template arguments, as for tsubst.
8298
8299 Returns an appropriate tsubst'd friend declaration. */
8300
8301 static tree
8302 tsubst_friend_function (tree decl, tree args)
8303 {
8304 tree new_friend;
8305
8306 if (TREE_CODE (decl) == FUNCTION_DECL
8307 && DECL_TEMPLATE_INSTANTIATION (decl)
8308 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8309 /* This was a friend declared with an explicit template
8310 argument list, e.g.:
8311
8312 friend void f<>(T);
8313
8314 to indicate that f was a template instantiation, not a new
8315 function declaration. Now, we have to figure out what
8316 instantiation of what template. */
8317 {
8318 tree template_id, arglist, fns;
8319 tree new_args;
8320 tree tmpl;
8321 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8322
8323 /* Friend functions are looked up in the containing namespace scope.
8324 We must enter that scope, to avoid finding member functions of the
8325 current class with same name. */
8326 push_nested_namespace (ns);
8327 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8328 tf_warning_or_error, NULL_TREE,
8329 /*integral_constant_expression_p=*/false);
8330 pop_nested_namespace (ns);
8331 arglist = tsubst (DECL_TI_ARGS (decl), args,
8332 tf_warning_or_error, NULL_TREE);
8333 template_id = lookup_template_function (fns, arglist);
8334
8335 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8336 tmpl = determine_specialization (template_id, new_friend,
8337 &new_args,
8338 /*need_member_template=*/0,
8339 TREE_VEC_LENGTH (args),
8340 tsk_none);
8341 return instantiate_template (tmpl, new_args, tf_error);
8342 }
8343
8344 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8345
8346 /* The NEW_FRIEND will look like an instantiation, to the
8347 compiler, but is not an instantiation from the point of view of
8348 the language. For example, we might have had:
8349
8350 template <class T> struct S {
8351 template <class U> friend void f(T, U);
8352 };
8353
8354 Then, in S<int>, template <class U> void f(int, U) is not an
8355 instantiation of anything. */
8356 if (new_friend == error_mark_node)
8357 return error_mark_node;
8358
8359 DECL_USE_TEMPLATE (new_friend) = 0;
8360 if (TREE_CODE (decl) == TEMPLATE_DECL)
8361 {
8362 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8363 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8364 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8365 }
8366
8367 /* The mangled name for the NEW_FRIEND is incorrect. The function
8368 is not a template instantiation and should not be mangled like
8369 one. Therefore, we forget the mangling here; we'll recompute it
8370 later if we need it. */
8371 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8372 {
8373 SET_DECL_RTL (new_friend, NULL);
8374 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8375 }
8376
8377 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8378 {
8379 tree old_decl;
8380 tree new_friend_template_info;
8381 tree new_friend_result_template_info;
8382 tree ns;
8383 int new_friend_is_defn;
8384
8385 /* We must save some information from NEW_FRIEND before calling
8386 duplicate decls since that function will free NEW_FRIEND if
8387 possible. */
8388 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8389 new_friend_is_defn =
8390 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8391 (template_for_substitution (new_friend)))
8392 != NULL_TREE);
8393 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8394 {
8395 /* This declaration is a `primary' template. */
8396 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8397
8398 new_friend_result_template_info
8399 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8400 }
8401 else
8402 new_friend_result_template_info = NULL_TREE;
8403
8404 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8405 if (new_friend_is_defn)
8406 DECL_INITIAL (new_friend) = error_mark_node;
8407
8408 /* Inside pushdecl_namespace_level, we will push into the
8409 current namespace. However, the friend function should go
8410 into the namespace of the template. */
8411 ns = decl_namespace_context (new_friend);
8412 push_nested_namespace (ns);
8413 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8414 pop_nested_namespace (ns);
8415
8416 if (old_decl == error_mark_node)
8417 return error_mark_node;
8418
8419 if (old_decl != new_friend)
8420 {
8421 /* This new friend declaration matched an existing
8422 declaration. For example, given:
8423
8424 template <class T> void f(T);
8425 template <class U> class C {
8426 template <class T> friend void f(T) {}
8427 };
8428
8429 the friend declaration actually provides the definition
8430 of `f', once C has been instantiated for some type. So,
8431 old_decl will be the out-of-class template declaration,
8432 while new_friend is the in-class definition.
8433
8434 But, if `f' was called before this point, the
8435 instantiation of `f' will have DECL_TI_ARGS corresponding
8436 to `T' but not to `U', references to which might appear
8437 in the definition of `f'. Previously, the most general
8438 template for an instantiation of `f' was the out-of-class
8439 version; now it is the in-class version. Therefore, we
8440 run through all specialization of `f', adding to their
8441 DECL_TI_ARGS appropriately. In particular, they need a
8442 new set of outer arguments, corresponding to the
8443 arguments for this class instantiation.
8444
8445 The same situation can arise with something like this:
8446
8447 friend void f(int);
8448 template <class T> class C {
8449 friend void f(T) {}
8450 };
8451
8452 when `C<int>' is instantiated. Now, `f(int)' is defined
8453 in the class. */
8454
8455 if (!new_friend_is_defn)
8456 /* On the other hand, if the in-class declaration does
8457 *not* provide a definition, then we don't want to alter
8458 existing definitions. We can just leave everything
8459 alone. */
8460 ;
8461 else
8462 {
8463 tree new_template = TI_TEMPLATE (new_friend_template_info);
8464 tree new_args = TI_ARGS (new_friend_template_info);
8465
8466 /* Overwrite whatever template info was there before, if
8467 any, with the new template information pertaining to
8468 the declaration. */
8469 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8470
8471 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8472 {
8473 /* We should have called reregister_specialization in
8474 duplicate_decls. */
8475 gcc_assert (retrieve_specialization (new_template,
8476 new_args, 0)
8477 == old_decl);
8478
8479 /* Instantiate it if the global has already been used. */
8480 if (DECL_ODR_USED (old_decl))
8481 instantiate_decl (old_decl, /*defer_ok=*/true,
8482 /*expl_inst_class_mem_p=*/false);
8483 }
8484 else
8485 {
8486 tree t;
8487
8488 /* Indicate that the old function template is a partial
8489 instantiation. */
8490 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8491 = new_friend_result_template_info;
8492
8493 gcc_assert (new_template
8494 == most_general_template (new_template));
8495 gcc_assert (new_template != old_decl);
8496
8497 /* Reassign any specializations already in the hash table
8498 to the new more general template, and add the
8499 additional template args. */
8500 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8501 t != NULL_TREE;
8502 t = TREE_CHAIN (t))
8503 {
8504 tree spec = TREE_VALUE (t);
8505 spec_entry elt;
8506
8507 elt.tmpl = old_decl;
8508 elt.args = DECL_TI_ARGS (spec);
8509 elt.spec = NULL_TREE;
8510
8511 htab_remove_elt (decl_specializations, &elt);
8512
8513 DECL_TI_ARGS (spec)
8514 = add_outermost_template_args (new_args,
8515 DECL_TI_ARGS (spec));
8516
8517 register_specialization
8518 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8519
8520 }
8521 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8522 }
8523 }
8524
8525 /* The information from NEW_FRIEND has been merged into OLD_DECL
8526 by duplicate_decls. */
8527 new_friend = old_decl;
8528 }
8529 }
8530 else
8531 {
8532 tree context = DECL_CONTEXT (new_friend);
8533 bool dependent_p;
8534
8535 /* In the code
8536 template <class T> class C {
8537 template <class U> friend void C1<U>::f (); // case 1
8538 friend void C2<T>::f (); // case 2
8539 };
8540 we only need to make sure CONTEXT is a complete type for
8541 case 2. To distinguish between the two cases, we note that
8542 CONTEXT of case 1 remains dependent type after tsubst while
8543 this isn't true for case 2. */
8544 ++processing_template_decl;
8545 dependent_p = dependent_type_p (context);
8546 --processing_template_decl;
8547
8548 if (!dependent_p
8549 && !complete_type_or_else (context, NULL_TREE))
8550 return error_mark_node;
8551
8552 if (COMPLETE_TYPE_P (context))
8553 {
8554 tree fn = new_friend;
8555 /* do_friend adds the TEMPLATE_DECL for any member friend
8556 template even if it isn't a member template, i.e.
8557 template <class T> friend A<T>::f();
8558 Look through it in that case. */
8559 if (TREE_CODE (fn) == TEMPLATE_DECL
8560 && !PRIMARY_TEMPLATE_P (fn))
8561 fn = DECL_TEMPLATE_RESULT (fn);
8562 /* Check to see that the declaration is really present, and,
8563 possibly obtain an improved declaration. */
8564 fn = check_classfn (context, fn, NULL_TREE);
8565
8566 if (fn)
8567 new_friend = fn;
8568 }
8569 }
8570
8571 return new_friend;
8572 }
8573
8574 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8575 template arguments, as for tsubst.
8576
8577 Returns an appropriate tsubst'd friend type or error_mark_node on
8578 failure. */
8579
8580 static tree
8581 tsubst_friend_class (tree friend_tmpl, tree args)
8582 {
8583 tree friend_type;
8584 tree tmpl;
8585 tree context;
8586
8587 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8588 {
8589 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8590 return TREE_TYPE (t);
8591 }
8592
8593 context = CP_DECL_CONTEXT (friend_tmpl);
8594
8595 if (context != global_namespace)
8596 {
8597 if (TREE_CODE (context) == NAMESPACE_DECL)
8598 push_nested_namespace (context);
8599 else
8600 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8601 }
8602
8603 /* Look for a class template declaration. We look for hidden names
8604 because two friend declarations of the same template are the
8605 same. For example, in:
8606
8607 struct A {
8608 template <typename> friend class F;
8609 };
8610 template <typename> struct B {
8611 template <typename> friend class F;
8612 };
8613
8614 both F templates are the same. */
8615 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8616 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8617
8618 /* But, if we don't find one, it might be because we're in a
8619 situation like this:
8620
8621 template <class T>
8622 struct S {
8623 template <class U>
8624 friend struct S;
8625 };
8626
8627 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8628 for `S<int>', not the TEMPLATE_DECL. */
8629 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8630 {
8631 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8632 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8633 }
8634
8635 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8636 {
8637 /* The friend template has already been declared. Just
8638 check to see that the declarations match, and install any new
8639 default parameters. We must tsubst the default parameters,
8640 of course. We only need the innermost template parameters
8641 because that is all that redeclare_class_template will look
8642 at. */
8643 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8644 > TMPL_ARGS_DEPTH (args))
8645 {
8646 tree parms;
8647 location_t saved_input_location;
8648 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8649 args, tf_warning_or_error);
8650
8651 saved_input_location = input_location;
8652 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8653 redeclare_class_template (TREE_TYPE (tmpl), parms);
8654 input_location = saved_input_location;
8655
8656 }
8657
8658 friend_type = TREE_TYPE (tmpl);
8659 }
8660 else
8661 {
8662 /* The friend template has not already been declared. In this
8663 case, the instantiation of the template class will cause the
8664 injection of this template into the global scope. */
8665 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8666 if (tmpl == error_mark_node)
8667 return error_mark_node;
8668
8669 /* The new TMPL is not an instantiation of anything, so we
8670 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8671 the new type because that is supposed to be the corresponding
8672 template decl, i.e., TMPL. */
8673 DECL_USE_TEMPLATE (tmpl) = 0;
8674 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8675 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8676 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8677 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8678
8679 /* Inject this template into the global scope. */
8680 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8681 }
8682
8683 if (context != global_namespace)
8684 {
8685 if (TREE_CODE (context) == NAMESPACE_DECL)
8686 pop_nested_namespace (context);
8687 else
8688 pop_nested_class ();
8689 }
8690
8691 return friend_type;
8692 }
8693
8694 /* Returns zero if TYPE cannot be completed later due to circularity.
8695 Otherwise returns one. */
8696
8697 static int
8698 can_complete_type_without_circularity (tree type)
8699 {
8700 if (type == NULL_TREE || type == error_mark_node)
8701 return 0;
8702 else if (COMPLETE_TYPE_P (type))
8703 return 1;
8704 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8705 return can_complete_type_without_circularity (TREE_TYPE (type));
8706 else if (CLASS_TYPE_P (type)
8707 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8708 return 0;
8709 else
8710 return 1;
8711 }
8712
8713 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8714
8715 /* Apply any attributes which had to be deferred until instantiation
8716 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8717 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8718
8719 static void
8720 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8721 tree args, tsubst_flags_t complain, tree in_decl)
8722 {
8723 tree last_dep = NULL_TREE;
8724 tree t;
8725 tree *p;
8726
8727 for (t = attributes; t; t = TREE_CHAIN (t))
8728 if (ATTR_IS_DEPENDENT (t))
8729 {
8730 last_dep = t;
8731 attributes = copy_list (attributes);
8732 break;
8733 }
8734
8735 if (DECL_P (*decl_p))
8736 {
8737 if (TREE_TYPE (*decl_p) == error_mark_node)
8738 return;
8739 p = &DECL_ATTRIBUTES (*decl_p);
8740 }
8741 else
8742 p = &TYPE_ATTRIBUTES (*decl_p);
8743
8744 if (last_dep)
8745 {
8746 tree late_attrs = NULL_TREE;
8747 tree *q = &late_attrs;
8748
8749 for (*p = attributes; *p; )
8750 {
8751 t = *p;
8752 if (ATTR_IS_DEPENDENT (t))
8753 {
8754 *p = TREE_CHAIN (t);
8755 TREE_CHAIN (t) = NULL_TREE;
8756 if ((flag_openmp || flag_cilkplus)
8757 && is_attribute_p ("omp declare simd",
8758 get_attribute_name (t))
8759 && TREE_VALUE (t))
8760 {
8761 tree clauses = TREE_VALUE (TREE_VALUE (t));
8762 clauses = tsubst_omp_clauses (clauses, true, args,
8763 complain, in_decl);
8764 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8765 clauses = finish_omp_clauses (clauses);
8766 tree parms = DECL_ARGUMENTS (*decl_p);
8767 clauses
8768 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8769 if (clauses)
8770 TREE_VALUE (TREE_VALUE (t)) = clauses;
8771 else
8772 TREE_VALUE (t) = NULL_TREE;
8773 }
8774 /* If the first attribute argument is an identifier, don't
8775 pass it through tsubst. Attributes like mode, format,
8776 cleanup and several target specific attributes expect it
8777 unmodified. */
8778 else if (attribute_takes_identifier_p (get_attribute_name (t))
8779 && TREE_VALUE (t))
8780 {
8781 tree chain
8782 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8783 in_decl,
8784 /*integral_constant_expression_p=*/false);
8785 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8786 TREE_VALUE (t)
8787 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8788 chain);
8789 }
8790 else
8791 TREE_VALUE (t)
8792 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8793 /*integral_constant_expression_p=*/false);
8794 *q = t;
8795 q = &TREE_CHAIN (t);
8796 }
8797 else
8798 p = &TREE_CHAIN (t);
8799 }
8800
8801 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8802 }
8803 }
8804
8805 /* Perform (or defer) access check for typedefs that were referenced
8806 from within the template TMPL code.
8807 This is a subroutine of instantiate_decl and instantiate_class_template.
8808 TMPL is the template to consider and TARGS is the list of arguments of
8809 that template. */
8810
8811 static void
8812 perform_typedefs_access_check (tree tmpl, tree targs)
8813 {
8814 location_t saved_location;
8815 unsigned i;
8816 qualified_typedef_usage_t *iter;
8817
8818 if (!tmpl
8819 || (!CLASS_TYPE_P (tmpl)
8820 && TREE_CODE (tmpl) != FUNCTION_DECL))
8821 return;
8822
8823 saved_location = input_location;
8824 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8825 {
8826 tree type_decl = iter->typedef_decl;
8827 tree type_scope = iter->context;
8828
8829 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8830 continue;
8831
8832 if (uses_template_parms (type_decl))
8833 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8834 if (uses_template_parms (type_scope))
8835 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8836
8837 /* Make access check error messages point to the location
8838 of the use of the typedef. */
8839 input_location = iter->locus;
8840 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8841 type_decl, type_decl,
8842 tf_warning_or_error);
8843 }
8844 input_location = saved_location;
8845 }
8846
8847 static tree
8848 instantiate_class_template_1 (tree type)
8849 {
8850 tree templ, args, pattern, t, member;
8851 tree typedecl;
8852 tree pbinfo;
8853 tree base_list;
8854 unsigned int saved_maximum_field_alignment;
8855 tree fn_context;
8856
8857 if (type == error_mark_node)
8858 return error_mark_node;
8859
8860 if (COMPLETE_OR_OPEN_TYPE_P (type)
8861 || uses_template_parms (type))
8862 return type;
8863
8864 /* Figure out which template is being instantiated. */
8865 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8866 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8867
8868 /* Determine what specialization of the original template to
8869 instantiate. */
8870 t = most_specialized_class (type, tf_warning_or_error);
8871 if (t == error_mark_node)
8872 {
8873 TYPE_BEING_DEFINED (type) = 1;
8874 return error_mark_node;
8875 }
8876 else if (t)
8877 {
8878 /* This TYPE is actually an instantiation of a partial
8879 specialization. We replace the innermost set of ARGS with
8880 the arguments appropriate for substitution. For example,
8881 given:
8882
8883 template <class T> struct S {};
8884 template <class T> struct S<T*> {};
8885
8886 and supposing that we are instantiating S<int*>, ARGS will
8887 presently be {int*} -- but we need {int}. */
8888 pattern = TREE_TYPE (t);
8889 args = TREE_PURPOSE (t);
8890 }
8891 else
8892 {
8893 pattern = TREE_TYPE (templ);
8894 args = CLASSTYPE_TI_ARGS (type);
8895 }
8896
8897 /* If the template we're instantiating is incomplete, then clearly
8898 there's nothing we can do. */
8899 if (!COMPLETE_TYPE_P (pattern))
8900 return type;
8901
8902 /* If we've recursively instantiated too many templates, stop. */
8903 if (! push_tinst_level (type))
8904 return type;
8905
8906 /* Now we're really doing the instantiation. Mark the type as in
8907 the process of being defined. */
8908 TYPE_BEING_DEFINED (type) = 1;
8909
8910 /* We may be in the middle of deferred access check. Disable
8911 it now. */
8912 push_deferring_access_checks (dk_no_deferred);
8913
8914 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8915 if (!fn_context)
8916 push_to_top_level ();
8917 /* Use #pragma pack from the template context. */
8918 saved_maximum_field_alignment = maximum_field_alignment;
8919 maximum_field_alignment = TYPE_PRECISION (pattern);
8920
8921 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8922
8923 /* Set the input location to the most specialized template definition.
8924 This is needed if tsubsting causes an error. */
8925 typedecl = TYPE_MAIN_DECL (pattern);
8926 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8927 DECL_SOURCE_LOCATION (typedecl);
8928
8929 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8930 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8931 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8932 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8933 if (ANON_AGGR_TYPE_P (pattern))
8934 SET_ANON_AGGR_TYPE_P (type);
8935 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8936 {
8937 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8938 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8939 /* Adjust visibility for template arguments. */
8940 determine_visibility (TYPE_MAIN_DECL (type));
8941 }
8942 if (CLASS_TYPE_P (type))
8943 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8944
8945 pbinfo = TYPE_BINFO (pattern);
8946
8947 /* We should never instantiate a nested class before its enclosing
8948 class; we need to look up the nested class by name before we can
8949 instantiate it, and that lookup should instantiate the enclosing
8950 class. */
8951 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8952 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8953
8954 base_list = NULL_TREE;
8955 if (BINFO_N_BASE_BINFOS (pbinfo))
8956 {
8957 tree pbase_binfo;
8958 tree pushed_scope;
8959 int i;
8960
8961 /* We must enter the scope containing the type, as that is where
8962 the accessibility of types named in dependent bases are
8963 looked up from. */
8964 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8965
8966 /* Substitute into each of the bases to determine the actual
8967 basetypes. */
8968 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8969 {
8970 tree base;
8971 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8972 tree expanded_bases = NULL_TREE;
8973 int idx, len = 1;
8974
8975 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8976 {
8977 expanded_bases =
8978 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8979 args, tf_error, NULL_TREE);
8980 if (expanded_bases == error_mark_node)
8981 continue;
8982
8983 len = TREE_VEC_LENGTH (expanded_bases);
8984 }
8985
8986 for (idx = 0; idx < len; idx++)
8987 {
8988 if (expanded_bases)
8989 /* Extract the already-expanded base class. */
8990 base = TREE_VEC_ELT (expanded_bases, idx);
8991 else
8992 /* Substitute to figure out the base class. */
8993 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8994 NULL_TREE);
8995
8996 if (base == error_mark_node)
8997 continue;
8998
8999 base_list = tree_cons (access, base, base_list);
9000 if (BINFO_VIRTUAL_P (pbase_binfo))
9001 TREE_TYPE (base_list) = integer_type_node;
9002 }
9003 }
9004
9005 /* The list is now in reverse order; correct that. */
9006 base_list = nreverse (base_list);
9007
9008 if (pushed_scope)
9009 pop_scope (pushed_scope);
9010 }
9011 /* Now call xref_basetypes to set up all the base-class
9012 information. */
9013 xref_basetypes (type, base_list);
9014
9015 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9016 (int) ATTR_FLAG_TYPE_IN_PLACE,
9017 args, tf_error, NULL_TREE);
9018 fixup_attribute_variants (type);
9019
9020 /* Now that our base classes are set up, enter the scope of the
9021 class, so that name lookups into base classes, etc. will work
9022 correctly. This is precisely analogous to what we do in
9023 begin_class_definition when defining an ordinary non-template
9024 class, except we also need to push the enclosing classes. */
9025 push_nested_class (type);
9026
9027 /* Now members are processed in the order of declaration. */
9028 for (member = CLASSTYPE_DECL_LIST (pattern);
9029 member; member = TREE_CHAIN (member))
9030 {
9031 tree t = TREE_VALUE (member);
9032
9033 if (TREE_PURPOSE (member))
9034 {
9035 if (TYPE_P (t))
9036 {
9037 /* Build new CLASSTYPE_NESTED_UTDS. */
9038
9039 tree newtag;
9040 bool class_template_p;
9041
9042 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9043 && TYPE_LANG_SPECIFIC (t)
9044 && CLASSTYPE_IS_TEMPLATE (t));
9045 /* If the member is a class template, then -- even after
9046 substitution -- there may be dependent types in the
9047 template argument list for the class. We increment
9048 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9049 that function will assume that no types are dependent
9050 when outside of a template. */
9051 if (class_template_p)
9052 ++processing_template_decl;
9053 newtag = tsubst (t, args, tf_error, NULL_TREE);
9054 if (class_template_p)
9055 --processing_template_decl;
9056 if (newtag == error_mark_node)
9057 continue;
9058
9059 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9060 {
9061 tree name = TYPE_IDENTIFIER (t);
9062
9063 if (class_template_p)
9064 /* Unfortunately, lookup_template_class sets
9065 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9066 instantiation (i.e., for the type of a member
9067 template class nested within a template class.)
9068 This behavior is required for
9069 maybe_process_partial_specialization to work
9070 correctly, but is not accurate in this case;
9071 the TAG is not an instantiation of anything.
9072 (The corresponding TEMPLATE_DECL is an
9073 instantiation, but the TYPE is not.) */
9074 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9075
9076 /* Now, we call pushtag to put this NEWTAG into the scope of
9077 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9078 pushtag calling push_template_decl. We don't have to do
9079 this for enums because it will already have been done in
9080 tsubst_enum. */
9081 if (name)
9082 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9083 pushtag (name, newtag, /*tag_scope=*/ts_current);
9084 }
9085 }
9086 else if (DECL_DECLARES_FUNCTION_P (t))
9087 {
9088 /* Build new TYPE_METHODS. */
9089 tree r;
9090
9091 if (TREE_CODE (t) == TEMPLATE_DECL)
9092 ++processing_template_decl;
9093 r = tsubst (t, args, tf_error, NULL_TREE);
9094 if (TREE_CODE (t) == TEMPLATE_DECL)
9095 --processing_template_decl;
9096 set_current_access_from_decl (r);
9097 finish_member_declaration (r);
9098 /* Instantiate members marked with attribute used. */
9099 if (r != error_mark_node && DECL_PRESERVE_P (r))
9100 mark_used (r);
9101 if (TREE_CODE (r) == FUNCTION_DECL
9102 && DECL_OMP_DECLARE_REDUCTION_P (r))
9103 cp_check_omp_declare_reduction (r);
9104 }
9105 else
9106 {
9107 /* Build new TYPE_FIELDS. */
9108 if (TREE_CODE (t) == STATIC_ASSERT)
9109 {
9110 tree condition;
9111
9112 ++c_inhibit_evaluation_warnings;
9113 condition =
9114 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9115 tf_warning_or_error, NULL_TREE,
9116 /*integral_constant_expression_p=*/true);
9117 --c_inhibit_evaluation_warnings;
9118
9119 finish_static_assert (condition,
9120 STATIC_ASSERT_MESSAGE (t),
9121 STATIC_ASSERT_SOURCE_LOCATION (t),
9122 /*member_p=*/true);
9123 }
9124 else if (TREE_CODE (t) != CONST_DECL)
9125 {
9126 tree r;
9127 tree vec = NULL_TREE;
9128 int len = 1;
9129
9130 /* The file and line for this declaration, to
9131 assist in error message reporting. Since we
9132 called push_tinst_level above, we don't need to
9133 restore these. */
9134 input_location = DECL_SOURCE_LOCATION (t);
9135
9136 if (TREE_CODE (t) == TEMPLATE_DECL)
9137 ++processing_template_decl;
9138 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9139 if (TREE_CODE (t) == TEMPLATE_DECL)
9140 --processing_template_decl;
9141
9142 if (TREE_CODE (r) == TREE_VEC)
9143 {
9144 /* A capture pack became multiple fields. */
9145 vec = r;
9146 len = TREE_VEC_LENGTH (vec);
9147 }
9148
9149 for (int i = 0; i < len; ++i)
9150 {
9151 if (vec)
9152 r = TREE_VEC_ELT (vec, i);
9153 if (VAR_P (r))
9154 {
9155 /* In [temp.inst]:
9156
9157 [t]he initialization (and any associated
9158 side-effects) of a static data member does
9159 not occur unless the static data member is
9160 itself used in a way that requires the
9161 definition of the static data member to
9162 exist.
9163
9164 Therefore, we do not substitute into the
9165 initialized for the static data member here. */
9166 finish_static_data_member_decl
9167 (r,
9168 /*init=*/NULL_TREE,
9169 /*init_const_expr_p=*/false,
9170 /*asmspec_tree=*/NULL_TREE,
9171 /*flags=*/0);
9172 /* Instantiate members marked with attribute used. */
9173 if (r != error_mark_node && DECL_PRESERVE_P (r))
9174 mark_used (r);
9175 }
9176 else if (TREE_CODE (r) == FIELD_DECL)
9177 {
9178 /* Determine whether R has a valid type and can be
9179 completed later. If R is invalid, then its type
9180 is replaced by error_mark_node. */
9181 tree rtype = TREE_TYPE (r);
9182 if (can_complete_type_without_circularity (rtype))
9183 complete_type (rtype);
9184
9185 if (!COMPLETE_TYPE_P (rtype))
9186 {
9187 cxx_incomplete_type_error (r, rtype);
9188 TREE_TYPE (r) = error_mark_node;
9189 }
9190 }
9191
9192 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9193 such a thing will already have been added to the field
9194 list by tsubst_enum in finish_member_declaration in the
9195 CLASSTYPE_NESTED_UTDS case above. */
9196 if (!(TREE_CODE (r) == TYPE_DECL
9197 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9198 && DECL_ARTIFICIAL (r)))
9199 {
9200 set_current_access_from_decl (r);
9201 finish_member_declaration (r);
9202 }
9203 }
9204 }
9205 }
9206 }
9207 else
9208 {
9209 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9210 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9211 {
9212 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9213
9214 tree friend_type = t;
9215 bool adjust_processing_template_decl = false;
9216
9217 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9218 {
9219 /* template <class T> friend class C; */
9220 friend_type = tsubst_friend_class (friend_type, args);
9221 adjust_processing_template_decl = true;
9222 }
9223 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9224 {
9225 /* template <class T> friend class C::D; */
9226 friend_type = tsubst (friend_type, args,
9227 tf_warning_or_error, NULL_TREE);
9228 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9229 friend_type = TREE_TYPE (friend_type);
9230 adjust_processing_template_decl = true;
9231 }
9232 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9233 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9234 {
9235 /* This could be either
9236
9237 friend class T::C;
9238
9239 when dependent_type_p is false or
9240
9241 template <class U> friend class T::C;
9242
9243 otherwise. */
9244 friend_type = tsubst (friend_type, args,
9245 tf_warning_or_error, NULL_TREE);
9246 /* Bump processing_template_decl for correct
9247 dependent_type_p calculation. */
9248 ++processing_template_decl;
9249 if (dependent_type_p (friend_type))
9250 adjust_processing_template_decl = true;
9251 --processing_template_decl;
9252 }
9253 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9254 && hidden_name_p (TYPE_NAME (friend_type)))
9255 {
9256 /* friend class C;
9257
9258 where C hasn't been declared yet. Let's lookup name
9259 from namespace scope directly, bypassing any name that
9260 come from dependent base class. */
9261 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9262
9263 /* The call to xref_tag_from_type does injection for friend
9264 classes. */
9265 push_nested_namespace (ns);
9266 friend_type =
9267 xref_tag_from_type (friend_type, NULL_TREE,
9268 /*tag_scope=*/ts_current);
9269 pop_nested_namespace (ns);
9270 }
9271 else if (uses_template_parms (friend_type))
9272 /* friend class C<T>; */
9273 friend_type = tsubst (friend_type, args,
9274 tf_warning_or_error, NULL_TREE);
9275 /* Otherwise it's
9276
9277 friend class C;
9278
9279 where C is already declared or
9280
9281 friend class C<int>;
9282
9283 We don't have to do anything in these cases. */
9284
9285 if (adjust_processing_template_decl)
9286 /* Trick make_friend_class into realizing that the friend
9287 we're adding is a template, not an ordinary class. It's
9288 important that we use make_friend_class since it will
9289 perform some error-checking and output cross-reference
9290 information. */
9291 ++processing_template_decl;
9292
9293 if (friend_type != error_mark_node)
9294 make_friend_class (type, friend_type, /*complain=*/false);
9295
9296 if (adjust_processing_template_decl)
9297 --processing_template_decl;
9298 }
9299 else
9300 {
9301 /* Build new DECL_FRIENDLIST. */
9302 tree r;
9303
9304 /* The file and line for this declaration, to
9305 assist in error message reporting. Since we
9306 called push_tinst_level above, we don't need to
9307 restore these. */
9308 input_location = DECL_SOURCE_LOCATION (t);
9309
9310 if (TREE_CODE (t) == TEMPLATE_DECL)
9311 {
9312 ++processing_template_decl;
9313 push_deferring_access_checks (dk_no_check);
9314 }
9315
9316 r = tsubst_friend_function (t, args);
9317 add_friend (type, r, /*complain=*/false);
9318 if (TREE_CODE (t) == TEMPLATE_DECL)
9319 {
9320 pop_deferring_access_checks ();
9321 --processing_template_decl;
9322 }
9323 }
9324 }
9325 }
9326
9327 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9328 {
9329 tree decl = lambda_function (type);
9330 if (decl)
9331 {
9332 if (!DECL_TEMPLATE_INFO (decl)
9333 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9334 instantiate_decl (decl, false, false);
9335
9336 /* We need to instantiate the capture list from the template
9337 after we've instantiated the closure members, but before we
9338 consider adding the conversion op. Also keep any captures
9339 that may have been added during instantiation of the op(). */
9340 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9341 tree tmpl_cap
9342 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9343 args, tf_warning_or_error, NULL_TREE,
9344 false, false);
9345
9346 LAMBDA_EXPR_CAPTURE_LIST (expr)
9347 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9348
9349 maybe_add_lambda_conv_op (type);
9350 }
9351 else
9352 gcc_assert (errorcount);
9353 }
9354
9355 /* Set the file and line number information to whatever is given for
9356 the class itself. This puts error messages involving generated
9357 implicit functions at a predictable point, and the same point
9358 that would be used for non-template classes. */
9359 input_location = DECL_SOURCE_LOCATION (typedecl);
9360
9361 unreverse_member_declarations (type);
9362 finish_struct_1 (type);
9363 TYPE_BEING_DEFINED (type) = 0;
9364
9365 /* We don't instantiate default arguments for member functions. 14.7.1:
9366
9367 The implicit instantiation of a class template specialization causes
9368 the implicit instantiation of the declarations, but not of the
9369 definitions or default arguments, of the class member functions,
9370 member classes, static data members and member templates.... */
9371
9372 /* Some typedefs referenced from within the template code need to be access
9373 checked at template instantiation time, i.e now. These types were
9374 added to the template at parsing time. Let's get those and perform
9375 the access checks then. */
9376 perform_typedefs_access_check (pattern, args);
9377 perform_deferred_access_checks (tf_warning_or_error);
9378 pop_nested_class ();
9379 maximum_field_alignment = saved_maximum_field_alignment;
9380 if (!fn_context)
9381 pop_from_top_level ();
9382 pop_deferring_access_checks ();
9383 pop_tinst_level ();
9384
9385 /* The vtable for a template class can be emitted in any translation
9386 unit in which the class is instantiated. When there is no key
9387 method, however, finish_struct_1 will already have added TYPE to
9388 the keyed_classes list. */
9389 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9390 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9391
9392 return type;
9393 }
9394
9395 /* Wrapper for instantiate_class_template_1. */
9396
9397 tree
9398 instantiate_class_template (tree type)
9399 {
9400 tree ret;
9401 timevar_push (TV_TEMPLATE_INST);
9402 ret = instantiate_class_template_1 (type);
9403 timevar_pop (TV_TEMPLATE_INST);
9404 return ret;
9405 }
9406
9407 static tree
9408 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9409 {
9410 tree r;
9411
9412 if (!t)
9413 r = t;
9414 else if (TYPE_P (t))
9415 r = tsubst (t, args, complain, in_decl);
9416 else
9417 {
9418 if (!(complain & tf_warning))
9419 ++c_inhibit_evaluation_warnings;
9420 r = tsubst_expr (t, args, complain, in_decl,
9421 /*integral_constant_expression_p=*/true);
9422 if (!(complain & tf_warning))
9423 --c_inhibit_evaluation_warnings;
9424 }
9425 return r;
9426 }
9427
9428 /* Given a function parameter pack TMPL_PARM and some function parameters
9429 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9430 and set *SPEC_P to point at the next point in the list. */
9431
9432 static tree
9433 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9434 {
9435 /* Collect all of the extra "packed" parameters into an
9436 argument pack. */
9437 tree parmvec;
9438 tree parmtypevec;
9439 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9440 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9441 tree spec_parm = *spec_p;
9442 int i, len;
9443
9444 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9445 if (tmpl_parm
9446 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9447 break;
9448
9449 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9450 parmvec = make_tree_vec (len);
9451 parmtypevec = make_tree_vec (len);
9452 spec_parm = *spec_p;
9453 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9454 {
9455 TREE_VEC_ELT (parmvec, i) = spec_parm;
9456 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9457 }
9458
9459 /* Build the argument packs. */
9460 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9461 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9462 TREE_TYPE (argpack) = argtypepack;
9463 *spec_p = spec_parm;
9464
9465 return argpack;
9466 }
9467
9468 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9469 NONTYPE_ARGUMENT_PACK. */
9470
9471 static tree
9472 make_fnparm_pack (tree spec_parm)
9473 {
9474 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9475 }
9476
9477 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9478 pack expansion. */
9479
9480 static bool
9481 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9482 {
9483 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9484 if (i >= TREE_VEC_LENGTH (vec))
9485 return false;
9486 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9487 }
9488
9489
9490 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9491
9492 static tree
9493 make_argument_pack_select (tree arg_pack, unsigned index)
9494 {
9495 tree aps = make_node (ARGUMENT_PACK_SELECT);
9496
9497 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9498 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9499
9500 return aps;
9501 }
9502
9503 /* This is a subroutine of tsubst_pack_expansion.
9504
9505 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9506 mechanism to store the (non complete list of) arguments of the
9507 substitution and return a non substituted pack expansion, in order
9508 to wait for when we have enough arguments to really perform the
9509 substitution. */
9510
9511 static bool
9512 use_pack_expansion_extra_args_p (tree parm_packs,
9513 int arg_pack_len,
9514 bool has_empty_arg)
9515 {
9516 /* If one pack has an expansion and another pack has a normal
9517 argument or if one pack has an empty argument and an another
9518 one hasn't then tsubst_pack_expansion cannot perform the
9519 substitution and need to fall back on the
9520 PACK_EXPANSION_EXTRA mechanism. */
9521 if (parm_packs == NULL_TREE)
9522 return false;
9523 else if (has_empty_arg)
9524 return true;
9525
9526 bool has_expansion_arg = false;
9527 for (int i = 0 ; i < arg_pack_len; ++i)
9528 {
9529 bool has_non_expansion_arg = false;
9530 for (tree parm_pack = parm_packs;
9531 parm_pack;
9532 parm_pack = TREE_CHAIN (parm_pack))
9533 {
9534 tree arg = TREE_VALUE (parm_pack);
9535
9536 if (argument_pack_element_is_expansion_p (arg, i))
9537 has_expansion_arg = true;
9538 else
9539 has_non_expansion_arg = true;
9540 }
9541
9542 if (has_expansion_arg && has_non_expansion_arg)
9543 return true;
9544 }
9545 return false;
9546 }
9547
9548 /* [temp.variadic]/6 says that:
9549
9550 The instantiation of a pack expansion [...]
9551 produces a list E1,E2, ..., En, where N is the number of elements
9552 in the pack expansion parameters.
9553
9554 This subroutine of tsubst_pack_expansion produces one of these Ei.
9555
9556 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9557 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9558 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9559 INDEX is the index 'i' of the element Ei to produce. ARGS,
9560 COMPLAIN, and IN_DECL are the same parameters as for the
9561 tsubst_pack_expansion function.
9562
9563 The function returns the resulting Ei upon successful completion,
9564 or error_mark_node.
9565
9566 Note that this function possibly modifies the ARGS parameter, so
9567 it's the responsibility of the caller to restore it. */
9568
9569 static tree
9570 gen_elem_of_pack_expansion_instantiation (tree pattern,
9571 tree parm_packs,
9572 unsigned index,
9573 tree args /* This parm gets
9574 modified. */,
9575 tsubst_flags_t complain,
9576 tree in_decl)
9577 {
9578 tree t;
9579 bool ith_elem_is_expansion = false;
9580
9581 /* For each parameter pack, change the substitution of the parameter
9582 pack to the ith argument in its argument pack, then expand the
9583 pattern. */
9584 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9585 {
9586 tree parm = TREE_PURPOSE (pack);
9587 tree arg_pack = TREE_VALUE (pack);
9588 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9589
9590 ith_elem_is_expansion |=
9591 argument_pack_element_is_expansion_p (arg_pack, index);
9592
9593 /* Select the Ith argument from the pack. */
9594 if (TREE_CODE (parm) == PARM_DECL
9595 || TREE_CODE (parm) == FIELD_DECL)
9596 {
9597 if (index == 0)
9598 {
9599 aps = make_argument_pack_select (arg_pack, index);
9600 mark_used (parm);
9601 register_local_specialization (aps, parm);
9602 }
9603 else
9604 aps = retrieve_local_specialization (parm);
9605 }
9606 else
9607 {
9608 int idx, level;
9609 template_parm_level_and_index (parm, &level, &idx);
9610
9611 if (index == 0)
9612 {
9613 aps = make_argument_pack_select (arg_pack, index);
9614 /* Update the corresponding argument. */
9615 TMPL_ARG (args, level, idx) = aps;
9616 }
9617 else
9618 /* Re-use the ARGUMENT_PACK_SELECT. */
9619 aps = TMPL_ARG (args, level, idx);
9620 }
9621 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9622 }
9623
9624 /* Substitute into the PATTERN with the (possibly altered)
9625 arguments. */
9626 if (pattern == in_decl)
9627 /* Expanding a fixed parameter pack from
9628 coerce_template_parameter_pack. */
9629 t = tsubst_decl (pattern, args, complain);
9630 else if (!TYPE_P (pattern))
9631 t = tsubst_expr (pattern, args, complain, in_decl,
9632 /*integral_constant_expression_p=*/false);
9633 else
9634 t = tsubst (pattern, args, complain, in_decl);
9635
9636 /* If the Ith argument pack element is a pack expansion, then
9637 the Ith element resulting from the substituting is going to
9638 be a pack expansion as well. */
9639 if (ith_elem_is_expansion)
9640 t = make_pack_expansion (t);
9641
9642 return t;
9643 }
9644
9645 /* Substitute ARGS into T, which is an pack expansion
9646 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9647 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9648 (if only a partial substitution could be performed) or
9649 ERROR_MARK_NODE if there was an error. */
9650 tree
9651 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9652 tree in_decl)
9653 {
9654 tree pattern;
9655 tree pack, packs = NULL_TREE;
9656 bool unsubstituted_packs = false;
9657 int i, len = -1;
9658 tree result;
9659 struct pointer_map_t *saved_local_specializations = NULL;
9660 bool need_local_specializations = false;
9661 int levels;
9662
9663 gcc_assert (PACK_EXPANSION_P (t));
9664 pattern = PACK_EXPANSION_PATTERN (t);
9665
9666 /* Add in any args remembered from an earlier partial instantiation. */
9667 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9668
9669 levels = TMPL_ARGS_DEPTH (args);
9670
9671 /* Determine the argument packs that will instantiate the parameter
9672 packs used in the expansion expression. While we're at it,
9673 compute the number of arguments to be expanded and make sure it
9674 is consistent. */
9675 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9676 pack = TREE_CHAIN (pack))
9677 {
9678 tree parm_pack = TREE_VALUE (pack);
9679 tree arg_pack = NULL_TREE;
9680 tree orig_arg = NULL_TREE;
9681 int level = 0;
9682
9683 if (TREE_CODE (parm_pack) == BASES)
9684 {
9685 if (BASES_DIRECT (parm_pack))
9686 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9687 args, complain, in_decl, false));
9688 else
9689 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9690 args, complain, in_decl, false));
9691 }
9692 if (TREE_CODE (parm_pack) == PARM_DECL)
9693 {
9694 if (PACK_EXPANSION_LOCAL_P (t))
9695 arg_pack = retrieve_local_specialization (parm_pack);
9696 else
9697 {
9698 /* We can't rely on local_specializations for a parameter
9699 name used later in a function declaration (such as in a
9700 late-specified return type). Even if it exists, it might
9701 have the wrong value for a recursive call. Just make a
9702 dummy decl, since it's only used for its type. */
9703 arg_pack = tsubst_decl (parm_pack, args, complain);
9704 if (arg_pack && DECL_PACK_P (arg_pack))
9705 /* Partial instantiation of the parm_pack, we can't build
9706 up an argument pack yet. */
9707 arg_pack = NULL_TREE;
9708 else
9709 arg_pack = make_fnparm_pack (arg_pack);
9710 need_local_specializations = true;
9711 }
9712 }
9713 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9714 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9715 else
9716 {
9717 int idx;
9718 template_parm_level_and_index (parm_pack, &level, &idx);
9719
9720 if (level <= levels)
9721 arg_pack = TMPL_ARG (args, level, idx);
9722 }
9723
9724 orig_arg = arg_pack;
9725 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9726 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9727
9728 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9729 /* This can only happen if we forget to expand an argument
9730 pack somewhere else. Just return an error, silently. */
9731 {
9732 result = make_tree_vec (1);
9733 TREE_VEC_ELT (result, 0) = error_mark_node;
9734 return result;
9735 }
9736
9737 if (arg_pack)
9738 {
9739 int my_len =
9740 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9741
9742 /* Don't bother trying to do a partial substitution with
9743 incomplete packs; we'll try again after deduction. */
9744 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9745 return t;
9746
9747 if (len < 0)
9748 len = my_len;
9749 else if (len != my_len)
9750 {
9751 if (!(complain & tf_error))
9752 /* Fail quietly. */;
9753 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9754 error ("mismatched argument pack lengths while expanding "
9755 "%<%T%>",
9756 pattern);
9757 else
9758 error ("mismatched argument pack lengths while expanding "
9759 "%<%E%>",
9760 pattern);
9761 return error_mark_node;
9762 }
9763
9764 /* Keep track of the parameter packs and their corresponding
9765 argument packs. */
9766 packs = tree_cons (parm_pack, arg_pack, packs);
9767 TREE_TYPE (packs) = orig_arg;
9768 }
9769 else
9770 {
9771 /* We can't substitute for this parameter pack. We use a flag as
9772 well as the missing_level counter because function parameter
9773 packs don't have a level. */
9774 unsubstituted_packs = true;
9775 }
9776 }
9777
9778 /* We cannot expand this expansion expression, because we don't have
9779 all of the argument packs we need. */
9780 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9781 {
9782 /* We got some full packs, but we can't substitute them in until we
9783 have values for all the packs. So remember these until then. */
9784
9785 t = make_pack_expansion (pattern);
9786 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9787 return t;
9788 }
9789 else if (unsubstituted_packs)
9790 {
9791 /* There were no real arguments, we're just replacing a parameter
9792 pack with another version of itself. Substitute into the
9793 pattern and return a PACK_EXPANSION_*. The caller will need to
9794 deal with that. */
9795 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9796 t = tsubst_expr (pattern, args, complain, in_decl,
9797 /*integral_constant_expression_p=*/false);
9798 else
9799 t = tsubst (pattern, args, complain, in_decl);
9800 t = make_pack_expansion (t);
9801 return t;
9802 }
9803
9804 gcc_assert (len >= 0);
9805
9806 if (need_local_specializations)
9807 {
9808 /* We're in a late-specified return type, so create our own local
9809 specializations map; the current map is either NULL or (in the
9810 case of recursive unification) might have bindings that we don't
9811 want to use or alter. */
9812 saved_local_specializations = local_specializations;
9813 local_specializations = pointer_map_create ();
9814 }
9815
9816 /* For each argument in each argument pack, substitute into the
9817 pattern. */
9818 result = make_tree_vec (len);
9819 for (i = 0; i < len; ++i)
9820 {
9821 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9822 i,
9823 args, complain,
9824 in_decl);
9825 TREE_VEC_ELT (result, i) = t;
9826 if (t == error_mark_node)
9827 {
9828 result = error_mark_node;
9829 break;
9830 }
9831 }
9832
9833 /* Update ARGS to restore the substitution from parameter packs to
9834 their argument packs. */
9835 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9836 {
9837 tree parm = TREE_PURPOSE (pack);
9838
9839 if (TREE_CODE (parm) == PARM_DECL
9840 || TREE_CODE (parm) == FIELD_DECL)
9841 register_local_specialization (TREE_TYPE (pack), parm);
9842 else
9843 {
9844 int idx, level;
9845
9846 if (TREE_VALUE (pack) == NULL_TREE)
9847 continue;
9848
9849 template_parm_level_and_index (parm, &level, &idx);
9850
9851 /* Update the corresponding argument. */
9852 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9853 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9854 TREE_TYPE (pack);
9855 else
9856 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9857 }
9858 }
9859
9860 if (need_local_specializations)
9861 {
9862 pointer_map_destroy (local_specializations);
9863 local_specializations = saved_local_specializations;
9864 }
9865
9866 return result;
9867 }
9868
9869 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9870 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9871 parameter packs; all parms generated from a function parameter pack will
9872 have the same DECL_PARM_INDEX. */
9873
9874 tree
9875 get_pattern_parm (tree parm, tree tmpl)
9876 {
9877 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9878 tree patparm;
9879
9880 if (DECL_ARTIFICIAL (parm))
9881 {
9882 for (patparm = DECL_ARGUMENTS (pattern);
9883 patparm; patparm = DECL_CHAIN (patparm))
9884 if (DECL_ARTIFICIAL (patparm)
9885 && DECL_NAME (parm) == DECL_NAME (patparm))
9886 break;
9887 }
9888 else
9889 {
9890 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9891 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9892 gcc_assert (DECL_PARM_INDEX (patparm)
9893 == DECL_PARM_INDEX (parm));
9894 }
9895
9896 return patparm;
9897 }
9898
9899 /* Substitute ARGS into the vector or list of template arguments T. */
9900
9901 static tree
9902 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9903 {
9904 tree orig_t = t;
9905 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9906 tree *elts;
9907
9908 if (t == error_mark_node)
9909 return error_mark_node;
9910
9911 len = TREE_VEC_LENGTH (t);
9912 elts = XALLOCAVEC (tree, len);
9913
9914 for (i = 0; i < len; i++)
9915 {
9916 tree orig_arg = TREE_VEC_ELT (t, i);
9917 tree new_arg;
9918
9919 if (TREE_CODE (orig_arg) == TREE_VEC)
9920 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9921 else if (PACK_EXPANSION_P (orig_arg))
9922 {
9923 /* Substitute into an expansion expression. */
9924 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9925
9926 if (TREE_CODE (new_arg) == TREE_VEC)
9927 /* Add to the expanded length adjustment the number of
9928 expanded arguments. We subtract one from this
9929 measurement, because the argument pack expression
9930 itself is already counted as 1 in
9931 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9932 the argument pack is empty. */
9933 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9934 }
9935 else if (ARGUMENT_PACK_P (orig_arg))
9936 {
9937 /* Substitute into each of the arguments. */
9938 new_arg = TYPE_P (orig_arg)
9939 ? cxx_make_type (TREE_CODE (orig_arg))
9940 : make_node (TREE_CODE (orig_arg));
9941
9942 SET_ARGUMENT_PACK_ARGS (
9943 new_arg,
9944 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9945 args, complain, in_decl));
9946
9947 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9948 new_arg = error_mark_node;
9949
9950 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9951 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9952 complain, in_decl);
9953 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9954
9955 if (TREE_TYPE (new_arg) == error_mark_node)
9956 new_arg = error_mark_node;
9957 }
9958 }
9959 else
9960 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9961
9962 if (new_arg == error_mark_node)
9963 return error_mark_node;
9964
9965 elts[i] = new_arg;
9966 if (new_arg != orig_arg)
9967 need_new = 1;
9968 }
9969
9970 if (!need_new)
9971 return t;
9972
9973 /* Make space for the expanded arguments coming from template
9974 argument packs. */
9975 t = make_tree_vec (len + expanded_len_adjust);
9976 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9977 arguments for a member template.
9978 In that case each TREE_VEC in ORIG_T represents a level of template
9979 arguments, and ORIG_T won't carry any non defaulted argument count.
9980 It will rather be the nested TREE_VECs that will carry one.
9981 In other words, ORIG_T carries a non defaulted argument count only
9982 if it doesn't contain any nested TREE_VEC. */
9983 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9984 {
9985 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9986 count += expanded_len_adjust;
9987 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9988 }
9989 for (i = 0, out = 0; i < len; i++)
9990 {
9991 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9992 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9993 && TREE_CODE (elts[i]) == TREE_VEC)
9994 {
9995 int idx;
9996
9997 /* Now expand the template argument pack "in place". */
9998 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9999 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10000 }
10001 else
10002 {
10003 TREE_VEC_ELT (t, out) = elts[i];
10004 out++;
10005 }
10006 }
10007
10008 return t;
10009 }
10010
10011 /* Return the result of substituting ARGS into the template parameters
10012 given by PARMS. If there are m levels of ARGS and m + n levels of
10013 PARMS, then the result will contain n levels of PARMS. For
10014 example, if PARMS is `template <class T> template <class U>
10015 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10016 result will be `template <int*, double, class V>'. */
10017
10018 static tree
10019 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10020 {
10021 tree r = NULL_TREE;
10022 tree* new_parms;
10023
10024 /* When substituting into a template, we must set
10025 PROCESSING_TEMPLATE_DECL as the template parameters may be
10026 dependent if they are based on one-another, and the dependency
10027 predicates are short-circuit outside of templates. */
10028 ++processing_template_decl;
10029
10030 for (new_parms = &r;
10031 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10032 new_parms = &(TREE_CHAIN (*new_parms)),
10033 parms = TREE_CHAIN (parms))
10034 {
10035 tree new_vec =
10036 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10037 int i;
10038
10039 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10040 {
10041 tree tuple;
10042
10043 if (parms == error_mark_node)
10044 continue;
10045
10046 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10047
10048 if (tuple == error_mark_node)
10049 continue;
10050
10051 TREE_VEC_ELT (new_vec, i) =
10052 tsubst_template_parm (tuple, args, complain);
10053 }
10054
10055 *new_parms =
10056 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10057 - TMPL_ARGS_DEPTH (args)),
10058 new_vec, NULL_TREE);
10059 }
10060
10061 --processing_template_decl;
10062
10063 return r;
10064 }
10065
10066 /* Return the result of substituting ARGS into one template parameter
10067 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10068 parameter and which TREE_PURPOSE is the default argument of the
10069 template parameter. */
10070
10071 static tree
10072 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10073 {
10074 tree default_value, parm_decl;
10075
10076 if (args == NULL_TREE
10077 || t == NULL_TREE
10078 || t == error_mark_node)
10079 return t;
10080
10081 gcc_assert (TREE_CODE (t) == TREE_LIST);
10082
10083 default_value = TREE_PURPOSE (t);
10084 parm_decl = TREE_VALUE (t);
10085
10086 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10087 if (TREE_CODE (parm_decl) == PARM_DECL
10088 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10089 parm_decl = error_mark_node;
10090 default_value = tsubst_template_arg (default_value, args,
10091 complain, NULL_TREE);
10092
10093 return build_tree_list (default_value, parm_decl);
10094 }
10095
10096 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10097 type T. If T is not an aggregate or enumeration type, it is
10098 handled as if by tsubst. IN_DECL is as for tsubst. If
10099 ENTERING_SCOPE is nonzero, T is the context for a template which
10100 we are presently tsubst'ing. Return the substituted value. */
10101
10102 static tree
10103 tsubst_aggr_type (tree t,
10104 tree args,
10105 tsubst_flags_t complain,
10106 tree in_decl,
10107 int entering_scope)
10108 {
10109 if (t == NULL_TREE)
10110 return NULL_TREE;
10111
10112 switch (TREE_CODE (t))
10113 {
10114 case RECORD_TYPE:
10115 if (TYPE_PTRMEMFUNC_P (t))
10116 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10117
10118 /* Else fall through. */
10119 case ENUMERAL_TYPE:
10120 case UNION_TYPE:
10121 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10122 {
10123 tree argvec;
10124 tree context;
10125 tree r;
10126 int saved_unevaluated_operand;
10127 int saved_inhibit_evaluation_warnings;
10128
10129 /* In "sizeof(X<I>)" we need to evaluate "I". */
10130 saved_unevaluated_operand = cp_unevaluated_operand;
10131 cp_unevaluated_operand = 0;
10132 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10133 c_inhibit_evaluation_warnings = 0;
10134
10135 /* First, determine the context for the type we are looking
10136 up. */
10137 context = TYPE_CONTEXT (t);
10138 if (context && TYPE_P (context))
10139 {
10140 context = tsubst_aggr_type (context, args, complain,
10141 in_decl, /*entering_scope=*/1);
10142 /* If context is a nested class inside a class template,
10143 it may still need to be instantiated (c++/33959). */
10144 context = complete_type (context);
10145 }
10146
10147 /* Then, figure out what arguments are appropriate for the
10148 type we are trying to find. For example, given:
10149
10150 template <class T> struct S;
10151 template <class T, class U> void f(T, U) { S<U> su; }
10152
10153 and supposing that we are instantiating f<int, double>,
10154 then our ARGS will be {int, double}, but, when looking up
10155 S we only want {double}. */
10156 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10157 complain, in_decl);
10158 if (argvec == error_mark_node)
10159 r = error_mark_node;
10160 else
10161 {
10162 r = lookup_template_class (t, argvec, in_decl, context,
10163 entering_scope, complain);
10164 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10165 }
10166
10167 cp_unevaluated_operand = saved_unevaluated_operand;
10168 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10169
10170 return r;
10171 }
10172 else
10173 /* This is not a template type, so there's nothing to do. */
10174 return t;
10175
10176 default:
10177 return tsubst (t, args, complain, in_decl);
10178 }
10179 }
10180
10181 /* Substitute into the default argument ARG (a default argument for
10182 FN), which has the indicated TYPE. */
10183
10184 tree
10185 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10186 {
10187 tree saved_class_ptr = NULL_TREE;
10188 tree saved_class_ref = NULL_TREE;
10189 int errs = errorcount + sorrycount;
10190
10191 /* This can happen in invalid code. */
10192 if (TREE_CODE (arg) == DEFAULT_ARG)
10193 return arg;
10194
10195 /* This default argument came from a template. Instantiate the
10196 default argument here, not in tsubst. In the case of
10197 something like:
10198
10199 template <class T>
10200 struct S {
10201 static T t();
10202 void f(T = t());
10203 };
10204
10205 we must be careful to do name lookup in the scope of S<T>,
10206 rather than in the current class. */
10207 push_access_scope (fn);
10208 /* The "this" pointer is not valid in a default argument. */
10209 if (cfun)
10210 {
10211 saved_class_ptr = current_class_ptr;
10212 cp_function_chain->x_current_class_ptr = NULL_TREE;
10213 saved_class_ref = current_class_ref;
10214 cp_function_chain->x_current_class_ref = NULL_TREE;
10215 }
10216
10217 push_deferring_access_checks(dk_no_deferred);
10218 /* The default argument expression may cause implicitly defined
10219 member functions to be synthesized, which will result in garbage
10220 collection. We must treat this situation as if we were within
10221 the body of function so as to avoid collecting live data on the
10222 stack. */
10223 ++function_depth;
10224 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10225 complain, NULL_TREE,
10226 /*integral_constant_expression_p=*/false);
10227 --function_depth;
10228 pop_deferring_access_checks();
10229
10230 /* Restore the "this" pointer. */
10231 if (cfun)
10232 {
10233 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10234 cp_function_chain->x_current_class_ref = saved_class_ref;
10235 }
10236
10237 if (errorcount+sorrycount > errs
10238 && (complain & tf_warning_or_error))
10239 inform (input_location,
10240 " when instantiating default argument for call to %D", fn);
10241
10242 /* Make sure the default argument is reasonable. */
10243 arg = check_default_argument (type, arg, complain);
10244
10245 pop_access_scope (fn);
10246
10247 return arg;
10248 }
10249
10250 /* Substitute into all the default arguments for FN. */
10251
10252 static void
10253 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10254 {
10255 tree arg;
10256 tree tmpl_args;
10257
10258 tmpl_args = DECL_TI_ARGS (fn);
10259
10260 /* If this function is not yet instantiated, we certainly don't need
10261 its default arguments. */
10262 if (uses_template_parms (tmpl_args))
10263 return;
10264 /* Don't do this again for clones. */
10265 if (DECL_CLONED_FUNCTION_P (fn))
10266 return;
10267
10268 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10269 arg;
10270 arg = TREE_CHAIN (arg))
10271 if (TREE_PURPOSE (arg))
10272 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10273 TREE_VALUE (arg),
10274 TREE_PURPOSE (arg),
10275 complain);
10276 }
10277
10278 /* Substitute the ARGS into the T, which is a _DECL. Return the
10279 result of the substitution. Issue error and warning messages under
10280 control of COMPLAIN. */
10281
10282 static tree
10283 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10284 {
10285 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10286 location_t saved_loc;
10287 tree r = NULL_TREE;
10288 tree in_decl = t;
10289 hashval_t hash = 0;
10290
10291 /* Set the filename and linenumber to improve error-reporting. */
10292 saved_loc = input_location;
10293 input_location = DECL_SOURCE_LOCATION (t);
10294
10295 switch (TREE_CODE (t))
10296 {
10297 case TEMPLATE_DECL:
10298 {
10299 /* We can get here when processing a member function template,
10300 member class template, or template template parameter. */
10301 tree decl = DECL_TEMPLATE_RESULT (t);
10302 tree spec;
10303 tree tmpl_args;
10304 tree full_args;
10305
10306 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10307 {
10308 /* Template template parameter is treated here. */
10309 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10310 if (new_type == error_mark_node)
10311 RETURN (error_mark_node);
10312 /* If we get a real template back, return it. This can happen in
10313 the context of most_specialized_class. */
10314 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10315 return new_type;
10316
10317 r = copy_decl (t);
10318 DECL_CHAIN (r) = NULL_TREE;
10319 TREE_TYPE (r) = new_type;
10320 DECL_TEMPLATE_RESULT (r)
10321 = build_decl (DECL_SOURCE_LOCATION (decl),
10322 TYPE_DECL, DECL_NAME (decl), new_type);
10323 DECL_TEMPLATE_PARMS (r)
10324 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10325 complain);
10326 TYPE_NAME (new_type) = r;
10327 break;
10328 }
10329
10330 /* We might already have an instance of this template.
10331 The ARGS are for the surrounding class type, so the
10332 full args contain the tsubst'd args for the context,
10333 plus the innermost args from the template decl. */
10334 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10335 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10336 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10337 /* Because this is a template, the arguments will still be
10338 dependent, even after substitution. If
10339 PROCESSING_TEMPLATE_DECL is not set, the dependency
10340 predicates will short-circuit. */
10341 ++processing_template_decl;
10342 full_args = tsubst_template_args (tmpl_args, args,
10343 complain, in_decl);
10344 --processing_template_decl;
10345 if (full_args == error_mark_node)
10346 RETURN (error_mark_node);
10347
10348 /* If this is a default template template argument,
10349 tsubst might not have changed anything. */
10350 if (full_args == tmpl_args)
10351 RETURN (t);
10352
10353 hash = hash_tmpl_and_args (t, full_args);
10354 spec = retrieve_specialization (t, full_args, hash);
10355 if (spec != NULL_TREE)
10356 {
10357 r = spec;
10358 break;
10359 }
10360
10361 /* Make a new template decl. It will be similar to the
10362 original, but will record the current template arguments.
10363 We also create a new function declaration, which is just
10364 like the old one, but points to this new template, rather
10365 than the old one. */
10366 r = copy_decl (t);
10367 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10368 DECL_CHAIN (r) = NULL_TREE;
10369
10370 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10371
10372 if (TREE_CODE (decl) == TYPE_DECL
10373 && !TYPE_DECL_ALIAS_P (decl))
10374 {
10375 tree new_type;
10376 ++processing_template_decl;
10377 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10378 --processing_template_decl;
10379 if (new_type == error_mark_node)
10380 RETURN (error_mark_node);
10381
10382 TREE_TYPE (r) = new_type;
10383 /* For a partial specialization, we need to keep pointing to
10384 the primary template. */
10385 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10386 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10387 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10388 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10389 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10390 }
10391 else
10392 {
10393 tree new_decl;
10394 ++processing_template_decl;
10395 new_decl = tsubst (decl, args, complain, in_decl);
10396 --processing_template_decl;
10397 if (new_decl == error_mark_node)
10398 RETURN (error_mark_node);
10399
10400 DECL_TEMPLATE_RESULT (r) = new_decl;
10401 DECL_TI_TEMPLATE (new_decl) = r;
10402 TREE_TYPE (r) = TREE_TYPE (new_decl);
10403 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10404 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10405 }
10406
10407 SET_DECL_IMPLICIT_INSTANTIATION (r);
10408 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10409 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10410
10411 /* The template parameters for this new template are all the
10412 template parameters for the old template, except the
10413 outermost level of parameters. */
10414 DECL_TEMPLATE_PARMS (r)
10415 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10416 complain);
10417
10418 if (PRIMARY_TEMPLATE_P (t))
10419 DECL_PRIMARY_TEMPLATE (r) = r;
10420
10421 if (TREE_CODE (decl) != TYPE_DECL)
10422 /* Record this non-type partial instantiation. */
10423 register_specialization (r, t,
10424 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10425 false, hash);
10426 }
10427 break;
10428
10429 case FUNCTION_DECL:
10430 {
10431 tree ctx;
10432 tree argvec = NULL_TREE;
10433 tree *friends;
10434 tree gen_tmpl;
10435 tree type;
10436 int member;
10437 int args_depth;
10438 int parms_depth;
10439
10440 /* Nobody should be tsubst'ing into non-template functions. */
10441 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10442
10443 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10444 {
10445 tree spec;
10446 bool dependent_p;
10447
10448 /* If T is not dependent, just return it. We have to
10449 increment PROCESSING_TEMPLATE_DECL because
10450 value_dependent_expression_p assumes that nothing is
10451 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10452 ++processing_template_decl;
10453 dependent_p = value_dependent_expression_p (t);
10454 --processing_template_decl;
10455 if (!dependent_p)
10456 RETURN (t);
10457
10458 /* Calculate the most general template of which R is a
10459 specialization, and the complete set of arguments used to
10460 specialize R. */
10461 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10462 argvec = tsubst_template_args (DECL_TI_ARGS
10463 (DECL_TEMPLATE_RESULT
10464 (DECL_TI_TEMPLATE (t))),
10465 args, complain, in_decl);
10466 if (argvec == error_mark_node)
10467 RETURN (error_mark_node);
10468
10469 /* Check to see if we already have this specialization. */
10470 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10471 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10472
10473 if (spec)
10474 {
10475 r = spec;
10476 break;
10477 }
10478
10479 /* We can see more levels of arguments than parameters if
10480 there was a specialization of a member template, like
10481 this:
10482
10483 template <class T> struct S { template <class U> void f(); }
10484 template <> template <class U> void S<int>::f(U);
10485
10486 Here, we'll be substituting into the specialization,
10487 because that's where we can find the code we actually
10488 want to generate, but we'll have enough arguments for
10489 the most general template.
10490
10491 We also deal with the peculiar case:
10492
10493 template <class T> struct S {
10494 template <class U> friend void f();
10495 };
10496 template <class U> void f() {}
10497 template S<int>;
10498 template void f<double>();
10499
10500 Here, the ARGS for the instantiation of will be {int,
10501 double}. But, we only need as many ARGS as there are
10502 levels of template parameters in CODE_PATTERN. We are
10503 careful not to get fooled into reducing the ARGS in
10504 situations like:
10505
10506 template <class T> struct S { template <class U> void f(U); }
10507 template <class T> template <> void S<T>::f(int) {}
10508
10509 which we can spot because the pattern will be a
10510 specialization in this case. */
10511 args_depth = TMPL_ARGS_DEPTH (args);
10512 parms_depth =
10513 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10514 if (args_depth > parms_depth
10515 && !DECL_TEMPLATE_SPECIALIZATION (t))
10516 args = get_innermost_template_args (args, parms_depth);
10517 }
10518 else
10519 {
10520 /* This special case arises when we have something like this:
10521
10522 template <class T> struct S {
10523 friend void f<int>(int, double);
10524 };
10525
10526 Here, the DECL_TI_TEMPLATE for the friend declaration
10527 will be an IDENTIFIER_NODE. We are being called from
10528 tsubst_friend_function, and we want only to create a
10529 new decl (R) with appropriate types so that we can call
10530 determine_specialization. */
10531 gen_tmpl = NULL_TREE;
10532 }
10533
10534 if (DECL_CLASS_SCOPE_P (t))
10535 {
10536 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10537 member = 2;
10538 else
10539 member = 1;
10540 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10541 complain, t, /*entering_scope=*/1);
10542 }
10543 else
10544 {
10545 member = 0;
10546 ctx = DECL_CONTEXT (t);
10547 }
10548 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10549 if (type == error_mark_node)
10550 RETURN (error_mark_node);
10551
10552 /* If we hit excessive deduction depth, the type is bogus even if
10553 it isn't error_mark_node, so don't build a decl. */
10554 if (excessive_deduction_depth)
10555 RETURN (error_mark_node);
10556
10557 /* We do NOT check for matching decls pushed separately at this
10558 point, as they may not represent instantiations of this
10559 template, and in any case are considered separate under the
10560 discrete model. */
10561 r = copy_decl (t);
10562 DECL_USE_TEMPLATE (r) = 0;
10563 TREE_TYPE (r) = type;
10564 /* Clear out the mangled name and RTL for the instantiation. */
10565 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10566 SET_DECL_RTL (r, NULL);
10567 /* Leave DECL_INITIAL set on deleted instantiations. */
10568 if (!DECL_DELETED_FN (r))
10569 DECL_INITIAL (r) = NULL_TREE;
10570 DECL_CONTEXT (r) = ctx;
10571
10572 /* OpenMP UDRs have the only argument a reference to the declared
10573 type. We want to diagnose if the declared type is a reference,
10574 which is invalid, but as references to references are usually
10575 quietly merged, diagnose it here. */
10576 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10577 {
10578 tree argtype
10579 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10580 argtype = tsubst (argtype, args, complain, in_decl);
10581 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10582 error_at (DECL_SOURCE_LOCATION (t),
10583 "reference type %qT in "
10584 "%<#pragma omp declare reduction%>", argtype);
10585 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10586 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10587 argtype);
10588 }
10589
10590 if (member && DECL_CONV_FN_P (r))
10591 /* Type-conversion operator. Reconstruct the name, in
10592 case it's the name of one of the template's parameters. */
10593 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10594
10595 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10596 complain, t);
10597 DECL_RESULT (r) = NULL_TREE;
10598
10599 TREE_STATIC (r) = 0;
10600 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10601 DECL_EXTERNAL (r) = 1;
10602 /* If this is an instantiation of a function with internal
10603 linkage, we already know what object file linkage will be
10604 assigned to the instantiation. */
10605 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10606 DECL_DEFER_OUTPUT (r) = 0;
10607 DECL_CHAIN (r) = NULL_TREE;
10608 DECL_PENDING_INLINE_INFO (r) = 0;
10609 DECL_PENDING_INLINE_P (r) = 0;
10610 DECL_SAVED_TREE (r) = NULL_TREE;
10611 DECL_STRUCT_FUNCTION (r) = NULL;
10612 TREE_USED (r) = 0;
10613 /* We'll re-clone as appropriate in instantiate_template. */
10614 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10615
10616 /* If we aren't complaining now, return on error before we register
10617 the specialization so that we'll complain eventually. */
10618 if ((complain & tf_error) == 0
10619 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10620 && !grok_op_properties (r, /*complain=*/false))
10621 RETURN (error_mark_node);
10622
10623 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10624 this in the special friend case mentioned above where
10625 GEN_TMPL is NULL. */
10626 if (gen_tmpl)
10627 {
10628 DECL_TEMPLATE_INFO (r)
10629 = build_template_info (gen_tmpl, argvec);
10630 SET_DECL_IMPLICIT_INSTANTIATION (r);
10631
10632 tree new_r
10633 = register_specialization (r, gen_tmpl, argvec, false, hash);
10634 if (new_r != r)
10635 /* We instantiated this while substituting into
10636 the type earlier (template/friend54.C). */
10637 RETURN (new_r);
10638
10639 /* We're not supposed to instantiate default arguments
10640 until they are called, for a template. But, for a
10641 declaration like:
10642
10643 template <class T> void f ()
10644 { extern void g(int i = T()); }
10645
10646 we should do the substitution when the template is
10647 instantiated. We handle the member function case in
10648 instantiate_class_template since the default arguments
10649 might refer to other members of the class. */
10650 if (!member
10651 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10652 && !uses_template_parms (argvec))
10653 tsubst_default_arguments (r, complain);
10654 }
10655 else
10656 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10657
10658 /* Copy the list of befriending classes. */
10659 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10660 *friends;
10661 friends = &TREE_CHAIN (*friends))
10662 {
10663 *friends = copy_node (*friends);
10664 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10665 args, complain,
10666 in_decl);
10667 }
10668
10669 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10670 {
10671 maybe_retrofit_in_chrg (r);
10672 if (DECL_CONSTRUCTOR_P (r))
10673 grok_ctor_properties (ctx, r);
10674 if (DECL_INHERITED_CTOR_BASE (r))
10675 deduce_inheriting_ctor (r);
10676 /* If this is an instantiation of a member template, clone it.
10677 If it isn't, that'll be handled by
10678 clone_constructors_and_destructors. */
10679 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10680 clone_function_decl (r, /*update_method_vec_p=*/0);
10681 }
10682 else if ((complain & tf_error) != 0
10683 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10684 && !grok_op_properties (r, /*complain=*/true))
10685 RETURN (error_mark_node);
10686
10687 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10688 SET_DECL_FRIEND_CONTEXT (r,
10689 tsubst (DECL_FRIEND_CONTEXT (t),
10690 args, complain, in_decl));
10691
10692 /* Possibly limit visibility based on template args. */
10693 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10694 if (DECL_VISIBILITY_SPECIFIED (t))
10695 {
10696 DECL_VISIBILITY_SPECIFIED (r) = 0;
10697 DECL_ATTRIBUTES (r)
10698 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10699 }
10700 determine_visibility (r);
10701 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10702 && !processing_template_decl)
10703 defaulted_late_check (r);
10704
10705 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10706 args, complain, in_decl);
10707 }
10708 break;
10709
10710 case PARM_DECL:
10711 {
10712 tree type = NULL_TREE;
10713 int i, len = 1;
10714 tree expanded_types = NULL_TREE;
10715 tree prev_r = NULL_TREE;
10716 tree first_r = NULL_TREE;
10717
10718 if (DECL_PACK_P (t))
10719 {
10720 /* If there is a local specialization that isn't a
10721 parameter pack, it means that we're doing a "simple"
10722 substitution from inside tsubst_pack_expansion. Just
10723 return the local specialization (which will be a single
10724 parm). */
10725 tree spec = retrieve_local_specialization (t);
10726 if (spec
10727 && TREE_CODE (spec) == PARM_DECL
10728 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10729 RETURN (spec);
10730
10731 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10732 the parameters in this function parameter pack. */
10733 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10734 complain, in_decl);
10735 if (TREE_CODE (expanded_types) == TREE_VEC)
10736 {
10737 len = TREE_VEC_LENGTH (expanded_types);
10738
10739 /* Zero-length parameter packs are boring. Just substitute
10740 into the chain. */
10741 if (len == 0)
10742 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10743 TREE_CHAIN (t)));
10744 }
10745 else
10746 {
10747 /* All we did was update the type. Make a note of that. */
10748 type = expanded_types;
10749 expanded_types = NULL_TREE;
10750 }
10751 }
10752
10753 /* Loop through all of the parameters we'll build. When T is
10754 a function parameter pack, LEN is the number of expanded
10755 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10756 r = NULL_TREE;
10757 for (i = 0; i < len; ++i)
10758 {
10759 prev_r = r;
10760 r = copy_node (t);
10761 if (DECL_TEMPLATE_PARM_P (t))
10762 SET_DECL_TEMPLATE_PARM_P (r);
10763
10764 if (expanded_types)
10765 /* We're on the Ith parameter of the function parameter
10766 pack. */
10767 {
10768 /* Get the Ith type. */
10769 type = TREE_VEC_ELT (expanded_types, i);
10770
10771 /* Rename the parameter to include the index. */
10772 DECL_NAME (r)
10773 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10774 }
10775 else if (!type)
10776 /* We're dealing with a normal parameter. */
10777 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10778
10779 type = type_decays_to (type);
10780 TREE_TYPE (r) = type;
10781 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10782
10783 if (DECL_INITIAL (r))
10784 {
10785 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10786 DECL_INITIAL (r) = TREE_TYPE (r);
10787 else
10788 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10789 complain, in_decl);
10790 }
10791
10792 DECL_CONTEXT (r) = NULL_TREE;
10793
10794 if (!DECL_TEMPLATE_PARM_P (r))
10795 DECL_ARG_TYPE (r) = type_passed_as (type);
10796
10797 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10798 args, complain, in_decl);
10799
10800 /* Keep track of the first new parameter we
10801 generate. That's what will be returned to the
10802 caller. */
10803 if (!first_r)
10804 first_r = r;
10805
10806 /* Build a proper chain of parameters when substituting
10807 into a function parameter pack. */
10808 if (prev_r)
10809 DECL_CHAIN (prev_r) = r;
10810 }
10811
10812 /* If cp_unevaluated_operand is set, we're just looking for a
10813 single dummy parameter, so don't keep going. */
10814 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10815 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10816 complain, DECL_CHAIN (t));
10817
10818 /* FIRST_R contains the start of the chain we've built. */
10819 r = first_r;
10820 }
10821 break;
10822
10823 case FIELD_DECL:
10824 {
10825 tree type = NULL_TREE;
10826 tree vec = NULL_TREE;
10827 tree expanded_types = NULL_TREE;
10828 int len = 1;
10829
10830 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10831 {
10832 /* This field is a lambda capture pack. Return a TREE_VEC of
10833 the expanded fields to instantiate_class_template_1 and
10834 store them in the specializations hash table as a
10835 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10836 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10837 complain, in_decl);
10838 if (TREE_CODE (expanded_types) == TREE_VEC)
10839 {
10840 len = TREE_VEC_LENGTH (expanded_types);
10841 vec = make_tree_vec (len);
10842 }
10843 else
10844 {
10845 /* All we did was update the type. Make a note of that. */
10846 type = expanded_types;
10847 expanded_types = NULL_TREE;
10848 }
10849 }
10850
10851 for (int i = 0; i < len; ++i)
10852 {
10853 r = copy_decl (t);
10854 if (expanded_types)
10855 {
10856 type = TREE_VEC_ELT (expanded_types, i);
10857 DECL_NAME (r)
10858 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10859 }
10860 else if (!type)
10861 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10862
10863 if (type == error_mark_node)
10864 RETURN (error_mark_node);
10865 TREE_TYPE (r) = type;
10866 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10867
10868 if (DECL_C_BIT_FIELD (r))
10869 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10870 non-bit-fields DECL_INITIAL is a non-static data member
10871 initializer, which gets deferred instantiation. */
10872 DECL_INITIAL (r)
10873 = tsubst_expr (DECL_INITIAL (t), args,
10874 complain, in_decl,
10875 /*integral_constant_expression_p=*/true);
10876 else if (DECL_INITIAL (t))
10877 {
10878 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10879 NSDMI in perform_member_init. Still set DECL_INITIAL
10880 so that we know there is one. */
10881 DECL_INITIAL (r) = void_zero_node;
10882 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10883 retrofit_lang_decl (r);
10884 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10885 }
10886 /* We don't have to set DECL_CONTEXT here; it is set by
10887 finish_member_declaration. */
10888 DECL_CHAIN (r) = NULL_TREE;
10889
10890 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10891 args, complain, in_decl);
10892
10893 if (vec)
10894 TREE_VEC_ELT (vec, i) = r;
10895 }
10896
10897 if (vec)
10898 {
10899 r = vec;
10900 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10901 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10902 SET_ARGUMENT_PACK_ARGS (pack, vec);
10903 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10904 TREE_TYPE (pack) = tpack;
10905 register_specialization (pack, t, args, false, 0);
10906 }
10907 }
10908 break;
10909
10910 case USING_DECL:
10911 /* We reach here only for member using decls. We also need to check
10912 uses_template_parms because DECL_DEPENDENT_P is not set for a
10913 using-declaration that designates a member of the current
10914 instantiation (c++/53549). */
10915 if (DECL_DEPENDENT_P (t)
10916 || uses_template_parms (USING_DECL_SCOPE (t)))
10917 {
10918 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10919 complain, in_decl);
10920 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10921 r = do_class_using_decl (inst_scope, name);
10922 if (!r)
10923 r = error_mark_node;
10924 else
10925 {
10926 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10927 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10928 }
10929 }
10930 else
10931 {
10932 r = copy_node (t);
10933 DECL_CHAIN (r) = NULL_TREE;
10934 }
10935 break;
10936
10937 case TYPE_DECL:
10938 case VAR_DECL:
10939 {
10940 tree argvec = NULL_TREE;
10941 tree gen_tmpl = NULL_TREE;
10942 tree spec;
10943 tree tmpl = NULL_TREE;
10944 tree ctx;
10945 tree type = NULL_TREE;
10946 bool local_p;
10947
10948 if (TREE_TYPE (t) == error_mark_node)
10949 RETURN (error_mark_node);
10950
10951 if (TREE_CODE (t) == TYPE_DECL
10952 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10953 {
10954 /* If this is the canonical decl, we don't have to
10955 mess with instantiations, and often we can't (for
10956 typename, template type parms and such). Note that
10957 TYPE_NAME is not correct for the above test if
10958 we've copied the type for a typedef. */
10959 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10960 if (type == error_mark_node)
10961 RETURN (error_mark_node);
10962 r = TYPE_NAME (type);
10963 break;
10964 }
10965
10966 /* Check to see if we already have the specialization we
10967 need. */
10968 spec = NULL_TREE;
10969 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10970 {
10971 /* T is a static data member or namespace-scope entity.
10972 We have to substitute into namespace-scope variables
10973 (even though such entities are never templates) because
10974 of cases like:
10975
10976 template <class T> void f() { extern T t; }
10977
10978 where the entity referenced is not known until
10979 instantiation time. */
10980 local_p = false;
10981 ctx = DECL_CONTEXT (t);
10982 if (DECL_CLASS_SCOPE_P (t))
10983 {
10984 ctx = tsubst_aggr_type (ctx, args,
10985 complain,
10986 in_decl, /*entering_scope=*/1);
10987 /* If CTX is unchanged, then T is in fact the
10988 specialization we want. That situation occurs when
10989 referencing a static data member within in its own
10990 class. We can use pointer equality, rather than
10991 same_type_p, because DECL_CONTEXT is always
10992 canonical... */
10993 if (ctx == DECL_CONTEXT (t)
10994 && (TREE_CODE (t) != TYPE_DECL
10995 /* ... unless T is a member template; in which
10996 case our caller can be willing to create a
10997 specialization of that template represented
10998 by T. */
10999 || !(DECL_TI_TEMPLATE (t)
11000 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
11001 spec = t;
11002 }
11003
11004 if (!spec)
11005 {
11006 tmpl = DECL_TI_TEMPLATE (t);
11007 gen_tmpl = most_general_template (tmpl);
11008 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11009 if (argvec == error_mark_node)
11010 RETURN (error_mark_node);
11011 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11012 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11013 }
11014 }
11015 else
11016 {
11017 /* A local variable. */
11018 local_p = true;
11019 /* Subsequent calls to pushdecl will fill this in. */
11020 ctx = NULL_TREE;
11021 spec = retrieve_local_specialization (t);
11022 }
11023 /* If we already have the specialization we need, there is
11024 nothing more to do. */
11025 if (spec)
11026 {
11027 r = spec;
11028 break;
11029 }
11030
11031 /* Create a new node for the specialization we need. */
11032 r = copy_decl (t);
11033 if (type == NULL_TREE)
11034 {
11035 if (is_typedef_decl (t))
11036 type = DECL_ORIGINAL_TYPE (t);
11037 else
11038 type = TREE_TYPE (t);
11039 if (VAR_P (t)
11040 && VAR_HAD_UNKNOWN_BOUND (t)
11041 && type != error_mark_node)
11042 type = strip_array_domain (type);
11043 type = tsubst (type, args, complain, in_decl);
11044 }
11045 if (VAR_P (r))
11046 {
11047 /* Even if the original location is out of scope, the
11048 newly substituted one is not. */
11049 DECL_DEAD_FOR_LOCAL (r) = 0;
11050 DECL_INITIALIZED_P (r) = 0;
11051 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11052 if (type == error_mark_node)
11053 RETURN (error_mark_node);
11054 if (TREE_CODE (type) == FUNCTION_TYPE)
11055 {
11056 /* It may seem that this case cannot occur, since:
11057
11058 typedef void f();
11059 void g() { f x; }
11060
11061 declares a function, not a variable. However:
11062
11063 typedef void f();
11064 template <typename T> void g() { T t; }
11065 template void g<f>();
11066
11067 is an attempt to declare a variable with function
11068 type. */
11069 error ("variable %qD has function type",
11070 /* R is not yet sufficiently initialized, so we
11071 just use its name. */
11072 DECL_NAME (r));
11073 RETURN (error_mark_node);
11074 }
11075 type = complete_type (type);
11076 /* Wait until cp_finish_decl to set this again, to handle
11077 circular dependency (template/instantiate6.C). */
11078 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11079 type = check_var_type (DECL_NAME (r), type);
11080
11081 if (DECL_HAS_VALUE_EXPR_P (t))
11082 {
11083 tree ve = DECL_VALUE_EXPR (t);
11084 ve = tsubst_expr (ve, args, complain, in_decl,
11085 /*constant_expression_p=*/false);
11086 if (REFERENCE_REF_P (ve))
11087 {
11088 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11089 ve = TREE_OPERAND (ve, 0);
11090 }
11091 SET_DECL_VALUE_EXPR (r, ve);
11092 }
11093 }
11094 else if (DECL_SELF_REFERENCE_P (t))
11095 SET_DECL_SELF_REFERENCE_P (r);
11096 TREE_TYPE (r) = type;
11097 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11098 DECL_CONTEXT (r) = ctx;
11099 /* Clear out the mangled name and RTL for the instantiation. */
11100 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11101 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11102 SET_DECL_RTL (r, NULL);
11103 /* The initializer must not be expanded until it is required;
11104 see [temp.inst]. */
11105 DECL_INITIAL (r) = NULL_TREE;
11106 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11107 SET_DECL_RTL (r, NULL);
11108 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11109 if (VAR_P (r))
11110 {
11111 /* Possibly limit visibility based on template args. */
11112 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11113 if (DECL_VISIBILITY_SPECIFIED (t))
11114 {
11115 DECL_VISIBILITY_SPECIFIED (r) = 0;
11116 DECL_ATTRIBUTES (r)
11117 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11118 }
11119 determine_visibility (r);
11120 }
11121
11122 if (!local_p)
11123 {
11124 /* A static data member declaration is always marked
11125 external when it is declared in-class, even if an
11126 initializer is present. We mimic the non-template
11127 processing here. */
11128 DECL_EXTERNAL (r) = 1;
11129
11130 register_specialization (r, gen_tmpl, argvec, false, hash);
11131 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11132 SET_DECL_IMPLICIT_INSTANTIATION (r);
11133 }
11134 else if (!cp_unevaluated_operand)
11135 register_local_specialization (r, t);
11136
11137 DECL_CHAIN (r) = NULL_TREE;
11138
11139 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11140 /*flags=*/0,
11141 args, complain, in_decl);
11142
11143 /* Preserve a typedef that names a type. */
11144 if (is_typedef_decl (r))
11145 {
11146 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11147 set_underlying_type (r);
11148 }
11149
11150 layout_decl (r, 0);
11151 }
11152 break;
11153
11154 default:
11155 gcc_unreachable ();
11156 }
11157 #undef RETURN
11158
11159 out:
11160 /* Restore the file and line information. */
11161 input_location = saved_loc;
11162
11163 return r;
11164 }
11165
11166 /* Substitute into the ARG_TYPES of a function type.
11167 If END is a TREE_CHAIN, leave it and any following types
11168 un-substituted. */
11169
11170 static tree
11171 tsubst_arg_types (tree arg_types,
11172 tree args,
11173 tree end,
11174 tsubst_flags_t complain,
11175 tree in_decl)
11176 {
11177 tree remaining_arg_types;
11178 tree type = NULL_TREE;
11179 int i = 1;
11180 tree expanded_args = NULL_TREE;
11181 tree default_arg;
11182
11183 if (!arg_types || arg_types == void_list_node || arg_types == end)
11184 return arg_types;
11185
11186 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11187 args, end, complain, in_decl);
11188 if (remaining_arg_types == error_mark_node)
11189 return error_mark_node;
11190
11191 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11192 {
11193 /* For a pack expansion, perform substitution on the
11194 entire expression. Later on, we'll handle the arguments
11195 one-by-one. */
11196 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11197 args, complain, in_decl);
11198
11199 if (TREE_CODE (expanded_args) == TREE_VEC)
11200 /* So that we'll spin through the parameters, one by one. */
11201 i = TREE_VEC_LENGTH (expanded_args);
11202 else
11203 {
11204 /* We only partially substituted into the parameter
11205 pack. Our type is TYPE_PACK_EXPANSION. */
11206 type = expanded_args;
11207 expanded_args = NULL_TREE;
11208 }
11209 }
11210
11211 while (i > 0) {
11212 --i;
11213
11214 if (expanded_args)
11215 type = TREE_VEC_ELT (expanded_args, i);
11216 else if (!type)
11217 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11218
11219 if (type == error_mark_node)
11220 return error_mark_node;
11221 if (VOID_TYPE_P (type))
11222 {
11223 if (complain & tf_error)
11224 {
11225 error ("invalid parameter type %qT", type);
11226 if (in_decl)
11227 error ("in declaration %q+D", in_decl);
11228 }
11229 return error_mark_node;
11230 }
11231 /* DR 657. */
11232 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11233 return error_mark_node;
11234
11235 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11236 top-level qualifiers as required. */
11237 type = cv_unqualified (type_decays_to (type));
11238
11239 /* We do not substitute into default arguments here. The standard
11240 mandates that they be instantiated only when needed, which is
11241 done in build_over_call. */
11242 default_arg = TREE_PURPOSE (arg_types);
11243
11244 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11245 {
11246 /* We've instantiated a template before its default arguments
11247 have been parsed. This can happen for a nested template
11248 class, and is not an error unless we require the default
11249 argument in a call of this function. */
11250 remaining_arg_types =
11251 tree_cons (default_arg, type, remaining_arg_types);
11252 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11253 }
11254 else
11255 remaining_arg_types =
11256 hash_tree_cons (default_arg, type, remaining_arg_types);
11257 }
11258
11259 return remaining_arg_types;
11260 }
11261
11262 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11263 *not* handle the exception-specification for FNTYPE, because the
11264 initial substitution of explicitly provided template parameters
11265 during argument deduction forbids substitution into the
11266 exception-specification:
11267
11268 [temp.deduct]
11269
11270 All references in the function type of the function template to the
11271 corresponding template parameters are replaced by the specified tem-
11272 plate argument values. If a substitution in a template parameter or
11273 in the function type of the function template results in an invalid
11274 type, type deduction fails. [Note: The equivalent substitution in
11275 exception specifications is done only when the function is instanti-
11276 ated, at which point a program is ill-formed if the substitution
11277 results in an invalid type.] */
11278
11279 static tree
11280 tsubst_function_type (tree t,
11281 tree args,
11282 tsubst_flags_t complain,
11283 tree in_decl)
11284 {
11285 tree return_type;
11286 tree arg_types;
11287 tree fntype;
11288
11289 /* The TYPE_CONTEXT is not used for function/method types. */
11290 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11291
11292 /* Substitute the return type. */
11293 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11294 if (return_type == error_mark_node)
11295 return error_mark_node;
11296 /* DR 486 clarifies that creation of a function type with an
11297 invalid return type is a deduction failure. */
11298 if (TREE_CODE (return_type) == ARRAY_TYPE
11299 || TREE_CODE (return_type) == FUNCTION_TYPE)
11300 {
11301 if (complain & tf_error)
11302 {
11303 if (TREE_CODE (return_type) == ARRAY_TYPE)
11304 error ("function returning an array");
11305 else
11306 error ("function returning a function");
11307 }
11308 return error_mark_node;
11309 }
11310 /* And DR 657. */
11311 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11312 return error_mark_node;
11313
11314 /* Substitute the argument types. */
11315 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11316 complain, in_decl);
11317 if (arg_types == error_mark_node)
11318 return error_mark_node;
11319
11320 /* Construct a new type node and return it. */
11321 if (TREE_CODE (t) == FUNCTION_TYPE)
11322 {
11323 fntype = build_function_type (return_type, arg_types);
11324 fntype = apply_memfn_quals (fntype,
11325 type_memfn_quals (t),
11326 type_memfn_rqual (t));
11327 }
11328 else
11329 {
11330 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11331 /* Don't pick up extra function qualifiers from the basetype. */
11332 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11333 if (! MAYBE_CLASS_TYPE_P (r))
11334 {
11335 /* [temp.deduct]
11336
11337 Type deduction may fail for any of the following
11338 reasons:
11339
11340 -- Attempting to create "pointer to member of T" when T
11341 is not a class type. */
11342 if (complain & tf_error)
11343 error ("creating pointer to member function of non-class type %qT",
11344 r);
11345 return error_mark_node;
11346 }
11347
11348 fntype = build_method_type_directly (r, return_type,
11349 TREE_CHAIN (arg_types));
11350 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11351 }
11352 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11353
11354 return fntype;
11355 }
11356
11357 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11358 ARGS into that specification, and return the substituted
11359 specification. If there is no specification, return NULL_TREE. */
11360
11361 static tree
11362 tsubst_exception_specification (tree fntype,
11363 tree args,
11364 tsubst_flags_t complain,
11365 tree in_decl,
11366 bool defer_ok)
11367 {
11368 tree specs;
11369 tree new_specs;
11370
11371 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11372 new_specs = NULL_TREE;
11373 if (specs && TREE_PURPOSE (specs))
11374 {
11375 /* A noexcept-specifier. */
11376 tree expr = TREE_PURPOSE (specs);
11377 if (TREE_CODE (expr) == INTEGER_CST)
11378 new_specs = expr;
11379 else if (defer_ok)
11380 {
11381 /* Defer instantiation of noexcept-specifiers to avoid
11382 excessive instantiations (c++/49107). */
11383 new_specs = make_node (DEFERRED_NOEXCEPT);
11384 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11385 {
11386 /* We already partially instantiated this member template,
11387 so combine the new args with the old. */
11388 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11389 = DEFERRED_NOEXCEPT_PATTERN (expr);
11390 DEFERRED_NOEXCEPT_ARGS (new_specs)
11391 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11392 }
11393 else
11394 {
11395 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11396 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11397 }
11398 }
11399 else
11400 new_specs = tsubst_copy_and_build
11401 (expr, args, complain, in_decl, /*function_p=*/false,
11402 /*integral_constant_expression_p=*/true);
11403 new_specs = build_noexcept_spec (new_specs, complain);
11404 }
11405 else if (specs)
11406 {
11407 if (! TREE_VALUE (specs))
11408 new_specs = specs;
11409 else
11410 while (specs)
11411 {
11412 tree spec;
11413 int i, len = 1;
11414 tree expanded_specs = NULL_TREE;
11415
11416 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11417 {
11418 /* Expand the pack expansion type. */
11419 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11420 args, complain,
11421 in_decl);
11422
11423 if (expanded_specs == error_mark_node)
11424 return error_mark_node;
11425 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11426 len = TREE_VEC_LENGTH (expanded_specs);
11427 else
11428 {
11429 /* We're substituting into a member template, so
11430 we got a TYPE_PACK_EXPANSION back. Add that
11431 expansion and move on. */
11432 gcc_assert (TREE_CODE (expanded_specs)
11433 == TYPE_PACK_EXPANSION);
11434 new_specs = add_exception_specifier (new_specs,
11435 expanded_specs,
11436 complain);
11437 specs = TREE_CHAIN (specs);
11438 continue;
11439 }
11440 }
11441
11442 for (i = 0; i < len; ++i)
11443 {
11444 if (expanded_specs)
11445 spec = TREE_VEC_ELT (expanded_specs, i);
11446 else
11447 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11448 if (spec == error_mark_node)
11449 return spec;
11450 new_specs = add_exception_specifier (new_specs, spec,
11451 complain);
11452 }
11453
11454 specs = TREE_CHAIN (specs);
11455 }
11456 }
11457 return new_specs;
11458 }
11459
11460 /* Take the tree structure T and replace template parameters used
11461 therein with the argument vector ARGS. IN_DECL is an associated
11462 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11463 Issue error and warning messages under control of COMPLAIN. Note
11464 that we must be relatively non-tolerant of extensions here, in
11465 order to preserve conformance; if we allow substitutions that
11466 should not be allowed, we may allow argument deductions that should
11467 not succeed, and therefore report ambiguous overload situations
11468 where there are none. In theory, we could allow the substitution,
11469 but indicate that it should have failed, and allow our caller to
11470 make sure that the right thing happens, but we don't try to do this
11471 yet.
11472
11473 This function is used for dealing with types, decls and the like;
11474 for expressions, use tsubst_expr or tsubst_copy. */
11475
11476 tree
11477 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11478 {
11479 enum tree_code code;
11480 tree type, r = NULL_TREE;
11481
11482 if (t == NULL_TREE || t == error_mark_node
11483 || t == integer_type_node
11484 || t == void_type_node
11485 || t == char_type_node
11486 || t == unknown_type_node
11487 || TREE_CODE (t) == NAMESPACE_DECL
11488 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11489 return t;
11490
11491 if (DECL_P (t))
11492 return tsubst_decl (t, args, complain);
11493
11494 if (args == NULL_TREE)
11495 return t;
11496
11497 code = TREE_CODE (t);
11498
11499 if (code == IDENTIFIER_NODE)
11500 type = IDENTIFIER_TYPE_VALUE (t);
11501 else
11502 type = TREE_TYPE (t);
11503
11504 gcc_assert (type != unknown_type_node);
11505
11506 /* Reuse typedefs. We need to do this to handle dependent attributes,
11507 such as attribute aligned. */
11508 if (TYPE_P (t)
11509 && typedef_variant_p (t))
11510 {
11511 tree decl = TYPE_NAME (t);
11512
11513 if (alias_template_specialization_p (t))
11514 {
11515 /* DECL represents an alias template and we want to
11516 instantiate it. */
11517 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11518 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11519 r = instantiate_alias_template (tmpl, gen_args, complain);
11520 }
11521 else if (DECL_CLASS_SCOPE_P (decl)
11522 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11523 && uses_template_parms (DECL_CONTEXT (decl)))
11524 {
11525 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11526 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11527 r = retrieve_specialization (tmpl, gen_args, 0);
11528 }
11529 else if (DECL_FUNCTION_SCOPE_P (decl)
11530 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11531 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11532 r = retrieve_local_specialization (decl);
11533 else
11534 /* The typedef is from a non-template context. */
11535 return t;
11536
11537 if (r)
11538 {
11539 r = TREE_TYPE (r);
11540 r = cp_build_qualified_type_real
11541 (r, cp_type_quals (t) | cp_type_quals (r),
11542 complain | tf_ignore_bad_quals);
11543 return r;
11544 }
11545 else
11546 {
11547 /* We don't have an instantiation yet, so drop the typedef. */
11548 int quals = cp_type_quals (t);
11549 t = DECL_ORIGINAL_TYPE (decl);
11550 t = cp_build_qualified_type_real (t, quals,
11551 complain | tf_ignore_bad_quals);
11552 }
11553 }
11554
11555 if (type
11556 && code != TYPENAME_TYPE
11557 && code != TEMPLATE_TYPE_PARM
11558 && code != IDENTIFIER_NODE
11559 && code != FUNCTION_TYPE
11560 && code != METHOD_TYPE)
11561 type = tsubst (type, args, complain, in_decl);
11562 if (type == error_mark_node)
11563 return error_mark_node;
11564
11565 switch (code)
11566 {
11567 case RECORD_TYPE:
11568 case UNION_TYPE:
11569 case ENUMERAL_TYPE:
11570 return tsubst_aggr_type (t, args, complain, in_decl,
11571 /*entering_scope=*/0);
11572
11573 case ERROR_MARK:
11574 case IDENTIFIER_NODE:
11575 case VOID_TYPE:
11576 case REAL_TYPE:
11577 case COMPLEX_TYPE:
11578 case VECTOR_TYPE:
11579 case BOOLEAN_TYPE:
11580 case NULLPTR_TYPE:
11581 case LANG_TYPE:
11582 return t;
11583
11584 case INTEGER_TYPE:
11585 if (t == integer_type_node)
11586 return t;
11587
11588 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11589 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11590 return t;
11591
11592 {
11593 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11594
11595 max = tsubst_expr (omax, args, complain, in_decl,
11596 /*integral_constant_expression_p=*/false);
11597
11598 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11599 needed. */
11600 if (TREE_CODE (max) == NOP_EXPR
11601 && TREE_SIDE_EFFECTS (omax)
11602 && !TREE_TYPE (max))
11603 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11604
11605 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11606 with TREE_SIDE_EFFECTS that indicates this is not an integral
11607 constant expression. */
11608 if (processing_template_decl
11609 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11610 {
11611 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11612 TREE_SIDE_EFFECTS (max) = 1;
11613 }
11614
11615 return compute_array_index_type (NULL_TREE, max, complain);
11616 }
11617
11618 case TEMPLATE_TYPE_PARM:
11619 case TEMPLATE_TEMPLATE_PARM:
11620 case BOUND_TEMPLATE_TEMPLATE_PARM:
11621 case TEMPLATE_PARM_INDEX:
11622 {
11623 int idx;
11624 int level;
11625 int levels;
11626 tree arg = NULL_TREE;
11627
11628 r = NULL_TREE;
11629
11630 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11631 template_parm_level_and_index (t, &level, &idx);
11632
11633 levels = TMPL_ARGS_DEPTH (args);
11634 if (level <= levels)
11635 {
11636 arg = TMPL_ARG (args, level, idx);
11637
11638 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11639 {
11640 /* See through ARGUMENT_PACK_SELECT arguments. */
11641 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11642 /* If the selected argument is an expansion E, that most
11643 likely means we were called from
11644 gen_elem_of_pack_expansion_instantiation during the
11645 substituting of pack an argument pack (which Ith
11646 element is a pack expansion, where I is
11647 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11648 In this case, the Ith element resulting from this
11649 substituting is going to be a pack expansion, which
11650 pattern is the pattern of E. Let's return the
11651 pattern of E, and
11652 gen_elem_of_pack_expansion_instantiation will
11653 build the resulting pack expansion from it. */
11654 if (PACK_EXPANSION_P (arg))
11655 arg = PACK_EXPANSION_PATTERN (arg);
11656 }
11657 }
11658
11659 if (arg == error_mark_node)
11660 return error_mark_node;
11661 else if (arg != NULL_TREE)
11662 {
11663 if (ARGUMENT_PACK_P (arg))
11664 /* If ARG is an argument pack, we don't actually want to
11665 perform a substitution here, because substitutions
11666 for argument packs are only done
11667 element-by-element. We can get to this point when
11668 substituting the type of a non-type template
11669 parameter pack, when that type actually contains
11670 template parameter packs from an outer template, e.g.,
11671
11672 template<typename... Types> struct A {
11673 template<Types... Values> struct B { };
11674 }; */
11675 return t;
11676
11677 if (code == TEMPLATE_TYPE_PARM)
11678 {
11679 int quals;
11680 gcc_assert (TYPE_P (arg));
11681
11682 quals = cp_type_quals (arg) | cp_type_quals (t);
11683
11684 return cp_build_qualified_type_real
11685 (arg, quals, complain | tf_ignore_bad_quals);
11686 }
11687 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11688 {
11689 /* We are processing a type constructed from a
11690 template template parameter. */
11691 tree argvec = tsubst (TYPE_TI_ARGS (t),
11692 args, complain, in_decl);
11693 if (argvec == error_mark_node)
11694 return error_mark_node;
11695
11696 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11697 || TREE_CODE (arg) == TEMPLATE_DECL
11698 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11699
11700 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11701 /* Consider this code:
11702
11703 template <template <class> class Template>
11704 struct Internal {
11705 template <class Arg> using Bind = Template<Arg>;
11706 };
11707
11708 template <template <class> class Template, class Arg>
11709 using Instantiate = Template<Arg>; //#0
11710
11711 template <template <class> class Template,
11712 class Argument>
11713 using Bind =
11714 Instantiate<Internal<Template>::template Bind,
11715 Argument>; //#1
11716
11717 When #1 is parsed, the
11718 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11719 parameter `Template' in #0 matches the
11720 UNBOUND_CLASS_TEMPLATE representing the argument
11721 `Internal<Template>::template Bind'; We then want
11722 to assemble the type `Bind<Argument>' that can't
11723 be fully created right now, because
11724 `Internal<Template>' not being complete, the Bind
11725 template cannot be looked up in that context. So
11726 we need to "store" `Bind<Argument>' for later
11727 when the context of Bind becomes complete. Let's
11728 store that in a TYPENAME_TYPE. */
11729 return make_typename_type (TYPE_CONTEXT (arg),
11730 build_nt (TEMPLATE_ID_EXPR,
11731 TYPE_IDENTIFIER (arg),
11732 argvec),
11733 typename_type,
11734 complain);
11735
11736 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11737 are resolving nested-types in the signature of a
11738 member function templates. Otherwise ARG is a
11739 TEMPLATE_DECL and is the real template to be
11740 instantiated. */
11741 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11742 arg = TYPE_NAME (arg);
11743
11744 r = lookup_template_class (arg,
11745 argvec, in_decl,
11746 DECL_CONTEXT (arg),
11747 /*entering_scope=*/0,
11748 complain);
11749 return cp_build_qualified_type_real
11750 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11751 }
11752 else
11753 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11754 return convert_from_reference (unshare_expr (arg));
11755 }
11756
11757 if (level == 1)
11758 /* This can happen during the attempted tsubst'ing in
11759 unify. This means that we don't yet have any information
11760 about the template parameter in question. */
11761 return t;
11762
11763 /* Early in template argument deduction substitution, we don't
11764 want to reduce the level of 'auto', or it will be confused
11765 with a normal template parm in subsequent deduction. */
11766 if (is_auto (t) && (complain & tf_partial))
11767 return t;
11768
11769 /* If we get here, we must have been looking at a parm for a
11770 more deeply nested template. Make a new version of this
11771 template parameter, but with a lower level. */
11772 switch (code)
11773 {
11774 case TEMPLATE_TYPE_PARM:
11775 case TEMPLATE_TEMPLATE_PARM:
11776 case BOUND_TEMPLATE_TEMPLATE_PARM:
11777 if (cp_type_quals (t))
11778 {
11779 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11780 r = cp_build_qualified_type_real
11781 (r, cp_type_quals (t),
11782 complain | (code == TEMPLATE_TYPE_PARM
11783 ? tf_ignore_bad_quals : 0));
11784 }
11785 else
11786 {
11787 r = copy_type (t);
11788 TEMPLATE_TYPE_PARM_INDEX (r)
11789 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11790 r, levels, args, complain);
11791 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11792 TYPE_MAIN_VARIANT (r) = r;
11793 TYPE_POINTER_TO (r) = NULL_TREE;
11794 TYPE_REFERENCE_TO (r) = NULL_TREE;
11795
11796 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11797 /* We have reduced the level of the template
11798 template parameter, but not the levels of its
11799 template parameters, so canonical_type_parameter
11800 will not be able to find the canonical template
11801 template parameter for this level. Thus, we
11802 require structural equality checking to compare
11803 TEMPLATE_TEMPLATE_PARMs. */
11804 SET_TYPE_STRUCTURAL_EQUALITY (r);
11805 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11806 SET_TYPE_STRUCTURAL_EQUALITY (r);
11807 else
11808 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11809
11810 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11811 {
11812 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11813 complain, in_decl);
11814 if (argvec == error_mark_node)
11815 return error_mark_node;
11816
11817 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11818 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11819 }
11820 }
11821 break;
11822
11823 case TEMPLATE_PARM_INDEX:
11824 r = reduce_template_parm_level (t, type, levels, args, complain);
11825 break;
11826
11827 default:
11828 gcc_unreachable ();
11829 }
11830
11831 return r;
11832 }
11833
11834 case TREE_LIST:
11835 {
11836 tree purpose, value, chain;
11837
11838 if (t == void_list_node)
11839 return t;
11840
11841 purpose = TREE_PURPOSE (t);
11842 if (purpose)
11843 {
11844 purpose = tsubst (purpose, args, complain, in_decl);
11845 if (purpose == error_mark_node)
11846 return error_mark_node;
11847 }
11848 value = TREE_VALUE (t);
11849 if (value)
11850 {
11851 value = tsubst (value, args, complain, in_decl);
11852 if (value == error_mark_node)
11853 return error_mark_node;
11854 }
11855 chain = TREE_CHAIN (t);
11856 if (chain && chain != void_type_node)
11857 {
11858 chain = tsubst (chain, args, complain, in_decl);
11859 if (chain == error_mark_node)
11860 return error_mark_node;
11861 }
11862 if (purpose == TREE_PURPOSE (t)
11863 && value == TREE_VALUE (t)
11864 && chain == TREE_CHAIN (t))
11865 return t;
11866 return hash_tree_cons (purpose, value, chain);
11867 }
11868
11869 case TREE_BINFO:
11870 /* We should never be tsubsting a binfo. */
11871 gcc_unreachable ();
11872
11873 case TREE_VEC:
11874 /* A vector of template arguments. */
11875 gcc_assert (!type);
11876 return tsubst_template_args (t, args, complain, in_decl);
11877
11878 case POINTER_TYPE:
11879 case REFERENCE_TYPE:
11880 {
11881 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11882 return t;
11883
11884 /* [temp.deduct]
11885
11886 Type deduction may fail for any of the following
11887 reasons:
11888
11889 -- Attempting to create a pointer to reference type.
11890 -- Attempting to create a reference to a reference type or
11891 a reference to void.
11892
11893 Core issue 106 says that creating a reference to a reference
11894 during instantiation is no longer a cause for failure. We
11895 only enforce this check in strict C++98 mode. */
11896 if ((TREE_CODE (type) == REFERENCE_TYPE
11897 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11898 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11899 {
11900 static location_t last_loc;
11901
11902 /* We keep track of the last time we issued this error
11903 message to avoid spewing a ton of messages during a
11904 single bad template instantiation. */
11905 if (complain & tf_error
11906 && last_loc != input_location)
11907 {
11908 if (VOID_TYPE_P (type))
11909 error ("forming reference to void");
11910 else if (code == POINTER_TYPE)
11911 error ("forming pointer to reference type %qT", type);
11912 else
11913 error ("forming reference to reference type %qT", type);
11914 last_loc = input_location;
11915 }
11916
11917 return error_mark_node;
11918 }
11919 else if (TREE_CODE (type) == FUNCTION_TYPE
11920 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11921 || type_memfn_rqual (type) != REF_QUAL_NONE))
11922 {
11923 if (complain & tf_error)
11924 {
11925 if (code == POINTER_TYPE)
11926 error ("forming pointer to qualified function type %qT",
11927 type);
11928 else
11929 error ("forming reference to qualified function type %qT",
11930 type);
11931 }
11932 return error_mark_node;
11933 }
11934 else if (code == POINTER_TYPE)
11935 {
11936 r = build_pointer_type (type);
11937 if (TREE_CODE (type) == METHOD_TYPE)
11938 r = build_ptrmemfunc_type (r);
11939 }
11940 else if (TREE_CODE (type) == REFERENCE_TYPE)
11941 /* In C++0x, during template argument substitution, when there is an
11942 attempt to create a reference to a reference type, reference
11943 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11944
11945 "If a template-argument for a template-parameter T names a type
11946 that is a reference to a type A, an attempt to create the type
11947 'lvalue reference to cv T' creates the type 'lvalue reference to
11948 A,' while an attempt to create the type type rvalue reference to
11949 cv T' creates the type T"
11950 */
11951 r = cp_build_reference_type
11952 (TREE_TYPE (type),
11953 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11954 else
11955 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11956 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11957
11958 if (cxx_dialect >= cxx1y
11959 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11960 && array_of_runtime_bound_p (type)
11961 && (flag_iso || warn_vla > 0))
11962 {
11963 if (complain & tf_warning_or_error)
11964 pedwarn
11965 (input_location, OPT_Wvla,
11966 code == REFERENCE_TYPE
11967 ? G_("cannot declare reference to array of runtime bound")
11968 : G_("cannot declare pointer to array of runtime bound"));
11969 else
11970 r = error_mark_node;
11971 }
11972
11973 if (r != error_mark_node)
11974 /* Will this ever be needed for TYPE_..._TO values? */
11975 layout_type (r);
11976
11977 return r;
11978 }
11979 case OFFSET_TYPE:
11980 {
11981 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11982 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11983 {
11984 /* [temp.deduct]
11985
11986 Type deduction may fail for any of the following
11987 reasons:
11988
11989 -- Attempting to create "pointer to member of T" when T
11990 is not a class type. */
11991 if (complain & tf_error)
11992 error ("creating pointer to member of non-class type %qT", r);
11993 return error_mark_node;
11994 }
11995 if (TREE_CODE (type) == REFERENCE_TYPE)
11996 {
11997 if (complain & tf_error)
11998 error ("creating pointer to member reference type %qT", type);
11999 return error_mark_node;
12000 }
12001 if (VOID_TYPE_P (type))
12002 {
12003 if (complain & tf_error)
12004 error ("creating pointer to member of type void");
12005 return error_mark_node;
12006 }
12007 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12008 if (TREE_CODE (type) == FUNCTION_TYPE)
12009 {
12010 /* The type of the implicit object parameter gets its
12011 cv-qualifiers from the FUNCTION_TYPE. */
12012 tree memptr;
12013 tree method_type
12014 = build_memfn_type (type, r, type_memfn_quals (type),
12015 type_memfn_rqual (type));
12016 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12017 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12018 complain);
12019 }
12020 else
12021 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12022 cp_type_quals (t),
12023 complain);
12024 }
12025 case FUNCTION_TYPE:
12026 case METHOD_TYPE:
12027 {
12028 tree fntype;
12029 tree specs;
12030 fntype = tsubst_function_type (t, args, complain, in_decl);
12031 if (fntype == error_mark_node)
12032 return error_mark_node;
12033
12034 /* Substitute the exception specification. */
12035 specs = tsubst_exception_specification (t, args, complain,
12036 in_decl, /*defer_ok*/true);
12037 if (specs == error_mark_node)
12038 return error_mark_node;
12039 if (specs)
12040 fntype = build_exception_variant (fntype, specs);
12041 return fntype;
12042 }
12043 case ARRAY_TYPE:
12044 {
12045 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12046 if (domain == error_mark_node)
12047 return error_mark_node;
12048
12049 /* As an optimization, we avoid regenerating the array type if
12050 it will obviously be the same as T. */
12051 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12052 return t;
12053
12054 /* These checks should match the ones in grokdeclarator.
12055
12056 [temp.deduct]
12057
12058 The deduction may fail for any of the following reasons:
12059
12060 -- Attempting to create an array with an element type that
12061 is void, a function type, or a reference type, or [DR337]
12062 an abstract class type. */
12063 if (VOID_TYPE_P (type)
12064 || TREE_CODE (type) == FUNCTION_TYPE
12065 || TREE_CODE (type) == REFERENCE_TYPE)
12066 {
12067 if (complain & tf_error)
12068 error ("creating array of %qT", type);
12069 return error_mark_node;
12070 }
12071
12072 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12073 return error_mark_node;
12074
12075 r = build_cplus_array_type (type, domain);
12076
12077 if (TYPE_USER_ALIGN (t))
12078 {
12079 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12080 TYPE_USER_ALIGN (r) = 1;
12081 }
12082
12083 return r;
12084 }
12085
12086 case TYPENAME_TYPE:
12087 {
12088 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12089 in_decl, /*entering_scope=*/1);
12090 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12091 complain, in_decl);
12092
12093 if (ctx == error_mark_node || f == error_mark_node)
12094 return error_mark_node;
12095
12096 if (!MAYBE_CLASS_TYPE_P (ctx))
12097 {
12098 if (complain & tf_error)
12099 error ("%qT is not a class, struct, or union type", ctx);
12100 return error_mark_node;
12101 }
12102 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12103 {
12104 /* Normally, make_typename_type does not require that the CTX
12105 have complete type in order to allow things like:
12106
12107 template <class T> struct S { typename S<T>::X Y; };
12108
12109 But, such constructs have already been resolved by this
12110 point, so here CTX really should have complete type, unless
12111 it's a partial instantiation. */
12112 ctx = complete_type (ctx);
12113 if (!COMPLETE_TYPE_P (ctx))
12114 {
12115 if (complain & tf_error)
12116 cxx_incomplete_type_error (NULL_TREE, ctx);
12117 return error_mark_node;
12118 }
12119 }
12120
12121 f = make_typename_type (ctx, f, typename_type,
12122 complain | tf_keep_type_decl);
12123 if (f == error_mark_node)
12124 return f;
12125 if (TREE_CODE (f) == TYPE_DECL)
12126 {
12127 complain |= tf_ignore_bad_quals;
12128 f = TREE_TYPE (f);
12129 }
12130
12131 if (TREE_CODE (f) != TYPENAME_TYPE)
12132 {
12133 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12134 {
12135 if (complain & tf_error)
12136 error ("%qT resolves to %qT, which is not an enumeration type",
12137 t, f);
12138 else
12139 return error_mark_node;
12140 }
12141 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12142 {
12143 if (complain & tf_error)
12144 error ("%qT resolves to %qT, which is is not a class type",
12145 t, f);
12146 else
12147 return error_mark_node;
12148 }
12149 }
12150
12151 return cp_build_qualified_type_real
12152 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12153 }
12154
12155 case UNBOUND_CLASS_TEMPLATE:
12156 {
12157 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12158 in_decl, /*entering_scope=*/1);
12159 tree name = TYPE_IDENTIFIER (t);
12160 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12161
12162 if (ctx == error_mark_node || name == error_mark_node)
12163 return error_mark_node;
12164
12165 if (parm_list)
12166 parm_list = tsubst_template_parms (parm_list, args, complain);
12167 return make_unbound_class_template (ctx, name, parm_list, complain);
12168 }
12169
12170 case TYPEOF_TYPE:
12171 {
12172 tree type;
12173
12174 ++cp_unevaluated_operand;
12175 ++c_inhibit_evaluation_warnings;
12176
12177 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12178 complain, in_decl,
12179 /*integral_constant_expression_p=*/false);
12180
12181 --cp_unevaluated_operand;
12182 --c_inhibit_evaluation_warnings;
12183
12184 type = finish_typeof (type);
12185 return cp_build_qualified_type_real (type,
12186 cp_type_quals (t)
12187 | cp_type_quals (type),
12188 complain);
12189 }
12190
12191 case DECLTYPE_TYPE:
12192 {
12193 tree type;
12194
12195 ++cp_unevaluated_operand;
12196 ++c_inhibit_evaluation_warnings;
12197
12198 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12199 complain|tf_decltype, in_decl,
12200 /*function_p*/false,
12201 /*integral_constant_expression*/false);
12202
12203 --cp_unevaluated_operand;
12204 --c_inhibit_evaluation_warnings;
12205
12206 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12207 type = lambda_capture_field_type (type,
12208 DECLTYPE_FOR_INIT_CAPTURE (t));
12209 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12210 type = lambda_proxy_type (type);
12211 else
12212 {
12213 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12214 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12215 && EXPR_P (type))
12216 /* In a template ~id could be either a complement expression
12217 or an unqualified-id naming a destructor; if instantiating
12218 it produces an expression, it's not an id-expression or
12219 member access. */
12220 id = false;
12221 type = finish_decltype_type (type, id, complain);
12222 }
12223 return cp_build_qualified_type_real (type,
12224 cp_type_quals (t)
12225 | cp_type_quals (type),
12226 complain);
12227 }
12228
12229 case UNDERLYING_TYPE:
12230 {
12231 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12232 complain, in_decl);
12233 return finish_underlying_type (type);
12234 }
12235
12236 case TYPE_ARGUMENT_PACK:
12237 case NONTYPE_ARGUMENT_PACK:
12238 {
12239 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12240 tree packed_out =
12241 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12242 args,
12243 complain,
12244 in_decl);
12245 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12246
12247 /* For template nontype argument packs, also substitute into
12248 the type. */
12249 if (code == NONTYPE_ARGUMENT_PACK)
12250 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12251
12252 return r;
12253 }
12254 break;
12255
12256 case INTEGER_CST:
12257 case REAL_CST:
12258 case STRING_CST:
12259 case PLUS_EXPR:
12260 case MINUS_EXPR:
12261 case NEGATE_EXPR:
12262 case NOP_EXPR:
12263 case INDIRECT_REF:
12264 case ADDR_EXPR:
12265 case CALL_EXPR:
12266 case ARRAY_REF:
12267 case SCOPE_REF:
12268 /* We should use one of the expression tsubsts for these codes. */
12269 gcc_unreachable ();
12270
12271 default:
12272 sorry ("use of %qs in template", get_tree_code_name (code));
12273 return error_mark_node;
12274 }
12275 }
12276
12277 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12278 type of the expression on the left-hand side of the "." or "->"
12279 operator. */
12280
12281 static tree
12282 tsubst_baselink (tree baselink, tree object_type,
12283 tree args, tsubst_flags_t complain, tree in_decl)
12284 {
12285 tree name;
12286 tree qualifying_scope;
12287 tree fns;
12288 tree optype;
12289 tree template_args = 0;
12290 bool template_id_p = false;
12291 bool qualified = BASELINK_QUALIFIED_P (baselink);
12292
12293 /* A baselink indicates a function from a base class. Both the
12294 BASELINK_ACCESS_BINFO and the base class referenced may
12295 indicate bases of the template class, rather than the
12296 instantiated class. In addition, lookups that were not
12297 ambiguous before may be ambiguous now. Therefore, we perform
12298 the lookup again. */
12299 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12300 qualifying_scope = tsubst (qualifying_scope, args,
12301 complain, in_decl);
12302 fns = BASELINK_FUNCTIONS (baselink);
12303 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12304 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12305 {
12306 template_id_p = true;
12307 template_args = TREE_OPERAND (fns, 1);
12308 fns = TREE_OPERAND (fns, 0);
12309 if (template_args)
12310 template_args = tsubst_template_args (template_args, args,
12311 complain, in_decl);
12312 }
12313 name = DECL_NAME (get_first_fn (fns));
12314 if (IDENTIFIER_TYPENAME_P (name))
12315 name = mangle_conv_op_name_for_type (optype);
12316 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12317 if (!baselink)
12318 return error_mark_node;
12319
12320 /* If lookup found a single function, mark it as used at this
12321 point. (If it lookup found multiple functions the one selected
12322 later by overload resolution will be marked as used at that
12323 point.) */
12324 if (BASELINK_P (baselink))
12325 fns = BASELINK_FUNCTIONS (baselink);
12326 if (!template_id_p && !really_overloaded_fn (fns))
12327 mark_used (OVL_CURRENT (fns));
12328
12329 /* Add back the template arguments, if present. */
12330 if (BASELINK_P (baselink) && template_id_p)
12331 BASELINK_FUNCTIONS (baselink)
12332 = build_nt (TEMPLATE_ID_EXPR,
12333 BASELINK_FUNCTIONS (baselink),
12334 template_args);
12335 /* Update the conversion operator type. */
12336 BASELINK_OPTYPE (baselink) = optype;
12337
12338 if (!object_type)
12339 object_type = current_class_type;
12340
12341 if (qualified)
12342 baselink = adjust_result_of_qualified_name_lookup (baselink,
12343 qualifying_scope,
12344 object_type);
12345 return baselink;
12346 }
12347
12348 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12349 true if the qualified-id will be a postfix-expression in-and-of
12350 itself; false if more of the postfix-expression follows the
12351 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12352 of "&". */
12353
12354 static tree
12355 tsubst_qualified_id (tree qualified_id, tree args,
12356 tsubst_flags_t complain, tree in_decl,
12357 bool done, bool address_p)
12358 {
12359 tree expr;
12360 tree scope;
12361 tree name;
12362 bool is_template;
12363 tree template_args;
12364 location_t loc = UNKNOWN_LOCATION;
12365
12366 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12367
12368 /* Figure out what name to look up. */
12369 name = TREE_OPERAND (qualified_id, 1);
12370 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12371 {
12372 is_template = true;
12373 loc = EXPR_LOCATION (name);
12374 template_args = TREE_OPERAND (name, 1);
12375 if (template_args)
12376 template_args = tsubst_template_args (template_args, args,
12377 complain, in_decl);
12378 name = TREE_OPERAND (name, 0);
12379 }
12380 else
12381 {
12382 is_template = false;
12383 template_args = NULL_TREE;
12384 }
12385
12386 /* Substitute into the qualifying scope. When there are no ARGS, we
12387 are just trying to simplify a non-dependent expression. In that
12388 case the qualifying scope may be dependent, and, in any case,
12389 substituting will not help. */
12390 scope = TREE_OPERAND (qualified_id, 0);
12391 if (args)
12392 {
12393 scope = tsubst (scope, args, complain, in_decl);
12394 expr = tsubst_copy (name, args, complain, in_decl);
12395 }
12396 else
12397 expr = name;
12398
12399 if (dependent_scope_p (scope))
12400 {
12401 if (is_template)
12402 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12403 return build_qualified_name (NULL_TREE, scope, expr,
12404 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12405 }
12406
12407 if (!BASELINK_P (name) && !DECL_P (expr))
12408 {
12409 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12410 {
12411 /* A BIT_NOT_EXPR is used to represent a destructor. */
12412 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12413 {
12414 error ("qualifying type %qT does not match destructor name ~%qT",
12415 scope, TREE_OPERAND (expr, 0));
12416 expr = error_mark_node;
12417 }
12418 else
12419 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12420 /*is_type_p=*/0, false);
12421 }
12422 else
12423 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12424 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12425 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12426 {
12427 if (complain & tf_error)
12428 {
12429 error ("dependent-name %qE is parsed as a non-type, but "
12430 "instantiation yields a type", qualified_id);
12431 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12432 }
12433 return error_mark_node;
12434 }
12435 }
12436
12437 if (DECL_P (expr))
12438 {
12439 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12440 scope);
12441 /* Remember that there was a reference to this entity. */
12442 mark_used (expr);
12443 }
12444
12445 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12446 {
12447 if (complain & tf_error)
12448 qualified_name_lookup_error (scope,
12449 TREE_OPERAND (qualified_id, 1),
12450 expr, input_location);
12451 return error_mark_node;
12452 }
12453
12454 if (is_template)
12455 expr = lookup_template_function (expr, template_args);
12456
12457 if (expr == error_mark_node && complain & tf_error)
12458 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12459 expr, input_location);
12460 else if (TYPE_P (scope))
12461 {
12462 expr = (adjust_result_of_qualified_name_lookup
12463 (expr, scope, current_nonlambda_class_type ()));
12464 expr = (finish_qualified_id_expr
12465 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12466 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12467 /*template_arg_p=*/false, complain));
12468 }
12469
12470 /* Expressions do not generally have reference type. */
12471 if (TREE_CODE (expr) != SCOPE_REF
12472 /* However, if we're about to form a pointer-to-member, we just
12473 want the referenced member referenced. */
12474 && TREE_CODE (expr) != OFFSET_REF)
12475 expr = convert_from_reference (expr);
12476
12477 return expr;
12478 }
12479
12480 /* Like tsubst, but deals with expressions. This function just replaces
12481 template parms; to finish processing the resultant expression, use
12482 tsubst_copy_and_build or tsubst_expr. */
12483
12484 static tree
12485 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12486 {
12487 enum tree_code code;
12488 tree r;
12489
12490 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12491 return t;
12492
12493 code = TREE_CODE (t);
12494
12495 switch (code)
12496 {
12497 case PARM_DECL:
12498 r = retrieve_local_specialization (t);
12499
12500 if (r == NULL_TREE)
12501 {
12502 /* We get here for a use of 'this' in an NSDMI. */
12503 if (DECL_NAME (t) == this_identifier
12504 && at_function_scope_p ()
12505 && DECL_CONSTRUCTOR_P (current_function_decl))
12506 return current_class_ptr;
12507
12508 /* This can happen for a parameter name used later in a function
12509 declaration (such as in a late-specified return type). Just
12510 make a dummy decl, since it's only used for its type. */
12511 gcc_assert (cp_unevaluated_operand != 0);
12512 r = tsubst_decl (t, args, complain);
12513 /* Give it the template pattern as its context; its true context
12514 hasn't been instantiated yet and this is good enough for
12515 mangling. */
12516 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12517 }
12518
12519 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12520 r = ARGUMENT_PACK_SELECT_ARG (r);
12521 mark_used (r);
12522 return r;
12523
12524 case CONST_DECL:
12525 {
12526 tree enum_type;
12527 tree v;
12528
12529 if (DECL_TEMPLATE_PARM_P (t))
12530 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12531 /* There is no need to substitute into namespace-scope
12532 enumerators. */
12533 if (DECL_NAMESPACE_SCOPE_P (t))
12534 return t;
12535 /* If ARGS is NULL, then T is known to be non-dependent. */
12536 if (args == NULL_TREE)
12537 return integral_constant_value (t);
12538
12539 /* Unfortunately, we cannot just call lookup_name here.
12540 Consider:
12541
12542 template <int I> int f() {
12543 enum E { a = I };
12544 struct S { void g() { E e = a; } };
12545 };
12546
12547 When we instantiate f<7>::S::g(), say, lookup_name is not
12548 clever enough to find f<7>::a. */
12549 enum_type
12550 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12551 /*entering_scope=*/0);
12552
12553 for (v = TYPE_VALUES (enum_type);
12554 v != NULL_TREE;
12555 v = TREE_CHAIN (v))
12556 if (TREE_PURPOSE (v) == DECL_NAME (t))
12557 return TREE_VALUE (v);
12558
12559 /* We didn't find the name. That should never happen; if
12560 name-lookup found it during preliminary parsing, we
12561 should find it again here during instantiation. */
12562 gcc_unreachable ();
12563 }
12564 return t;
12565
12566 case FIELD_DECL:
12567 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12568 {
12569 /* Check for a local specialization set up by
12570 tsubst_pack_expansion. */
12571 if (tree r = retrieve_local_specialization (t))
12572 {
12573 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12574 r = ARGUMENT_PACK_SELECT_ARG (r);
12575 return r;
12576 }
12577
12578 /* When retrieving a capture pack from a generic lambda, remove the
12579 lambda call op's own template argument list from ARGS. Only the
12580 template arguments active for the closure type should be used to
12581 retrieve the pack specialization. */
12582 if (LAMBDA_FUNCTION_P (current_function_decl)
12583 && (template_class_depth (DECL_CONTEXT (t))
12584 != TMPL_ARGS_DEPTH (args)))
12585 args = strip_innermost_template_args (args, 1);
12586
12587 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12588 tsubst_decl put in the hash table. */
12589 return retrieve_specialization (t, args, 0);
12590 }
12591
12592 if (DECL_CONTEXT (t))
12593 {
12594 tree ctx;
12595
12596 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12597 /*entering_scope=*/1);
12598 if (ctx != DECL_CONTEXT (t))
12599 {
12600 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12601 if (!r)
12602 {
12603 if (complain & tf_error)
12604 error ("using invalid field %qD", t);
12605 return error_mark_node;
12606 }
12607 return r;
12608 }
12609 }
12610
12611 return t;
12612
12613 case VAR_DECL:
12614 case FUNCTION_DECL:
12615 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12616 r = tsubst (t, args, complain, in_decl);
12617 else if (local_variable_p (t))
12618 {
12619 r = retrieve_local_specialization (t);
12620 if (r == NULL_TREE)
12621 {
12622 if (DECL_ANON_UNION_VAR_P (t))
12623 {
12624 /* Just use name lookup to find a member alias for an
12625 anonymous union, but then add it to the hash table. */
12626 r = lookup_name (DECL_NAME (t));
12627 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12628 register_local_specialization (r, t);
12629 }
12630 else
12631 {
12632 /* This can happen for a variable used in a late-specified
12633 return type of a local lambda. Just make a dummy decl
12634 since it's only used for its type. */
12635 if (cp_unevaluated_operand)
12636 return tsubst_decl (t, args, complain);
12637 gcc_assert (errorcount || sorrycount);
12638 return error_mark_node;
12639 }
12640 }
12641 }
12642 else
12643 r = t;
12644 mark_used (r);
12645 return r;
12646
12647 case NAMESPACE_DECL:
12648 return t;
12649
12650 case OVERLOAD:
12651 /* An OVERLOAD will always be a non-dependent overload set; an
12652 overload set from function scope will just be represented with an
12653 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12654 gcc_assert (!uses_template_parms (t));
12655 return t;
12656
12657 case BASELINK:
12658 return tsubst_baselink (t, current_nonlambda_class_type (),
12659 args, complain, in_decl);
12660
12661 case TEMPLATE_DECL:
12662 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12663 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12664 args, complain, in_decl);
12665 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12666 return tsubst (t, args, complain, in_decl);
12667 else if (DECL_CLASS_SCOPE_P (t)
12668 && uses_template_parms (DECL_CONTEXT (t)))
12669 {
12670 /* Template template argument like the following example need
12671 special treatment:
12672
12673 template <template <class> class TT> struct C {};
12674 template <class T> struct D {
12675 template <class U> struct E {};
12676 C<E> c; // #1
12677 };
12678 D<int> d; // #2
12679
12680 We are processing the template argument `E' in #1 for
12681 the template instantiation #2. Originally, `E' is a
12682 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12683 have to substitute this with one having context `D<int>'. */
12684
12685 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12686 return lookup_field (context, DECL_NAME(t), 0, false);
12687 }
12688 else
12689 /* Ordinary template template argument. */
12690 return t;
12691
12692 case CAST_EXPR:
12693 case REINTERPRET_CAST_EXPR:
12694 case CONST_CAST_EXPR:
12695 case STATIC_CAST_EXPR:
12696 case DYNAMIC_CAST_EXPR:
12697 case IMPLICIT_CONV_EXPR:
12698 case CONVERT_EXPR:
12699 case NOP_EXPR:
12700 return build1
12701 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12702 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12703
12704 case SIZEOF_EXPR:
12705 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12706 {
12707
12708 tree expanded, op = TREE_OPERAND (t, 0);
12709 int len = 0;
12710
12711 if (SIZEOF_EXPR_TYPE_P (t))
12712 op = TREE_TYPE (op);
12713
12714 ++cp_unevaluated_operand;
12715 ++c_inhibit_evaluation_warnings;
12716 /* We only want to compute the number of arguments. */
12717 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12718 --cp_unevaluated_operand;
12719 --c_inhibit_evaluation_warnings;
12720
12721 if (TREE_CODE (expanded) == TREE_VEC)
12722 len = TREE_VEC_LENGTH (expanded);
12723
12724 if (expanded == error_mark_node)
12725 return error_mark_node;
12726 else if (PACK_EXPANSION_P (expanded)
12727 || (TREE_CODE (expanded) == TREE_VEC
12728 && len > 0
12729 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12730 {
12731 if (TREE_CODE (expanded) == TREE_VEC)
12732 expanded = TREE_VEC_ELT (expanded, len - 1);
12733
12734 if (TYPE_P (expanded))
12735 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12736 complain & tf_error);
12737 else
12738 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12739 complain & tf_error);
12740 }
12741 else
12742 return build_int_cst (size_type_node, len);
12743 }
12744 if (SIZEOF_EXPR_TYPE_P (t))
12745 {
12746 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12747 args, complain, in_decl);
12748 r = build1 (NOP_EXPR, r, error_mark_node);
12749 r = build1 (SIZEOF_EXPR,
12750 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12751 SIZEOF_EXPR_TYPE_P (r) = 1;
12752 return r;
12753 }
12754 /* Fall through */
12755
12756 case INDIRECT_REF:
12757 case NEGATE_EXPR:
12758 case TRUTH_NOT_EXPR:
12759 case BIT_NOT_EXPR:
12760 case ADDR_EXPR:
12761 case UNARY_PLUS_EXPR: /* Unary + */
12762 case ALIGNOF_EXPR:
12763 case AT_ENCODE_EXPR:
12764 case ARROW_EXPR:
12765 case THROW_EXPR:
12766 case TYPEID_EXPR:
12767 case REALPART_EXPR:
12768 case IMAGPART_EXPR:
12769 case PAREN_EXPR:
12770 return build1
12771 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12772 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12773
12774 case COMPONENT_REF:
12775 {
12776 tree object;
12777 tree name;
12778
12779 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12780 name = TREE_OPERAND (t, 1);
12781 if (TREE_CODE (name) == BIT_NOT_EXPR)
12782 {
12783 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12784 complain, in_decl);
12785 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12786 }
12787 else if (TREE_CODE (name) == SCOPE_REF
12788 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12789 {
12790 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12791 complain, in_decl);
12792 name = TREE_OPERAND (name, 1);
12793 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12794 complain, in_decl);
12795 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12796 name = build_qualified_name (/*type=*/NULL_TREE,
12797 base, name,
12798 /*template_p=*/false);
12799 }
12800 else if (BASELINK_P (name))
12801 name = tsubst_baselink (name,
12802 non_reference (TREE_TYPE (object)),
12803 args, complain,
12804 in_decl);
12805 else
12806 name = tsubst_copy (name, args, complain, in_decl);
12807 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12808 }
12809
12810 case PLUS_EXPR:
12811 case MINUS_EXPR:
12812 case MULT_EXPR:
12813 case TRUNC_DIV_EXPR:
12814 case CEIL_DIV_EXPR:
12815 case FLOOR_DIV_EXPR:
12816 case ROUND_DIV_EXPR:
12817 case EXACT_DIV_EXPR:
12818 case BIT_AND_EXPR:
12819 case BIT_IOR_EXPR:
12820 case BIT_XOR_EXPR:
12821 case TRUNC_MOD_EXPR:
12822 case FLOOR_MOD_EXPR:
12823 case TRUTH_ANDIF_EXPR:
12824 case TRUTH_ORIF_EXPR:
12825 case TRUTH_AND_EXPR:
12826 case TRUTH_OR_EXPR:
12827 case RSHIFT_EXPR:
12828 case LSHIFT_EXPR:
12829 case RROTATE_EXPR:
12830 case LROTATE_EXPR:
12831 case EQ_EXPR:
12832 case NE_EXPR:
12833 case MAX_EXPR:
12834 case MIN_EXPR:
12835 case LE_EXPR:
12836 case GE_EXPR:
12837 case LT_EXPR:
12838 case GT_EXPR:
12839 case COMPOUND_EXPR:
12840 case DOTSTAR_EXPR:
12841 case MEMBER_REF:
12842 case PREDECREMENT_EXPR:
12843 case PREINCREMENT_EXPR:
12844 case POSTDECREMENT_EXPR:
12845 case POSTINCREMENT_EXPR:
12846 return build_nt
12847 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12848 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12849
12850 case SCOPE_REF:
12851 return build_qualified_name (/*type=*/NULL_TREE,
12852 tsubst_copy (TREE_OPERAND (t, 0),
12853 args, complain, in_decl),
12854 tsubst_copy (TREE_OPERAND (t, 1),
12855 args, complain, in_decl),
12856 QUALIFIED_NAME_IS_TEMPLATE (t));
12857
12858 case ARRAY_REF:
12859 return build_nt
12860 (ARRAY_REF,
12861 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12862 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12863 NULL_TREE, NULL_TREE);
12864
12865 case CALL_EXPR:
12866 {
12867 int n = VL_EXP_OPERAND_LENGTH (t);
12868 tree result = build_vl_exp (CALL_EXPR, n);
12869 int i;
12870 for (i = 0; i < n; i++)
12871 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12872 complain, in_decl);
12873 return result;
12874 }
12875
12876 case COND_EXPR:
12877 case MODOP_EXPR:
12878 case PSEUDO_DTOR_EXPR:
12879 case VEC_PERM_EXPR:
12880 {
12881 r = build_nt
12882 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12883 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12884 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12885 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12886 return r;
12887 }
12888
12889 case NEW_EXPR:
12890 {
12891 r = build_nt
12892 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12893 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12894 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12895 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12896 return r;
12897 }
12898
12899 case DELETE_EXPR:
12900 {
12901 r = build_nt
12902 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12903 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12904 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12905 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12906 return r;
12907 }
12908
12909 case TEMPLATE_ID_EXPR:
12910 {
12911 /* Substituted template arguments */
12912 tree fn = TREE_OPERAND (t, 0);
12913 tree targs = TREE_OPERAND (t, 1);
12914
12915 fn = tsubst_copy (fn, args, complain, in_decl);
12916 if (targs)
12917 targs = tsubst_template_args (targs, args, complain, in_decl);
12918
12919 return lookup_template_function (fn, targs);
12920 }
12921
12922 case TREE_LIST:
12923 {
12924 tree purpose, value, chain;
12925
12926 if (t == void_list_node)
12927 return t;
12928
12929 purpose = TREE_PURPOSE (t);
12930 if (purpose)
12931 purpose = tsubst_copy (purpose, args, complain, in_decl);
12932 value = TREE_VALUE (t);
12933 if (value)
12934 value = tsubst_copy (value, args, complain, in_decl);
12935 chain = TREE_CHAIN (t);
12936 if (chain && chain != void_type_node)
12937 chain = tsubst_copy (chain, args, complain, in_decl);
12938 if (purpose == TREE_PURPOSE (t)
12939 && value == TREE_VALUE (t)
12940 && chain == TREE_CHAIN (t))
12941 return t;
12942 return tree_cons (purpose, value, chain);
12943 }
12944
12945 case RECORD_TYPE:
12946 case UNION_TYPE:
12947 case ENUMERAL_TYPE:
12948 case INTEGER_TYPE:
12949 case TEMPLATE_TYPE_PARM:
12950 case TEMPLATE_TEMPLATE_PARM:
12951 case BOUND_TEMPLATE_TEMPLATE_PARM:
12952 case TEMPLATE_PARM_INDEX:
12953 case POINTER_TYPE:
12954 case REFERENCE_TYPE:
12955 case OFFSET_TYPE:
12956 case FUNCTION_TYPE:
12957 case METHOD_TYPE:
12958 case ARRAY_TYPE:
12959 case TYPENAME_TYPE:
12960 case UNBOUND_CLASS_TEMPLATE:
12961 case TYPEOF_TYPE:
12962 case DECLTYPE_TYPE:
12963 case TYPE_DECL:
12964 return tsubst (t, args, complain, in_decl);
12965
12966 case USING_DECL:
12967 t = DECL_NAME (t);
12968 /* Fall through. */
12969 case IDENTIFIER_NODE:
12970 if (IDENTIFIER_TYPENAME_P (t))
12971 {
12972 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12973 return mangle_conv_op_name_for_type (new_type);
12974 }
12975 else
12976 return t;
12977
12978 case CONSTRUCTOR:
12979 /* This is handled by tsubst_copy_and_build. */
12980 gcc_unreachable ();
12981
12982 case VA_ARG_EXPR:
12983 return build_x_va_arg (EXPR_LOCATION (t),
12984 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12985 in_decl),
12986 tsubst (TREE_TYPE (t), args, complain, in_decl));
12987
12988 case CLEANUP_POINT_EXPR:
12989 /* We shouldn't have built any of these during initial template
12990 generation. Instead, they should be built during instantiation
12991 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12992 gcc_unreachable ();
12993
12994 case OFFSET_REF:
12995 r = build2
12996 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12997 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12998 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12999 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13000 mark_used (TREE_OPERAND (r, 1));
13001 return r;
13002
13003 case EXPR_PACK_EXPANSION:
13004 error ("invalid use of pack expansion expression");
13005 return error_mark_node;
13006
13007 case NONTYPE_ARGUMENT_PACK:
13008 error ("use %<...%> to expand argument pack");
13009 return error_mark_node;
13010
13011 case INTEGER_CST:
13012 case REAL_CST:
13013 case STRING_CST:
13014 case COMPLEX_CST:
13015 {
13016 /* Instantiate any typedefs in the type. */
13017 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13018 r = fold_convert (type, t);
13019 gcc_assert (TREE_CODE (r) == code);
13020 return r;
13021 }
13022
13023 case PTRMEM_CST:
13024 /* These can sometimes show up in a partial instantiation, but never
13025 involve template parms. */
13026 gcc_assert (!uses_template_parms (t));
13027 return t;
13028
13029 default:
13030 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13031 gcc_checking_assert (false);
13032 return t;
13033 }
13034 }
13035
13036 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13037
13038 static tree
13039 tsubst_omp_clauses (tree clauses, bool declare_simd,
13040 tree args, tsubst_flags_t complain, tree in_decl)
13041 {
13042 tree new_clauses = NULL, nc, oc;
13043
13044 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13045 {
13046 nc = copy_node (oc);
13047 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13048 new_clauses = nc;
13049
13050 switch (OMP_CLAUSE_CODE (nc))
13051 {
13052 case OMP_CLAUSE_LASTPRIVATE:
13053 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13054 {
13055 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13056 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13057 in_decl, /*integral_constant_expression_p=*/false);
13058 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13059 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13060 }
13061 /* FALLTHRU */
13062 case OMP_CLAUSE_PRIVATE:
13063 case OMP_CLAUSE_SHARED:
13064 case OMP_CLAUSE_FIRSTPRIVATE:
13065 case OMP_CLAUSE_COPYIN:
13066 case OMP_CLAUSE_COPYPRIVATE:
13067 case OMP_CLAUSE_IF:
13068 case OMP_CLAUSE_NUM_THREADS:
13069 case OMP_CLAUSE_SCHEDULE:
13070 case OMP_CLAUSE_COLLAPSE:
13071 case OMP_CLAUSE_FINAL:
13072 case OMP_CLAUSE_DEPEND:
13073 case OMP_CLAUSE_FROM:
13074 case OMP_CLAUSE_TO:
13075 case OMP_CLAUSE_UNIFORM:
13076 case OMP_CLAUSE_MAP:
13077 case OMP_CLAUSE_DEVICE:
13078 case OMP_CLAUSE_DIST_SCHEDULE:
13079 case OMP_CLAUSE_NUM_TEAMS:
13080 case OMP_CLAUSE_THREAD_LIMIT:
13081 case OMP_CLAUSE_SAFELEN:
13082 case OMP_CLAUSE_SIMDLEN:
13083 OMP_CLAUSE_OPERAND (nc, 0)
13084 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13085 in_decl, /*integral_constant_expression_p=*/false);
13086 break;
13087 case OMP_CLAUSE_REDUCTION:
13088 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13089 {
13090 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13091 if (TREE_CODE (placeholder) == SCOPE_REF)
13092 {
13093 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13094 complain, in_decl);
13095 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13096 = build_qualified_name (NULL_TREE, scope,
13097 TREE_OPERAND (placeholder, 1),
13098 false);
13099 }
13100 else
13101 gcc_assert (identifier_p (placeholder));
13102 }
13103 OMP_CLAUSE_OPERAND (nc, 0)
13104 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13105 in_decl, /*integral_constant_expression_p=*/false);
13106 break;
13107 case OMP_CLAUSE_LINEAR:
13108 case OMP_CLAUSE_ALIGNED:
13109 OMP_CLAUSE_OPERAND (nc, 0)
13110 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13111 in_decl, /*integral_constant_expression_p=*/false);
13112 OMP_CLAUSE_OPERAND (nc, 1)
13113 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13114 in_decl, /*integral_constant_expression_p=*/false);
13115 break;
13116
13117 case OMP_CLAUSE_NOWAIT:
13118 case OMP_CLAUSE_ORDERED:
13119 case OMP_CLAUSE_DEFAULT:
13120 case OMP_CLAUSE_UNTIED:
13121 case OMP_CLAUSE_MERGEABLE:
13122 case OMP_CLAUSE_INBRANCH:
13123 case OMP_CLAUSE_NOTINBRANCH:
13124 case OMP_CLAUSE_PROC_BIND:
13125 case OMP_CLAUSE_FOR:
13126 case OMP_CLAUSE_PARALLEL:
13127 case OMP_CLAUSE_SECTIONS:
13128 case OMP_CLAUSE_TASKGROUP:
13129 break;
13130 default:
13131 gcc_unreachable ();
13132 }
13133 }
13134
13135 new_clauses = nreverse (new_clauses);
13136 if (!declare_simd)
13137 new_clauses = finish_omp_clauses (new_clauses);
13138 return new_clauses;
13139 }
13140
13141 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13142
13143 static tree
13144 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13145 tree in_decl)
13146 {
13147 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13148
13149 tree purpose, value, chain;
13150
13151 if (t == NULL)
13152 return t;
13153
13154 if (TREE_CODE (t) != TREE_LIST)
13155 return tsubst_copy_and_build (t, args, complain, in_decl,
13156 /*function_p=*/false,
13157 /*integral_constant_expression_p=*/false);
13158
13159 if (t == void_list_node)
13160 return t;
13161
13162 purpose = TREE_PURPOSE (t);
13163 if (purpose)
13164 purpose = RECUR (purpose);
13165 value = TREE_VALUE (t);
13166 if (value)
13167 {
13168 if (TREE_CODE (value) != LABEL_DECL)
13169 value = RECUR (value);
13170 else
13171 {
13172 value = lookup_label (DECL_NAME (value));
13173 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13174 TREE_USED (value) = 1;
13175 }
13176 }
13177 chain = TREE_CHAIN (t);
13178 if (chain && chain != void_type_node)
13179 chain = RECUR (chain);
13180 return tree_cons (purpose, value, chain);
13181 #undef RECUR
13182 }
13183
13184 /* Substitute one OMP_FOR iterator. */
13185
13186 static void
13187 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13188 tree condv, tree incrv, tree *clauses,
13189 tree args, tsubst_flags_t complain, tree in_decl,
13190 bool integral_constant_expression_p)
13191 {
13192 #define RECUR(NODE) \
13193 tsubst_expr ((NODE), args, complain, in_decl, \
13194 integral_constant_expression_p)
13195 tree decl, init, cond, incr;
13196
13197 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13198 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13199 decl = TREE_OPERAND (init, 0);
13200 init = TREE_OPERAND (init, 1);
13201 tree decl_expr = NULL_TREE;
13202 if (init && TREE_CODE (init) == DECL_EXPR)
13203 {
13204 /* We need to jump through some hoops to handle declarations in the
13205 for-init-statement, since we might need to handle auto deduction,
13206 but we need to keep control of initialization. */
13207 decl_expr = init;
13208 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13209 decl = tsubst_decl (decl, args, complain);
13210 }
13211 else
13212 decl = RECUR (decl);
13213 init = RECUR (init);
13214
13215 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13216 if (auto_node && init)
13217 TREE_TYPE (decl)
13218 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13219
13220 gcc_assert (!type_dependent_expression_p (decl));
13221
13222 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13223 {
13224 if (decl_expr)
13225 {
13226 /* Declare the variable, but don't let that initialize it. */
13227 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13228 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13229 RECUR (decl_expr);
13230 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13231 }
13232
13233 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13234 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13235 if (TREE_CODE (incr) == MODIFY_EXPR)
13236 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13237 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13238 RECUR (TREE_OPERAND (incr, 1)),
13239 complain);
13240 else
13241 incr = RECUR (incr);
13242 TREE_VEC_ELT (declv, i) = decl;
13243 TREE_VEC_ELT (initv, i) = init;
13244 TREE_VEC_ELT (condv, i) = cond;
13245 TREE_VEC_ELT (incrv, i) = incr;
13246 return;
13247 }
13248
13249 if (decl_expr)
13250 {
13251 /* Declare and initialize the variable. */
13252 RECUR (decl_expr);
13253 init = NULL_TREE;
13254 }
13255 else if (init)
13256 {
13257 tree c;
13258 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13259 {
13260 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13261 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13262 && OMP_CLAUSE_DECL (c) == decl)
13263 break;
13264 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13265 && OMP_CLAUSE_DECL (c) == decl)
13266 error ("iteration variable %qD should not be firstprivate", decl);
13267 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13268 && OMP_CLAUSE_DECL (c) == decl)
13269 error ("iteration variable %qD should not be reduction", decl);
13270 }
13271 if (c == NULL)
13272 {
13273 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13274 OMP_CLAUSE_DECL (c) = decl;
13275 c = finish_omp_clauses (c);
13276 if (c)
13277 {
13278 OMP_CLAUSE_CHAIN (c) = *clauses;
13279 *clauses = c;
13280 }
13281 }
13282 }
13283 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13284 if (COMPARISON_CLASS_P (cond))
13285 cond = build2 (TREE_CODE (cond), boolean_type_node,
13286 RECUR (TREE_OPERAND (cond, 0)),
13287 RECUR (TREE_OPERAND (cond, 1)));
13288 else
13289 cond = RECUR (cond);
13290 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13291 switch (TREE_CODE (incr))
13292 {
13293 case PREINCREMENT_EXPR:
13294 case PREDECREMENT_EXPR:
13295 case POSTINCREMENT_EXPR:
13296 case POSTDECREMENT_EXPR:
13297 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13298 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13299 break;
13300 case MODIFY_EXPR:
13301 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13302 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13303 {
13304 tree rhs = TREE_OPERAND (incr, 1);
13305 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13306 RECUR (TREE_OPERAND (incr, 0)),
13307 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13308 RECUR (TREE_OPERAND (rhs, 0)),
13309 RECUR (TREE_OPERAND (rhs, 1))));
13310 }
13311 else
13312 incr = RECUR (incr);
13313 break;
13314 case MODOP_EXPR:
13315 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13316 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13317 {
13318 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13319 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13320 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13321 TREE_TYPE (decl), lhs,
13322 RECUR (TREE_OPERAND (incr, 2))));
13323 }
13324 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13325 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13326 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13327 {
13328 tree rhs = TREE_OPERAND (incr, 2);
13329 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13330 RECUR (TREE_OPERAND (incr, 0)),
13331 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13332 RECUR (TREE_OPERAND (rhs, 0)),
13333 RECUR (TREE_OPERAND (rhs, 1))));
13334 }
13335 else
13336 incr = RECUR (incr);
13337 break;
13338 default:
13339 incr = RECUR (incr);
13340 break;
13341 }
13342
13343 TREE_VEC_ELT (declv, i) = decl;
13344 TREE_VEC_ELT (initv, i) = init;
13345 TREE_VEC_ELT (condv, i) = cond;
13346 TREE_VEC_ELT (incrv, i) = incr;
13347 #undef RECUR
13348 }
13349
13350 /* Like tsubst_copy for expressions, etc. but also does semantic
13351 processing. */
13352
13353 static tree
13354 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13355 bool integral_constant_expression_p)
13356 {
13357 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13358 #define RECUR(NODE) \
13359 tsubst_expr ((NODE), args, complain, in_decl, \
13360 integral_constant_expression_p)
13361
13362 tree stmt, tmp;
13363 tree r;
13364 location_t loc;
13365
13366 if (t == NULL_TREE || t == error_mark_node)
13367 return t;
13368
13369 loc = input_location;
13370 if (EXPR_HAS_LOCATION (t))
13371 input_location = EXPR_LOCATION (t);
13372 if (STATEMENT_CODE_P (TREE_CODE (t)))
13373 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13374
13375 switch (TREE_CODE (t))
13376 {
13377 case STATEMENT_LIST:
13378 {
13379 tree_stmt_iterator i;
13380 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13381 RECUR (tsi_stmt (i));
13382 break;
13383 }
13384
13385 case CTOR_INITIALIZER:
13386 finish_mem_initializers (tsubst_initializer_list
13387 (TREE_OPERAND (t, 0), args));
13388 break;
13389
13390 case RETURN_EXPR:
13391 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13392 break;
13393
13394 case EXPR_STMT:
13395 tmp = RECUR (EXPR_STMT_EXPR (t));
13396 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13397 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13398 else
13399 finish_expr_stmt (tmp);
13400 break;
13401
13402 case USING_STMT:
13403 do_using_directive (USING_STMT_NAMESPACE (t));
13404 break;
13405
13406 case DECL_EXPR:
13407 {
13408 tree decl, pattern_decl;
13409 tree init;
13410
13411 pattern_decl = decl = DECL_EXPR_DECL (t);
13412 if (TREE_CODE (decl) == LABEL_DECL)
13413 finish_label_decl (DECL_NAME (decl));
13414 else if (TREE_CODE (decl) == USING_DECL)
13415 {
13416 tree scope = USING_DECL_SCOPE (decl);
13417 tree name = DECL_NAME (decl);
13418 tree decl;
13419
13420 scope = tsubst (scope, args, complain, in_decl);
13421 decl = lookup_qualified_name (scope, name,
13422 /*is_type_p=*/false,
13423 /*complain=*/false);
13424 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13425 qualified_name_lookup_error (scope, name, decl, input_location);
13426 else
13427 do_local_using_decl (decl, scope, name);
13428 }
13429 else if (DECL_PACK_P (decl))
13430 {
13431 /* Don't build up decls for a variadic capture proxy, we'll
13432 instantiate the elements directly as needed. */
13433 break;
13434 }
13435 else
13436 {
13437 init = DECL_INITIAL (decl);
13438 decl = tsubst (decl, args, complain, in_decl);
13439 if (decl != error_mark_node)
13440 {
13441 /* By marking the declaration as instantiated, we avoid
13442 trying to instantiate it. Since instantiate_decl can't
13443 handle local variables, and since we've already done
13444 all that needs to be done, that's the right thing to
13445 do. */
13446 if (VAR_P (decl))
13447 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13448 if (VAR_P (decl)
13449 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13450 /* Anonymous aggregates are a special case. */
13451 finish_anon_union (decl);
13452 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13453 {
13454 DECL_CONTEXT (decl) = current_function_decl;
13455 if (DECL_NAME (decl) == this_identifier)
13456 {
13457 tree lam = DECL_CONTEXT (current_function_decl);
13458 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13459 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13460 }
13461 insert_capture_proxy (decl);
13462 }
13463 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13464 /* We already did a pushtag. */;
13465 else if (TREE_CODE (decl) == FUNCTION_DECL
13466 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13467 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13468 {
13469 DECL_CONTEXT (decl) = NULL_TREE;
13470 pushdecl (decl);
13471 DECL_CONTEXT (decl) = current_function_decl;
13472 cp_check_omp_declare_reduction (decl);
13473 }
13474 else
13475 {
13476 int const_init = false;
13477 maybe_push_decl (decl);
13478 if (VAR_P (decl)
13479 && DECL_PRETTY_FUNCTION_P (decl))
13480 {
13481 /* For __PRETTY_FUNCTION__ we have to adjust the
13482 initializer. */
13483 const char *const name
13484 = cxx_printable_name (current_function_decl, 2);
13485 init = cp_fname_init (name, &TREE_TYPE (decl));
13486 }
13487 else
13488 {
13489 tree t = RECUR (init);
13490
13491 if (init && !t)
13492 {
13493 /* If we had an initializer but it
13494 instantiated to nothing,
13495 value-initialize the object. This will
13496 only occur when the initializer was a
13497 pack expansion where the parameter packs
13498 used in that expansion were of length
13499 zero. */
13500 init = build_value_init (TREE_TYPE (decl),
13501 complain);
13502 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13503 init = get_target_expr_sfinae (init, complain);
13504 }
13505 else
13506 init = t;
13507 }
13508
13509 if (VAR_P (decl))
13510 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13511 (pattern_decl));
13512 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13513 }
13514 }
13515 }
13516
13517 break;
13518 }
13519
13520 case FOR_STMT:
13521 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13522 RECUR (FOR_INIT_STMT (t));
13523 finish_for_init_stmt (stmt);
13524 tmp = RECUR (FOR_COND (t));
13525 finish_for_cond (tmp, stmt, false);
13526 tmp = RECUR (FOR_EXPR (t));
13527 finish_for_expr (tmp, stmt);
13528 RECUR (FOR_BODY (t));
13529 finish_for_stmt (stmt);
13530 break;
13531
13532 case RANGE_FOR_STMT:
13533 {
13534 tree decl, expr;
13535 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13536 decl = RANGE_FOR_DECL (t);
13537 decl = tsubst (decl, args, complain, in_decl);
13538 maybe_push_decl (decl);
13539 expr = RECUR (RANGE_FOR_EXPR (t));
13540 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13541 RECUR (RANGE_FOR_BODY (t));
13542 finish_for_stmt (stmt);
13543 }
13544 break;
13545
13546 case WHILE_STMT:
13547 stmt = begin_while_stmt ();
13548 tmp = RECUR (WHILE_COND (t));
13549 finish_while_stmt_cond (tmp, stmt, false);
13550 RECUR (WHILE_BODY (t));
13551 finish_while_stmt (stmt);
13552 break;
13553
13554 case DO_STMT:
13555 stmt = begin_do_stmt ();
13556 RECUR (DO_BODY (t));
13557 finish_do_body (stmt);
13558 tmp = RECUR (DO_COND (t));
13559 finish_do_stmt (tmp, stmt, false);
13560 break;
13561
13562 case IF_STMT:
13563 stmt = begin_if_stmt ();
13564 tmp = RECUR (IF_COND (t));
13565 finish_if_stmt_cond (tmp, stmt);
13566 RECUR (THEN_CLAUSE (t));
13567 finish_then_clause (stmt);
13568
13569 if (ELSE_CLAUSE (t))
13570 {
13571 begin_else_clause (stmt);
13572 RECUR (ELSE_CLAUSE (t));
13573 finish_else_clause (stmt);
13574 }
13575
13576 finish_if_stmt (stmt);
13577 break;
13578
13579 case BIND_EXPR:
13580 if (BIND_EXPR_BODY_BLOCK (t))
13581 stmt = begin_function_body ();
13582 else
13583 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13584 ? BCS_TRY_BLOCK : 0);
13585
13586 RECUR (BIND_EXPR_BODY (t));
13587
13588 if (BIND_EXPR_BODY_BLOCK (t))
13589 finish_function_body (stmt);
13590 else
13591 finish_compound_stmt (stmt);
13592 break;
13593
13594 case BREAK_STMT:
13595 finish_break_stmt ();
13596 break;
13597
13598 case CONTINUE_STMT:
13599 finish_continue_stmt ();
13600 break;
13601
13602 case SWITCH_STMT:
13603 stmt = begin_switch_stmt ();
13604 tmp = RECUR (SWITCH_STMT_COND (t));
13605 finish_switch_cond (tmp, stmt);
13606 RECUR (SWITCH_STMT_BODY (t));
13607 finish_switch_stmt (stmt);
13608 break;
13609
13610 case CASE_LABEL_EXPR:
13611 finish_case_label (EXPR_LOCATION (t),
13612 RECUR (CASE_LOW (t)),
13613 RECUR (CASE_HIGH (t)));
13614 break;
13615
13616 case LABEL_EXPR:
13617 {
13618 tree decl = LABEL_EXPR_LABEL (t);
13619 tree label;
13620
13621 label = finish_label_stmt (DECL_NAME (decl));
13622 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13623 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13624 }
13625 break;
13626
13627 case GOTO_EXPR:
13628 tmp = GOTO_DESTINATION (t);
13629 if (TREE_CODE (tmp) != LABEL_DECL)
13630 /* Computed goto's must be tsubst'd into. On the other hand,
13631 non-computed gotos must not be; the identifier in question
13632 will have no binding. */
13633 tmp = RECUR (tmp);
13634 else
13635 tmp = DECL_NAME (tmp);
13636 finish_goto_stmt (tmp);
13637 break;
13638
13639 case ASM_EXPR:
13640 tmp = finish_asm_stmt
13641 (ASM_VOLATILE_P (t),
13642 RECUR (ASM_STRING (t)),
13643 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13644 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13645 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13646 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13647 {
13648 tree asm_expr = tmp;
13649 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13650 asm_expr = TREE_OPERAND (asm_expr, 0);
13651 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13652 }
13653 break;
13654
13655 case TRY_BLOCK:
13656 if (CLEANUP_P (t))
13657 {
13658 stmt = begin_try_block ();
13659 RECUR (TRY_STMTS (t));
13660 finish_cleanup_try_block (stmt);
13661 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13662 }
13663 else
13664 {
13665 tree compound_stmt = NULL_TREE;
13666
13667 if (FN_TRY_BLOCK_P (t))
13668 stmt = begin_function_try_block (&compound_stmt);
13669 else
13670 stmt = begin_try_block ();
13671
13672 RECUR (TRY_STMTS (t));
13673
13674 if (FN_TRY_BLOCK_P (t))
13675 finish_function_try_block (stmt);
13676 else
13677 finish_try_block (stmt);
13678
13679 RECUR (TRY_HANDLERS (t));
13680 if (FN_TRY_BLOCK_P (t))
13681 finish_function_handler_sequence (stmt, compound_stmt);
13682 else
13683 finish_handler_sequence (stmt);
13684 }
13685 break;
13686
13687 case HANDLER:
13688 {
13689 tree decl = HANDLER_PARMS (t);
13690
13691 if (decl)
13692 {
13693 decl = tsubst (decl, args, complain, in_decl);
13694 /* Prevent instantiate_decl from trying to instantiate
13695 this variable. We've already done all that needs to be
13696 done. */
13697 if (decl != error_mark_node)
13698 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13699 }
13700 stmt = begin_handler ();
13701 finish_handler_parms (decl, stmt);
13702 RECUR (HANDLER_BODY (t));
13703 finish_handler (stmt);
13704 }
13705 break;
13706
13707 case TAG_DEFN:
13708 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13709 if (CLASS_TYPE_P (tmp))
13710 {
13711 /* Local classes are not independent templates; they are
13712 instantiated along with their containing function. And this
13713 way we don't have to deal with pushing out of one local class
13714 to instantiate a member of another local class. */
13715 tree fn;
13716 /* Closures are handled by the LAMBDA_EXPR. */
13717 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13718 complete_type (tmp);
13719 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13720 if (!DECL_ARTIFICIAL (fn))
13721 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13722 }
13723 break;
13724
13725 case STATIC_ASSERT:
13726 {
13727 tree condition;
13728
13729 ++c_inhibit_evaluation_warnings;
13730 condition =
13731 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13732 args,
13733 complain, in_decl,
13734 /*integral_constant_expression_p=*/true);
13735 --c_inhibit_evaluation_warnings;
13736
13737 finish_static_assert (condition,
13738 STATIC_ASSERT_MESSAGE (t),
13739 STATIC_ASSERT_SOURCE_LOCATION (t),
13740 /*member_p=*/false);
13741 }
13742 break;
13743
13744 case OMP_PARALLEL:
13745 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13746 args, complain, in_decl);
13747 stmt = begin_omp_parallel ();
13748 RECUR (OMP_PARALLEL_BODY (t));
13749 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13750 = OMP_PARALLEL_COMBINED (t);
13751 break;
13752
13753 case OMP_TASK:
13754 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13755 args, complain, in_decl);
13756 stmt = begin_omp_task ();
13757 RECUR (OMP_TASK_BODY (t));
13758 finish_omp_task (tmp, stmt);
13759 break;
13760
13761 case OMP_FOR:
13762 case OMP_SIMD:
13763 case CILK_SIMD:
13764 case OMP_DISTRIBUTE:
13765 {
13766 tree clauses, body, pre_body;
13767 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13768 tree incrv = NULL_TREE;
13769 int i;
13770
13771 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13772 args, complain, in_decl);
13773 if (OMP_FOR_INIT (t) != NULL_TREE)
13774 {
13775 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13776 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13777 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13778 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13779 }
13780
13781 stmt = begin_omp_structured_block ();
13782
13783 pre_body = push_stmt_list ();
13784 RECUR (OMP_FOR_PRE_BODY (t));
13785 pre_body = pop_stmt_list (pre_body);
13786
13787 if (OMP_FOR_INIT (t) != NULL_TREE)
13788 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13789 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13790 &clauses, args, complain, in_decl,
13791 integral_constant_expression_p);
13792
13793 body = push_stmt_list ();
13794 RECUR (OMP_FOR_BODY (t));
13795 body = pop_stmt_list (body);
13796
13797 if (OMP_FOR_INIT (t) != NULL_TREE)
13798 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13799 condv, incrv, body, pre_body, clauses);
13800 else
13801 {
13802 t = make_node (TREE_CODE (t));
13803 TREE_TYPE (t) = void_type_node;
13804 OMP_FOR_BODY (t) = body;
13805 OMP_FOR_PRE_BODY (t) = pre_body;
13806 OMP_FOR_CLAUSES (t) = clauses;
13807 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13808 add_stmt (t);
13809 }
13810
13811 add_stmt (finish_omp_structured_block (stmt));
13812 }
13813 break;
13814
13815 case OMP_SECTIONS:
13816 case OMP_SINGLE:
13817 case OMP_TEAMS:
13818 case OMP_TARGET_DATA:
13819 case OMP_TARGET:
13820 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13821 args, complain, in_decl);
13822 stmt = push_stmt_list ();
13823 RECUR (OMP_BODY (t));
13824 stmt = pop_stmt_list (stmt);
13825
13826 t = copy_node (t);
13827 OMP_BODY (t) = stmt;
13828 OMP_CLAUSES (t) = tmp;
13829 add_stmt (t);
13830 break;
13831
13832 case OMP_TARGET_UPDATE:
13833 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13834 args, complain, in_decl);
13835 t = copy_node (t);
13836 OMP_CLAUSES (t) = tmp;
13837 add_stmt (t);
13838 break;
13839
13840 case OMP_SECTION:
13841 case OMP_CRITICAL:
13842 case OMP_MASTER:
13843 case OMP_TASKGROUP:
13844 case OMP_ORDERED:
13845 stmt = push_stmt_list ();
13846 RECUR (OMP_BODY (t));
13847 stmt = pop_stmt_list (stmt);
13848
13849 t = copy_node (t);
13850 OMP_BODY (t) = stmt;
13851 add_stmt (t);
13852 break;
13853
13854 case OMP_ATOMIC:
13855 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13856 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13857 {
13858 tree op1 = TREE_OPERAND (t, 1);
13859 tree rhs1 = NULL_TREE;
13860 tree lhs, rhs;
13861 if (TREE_CODE (op1) == COMPOUND_EXPR)
13862 {
13863 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13864 op1 = TREE_OPERAND (op1, 1);
13865 }
13866 lhs = RECUR (TREE_OPERAND (op1, 0));
13867 rhs = RECUR (TREE_OPERAND (op1, 1));
13868 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13869 NULL_TREE, NULL_TREE, rhs1,
13870 OMP_ATOMIC_SEQ_CST (t));
13871 }
13872 else
13873 {
13874 tree op1 = TREE_OPERAND (t, 1);
13875 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13876 tree rhs1 = NULL_TREE;
13877 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13878 enum tree_code opcode = NOP_EXPR;
13879 if (code == OMP_ATOMIC_READ)
13880 {
13881 v = RECUR (TREE_OPERAND (op1, 0));
13882 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13883 }
13884 else if (code == OMP_ATOMIC_CAPTURE_OLD
13885 || code == OMP_ATOMIC_CAPTURE_NEW)
13886 {
13887 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13888 v = RECUR (TREE_OPERAND (op1, 0));
13889 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13890 if (TREE_CODE (op11) == COMPOUND_EXPR)
13891 {
13892 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13893 op11 = TREE_OPERAND (op11, 1);
13894 }
13895 lhs = RECUR (TREE_OPERAND (op11, 0));
13896 rhs = RECUR (TREE_OPERAND (op11, 1));
13897 opcode = TREE_CODE (op11);
13898 if (opcode == MODIFY_EXPR)
13899 opcode = NOP_EXPR;
13900 }
13901 else
13902 {
13903 code = OMP_ATOMIC;
13904 lhs = RECUR (TREE_OPERAND (op1, 0));
13905 rhs = RECUR (TREE_OPERAND (op1, 1));
13906 }
13907 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13908 OMP_ATOMIC_SEQ_CST (t));
13909 }
13910 break;
13911
13912 case TRANSACTION_EXPR:
13913 {
13914 int flags = 0;
13915 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13916 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13917
13918 if (TRANSACTION_EXPR_IS_STMT (t))
13919 {
13920 tree body = TRANSACTION_EXPR_BODY (t);
13921 tree noex = NULL_TREE;
13922 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13923 {
13924 noex = MUST_NOT_THROW_COND (body);
13925 if (noex == NULL_TREE)
13926 noex = boolean_true_node;
13927 body = TREE_OPERAND (body, 0);
13928 }
13929 stmt = begin_transaction_stmt (input_location, NULL, flags);
13930 RECUR (body);
13931 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13932 }
13933 else
13934 {
13935 stmt = build_transaction_expr (EXPR_LOCATION (t),
13936 RECUR (TRANSACTION_EXPR_BODY (t)),
13937 flags, NULL_TREE);
13938 RETURN (stmt);
13939 }
13940 }
13941 break;
13942
13943 case MUST_NOT_THROW_EXPR:
13944 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13945 RECUR (MUST_NOT_THROW_COND (t))));
13946
13947 case EXPR_PACK_EXPANSION:
13948 error ("invalid use of pack expansion expression");
13949 RETURN (error_mark_node);
13950
13951 case NONTYPE_ARGUMENT_PACK:
13952 error ("use %<...%> to expand argument pack");
13953 RETURN (error_mark_node);
13954
13955 case CILK_SPAWN_STMT:
13956 cfun->calls_cilk_spawn = 1;
13957 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13958
13959 case CILK_SYNC_STMT:
13960 RETURN (build_cilk_sync ());
13961
13962 case COMPOUND_EXPR:
13963 tmp = RECUR (TREE_OPERAND (t, 0));
13964 if (tmp == NULL_TREE)
13965 /* If the first operand was a statement, we're done with it. */
13966 RETURN (RECUR (TREE_OPERAND (t, 1)));
13967 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13968 RECUR (TREE_OPERAND (t, 1)),
13969 complain));
13970
13971 case ANNOTATE_EXPR:
13972 tmp = RECUR (TREE_OPERAND (t, 0));
13973 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
13974 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
13975
13976 default:
13977 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13978
13979 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13980 /*function_p=*/false,
13981 integral_constant_expression_p));
13982 }
13983
13984 RETURN (NULL_TREE);
13985 out:
13986 input_location = loc;
13987 return r;
13988 #undef RECUR
13989 #undef RETURN
13990 }
13991
13992 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13993 function. For description of the body see comment above
13994 cp_parser_omp_declare_reduction_exprs. */
13995
13996 static void
13997 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13998 {
13999 if (t == NULL_TREE || t == error_mark_node)
14000 return;
14001
14002 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14003
14004 tree_stmt_iterator tsi;
14005 int i;
14006 tree stmts[7];
14007 memset (stmts, 0, sizeof stmts);
14008 for (i = 0, tsi = tsi_start (t);
14009 i < 7 && !tsi_end_p (tsi);
14010 i++, tsi_next (&tsi))
14011 stmts[i] = tsi_stmt (tsi);
14012 gcc_assert (tsi_end_p (tsi));
14013
14014 if (i >= 3)
14015 {
14016 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14017 && TREE_CODE (stmts[1]) == DECL_EXPR);
14018 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14019 args, complain, in_decl);
14020 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14021 args, complain, in_decl);
14022 DECL_CONTEXT (omp_out) = current_function_decl;
14023 DECL_CONTEXT (omp_in) = current_function_decl;
14024 keep_next_level (true);
14025 tree block = begin_omp_structured_block ();
14026 tsubst_expr (stmts[2], args, complain, in_decl, false);
14027 block = finish_omp_structured_block (block);
14028 block = maybe_cleanup_point_expr_void (block);
14029 add_decl_expr (omp_out);
14030 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14031 TREE_NO_WARNING (omp_out) = 1;
14032 add_decl_expr (omp_in);
14033 finish_expr_stmt (block);
14034 }
14035 if (i >= 6)
14036 {
14037 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14038 && TREE_CODE (stmts[4]) == DECL_EXPR);
14039 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14040 args, complain, in_decl);
14041 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14042 args, complain, in_decl);
14043 DECL_CONTEXT (omp_priv) = current_function_decl;
14044 DECL_CONTEXT (omp_orig) = current_function_decl;
14045 keep_next_level (true);
14046 tree block = begin_omp_structured_block ();
14047 tsubst_expr (stmts[5], args, complain, in_decl, false);
14048 block = finish_omp_structured_block (block);
14049 block = maybe_cleanup_point_expr_void (block);
14050 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14051 add_decl_expr (omp_priv);
14052 add_decl_expr (omp_orig);
14053 finish_expr_stmt (block);
14054 if (i == 7)
14055 add_decl_expr (omp_orig);
14056 }
14057 }
14058
14059 /* T is a postfix-expression that is not being used in a function
14060 call. Return the substituted version of T. */
14061
14062 static tree
14063 tsubst_non_call_postfix_expression (tree t, tree args,
14064 tsubst_flags_t complain,
14065 tree in_decl)
14066 {
14067 if (TREE_CODE (t) == SCOPE_REF)
14068 t = tsubst_qualified_id (t, args, complain, in_decl,
14069 /*done=*/false, /*address_p=*/false);
14070 else
14071 t = tsubst_copy_and_build (t, args, complain, in_decl,
14072 /*function_p=*/false,
14073 /*integral_constant_expression_p=*/false);
14074
14075 return t;
14076 }
14077
14078 /* Sentinel to disable certain warnings during template substitution. */
14079
14080 struct warning_sentinel {
14081 int &flag;
14082 int val;
14083 warning_sentinel(int& flag, bool suppress=true)
14084 : flag(flag), val(flag) { if (suppress) flag = 0; }
14085 ~warning_sentinel() { flag = val; }
14086 };
14087
14088 /* Like tsubst but deals with expressions and performs semantic
14089 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14090
14091 tree
14092 tsubst_copy_and_build (tree t,
14093 tree args,
14094 tsubst_flags_t complain,
14095 tree in_decl,
14096 bool function_p,
14097 bool integral_constant_expression_p)
14098 {
14099 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14100 #define RECUR(NODE) \
14101 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14102 /*function_p=*/false, \
14103 integral_constant_expression_p)
14104
14105 tree retval, op1;
14106 location_t loc;
14107
14108 if (t == NULL_TREE || t == error_mark_node)
14109 return t;
14110
14111 loc = input_location;
14112 if (EXPR_HAS_LOCATION (t))
14113 input_location = EXPR_LOCATION (t);
14114
14115 /* N3276 decltype magic only applies to calls at the top level or on the
14116 right side of a comma. */
14117 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14118 complain &= ~tf_decltype;
14119
14120 switch (TREE_CODE (t))
14121 {
14122 case USING_DECL:
14123 t = DECL_NAME (t);
14124 /* Fall through. */
14125 case IDENTIFIER_NODE:
14126 {
14127 tree decl;
14128 cp_id_kind idk;
14129 bool non_integral_constant_expression_p;
14130 const char *error_msg;
14131
14132 if (IDENTIFIER_TYPENAME_P (t))
14133 {
14134 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14135 t = mangle_conv_op_name_for_type (new_type);
14136 }
14137
14138 /* Look up the name. */
14139 decl = lookup_name (t);
14140
14141 /* By convention, expressions use ERROR_MARK_NODE to indicate
14142 failure, not NULL_TREE. */
14143 if (decl == NULL_TREE)
14144 decl = error_mark_node;
14145
14146 decl = finish_id_expression (t, decl, NULL_TREE,
14147 &idk,
14148 integral_constant_expression_p,
14149 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14150 &non_integral_constant_expression_p,
14151 /*template_p=*/false,
14152 /*done=*/true,
14153 /*address_p=*/false,
14154 /*template_arg_p=*/false,
14155 &error_msg,
14156 input_location);
14157 if (error_msg)
14158 error (error_msg);
14159 if (!function_p && identifier_p (decl))
14160 {
14161 if (complain & tf_error)
14162 unqualified_name_lookup_error (decl);
14163 decl = error_mark_node;
14164 }
14165 RETURN (decl);
14166 }
14167
14168 case TEMPLATE_ID_EXPR:
14169 {
14170 tree object;
14171 tree templ = RECUR (TREE_OPERAND (t, 0));
14172 tree targs = TREE_OPERAND (t, 1);
14173
14174 if (targs)
14175 targs = tsubst_template_args (targs, args, complain, in_decl);
14176
14177 if (TREE_CODE (templ) == COMPONENT_REF)
14178 {
14179 object = TREE_OPERAND (templ, 0);
14180 templ = TREE_OPERAND (templ, 1);
14181 }
14182 else
14183 object = NULL_TREE;
14184 templ = lookup_template_function (templ, targs);
14185
14186 if (object)
14187 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14188 object, templ, NULL_TREE));
14189 else
14190 RETURN (baselink_for_fns (templ));
14191 }
14192
14193 case INDIRECT_REF:
14194 {
14195 tree r = RECUR (TREE_OPERAND (t, 0));
14196
14197 if (REFERENCE_REF_P (t))
14198 {
14199 /* A type conversion to reference type will be enclosed in
14200 such an indirect ref, but the substitution of the cast
14201 will have also added such an indirect ref. */
14202 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14203 r = convert_from_reference (r);
14204 }
14205 else
14206 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14207 complain|decltype_flag);
14208 RETURN (r);
14209 }
14210
14211 case NOP_EXPR:
14212 RETURN (build_nop
14213 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14214 RECUR (TREE_OPERAND (t, 0))));
14215
14216 case IMPLICIT_CONV_EXPR:
14217 {
14218 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14219 tree expr = RECUR (TREE_OPERAND (t, 0));
14220 int flags = LOOKUP_IMPLICIT;
14221 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14222 flags = LOOKUP_NORMAL;
14223 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14224 flags));
14225 }
14226
14227 case CONVERT_EXPR:
14228 RETURN (build1
14229 (CONVERT_EXPR,
14230 tsubst (TREE_TYPE (t), args, complain, in_decl),
14231 RECUR (TREE_OPERAND (t, 0))));
14232
14233 case CAST_EXPR:
14234 case REINTERPRET_CAST_EXPR:
14235 case CONST_CAST_EXPR:
14236 case DYNAMIC_CAST_EXPR:
14237 case STATIC_CAST_EXPR:
14238 {
14239 tree type;
14240 tree op, r = NULL_TREE;
14241
14242 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14243 if (integral_constant_expression_p
14244 && !cast_valid_in_integral_constant_expression_p (type))
14245 {
14246 if (complain & tf_error)
14247 error ("a cast to a type other than an integral or "
14248 "enumeration type cannot appear in a constant-expression");
14249 RETURN (error_mark_node);
14250 }
14251
14252 op = RECUR (TREE_OPERAND (t, 0));
14253
14254 warning_sentinel s(warn_useless_cast);
14255 switch (TREE_CODE (t))
14256 {
14257 case CAST_EXPR:
14258 r = build_functional_cast (type, op, complain);
14259 break;
14260 case REINTERPRET_CAST_EXPR:
14261 r = build_reinterpret_cast (type, op, complain);
14262 break;
14263 case CONST_CAST_EXPR:
14264 r = build_const_cast (type, op, complain);
14265 break;
14266 case DYNAMIC_CAST_EXPR:
14267 r = build_dynamic_cast (type, op, complain);
14268 break;
14269 case STATIC_CAST_EXPR:
14270 r = build_static_cast (type, op, complain);
14271 break;
14272 default:
14273 gcc_unreachable ();
14274 }
14275
14276 RETURN (r);
14277 }
14278
14279 case POSTDECREMENT_EXPR:
14280 case POSTINCREMENT_EXPR:
14281 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14282 args, complain, in_decl);
14283 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14284 complain|decltype_flag));
14285
14286 case PREDECREMENT_EXPR:
14287 case PREINCREMENT_EXPR:
14288 case NEGATE_EXPR:
14289 case BIT_NOT_EXPR:
14290 case ABS_EXPR:
14291 case TRUTH_NOT_EXPR:
14292 case UNARY_PLUS_EXPR: /* Unary + */
14293 case REALPART_EXPR:
14294 case IMAGPART_EXPR:
14295 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14296 RECUR (TREE_OPERAND (t, 0)),
14297 complain|decltype_flag));
14298
14299 case FIX_TRUNC_EXPR:
14300 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14301 0, complain));
14302
14303 case ADDR_EXPR:
14304 op1 = TREE_OPERAND (t, 0);
14305 if (TREE_CODE (op1) == LABEL_DECL)
14306 RETURN (finish_label_address_expr (DECL_NAME (op1),
14307 EXPR_LOCATION (op1)));
14308 if (TREE_CODE (op1) == SCOPE_REF)
14309 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14310 /*done=*/true, /*address_p=*/true);
14311 else
14312 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14313 in_decl);
14314 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14315 complain|decltype_flag));
14316
14317 case PLUS_EXPR:
14318 case MINUS_EXPR:
14319 case MULT_EXPR:
14320 case TRUNC_DIV_EXPR:
14321 case CEIL_DIV_EXPR:
14322 case FLOOR_DIV_EXPR:
14323 case ROUND_DIV_EXPR:
14324 case EXACT_DIV_EXPR:
14325 case BIT_AND_EXPR:
14326 case BIT_IOR_EXPR:
14327 case BIT_XOR_EXPR:
14328 case TRUNC_MOD_EXPR:
14329 case FLOOR_MOD_EXPR:
14330 case TRUTH_ANDIF_EXPR:
14331 case TRUTH_ORIF_EXPR:
14332 case TRUTH_AND_EXPR:
14333 case TRUTH_OR_EXPR:
14334 case RSHIFT_EXPR:
14335 case LSHIFT_EXPR:
14336 case RROTATE_EXPR:
14337 case LROTATE_EXPR:
14338 case EQ_EXPR:
14339 case NE_EXPR:
14340 case MAX_EXPR:
14341 case MIN_EXPR:
14342 case LE_EXPR:
14343 case GE_EXPR:
14344 case LT_EXPR:
14345 case GT_EXPR:
14346 case MEMBER_REF:
14347 case DOTSTAR_EXPR:
14348 {
14349 warning_sentinel s1(warn_type_limits);
14350 warning_sentinel s2(warn_div_by_zero);
14351 tree r = build_x_binary_op
14352 (input_location, TREE_CODE (t),
14353 RECUR (TREE_OPERAND (t, 0)),
14354 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14355 ? ERROR_MARK
14356 : TREE_CODE (TREE_OPERAND (t, 0))),
14357 RECUR (TREE_OPERAND (t, 1)),
14358 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14359 ? ERROR_MARK
14360 : TREE_CODE (TREE_OPERAND (t, 1))),
14361 /*overload=*/NULL,
14362 complain|decltype_flag);
14363 if (EXPR_P (r) && TREE_NO_WARNING (t))
14364 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14365
14366 RETURN (r);
14367 }
14368
14369 case POINTER_PLUS_EXPR:
14370 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14371 RECUR (TREE_OPERAND (t, 1)));
14372
14373 case SCOPE_REF:
14374 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14375 /*address_p=*/false));
14376 case ARRAY_REF:
14377 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14378 args, complain, in_decl);
14379 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14380 RECUR (TREE_OPERAND (t, 1)),
14381 complain|decltype_flag));
14382
14383 case ARRAY_NOTATION_REF:
14384 {
14385 tree start_index, length, stride;
14386 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14387 args, complain, in_decl);
14388 start_index = RECUR (ARRAY_NOTATION_START (t));
14389 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14390 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14391 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14392 length, stride, TREE_TYPE (op1)));
14393 }
14394 case SIZEOF_EXPR:
14395 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14396 RETURN (tsubst_copy (t, args, complain, in_decl));
14397 /* Fall through */
14398
14399 case ALIGNOF_EXPR:
14400 {
14401 tree r;
14402
14403 op1 = TREE_OPERAND (t, 0);
14404 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14405 op1 = TREE_TYPE (op1);
14406 if (!args)
14407 {
14408 /* When there are no ARGS, we are trying to evaluate a
14409 non-dependent expression from the parser. Trying to do
14410 the substitutions may not work. */
14411 if (!TYPE_P (op1))
14412 op1 = TREE_TYPE (op1);
14413 }
14414 else
14415 {
14416 ++cp_unevaluated_operand;
14417 ++c_inhibit_evaluation_warnings;
14418 if (TYPE_P (op1))
14419 op1 = tsubst (op1, args, complain, in_decl);
14420 else
14421 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14422 /*function_p=*/false,
14423 /*integral_constant_expression_p=*/
14424 false);
14425 --cp_unevaluated_operand;
14426 --c_inhibit_evaluation_warnings;
14427 }
14428 if (TYPE_P (op1))
14429 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14430 complain & tf_error);
14431 else
14432 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14433 complain & tf_error);
14434 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14435 {
14436 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14437 {
14438 if (!processing_template_decl && TYPE_P (op1))
14439 {
14440 r = build_min (SIZEOF_EXPR, size_type_node,
14441 build1 (NOP_EXPR, op1, error_mark_node));
14442 SIZEOF_EXPR_TYPE_P (r) = 1;
14443 }
14444 else
14445 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14446 TREE_SIDE_EFFECTS (r) = 0;
14447 TREE_READONLY (r) = 1;
14448 }
14449 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14450 }
14451 RETURN (r);
14452 }
14453
14454 case AT_ENCODE_EXPR:
14455 {
14456 op1 = TREE_OPERAND (t, 0);
14457 ++cp_unevaluated_operand;
14458 ++c_inhibit_evaluation_warnings;
14459 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14460 /*function_p=*/false,
14461 /*integral_constant_expression_p=*/false);
14462 --cp_unevaluated_operand;
14463 --c_inhibit_evaluation_warnings;
14464 RETURN (objc_build_encode_expr (op1));
14465 }
14466
14467 case NOEXCEPT_EXPR:
14468 op1 = TREE_OPERAND (t, 0);
14469 ++cp_unevaluated_operand;
14470 ++c_inhibit_evaluation_warnings;
14471 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14472 /*function_p=*/false,
14473 /*integral_constant_expression_p=*/false);
14474 --cp_unevaluated_operand;
14475 --c_inhibit_evaluation_warnings;
14476 RETURN (finish_noexcept_expr (op1, complain));
14477
14478 case MODOP_EXPR:
14479 {
14480 warning_sentinel s(warn_div_by_zero);
14481 tree r = build_x_modify_expr
14482 (EXPR_LOCATION (t),
14483 RECUR (TREE_OPERAND (t, 0)),
14484 TREE_CODE (TREE_OPERAND (t, 1)),
14485 RECUR (TREE_OPERAND (t, 2)),
14486 complain|decltype_flag);
14487 /* TREE_NO_WARNING must be set if either the expression was
14488 parenthesized or it uses an operator such as >>= rather
14489 than plain assignment. In the former case, it was already
14490 set and must be copied. In the latter case,
14491 build_x_modify_expr sets it and it must not be reset
14492 here. */
14493 if (TREE_NO_WARNING (t))
14494 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14495
14496 RETURN (r);
14497 }
14498
14499 case ARROW_EXPR:
14500 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14501 args, complain, in_decl);
14502 /* Remember that there was a reference to this entity. */
14503 if (DECL_P (op1))
14504 mark_used (op1);
14505 RETURN (build_x_arrow (input_location, op1, complain));
14506
14507 case NEW_EXPR:
14508 {
14509 tree placement = RECUR (TREE_OPERAND (t, 0));
14510 tree init = RECUR (TREE_OPERAND (t, 3));
14511 vec<tree, va_gc> *placement_vec;
14512 vec<tree, va_gc> *init_vec;
14513 tree ret;
14514
14515 if (placement == NULL_TREE)
14516 placement_vec = NULL;
14517 else
14518 {
14519 placement_vec = make_tree_vector ();
14520 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14521 vec_safe_push (placement_vec, TREE_VALUE (placement));
14522 }
14523
14524 /* If there was an initializer in the original tree, but it
14525 instantiated to an empty list, then we should pass a
14526 non-NULL empty vector to tell build_new that it was an
14527 empty initializer() rather than no initializer. This can
14528 only happen when the initializer is a pack expansion whose
14529 parameter packs are of length zero. */
14530 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14531 init_vec = NULL;
14532 else
14533 {
14534 init_vec = make_tree_vector ();
14535 if (init == void_zero_node)
14536 gcc_assert (init_vec != NULL);
14537 else
14538 {
14539 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14540 vec_safe_push (init_vec, TREE_VALUE (init));
14541 }
14542 }
14543
14544 ret = build_new (&placement_vec,
14545 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14546 RECUR (TREE_OPERAND (t, 2)),
14547 &init_vec,
14548 NEW_EXPR_USE_GLOBAL (t),
14549 complain);
14550
14551 if (placement_vec != NULL)
14552 release_tree_vector (placement_vec);
14553 if (init_vec != NULL)
14554 release_tree_vector (init_vec);
14555
14556 RETURN (ret);
14557 }
14558
14559 case DELETE_EXPR:
14560 RETURN (delete_sanity
14561 (RECUR (TREE_OPERAND (t, 0)),
14562 RECUR (TREE_OPERAND (t, 1)),
14563 DELETE_EXPR_USE_VEC (t),
14564 DELETE_EXPR_USE_GLOBAL (t),
14565 complain));
14566
14567 case COMPOUND_EXPR:
14568 {
14569 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14570 complain & ~tf_decltype, in_decl,
14571 /*function_p=*/false,
14572 integral_constant_expression_p);
14573 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14574 op0,
14575 RECUR (TREE_OPERAND (t, 1)),
14576 complain|decltype_flag));
14577 }
14578
14579 case CALL_EXPR:
14580 {
14581 tree function;
14582 vec<tree, va_gc> *call_args;
14583 unsigned int nargs, i;
14584 bool qualified_p;
14585 bool koenig_p;
14586 tree ret;
14587
14588 function = CALL_EXPR_FN (t);
14589 /* When we parsed the expression, we determined whether or
14590 not Koenig lookup should be performed. */
14591 koenig_p = KOENIG_LOOKUP_P (t);
14592 if (TREE_CODE (function) == SCOPE_REF)
14593 {
14594 qualified_p = true;
14595 function = tsubst_qualified_id (function, args, complain, in_decl,
14596 /*done=*/false,
14597 /*address_p=*/false);
14598 }
14599 else if (koenig_p && identifier_p (function))
14600 {
14601 /* Do nothing; calling tsubst_copy_and_build on an identifier
14602 would incorrectly perform unqualified lookup again.
14603
14604 Note that we can also have an IDENTIFIER_NODE if the earlier
14605 unqualified lookup found a member function; in that case
14606 koenig_p will be false and we do want to do the lookup
14607 again to find the instantiated member function.
14608
14609 FIXME but doing that causes c++/15272, so we need to stop
14610 using IDENTIFIER_NODE in that situation. */
14611 qualified_p = false;
14612 }
14613 else
14614 {
14615 if (TREE_CODE (function) == COMPONENT_REF)
14616 {
14617 tree op = TREE_OPERAND (function, 1);
14618
14619 qualified_p = (TREE_CODE (op) == SCOPE_REF
14620 || (BASELINK_P (op)
14621 && BASELINK_QUALIFIED_P (op)));
14622 }
14623 else
14624 qualified_p = false;
14625
14626 if (TREE_CODE (function) == ADDR_EXPR
14627 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14628 /* Avoid error about taking the address of a constructor. */
14629 function = TREE_OPERAND (function, 0);
14630
14631 function = tsubst_copy_and_build (function, args, complain,
14632 in_decl,
14633 !qualified_p,
14634 integral_constant_expression_p);
14635
14636 if (BASELINK_P (function))
14637 qualified_p = true;
14638 }
14639
14640 nargs = call_expr_nargs (t);
14641 call_args = make_tree_vector ();
14642 for (i = 0; i < nargs; ++i)
14643 {
14644 tree arg = CALL_EXPR_ARG (t, i);
14645
14646 if (!PACK_EXPANSION_P (arg))
14647 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14648 else
14649 {
14650 /* Expand the pack expansion and push each entry onto
14651 CALL_ARGS. */
14652 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14653 if (TREE_CODE (arg) == TREE_VEC)
14654 {
14655 unsigned int len, j;
14656
14657 len = TREE_VEC_LENGTH (arg);
14658 for (j = 0; j < len; ++j)
14659 {
14660 tree value = TREE_VEC_ELT (arg, j);
14661 if (value != NULL_TREE)
14662 value = convert_from_reference (value);
14663 vec_safe_push (call_args, value);
14664 }
14665 }
14666 else
14667 {
14668 /* A partial substitution. Add one entry. */
14669 vec_safe_push (call_args, arg);
14670 }
14671 }
14672 }
14673
14674 /* We do not perform argument-dependent lookup if normal
14675 lookup finds a non-function, in accordance with the
14676 expected resolution of DR 218. */
14677 if (koenig_p
14678 && ((is_overloaded_fn (function)
14679 /* If lookup found a member function, the Koenig lookup is
14680 not appropriate, even if an unqualified-name was used
14681 to denote the function. */
14682 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14683 || identifier_p (function))
14684 /* Only do this when substitution turns a dependent call
14685 into a non-dependent call. */
14686 && type_dependent_expression_p_push (t)
14687 && !any_type_dependent_arguments_p (call_args))
14688 function = perform_koenig_lookup (function, call_args, tf_none);
14689
14690 if (identifier_p (function)
14691 && !any_type_dependent_arguments_p (call_args))
14692 {
14693 if (koenig_p && (complain & tf_warning_or_error))
14694 {
14695 /* For backwards compatibility and good diagnostics, try
14696 the unqualified lookup again if we aren't in SFINAE
14697 context. */
14698 tree unq = (tsubst_copy_and_build
14699 (function, args, complain, in_decl, true,
14700 integral_constant_expression_p));
14701 if (unq == error_mark_node)
14702 RETURN (error_mark_node);
14703
14704 if (unq != function)
14705 {
14706 tree fn = unq;
14707 if (INDIRECT_REF_P (fn))
14708 fn = TREE_OPERAND (fn, 0);
14709 if (TREE_CODE (fn) == COMPONENT_REF)
14710 fn = TREE_OPERAND (fn, 1);
14711 if (is_overloaded_fn (fn))
14712 fn = get_first_fn (fn);
14713 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14714 "%qD was not declared in this scope, "
14715 "and no declarations were found by "
14716 "argument-dependent lookup at the point "
14717 "of instantiation", function))
14718 {
14719 if (!DECL_P (fn))
14720 /* Can't say anything more. */;
14721 else if (DECL_CLASS_SCOPE_P (fn))
14722 {
14723 location_t loc = EXPR_LOC_OR_LOC (t,
14724 input_location);
14725 inform (loc,
14726 "declarations in dependent base %qT are "
14727 "not found by unqualified lookup",
14728 DECL_CLASS_CONTEXT (fn));
14729 if (current_class_ptr)
14730 inform (loc,
14731 "use %<this->%D%> instead", function);
14732 else
14733 inform (loc,
14734 "use %<%T::%D%> instead",
14735 current_class_name, function);
14736 }
14737 else
14738 inform (0, "%q+D declared here, later in the "
14739 "translation unit", fn);
14740 }
14741 function = unq;
14742 }
14743 }
14744 if (identifier_p (function))
14745 {
14746 if (complain & tf_error)
14747 unqualified_name_lookup_error (function);
14748 release_tree_vector (call_args);
14749 RETURN (error_mark_node);
14750 }
14751 }
14752
14753 /* Remember that there was a reference to this entity. */
14754 if (DECL_P (function))
14755 mark_used (function);
14756
14757 /* Put back tf_decltype for the actual call. */
14758 complain |= decltype_flag;
14759
14760 if (TREE_CODE (function) == OFFSET_REF)
14761 ret = build_offset_ref_call_from_tree (function, &call_args,
14762 complain);
14763 else if (TREE_CODE (function) == COMPONENT_REF)
14764 {
14765 tree instance = TREE_OPERAND (function, 0);
14766 tree fn = TREE_OPERAND (function, 1);
14767
14768 if (processing_template_decl
14769 && (type_dependent_expression_p (instance)
14770 || (!BASELINK_P (fn)
14771 && TREE_CODE (fn) != FIELD_DECL)
14772 || type_dependent_expression_p (fn)
14773 || any_type_dependent_arguments_p (call_args)))
14774 ret = build_nt_call_vec (function, call_args);
14775 else if (!BASELINK_P (fn))
14776 ret = finish_call_expr (function, &call_args,
14777 /*disallow_virtual=*/false,
14778 /*koenig_p=*/false,
14779 complain);
14780 else
14781 ret = (build_new_method_call
14782 (instance, fn,
14783 &call_args, NULL_TREE,
14784 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14785 /*fn_p=*/NULL,
14786 complain));
14787 }
14788 else
14789 ret = finish_call_expr (function, &call_args,
14790 /*disallow_virtual=*/qualified_p,
14791 koenig_p,
14792 complain);
14793
14794 release_tree_vector (call_args);
14795
14796 RETURN (ret);
14797 }
14798
14799 case COND_EXPR:
14800 {
14801 tree cond = RECUR (TREE_OPERAND (t, 0));
14802 tree exp1, exp2;
14803
14804 if (TREE_CODE (cond) == INTEGER_CST)
14805 {
14806 if (integer_zerop (cond))
14807 {
14808 ++c_inhibit_evaluation_warnings;
14809 exp1 = RECUR (TREE_OPERAND (t, 1));
14810 --c_inhibit_evaluation_warnings;
14811 exp2 = RECUR (TREE_OPERAND (t, 2));
14812 }
14813 else
14814 {
14815 exp1 = RECUR (TREE_OPERAND (t, 1));
14816 ++c_inhibit_evaluation_warnings;
14817 exp2 = RECUR (TREE_OPERAND (t, 2));
14818 --c_inhibit_evaluation_warnings;
14819 }
14820 }
14821 else
14822 {
14823 exp1 = RECUR (TREE_OPERAND (t, 1));
14824 exp2 = RECUR (TREE_OPERAND (t, 2));
14825 }
14826
14827 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14828 cond, exp1, exp2, complain));
14829 }
14830
14831 case PSEUDO_DTOR_EXPR:
14832 RETURN (finish_pseudo_destructor_expr
14833 (RECUR (TREE_OPERAND (t, 0)),
14834 RECUR (TREE_OPERAND (t, 1)),
14835 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14836 input_location));
14837
14838 case TREE_LIST:
14839 {
14840 tree purpose, value, chain;
14841
14842 if (t == void_list_node)
14843 RETURN (t);
14844
14845 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14846 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14847 {
14848 /* We have pack expansions, so expand those and
14849 create a new list out of it. */
14850 tree purposevec = NULL_TREE;
14851 tree valuevec = NULL_TREE;
14852 tree chain;
14853 int i, len = -1;
14854
14855 /* Expand the argument expressions. */
14856 if (TREE_PURPOSE (t))
14857 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14858 complain, in_decl);
14859 if (TREE_VALUE (t))
14860 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14861 complain, in_decl);
14862
14863 /* Build the rest of the list. */
14864 chain = TREE_CHAIN (t);
14865 if (chain && chain != void_type_node)
14866 chain = RECUR (chain);
14867
14868 /* Determine the number of arguments. */
14869 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14870 {
14871 len = TREE_VEC_LENGTH (purposevec);
14872 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14873 }
14874 else if (TREE_CODE (valuevec) == TREE_VEC)
14875 len = TREE_VEC_LENGTH (valuevec);
14876 else
14877 {
14878 /* Since we only performed a partial substitution into
14879 the argument pack, we only RETURN (a single list
14880 node. */
14881 if (purposevec == TREE_PURPOSE (t)
14882 && valuevec == TREE_VALUE (t)
14883 && chain == TREE_CHAIN (t))
14884 RETURN (t);
14885
14886 RETURN (tree_cons (purposevec, valuevec, chain));
14887 }
14888
14889 /* Convert the argument vectors into a TREE_LIST */
14890 i = len;
14891 while (i > 0)
14892 {
14893 /* Grab the Ith values. */
14894 i--;
14895 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14896 : NULL_TREE;
14897 value
14898 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14899 : NULL_TREE;
14900
14901 /* Build the list (backwards). */
14902 chain = tree_cons (purpose, value, chain);
14903 }
14904
14905 RETURN (chain);
14906 }
14907
14908 purpose = TREE_PURPOSE (t);
14909 if (purpose)
14910 purpose = RECUR (purpose);
14911 value = TREE_VALUE (t);
14912 if (value)
14913 value = RECUR (value);
14914 chain = TREE_CHAIN (t);
14915 if (chain && chain != void_type_node)
14916 chain = RECUR (chain);
14917 if (purpose == TREE_PURPOSE (t)
14918 && value == TREE_VALUE (t)
14919 && chain == TREE_CHAIN (t))
14920 RETURN (t);
14921 RETURN (tree_cons (purpose, value, chain));
14922 }
14923
14924 case COMPONENT_REF:
14925 {
14926 tree object;
14927 tree object_type;
14928 tree member;
14929 tree r;
14930
14931 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14932 args, complain, in_decl);
14933 /* Remember that there was a reference to this entity. */
14934 if (DECL_P (object))
14935 mark_used (object);
14936 object_type = TREE_TYPE (object);
14937
14938 member = TREE_OPERAND (t, 1);
14939 if (BASELINK_P (member))
14940 member = tsubst_baselink (member,
14941 non_reference (TREE_TYPE (object)),
14942 args, complain, in_decl);
14943 else
14944 member = tsubst_copy (member, args, complain, in_decl);
14945 if (member == error_mark_node)
14946 RETURN (error_mark_node);
14947
14948 if (type_dependent_expression_p (object))
14949 /* We can't do much here. */;
14950 else if (!CLASS_TYPE_P (object_type))
14951 {
14952 if (scalarish_type_p (object_type))
14953 {
14954 tree s = NULL_TREE;
14955 tree dtor = member;
14956
14957 if (TREE_CODE (dtor) == SCOPE_REF)
14958 {
14959 s = TREE_OPERAND (dtor, 0);
14960 dtor = TREE_OPERAND (dtor, 1);
14961 }
14962 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14963 {
14964 dtor = TREE_OPERAND (dtor, 0);
14965 if (TYPE_P (dtor))
14966 RETURN (finish_pseudo_destructor_expr
14967 (object, s, dtor, input_location));
14968 }
14969 }
14970 }
14971 else if (TREE_CODE (member) == SCOPE_REF
14972 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14973 {
14974 /* Lookup the template functions now that we know what the
14975 scope is. */
14976 tree scope = TREE_OPERAND (member, 0);
14977 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14978 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14979 member = lookup_qualified_name (scope, tmpl,
14980 /*is_type_p=*/false,
14981 /*complain=*/false);
14982 if (BASELINK_P (member))
14983 {
14984 BASELINK_FUNCTIONS (member)
14985 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14986 args);
14987 member = (adjust_result_of_qualified_name_lookup
14988 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14989 object_type));
14990 }
14991 else
14992 {
14993 qualified_name_lookup_error (scope, tmpl, member,
14994 input_location);
14995 RETURN (error_mark_node);
14996 }
14997 }
14998 else if (TREE_CODE (member) == SCOPE_REF
14999 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15000 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15001 {
15002 if (complain & tf_error)
15003 {
15004 if (TYPE_P (TREE_OPERAND (member, 0)))
15005 error ("%qT is not a class or namespace",
15006 TREE_OPERAND (member, 0));
15007 else
15008 error ("%qD is not a class or namespace",
15009 TREE_OPERAND (member, 0));
15010 }
15011 RETURN (error_mark_node);
15012 }
15013 else if (TREE_CODE (member) == FIELD_DECL)
15014 {
15015 r = finish_non_static_data_member (member, object, NULL_TREE);
15016 if (TREE_CODE (r) == COMPONENT_REF)
15017 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15018 RETURN (r);
15019 }
15020
15021 r = finish_class_member_access_expr (object, member,
15022 /*template_p=*/false,
15023 complain);
15024 if (TREE_CODE (r) == COMPONENT_REF)
15025 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15026 RETURN (r);
15027 }
15028
15029 case THROW_EXPR:
15030 RETURN (build_throw
15031 (RECUR (TREE_OPERAND (t, 0))));
15032
15033 case CONSTRUCTOR:
15034 {
15035 vec<constructor_elt, va_gc> *n;
15036 constructor_elt *ce;
15037 unsigned HOST_WIDE_INT idx;
15038 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15039 bool process_index_p;
15040 int newlen;
15041 bool need_copy_p = false;
15042 tree r;
15043
15044 if (type == error_mark_node)
15045 RETURN (error_mark_node);
15046
15047 /* digest_init will do the wrong thing if we let it. */
15048 if (type && TYPE_PTRMEMFUNC_P (type))
15049 RETURN (t);
15050
15051 /* We do not want to process the index of aggregate
15052 initializers as they are identifier nodes which will be
15053 looked up by digest_init. */
15054 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15055
15056 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15057 newlen = vec_safe_length (n);
15058 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15059 {
15060 if (ce->index && process_index_p
15061 /* An identifier index is looked up in the type
15062 being initialized, not the current scope. */
15063 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15064 ce->index = RECUR (ce->index);
15065
15066 if (PACK_EXPANSION_P (ce->value))
15067 {
15068 /* Substitute into the pack expansion. */
15069 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15070 in_decl);
15071
15072 if (ce->value == error_mark_node
15073 || PACK_EXPANSION_P (ce->value))
15074 ;
15075 else if (TREE_VEC_LENGTH (ce->value) == 1)
15076 /* Just move the argument into place. */
15077 ce->value = TREE_VEC_ELT (ce->value, 0);
15078 else
15079 {
15080 /* Update the length of the final CONSTRUCTOR
15081 arguments vector, and note that we will need to
15082 copy.*/
15083 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15084 need_copy_p = true;
15085 }
15086 }
15087 else
15088 ce->value = RECUR (ce->value);
15089 }
15090
15091 if (need_copy_p)
15092 {
15093 vec<constructor_elt, va_gc> *old_n = n;
15094
15095 vec_alloc (n, newlen);
15096 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15097 {
15098 if (TREE_CODE (ce->value) == TREE_VEC)
15099 {
15100 int i, len = TREE_VEC_LENGTH (ce->value);
15101 for (i = 0; i < len; ++i)
15102 CONSTRUCTOR_APPEND_ELT (n, 0,
15103 TREE_VEC_ELT (ce->value, i));
15104 }
15105 else
15106 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15107 }
15108 }
15109
15110 r = build_constructor (init_list_type_node, n);
15111 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15112
15113 if (TREE_HAS_CONSTRUCTOR (t))
15114 RETURN (finish_compound_literal (type, r, complain));
15115
15116 TREE_TYPE (r) = type;
15117 RETURN (r);
15118 }
15119
15120 case TYPEID_EXPR:
15121 {
15122 tree operand_0 = TREE_OPERAND (t, 0);
15123 if (TYPE_P (operand_0))
15124 {
15125 operand_0 = tsubst (operand_0, args, complain, in_decl);
15126 RETURN (get_typeid (operand_0, complain));
15127 }
15128 else
15129 {
15130 operand_0 = RECUR (operand_0);
15131 RETURN (build_typeid (operand_0, complain));
15132 }
15133 }
15134
15135 case VAR_DECL:
15136 if (!args)
15137 RETURN (t);
15138 else if (DECL_PACK_P (t))
15139 {
15140 /* We don't build decls for an instantiation of a
15141 variadic capture proxy, we instantiate the elements
15142 when needed. */
15143 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15144 return RECUR (DECL_VALUE_EXPR (t));
15145 }
15146 /* Fall through */
15147
15148 case PARM_DECL:
15149 {
15150 tree r = tsubst_copy (t, args, complain, in_decl);
15151
15152 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15153 /* If the original type was a reference, we'll be wrapped in
15154 the appropriate INDIRECT_REF. */
15155 r = convert_from_reference (r);
15156 RETURN (r);
15157 }
15158
15159 case VA_ARG_EXPR:
15160 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15161 RECUR (TREE_OPERAND (t, 0)),
15162 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15163
15164 case OFFSETOF_EXPR:
15165 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15166
15167 case TRAIT_EXPR:
15168 {
15169 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15170 complain, in_decl);
15171
15172 tree type2 = TRAIT_EXPR_TYPE2 (t);
15173 if (type2)
15174 type2 = tsubst (type2, args, complain, in_decl);
15175
15176 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15177 }
15178
15179 case STMT_EXPR:
15180 {
15181 tree old_stmt_expr = cur_stmt_expr;
15182 tree stmt_expr = begin_stmt_expr ();
15183
15184 cur_stmt_expr = stmt_expr;
15185 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15186 integral_constant_expression_p);
15187 stmt_expr = finish_stmt_expr (stmt_expr, false);
15188 cur_stmt_expr = old_stmt_expr;
15189
15190 /* If the resulting list of expression statement is empty,
15191 fold it further into void_zero_node. */
15192 if (empty_expr_stmt_p (stmt_expr))
15193 stmt_expr = void_zero_node;
15194
15195 RETURN (stmt_expr);
15196 }
15197
15198 case LAMBDA_EXPR:
15199 {
15200 tree r = build_lambda_expr ();
15201
15202 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15203 LAMBDA_EXPR_CLOSURE (r) = type;
15204 CLASSTYPE_LAMBDA_EXPR (type) = r;
15205
15206 LAMBDA_EXPR_LOCATION (r)
15207 = LAMBDA_EXPR_LOCATION (t);
15208 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15209 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15210 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15211 LAMBDA_EXPR_DISCRIMINATOR (r)
15212 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15213 /* For a function scope, we want to use tsubst so that we don't
15214 complain about referring to an auto function before its return
15215 type has been deduced. Otherwise, we want to use tsubst_copy so
15216 that we look up the existing field/parameter/variable rather
15217 than build a new one. */
15218 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15219 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15220 scope = tsubst (scope, args, complain, in_decl);
15221 else if (scope && TREE_CODE (scope) == PARM_DECL)
15222 {
15223 /* Look up the parameter we want directly, as tsubst_copy
15224 doesn't do what we need. */
15225 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15226 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15227 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15228 parm = DECL_CHAIN (parm);
15229 scope = parm;
15230 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15231 if (DECL_CONTEXT (scope) == NULL_TREE)
15232 DECL_CONTEXT (scope) = fn;
15233 }
15234 else
15235 scope = RECUR (scope);
15236 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15237 LAMBDA_EXPR_RETURN_TYPE (r)
15238 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15239
15240 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15241 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15242
15243 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15244 determine_visibility (TYPE_NAME (type));
15245 /* Now that we know visibility, instantiate the type so we have a
15246 declaration of the op() for later calls to lambda_function. */
15247 complete_type (type);
15248
15249 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15250
15251 RETURN (build_lambda_object (r));
15252 }
15253
15254 case TARGET_EXPR:
15255 /* We can get here for a constant initializer of non-dependent type.
15256 FIXME stop folding in cp_parser_initializer_clause. */
15257 {
15258 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15259 complain);
15260 RETURN (r);
15261 }
15262
15263 case TRANSACTION_EXPR:
15264 RETURN (tsubst_expr(t, args, complain, in_decl,
15265 integral_constant_expression_p));
15266
15267 case PAREN_EXPR:
15268 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15269
15270 case VEC_PERM_EXPR:
15271 RETURN (build_x_vec_perm_expr (input_location,
15272 RECUR (TREE_OPERAND (t, 0)),
15273 RECUR (TREE_OPERAND (t, 1)),
15274 RECUR (TREE_OPERAND (t, 2)),
15275 complain));
15276
15277 default:
15278 /* Handle Objective-C++ constructs, if appropriate. */
15279 {
15280 tree subst
15281 = objcp_tsubst_copy_and_build (t, args, complain,
15282 in_decl, /*function_p=*/false);
15283 if (subst)
15284 RETURN (subst);
15285 }
15286 RETURN (tsubst_copy (t, args, complain, in_decl));
15287 }
15288
15289 #undef RECUR
15290 #undef RETURN
15291 out:
15292 input_location = loc;
15293 return retval;
15294 }
15295
15296 /* Verify that the instantiated ARGS are valid. For type arguments,
15297 make sure that the type's linkage is ok. For non-type arguments,
15298 make sure they are constants if they are integral or enumerations.
15299 Emit an error under control of COMPLAIN, and return TRUE on error. */
15300
15301 static bool
15302 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15303 {
15304 if (dependent_template_arg_p (t))
15305 return false;
15306 if (ARGUMENT_PACK_P (t))
15307 {
15308 tree vec = ARGUMENT_PACK_ARGS (t);
15309 int len = TREE_VEC_LENGTH (vec);
15310 bool result = false;
15311 int i;
15312
15313 for (i = 0; i < len; ++i)
15314 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15315 result = true;
15316 return result;
15317 }
15318 else if (TYPE_P (t))
15319 {
15320 /* [basic.link]: A name with no linkage (notably, the name
15321 of a class or enumeration declared in a local scope)
15322 shall not be used to declare an entity with linkage.
15323 This implies that names with no linkage cannot be used as
15324 template arguments
15325
15326 DR 757 relaxes this restriction for C++0x. */
15327 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15328 : no_linkage_check (t, /*relaxed_p=*/false));
15329
15330 if (nt)
15331 {
15332 /* DR 488 makes use of a type with no linkage cause
15333 type deduction to fail. */
15334 if (complain & tf_error)
15335 {
15336 if (TYPE_ANONYMOUS_P (nt))
15337 error ("%qT is/uses anonymous type", t);
15338 else
15339 error ("template argument for %qD uses local type %qT",
15340 tmpl, t);
15341 }
15342 return true;
15343 }
15344 /* In order to avoid all sorts of complications, we do not
15345 allow variably-modified types as template arguments. */
15346 else if (variably_modified_type_p (t, NULL_TREE))
15347 {
15348 if (complain & tf_error)
15349 error ("%qT is a variably modified type", t);
15350 return true;
15351 }
15352 }
15353 /* Class template and alias template arguments should be OK. */
15354 else if (DECL_TYPE_TEMPLATE_P (t))
15355 ;
15356 /* A non-type argument of integral or enumerated type must be a
15357 constant. */
15358 else if (TREE_TYPE (t)
15359 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15360 && !TREE_CONSTANT (t))
15361 {
15362 if (complain & tf_error)
15363 error ("integral expression %qE is not constant", t);
15364 return true;
15365 }
15366 return false;
15367 }
15368
15369 static bool
15370 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15371 {
15372 int ix, len = DECL_NTPARMS (tmpl);
15373 bool result = false;
15374
15375 for (ix = 0; ix != len; ix++)
15376 {
15377 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15378 result = true;
15379 }
15380 if (result && (complain & tf_error))
15381 error (" trying to instantiate %qD", tmpl);
15382 return result;
15383 }
15384
15385 /* We're out of SFINAE context now, so generate diagnostics for the access
15386 errors we saw earlier when instantiating D from TMPL and ARGS. */
15387
15388 static void
15389 recheck_decl_substitution (tree d, tree tmpl, tree args)
15390 {
15391 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15392 tree type = TREE_TYPE (pattern);
15393 location_t loc = input_location;
15394
15395 push_access_scope (d);
15396 push_deferring_access_checks (dk_no_deferred);
15397 input_location = DECL_SOURCE_LOCATION (pattern);
15398 tsubst (type, args, tf_warning_or_error, d);
15399 input_location = loc;
15400 pop_deferring_access_checks ();
15401 pop_access_scope (d);
15402 }
15403
15404 /* Instantiate the indicated variable, function, or alias template TMPL with
15405 the template arguments in TARG_PTR. */
15406
15407 static tree
15408 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15409 {
15410 tree targ_ptr = orig_args;
15411 tree fndecl;
15412 tree gen_tmpl;
15413 tree spec;
15414 bool access_ok = true;
15415
15416 if (tmpl == error_mark_node)
15417 return error_mark_node;
15418
15419 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15420
15421 /* If this function is a clone, handle it specially. */
15422 if (DECL_CLONED_FUNCTION_P (tmpl))
15423 {
15424 tree spec;
15425 tree clone;
15426
15427 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15428 DECL_CLONED_FUNCTION. */
15429 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15430 targ_ptr, complain);
15431 if (spec == error_mark_node)
15432 return error_mark_node;
15433
15434 /* Look for the clone. */
15435 FOR_EACH_CLONE (clone, spec)
15436 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15437 return clone;
15438 /* We should always have found the clone by now. */
15439 gcc_unreachable ();
15440 return NULL_TREE;
15441 }
15442
15443 if (targ_ptr == error_mark_node)
15444 return error_mark_node;
15445
15446 /* Check to see if we already have this specialization. */
15447 gen_tmpl = most_general_template (tmpl);
15448 if (tmpl != gen_tmpl)
15449 /* The TMPL is a partial instantiation. To get a full set of
15450 arguments we must add the arguments used to perform the
15451 partial instantiation. */
15452 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15453 targ_ptr);
15454
15455 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15456 but it doesn't seem to be on the hot path. */
15457 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15458
15459 gcc_assert (tmpl == gen_tmpl
15460 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15461 == spec)
15462 || fndecl == NULL_TREE);
15463
15464 if (spec != NULL_TREE)
15465 {
15466 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15467 {
15468 if (complain & tf_error)
15469 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15470 return error_mark_node;
15471 }
15472 return spec;
15473 }
15474
15475 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15476 complain))
15477 return error_mark_node;
15478
15479 /* We are building a FUNCTION_DECL, during which the access of its
15480 parameters and return types have to be checked. However this
15481 FUNCTION_DECL which is the desired context for access checking
15482 is not built yet. We solve this chicken-and-egg problem by
15483 deferring all checks until we have the FUNCTION_DECL. */
15484 push_deferring_access_checks (dk_deferred);
15485
15486 /* Instantiation of the function happens in the context of the function
15487 template, not the context of the overload resolution we're doing. */
15488 push_to_top_level ();
15489 /* If there are dependent arguments, e.g. because we're doing partial
15490 ordering, make sure processing_template_decl stays set. */
15491 if (uses_template_parms (targ_ptr))
15492 ++processing_template_decl;
15493 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15494 {
15495 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15496 complain, gen_tmpl);
15497 push_nested_class (ctx);
15498 }
15499 /* Substitute template parameters to obtain the specialization. */
15500 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15501 targ_ptr, complain, gen_tmpl);
15502 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15503 pop_nested_class ();
15504 pop_from_top_level ();
15505
15506 if (fndecl == error_mark_node)
15507 {
15508 pop_deferring_access_checks ();
15509 return error_mark_node;
15510 }
15511
15512 /* The DECL_TI_TEMPLATE should always be the immediate parent
15513 template, not the most general template. */
15514 DECL_TI_TEMPLATE (fndecl) = tmpl;
15515
15516 /* Now we know the specialization, compute access previously
15517 deferred. */
15518 push_access_scope (fndecl);
15519 if (!perform_deferred_access_checks (complain))
15520 access_ok = false;
15521 pop_access_scope (fndecl);
15522 pop_deferring_access_checks ();
15523
15524 /* If we've just instantiated the main entry point for a function,
15525 instantiate all the alternate entry points as well. We do this
15526 by cloning the instantiation of the main entry point, not by
15527 instantiating the template clones. */
15528 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15529 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15530
15531 if (!access_ok)
15532 {
15533 if (!(complain & tf_error))
15534 {
15535 /* Remember to reinstantiate when we're out of SFINAE so the user
15536 can see the errors. */
15537 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15538 }
15539 return error_mark_node;
15540 }
15541 return fndecl;
15542 }
15543
15544 /* Wrapper for instantiate_template_1. */
15545
15546 tree
15547 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15548 {
15549 tree ret;
15550 timevar_push (TV_TEMPLATE_INST);
15551 ret = instantiate_template_1 (tmpl, orig_args, complain);
15552 timevar_pop (TV_TEMPLATE_INST);
15553 return ret;
15554 }
15555
15556 /* Instantiate the alias template TMPL with ARGS. Also push a template
15557 instantiation level, which instantiate_template doesn't do because
15558 functions and variables have sufficient context established by the
15559 callers. */
15560
15561 static tree
15562 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15563 {
15564 struct pending_template *old_last_pend = last_pending_template;
15565 struct tinst_level *old_error_tinst = last_error_tinst_level;
15566 if (tmpl == error_mark_node || args == error_mark_node)
15567 return error_mark_node;
15568 tree tinst = build_tree_list (tmpl, args);
15569 if (!push_tinst_level (tinst))
15570 {
15571 ggc_free (tinst);
15572 return error_mark_node;
15573 }
15574
15575 args =
15576 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15577 args, tmpl, complain,
15578 /*require_all_args=*/true,
15579 /*use_default_args=*/true);
15580
15581 tree r = instantiate_template (tmpl, args, complain);
15582 pop_tinst_level ();
15583 /* We can't free this if a pending_template entry or last_error_tinst_level
15584 is pointing at it. */
15585 if (last_pending_template == old_last_pend
15586 && last_error_tinst_level == old_error_tinst)
15587 ggc_free (tinst);
15588
15589 return r;
15590 }
15591
15592 /* PARM is a template parameter pack for FN. Returns true iff
15593 PARM is used in a deducible way in the argument list of FN. */
15594
15595 static bool
15596 pack_deducible_p (tree parm, tree fn)
15597 {
15598 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15599 for (; t; t = TREE_CHAIN (t))
15600 {
15601 tree type = TREE_VALUE (t);
15602 tree packs;
15603 if (!PACK_EXPANSION_P (type))
15604 continue;
15605 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15606 packs; packs = TREE_CHAIN (packs))
15607 if (TREE_VALUE (packs) == parm)
15608 {
15609 /* The template parameter pack is used in a function parameter
15610 pack. If this is the end of the parameter list, the
15611 template parameter pack is deducible. */
15612 if (TREE_CHAIN (t) == void_list_node)
15613 return true;
15614 else
15615 /* Otherwise, not. Well, it could be deduced from
15616 a non-pack parameter, but doing so would end up with
15617 a deduction mismatch, so don't bother. */
15618 return false;
15619 }
15620 }
15621 /* The template parameter pack isn't used in any function parameter
15622 packs, but it might be used deeper, e.g. tuple<Args...>. */
15623 return true;
15624 }
15625
15626 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15627 NARGS elements of the arguments that are being used when calling
15628 it. TARGS is a vector into which the deduced template arguments
15629 are placed.
15630
15631 Returns either a FUNCTION_DECL for the matching specialization of FN or
15632 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15633 true, diagnostics will be printed to explain why it failed.
15634
15635 If FN is a conversion operator, or we are trying to produce a specific
15636 specialization, RETURN_TYPE is the return type desired.
15637
15638 The EXPLICIT_TARGS are explicit template arguments provided via a
15639 template-id.
15640
15641 The parameter STRICT is one of:
15642
15643 DEDUCE_CALL:
15644 We are deducing arguments for a function call, as in
15645 [temp.deduct.call].
15646
15647 DEDUCE_CONV:
15648 We are deducing arguments for a conversion function, as in
15649 [temp.deduct.conv].
15650
15651 DEDUCE_EXACT:
15652 We are deducing arguments when doing an explicit instantiation
15653 as in [temp.explicit], when determining an explicit specialization
15654 as in [temp.expl.spec], or when taking the address of a function
15655 template, as in [temp.deduct.funcaddr]. */
15656
15657 tree
15658 fn_type_unification (tree fn,
15659 tree explicit_targs,
15660 tree targs,
15661 const tree *args,
15662 unsigned int nargs,
15663 tree return_type,
15664 unification_kind_t strict,
15665 int flags,
15666 bool explain_p,
15667 bool decltype_p)
15668 {
15669 tree parms;
15670 tree fntype;
15671 tree decl = NULL_TREE;
15672 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15673 bool ok;
15674 static int deduction_depth;
15675 struct pending_template *old_last_pend = last_pending_template;
15676 struct tinst_level *old_error_tinst = last_error_tinst_level;
15677 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15678 tree tinst;
15679 tree r = error_mark_node;
15680
15681 if (decltype_p)
15682 complain |= tf_decltype;
15683
15684 /* In C++0x, it's possible to have a function template whose type depends
15685 on itself recursively. This is most obvious with decltype, but can also
15686 occur with enumeration scope (c++/48969). So we need to catch infinite
15687 recursion and reject the substitution at deduction time; this function
15688 will return error_mark_node for any repeated substitution.
15689
15690 This also catches excessive recursion such as when f<N> depends on
15691 f<N-1> across all integers, and returns error_mark_node for all the
15692 substitutions back up to the initial one.
15693
15694 This is, of course, not reentrant. */
15695 if (excessive_deduction_depth)
15696 return error_mark_node;
15697 tinst = build_tree_list (fn, NULL_TREE);
15698 ++deduction_depth;
15699
15700 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15701
15702 fntype = TREE_TYPE (fn);
15703 if (explicit_targs)
15704 {
15705 /* [temp.deduct]
15706
15707 The specified template arguments must match the template
15708 parameters in kind (i.e., type, nontype, template), and there
15709 must not be more arguments than there are parameters;
15710 otherwise type deduction fails.
15711
15712 Nontype arguments must match the types of the corresponding
15713 nontype template parameters, or must be convertible to the
15714 types of the corresponding nontype parameters as specified in
15715 _temp.arg.nontype_, otherwise type deduction fails.
15716
15717 All references in the function type of the function template
15718 to the corresponding template parameters are replaced by the
15719 specified template argument values. If a substitution in a
15720 template parameter or in the function type of the function
15721 template results in an invalid type, type deduction fails. */
15722 int i, len = TREE_VEC_LENGTH (tparms);
15723 location_t loc = input_location;
15724 bool incomplete = false;
15725
15726 /* Adjust any explicit template arguments before entering the
15727 substitution context. */
15728 explicit_targs
15729 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15730 complain,
15731 /*require_all_args=*/false,
15732 /*use_default_args=*/false));
15733 if (explicit_targs == error_mark_node)
15734 goto fail;
15735
15736 /* Substitute the explicit args into the function type. This is
15737 necessary so that, for instance, explicitly declared function
15738 arguments can match null pointed constants. If we were given
15739 an incomplete set of explicit args, we must not do semantic
15740 processing during substitution as we could create partial
15741 instantiations. */
15742 for (i = 0; i < len; i++)
15743 {
15744 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15745 bool parameter_pack = false;
15746 tree targ = TREE_VEC_ELT (explicit_targs, i);
15747
15748 /* Dig out the actual parm. */
15749 if (TREE_CODE (parm) == TYPE_DECL
15750 || TREE_CODE (parm) == TEMPLATE_DECL)
15751 {
15752 parm = TREE_TYPE (parm);
15753 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15754 }
15755 else if (TREE_CODE (parm) == PARM_DECL)
15756 {
15757 parm = DECL_INITIAL (parm);
15758 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15759 }
15760
15761 if (!parameter_pack && targ == NULL_TREE)
15762 /* No explicit argument for this template parameter. */
15763 incomplete = true;
15764
15765 if (parameter_pack && pack_deducible_p (parm, fn))
15766 {
15767 /* Mark the argument pack as "incomplete". We could
15768 still deduce more arguments during unification.
15769 We remove this mark in type_unification_real. */
15770 if (targ)
15771 {
15772 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15773 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15774 = ARGUMENT_PACK_ARGS (targ);
15775 }
15776
15777 /* We have some incomplete argument packs. */
15778 incomplete = true;
15779 }
15780 }
15781
15782 TREE_VALUE (tinst) = explicit_targs;
15783 if (!push_tinst_level (tinst))
15784 {
15785 excessive_deduction_depth = true;
15786 goto fail;
15787 }
15788 processing_template_decl += incomplete;
15789 input_location = DECL_SOURCE_LOCATION (fn);
15790 /* Ignore any access checks; we'll see them again in
15791 instantiate_template and they might have the wrong
15792 access path at this point. */
15793 push_deferring_access_checks (dk_deferred);
15794 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15795 complain | tf_partial, NULL_TREE);
15796 pop_deferring_access_checks ();
15797 input_location = loc;
15798 processing_template_decl -= incomplete;
15799 pop_tinst_level ();
15800
15801 if (fntype == error_mark_node)
15802 goto fail;
15803
15804 /* Place the explicitly specified arguments in TARGS. */
15805 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15806 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15807 }
15808
15809 /* Never do unification on the 'this' parameter. */
15810 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15811
15812 if (return_type)
15813 {
15814 tree *new_args;
15815
15816 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15817 new_args = XALLOCAVEC (tree, nargs + 1);
15818 new_args[0] = return_type;
15819 memcpy (new_args + 1, args, nargs * sizeof (tree));
15820 args = new_args;
15821 ++nargs;
15822 }
15823
15824 /* We allow incomplete unification without an error message here
15825 because the standard doesn't seem to explicitly prohibit it. Our
15826 callers must be ready to deal with unification failures in any
15827 event. */
15828
15829 TREE_VALUE (tinst) = targs;
15830 /* If we aren't explaining yet, push tinst context so we can see where
15831 any errors (e.g. from class instantiations triggered by instantiation
15832 of default template arguments) come from. If we are explaining, this
15833 context is redundant. */
15834 if (!explain_p && !push_tinst_level (tinst))
15835 {
15836 excessive_deduction_depth = true;
15837 goto fail;
15838 }
15839
15840 /* type_unification_real will pass back any access checks from default
15841 template argument substitution. */
15842 vec<deferred_access_check, va_gc> *checks;
15843 checks = NULL;
15844
15845 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15846 targs, parms, args, nargs, /*subr=*/0,
15847 strict, flags, &checks, explain_p);
15848 if (!explain_p)
15849 pop_tinst_level ();
15850 if (!ok)
15851 goto fail;
15852
15853 /* Now that we have bindings for all of the template arguments,
15854 ensure that the arguments deduced for the template template
15855 parameters have compatible template parameter lists. We cannot
15856 check this property before we have deduced all template
15857 arguments, because the template parameter types of a template
15858 template parameter might depend on prior template parameters
15859 deduced after the template template parameter. The following
15860 ill-formed example illustrates this issue:
15861
15862 template<typename T, template<T> class C> void f(C<5>, T);
15863
15864 template<int N> struct X {};
15865
15866 void g() {
15867 f(X<5>(), 5l); // error: template argument deduction fails
15868 }
15869
15870 The template parameter list of 'C' depends on the template type
15871 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15872 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15873 time that we deduce 'C'. */
15874 if (!template_template_parm_bindings_ok_p
15875 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15876 {
15877 unify_inconsistent_template_template_parameters (explain_p);
15878 goto fail;
15879 }
15880
15881 /* All is well so far. Now, check:
15882
15883 [temp.deduct]
15884
15885 When all template arguments have been deduced, all uses of
15886 template parameters in nondeduced contexts are replaced with
15887 the corresponding deduced argument values. If the
15888 substitution results in an invalid type, as described above,
15889 type deduction fails. */
15890 TREE_VALUE (tinst) = targs;
15891 if (!push_tinst_level (tinst))
15892 {
15893 excessive_deduction_depth = true;
15894 goto fail;
15895 }
15896
15897 /* Also collect access checks from the instantiation. */
15898 reopen_deferring_access_checks (checks);
15899
15900 decl = instantiate_template (fn, targs, complain);
15901
15902 checks = get_deferred_access_checks ();
15903 pop_deferring_access_checks ();
15904
15905 pop_tinst_level ();
15906
15907 if (decl == error_mark_node)
15908 goto fail;
15909
15910 /* Now perform any access checks encountered during substitution. */
15911 push_access_scope (decl);
15912 ok = perform_access_checks (checks, complain);
15913 pop_access_scope (decl);
15914 if (!ok)
15915 goto fail;
15916
15917 /* If we're looking for an exact match, check that what we got
15918 is indeed an exact match. It might not be if some template
15919 parameters are used in non-deduced contexts. But don't check
15920 for an exact match if we have dependent template arguments;
15921 in that case we're doing partial ordering, and we already know
15922 that we have two candidates that will provide the actual type. */
15923 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
15924 {
15925 tree substed = TREE_TYPE (decl);
15926 unsigned int i;
15927
15928 tree sarg
15929 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15930 if (return_type)
15931 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15932 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15933 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15934 {
15935 unify_type_mismatch (explain_p, args[i],
15936 TREE_VALUE (sarg));
15937 goto fail;
15938 }
15939 }
15940
15941 r = decl;
15942
15943 fail:
15944 --deduction_depth;
15945 if (excessive_deduction_depth)
15946 {
15947 if (deduction_depth == 0)
15948 /* Reset once we're all the way out. */
15949 excessive_deduction_depth = false;
15950 }
15951
15952 /* We can't free this if a pending_template entry or last_error_tinst_level
15953 is pointing at it. */
15954 if (last_pending_template == old_last_pend
15955 && last_error_tinst_level == old_error_tinst)
15956 ggc_free (tinst);
15957
15958 return r;
15959 }
15960
15961 /* Adjust types before performing type deduction, as described in
15962 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15963 sections are symmetric. PARM is the type of a function parameter
15964 or the return type of the conversion function. ARG is the type of
15965 the argument passed to the call, or the type of the value
15966 initialized with the result of the conversion function.
15967 ARG_EXPR is the original argument expression, which may be null. */
15968
15969 static int
15970 maybe_adjust_types_for_deduction (unification_kind_t strict,
15971 tree* parm,
15972 tree* arg,
15973 tree arg_expr)
15974 {
15975 int result = 0;
15976
15977 switch (strict)
15978 {
15979 case DEDUCE_CALL:
15980 break;
15981
15982 case DEDUCE_CONV:
15983 {
15984 /* Swap PARM and ARG throughout the remainder of this
15985 function; the handling is precisely symmetric since PARM
15986 will initialize ARG rather than vice versa. */
15987 tree* temp = parm;
15988 parm = arg;
15989 arg = temp;
15990 break;
15991 }
15992
15993 case DEDUCE_EXACT:
15994 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15995 too, but here handle it by stripping the reference from PARM
15996 rather than by adding it to ARG. */
15997 if (TREE_CODE (*parm) == REFERENCE_TYPE
15998 && TYPE_REF_IS_RVALUE (*parm)
15999 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16000 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16001 && TREE_CODE (*arg) == REFERENCE_TYPE
16002 && !TYPE_REF_IS_RVALUE (*arg))
16003 *parm = TREE_TYPE (*parm);
16004 /* Nothing else to do in this case. */
16005 return 0;
16006
16007 default:
16008 gcc_unreachable ();
16009 }
16010
16011 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16012 {
16013 /* [temp.deduct.call]
16014
16015 If P is not a reference type:
16016
16017 --If A is an array type, the pointer type produced by the
16018 array-to-pointer standard conversion (_conv.array_) is
16019 used in place of A for type deduction; otherwise,
16020
16021 --If A is a function type, the pointer type produced by
16022 the function-to-pointer standard conversion
16023 (_conv.func_) is used in place of A for type deduction;
16024 otherwise,
16025
16026 --If A is a cv-qualified type, the top level
16027 cv-qualifiers of A's type are ignored for type
16028 deduction. */
16029 if (TREE_CODE (*arg) == ARRAY_TYPE)
16030 *arg = build_pointer_type (TREE_TYPE (*arg));
16031 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16032 *arg = build_pointer_type (*arg);
16033 else
16034 *arg = TYPE_MAIN_VARIANT (*arg);
16035 }
16036
16037 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16038 of the form T&&, where T is a template parameter, and the argument
16039 is an lvalue, T is deduced as A& */
16040 if (TREE_CODE (*parm) == REFERENCE_TYPE
16041 && TYPE_REF_IS_RVALUE (*parm)
16042 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16043 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16044 && (arg_expr ? real_lvalue_p (arg_expr)
16045 /* try_one_overload doesn't provide an arg_expr, but
16046 functions are always lvalues. */
16047 : TREE_CODE (*arg) == FUNCTION_TYPE))
16048 *arg = build_reference_type (*arg);
16049
16050 /* [temp.deduct.call]
16051
16052 If P is a cv-qualified type, the top level cv-qualifiers
16053 of P's type are ignored for type deduction. If P is a
16054 reference type, the type referred to by P is used for
16055 type deduction. */
16056 *parm = TYPE_MAIN_VARIANT (*parm);
16057 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16058 {
16059 *parm = TREE_TYPE (*parm);
16060 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16061 }
16062
16063 /* DR 322. For conversion deduction, remove a reference type on parm
16064 too (which has been swapped into ARG). */
16065 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16066 *arg = TREE_TYPE (*arg);
16067
16068 return result;
16069 }
16070
16071 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16072 template which does contain any deducible template parameters; check if
16073 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16074 unify_one_argument. */
16075
16076 static int
16077 check_non_deducible_conversion (tree parm, tree arg, int strict,
16078 int flags, bool explain_p)
16079 {
16080 tree type;
16081
16082 if (!TYPE_P (arg))
16083 type = TREE_TYPE (arg);
16084 else
16085 type = arg;
16086
16087 if (same_type_p (parm, type))
16088 return unify_success (explain_p);
16089
16090 if (strict == DEDUCE_CONV)
16091 {
16092 if (can_convert_arg (type, parm, NULL_TREE, flags,
16093 explain_p ? tf_warning_or_error : tf_none))
16094 return unify_success (explain_p);
16095 }
16096 else if (strict != DEDUCE_EXACT)
16097 {
16098 if (can_convert_arg (parm, type,
16099 TYPE_P (arg) ? NULL_TREE : arg,
16100 flags, explain_p ? tf_warning_or_error : tf_none))
16101 return unify_success (explain_p);
16102 }
16103
16104 if (strict == DEDUCE_EXACT)
16105 return unify_type_mismatch (explain_p, parm, arg);
16106 else
16107 return unify_arg_conversion (explain_p, parm, type, arg);
16108 }
16109
16110 static bool uses_deducible_template_parms (tree type);
16111
16112 /* Returns true iff the expression EXPR is one from which a template
16113 argument can be deduced. In other words, if it's an undecorated
16114 use of a template non-type parameter. */
16115
16116 static bool
16117 deducible_expression (tree expr)
16118 {
16119 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16120 }
16121
16122 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16123 deducible way; that is, if it has a max value of <PARM> - 1. */
16124
16125 static bool
16126 deducible_array_bound (tree domain)
16127 {
16128 if (domain == NULL_TREE)
16129 return false;
16130
16131 tree max = TYPE_MAX_VALUE (domain);
16132 if (TREE_CODE (max) != MINUS_EXPR)
16133 return false;
16134
16135 return deducible_expression (TREE_OPERAND (max, 0));
16136 }
16137
16138 /* Returns true iff the template arguments ARGS use a template parameter
16139 in a deducible way. */
16140
16141 static bool
16142 deducible_template_args (tree args)
16143 {
16144 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16145 {
16146 bool deducible;
16147 tree elt = TREE_VEC_ELT (args, i);
16148 if (ARGUMENT_PACK_P (elt))
16149 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16150 else
16151 {
16152 if (PACK_EXPANSION_P (elt))
16153 elt = PACK_EXPANSION_PATTERN (elt);
16154 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16155 deducible = true;
16156 else if (TYPE_P (elt))
16157 deducible = uses_deducible_template_parms (elt);
16158 else
16159 deducible = deducible_expression (elt);
16160 }
16161 if (deducible)
16162 return true;
16163 }
16164 return false;
16165 }
16166
16167 /* Returns true iff TYPE contains any deducible references to template
16168 parameters, as per 14.8.2.5. */
16169
16170 static bool
16171 uses_deducible_template_parms (tree type)
16172 {
16173 if (PACK_EXPANSION_P (type))
16174 type = PACK_EXPANSION_PATTERN (type);
16175
16176 /* T
16177 cv-list T
16178 TT<T>
16179 TT<i>
16180 TT<> */
16181 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16182 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16183 return true;
16184
16185 /* T*
16186 T&
16187 T&& */
16188 if (POINTER_TYPE_P (type))
16189 return uses_deducible_template_parms (TREE_TYPE (type));
16190
16191 /* T[integer-constant ]
16192 type [i] */
16193 if (TREE_CODE (type) == ARRAY_TYPE)
16194 return (uses_deducible_template_parms (TREE_TYPE (type))
16195 || deducible_array_bound (TYPE_DOMAIN (type)));
16196
16197 /* T type ::*
16198 type T::*
16199 T T::*
16200 T (type ::*)()
16201 type (T::*)()
16202 type (type ::*)(T)
16203 type (T::*)(T)
16204 T (type ::*)(T)
16205 T (T::*)()
16206 T (T::*)(T) */
16207 if (TYPE_PTRMEM_P (type))
16208 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16209 || (uses_deducible_template_parms
16210 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16211
16212 /* template-name <T> (where template-name refers to a class template)
16213 template-name <i> (where template-name refers to a class template) */
16214 if (CLASS_TYPE_P (type)
16215 && CLASSTYPE_TEMPLATE_INFO (type)
16216 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16217 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16218 (CLASSTYPE_TI_ARGS (type)));
16219
16220 /* type (T)
16221 T()
16222 T(T) */
16223 if (TREE_CODE (type) == FUNCTION_TYPE
16224 || TREE_CODE (type) == METHOD_TYPE)
16225 {
16226 if (uses_deducible_template_parms (TREE_TYPE (type)))
16227 return true;
16228 tree parm = TYPE_ARG_TYPES (type);
16229 if (TREE_CODE (type) == METHOD_TYPE)
16230 parm = TREE_CHAIN (parm);
16231 for (; parm; parm = TREE_CHAIN (parm))
16232 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16233 return true;
16234 }
16235
16236 return false;
16237 }
16238
16239 /* Subroutine of type_unification_real and unify_pack_expansion to
16240 handle unification of a single P/A pair. Parameters are as
16241 for those functions. */
16242
16243 static int
16244 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16245 int subr, unification_kind_t strict, int flags,
16246 bool explain_p)
16247 {
16248 tree arg_expr = NULL_TREE;
16249 int arg_strict;
16250
16251 if (arg == error_mark_node || parm == error_mark_node)
16252 return unify_invalid (explain_p);
16253 if (arg == unknown_type_node)
16254 /* We can't deduce anything from this, but we might get all the
16255 template args from other function args. */
16256 return unify_success (explain_p);
16257
16258 /* Implicit conversions (Clause 4) will be performed on a function
16259 argument to convert it to the type of the corresponding function
16260 parameter if the parameter type contains no template-parameters that
16261 participate in template argument deduction. */
16262 if (TYPE_P (parm) && !uses_template_parms (parm))
16263 /* For function parameters that contain no template-parameters at all,
16264 we have historically checked for convertibility in order to shortcut
16265 consideration of this candidate. */
16266 return check_non_deducible_conversion (parm, arg, strict, flags,
16267 explain_p);
16268 else if (strict == DEDUCE_CALL
16269 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16270 /* For function parameters with only non-deducible template parameters,
16271 just return. */
16272 return unify_success (explain_p);
16273
16274 switch (strict)
16275 {
16276 case DEDUCE_CALL:
16277 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16278 | UNIFY_ALLOW_MORE_CV_QUAL
16279 | UNIFY_ALLOW_DERIVED);
16280 break;
16281
16282 case DEDUCE_CONV:
16283 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16284 break;
16285
16286 case DEDUCE_EXACT:
16287 arg_strict = UNIFY_ALLOW_NONE;
16288 break;
16289
16290 default:
16291 gcc_unreachable ();
16292 }
16293
16294 /* We only do these transformations if this is the top-level
16295 parameter_type_list in a call or declaration matching; in other
16296 situations (nested function declarators, template argument lists) we
16297 won't be comparing a type to an expression, and we don't do any type
16298 adjustments. */
16299 if (!subr)
16300 {
16301 if (!TYPE_P (arg))
16302 {
16303 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16304 if (type_unknown_p (arg))
16305 {
16306 /* [temp.deduct.type] A template-argument can be
16307 deduced from a pointer to function or pointer
16308 to member function argument if the set of
16309 overloaded functions does not contain function
16310 templates and at most one of a set of
16311 overloaded functions provides a unique
16312 match. */
16313
16314 if (resolve_overloaded_unification
16315 (tparms, targs, parm, arg, strict,
16316 arg_strict, explain_p))
16317 return unify_success (explain_p);
16318 return unify_overload_resolution_failure (explain_p, arg);
16319 }
16320
16321 arg_expr = arg;
16322 arg = unlowered_expr_type (arg);
16323 if (arg == error_mark_node)
16324 return unify_invalid (explain_p);
16325 }
16326
16327 arg_strict |=
16328 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16329 }
16330 else
16331 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16332 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16333
16334 /* For deduction from an init-list we need the actual list. */
16335 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16336 arg = arg_expr;
16337 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16338 }
16339
16340 /* Most parms like fn_type_unification.
16341
16342 If SUBR is 1, we're being called recursively (to unify the
16343 arguments of a function or method parameter of a function
16344 template).
16345
16346 CHECKS is a pointer to a vector of access checks encountered while
16347 substituting default template arguments. */
16348
16349 static int
16350 type_unification_real (tree tparms,
16351 tree targs,
16352 tree xparms,
16353 const tree *xargs,
16354 unsigned int xnargs,
16355 int subr,
16356 unification_kind_t strict,
16357 int flags,
16358 vec<deferred_access_check, va_gc> **checks,
16359 bool explain_p)
16360 {
16361 tree parm, arg;
16362 int i;
16363 int ntparms = TREE_VEC_LENGTH (tparms);
16364 int saw_undeduced = 0;
16365 tree parms;
16366 const tree *args;
16367 unsigned int nargs;
16368 unsigned int ia;
16369
16370 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16371 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16372 gcc_assert (ntparms > 0);
16373
16374 /* Reset the number of non-defaulted template arguments contained
16375 in TARGS. */
16376 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16377
16378 again:
16379 parms = xparms;
16380 args = xargs;
16381 nargs = xnargs;
16382
16383 ia = 0;
16384 while (parms && parms != void_list_node
16385 && ia < nargs)
16386 {
16387 parm = TREE_VALUE (parms);
16388
16389 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16390 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16391 /* For a function parameter pack that occurs at the end of the
16392 parameter-declaration-list, the type A of each remaining
16393 argument of the call is compared with the type P of the
16394 declarator-id of the function parameter pack. */
16395 break;
16396
16397 parms = TREE_CHAIN (parms);
16398
16399 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16400 /* For a function parameter pack that does not occur at the
16401 end of the parameter-declaration-list, the type of the
16402 parameter pack is a non-deduced context. */
16403 continue;
16404
16405 arg = args[ia];
16406 ++ia;
16407
16408 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16409 flags, explain_p))
16410 return 1;
16411 }
16412
16413 if (parms
16414 && parms != void_list_node
16415 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16416 {
16417 /* Unify the remaining arguments with the pack expansion type. */
16418 tree argvec;
16419 tree parmvec = make_tree_vec (1);
16420
16421 /* Allocate a TREE_VEC and copy in all of the arguments */
16422 argvec = make_tree_vec (nargs - ia);
16423 for (i = 0; ia < nargs; ++ia, ++i)
16424 TREE_VEC_ELT (argvec, i) = args[ia];
16425
16426 /* Copy the parameter into parmvec. */
16427 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16428 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16429 /*subr=*/subr, explain_p))
16430 return 1;
16431
16432 /* Advance to the end of the list of parameters. */
16433 parms = TREE_CHAIN (parms);
16434 }
16435
16436 /* Fail if we've reached the end of the parm list, and more args
16437 are present, and the parm list isn't variadic. */
16438 if (ia < nargs && parms == void_list_node)
16439 return unify_too_many_arguments (explain_p, nargs, ia);
16440 /* Fail if parms are left and they don't have default values. */
16441 if (parms && parms != void_list_node
16442 && TREE_PURPOSE (parms) == NULL_TREE)
16443 {
16444 unsigned int count = nargs;
16445 tree p = parms;
16446 while (p && p != void_list_node)
16447 {
16448 count++;
16449 p = TREE_CHAIN (p);
16450 }
16451 return unify_too_few_arguments (explain_p, ia, count);
16452 }
16453
16454 if (!subr)
16455 {
16456 tsubst_flags_t complain = (explain_p
16457 ? tf_warning_or_error
16458 : tf_none);
16459
16460 for (i = 0; i < ntparms; i++)
16461 {
16462 tree targ = TREE_VEC_ELT (targs, i);
16463 tree tparm = TREE_VEC_ELT (tparms, i);
16464
16465 /* Clear the "incomplete" flags on all argument packs now so that
16466 substituting them into later default arguments works. */
16467 if (targ && ARGUMENT_PACK_P (targ))
16468 {
16469 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16470 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16471 }
16472
16473 if (targ || tparm == error_mark_node)
16474 continue;
16475 tparm = TREE_VALUE (tparm);
16476
16477 /* If this is an undeduced nontype parameter that depends on
16478 a type parameter, try another pass; its type may have been
16479 deduced from a later argument than the one from which
16480 this parameter can be deduced. */
16481 if (TREE_CODE (tparm) == PARM_DECL
16482 && uses_template_parms (TREE_TYPE (tparm))
16483 && !saw_undeduced++)
16484 goto again;
16485
16486 /* Core issue #226 (C++0x) [temp.deduct]:
16487
16488 If a template argument has not been deduced, its
16489 default template argument, if any, is used.
16490
16491 When we are in C++98 mode, TREE_PURPOSE will either
16492 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16493 to explicitly check cxx_dialect here. */
16494 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16495 {
16496 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16497 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16498 reopen_deferring_access_checks (*checks);
16499 location_t save_loc = input_location;
16500 if (DECL_P (parm))
16501 input_location = DECL_SOURCE_LOCATION (parm);
16502 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16503 arg = convert_template_argument (parm, arg, targs, complain,
16504 i, NULL_TREE);
16505 input_location = save_loc;
16506 *checks = get_deferred_access_checks ();
16507 pop_deferring_access_checks ();
16508 if (arg == error_mark_node)
16509 return 1;
16510 else
16511 {
16512 TREE_VEC_ELT (targs, i) = arg;
16513 /* The position of the first default template argument,
16514 is also the number of non-defaulted arguments in TARGS.
16515 Record that. */
16516 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16517 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16518 continue;
16519 }
16520 }
16521
16522 /* If the type parameter is a parameter pack, then it will
16523 be deduced to an empty parameter pack. */
16524 if (template_parameter_pack_p (tparm))
16525 {
16526 tree arg;
16527
16528 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16529 {
16530 arg = make_node (NONTYPE_ARGUMENT_PACK);
16531 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16532 TREE_CONSTANT (arg) = 1;
16533 }
16534 else
16535 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16536
16537 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16538
16539 TREE_VEC_ELT (targs, i) = arg;
16540 continue;
16541 }
16542
16543 return unify_parameter_deduction_failure (explain_p, tparm);
16544 }
16545 }
16546 #ifdef ENABLE_CHECKING
16547 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16548 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16549 #endif
16550
16551 return unify_success (explain_p);
16552 }
16553
16554 /* Subroutine of type_unification_real. Args are like the variables
16555 at the call site. ARG is an overloaded function (or template-id);
16556 we try deducing template args from each of the overloads, and if
16557 only one succeeds, we go with that. Modifies TARGS and returns
16558 true on success. */
16559
16560 static bool
16561 resolve_overloaded_unification (tree tparms,
16562 tree targs,
16563 tree parm,
16564 tree arg,
16565 unification_kind_t strict,
16566 int sub_strict,
16567 bool explain_p)
16568 {
16569 tree tempargs = copy_node (targs);
16570 int good = 0;
16571 tree goodfn = NULL_TREE;
16572 bool addr_p;
16573
16574 if (TREE_CODE (arg) == ADDR_EXPR)
16575 {
16576 arg = TREE_OPERAND (arg, 0);
16577 addr_p = true;
16578 }
16579 else
16580 addr_p = false;
16581
16582 if (TREE_CODE (arg) == COMPONENT_REF)
16583 /* Handle `&x' where `x' is some static or non-static member
16584 function name. */
16585 arg = TREE_OPERAND (arg, 1);
16586
16587 if (TREE_CODE (arg) == OFFSET_REF)
16588 arg = TREE_OPERAND (arg, 1);
16589
16590 /* Strip baselink information. */
16591 if (BASELINK_P (arg))
16592 arg = BASELINK_FUNCTIONS (arg);
16593
16594 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16595 {
16596 /* If we got some explicit template args, we need to plug them into
16597 the affected templates before we try to unify, in case the
16598 explicit args will completely resolve the templates in question. */
16599
16600 int ok = 0;
16601 tree expl_subargs = TREE_OPERAND (arg, 1);
16602 arg = TREE_OPERAND (arg, 0);
16603
16604 for (; arg; arg = OVL_NEXT (arg))
16605 {
16606 tree fn = OVL_CURRENT (arg);
16607 tree subargs, elem;
16608
16609 if (TREE_CODE (fn) != TEMPLATE_DECL)
16610 continue;
16611
16612 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16613 expl_subargs, NULL_TREE, tf_none,
16614 /*require_all_args=*/true,
16615 /*use_default_args=*/true);
16616 if (subargs != error_mark_node
16617 && !any_dependent_template_arguments_p (subargs))
16618 {
16619 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16620 if (try_one_overload (tparms, targs, tempargs, parm,
16621 elem, strict, sub_strict, addr_p, explain_p)
16622 && (!goodfn || !same_type_p (goodfn, elem)))
16623 {
16624 goodfn = elem;
16625 ++good;
16626 }
16627 }
16628 else if (subargs)
16629 ++ok;
16630 }
16631 /* If no templates (or more than one) are fully resolved by the
16632 explicit arguments, this template-id is a non-deduced context; it
16633 could still be OK if we deduce all template arguments for the
16634 enclosing call through other arguments. */
16635 if (good != 1)
16636 good = ok;
16637 }
16638 else if (TREE_CODE (arg) != OVERLOAD
16639 && TREE_CODE (arg) != FUNCTION_DECL)
16640 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16641 -- but the deduction does not succeed because the expression is
16642 not just the function on its own. */
16643 return false;
16644 else
16645 for (; arg; arg = OVL_NEXT (arg))
16646 if (try_one_overload (tparms, targs, tempargs, parm,
16647 TREE_TYPE (OVL_CURRENT (arg)),
16648 strict, sub_strict, addr_p, explain_p)
16649 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16650 {
16651 goodfn = OVL_CURRENT (arg);
16652 ++good;
16653 }
16654
16655 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16656 to function or pointer to member function argument if the set of
16657 overloaded functions does not contain function templates and at most
16658 one of a set of overloaded functions provides a unique match.
16659
16660 So if we found multiple possibilities, we return success but don't
16661 deduce anything. */
16662
16663 if (good == 1)
16664 {
16665 int i = TREE_VEC_LENGTH (targs);
16666 for (; i--; )
16667 if (TREE_VEC_ELT (tempargs, i))
16668 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16669 }
16670 if (good)
16671 return true;
16672
16673 return false;
16674 }
16675
16676 /* Core DR 115: In contexts where deduction is done and fails, or in
16677 contexts where deduction is not done, if a template argument list is
16678 specified and it, along with any default template arguments, identifies
16679 a single function template specialization, then the template-id is an
16680 lvalue for the function template specialization. */
16681
16682 tree
16683 resolve_nondeduced_context (tree orig_expr)
16684 {
16685 tree expr, offset, baselink;
16686 bool addr;
16687
16688 if (!type_unknown_p (orig_expr))
16689 return orig_expr;
16690
16691 expr = orig_expr;
16692 addr = false;
16693 offset = NULL_TREE;
16694 baselink = NULL_TREE;
16695
16696 if (TREE_CODE (expr) == ADDR_EXPR)
16697 {
16698 expr = TREE_OPERAND (expr, 0);
16699 addr = true;
16700 }
16701 if (TREE_CODE (expr) == OFFSET_REF)
16702 {
16703 offset = expr;
16704 expr = TREE_OPERAND (expr, 1);
16705 }
16706 if (BASELINK_P (expr))
16707 {
16708 baselink = expr;
16709 expr = BASELINK_FUNCTIONS (expr);
16710 }
16711
16712 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16713 {
16714 int good = 0;
16715 tree goodfn = NULL_TREE;
16716
16717 /* If we got some explicit template args, we need to plug them into
16718 the affected templates before we try to unify, in case the
16719 explicit args will completely resolve the templates in question. */
16720
16721 tree expl_subargs = TREE_OPERAND (expr, 1);
16722 tree arg = TREE_OPERAND (expr, 0);
16723 tree badfn = NULL_TREE;
16724 tree badargs = NULL_TREE;
16725
16726 for (; arg; arg = OVL_NEXT (arg))
16727 {
16728 tree fn = OVL_CURRENT (arg);
16729 tree subargs, elem;
16730
16731 if (TREE_CODE (fn) != TEMPLATE_DECL)
16732 continue;
16733
16734 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16735 expl_subargs, NULL_TREE, tf_none,
16736 /*require_all_args=*/true,
16737 /*use_default_args=*/true);
16738 if (subargs != error_mark_node
16739 && !any_dependent_template_arguments_p (subargs))
16740 {
16741 elem = instantiate_template (fn, subargs, tf_none);
16742 if (elem == error_mark_node)
16743 {
16744 badfn = fn;
16745 badargs = subargs;
16746 }
16747 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16748 {
16749 goodfn = elem;
16750 ++good;
16751 }
16752 }
16753 }
16754 if (good == 1)
16755 {
16756 mark_used (goodfn);
16757 expr = goodfn;
16758 if (baselink)
16759 expr = build_baselink (BASELINK_BINFO (baselink),
16760 BASELINK_ACCESS_BINFO (baselink),
16761 expr, BASELINK_OPTYPE (baselink));
16762 if (offset)
16763 {
16764 tree base
16765 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16766 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16767 }
16768 if (addr)
16769 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16770 return expr;
16771 }
16772 else if (good == 0 && badargs)
16773 /* There were no good options and at least one bad one, so let the
16774 user know what the problem is. */
16775 instantiate_template (badfn, badargs, tf_warning_or_error);
16776 }
16777 return orig_expr;
16778 }
16779
16780 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16781 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16782 different overloads deduce different arguments for a given parm.
16783 ADDR_P is true if the expression for which deduction is being
16784 performed was of the form "& fn" rather than simply "fn".
16785
16786 Returns 1 on success. */
16787
16788 static int
16789 try_one_overload (tree tparms,
16790 tree orig_targs,
16791 tree targs,
16792 tree parm,
16793 tree arg,
16794 unification_kind_t strict,
16795 int sub_strict,
16796 bool addr_p,
16797 bool explain_p)
16798 {
16799 int nargs;
16800 tree tempargs;
16801 int i;
16802
16803 if (arg == error_mark_node)
16804 return 0;
16805
16806 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16807 to function or pointer to member function argument if the set of
16808 overloaded functions does not contain function templates and at most
16809 one of a set of overloaded functions provides a unique match.
16810
16811 So if this is a template, just return success. */
16812
16813 if (uses_template_parms (arg))
16814 return 1;
16815
16816 if (TREE_CODE (arg) == METHOD_TYPE)
16817 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16818 else if (addr_p)
16819 arg = build_pointer_type (arg);
16820
16821 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16822
16823 /* We don't copy orig_targs for this because if we have already deduced
16824 some template args from previous args, unify would complain when we
16825 try to deduce a template parameter for the same argument, even though
16826 there isn't really a conflict. */
16827 nargs = TREE_VEC_LENGTH (targs);
16828 tempargs = make_tree_vec (nargs);
16829
16830 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16831 return 0;
16832
16833 /* First make sure we didn't deduce anything that conflicts with
16834 explicitly specified args. */
16835 for (i = nargs; i--; )
16836 {
16837 tree elt = TREE_VEC_ELT (tempargs, i);
16838 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16839
16840 if (!elt)
16841 /*NOP*/;
16842 else if (uses_template_parms (elt))
16843 /* Since we're unifying against ourselves, we will fill in
16844 template args used in the function parm list with our own
16845 template parms. Discard them. */
16846 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16847 else if (oldelt && !template_args_equal (oldelt, elt))
16848 return 0;
16849 }
16850
16851 for (i = nargs; i--; )
16852 {
16853 tree elt = TREE_VEC_ELT (tempargs, i);
16854
16855 if (elt)
16856 TREE_VEC_ELT (targs, i) = elt;
16857 }
16858
16859 return 1;
16860 }
16861
16862 /* PARM is a template class (perhaps with unbound template
16863 parameters). ARG is a fully instantiated type. If ARG can be
16864 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16865 TARGS are as for unify. */
16866
16867 static tree
16868 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16869 bool explain_p)
16870 {
16871 tree copy_of_targs;
16872
16873 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16874 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16875 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16876 return NULL_TREE;
16877
16878 /* We need to make a new template argument vector for the call to
16879 unify. If we used TARGS, we'd clutter it up with the result of
16880 the attempted unification, even if this class didn't work out.
16881 We also don't want to commit ourselves to all the unifications
16882 we've already done, since unification is supposed to be done on
16883 an argument-by-argument basis. In other words, consider the
16884 following pathological case:
16885
16886 template <int I, int J, int K>
16887 struct S {};
16888
16889 template <int I, int J>
16890 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16891
16892 template <int I, int J, int K>
16893 void f(S<I, J, K>, S<I, I, I>);
16894
16895 void g() {
16896 S<0, 0, 0> s0;
16897 S<0, 1, 2> s2;
16898
16899 f(s0, s2);
16900 }
16901
16902 Now, by the time we consider the unification involving `s2', we
16903 already know that we must have `f<0, 0, 0>'. But, even though
16904 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16905 because there are two ways to unify base classes of S<0, 1, 2>
16906 with S<I, I, I>. If we kept the already deduced knowledge, we
16907 would reject the possibility I=1. */
16908 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16909
16910 /* If unification failed, we're done. */
16911 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16912 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16913 return NULL_TREE;
16914
16915 return arg;
16916 }
16917
16918 /* Given a template type PARM and a class type ARG, find the unique
16919 base type in ARG that is an instance of PARM. We do not examine
16920 ARG itself; only its base-classes. If there is not exactly one
16921 appropriate base class, return NULL_TREE. PARM may be the type of
16922 a partial specialization, as well as a plain template type. Used
16923 by unify. */
16924
16925 static enum template_base_result
16926 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16927 bool explain_p, tree *result)
16928 {
16929 tree rval = NULL_TREE;
16930 tree binfo;
16931
16932 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16933
16934 binfo = TYPE_BINFO (complete_type (arg));
16935 if (!binfo)
16936 {
16937 /* The type could not be completed. */
16938 *result = NULL_TREE;
16939 return tbr_incomplete_type;
16940 }
16941
16942 /* Walk in inheritance graph order. The search order is not
16943 important, and this avoids multiple walks of virtual bases. */
16944 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16945 {
16946 tree r = try_class_unification (tparms, targs, parm,
16947 BINFO_TYPE (binfo), explain_p);
16948
16949 if (r)
16950 {
16951 /* If there is more than one satisfactory baseclass, then:
16952
16953 [temp.deduct.call]
16954
16955 If they yield more than one possible deduced A, the type
16956 deduction fails.
16957
16958 applies. */
16959 if (rval && !same_type_p (r, rval))
16960 {
16961 *result = NULL_TREE;
16962 return tbr_ambiguous_baseclass;
16963 }
16964
16965 rval = r;
16966 }
16967 }
16968
16969 *result = rval;
16970 return tbr_success;
16971 }
16972
16973 /* Returns the level of DECL, which declares a template parameter. */
16974
16975 static int
16976 template_decl_level (tree decl)
16977 {
16978 switch (TREE_CODE (decl))
16979 {
16980 case TYPE_DECL:
16981 case TEMPLATE_DECL:
16982 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16983
16984 case PARM_DECL:
16985 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16986
16987 default:
16988 gcc_unreachable ();
16989 }
16990 return 0;
16991 }
16992
16993 /* Decide whether ARG can be unified with PARM, considering only the
16994 cv-qualifiers of each type, given STRICT as documented for unify.
16995 Returns nonzero iff the unification is OK on that basis. */
16996
16997 static int
16998 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16999 {
17000 int arg_quals = cp_type_quals (arg);
17001 int parm_quals = cp_type_quals (parm);
17002
17003 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17004 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17005 {
17006 /* Although a CVR qualifier is ignored when being applied to a
17007 substituted template parameter ([8.3.2]/1 for example), that
17008 does not allow us to unify "const T" with "int&" because both
17009 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17010 It is ok when we're allowing additional CV qualifiers
17011 at the outer level [14.8.2.1]/3,1st bullet. */
17012 if ((TREE_CODE (arg) == REFERENCE_TYPE
17013 || TREE_CODE (arg) == FUNCTION_TYPE
17014 || TREE_CODE (arg) == METHOD_TYPE)
17015 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17016 return 0;
17017
17018 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17019 && (parm_quals & TYPE_QUAL_RESTRICT))
17020 return 0;
17021 }
17022
17023 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17024 && (arg_quals & parm_quals) != parm_quals)
17025 return 0;
17026
17027 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17028 && (parm_quals & arg_quals) != arg_quals)
17029 return 0;
17030
17031 return 1;
17032 }
17033
17034 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17035 void
17036 template_parm_level_and_index (tree parm, int* level, int* index)
17037 {
17038 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17039 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17040 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17041 {
17042 *index = TEMPLATE_TYPE_IDX (parm);
17043 *level = TEMPLATE_TYPE_LEVEL (parm);
17044 }
17045 else
17046 {
17047 *index = TEMPLATE_PARM_IDX (parm);
17048 *level = TEMPLATE_PARM_LEVEL (parm);
17049 }
17050 }
17051
17052 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17053 do { \
17054 if (unify (TP, TA, P, A, S, EP)) \
17055 return 1; \
17056 } while (0);
17057
17058 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17059 expansion at the end of PACKED_PARMS. Returns 0 if the type
17060 deduction succeeds, 1 otherwise. STRICT is the same as in
17061 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17062 call argument list. We'll need to adjust the arguments to make them
17063 types. SUBR tells us if this is from a recursive call to
17064 type_unification_real, or for comparing two template argument
17065 lists. */
17066
17067 static int
17068 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17069 tree packed_args, unification_kind_t strict,
17070 bool subr, bool explain_p)
17071 {
17072 tree parm
17073 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17074 tree pattern = PACK_EXPANSION_PATTERN (parm);
17075 tree pack, packs = NULL_TREE;
17076 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17077
17078 packed_args = expand_template_argument_pack (packed_args);
17079
17080 int len = TREE_VEC_LENGTH (packed_args);
17081
17082 /* Determine the parameter packs we will be deducing from the
17083 pattern, and record their current deductions. */
17084 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17085 pack; pack = TREE_CHAIN (pack))
17086 {
17087 tree parm_pack = TREE_VALUE (pack);
17088 int idx, level;
17089
17090 /* Determine the index and level of this parameter pack. */
17091 template_parm_level_and_index (parm_pack, &level, &idx);
17092
17093 /* Keep track of the parameter packs and their corresponding
17094 argument packs. */
17095 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17096 TREE_TYPE (packs) = make_tree_vec (len - start);
17097 }
17098
17099 /* Loop through all of the arguments that have not yet been
17100 unified and unify each with the pattern. */
17101 for (i = start; i < len; i++)
17102 {
17103 tree parm;
17104 bool any_explicit = false;
17105 tree arg = TREE_VEC_ELT (packed_args, i);
17106
17107 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17108 or the element of its argument pack at the current index if
17109 this argument was explicitly specified. */
17110 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17111 {
17112 int idx, level;
17113 tree arg, pargs;
17114 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17115
17116 arg = NULL_TREE;
17117 if (TREE_VALUE (pack)
17118 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17119 && (i - start < TREE_VEC_LENGTH (pargs)))
17120 {
17121 any_explicit = true;
17122 arg = TREE_VEC_ELT (pargs, i - start);
17123 }
17124 TMPL_ARG (targs, level, idx) = arg;
17125 }
17126
17127 /* If we had explicit template arguments, substitute them into the
17128 pattern before deduction. */
17129 if (any_explicit)
17130 {
17131 /* Some arguments might still be unspecified or dependent. */
17132 bool dependent;
17133 ++processing_template_decl;
17134 dependent = any_dependent_template_arguments_p (targs);
17135 if (!dependent)
17136 --processing_template_decl;
17137 parm = tsubst (pattern, targs,
17138 explain_p ? tf_warning_or_error : tf_none,
17139 NULL_TREE);
17140 if (dependent)
17141 --processing_template_decl;
17142 if (parm == error_mark_node)
17143 return 1;
17144 }
17145 else
17146 parm = pattern;
17147
17148 /* Unify the pattern with the current argument. */
17149 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17150 LOOKUP_IMPLICIT, explain_p))
17151 return 1;
17152
17153 /* For each parameter pack, collect the deduced value. */
17154 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17155 {
17156 int idx, level;
17157 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17158
17159 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17160 TMPL_ARG (targs, level, idx);
17161 }
17162 }
17163
17164 /* Verify that the results of unification with the parameter packs
17165 produce results consistent with what we've seen before, and make
17166 the deduced argument packs available. */
17167 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17168 {
17169 tree old_pack = TREE_VALUE (pack);
17170 tree new_args = TREE_TYPE (pack);
17171 int i, len = TREE_VEC_LENGTH (new_args);
17172 int idx, level;
17173 bool nondeduced_p = false;
17174
17175 /* By default keep the original deduced argument pack.
17176 If necessary, more specific code is going to update the
17177 resulting deduced argument later down in this function. */
17178 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17179 TMPL_ARG (targs, level, idx) = old_pack;
17180
17181 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17182 actually deduce anything. */
17183 for (i = 0; i < len && !nondeduced_p; ++i)
17184 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17185 nondeduced_p = true;
17186 if (nondeduced_p)
17187 continue;
17188
17189 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17190 {
17191 /* If we had fewer function args than explicit template args,
17192 just use the explicits. */
17193 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17194 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17195 if (len < explicit_len)
17196 new_args = explicit_args;
17197 }
17198
17199 if (!old_pack)
17200 {
17201 tree result;
17202 /* Build the deduced *_ARGUMENT_PACK. */
17203 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17204 {
17205 result = make_node (NONTYPE_ARGUMENT_PACK);
17206 TREE_TYPE (result) =
17207 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17208 TREE_CONSTANT (result) = 1;
17209 }
17210 else
17211 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17212
17213 SET_ARGUMENT_PACK_ARGS (result, new_args);
17214
17215 /* Note the deduced argument packs for this parameter
17216 pack. */
17217 TMPL_ARG (targs, level, idx) = result;
17218 }
17219 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17220 && (ARGUMENT_PACK_ARGS (old_pack)
17221 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17222 {
17223 /* We only had the explicitly-provided arguments before, but
17224 now we have a complete set of arguments. */
17225 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17226
17227 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17228 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17229 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17230 }
17231 else
17232 {
17233 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17234 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17235
17236 if (!comp_template_args_with_info (old_args, new_args,
17237 &bad_old_arg, &bad_new_arg))
17238 /* Inconsistent unification of this parameter pack. */
17239 return unify_parameter_pack_inconsistent (explain_p,
17240 bad_old_arg,
17241 bad_new_arg);
17242 }
17243 }
17244
17245 return unify_success (explain_p);
17246 }
17247
17248 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17249 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17250 parameters and return value are as for unify. */
17251
17252 static int
17253 unify_array_domain (tree tparms, tree targs,
17254 tree parm_dom, tree arg_dom,
17255 bool explain_p)
17256 {
17257 tree parm_max;
17258 tree arg_max;
17259 bool parm_cst;
17260 bool arg_cst;
17261
17262 /* Our representation of array types uses "N - 1" as the
17263 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17264 not an integer constant. We cannot unify arbitrarily
17265 complex expressions, so we eliminate the MINUS_EXPRs
17266 here. */
17267 parm_max = TYPE_MAX_VALUE (parm_dom);
17268 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17269 if (!parm_cst)
17270 {
17271 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17272 parm_max = TREE_OPERAND (parm_max, 0);
17273 }
17274 arg_max = TYPE_MAX_VALUE (arg_dom);
17275 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17276 if (!arg_cst)
17277 {
17278 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17279 trying to unify the type of a variable with the type
17280 of a template parameter. For example:
17281
17282 template <unsigned int N>
17283 void f (char (&) [N]);
17284 int g();
17285 void h(int i) {
17286 char a[g(i)];
17287 f(a);
17288 }
17289
17290 Here, the type of the ARG will be "int [g(i)]", and
17291 may be a SAVE_EXPR, etc. */
17292 if (TREE_CODE (arg_max) != MINUS_EXPR)
17293 return unify_vla_arg (explain_p, arg_dom);
17294 arg_max = TREE_OPERAND (arg_max, 0);
17295 }
17296
17297 /* If only one of the bounds used a MINUS_EXPR, compensate
17298 by adding one to the other bound. */
17299 if (parm_cst && !arg_cst)
17300 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17301 integer_type_node,
17302 parm_max,
17303 integer_one_node);
17304 else if (arg_cst && !parm_cst)
17305 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17306 integer_type_node,
17307 arg_max,
17308 integer_one_node);
17309
17310 return unify (tparms, targs, parm_max, arg_max,
17311 UNIFY_ALLOW_INTEGER, explain_p);
17312 }
17313
17314 /* Deduce the value of template parameters. TPARMS is the (innermost)
17315 set of template parameters to a template. TARGS is the bindings
17316 for those template parameters, as determined thus far; TARGS may
17317 include template arguments for outer levels of template parameters
17318 as well. PARM is a parameter to a template function, or a
17319 subcomponent of that parameter; ARG is the corresponding argument.
17320 This function attempts to match PARM with ARG in a manner
17321 consistent with the existing assignments in TARGS. If more values
17322 are deduced, then TARGS is updated.
17323
17324 Returns 0 if the type deduction succeeds, 1 otherwise. The
17325 parameter STRICT is a bitwise or of the following flags:
17326
17327 UNIFY_ALLOW_NONE:
17328 Require an exact match between PARM and ARG.
17329 UNIFY_ALLOW_MORE_CV_QUAL:
17330 Allow the deduced ARG to be more cv-qualified (by qualification
17331 conversion) than ARG.
17332 UNIFY_ALLOW_LESS_CV_QUAL:
17333 Allow the deduced ARG to be less cv-qualified than ARG.
17334 UNIFY_ALLOW_DERIVED:
17335 Allow the deduced ARG to be a template base class of ARG,
17336 or a pointer to a template base class of the type pointed to by
17337 ARG.
17338 UNIFY_ALLOW_INTEGER:
17339 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17340 case for more information.
17341 UNIFY_ALLOW_OUTER_LEVEL:
17342 This is the outermost level of a deduction. Used to determine validity
17343 of qualification conversions. A valid qualification conversion must
17344 have const qualified pointers leading up to the inner type which
17345 requires additional CV quals, except at the outer level, where const
17346 is not required [conv.qual]. It would be normal to set this flag in
17347 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17348 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17349 This is the outermost level of a deduction, and PARM can be more CV
17350 qualified at this point.
17351 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17352 This is the outermost level of a deduction, and PARM can be less CV
17353 qualified at this point. */
17354
17355 static int
17356 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17357 bool explain_p)
17358 {
17359 int idx;
17360 tree targ;
17361 tree tparm;
17362 int strict_in = strict;
17363
17364 /* I don't think this will do the right thing with respect to types.
17365 But the only case I've seen it in so far has been array bounds, where
17366 signedness is the only information lost, and I think that will be
17367 okay. */
17368 while (TREE_CODE (parm) == NOP_EXPR)
17369 parm = TREE_OPERAND (parm, 0);
17370
17371 if (arg == error_mark_node)
17372 return unify_invalid (explain_p);
17373 if (arg == unknown_type_node
17374 || arg == init_list_type_node)
17375 /* We can't deduce anything from this, but we might get all the
17376 template args from other function args. */
17377 return unify_success (explain_p);
17378
17379 /* If PARM uses template parameters, then we can't bail out here,
17380 even if ARG == PARM, since we won't record unifications for the
17381 template parameters. We might need them if we're trying to
17382 figure out which of two things is more specialized. */
17383 if (arg == parm && !uses_template_parms (parm))
17384 return unify_success (explain_p);
17385
17386 /* Handle init lists early, so the rest of the function can assume
17387 we're dealing with a type. */
17388 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17389 {
17390 tree elt, elttype;
17391 unsigned i;
17392 tree orig_parm = parm;
17393
17394 /* Replace T with std::initializer_list<T> for deduction. */
17395 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17396 && flag_deduce_init_list)
17397 parm = listify (parm);
17398
17399 if (!is_std_init_list (parm)
17400 && TREE_CODE (parm) != ARRAY_TYPE)
17401 /* We can only deduce from an initializer list argument if the
17402 parameter is std::initializer_list or an array; otherwise this
17403 is a non-deduced context. */
17404 return unify_success (explain_p);
17405
17406 if (TREE_CODE (parm) == ARRAY_TYPE)
17407 elttype = TREE_TYPE (parm);
17408 else
17409 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17410
17411 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17412 {
17413 int elt_strict = strict;
17414
17415 if (elt == error_mark_node)
17416 return unify_invalid (explain_p);
17417
17418 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17419 {
17420 tree type = TREE_TYPE (elt);
17421 /* It should only be possible to get here for a call. */
17422 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17423 elt_strict |= maybe_adjust_types_for_deduction
17424 (DEDUCE_CALL, &elttype, &type, elt);
17425 elt = type;
17426 }
17427
17428 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17429 explain_p);
17430 }
17431
17432 if (TREE_CODE (parm) == ARRAY_TYPE
17433 && deducible_array_bound (TYPE_DOMAIN (parm)))
17434 {
17435 /* Also deduce from the length of the initializer list. */
17436 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17437 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17438 if (idx == error_mark_node)
17439 return unify_invalid (explain_p);
17440 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17441 idx, explain_p);
17442 }
17443
17444 /* If the std::initializer_list<T> deduction worked, replace the
17445 deduced A with std::initializer_list<A>. */
17446 if (orig_parm != parm)
17447 {
17448 idx = TEMPLATE_TYPE_IDX (orig_parm);
17449 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17450 targ = listify (targ);
17451 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17452 }
17453 return unify_success (explain_p);
17454 }
17455
17456 /* Immediately reject some pairs that won't unify because of
17457 cv-qualification mismatches. */
17458 if (TREE_CODE (arg) == TREE_CODE (parm)
17459 && TYPE_P (arg)
17460 /* It is the elements of the array which hold the cv quals of an array
17461 type, and the elements might be template type parms. We'll check
17462 when we recurse. */
17463 && TREE_CODE (arg) != ARRAY_TYPE
17464 /* We check the cv-qualifiers when unifying with template type
17465 parameters below. We want to allow ARG `const T' to unify with
17466 PARM `T' for example, when computing which of two templates
17467 is more specialized, for example. */
17468 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17469 && !check_cv_quals_for_unify (strict_in, arg, parm))
17470 return unify_cv_qual_mismatch (explain_p, parm, arg);
17471
17472 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17473 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17474 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17475 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17476 strict &= ~UNIFY_ALLOW_DERIVED;
17477 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17478 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17479
17480 switch (TREE_CODE (parm))
17481 {
17482 case TYPENAME_TYPE:
17483 case SCOPE_REF:
17484 case UNBOUND_CLASS_TEMPLATE:
17485 /* In a type which contains a nested-name-specifier, template
17486 argument values cannot be deduced for template parameters used
17487 within the nested-name-specifier. */
17488 return unify_success (explain_p);
17489
17490 case TEMPLATE_TYPE_PARM:
17491 case TEMPLATE_TEMPLATE_PARM:
17492 case BOUND_TEMPLATE_TEMPLATE_PARM:
17493 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17494 if (error_operand_p (tparm))
17495 return unify_invalid (explain_p);
17496
17497 if (TEMPLATE_TYPE_LEVEL (parm)
17498 != template_decl_level (tparm))
17499 /* The PARM is not one we're trying to unify. Just check
17500 to see if it matches ARG. */
17501 {
17502 if (TREE_CODE (arg) == TREE_CODE (parm)
17503 && (is_auto (parm) ? is_auto (arg)
17504 : same_type_p (parm, arg)))
17505 return unify_success (explain_p);
17506 else
17507 return unify_type_mismatch (explain_p, parm, arg);
17508 }
17509 idx = TEMPLATE_TYPE_IDX (parm);
17510 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17511 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17512 if (error_operand_p (tparm))
17513 return unify_invalid (explain_p);
17514
17515 /* Check for mixed types and values. */
17516 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17517 && TREE_CODE (tparm) != TYPE_DECL)
17518 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17519 && TREE_CODE (tparm) != TEMPLATE_DECL))
17520 gcc_unreachable ();
17521
17522 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17523 {
17524 /* ARG must be constructed from a template class or a template
17525 template parameter. */
17526 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17527 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17528 return unify_template_deduction_failure (explain_p, parm, arg);
17529 {
17530 tree parmvec = TYPE_TI_ARGS (parm);
17531 /* An alias template name is never deduced. */
17532 if (TYPE_ALIAS_P (arg))
17533 arg = strip_typedefs (arg);
17534 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17535 tree full_argvec = add_to_template_args (targs, argvec);
17536 tree parm_parms
17537 = DECL_INNERMOST_TEMPLATE_PARMS
17538 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17539 int i, len;
17540 int parm_variadic_p = 0;
17541
17542 /* The resolution to DR150 makes clear that default
17543 arguments for an N-argument may not be used to bind T
17544 to a template template parameter with fewer than N
17545 parameters. It is not safe to permit the binding of
17546 default arguments as an extension, as that may change
17547 the meaning of a conforming program. Consider:
17548
17549 struct Dense { static const unsigned int dim = 1; };
17550
17551 template <template <typename> class View,
17552 typename Block>
17553 void operator+(float, View<Block> const&);
17554
17555 template <typename Block,
17556 unsigned int Dim = Block::dim>
17557 struct Lvalue_proxy { operator float() const; };
17558
17559 void
17560 test_1d (void) {
17561 Lvalue_proxy<Dense> p;
17562 float b;
17563 b + p;
17564 }
17565
17566 Here, if Lvalue_proxy is permitted to bind to View, then
17567 the global operator+ will be used; if they are not, the
17568 Lvalue_proxy will be converted to float. */
17569 if (coerce_template_parms (parm_parms,
17570 full_argvec,
17571 TYPE_TI_TEMPLATE (parm),
17572 (explain_p
17573 ? tf_warning_or_error
17574 : tf_none),
17575 /*require_all_args=*/true,
17576 /*use_default_args=*/false)
17577 == error_mark_node)
17578 return 1;
17579
17580 /* Deduce arguments T, i from TT<T> or TT<i>.
17581 We check each element of PARMVEC and ARGVEC individually
17582 rather than the whole TREE_VEC since they can have
17583 different number of elements. */
17584
17585 parmvec = expand_template_argument_pack (parmvec);
17586 argvec = expand_template_argument_pack (argvec);
17587
17588 len = TREE_VEC_LENGTH (parmvec);
17589
17590 /* Check if the parameters end in a pack, making them
17591 variadic. */
17592 if (len > 0
17593 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17594 parm_variadic_p = 1;
17595
17596 for (i = 0; i < len - parm_variadic_p; ++i)
17597 /* If the template argument list of P contains a pack
17598 expansion that is not the last template argument, the
17599 entire template argument list is a non-deduced
17600 context. */
17601 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17602 return unify_success (explain_p);
17603
17604 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17605 return unify_too_few_arguments (explain_p,
17606 TREE_VEC_LENGTH (argvec), len);
17607
17608 for (i = 0; i < len - parm_variadic_p; ++i)
17609 {
17610 RECUR_AND_CHECK_FAILURE (tparms, targs,
17611 TREE_VEC_ELT (parmvec, i),
17612 TREE_VEC_ELT (argvec, i),
17613 UNIFY_ALLOW_NONE, explain_p);
17614 }
17615
17616 if (parm_variadic_p
17617 && unify_pack_expansion (tparms, targs,
17618 parmvec, argvec,
17619 DEDUCE_EXACT,
17620 /*subr=*/true, explain_p))
17621 return 1;
17622 }
17623 arg = TYPE_TI_TEMPLATE (arg);
17624
17625 /* Fall through to deduce template name. */
17626 }
17627
17628 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17629 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17630 {
17631 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17632
17633 /* Simple cases: Value already set, does match or doesn't. */
17634 if (targ != NULL_TREE && template_args_equal (targ, arg))
17635 return unify_success (explain_p);
17636 else if (targ)
17637 return unify_inconsistency (explain_p, parm, targ, arg);
17638 }
17639 else
17640 {
17641 /* If PARM is `const T' and ARG is only `int', we don't have
17642 a match unless we are allowing additional qualification.
17643 If ARG is `const int' and PARM is just `T' that's OK;
17644 that binds `const int' to `T'. */
17645 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17646 arg, parm))
17647 return unify_cv_qual_mismatch (explain_p, parm, arg);
17648
17649 /* Consider the case where ARG is `const volatile int' and
17650 PARM is `const T'. Then, T should be `volatile int'. */
17651 arg = cp_build_qualified_type_real
17652 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17653 if (arg == error_mark_node)
17654 return unify_invalid (explain_p);
17655
17656 /* Simple cases: Value already set, does match or doesn't. */
17657 if (targ != NULL_TREE && same_type_p (targ, arg))
17658 return unify_success (explain_p);
17659 else if (targ)
17660 return unify_inconsistency (explain_p, parm, targ, arg);
17661
17662 /* Make sure that ARG is not a variable-sized array. (Note
17663 that were talking about variable-sized arrays (like
17664 `int[n]'), rather than arrays of unknown size (like
17665 `int[]').) We'll get very confused by such a type since
17666 the bound of the array is not constant, and therefore
17667 not mangleable. Besides, such types are not allowed in
17668 ISO C++, so we can do as we please here. We do allow
17669 them for 'auto' deduction, since that isn't ABI-exposed. */
17670 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17671 return unify_vla_arg (explain_p, arg);
17672
17673 /* Strip typedefs as in convert_template_argument. */
17674 arg = canonicalize_type_argument (arg, tf_none);
17675 }
17676
17677 /* If ARG is a parameter pack or an expansion, we cannot unify
17678 against it unless PARM is also a parameter pack. */
17679 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17680 && !template_parameter_pack_p (parm))
17681 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17682
17683 /* If the argument deduction results is a METHOD_TYPE,
17684 then there is a problem.
17685 METHOD_TYPE doesn't map to any real C++ type the result of
17686 the deduction can not be of that type. */
17687 if (TREE_CODE (arg) == METHOD_TYPE)
17688 return unify_method_type_error (explain_p, arg);
17689
17690 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17691 return unify_success (explain_p);
17692
17693 case TEMPLATE_PARM_INDEX:
17694 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17695 if (error_operand_p (tparm))
17696 return unify_invalid (explain_p);
17697
17698 if (TEMPLATE_PARM_LEVEL (parm)
17699 != template_decl_level (tparm))
17700 {
17701 /* The PARM is not one we're trying to unify. Just check
17702 to see if it matches ARG. */
17703 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17704 && cp_tree_equal (parm, arg));
17705 if (result)
17706 unify_expression_unequal (explain_p, parm, arg);
17707 return result;
17708 }
17709
17710 idx = TEMPLATE_PARM_IDX (parm);
17711 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17712
17713 if (targ)
17714 {
17715 int x = !cp_tree_equal (targ, arg);
17716 if (x)
17717 unify_inconsistency (explain_p, parm, targ, arg);
17718 return x;
17719 }
17720
17721 /* [temp.deduct.type] If, in the declaration of a function template
17722 with a non-type template-parameter, the non-type
17723 template-parameter is used in an expression in the function
17724 parameter-list and, if the corresponding template-argument is
17725 deduced, the template-argument type shall match the type of the
17726 template-parameter exactly, except that a template-argument
17727 deduced from an array bound may be of any integral type.
17728 The non-type parameter might use already deduced type parameters. */
17729 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17730 if (!TREE_TYPE (arg))
17731 /* Template-parameter dependent expression. Just accept it for now.
17732 It will later be processed in convert_template_argument. */
17733 ;
17734 else if (same_type_p (TREE_TYPE (arg), tparm))
17735 /* OK */;
17736 else if ((strict & UNIFY_ALLOW_INTEGER)
17737 && CP_INTEGRAL_TYPE_P (tparm))
17738 /* Convert the ARG to the type of PARM; the deduced non-type
17739 template argument must exactly match the types of the
17740 corresponding parameter. */
17741 arg = fold (build_nop (tparm, arg));
17742 else if (uses_template_parms (tparm))
17743 /* We haven't deduced the type of this parameter yet. Try again
17744 later. */
17745 return unify_success (explain_p);
17746 else
17747 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17748
17749 /* If ARG is a parameter pack or an expansion, we cannot unify
17750 against it unless PARM is also a parameter pack. */
17751 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17752 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17753 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17754
17755 arg = strip_typedefs_expr (arg);
17756 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17757 return unify_success (explain_p);
17758
17759 case PTRMEM_CST:
17760 {
17761 /* A pointer-to-member constant can be unified only with
17762 another constant. */
17763 if (TREE_CODE (arg) != PTRMEM_CST)
17764 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17765
17766 /* Just unify the class member. It would be useless (and possibly
17767 wrong, depending on the strict flags) to unify also
17768 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17769 arg refer to the same variable, even if through different
17770 classes. For instance:
17771
17772 struct A { int x; };
17773 struct B : A { };
17774
17775 Unification of &A::x and &B::x must succeed. */
17776 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17777 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17778 }
17779
17780 case POINTER_TYPE:
17781 {
17782 if (!TYPE_PTR_P (arg))
17783 return unify_type_mismatch (explain_p, parm, arg);
17784
17785 /* [temp.deduct.call]
17786
17787 A can be another pointer or pointer to member type that can
17788 be converted to the deduced A via a qualification
17789 conversion (_conv.qual_).
17790
17791 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17792 This will allow for additional cv-qualification of the
17793 pointed-to types if appropriate. */
17794
17795 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17796 /* The derived-to-base conversion only persists through one
17797 level of pointers. */
17798 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17799
17800 return unify (tparms, targs, TREE_TYPE (parm),
17801 TREE_TYPE (arg), strict, explain_p);
17802 }
17803
17804 case REFERENCE_TYPE:
17805 if (TREE_CODE (arg) != REFERENCE_TYPE)
17806 return unify_type_mismatch (explain_p, parm, arg);
17807 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17808 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17809
17810 case ARRAY_TYPE:
17811 if (TREE_CODE (arg) != ARRAY_TYPE)
17812 return unify_type_mismatch (explain_p, parm, arg);
17813 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17814 != (TYPE_DOMAIN (arg) == NULL_TREE))
17815 return unify_type_mismatch (explain_p, parm, arg);
17816 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17817 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17818 if (TYPE_DOMAIN (parm) != NULL_TREE)
17819 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17820 TYPE_DOMAIN (arg), explain_p);
17821 return unify_success (explain_p);
17822
17823 case REAL_TYPE:
17824 case COMPLEX_TYPE:
17825 case VECTOR_TYPE:
17826 case INTEGER_TYPE:
17827 case BOOLEAN_TYPE:
17828 case ENUMERAL_TYPE:
17829 case VOID_TYPE:
17830 case NULLPTR_TYPE:
17831 if (TREE_CODE (arg) != TREE_CODE (parm))
17832 return unify_type_mismatch (explain_p, parm, arg);
17833
17834 /* We have already checked cv-qualification at the top of the
17835 function. */
17836 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17837 return unify_type_mismatch (explain_p, parm, arg);
17838
17839 /* As far as unification is concerned, this wins. Later checks
17840 will invalidate it if necessary. */
17841 return unify_success (explain_p);
17842
17843 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17844 /* Type INTEGER_CST can come from ordinary constant template args. */
17845 case INTEGER_CST:
17846 while (TREE_CODE (arg) == NOP_EXPR)
17847 arg = TREE_OPERAND (arg, 0);
17848
17849 if (TREE_CODE (arg) != INTEGER_CST)
17850 return unify_template_argument_mismatch (explain_p, parm, arg);
17851 return (tree_int_cst_equal (parm, arg)
17852 ? unify_success (explain_p)
17853 : unify_template_argument_mismatch (explain_p, parm, arg));
17854
17855 case TREE_VEC:
17856 {
17857 int i, len, argslen;
17858 int parm_variadic_p = 0;
17859
17860 if (TREE_CODE (arg) != TREE_VEC)
17861 return unify_template_argument_mismatch (explain_p, parm, arg);
17862
17863 len = TREE_VEC_LENGTH (parm);
17864 argslen = TREE_VEC_LENGTH (arg);
17865
17866 /* Check for pack expansions in the parameters. */
17867 for (i = 0; i < len; ++i)
17868 {
17869 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17870 {
17871 if (i == len - 1)
17872 /* We can unify against something with a trailing
17873 parameter pack. */
17874 parm_variadic_p = 1;
17875 else
17876 /* [temp.deduct.type]/9: If the template argument list of
17877 P contains a pack expansion that is not the last
17878 template argument, the entire template argument list
17879 is a non-deduced context. */
17880 return unify_success (explain_p);
17881 }
17882 }
17883
17884 /* If we don't have enough arguments to satisfy the parameters
17885 (not counting the pack expression at the end), or we have
17886 too many arguments for a parameter list that doesn't end in
17887 a pack expression, we can't unify. */
17888 if (parm_variadic_p
17889 ? argslen < len - parm_variadic_p
17890 : argslen != len)
17891 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17892
17893 /* Unify all of the parameters that precede the (optional)
17894 pack expression. */
17895 for (i = 0; i < len - parm_variadic_p; ++i)
17896 {
17897 RECUR_AND_CHECK_FAILURE (tparms, targs,
17898 TREE_VEC_ELT (parm, i),
17899 TREE_VEC_ELT (arg, i),
17900 UNIFY_ALLOW_NONE, explain_p);
17901 }
17902 if (parm_variadic_p)
17903 return unify_pack_expansion (tparms, targs, parm, arg,
17904 DEDUCE_EXACT,
17905 /*subr=*/true, explain_p);
17906 return unify_success (explain_p);
17907 }
17908
17909 case RECORD_TYPE:
17910 case UNION_TYPE:
17911 if (TREE_CODE (arg) != TREE_CODE (parm))
17912 return unify_type_mismatch (explain_p, parm, arg);
17913
17914 if (TYPE_PTRMEMFUNC_P (parm))
17915 {
17916 if (!TYPE_PTRMEMFUNC_P (arg))
17917 return unify_type_mismatch (explain_p, parm, arg);
17918
17919 return unify (tparms, targs,
17920 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17921 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17922 strict, explain_p);
17923 }
17924
17925 if (CLASSTYPE_TEMPLATE_INFO (parm))
17926 {
17927 tree t = NULL_TREE;
17928
17929 if (strict_in & UNIFY_ALLOW_DERIVED)
17930 {
17931 /* First, we try to unify the PARM and ARG directly. */
17932 t = try_class_unification (tparms, targs,
17933 parm, arg, explain_p);
17934
17935 if (!t)
17936 {
17937 /* Fallback to the special case allowed in
17938 [temp.deduct.call]:
17939
17940 If P is a class, and P has the form
17941 template-id, then A can be a derived class of
17942 the deduced A. Likewise, if P is a pointer to
17943 a class of the form template-id, A can be a
17944 pointer to a derived class pointed to by the
17945 deduced A. */
17946 enum template_base_result r;
17947 r = get_template_base (tparms, targs, parm, arg,
17948 explain_p, &t);
17949
17950 if (!t)
17951 return unify_no_common_base (explain_p, r, parm, arg);
17952 }
17953 }
17954 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17955 && (CLASSTYPE_TI_TEMPLATE (parm)
17956 == CLASSTYPE_TI_TEMPLATE (arg)))
17957 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17958 Then, we should unify `int' and `U'. */
17959 t = arg;
17960 else
17961 /* There's no chance of unification succeeding. */
17962 return unify_type_mismatch (explain_p, parm, arg);
17963
17964 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17965 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17966 }
17967 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17968 return unify_type_mismatch (explain_p, parm, arg);
17969 return unify_success (explain_p);
17970
17971 case METHOD_TYPE:
17972 case FUNCTION_TYPE:
17973 {
17974 unsigned int nargs;
17975 tree *args;
17976 tree a;
17977 unsigned int i;
17978
17979 if (TREE_CODE (arg) != TREE_CODE (parm))
17980 return unify_type_mismatch (explain_p, parm, arg);
17981
17982 /* CV qualifications for methods can never be deduced, they must
17983 match exactly. We need to check them explicitly here,
17984 because type_unification_real treats them as any other
17985 cv-qualified parameter. */
17986 if (TREE_CODE (parm) == METHOD_TYPE
17987 && (!check_cv_quals_for_unify
17988 (UNIFY_ALLOW_NONE,
17989 class_of_this_parm (arg),
17990 class_of_this_parm (parm))))
17991 return unify_cv_qual_mismatch (explain_p, parm, arg);
17992
17993 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17994 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17995
17996 nargs = list_length (TYPE_ARG_TYPES (arg));
17997 args = XALLOCAVEC (tree, nargs);
17998 for (a = TYPE_ARG_TYPES (arg), i = 0;
17999 a != NULL_TREE && a != void_list_node;
18000 a = TREE_CHAIN (a), ++i)
18001 args[i] = TREE_VALUE (a);
18002 nargs = i;
18003
18004 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18005 args, nargs, 1, DEDUCE_EXACT,
18006 LOOKUP_NORMAL, NULL, explain_p);
18007 }
18008
18009 case OFFSET_TYPE:
18010 /* Unify a pointer to member with a pointer to member function, which
18011 deduces the type of the member as a function type. */
18012 if (TYPE_PTRMEMFUNC_P (arg))
18013 {
18014 /* Check top-level cv qualifiers */
18015 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18016 return unify_cv_qual_mismatch (explain_p, parm, arg);
18017
18018 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18019 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18020 UNIFY_ALLOW_NONE, explain_p);
18021
18022 /* Determine the type of the function we are unifying against. */
18023 tree fntype = static_fn_type (arg);
18024
18025 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18026 }
18027
18028 if (TREE_CODE (arg) != OFFSET_TYPE)
18029 return unify_type_mismatch (explain_p, parm, arg);
18030 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18031 TYPE_OFFSET_BASETYPE (arg),
18032 UNIFY_ALLOW_NONE, explain_p);
18033 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18034 strict, explain_p);
18035
18036 case CONST_DECL:
18037 if (DECL_TEMPLATE_PARM_P (parm))
18038 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18039 if (arg != integral_constant_value (parm))
18040 return unify_template_argument_mismatch (explain_p, parm, arg);
18041 return unify_success (explain_p);
18042
18043 case FIELD_DECL:
18044 case TEMPLATE_DECL:
18045 /* Matched cases are handled by the ARG == PARM test above. */
18046 return unify_template_argument_mismatch (explain_p, parm, arg);
18047
18048 case VAR_DECL:
18049 /* A non-type template parameter that is a variable should be a
18050 an integral constant, in which case, it whould have been
18051 folded into its (constant) value. So we should not be getting
18052 a variable here. */
18053 gcc_unreachable ();
18054
18055 case TYPE_ARGUMENT_PACK:
18056 case NONTYPE_ARGUMENT_PACK:
18057 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18058 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18059
18060 case TYPEOF_TYPE:
18061 case DECLTYPE_TYPE:
18062 case UNDERLYING_TYPE:
18063 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18064 or UNDERLYING_TYPE nodes. */
18065 return unify_success (explain_p);
18066
18067 case ERROR_MARK:
18068 /* Unification fails if we hit an error node. */
18069 return unify_invalid (explain_p);
18070
18071 case INDIRECT_REF:
18072 if (REFERENCE_REF_P (parm))
18073 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18074 strict, explain_p);
18075 /* FALLTHRU */
18076
18077 default:
18078 /* An unresolved overload is a nondeduced context. */
18079 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18080 return unify_success (explain_p);
18081 gcc_assert (EXPR_P (parm));
18082
18083 /* We must be looking at an expression. This can happen with
18084 something like:
18085
18086 template <int I>
18087 void foo(S<I>, S<I + 2>);
18088
18089 This is a "nondeduced context":
18090
18091 [deduct.type]
18092
18093 The nondeduced contexts are:
18094
18095 --A type that is a template-id in which one or more of
18096 the template-arguments is an expression that references
18097 a template-parameter.
18098
18099 In these cases, we assume deduction succeeded, but don't
18100 actually infer any unifications. */
18101
18102 if (!uses_template_parms (parm)
18103 && !template_args_equal (parm, arg))
18104 return unify_expression_unequal (explain_p, parm, arg);
18105 else
18106 return unify_success (explain_p);
18107 }
18108 }
18109 #undef RECUR_AND_CHECK_FAILURE
18110 \f
18111 /* Note that DECL can be defined in this translation unit, if
18112 required. */
18113
18114 static void
18115 mark_definable (tree decl)
18116 {
18117 tree clone;
18118 DECL_NOT_REALLY_EXTERN (decl) = 1;
18119 FOR_EACH_CLONE (clone, decl)
18120 DECL_NOT_REALLY_EXTERN (clone) = 1;
18121 }
18122
18123 /* Called if RESULT is explicitly instantiated, or is a member of an
18124 explicitly instantiated class. */
18125
18126 void
18127 mark_decl_instantiated (tree result, int extern_p)
18128 {
18129 SET_DECL_EXPLICIT_INSTANTIATION (result);
18130
18131 /* If this entity has already been written out, it's too late to
18132 make any modifications. */
18133 if (TREE_ASM_WRITTEN (result))
18134 return;
18135
18136 /* For anonymous namespace we don't need to do anything. */
18137 if (decl_anon_ns_mem_p (result))
18138 {
18139 gcc_assert (!TREE_PUBLIC (result));
18140 return;
18141 }
18142
18143 if (TREE_CODE (result) != FUNCTION_DECL)
18144 /* The TREE_PUBLIC flag for function declarations will have been
18145 set correctly by tsubst. */
18146 TREE_PUBLIC (result) = 1;
18147
18148 /* This might have been set by an earlier implicit instantiation. */
18149 DECL_COMDAT (result) = 0;
18150
18151 if (extern_p)
18152 DECL_NOT_REALLY_EXTERN (result) = 0;
18153 else
18154 {
18155 mark_definable (result);
18156 mark_needed (result);
18157 /* Always make artificials weak. */
18158 if (DECL_ARTIFICIAL (result) && flag_weak)
18159 comdat_linkage (result);
18160 /* For WIN32 we also want to put explicit instantiations in
18161 linkonce sections. */
18162 else if (TREE_PUBLIC (result))
18163 maybe_make_one_only (result);
18164 }
18165
18166 /* If EXTERN_P, then this function will not be emitted -- unless
18167 followed by an explicit instantiation, at which point its linkage
18168 will be adjusted. If !EXTERN_P, then this function will be
18169 emitted here. In neither circumstance do we want
18170 import_export_decl to adjust the linkage. */
18171 DECL_INTERFACE_KNOWN (result) = 1;
18172 }
18173
18174 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18175 important template arguments. If any are missing, we check whether
18176 they're important by using error_mark_node for substituting into any
18177 args that were used for partial ordering (the ones between ARGS and END)
18178 and seeing if it bubbles up. */
18179
18180 static bool
18181 check_undeduced_parms (tree targs, tree args, tree end)
18182 {
18183 bool found = false;
18184 int i;
18185 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18186 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18187 {
18188 found = true;
18189 TREE_VEC_ELT (targs, i) = error_mark_node;
18190 }
18191 if (found)
18192 {
18193 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18194 if (substed == error_mark_node)
18195 return true;
18196 }
18197 return false;
18198 }
18199
18200 /* Given two function templates PAT1 and PAT2, return:
18201
18202 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18203 -1 if PAT2 is more specialized than PAT1.
18204 0 if neither is more specialized.
18205
18206 LEN indicates the number of parameters we should consider
18207 (defaulted parameters should not be considered).
18208
18209 The 1998 std underspecified function template partial ordering, and
18210 DR214 addresses the issue. We take pairs of arguments, one from
18211 each of the templates, and deduce them against each other. One of
18212 the templates will be more specialized if all the *other*
18213 template's arguments deduce against its arguments and at least one
18214 of its arguments *does* *not* deduce against the other template's
18215 corresponding argument. Deduction is done as for class templates.
18216 The arguments used in deduction have reference and top level cv
18217 qualifiers removed. Iff both arguments were originally reference
18218 types *and* deduction succeeds in both directions, an lvalue reference
18219 wins against an rvalue reference and otherwise the template
18220 with the more cv-qualified argument wins for that pairing (if
18221 neither is more cv-qualified, they both are equal). Unlike regular
18222 deduction, after all the arguments have been deduced in this way,
18223 we do *not* verify the deduced template argument values can be
18224 substituted into non-deduced contexts.
18225
18226 The logic can be a bit confusing here, because we look at deduce1 and
18227 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18228 can find template arguments for pat1 to make arg1 look like arg2, that
18229 means that arg2 is at least as specialized as arg1. */
18230
18231 int
18232 more_specialized_fn (tree pat1, tree pat2, int len)
18233 {
18234 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18235 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18236 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18237 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18238 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18239 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18240 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18241 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18242 tree origs1, origs2;
18243 bool lose1 = false;
18244 bool lose2 = false;
18245
18246 /* Remove the this parameter from non-static member functions. If
18247 one is a non-static member function and the other is not a static
18248 member function, remove the first parameter from that function
18249 also. This situation occurs for operator functions where we
18250 locate both a member function (with this pointer) and non-member
18251 operator (with explicit first operand). */
18252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18253 {
18254 len--; /* LEN is the number of significant arguments for DECL1 */
18255 args1 = TREE_CHAIN (args1);
18256 if (!DECL_STATIC_FUNCTION_P (decl2))
18257 args2 = TREE_CHAIN (args2);
18258 }
18259 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18260 {
18261 args2 = TREE_CHAIN (args2);
18262 if (!DECL_STATIC_FUNCTION_P (decl1))
18263 {
18264 len--;
18265 args1 = TREE_CHAIN (args1);
18266 }
18267 }
18268
18269 /* If only one is a conversion operator, they are unordered. */
18270 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18271 return 0;
18272
18273 /* Consider the return type for a conversion function */
18274 if (DECL_CONV_FN_P (decl1))
18275 {
18276 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18277 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18278 len++;
18279 }
18280
18281 processing_template_decl++;
18282
18283 origs1 = args1;
18284 origs2 = args2;
18285
18286 while (len--
18287 /* Stop when an ellipsis is seen. */
18288 && args1 != NULL_TREE && args2 != NULL_TREE)
18289 {
18290 tree arg1 = TREE_VALUE (args1);
18291 tree arg2 = TREE_VALUE (args2);
18292 int deduce1, deduce2;
18293 int quals1 = -1;
18294 int quals2 = -1;
18295 int ref1 = 0;
18296 int ref2 = 0;
18297
18298 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18299 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18300 {
18301 /* When both arguments are pack expansions, we need only
18302 unify the patterns themselves. */
18303 arg1 = PACK_EXPANSION_PATTERN (arg1);
18304 arg2 = PACK_EXPANSION_PATTERN (arg2);
18305
18306 /* This is the last comparison we need to do. */
18307 len = 0;
18308 }
18309
18310 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18311 {
18312 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18313 arg1 = TREE_TYPE (arg1);
18314 quals1 = cp_type_quals (arg1);
18315 }
18316
18317 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18318 {
18319 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18320 arg2 = TREE_TYPE (arg2);
18321 quals2 = cp_type_quals (arg2);
18322 }
18323
18324 arg1 = TYPE_MAIN_VARIANT (arg1);
18325 arg2 = TYPE_MAIN_VARIANT (arg2);
18326
18327 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18328 {
18329 int i, len2 = list_length (args2);
18330 tree parmvec = make_tree_vec (1);
18331 tree argvec = make_tree_vec (len2);
18332 tree ta = args2;
18333
18334 /* Setup the parameter vector, which contains only ARG1. */
18335 TREE_VEC_ELT (parmvec, 0) = arg1;
18336
18337 /* Setup the argument vector, which contains the remaining
18338 arguments. */
18339 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18340 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18341
18342 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18343 argvec, DEDUCE_EXACT,
18344 /*subr=*/true, /*explain_p=*/false)
18345 == 0);
18346
18347 /* We cannot deduce in the other direction, because ARG1 is
18348 a pack expansion but ARG2 is not. */
18349 deduce2 = 0;
18350 }
18351 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18352 {
18353 int i, len1 = list_length (args1);
18354 tree parmvec = make_tree_vec (1);
18355 tree argvec = make_tree_vec (len1);
18356 tree ta = args1;
18357
18358 /* Setup the parameter vector, which contains only ARG1. */
18359 TREE_VEC_ELT (parmvec, 0) = arg2;
18360
18361 /* Setup the argument vector, which contains the remaining
18362 arguments. */
18363 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18364 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18365
18366 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18367 argvec, DEDUCE_EXACT,
18368 /*subr=*/true, /*explain_p=*/false)
18369 == 0);
18370
18371 /* We cannot deduce in the other direction, because ARG2 is
18372 a pack expansion but ARG1 is not.*/
18373 deduce1 = 0;
18374 }
18375
18376 else
18377 {
18378 /* The normal case, where neither argument is a pack
18379 expansion. */
18380 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18381 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18382 == 0);
18383 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18384 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18385 == 0);
18386 }
18387
18388 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18389 arg2, then arg2 is not as specialized as arg1. */
18390 if (!deduce1)
18391 lose2 = true;
18392 if (!deduce2)
18393 lose1 = true;
18394
18395 /* "If, for a given type, deduction succeeds in both directions
18396 (i.e., the types are identical after the transformations above)
18397 and both P and A were reference types (before being replaced with
18398 the type referred to above):
18399 - if the type from the argument template was an lvalue reference and
18400 the type from the parameter template was not, the argument type is
18401 considered to be more specialized than the other; otherwise,
18402 - if the type from the argument template is more cv-qualified
18403 than the type from the parameter template (as described above),
18404 the argument type is considered to be more specialized than the other;
18405 otherwise,
18406 - neither type is more specialized than the other." */
18407
18408 if (deduce1 && deduce2)
18409 {
18410 if (ref1 && ref2 && ref1 != ref2)
18411 {
18412 if (ref1 > ref2)
18413 lose1 = true;
18414 else
18415 lose2 = true;
18416 }
18417 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18418 {
18419 if ((quals1 & quals2) == quals2)
18420 lose2 = true;
18421 if ((quals1 & quals2) == quals1)
18422 lose1 = true;
18423 }
18424 }
18425
18426 if (lose1 && lose2)
18427 /* We've failed to deduce something in either direction.
18428 These must be unordered. */
18429 break;
18430
18431 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18432 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18433 /* We have already processed all of the arguments in our
18434 handing of the pack expansion type. */
18435 len = 0;
18436
18437 args1 = TREE_CHAIN (args1);
18438 args2 = TREE_CHAIN (args2);
18439 }
18440
18441 /* "In most cases, all template parameters must have values in order for
18442 deduction to succeed, but for partial ordering purposes a template
18443 parameter may remain without a value provided it is not used in the
18444 types being used for partial ordering."
18445
18446 Thus, if we are missing any of the targs1 we need to substitute into
18447 origs1, then pat2 is not as specialized as pat1. This can happen when
18448 there is a nondeduced context. */
18449 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18450 lose2 = true;
18451 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18452 lose1 = true;
18453
18454 processing_template_decl--;
18455
18456 /* All things being equal, if the next argument is a pack expansion
18457 for one function but not for the other, prefer the
18458 non-variadic function. FIXME this is bogus; see c++/41958. */
18459 if (lose1 == lose2
18460 && args1 && TREE_VALUE (args1)
18461 && args2 && TREE_VALUE (args2))
18462 {
18463 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18464 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18465 }
18466
18467 if (lose1 == lose2)
18468 return 0;
18469 else if (!lose1)
18470 return 1;
18471 else
18472 return -1;
18473 }
18474
18475 /* Determine which of two partial specializations of TMPL is more
18476 specialized.
18477
18478 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18479 to the first partial specialization. The TREE_VALUE is the
18480 innermost set of template parameters for the partial
18481 specialization. PAT2 is similar, but for the second template.
18482
18483 Return 1 if the first partial specialization is more specialized;
18484 -1 if the second is more specialized; 0 if neither is more
18485 specialized.
18486
18487 See [temp.class.order] for information about determining which of
18488 two templates is more specialized. */
18489
18490 static int
18491 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18492 {
18493 tree targs;
18494 tree tmpl1, tmpl2;
18495 int winner = 0;
18496 bool any_deductions = false;
18497
18498 tmpl1 = TREE_TYPE (pat1);
18499 tmpl2 = TREE_TYPE (pat2);
18500
18501 /* Just like what happens for functions, if we are ordering between
18502 different class template specializations, we may encounter dependent
18503 types in the arguments, and we need our dependency check functions
18504 to behave correctly. */
18505 ++processing_template_decl;
18506 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18507 CLASSTYPE_TI_ARGS (tmpl1),
18508 CLASSTYPE_TI_ARGS (tmpl2));
18509 if (targs)
18510 {
18511 --winner;
18512 any_deductions = true;
18513 }
18514
18515 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18516 CLASSTYPE_TI_ARGS (tmpl2),
18517 CLASSTYPE_TI_ARGS (tmpl1));
18518 if (targs)
18519 {
18520 ++winner;
18521 any_deductions = true;
18522 }
18523 --processing_template_decl;
18524
18525 /* In the case of a tie where at least one of the class templates
18526 has a parameter pack at the end, the template with the most
18527 non-packed parameters wins. */
18528 if (winner == 0
18529 && any_deductions
18530 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18531 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18532 {
18533 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18534 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18535 int len1 = TREE_VEC_LENGTH (args1);
18536 int len2 = TREE_VEC_LENGTH (args2);
18537
18538 /* We don't count the pack expansion at the end. */
18539 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18540 --len1;
18541 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18542 --len2;
18543
18544 if (len1 > len2)
18545 return 1;
18546 else if (len1 < len2)
18547 return -1;
18548 }
18549
18550 return winner;
18551 }
18552
18553 /* Return the template arguments that will produce the function signature
18554 DECL from the function template FN, with the explicit template
18555 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18556 also match. Return NULL_TREE if no satisfactory arguments could be
18557 found. */
18558
18559 static tree
18560 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18561 {
18562 int ntparms = DECL_NTPARMS (fn);
18563 tree targs = make_tree_vec (ntparms);
18564 tree decl_type = TREE_TYPE (decl);
18565 tree decl_arg_types;
18566 tree *args;
18567 unsigned int nargs, ix;
18568 tree arg;
18569
18570 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18571
18572 /* Never do unification on the 'this' parameter. */
18573 decl_arg_types = skip_artificial_parms_for (decl,
18574 TYPE_ARG_TYPES (decl_type));
18575
18576 nargs = list_length (decl_arg_types);
18577 args = XALLOCAVEC (tree, nargs);
18578 for (arg = decl_arg_types, ix = 0;
18579 arg != NULL_TREE && arg != void_list_node;
18580 arg = TREE_CHAIN (arg), ++ix)
18581 args[ix] = TREE_VALUE (arg);
18582
18583 if (fn_type_unification (fn, explicit_args, targs,
18584 args, ix,
18585 (check_rettype || DECL_CONV_FN_P (fn)
18586 ? TREE_TYPE (decl_type) : NULL_TREE),
18587 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18588 /*decltype*/false)
18589 == error_mark_node)
18590 return NULL_TREE;
18591
18592 return targs;
18593 }
18594
18595 /* Return the innermost template arguments that, when applied to a partial
18596 specialization of TMPL whose innermost template parameters are
18597 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18598 ARGS.
18599
18600 For example, suppose we have:
18601
18602 template <class T, class U> struct S {};
18603 template <class T> struct S<T*, int> {};
18604
18605 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18606 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18607 int}. The resulting vector will be {double}, indicating that `T'
18608 is bound to `double'. */
18609
18610 static tree
18611 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18612 {
18613 int i, ntparms = TREE_VEC_LENGTH (tparms);
18614 tree deduced_args;
18615 tree innermost_deduced_args;
18616
18617 innermost_deduced_args = make_tree_vec (ntparms);
18618 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18619 {
18620 deduced_args = copy_node (args);
18621 SET_TMPL_ARGS_LEVEL (deduced_args,
18622 TMPL_ARGS_DEPTH (deduced_args),
18623 innermost_deduced_args);
18624 }
18625 else
18626 deduced_args = innermost_deduced_args;
18627
18628 if (unify (tparms, deduced_args,
18629 INNERMOST_TEMPLATE_ARGS (spec_args),
18630 INNERMOST_TEMPLATE_ARGS (args),
18631 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18632 return NULL_TREE;
18633
18634 for (i = 0; i < ntparms; ++i)
18635 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18636 return NULL_TREE;
18637
18638 /* Verify that nondeduced template arguments agree with the type
18639 obtained from argument deduction.
18640
18641 For example:
18642
18643 struct A { typedef int X; };
18644 template <class T, class U> struct C {};
18645 template <class T> struct C<T, typename T::X> {};
18646
18647 Then with the instantiation `C<A, int>', we can deduce that
18648 `T' is `A' but unify () does not check whether `typename T::X'
18649 is `int'. */
18650 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18651 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18652 spec_args, tmpl,
18653 tf_none, false, false);
18654 if (spec_args == error_mark_node
18655 /* We only need to check the innermost arguments; the other
18656 arguments will always agree. */
18657 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18658 INNERMOST_TEMPLATE_ARGS (args)))
18659 return NULL_TREE;
18660
18661 /* Now that we have bindings for all of the template arguments,
18662 ensure that the arguments deduced for the template template
18663 parameters have compatible template parameter lists. See the use
18664 of template_template_parm_bindings_ok_p in fn_type_unification
18665 for more information. */
18666 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18667 return NULL_TREE;
18668
18669 return deduced_args;
18670 }
18671
18672 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18673 Return the TREE_LIST node with the most specialized template, if
18674 any. If there is no most specialized template, the error_mark_node
18675 is returned.
18676
18677 Note that this function does not look at, or modify, the
18678 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18679 returned is one of the elements of INSTANTIATIONS, callers may
18680 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18681 and retrieve it from the value returned. */
18682
18683 tree
18684 most_specialized_instantiation (tree templates)
18685 {
18686 tree fn, champ;
18687
18688 ++processing_template_decl;
18689
18690 champ = templates;
18691 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18692 {
18693 int fate = 0;
18694
18695 if (get_bindings (TREE_VALUE (champ),
18696 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18697 NULL_TREE, /*check_ret=*/true))
18698 fate--;
18699
18700 if (get_bindings (TREE_VALUE (fn),
18701 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18702 NULL_TREE, /*check_ret=*/true))
18703 fate++;
18704
18705 if (fate == -1)
18706 champ = fn;
18707 else if (!fate)
18708 {
18709 /* Equally specialized, move to next function. If there
18710 is no next function, nothing's most specialized. */
18711 fn = TREE_CHAIN (fn);
18712 champ = fn;
18713 if (!fn)
18714 break;
18715 }
18716 }
18717
18718 if (champ)
18719 /* Now verify that champ is better than everything earlier in the
18720 instantiation list. */
18721 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18722 if (get_bindings (TREE_VALUE (champ),
18723 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18724 NULL_TREE, /*check_ret=*/true)
18725 || !get_bindings (TREE_VALUE (fn),
18726 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18727 NULL_TREE, /*check_ret=*/true))
18728 {
18729 champ = NULL_TREE;
18730 break;
18731 }
18732
18733 processing_template_decl--;
18734
18735 if (!champ)
18736 return error_mark_node;
18737
18738 return champ;
18739 }
18740
18741 /* If DECL is a specialization of some template, return the most
18742 general such template. Otherwise, returns NULL_TREE.
18743
18744 For example, given:
18745
18746 template <class T> struct S { template <class U> void f(U); };
18747
18748 if TMPL is `template <class U> void S<int>::f(U)' this will return
18749 the full template. This function will not trace past partial
18750 specializations, however. For example, given in addition:
18751
18752 template <class T> struct S<T*> { template <class U> void f(U); };
18753
18754 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18755 `template <class T> template <class U> S<T*>::f(U)'. */
18756
18757 tree
18758 most_general_template (tree decl)
18759 {
18760 if (TREE_CODE (decl) != TEMPLATE_DECL)
18761 {
18762 if (tree tinfo = get_template_info (decl))
18763 decl = TI_TEMPLATE (tinfo);
18764 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
18765 template friend, or a FIELD_DECL for a capture pack. */
18766 if (TREE_CODE (decl) != TEMPLATE_DECL)
18767 return NULL_TREE;
18768 }
18769
18770 /* Look for more and more general templates. */
18771 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
18772 {
18773 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18774 (See cp-tree.h for details.) */
18775 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18776 break;
18777
18778 if (CLASS_TYPE_P (TREE_TYPE (decl))
18779 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18780 break;
18781
18782 /* Stop if we run into an explicitly specialized class template. */
18783 if (!DECL_NAMESPACE_SCOPE_P (decl)
18784 && DECL_CONTEXT (decl)
18785 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18786 break;
18787
18788 decl = DECL_TI_TEMPLATE (decl);
18789 }
18790
18791 return decl;
18792 }
18793
18794 /* Return the most specialized of the class template partial
18795 specializations which can produce TYPE, a specialization of some class
18796 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18797 a _TYPE node corresponding to the partial specialization, while the
18798 TREE_PURPOSE is the set of template arguments that must be
18799 substituted into the TREE_TYPE in order to generate TYPE.
18800
18801 If the choice of partial specialization is ambiguous, a diagnostic
18802 is issued, and the error_mark_node is returned. If there are no
18803 partial specializations matching TYPE, then NULL_TREE is
18804 returned, indicating that the primary template should be used. */
18805
18806 static tree
18807 most_specialized_class (tree type, tsubst_flags_t complain)
18808 {
18809 tree list = NULL_TREE;
18810 tree t;
18811 tree champ;
18812 int fate;
18813 bool ambiguous_p;
18814 tree outer_args = NULL_TREE;
18815
18816 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18817 tree main_tmpl = most_general_template (tmpl);
18818 tree args = CLASSTYPE_TI_ARGS (type);
18819
18820 /* For determining which partial specialization to use, only the
18821 innermost args are interesting. */
18822 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18823 {
18824 outer_args = strip_innermost_template_args (args, 1);
18825 args = INNERMOST_TEMPLATE_ARGS (args);
18826 }
18827
18828 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18829 {
18830 tree partial_spec_args;
18831 tree spec_args;
18832 tree spec_tmpl = TREE_VALUE (t);
18833 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18834
18835 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18836
18837 ++processing_template_decl;
18838
18839 if (outer_args)
18840 {
18841 /* Discard the outer levels of args, and then substitute in the
18842 template args from the enclosing class. */
18843 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18844 partial_spec_args = tsubst_template_args
18845 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18846
18847 /* And the same for the partial specialization TEMPLATE_DECL. */
18848 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18849 }
18850
18851 partial_spec_args =
18852 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18853 partial_spec_args,
18854 tmpl, tf_none,
18855 /*require_all_args=*/true,
18856 /*use_default_args=*/true);
18857
18858 --processing_template_decl;
18859
18860 if (partial_spec_args == error_mark_node)
18861 return error_mark_node;
18862 if (spec_tmpl == error_mark_node)
18863 return error_mark_node;
18864
18865 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18866 spec_args = get_class_bindings (tmpl, parms,
18867 partial_spec_args,
18868 args);
18869 if (spec_args)
18870 {
18871 if (outer_args)
18872 spec_args = add_to_template_args (outer_args, spec_args);
18873 list = tree_cons (spec_args, orig_parms, list);
18874 TREE_TYPE (list) = TREE_TYPE (t);
18875 }
18876 }
18877
18878 if (! list)
18879 return NULL_TREE;
18880
18881 ambiguous_p = false;
18882 t = list;
18883 champ = t;
18884 t = TREE_CHAIN (t);
18885 for (; t; t = TREE_CHAIN (t))
18886 {
18887 fate = more_specialized_class (tmpl, champ, t);
18888 if (fate == 1)
18889 ;
18890 else
18891 {
18892 if (fate == 0)
18893 {
18894 t = TREE_CHAIN (t);
18895 if (! t)
18896 {
18897 ambiguous_p = true;
18898 break;
18899 }
18900 }
18901 champ = t;
18902 }
18903 }
18904
18905 if (!ambiguous_p)
18906 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18907 {
18908 fate = more_specialized_class (tmpl, champ, t);
18909 if (fate != 1)
18910 {
18911 ambiguous_p = true;
18912 break;
18913 }
18914 }
18915
18916 if (ambiguous_p)
18917 {
18918 const char *str;
18919 char *spaces = NULL;
18920 if (!(complain & tf_error))
18921 return error_mark_node;
18922 error ("ambiguous class template instantiation for %q#T", type);
18923 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18924 for (t = list; t; t = TREE_CHAIN (t))
18925 {
18926 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18927 spaces = spaces ? spaces : get_spaces (str);
18928 }
18929 free (spaces);
18930 return error_mark_node;
18931 }
18932
18933 return champ;
18934 }
18935
18936 /* Explicitly instantiate DECL. */
18937
18938 void
18939 do_decl_instantiation (tree decl, tree storage)
18940 {
18941 tree result = NULL_TREE;
18942 int extern_p = 0;
18943
18944 if (!decl || decl == error_mark_node)
18945 /* An error occurred, for which grokdeclarator has already issued
18946 an appropriate message. */
18947 return;
18948 else if (! DECL_LANG_SPECIFIC (decl))
18949 {
18950 error ("explicit instantiation of non-template %q#D", decl);
18951 return;
18952 }
18953 else if (VAR_P (decl))
18954 {
18955 /* There is an asymmetry here in the way VAR_DECLs and
18956 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18957 the latter, the DECL we get back will be marked as a
18958 template instantiation, and the appropriate
18959 DECL_TEMPLATE_INFO will be set up. This does not happen for
18960 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18961 should handle VAR_DECLs as it currently handles
18962 FUNCTION_DECLs. */
18963 if (!DECL_CLASS_SCOPE_P (decl))
18964 {
18965 error ("%qD is not a static data member of a class template", decl);
18966 return;
18967 }
18968 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18969 if (!result || !VAR_P (result))
18970 {
18971 error ("no matching template for %qD found", decl);
18972 return;
18973 }
18974 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18975 {
18976 error ("type %qT for explicit instantiation %qD does not match "
18977 "declared type %qT", TREE_TYPE (result), decl,
18978 TREE_TYPE (decl));
18979 return;
18980 }
18981 }
18982 else if (TREE_CODE (decl) != FUNCTION_DECL)
18983 {
18984 error ("explicit instantiation of %q#D", decl);
18985 return;
18986 }
18987 else
18988 result = decl;
18989
18990 /* Check for various error cases. Note that if the explicit
18991 instantiation is valid the RESULT will currently be marked as an
18992 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18993 until we get here. */
18994
18995 if (DECL_TEMPLATE_SPECIALIZATION (result))
18996 {
18997 /* DR 259 [temp.spec].
18998
18999 Both an explicit instantiation and a declaration of an explicit
19000 specialization shall not appear in a program unless the explicit
19001 instantiation follows a declaration of the explicit specialization.
19002
19003 For a given set of template parameters, if an explicit
19004 instantiation of a template appears after a declaration of an
19005 explicit specialization for that template, the explicit
19006 instantiation has no effect. */
19007 return;
19008 }
19009 else if (DECL_EXPLICIT_INSTANTIATION (result))
19010 {
19011 /* [temp.spec]
19012
19013 No program shall explicitly instantiate any template more
19014 than once.
19015
19016 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19017 the first instantiation was `extern' and the second is not,
19018 and EXTERN_P for the opposite case. */
19019 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19020 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19021 /* If an "extern" explicit instantiation follows an ordinary
19022 explicit instantiation, the template is instantiated. */
19023 if (extern_p)
19024 return;
19025 }
19026 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19027 {
19028 error ("no matching template for %qD found", result);
19029 return;
19030 }
19031 else if (!DECL_TEMPLATE_INFO (result))
19032 {
19033 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19034 return;
19035 }
19036
19037 if (storage == NULL_TREE)
19038 ;
19039 else if (storage == ridpointers[(int) RID_EXTERN])
19040 {
19041 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19042 pedwarn (input_location, OPT_Wpedantic,
19043 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19044 "instantiations");
19045 extern_p = 1;
19046 }
19047 else
19048 error ("storage class %qD applied to template instantiation", storage);
19049
19050 check_explicit_instantiation_namespace (result);
19051 mark_decl_instantiated (result, extern_p);
19052 if (! extern_p)
19053 instantiate_decl (result, /*defer_ok=*/1,
19054 /*expl_inst_class_mem_p=*/false);
19055 }
19056
19057 static void
19058 mark_class_instantiated (tree t, int extern_p)
19059 {
19060 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19061 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19062 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19063 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19064 if (! extern_p)
19065 {
19066 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19067 rest_of_type_compilation (t, 1);
19068 }
19069 }
19070
19071 /* Called from do_type_instantiation through binding_table_foreach to
19072 do recursive instantiation for the type bound in ENTRY. */
19073 static void
19074 bt_instantiate_type_proc (binding_entry entry, void *data)
19075 {
19076 tree storage = *(tree *) data;
19077
19078 if (MAYBE_CLASS_TYPE_P (entry->type)
19079 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19080 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19081 }
19082
19083 /* Called from do_type_instantiation to instantiate a member
19084 (a member function or a static member variable) of an
19085 explicitly instantiated class template. */
19086 static void
19087 instantiate_class_member (tree decl, int extern_p)
19088 {
19089 mark_decl_instantiated (decl, extern_p);
19090 if (! extern_p)
19091 instantiate_decl (decl, /*defer_ok=*/1,
19092 /*expl_inst_class_mem_p=*/true);
19093 }
19094
19095 /* Perform an explicit instantiation of template class T. STORAGE, if
19096 non-null, is the RID for extern, inline or static. COMPLAIN is
19097 nonzero if this is called from the parser, zero if called recursively,
19098 since the standard is unclear (as detailed below). */
19099
19100 void
19101 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19102 {
19103 int extern_p = 0;
19104 int nomem_p = 0;
19105 int static_p = 0;
19106 int previous_instantiation_extern_p = 0;
19107
19108 if (TREE_CODE (t) == TYPE_DECL)
19109 t = TREE_TYPE (t);
19110
19111 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19112 {
19113 tree tmpl =
19114 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19115 if (tmpl)
19116 error ("explicit instantiation of non-class template %qD", tmpl);
19117 else
19118 error ("explicit instantiation of non-template type %qT", t);
19119 return;
19120 }
19121
19122 complete_type (t);
19123
19124 if (!COMPLETE_TYPE_P (t))
19125 {
19126 if (complain & tf_error)
19127 error ("explicit instantiation of %q#T before definition of template",
19128 t);
19129 return;
19130 }
19131
19132 if (storage != NULL_TREE)
19133 {
19134 if (!in_system_header_at (input_location))
19135 {
19136 if (storage == ridpointers[(int) RID_EXTERN])
19137 {
19138 if (cxx_dialect == cxx98)
19139 pedwarn (input_location, OPT_Wpedantic,
19140 "ISO C++ 1998 forbids the use of %<extern%> on "
19141 "explicit instantiations");
19142 }
19143 else
19144 pedwarn (input_location, OPT_Wpedantic,
19145 "ISO C++ forbids the use of %qE"
19146 " on explicit instantiations", storage);
19147 }
19148
19149 if (storage == ridpointers[(int) RID_INLINE])
19150 nomem_p = 1;
19151 else if (storage == ridpointers[(int) RID_EXTERN])
19152 extern_p = 1;
19153 else if (storage == ridpointers[(int) RID_STATIC])
19154 static_p = 1;
19155 else
19156 {
19157 error ("storage class %qD applied to template instantiation",
19158 storage);
19159 extern_p = 0;
19160 }
19161 }
19162
19163 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19164 {
19165 /* DR 259 [temp.spec].
19166
19167 Both an explicit instantiation and a declaration of an explicit
19168 specialization shall not appear in a program unless the explicit
19169 instantiation follows a declaration of the explicit specialization.
19170
19171 For a given set of template parameters, if an explicit
19172 instantiation of a template appears after a declaration of an
19173 explicit specialization for that template, the explicit
19174 instantiation has no effect. */
19175 return;
19176 }
19177 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19178 {
19179 /* [temp.spec]
19180
19181 No program shall explicitly instantiate any template more
19182 than once.
19183
19184 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19185 instantiation was `extern'. If EXTERN_P then the second is.
19186 These cases are OK. */
19187 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19188
19189 if (!previous_instantiation_extern_p && !extern_p
19190 && (complain & tf_error))
19191 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19192
19193 /* If we've already instantiated the template, just return now. */
19194 if (!CLASSTYPE_INTERFACE_ONLY (t))
19195 return;
19196 }
19197
19198 check_explicit_instantiation_namespace (TYPE_NAME (t));
19199 mark_class_instantiated (t, extern_p);
19200
19201 if (nomem_p)
19202 return;
19203
19204 {
19205 tree tmp;
19206
19207 /* In contrast to implicit instantiation, where only the
19208 declarations, and not the definitions, of members are
19209 instantiated, we have here:
19210
19211 [temp.explicit]
19212
19213 The explicit instantiation of a class template specialization
19214 implies the instantiation of all of its members not
19215 previously explicitly specialized in the translation unit
19216 containing the explicit instantiation.
19217
19218 Of course, we can't instantiate member template classes, since
19219 we don't have any arguments for them. Note that the standard
19220 is unclear on whether the instantiation of the members are
19221 *explicit* instantiations or not. However, the most natural
19222 interpretation is that it should be an explicit instantiation. */
19223
19224 if (! static_p)
19225 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19226 if (TREE_CODE (tmp) == FUNCTION_DECL
19227 && DECL_TEMPLATE_INSTANTIATION (tmp))
19228 instantiate_class_member (tmp, extern_p);
19229
19230 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19231 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19232 instantiate_class_member (tmp, extern_p);
19233
19234 if (CLASSTYPE_NESTED_UTDS (t))
19235 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19236 bt_instantiate_type_proc, &storage);
19237 }
19238 }
19239
19240 /* Given a function DECL, which is a specialization of TMPL, modify
19241 DECL to be a re-instantiation of TMPL with the same template
19242 arguments. TMPL should be the template into which tsubst'ing
19243 should occur for DECL, not the most general template.
19244
19245 One reason for doing this is a scenario like this:
19246
19247 template <class T>
19248 void f(const T&, int i);
19249
19250 void g() { f(3, 7); }
19251
19252 template <class T>
19253 void f(const T& t, const int i) { }
19254
19255 Note that when the template is first instantiated, with
19256 instantiate_template, the resulting DECL will have no name for the
19257 first parameter, and the wrong type for the second. So, when we go
19258 to instantiate the DECL, we regenerate it. */
19259
19260 static void
19261 regenerate_decl_from_template (tree decl, tree tmpl)
19262 {
19263 /* The arguments used to instantiate DECL, from the most general
19264 template. */
19265 tree args;
19266 tree code_pattern;
19267
19268 args = DECL_TI_ARGS (decl);
19269 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19270
19271 /* Make sure that we can see identifiers, and compute access
19272 correctly. */
19273 push_access_scope (decl);
19274
19275 if (TREE_CODE (decl) == FUNCTION_DECL)
19276 {
19277 tree decl_parm;
19278 tree pattern_parm;
19279 tree specs;
19280 int args_depth;
19281 int parms_depth;
19282
19283 args_depth = TMPL_ARGS_DEPTH (args);
19284 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19285 if (args_depth > parms_depth)
19286 args = get_innermost_template_args (args, parms_depth);
19287
19288 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19289 args, tf_error, NULL_TREE,
19290 /*defer_ok*/false);
19291 if (specs && specs != error_mark_node)
19292 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19293 specs);
19294
19295 /* Merge parameter declarations. */
19296 decl_parm = skip_artificial_parms_for (decl,
19297 DECL_ARGUMENTS (decl));
19298 pattern_parm
19299 = skip_artificial_parms_for (code_pattern,
19300 DECL_ARGUMENTS (code_pattern));
19301 while (decl_parm && !DECL_PACK_P (pattern_parm))
19302 {
19303 tree parm_type;
19304 tree attributes;
19305
19306 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19307 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19308 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19309 NULL_TREE);
19310 parm_type = type_decays_to (parm_type);
19311 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19312 TREE_TYPE (decl_parm) = parm_type;
19313 attributes = DECL_ATTRIBUTES (pattern_parm);
19314 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19315 {
19316 DECL_ATTRIBUTES (decl_parm) = attributes;
19317 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19318 }
19319 decl_parm = DECL_CHAIN (decl_parm);
19320 pattern_parm = DECL_CHAIN (pattern_parm);
19321 }
19322 /* Merge any parameters that match with the function parameter
19323 pack. */
19324 if (pattern_parm && DECL_PACK_P (pattern_parm))
19325 {
19326 int i, len;
19327 tree expanded_types;
19328 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19329 the parameters in this function parameter pack. */
19330 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19331 args, tf_error, NULL_TREE);
19332 len = TREE_VEC_LENGTH (expanded_types);
19333 for (i = 0; i < len; i++)
19334 {
19335 tree parm_type;
19336 tree attributes;
19337
19338 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19339 /* Rename the parameter to include the index. */
19340 DECL_NAME (decl_parm) =
19341 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19342 parm_type = TREE_VEC_ELT (expanded_types, i);
19343 parm_type = type_decays_to (parm_type);
19344 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19345 TREE_TYPE (decl_parm) = parm_type;
19346 attributes = DECL_ATTRIBUTES (pattern_parm);
19347 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19348 {
19349 DECL_ATTRIBUTES (decl_parm) = attributes;
19350 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19351 }
19352 decl_parm = DECL_CHAIN (decl_parm);
19353 }
19354 }
19355 /* Merge additional specifiers from the CODE_PATTERN. */
19356 if (DECL_DECLARED_INLINE_P (code_pattern)
19357 && !DECL_DECLARED_INLINE_P (decl))
19358 DECL_DECLARED_INLINE_P (decl) = 1;
19359 }
19360 else if (VAR_P (decl))
19361 {
19362 DECL_INITIAL (decl) =
19363 tsubst_expr (DECL_INITIAL (code_pattern), args,
19364 tf_error, DECL_TI_TEMPLATE (decl),
19365 /*integral_constant_expression_p=*/false);
19366 if (VAR_HAD_UNKNOWN_BOUND (decl))
19367 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19368 tf_error, DECL_TI_TEMPLATE (decl));
19369 }
19370 else
19371 gcc_unreachable ();
19372
19373 pop_access_scope (decl);
19374 }
19375
19376 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19377 substituted to get DECL. */
19378
19379 tree
19380 template_for_substitution (tree decl)
19381 {
19382 tree tmpl = DECL_TI_TEMPLATE (decl);
19383
19384 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19385 for the instantiation. This is not always the most general
19386 template. Consider, for example:
19387
19388 template <class T>
19389 struct S { template <class U> void f();
19390 template <> void f<int>(); };
19391
19392 and an instantiation of S<double>::f<int>. We want TD to be the
19393 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19394 while (/* An instantiation cannot have a definition, so we need a
19395 more general template. */
19396 DECL_TEMPLATE_INSTANTIATION (tmpl)
19397 /* We must also deal with friend templates. Given:
19398
19399 template <class T> struct S {
19400 template <class U> friend void f() {};
19401 };
19402
19403 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19404 so far as the language is concerned, but that's still
19405 where we get the pattern for the instantiation from. On
19406 other hand, if the definition comes outside the class, say:
19407
19408 template <class T> struct S {
19409 template <class U> friend void f();
19410 };
19411 template <class U> friend void f() {}
19412
19413 we don't need to look any further. That's what the check for
19414 DECL_INITIAL is for. */
19415 || (TREE_CODE (decl) == FUNCTION_DECL
19416 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19417 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19418 {
19419 /* The present template, TD, should not be a definition. If it
19420 were a definition, we should be using it! Note that we
19421 cannot restructure the loop to just keep going until we find
19422 a template with a definition, since that might go too far if
19423 a specialization was declared, but not defined. */
19424 gcc_assert (!VAR_P (decl)
19425 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19426
19427 /* Fetch the more general template. */
19428 tmpl = DECL_TI_TEMPLATE (tmpl);
19429 }
19430
19431 return tmpl;
19432 }
19433
19434 /* Returns true if we need to instantiate this template instance even if we
19435 know we aren't going to emit it.. */
19436
19437 bool
19438 always_instantiate_p (tree decl)
19439 {
19440 /* We always instantiate inline functions so that we can inline them. An
19441 explicit instantiation declaration prohibits implicit instantiation of
19442 non-inline functions. With high levels of optimization, we would
19443 normally inline non-inline functions -- but we're not allowed to do
19444 that for "extern template" functions. Therefore, we check
19445 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19446 return ((TREE_CODE (decl) == FUNCTION_DECL
19447 && (DECL_DECLARED_INLINE_P (decl)
19448 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19449 /* And we need to instantiate static data members so that
19450 their initializers are available in integral constant
19451 expressions. */
19452 || (VAR_P (decl)
19453 && decl_maybe_constant_var_p (decl)));
19454 }
19455
19456 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19457 instantiate it now, modifying TREE_TYPE (fn). */
19458
19459 void
19460 maybe_instantiate_noexcept (tree fn)
19461 {
19462 tree fntype, spec, noex, clone;
19463
19464 /* Don't instantiate a noexcept-specification from template context. */
19465 if (processing_template_decl)
19466 return;
19467
19468 if (DECL_CLONED_FUNCTION_P (fn))
19469 fn = DECL_CLONED_FUNCTION (fn);
19470 fntype = TREE_TYPE (fn);
19471 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19472
19473 if (!spec || !TREE_PURPOSE (spec))
19474 return;
19475
19476 noex = TREE_PURPOSE (spec);
19477
19478 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19479 {
19480 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
19481 spec = get_defaulted_eh_spec (fn);
19482 else if (push_tinst_level (fn))
19483 {
19484 push_access_scope (fn);
19485 push_deferring_access_checks (dk_no_deferred);
19486 input_location = DECL_SOURCE_LOCATION (fn);
19487 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19488 DEFERRED_NOEXCEPT_ARGS (noex),
19489 tf_warning_or_error, fn,
19490 /*function_p=*/false,
19491 /*integral_constant_expression_p=*/true);
19492 pop_deferring_access_checks ();
19493 pop_access_scope (fn);
19494 pop_tinst_level ();
19495 spec = build_noexcept_spec (noex, tf_warning_or_error);
19496 if (spec == error_mark_node)
19497 spec = noexcept_false_spec;
19498 }
19499 else
19500 spec = noexcept_false_spec;
19501
19502 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19503 }
19504
19505 FOR_EACH_CLONE (clone, fn)
19506 {
19507 if (TREE_TYPE (clone) == fntype)
19508 TREE_TYPE (clone) = TREE_TYPE (fn);
19509 else
19510 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19511 }
19512 }
19513
19514 /* Produce the definition of D, a _DECL generated from a template. If
19515 DEFER_OK is nonzero, then we don't have to actually do the
19516 instantiation now; we just have to do it sometime. Normally it is
19517 an error if this is an explicit instantiation but D is undefined.
19518 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19519 explicitly instantiated class template. */
19520
19521 tree
19522 instantiate_decl (tree d, int defer_ok,
19523 bool expl_inst_class_mem_p)
19524 {
19525 tree tmpl = DECL_TI_TEMPLATE (d);
19526 tree gen_args;
19527 tree args;
19528 tree td;
19529 tree code_pattern;
19530 tree spec;
19531 tree gen_tmpl;
19532 bool pattern_defined;
19533 location_t saved_loc = input_location;
19534 int saved_unevaluated_operand = cp_unevaluated_operand;
19535 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19536 bool external_p;
19537 tree fn_context;
19538 bool nested;
19539
19540 /* This function should only be used to instantiate templates for
19541 functions and static member variables. */
19542 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19543
19544 /* Variables are never deferred; if instantiation is required, they
19545 are instantiated right away. That allows for better code in the
19546 case that an expression refers to the value of the variable --
19547 if the variable has a constant value the referring expression can
19548 take advantage of that fact. */
19549 if (VAR_P (d)
19550 || DECL_DECLARED_CONSTEXPR_P (d))
19551 defer_ok = 0;
19552
19553 /* Don't instantiate cloned functions. Instead, instantiate the
19554 functions they cloned. */
19555 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19556 d = DECL_CLONED_FUNCTION (d);
19557
19558 if (DECL_TEMPLATE_INSTANTIATED (d)
19559 || (TREE_CODE (d) == FUNCTION_DECL
19560 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19561 || DECL_TEMPLATE_SPECIALIZATION (d))
19562 /* D has already been instantiated or explicitly specialized, so
19563 there's nothing for us to do here.
19564
19565 It might seem reasonable to check whether or not D is an explicit
19566 instantiation, and, if so, stop here. But when an explicit
19567 instantiation is deferred until the end of the compilation,
19568 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19569 the instantiation. */
19570 return d;
19571
19572 /* Check to see whether we know that this template will be
19573 instantiated in some other file, as with "extern template"
19574 extension. */
19575 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19576
19577 /* In general, we do not instantiate such templates. */
19578 if (external_p && !always_instantiate_p (d))
19579 return d;
19580
19581 gen_tmpl = most_general_template (tmpl);
19582 gen_args = DECL_TI_ARGS (d);
19583
19584 if (tmpl != gen_tmpl)
19585 /* We should already have the extra args. */
19586 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19587 == TMPL_ARGS_DEPTH (gen_args));
19588 /* And what's in the hash table should match D. */
19589 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19590 || spec == NULL_TREE);
19591
19592 /* This needs to happen before any tsubsting. */
19593 if (! push_tinst_level (d))
19594 return d;
19595
19596 timevar_push (TV_TEMPLATE_INST);
19597
19598 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19599 for the instantiation. */
19600 td = template_for_substitution (d);
19601 code_pattern = DECL_TEMPLATE_RESULT (td);
19602
19603 /* We should never be trying to instantiate a member of a class
19604 template or partial specialization. */
19605 gcc_assert (d != code_pattern);
19606
19607 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19608 || DECL_TEMPLATE_SPECIALIZATION (td))
19609 /* In the case of a friend template whose definition is provided
19610 outside the class, we may have too many arguments. Drop the
19611 ones we don't need. The same is true for specializations. */
19612 args = get_innermost_template_args
19613 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19614 else
19615 args = gen_args;
19616
19617 if (TREE_CODE (d) == FUNCTION_DECL)
19618 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19619 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19620 else
19621 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19622
19623 /* We may be in the middle of deferred access check. Disable it now. */
19624 push_deferring_access_checks (dk_no_deferred);
19625
19626 /* Unless an explicit instantiation directive has already determined
19627 the linkage of D, remember that a definition is available for
19628 this entity. */
19629 if (pattern_defined
19630 && !DECL_INTERFACE_KNOWN (d)
19631 && !DECL_NOT_REALLY_EXTERN (d))
19632 mark_definable (d);
19633
19634 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19635 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19636 input_location = DECL_SOURCE_LOCATION (d);
19637
19638 /* If D is a member of an explicitly instantiated class template,
19639 and no definition is available, treat it like an implicit
19640 instantiation. */
19641 if (!pattern_defined && expl_inst_class_mem_p
19642 && DECL_EXPLICIT_INSTANTIATION (d))
19643 {
19644 /* Leave linkage flags alone on instantiations with anonymous
19645 visibility. */
19646 if (TREE_PUBLIC (d))
19647 {
19648 DECL_NOT_REALLY_EXTERN (d) = 0;
19649 DECL_INTERFACE_KNOWN (d) = 0;
19650 }
19651 SET_DECL_IMPLICIT_INSTANTIATION (d);
19652 }
19653
19654 /* Defer all other templates, unless we have been explicitly
19655 forbidden from doing so. */
19656 if (/* If there is no definition, we cannot instantiate the
19657 template. */
19658 ! pattern_defined
19659 /* If it's OK to postpone instantiation, do so. */
19660 || defer_ok
19661 /* If this is a static data member that will be defined
19662 elsewhere, we don't want to instantiate the entire data
19663 member, but we do want to instantiate the initializer so that
19664 we can substitute that elsewhere. */
19665 || (external_p && VAR_P (d)))
19666 {
19667 /* The definition of the static data member is now required so
19668 we must substitute the initializer. */
19669 if (VAR_P (d)
19670 && !DECL_INITIAL (d)
19671 && DECL_INITIAL (code_pattern))
19672 {
19673 tree ns;
19674 tree init;
19675 bool const_init = false;
19676
19677 ns = decl_namespace_context (d);
19678 push_nested_namespace (ns);
19679 push_nested_class (DECL_CONTEXT (d));
19680 init = tsubst_expr (DECL_INITIAL (code_pattern),
19681 args,
19682 tf_warning_or_error, NULL_TREE,
19683 /*integral_constant_expression_p=*/false);
19684 /* Make sure the initializer is still constant, in case of
19685 circular dependency (template/instantiate6.C). */
19686 const_init
19687 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19688 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19689 /*asmspec_tree=*/NULL_TREE,
19690 LOOKUP_ONLYCONVERTING);
19691 pop_nested_class ();
19692 pop_nested_namespace (ns);
19693 }
19694
19695 /* We restore the source position here because it's used by
19696 add_pending_template. */
19697 input_location = saved_loc;
19698
19699 if (at_eof && !pattern_defined
19700 && DECL_EXPLICIT_INSTANTIATION (d)
19701 && DECL_NOT_REALLY_EXTERN (d))
19702 /* [temp.explicit]
19703
19704 The definition of a non-exported function template, a
19705 non-exported member function template, or a non-exported
19706 member function or static data member of a class template
19707 shall be present in every translation unit in which it is
19708 explicitly instantiated. */
19709 permerror (input_location, "explicit instantiation of %qD "
19710 "but no definition available", d);
19711
19712 /* If we're in unevaluated context, we just wanted to get the
19713 constant value; this isn't an odr use, so don't queue
19714 a full instantiation. */
19715 if (cp_unevaluated_operand != 0)
19716 goto out;
19717 /* ??? Historically, we have instantiated inline functions, even
19718 when marked as "extern template". */
19719 if (!(external_p && VAR_P (d)))
19720 add_pending_template (d);
19721 goto out;
19722 }
19723 /* Tell the repository that D is available in this translation unit
19724 -- and see if it is supposed to be instantiated here. */
19725 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19726 {
19727 /* In a PCH file, despite the fact that the repository hasn't
19728 requested instantiation in the PCH it is still possible that
19729 an instantiation will be required in a file that includes the
19730 PCH. */
19731 if (pch_file)
19732 add_pending_template (d);
19733 /* Instantiate inline functions so that the inliner can do its
19734 job, even though we'll not be emitting a copy of this
19735 function. */
19736 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19737 goto out;
19738 }
19739
19740 fn_context = decl_function_context (d);
19741 nested = (current_function_decl != NULL_TREE);
19742 if (!fn_context)
19743 push_to_top_level ();
19744 else
19745 {
19746 if (nested)
19747 push_function_context ();
19748 cp_unevaluated_operand = 0;
19749 c_inhibit_evaluation_warnings = 0;
19750 }
19751
19752 /* Mark D as instantiated so that recursive calls to
19753 instantiate_decl do not try to instantiate it again. */
19754 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19755
19756 /* Regenerate the declaration in case the template has been modified
19757 by a subsequent redeclaration. */
19758 regenerate_decl_from_template (d, td);
19759
19760 /* We already set the file and line above. Reset them now in case
19761 they changed as a result of calling regenerate_decl_from_template. */
19762 input_location = DECL_SOURCE_LOCATION (d);
19763
19764 if (VAR_P (d))
19765 {
19766 tree init;
19767 bool const_init = false;
19768
19769 /* Clear out DECL_RTL; whatever was there before may not be right
19770 since we've reset the type of the declaration. */
19771 SET_DECL_RTL (d, NULL);
19772 DECL_IN_AGGR_P (d) = 0;
19773
19774 /* The initializer is placed in DECL_INITIAL by
19775 regenerate_decl_from_template so we don't need to
19776 push/pop_access_scope again here. Pull it out so that
19777 cp_finish_decl can process it. */
19778 init = DECL_INITIAL (d);
19779 DECL_INITIAL (d) = NULL_TREE;
19780 DECL_INITIALIZED_P (d) = 0;
19781
19782 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19783 initializer. That function will defer actual emission until
19784 we have a chance to determine linkage. */
19785 DECL_EXTERNAL (d) = 0;
19786
19787 /* Enter the scope of D so that access-checking works correctly. */
19788 push_nested_class (DECL_CONTEXT (d));
19789 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19790 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19791 pop_nested_class ();
19792 }
19793 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19794 synthesize_method (d);
19795 else if (TREE_CODE (d) == FUNCTION_DECL)
19796 {
19797 struct pointer_map_t *saved_local_specializations;
19798 tree subst_decl;
19799 tree tmpl_parm;
19800 tree spec_parm;
19801 tree block = NULL_TREE;
19802
19803 /* Save away the current list, in case we are instantiating one
19804 template from within the body of another. */
19805 saved_local_specializations = local_specializations;
19806
19807 /* Set up the list of local specializations. */
19808 local_specializations = pointer_map_create ();
19809
19810 /* Set up context. */
19811 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19812 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19813 block = push_stmt_list ();
19814 else
19815 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19816
19817 /* Some typedefs referenced from within the template code need to be
19818 access checked at template instantiation time, i.e now. These
19819 types were added to the template at parsing time. Let's get those
19820 and perform the access checks then. */
19821 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19822 gen_args);
19823
19824 /* Create substitution entries for the parameters. */
19825 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19826 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19827 spec_parm = DECL_ARGUMENTS (d);
19828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19829 {
19830 register_local_specialization (spec_parm, tmpl_parm);
19831 spec_parm = skip_artificial_parms_for (d, spec_parm);
19832 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19833 }
19834 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19835 {
19836 if (!DECL_PACK_P (tmpl_parm))
19837 {
19838 register_local_specialization (spec_parm, tmpl_parm);
19839 spec_parm = DECL_CHAIN (spec_parm);
19840 }
19841 else
19842 {
19843 /* Register the (value) argument pack as a specialization of
19844 TMPL_PARM, then move on. */
19845 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19846 register_local_specialization (argpack, tmpl_parm);
19847 }
19848 }
19849 gcc_assert (!spec_parm);
19850
19851 /* Substitute into the body of the function. */
19852 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19853 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19854 tf_warning_or_error, tmpl);
19855 else
19856 {
19857 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19858 tf_warning_or_error, tmpl,
19859 /*integral_constant_expression_p=*/false);
19860
19861 /* Set the current input_location to the end of the function
19862 so that finish_function knows where we are. */
19863 input_location
19864 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19865
19866 /* Remember if we saw an infinite loop in the template. */
19867 current_function_infinite_loop
19868 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19869 }
19870
19871 /* We don't need the local specializations any more. */
19872 pointer_map_destroy (local_specializations);
19873 local_specializations = saved_local_specializations;
19874
19875 /* Finish the function. */
19876 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19877 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19878 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19879 else
19880 {
19881 d = finish_function (0);
19882 expand_or_defer_fn (d);
19883 }
19884
19885 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19886 cp_check_omp_declare_reduction (d);
19887 }
19888
19889 /* We're not deferring instantiation any more. */
19890 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19891
19892 if (!fn_context)
19893 pop_from_top_level ();
19894 else if (nested)
19895 pop_function_context ();
19896
19897 out:
19898 input_location = saved_loc;
19899 cp_unevaluated_operand = saved_unevaluated_operand;
19900 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19901 pop_deferring_access_checks ();
19902 pop_tinst_level ();
19903
19904 timevar_pop (TV_TEMPLATE_INST);
19905
19906 return d;
19907 }
19908
19909 /* Run through the list of templates that we wish we could
19910 instantiate, and instantiate any we can. RETRIES is the
19911 number of times we retry pending template instantiation. */
19912
19913 void
19914 instantiate_pending_templates (int retries)
19915 {
19916 int reconsider;
19917 location_t saved_loc = input_location;
19918
19919 /* Instantiating templates may trigger vtable generation. This in turn
19920 may require further template instantiations. We place a limit here
19921 to avoid infinite loop. */
19922 if (pending_templates && retries >= max_tinst_depth)
19923 {
19924 tree decl = pending_templates->tinst->decl;
19925
19926 error ("template instantiation depth exceeds maximum of %d"
19927 " instantiating %q+D, possibly from virtual table generation"
19928 " (use -ftemplate-depth= to increase the maximum)",
19929 max_tinst_depth, decl);
19930 if (TREE_CODE (decl) == FUNCTION_DECL)
19931 /* Pretend that we defined it. */
19932 DECL_INITIAL (decl) = error_mark_node;
19933 return;
19934 }
19935
19936 do
19937 {
19938 struct pending_template **t = &pending_templates;
19939 struct pending_template *last = NULL;
19940 reconsider = 0;
19941 while (*t)
19942 {
19943 tree instantiation = reopen_tinst_level ((*t)->tinst);
19944 bool complete = false;
19945
19946 if (TYPE_P (instantiation))
19947 {
19948 tree fn;
19949
19950 if (!COMPLETE_TYPE_P (instantiation))
19951 {
19952 instantiate_class_template (instantiation);
19953 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19954 for (fn = TYPE_METHODS (instantiation);
19955 fn;
19956 fn = TREE_CHAIN (fn))
19957 if (! DECL_ARTIFICIAL (fn))
19958 instantiate_decl (fn,
19959 /*defer_ok=*/0,
19960 /*expl_inst_class_mem_p=*/false);
19961 if (COMPLETE_TYPE_P (instantiation))
19962 reconsider = 1;
19963 }
19964
19965 complete = COMPLETE_TYPE_P (instantiation);
19966 }
19967 else
19968 {
19969 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19970 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19971 {
19972 instantiation
19973 = instantiate_decl (instantiation,
19974 /*defer_ok=*/0,
19975 /*expl_inst_class_mem_p=*/false);
19976 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19977 reconsider = 1;
19978 }
19979
19980 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19981 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19982 }
19983
19984 if (complete)
19985 /* If INSTANTIATION has been instantiated, then we don't
19986 need to consider it again in the future. */
19987 *t = (*t)->next;
19988 else
19989 {
19990 last = *t;
19991 t = &(*t)->next;
19992 }
19993 tinst_depth = 0;
19994 current_tinst_level = NULL;
19995 }
19996 last_pending_template = last;
19997 }
19998 while (reconsider);
19999
20000 input_location = saved_loc;
20001 }
20002
20003 /* Substitute ARGVEC into T, which is a list of initializers for
20004 either base class or a non-static data member. The TREE_PURPOSEs
20005 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20006 instantiate_decl. */
20007
20008 static tree
20009 tsubst_initializer_list (tree t, tree argvec)
20010 {
20011 tree inits = NULL_TREE;
20012
20013 for (; t; t = TREE_CHAIN (t))
20014 {
20015 tree decl;
20016 tree init;
20017 tree expanded_bases = NULL_TREE;
20018 tree expanded_arguments = NULL_TREE;
20019 int i, len = 1;
20020
20021 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20022 {
20023 tree expr;
20024 tree arg;
20025
20026 /* Expand the base class expansion type into separate base
20027 classes. */
20028 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20029 tf_warning_or_error,
20030 NULL_TREE);
20031 if (expanded_bases == error_mark_node)
20032 continue;
20033
20034 /* We'll be building separate TREE_LISTs of arguments for
20035 each base. */
20036 len = TREE_VEC_LENGTH (expanded_bases);
20037 expanded_arguments = make_tree_vec (len);
20038 for (i = 0; i < len; i++)
20039 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20040
20041 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20042 expand each argument in the TREE_VALUE of t. */
20043 expr = make_node (EXPR_PACK_EXPANSION);
20044 PACK_EXPANSION_LOCAL_P (expr) = true;
20045 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20046 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20047
20048 if (TREE_VALUE (t) == void_type_node)
20049 /* VOID_TYPE_NODE is used to indicate
20050 value-initialization. */
20051 {
20052 for (i = 0; i < len; i++)
20053 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20054 }
20055 else
20056 {
20057 /* Substitute parameter packs into each argument in the
20058 TREE_LIST. */
20059 in_base_initializer = 1;
20060 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20061 {
20062 tree expanded_exprs;
20063
20064 /* Expand the argument. */
20065 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20066 expanded_exprs
20067 = tsubst_pack_expansion (expr, argvec,
20068 tf_warning_or_error,
20069 NULL_TREE);
20070 if (expanded_exprs == error_mark_node)
20071 continue;
20072
20073 /* Prepend each of the expanded expressions to the
20074 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20075 for (i = 0; i < len; i++)
20076 {
20077 TREE_VEC_ELT (expanded_arguments, i) =
20078 tree_cons (NULL_TREE,
20079 TREE_VEC_ELT (expanded_exprs, i),
20080 TREE_VEC_ELT (expanded_arguments, i));
20081 }
20082 }
20083 in_base_initializer = 0;
20084
20085 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20086 since we built them backwards. */
20087 for (i = 0; i < len; i++)
20088 {
20089 TREE_VEC_ELT (expanded_arguments, i) =
20090 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20091 }
20092 }
20093 }
20094
20095 for (i = 0; i < len; ++i)
20096 {
20097 if (expanded_bases)
20098 {
20099 decl = TREE_VEC_ELT (expanded_bases, i);
20100 decl = expand_member_init (decl);
20101 init = TREE_VEC_ELT (expanded_arguments, i);
20102 }
20103 else
20104 {
20105 tree tmp;
20106 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20107 tf_warning_or_error, NULL_TREE);
20108
20109 decl = expand_member_init (decl);
20110 if (decl && !DECL_P (decl))
20111 in_base_initializer = 1;
20112
20113 init = TREE_VALUE (t);
20114 tmp = init;
20115 if (init != void_type_node)
20116 init = tsubst_expr (init, argvec,
20117 tf_warning_or_error, NULL_TREE,
20118 /*integral_constant_expression_p=*/false);
20119 if (init == NULL_TREE && tmp != NULL_TREE)
20120 /* If we had an initializer but it instantiated to nothing,
20121 value-initialize the object. This will only occur when
20122 the initializer was a pack expansion where the parameter
20123 packs used in that expansion were of length zero. */
20124 init = void_type_node;
20125 in_base_initializer = 0;
20126 }
20127
20128 if (decl)
20129 {
20130 init = build_tree_list (decl, init);
20131 TREE_CHAIN (init) = inits;
20132 inits = init;
20133 }
20134 }
20135 }
20136 return inits;
20137 }
20138
20139 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20140
20141 static void
20142 set_current_access_from_decl (tree decl)
20143 {
20144 if (TREE_PRIVATE (decl))
20145 current_access_specifier = access_private_node;
20146 else if (TREE_PROTECTED (decl))
20147 current_access_specifier = access_protected_node;
20148 else
20149 current_access_specifier = access_public_node;
20150 }
20151
20152 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20153 is the instantiation (which should have been created with
20154 start_enum) and ARGS are the template arguments to use. */
20155
20156 static void
20157 tsubst_enum (tree tag, tree newtag, tree args)
20158 {
20159 tree e;
20160
20161 if (SCOPED_ENUM_P (newtag))
20162 begin_scope (sk_scoped_enum, newtag);
20163
20164 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20165 {
20166 tree value;
20167 tree decl;
20168
20169 decl = TREE_VALUE (e);
20170 /* Note that in a template enum, the TREE_VALUE is the
20171 CONST_DECL, not the corresponding INTEGER_CST. */
20172 value = tsubst_expr (DECL_INITIAL (decl),
20173 args, tf_warning_or_error, NULL_TREE,
20174 /*integral_constant_expression_p=*/true);
20175
20176 /* Give this enumeration constant the correct access. */
20177 set_current_access_from_decl (decl);
20178
20179 /* Actually build the enumerator itself. */
20180 build_enumerator
20181 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20182 }
20183
20184 if (SCOPED_ENUM_P (newtag))
20185 finish_scope ();
20186
20187 finish_enum_value_list (newtag);
20188 finish_enum (newtag);
20189
20190 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20191 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20192 }
20193
20194 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20195 its type -- but without substituting the innermost set of template
20196 arguments. So, innermost set of template parameters will appear in
20197 the type. */
20198
20199 tree
20200 get_mostly_instantiated_function_type (tree decl)
20201 {
20202 tree fn_type;
20203 tree tmpl;
20204 tree targs;
20205 tree tparms;
20206 int parm_depth;
20207
20208 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20209 targs = DECL_TI_ARGS (decl);
20210 tparms = DECL_TEMPLATE_PARMS (tmpl);
20211 parm_depth = TMPL_PARMS_DEPTH (tparms);
20212
20213 /* There should be as many levels of arguments as there are levels
20214 of parameters. */
20215 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20216
20217 fn_type = TREE_TYPE (tmpl);
20218
20219 if (parm_depth == 1)
20220 /* No substitution is necessary. */
20221 ;
20222 else
20223 {
20224 int i;
20225 tree partial_args;
20226
20227 /* Replace the innermost level of the TARGS with NULL_TREEs to
20228 let tsubst know not to substitute for those parameters. */
20229 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20230 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20231 SET_TMPL_ARGS_LEVEL (partial_args, i,
20232 TMPL_ARGS_LEVEL (targs, i));
20233 SET_TMPL_ARGS_LEVEL (partial_args,
20234 TMPL_ARGS_DEPTH (targs),
20235 make_tree_vec (DECL_NTPARMS (tmpl)));
20236
20237 /* Make sure that we can see identifiers, and compute access
20238 correctly. */
20239 push_access_scope (decl);
20240
20241 ++processing_template_decl;
20242 /* Now, do the (partial) substitution to figure out the
20243 appropriate function type. */
20244 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20245 --processing_template_decl;
20246
20247 /* Substitute into the template parameters to obtain the real
20248 innermost set of parameters. This step is important if the
20249 innermost set of template parameters contains value
20250 parameters whose types depend on outer template parameters. */
20251 TREE_VEC_LENGTH (partial_args)--;
20252 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20253
20254 pop_access_scope (decl);
20255 }
20256
20257 return fn_type;
20258 }
20259
20260 /* Return truthvalue if we're processing a template different from
20261 the last one involved in diagnostics. */
20262 int
20263 problematic_instantiation_changed (void)
20264 {
20265 return current_tinst_level != last_error_tinst_level;
20266 }
20267
20268 /* Remember current template involved in diagnostics. */
20269 void
20270 record_last_problematic_instantiation (void)
20271 {
20272 last_error_tinst_level = current_tinst_level;
20273 }
20274
20275 struct tinst_level *
20276 current_instantiation (void)
20277 {
20278 return current_tinst_level;
20279 }
20280
20281 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20282 type. Return zero for ok, nonzero for disallowed. Issue error and
20283 warning messages under control of COMPLAIN. */
20284
20285 static int
20286 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20287 {
20288 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20289 return 0;
20290 else if (POINTER_TYPE_P (type))
20291 return 0;
20292 else if (TYPE_PTRMEM_P (type))
20293 return 0;
20294 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20295 return 0;
20296 else if (TREE_CODE (type) == TYPENAME_TYPE)
20297 return 0;
20298 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20299 return 0;
20300 else if (TREE_CODE (type) == NULLPTR_TYPE)
20301 return 0;
20302
20303 if (complain & tf_error)
20304 {
20305 if (type == error_mark_node)
20306 inform (input_location, "invalid template non-type parameter");
20307 else
20308 error ("%q#T is not a valid type for a template non-type parameter",
20309 type);
20310 }
20311 return 1;
20312 }
20313
20314 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20315 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20316
20317 static bool
20318 dependent_type_p_r (tree type)
20319 {
20320 tree scope;
20321
20322 /* [temp.dep.type]
20323
20324 A type is dependent if it is:
20325
20326 -- a template parameter. Template template parameters are types
20327 for us (since TYPE_P holds true for them) so we handle
20328 them here. */
20329 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20330 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20331 return true;
20332 /* -- a qualified-id with a nested-name-specifier which contains a
20333 class-name that names a dependent type or whose unqualified-id
20334 names a dependent type. */
20335 if (TREE_CODE (type) == TYPENAME_TYPE)
20336 return true;
20337 /* -- a cv-qualified type where the cv-unqualified type is
20338 dependent. */
20339 type = TYPE_MAIN_VARIANT (type);
20340 /* -- a compound type constructed from any dependent type. */
20341 if (TYPE_PTRMEM_P (type))
20342 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20343 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20344 (type)));
20345 else if (TYPE_PTR_P (type)
20346 || TREE_CODE (type) == REFERENCE_TYPE)
20347 return dependent_type_p (TREE_TYPE (type));
20348 else if (TREE_CODE (type) == FUNCTION_TYPE
20349 || TREE_CODE (type) == METHOD_TYPE)
20350 {
20351 tree arg_type;
20352
20353 if (dependent_type_p (TREE_TYPE (type)))
20354 return true;
20355 for (arg_type = TYPE_ARG_TYPES (type);
20356 arg_type;
20357 arg_type = TREE_CHAIN (arg_type))
20358 if (dependent_type_p (TREE_VALUE (arg_type)))
20359 return true;
20360 return false;
20361 }
20362 /* -- an array type constructed from any dependent type or whose
20363 size is specified by a constant expression that is
20364 value-dependent.
20365
20366 We checked for type- and value-dependence of the bounds in
20367 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20368 if (TREE_CODE (type) == ARRAY_TYPE)
20369 {
20370 if (TYPE_DOMAIN (type)
20371 && dependent_type_p (TYPE_DOMAIN (type)))
20372 return true;
20373 return dependent_type_p (TREE_TYPE (type));
20374 }
20375
20376 /* -- a template-id in which either the template name is a template
20377 parameter ... */
20378 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20379 return true;
20380 /* ... or any of the template arguments is a dependent type or
20381 an expression that is type-dependent or value-dependent. */
20382 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20383 && (any_dependent_template_arguments_p
20384 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20385 return true;
20386
20387 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20388 dependent; if the argument of the `typeof' expression is not
20389 type-dependent, then it should already been have resolved. */
20390 if (TREE_CODE (type) == TYPEOF_TYPE
20391 || TREE_CODE (type) == DECLTYPE_TYPE
20392 || TREE_CODE (type) == UNDERLYING_TYPE)
20393 return true;
20394
20395 /* A template argument pack is dependent if any of its packed
20396 arguments are. */
20397 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20398 {
20399 tree args = ARGUMENT_PACK_ARGS (type);
20400 int i, len = TREE_VEC_LENGTH (args);
20401 for (i = 0; i < len; ++i)
20402 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20403 return true;
20404 }
20405
20406 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20407 be template parameters. */
20408 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20409 return true;
20410
20411 /* The standard does not specifically mention types that are local
20412 to template functions or local classes, but they should be
20413 considered dependent too. For example:
20414
20415 template <int I> void f() {
20416 enum E { a = I };
20417 S<sizeof (E)> s;
20418 }
20419
20420 The size of `E' cannot be known until the value of `I' has been
20421 determined. Therefore, `E' must be considered dependent. */
20422 scope = TYPE_CONTEXT (type);
20423 if (scope && TYPE_P (scope))
20424 return dependent_type_p (scope);
20425 /* Don't use type_dependent_expression_p here, as it can lead
20426 to infinite recursion trying to determine whether a lambda
20427 nested in a lambda is dependent (c++/47687). */
20428 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20429 && DECL_LANG_SPECIFIC (scope)
20430 && DECL_TEMPLATE_INFO (scope)
20431 && (any_dependent_template_arguments_p
20432 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20433 return true;
20434
20435 /* Other types are non-dependent. */
20436 return false;
20437 }
20438
20439 /* Returns TRUE if TYPE is dependent, in the sense of
20440 [temp.dep.type]. Note that a NULL type is considered dependent. */
20441
20442 bool
20443 dependent_type_p (tree type)
20444 {
20445 /* If there are no template parameters in scope, then there can't be
20446 any dependent types. */
20447 if (!processing_template_decl)
20448 {
20449 /* If we are not processing a template, then nobody should be
20450 providing us with a dependent type. */
20451 gcc_assert (type);
20452 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20453 return false;
20454 }
20455
20456 /* If the type is NULL, we have not computed a type for the entity
20457 in question; in that case, the type is dependent. */
20458 if (!type)
20459 return true;
20460
20461 /* Erroneous types can be considered non-dependent. */
20462 if (type == error_mark_node)
20463 return false;
20464
20465 /* If we have not already computed the appropriate value for TYPE,
20466 do so now. */
20467 if (!TYPE_DEPENDENT_P_VALID (type))
20468 {
20469 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20470 TYPE_DEPENDENT_P_VALID (type) = 1;
20471 }
20472
20473 return TYPE_DEPENDENT_P (type);
20474 }
20475
20476 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20477 lookup. In other words, a dependent type that is not the current
20478 instantiation. */
20479
20480 bool
20481 dependent_scope_p (tree scope)
20482 {
20483 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20484 && !currently_open_class (scope));
20485 }
20486
20487 /* T is a SCOPE_REF; return whether we need to consider it
20488 instantiation-dependent so that we can check access at instantiation
20489 time even though we know which member it resolves to. */
20490
20491 static bool
20492 instantiation_dependent_scope_ref_p (tree t)
20493 {
20494 if (DECL_P (TREE_OPERAND (t, 1))
20495 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20496 && accessible_in_template_p (TREE_OPERAND (t, 0),
20497 TREE_OPERAND (t, 1)))
20498 return false;
20499 else
20500 return true;
20501 }
20502
20503 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20504 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20505 expression. */
20506
20507 /* Note that this predicate is not appropriate for general expressions;
20508 only constant expressions (that satisfy potential_constant_expression)
20509 can be tested for value dependence. */
20510
20511 bool
20512 value_dependent_expression_p (tree expression)
20513 {
20514 if (!processing_template_decl)
20515 return false;
20516
20517 /* A name declared with a dependent type. */
20518 if (DECL_P (expression) && type_dependent_expression_p (expression))
20519 return true;
20520
20521 switch (TREE_CODE (expression))
20522 {
20523 case IDENTIFIER_NODE:
20524 /* A name that has not been looked up -- must be dependent. */
20525 return true;
20526
20527 case TEMPLATE_PARM_INDEX:
20528 /* A non-type template parm. */
20529 return true;
20530
20531 case CONST_DECL:
20532 /* A non-type template parm. */
20533 if (DECL_TEMPLATE_PARM_P (expression))
20534 return true;
20535 return value_dependent_expression_p (DECL_INITIAL (expression));
20536
20537 case VAR_DECL:
20538 /* A constant with literal type and is initialized
20539 with an expression that is value-dependent.
20540
20541 Note that a non-dependent parenthesized initializer will have
20542 already been replaced with its constant value, so if we see
20543 a TREE_LIST it must be dependent. */
20544 if (DECL_INITIAL (expression)
20545 && decl_constant_var_p (expression)
20546 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20547 || value_dependent_expression_p (DECL_INITIAL (expression))))
20548 return true;
20549 return false;
20550
20551 case DYNAMIC_CAST_EXPR:
20552 case STATIC_CAST_EXPR:
20553 case CONST_CAST_EXPR:
20554 case REINTERPRET_CAST_EXPR:
20555 case CAST_EXPR:
20556 /* These expressions are value-dependent if the type to which
20557 the cast occurs is dependent or the expression being casted
20558 is value-dependent. */
20559 {
20560 tree type = TREE_TYPE (expression);
20561
20562 if (dependent_type_p (type))
20563 return true;
20564
20565 /* A functional cast has a list of operands. */
20566 expression = TREE_OPERAND (expression, 0);
20567 if (!expression)
20568 {
20569 /* If there are no operands, it must be an expression such
20570 as "int()". This should not happen for aggregate types
20571 because it would form non-constant expressions. */
20572 gcc_assert (cxx_dialect >= cxx11
20573 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20574
20575 return false;
20576 }
20577
20578 if (TREE_CODE (expression) == TREE_LIST)
20579 return any_value_dependent_elements_p (expression);
20580
20581 return value_dependent_expression_p (expression);
20582 }
20583
20584 case SIZEOF_EXPR:
20585 if (SIZEOF_EXPR_TYPE_P (expression))
20586 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20587 /* FALLTHRU */
20588 case ALIGNOF_EXPR:
20589 case TYPEID_EXPR:
20590 /* A `sizeof' expression is value-dependent if the operand is
20591 type-dependent or is a pack expansion. */
20592 expression = TREE_OPERAND (expression, 0);
20593 if (PACK_EXPANSION_P (expression))
20594 return true;
20595 else if (TYPE_P (expression))
20596 return dependent_type_p (expression);
20597 return instantiation_dependent_expression_p (expression);
20598
20599 case AT_ENCODE_EXPR:
20600 /* An 'encode' expression is value-dependent if the operand is
20601 type-dependent. */
20602 expression = TREE_OPERAND (expression, 0);
20603 return dependent_type_p (expression);
20604
20605 case NOEXCEPT_EXPR:
20606 expression = TREE_OPERAND (expression, 0);
20607 return instantiation_dependent_expression_p (expression);
20608
20609 case SCOPE_REF:
20610 /* All instantiation-dependent expressions should also be considered
20611 value-dependent. */
20612 return instantiation_dependent_scope_ref_p (expression);
20613
20614 case COMPONENT_REF:
20615 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20616 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20617
20618 case NONTYPE_ARGUMENT_PACK:
20619 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20620 is value-dependent. */
20621 {
20622 tree values = ARGUMENT_PACK_ARGS (expression);
20623 int i, len = TREE_VEC_LENGTH (values);
20624
20625 for (i = 0; i < len; ++i)
20626 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20627 return true;
20628
20629 return false;
20630 }
20631
20632 case TRAIT_EXPR:
20633 {
20634 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20635 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20636 || (type2 ? dependent_type_p (type2) : false));
20637 }
20638
20639 case MODOP_EXPR:
20640 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20641 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20642
20643 case ARRAY_REF:
20644 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20645 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20646
20647 case ADDR_EXPR:
20648 {
20649 tree op = TREE_OPERAND (expression, 0);
20650 return (value_dependent_expression_p (op)
20651 || has_value_dependent_address (op));
20652 }
20653
20654 case CALL_EXPR:
20655 {
20656 tree fn = get_callee_fndecl (expression);
20657 int i, nargs;
20658 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20659 return true;
20660 nargs = call_expr_nargs (expression);
20661 for (i = 0; i < nargs; ++i)
20662 {
20663 tree op = CALL_EXPR_ARG (expression, i);
20664 /* In a call to a constexpr member function, look through the
20665 implicit ADDR_EXPR on the object argument so that it doesn't
20666 cause the call to be considered value-dependent. We also
20667 look through it in potential_constant_expression. */
20668 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20670 && TREE_CODE (op) == ADDR_EXPR)
20671 op = TREE_OPERAND (op, 0);
20672 if (value_dependent_expression_p (op))
20673 return true;
20674 }
20675 return false;
20676 }
20677
20678 case TEMPLATE_ID_EXPR:
20679 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20680 type-dependent. */
20681 return type_dependent_expression_p (expression);
20682
20683 case CONSTRUCTOR:
20684 {
20685 unsigned ix;
20686 tree val;
20687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20688 if (value_dependent_expression_p (val))
20689 return true;
20690 return false;
20691 }
20692
20693 case STMT_EXPR:
20694 /* Treat a GNU statement expression as dependent to avoid crashing
20695 under fold_non_dependent_expr; it can't be constant. */
20696 return true;
20697
20698 default:
20699 /* A constant expression is value-dependent if any subexpression is
20700 value-dependent. */
20701 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20702 {
20703 case tcc_reference:
20704 case tcc_unary:
20705 case tcc_comparison:
20706 case tcc_binary:
20707 case tcc_expression:
20708 case tcc_vl_exp:
20709 {
20710 int i, len = cp_tree_operand_length (expression);
20711
20712 for (i = 0; i < len; i++)
20713 {
20714 tree t = TREE_OPERAND (expression, i);
20715
20716 /* In some cases, some of the operands may be missing.l
20717 (For example, in the case of PREDECREMENT_EXPR, the
20718 amount to increment by may be missing.) That doesn't
20719 make the expression dependent. */
20720 if (t && value_dependent_expression_p (t))
20721 return true;
20722 }
20723 }
20724 break;
20725 default:
20726 break;
20727 }
20728 break;
20729 }
20730
20731 /* The expression is not value-dependent. */
20732 return false;
20733 }
20734
20735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20736 [temp.dep.expr]. Note that an expression with no type is
20737 considered dependent. Other parts of the compiler arrange for an
20738 expression with type-dependent subexpressions to have no type, so
20739 this function doesn't have to be fully recursive. */
20740
20741 bool
20742 type_dependent_expression_p (tree expression)
20743 {
20744 if (!processing_template_decl)
20745 return false;
20746
20747 if (expression == NULL_TREE || expression == error_mark_node)
20748 return false;
20749
20750 /* An unresolved name is always dependent. */
20751 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20752 return true;
20753
20754 /* Some expression forms are never type-dependent. */
20755 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20756 || TREE_CODE (expression) == SIZEOF_EXPR
20757 || TREE_CODE (expression) == ALIGNOF_EXPR
20758 || TREE_CODE (expression) == AT_ENCODE_EXPR
20759 || TREE_CODE (expression) == NOEXCEPT_EXPR
20760 || TREE_CODE (expression) == TRAIT_EXPR
20761 || TREE_CODE (expression) == TYPEID_EXPR
20762 || TREE_CODE (expression) == DELETE_EXPR
20763 || TREE_CODE (expression) == VEC_DELETE_EXPR
20764 || TREE_CODE (expression) == THROW_EXPR)
20765 return false;
20766
20767 /* The types of these expressions depends only on the type to which
20768 the cast occurs. */
20769 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20770 || TREE_CODE (expression) == STATIC_CAST_EXPR
20771 || TREE_CODE (expression) == CONST_CAST_EXPR
20772 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20773 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20774 || TREE_CODE (expression) == CAST_EXPR)
20775 return dependent_type_p (TREE_TYPE (expression));
20776
20777 /* The types of these expressions depends only on the type created
20778 by the expression. */
20779 if (TREE_CODE (expression) == NEW_EXPR
20780 || TREE_CODE (expression) == VEC_NEW_EXPR)
20781 {
20782 /* For NEW_EXPR tree nodes created inside a template, either
20783 the object type itself or a TREE_LIST may appear as the
20784 operand 1. */
20785 tree type = TREE_OPERAND (expression, 1);
20786 if (TREE_CODE (type) == TREE_LIST)
20787 /* This is an array type. We need to check array dimensions
20788 as well. */
20789 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20790 || value_dependent_expression_p
20791 (TREE_OPERAND (TREE_VALUE (type), 1));
20792 else
20793 return dependent_type_p (type);
20794 }
20795
20796 if (TREE_CODE (expression) == SCOPE_REF)
20797 {
20798 tree scope = TREE_OPERAND (expression, 0);
20799 tree name = TREE_OPERAND (expression, 1);
20800
20801 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20802 contains an identifier associated by name lookup with one or more
20803 declarations declared with a dependent type, or...a
20804 nested-name-specifier or qualified-id that names a member of an
20805 unknown specialization. */
20806 return (type_dependent_expression_p (name)
20807 || dependent_scope_p (scope));
20808 }
20809
20810 if (TREE_CODE (expression) == FUNCTION_DECL
20811 && DECL_LANG_SPECIFIC (expression)
20812 && DECL_TEMPLATE_INFO (expression)
20813 && (any_dependent_template_arguments_p
20814 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20815 return true;
20816
20817 if (TREE_CODE (expression) == TEMPLATE_DECL
20818 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20819 return false;
20820
20821 if (TREE_CODE (expression) == STMT_EXPR)
20822 expression = stmt_expr_value_expr (expression);
20823
20824 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20825 {
20826 tree elt;
20827 unsigned i;
20828
20829 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20830 {
20831 if (type_dependent_expression_p (elt))
20832 return true;
20833 }
20834 return false;
20835 }
20836
20837 /* A static data member of the current instantiation with incomplete
20838 array type is type-dependent, as the definition and specializations
20839 can have different bounds. */
20840 if (VAR_P (expression)
20841 && DECL_CLASS_SCOPE_P (expression)
20842 && dependent_type_p (DECL_CONTEXT (expression))
20843 && VAR_HAD_UNKNOWN_BOUND (expression))
20844 return true;
20845
20846 /* An array of unknown bound depending on a variadic parameter, eg:
20847
20848 template<typename... Args>
20849 void foo (Args... args)
20850 {
20851 int arr[] = { args... };
20852 }
20853
20854 template<int... vals>
20855 void bar ()
20856 {
20857 int arr[] = { vals... };
20858 }
20859
20860 If the array has no length and has an initializer, it must be that
20861 we couldn't determine its length in cp_complete_array_type because
20862 it is dependent. */
20863 if (VAR_P (expression)
20864 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20865 && !TYPE_DOMAIN (TREE_TYPE (expression))
20866 && DECL_INITIAL (expression))
20867 return true;
20868
20869 if (TREE_TYPE (expression) == unknown_type_node)
20870 {
20871 if (TREE_CODE (expression) == ADDR_EXPR)
20872 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20873 if (TREE_CODE (expression) == COMPONENT_REF
20874 || TREE_CODE (expression) == OFFSET_REF)
20875 {
20876 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20877 return true;
20878 expression = TREE_OPERAND (expression, 1);
20879 if (identifier_p (expression))
20880 return false;
20881 }
20882 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20883 if (TREE_CODE (expression) == SCOPE_REF)
20884 return false;
20885
20886 /* Always dependent, on the number of arguments if nothing else. */
20887 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20888 return true;
20889
20890 if (BASELINK_P (expression))
20891 expression = BASELINK_FUNCTIONS (expression);
20892
20893 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20894 {
20895 if (any_dependent_template_arguments_p
20896 (TREE_OPERAND (expression, 1)))
20897 return true;
20898 expression = TREE_OPERAND (expression, 0);
20899 }
20900 gcc_assert (TREE_CODE (expression) == OVERLOAD
20901 || TREE_CODE (expression) == FUNCTION_DECL);
20902
20903 while (expression)
20904 {
20905 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20906 return true;
20907 expression = OVL_NEXT (expression);
20908 }
20909 return false;
20910 }
20911
20912 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20913
20914 return (dependent_type_p (TREE_TYPE (expression)));
20915 }
20916
20917 /* walk_tree callback function for instantiation_dependent_expression_p,
20918 below. Returns non-zero if a dependent subexpression is found. */
20919
20920 static tree
20921 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20922 void * /*data*/)
20923 {
20924 if (TYPE_P (*tp))
20925 {
20926 /* We don't have to worry about decltype currently because decltype
20927 of an instantiation-dependent expr is a dependent type. This
20928 might change depending on the resolution of DR 1172. */
20929 *walk_subtrees = false;
20930 return NULL_TREE;
20931 }
20932 enum tree_code code = TREE_CODE (*tp);
20933 switch (code)
20934 {
20935 /* Don't treat an argument list as dependent just because it has no
20936 TREE_TYPE. */
20937 case TREE_LIST:
20938 case TREE_VEC:
20939 return NULL_TREE;
20940
20941 case VAR_DECL:
20942 case CONST_DECL:
20943 /* A constant with a dependent initializer is dependent. */
20944 if (value_dependent_expression_p (*tp))
20945 return *tp;
20946 break;
20947
20948 case TEMPLATE_PARM_INDEX:
20949 return *tp;
20950
20951 /* Handle expressions with type operands. */
20952 case SIZEOF_EXPR:
20953 case ALIGNOF_EXPR:
20954 case TYPEID_EXPR:
20955 case AT_ENCODE_EXPR:
20956 {
20957 tree op = TREE_OPERAND (*tp, 0);
20958 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20959 op = TREE_TYPE (op);
20960 if (TYPE_P (op))
20961 {
20962 if (dependent_type_p (op))
20963 return *tp;
20964 else
20965 {
20966 *walk_subtrees = false;
20967 return NULL_TREE;
20968 }
20969 }
20970 break;
20971 }
20972
20973 case TRAIT_EXPR:
20974 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20975 || (TRAIT_EXPR_TYPE2 (*tp)
20976 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20977 return *tp;
20978 *walk_subtrees = false;
20979 return NULL_TREE;
20980
20981 case COMPONENT_REF:
20982 if (identifier_p (TREE_OPERAND (*tp, 1)))
20983 /* In a template, finish_class_member_access_expr creates a
20984 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20985 type-dependent, so that we can check access control at
20986 instantiation time (PR 42277). See also Core issue 1273. */
20987 return *tp;
20988 break;
20989
20990 case SCOPE_REF:
20991 if (instantiation_dependent_scope_ref_p (*tp))
20992 return *tp;
20993 else
20994 break;
20995
20996 /* Treat statement-expressions as dependent. */
20997 case BIND_EXPR:
20998 return *tp;
20999
21000 default:
21001 break;
21002 }
21003
21004 if (type_dependent_expression_p (*tp))
21005 return *tp;
21006 else
21007 return NULL_TREE;
21008 }
21009
21010 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21011 sense defined by the ABI:
21012
21013 "An expression is instantiation-dependent if it is type-dependent
21014 or value-dependent, or it has a subexpression that is type-dependent
21015 or value-dependent." */
21016
21017 bool
21018 instantiation_dependent_expression_p (tree expression)
21019 {
21020 tree result;
21021
21022 if (!processing_template_decl)
21023 return false;
21024
21025 if (expression == error_mark_node)
21026 return false;
21027
21028 result = cp_walk_tree_without_duplicates (&expression,
21029 instantiation_dependent_r, NULL);
21030 return result != NULL_TREE;
21031 }
21032
21033 /* Like type_dependent_expression_p, but it also works while not processing
21034 a template definition, i.e. during substitution or mangling. */
21035
21036 bool
21037 type_dependent_expression_p_push (tree expr)
21038 {
21039 bool b;
21040 ++processing_template_decl;
21041 b = type_dependent_expression_p (expr);
21042 --processing_template_decl;
21043 return b;
21044 }
21045
21046 /* Returns TRUE if ARGS contains a type-dependent expression. */
21047
21048 bool
21049 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21050 {
21051 unsigned int i;
21052 tree arg;
21053
21054 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21055 {
21056 if (type_dependent_expression_p (arg))
21057 return true;
21058 }
21059 return false;
21060 }
21061
21062 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21063 expressions) contains any type-dependent expressions. */
21064
21065 bool
21066 any_type_dependent_elements_p (const_tree list)
21067 {
21068 for (; list; list = TREE_CHAIN (list))
21069 if (type_dependent_expression_p (TREE_VALUE (list)))
21070 return true;
21071
21072 return false;
21073 }
21074
21075 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21076 expressions) contains any value-dependent expressions. */
21077
21078 bool
21079 any_value_dependent_elements_p (const_tree list)
21080 {
21081 for (; list; list = TREE_CHAIN (list))
21082 if (value_dependent_expression_p (TREE_VALUE (list)))
21083 return true;
21084
21085 return false;
21086 }
21087
21088 /* Returns TRUE if the ARG (a template argument) is dependent. */
21089
21090 bool
21091 dependent_template_arg_p (tree arg)
21092 {
21093 if (!processing_template_decl)
21094 return false;
21095
21096 /* Assume a template argument that was wrongly written by the user
21097 is dependent. This is consistent with what
21098 any_dependent_template_arguments_p [that calls this function]
21099 does. */
21100 if (!arg || arg == error_mark_node)
21101 return true;
21102
21103 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21104 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21105
21106 if (TREE_CODE (arg) == TEMPLATE_DECL
21107 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21108 return dependent_template_p (arg);
21109 else if (ARGUMENT_PACK_P (arg))
21110 {
21111 tree args = ARGUMENT_PACK_ARGS (arg);
21112 int i, len = TREE_VEC_LENGTH (args);
21113 for (i = 0; i < len; ++i)
21114 {
21115 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21116 return true;
21117 }
21118
21119 return false;
21120 }
21121 else if (TYPE_P (arg))
21122 return dependent_type_p (arg);
21123 else
21124 return (type_dependent_expression_p (arg)
21125 || value_dependent_expression_p (arg));
21126 }
21127
21128 /* Returns true if ARGS (a collection of template arguments) contains
21129 any types that require structural equality testing. */
21130
21131 bool
21132 any_template_arguments_need_structural_equality_p (tree args)
21133 {
21134 int i;
21135 int j;
21136
21137 if (!args)
21138 return false;
21139 if (args == error_mark_node)
21140 return true;
21141
21142 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21143 {
21144 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21145 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21146 {
21147 tree arg = TREE_VEC_ELT (level, j);
21148 tree packed_args = NULL_TREE;
21149 int k, len = 1;
21150
21151 if (ARGUMENT_PACK_P (arg))
21152 {
21153 /* Look inside the argument pack. */
21154 packed_args = ARGUMENT_PACK_ARGS (arg);
21155 len = TREE_VEC_LENGTH (packed_args);
21156 }
21157
21158 for (k = 0; k < len; ++k)
21159 {
21160 if (packed_args)
21161 arg = TREE_VEC_ELT (packed_args, k);
21162
21163 if (error_operand_p (arg))
21164 return true;
21165 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21166 continue;
21167 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21168 return true;
21169 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21170 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21171 return true;
21172 }
21173 }
21174 }
21175
21176 return false;
21177 }
21178
21179 /* Returns true if ARGS (a collection of template arguments) contains
21180 any dependent arguments. */
21181
21182 bool
21183 any_dependent_template_arguments_p (const_tree args)
21184 {
21185 int i;
21186 int j;
21187
21188 if (!args)
21189 return false;
21190 if (args == error_mark_node)
21191 return true;
21192
21193 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21194 {
21195 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21196 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21197 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21198 return true;
21199 }
21200
21201 return false;
21202 }
21203
21204 /* Returns TRUE if the template TMPL is dependent. */
21205
21206 bool
21207 dependent_template_p (tree tmpl)
21208 {
21209 if (TREE_CODE (tmpl) == OVERLOAD)
21210 {
21211 while (tmpl)
21212 {
21213 if (dependent_template_p (OVL_CURRENT (tmpl)))
21214 return true;
21215 tmpl = OVL_NEXT (tmpl);
21216 }
21217 return false;
21218 }
21219
21220 /* Template template parameters are dependent. */
21221 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21222 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21223 return true;
21224 /* So are names that have not been looked up. */
21225 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21226 return true;
21227 /* So are member templates of dependent classes. */
21228 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21229 return dependent_type_p (DECL_CONTEXT (tmpl));
21230 return false;
21231 }
21232
21233 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21234
21235 bool
21236 dependent_template_id_p (tree tmpl, tree args)
21237 {
21238 return (dependent_template_p (tmpl)
21239 || any_dependent_template_arguments_p (args));
21240 }
21241
21242 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21243 is dependent. */
21244
21245 bool
21246 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21247 {
21248 int i;
21249
21250 if (!processing_template_decl)
21251 return false;
21252
21253 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21254 {
21255 tree decl = TREE_VEC_ELT (declv, i);
21256 tree init = TREE_VEC_ELT (initv, i);
21257 tree cond = TREE_VEC_ELT (condv, i);
21258 tree incr = TREE_VEC_ELT (incrv, i);
21259
21260 if (type_dependent_expression_p (decl))
21261 return true;
21262
21263 if (init && type_dependent_expression_p (init))
21264 return true;
21265
21266 if (type_dependent_expression_p (cond))
21267 return true;
21268
21269 if (COMPARISON_CLASS_P (cond)
21270 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21271 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21272 return true;
21273
21274 if (TREE_CODE (incr) == MODOP_EXPR)
21275 {
21276 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21277 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21278 return true;
21279 }
21280 else if (type_dependent_expression_p (incr))
21281 return true;
21282 else if (TREE_CODE (incr) == MODIFY_EXPR)
21283 {
21284 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21285 return true;
21286 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21287 {
21288 tree t = TREE_OPERAND (incr, 1);
21289 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21290 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21291 return true;
21292 }
21293 }
21294 }
21295
21296 return false;
21297 }
21298
21299 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21300 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21301 no such TYPE can be found. Note that this function peers inside
21302 uninstantiated templates and therefore should be used only in
21303 extremely limited situations. ONLY_CURRENT_P restricts this
21304 peering to the currently open classes hierarchy (which is required
21305 when comparing types). */
21306
21307 tree
21308 resolve_typename_type (tree type, bool only_current_p)
21309 {
21310 tree scope;
21311 tree name;
21312 tree decl;
21313 int quals;
21314 tree pushed_scope;
21315 tree result;
21316
21317 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21318
21319 scope = TYPE_CONTEXT (type);
21320 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21321 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21322 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21323 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21324 identifier of the TYPENAME_TYPE anymore.
21325 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21326 TYPENAME_TYPE instead, we avoid messing up with a possible
21327 typedef variant case. */
21328 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21329
21330 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21331 it first before we can figure out what NAME refers to. */
21332 if (TREE_CODE (scope) == TYPENAME_TYPE)
21333 {
21334 if (TYPENAME_IS_RESOLVING_P (scope))
21335 /* Given a class template A with a dependent base with nested type C,
21336 typedef typename A::C::C C will land us here, as trying to resolve
21337 the initial A::C leads to the local C typedef, which leads back to
21338 A::C::C. So we break the recursion now. */
21339 return type;
21340 else
21341 scope = resolve_typename_type (scope, only_current_p);
21342 }
21343 /* If we don't know what SCOPE refers to, then we cannot resolve the
21344 TYPENAME_TYPE. */
21345 if (TREE_CODE (scope) == TYPENAME_TYPE)
21346 return type;
21347 /* If the SCOPE is a template type parameter, we have no way of
21348 resolving the name. */
21349 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21350 return type;
21351 /* If the SCOPE is not the current instantiation, there's no reason
21352 to look inside it. */
21353 if (only_current_p && !currently_open_class (scope))
21354 return type;
21355 /* If this is a typedef, we don't want to look inside (c++/11987). */
21356 if (typedef_variant_p (type))
21357 return type;
21358 /* If SCOPE isn't the template itself, it will not have a valid
21359 TYPE_FIELDS list. */
21360 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21361 /* scope is either the template itself or a compatible instantiation
21362 like X<T>, so look up the name in the original template. */
21363 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21364 else
21365 /* scope is a partial instantiation, so we can't do the lookup or we
21366 will lose the template arguments. */
21367 return type;
21368 /* Enter the SCOPE so that name lookup will be resolved as if we
21369 were in the class definition. In particular, SCOPE will no
21370 longer be considered a dependent type. */
21371 pushed_scope = push_scope (scope);
21372 /* Look up the declaration. */
21373 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21374 tf_warning_or_error);
21375
21376 result = NULL_TREE;
21377
21378 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21379 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21380 if (!decl)
21381 /*nop*/;
21382 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21383 && TREE_CODE (decl) == TYPE_DECL)
21384 {
21385 result = TREE_TYPE (decl);
21386 if (result == error_mark_node)
21387 result = NULL_TREE;
21388 }
21389 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21390 && DECL_CLASS_TEMPLATE_P (decl))
21391 {
21392 tree tmpl;
21393 tree args;
21394 /* Obtain the template and the arguments. */
21395 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21396 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21397 /* Instantiate the template. */
21398 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21399 /*entering_scope=*/0,
21400 tf_error | tf_user);
21401 if (result == error_mark_node)
21402 result = NULL_TREE;
21403 }
21404
21405 /* Leave the SCOPE. */
21406 if (pushed_scope)
21407 pop_scope (pushed_scope);
21408
21409 /* If we failed to resolve it, return the original typename. */
21410 if (!result)
21411 return type;
21412
21413 /* If lookup found a typename type, resolve that too. */
21414 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21415 {
21416 /* Ill-formed programs can cause infinite recursion here, so we
21417 must catch that. */
21418 TYPENAME_IS_RESOLVING_P (type) = 1;
21419 result = resolve_typename_type (result, only_current_p);
21420 TYPENAME_IS_RESOLVING_P (type) = 0;
21421 }
21422
21423 /* Qualify the resulting type. */
21424 quals = cp_type_quals (type);
21425 if (quals)
21426 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21427
21428 return result;
21429 }
21430
21431 /* EXPR is an expression which is not type-dependent. Return a proxy
21432 for EXPR that can be used to compute the types of larger
21433 expressions containing EXPR. */
21434
21435 tree
21436 build_non_dependent_expr (tree expr)
21437 {
21438 tree inner_expr;
21439
21440 #ifdef ENABLE_CHECKING
21441 /* Try to get a constant value for all non-dependent expressions in
21442 order to expose bugs in *_dependent_expression_p and constexpr. */
21443 if (cxx_dialect >= cxx11)
21444 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21445 #endif
21446
21447 /* Preserve OVERLOADs; the functions must be available to resolve
21448 types. */
21449 inner_expr = expr;
21450 if (TREE_CODE (inner_expr) == STMT_EXPR)
21451 inner_expr = stmt_expr_value_expr (inner_expr);
21452 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21453 inner_expr = TREE_OPERAND (inner_expr, 0);
21454 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21455 inner_expr = TREE_OPERAND (inner_expr, 1);
21456 if (is_overloaded_fn (inner_expr)
21457 || TREE_CODE (inner_expr) == OFFSET_REF)
21458 return expr;
21459 /* There is no need to return a proxy for a variable. */
21460 if (VAR_P (expr))
21461 return expr;
21462 /* Preserve string constants; conversions from string constants to
21463 "char *" are allowed, even though normally a "const char *"
21464 cannot be used to initialize a "char *". */
21465 if (TREE_CODE (expr) == STRING_CST)
21466 return expr;
21467 /* Preserve arithmetic constants, as an optimization -- there is no
21468 reason to create a new node. */
21469 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21470 return expr;
21471 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21472 There is at least one place where we want to know that a
21473 particular expression is a throw-expression: when checking a ?:
21474 expression, there are special rules if the second or third
21475 argument is a throw-expression. */
21476 if (TREE_CODE (expr) == THROW_EXPR)
21477 return expr;
21478
21479 /* Don't wrap an initializer list, we need to be able to look inside. */
21480 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21481 return expr;
21482
21483 /* Don't wrap a dummy object, we need to be able to test for it. */
21484 if (is_dummy_object (expr))
21485 return expr;
21486
21487 if (TREE_CODE (expr) == COND_EXPR)
21488 return build3 (COND_EXPR,
21489 TREE_TYPE (expr),
21490 TREE_OPERAND (expr, 0),
21491 (TREE_OPERAND (expr, 1)
21492 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21493 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21494 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21495 if (TREE_CODE (expr) == COMPOUND_EXPR
21496 && !COMPOUND_EXPR_OVERLOADED (expr))
21497 return build2 (COMPOUND_EXPR,
21498 TREE_TYPE (expr),
21499 TREE_OPERAND (expr, 0),
21500 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21501
21502 /* If the type is unknown, it can't really be non-dependent */
21503 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21504
21505 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21506 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21507 }
21508
21509 /* ARGS is a vector of expressions as arguments to a function call.
21510 Replace the arguments with equivalent non-dependent expressions.
21511 This modifies ARGS in place. */
21512
21513 void
21514 make_args_non_dependent (vec<tree, va_gc> *args)
21515 {
21516 unsigned int ix;
21517 tree arg;
21518
21519 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21520 {
21521 tree newarg = build_non_dependent_expr (arg);
21522 if (newarg != arg)
21523 (*args)[ix] = newarg;
21524 }
21525 }
21526
21527 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21528 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21529 parms. */
21530
21531 static tree
21532 make_auto_1 (tree name)
21533 {
21534 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21535 TYPE_NAME (au) = build_decl (input_location,
21536 TYPE_DECL, name, au);
21537 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21538 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21539 (0, processing_template_decl + 1, processing_template_decl + 1,
21540 TYPE_NAME (au), NULL_TREE);
21541 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21542 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21543 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21544
21545 return au;
21546 }
21547
21548 tree
21549 make_decltype_auto (void)
21550 {
21551 return make_auto_1 (get_identifier ("decltype(auto)"));
21552 }
21553
21554 tree
21555 make_auto (void)
21556 {
21557 return make_auto_1 (get_identifier ("auto"));
21558 }
21559
21560 /* Given type ARG, return std::initializer_list<ARG>. */
21561
21562 static tree
21563 listify (tree arg)
21564 {
21565 tree std_init_list = namespace_binding
21566 (get_identifier ("initializer_list"), std_node);
21567 tree argvec;
21568 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21569 {
21570 error ("deducing from brace-enclosed initializer list requires "
21571 "#include <initializer_list>");
21572 return error_mark_node;
21573 }
21574 argvec = make_tree_vec (1);
21575 TREE_VEC_ELT (argvec, 0) = arg;
21576 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21577 NULL_TREE, 0, tf_warning_or_error);
21578 }
21579
21580 /* Replace auto in TYPE with std::initializer_list<auto>. */
21581
21582 static tree
21583 listify_autos (tree type, tree auto_node)
21584 {
21585 tree init_auto = listify (auto_node);
21586 tree argvec = make_tree_vec (1);
21587 TREE_VEC_ELT (argvec, 0) = init_auto;
21588 if (processing_template_decl)
21589 argvec = add_to_template_args (current_template_args (), argvec);
21590 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21591 }
21592
21593 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21594 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21595
21596 tree
21597 do_auto_deduction (tree type, tree init, tree auto_node)
21598 {
21599 tree targs;
21600
21601 if (init == error_mark_node)
21602 return error_mark_node;
21603
21604 if (type_dependent_expression_p (init))
21605 /* Defining a subset of type-dependent expressions that we can deduce
21606 from ahead of time isn't worth the trouble. */
21607 return type;
21608
21609 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21610 with either a new invented type template parameter U or, if the
21611 initializer is a braced-init-list (8.5.4), with
21612 std::initializer_list<U>. */
21613 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21614 type = listify_autos (type, auto_node);
21615
21616 init = resolve_nondeduced_context (init);
21617
21618 targs = make_tree_vec (1);
21619 if (AUTO_IS_DECLTYPE (auto_node))
21620 {
21621 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
21622 && !REF_PARENTHESIZED_P (init)));
21623 TREE_VEC_ELT (targs, 0)
21624 = finish_decltype_type (init, id, tf_warning_or_error);
21625 if (type != auto_node)
21626 {
21627 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21628 return error_mark_node;
21629 }
21630 }
21631 else
21632 {
21633 tree parms = build_tree_list (NULL_TREE, type);
21634 tree tparms = make_tree_vec (1);
21635 int val;
21636
21637 TREE_VEC_ELT (tparms, 0)
21638 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21639 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21640 DEDUCE_CALL, LOOKUP_NORMAL,
21641 NULL, /*explain_p=*/false);
21642 if (val > 0)
21643 {
21644 if (processing_template_decl)
21645 /* Try again at instantiation time. */
21646 return type;
21647 if (type && type != error_mark_node)
21648 /* If type is error_mark_node a diagnostic must have been
21649 emitted by now. Also, having a mention to '<type error>'
21650 in the diagnostic is not really useful to the user. */
21651 {
21652 if (cfun && auto_node == current_function_auto_return_pattern
21653 && LAMBDA_FUNCTION_P (current_function_decl))
21654 error ("unable to deduce lambda return type from %qE", init);
21655 else
21656 error ("unable to deduce %qT from %qE", type, init);
21657 }
21658 return error_mark_node;
21659 }
21660 }
21661
21662 /* If the list of declarators contains more than one declarator, the type
21663 of each declared variable is determined as described above. If the
21664 type deduced for the template parameter U is not the same in each
21665 deduction, the program is ill-formed. */
21666 if (TREE_TYPE (auto_node)
21667 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21668 {
21669 if (cfun && auto_node == current_function_auto_return_pattern
21670 && LAMBDA_FUNCTION_P (current_function_decl))
21671 error ("inconsistent types %qT and %qT deduced for "
21672 "lambda return type", TREE_TYPE (auto_node),
21673 TREE_VEC_ELT (targs, 0));
21674 else
21675 error ("inconsistent deduction for %qT: %qT and then %qT",
21676 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21677 return error_mark_node;
21678 }
21679 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21680
21681 if (processing_template_decl)
21682 targs = add_to_template_args (current_template_args (), targs);
21683 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21684 }
21685
21686 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21687 result. */
21688
21689 tree
21690 splice_late_return_type (tree type, tree late_return_type)
21691 {
21692 tree argvec;
21693
21694 if (late_return_type == NULL_TREE)
21695 return type;
21696 argvec = make_tree_vec (1);
21697 TREE_VEC_ELT (argvec, 0) = late_return_type;
21698 if (processing_template_parmlist)
21699 /* For a late-specified return type in a template type-parameter, we
21700 need to add a dummy argument level for its parmlist. */
21701 argvec = add_to_template_args
21702 (make_tree_vec (processing_template_parmlist), argvec);
21703 if (current_template_parms)
21704 argvec = add_to_template_args (current_template_args (), argvec);
21705 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21706 }
21707
21708 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21709 'decltype(auto)'. */
21710
21711 bool
21712 is_auto (const_tree type)
21713 {
21714 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21715 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21716 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21717 return true;
21718 else
21719 return false;
21720 }
21721
21722 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21723 a use of `auto'. Returns NULL_TREE otherwise. */
21724
21725 tree
21726 type_uses_auto (tree type)
21727 {
21728 return find_type_usage (type, is_auto);
21729 }
21730
21731 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21732 'decltype(auto)' or a concept. */
21733
21734 bool
21735 is_auto_or_concept (const_tree type)
21736 {
21737 return is_auto (type); // or concept
21738 }
21739
21740 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21741 a concept identifier) iff TYPE contains a use of a generic type. Returns
21742 NULL_TREE otherwise. */
21743
21744 tree
21745 type_uses_auto_or_concept (tree type)
21746 {
21747 return find_type_usage (type, is_auto_or_concept);
21748 }
21749
21750
21751 /* For a given template T, return the vector of typedefs referenced
21752 in T for which access check is needed at T instantiation time.
21753 T is either a FUNCTION_DECL or a RECORD_TYPE.
21754 Those typedefs were added to T by the function
21755 append_type_to_template_for_access_check. */
21756
21757 vec<qualified_typedef_usage_t, va_gc> *
21758 get_types_needing_access_check (tree t)
21759 {
21760 tree ti;
21761 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21762
21763 if (!t || t == error_mark_node)
21764 return NULL;
21765
21766 if (!(ti = get_template_info (t)))
21767 return NULL;
21768
21769 if (CLASS_TYPE_P (t)
21770 || TREE_CODE (t) == FUNCTION_DECL)
21771 {
21772 if (!TI_TEMPLATE (ti))
21773 return NULL;
21774
21775 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21776 }
21777
21778 return result;
21779 }
21780
21781 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21782 tied to T. That list of typedefs will be access checked at
21783 T instantiation time.
21784 T is either a FUNCTION_DECL or a RECORD_TYPE.
21785 TYPE_DECL is a TYPE_DECL node representing a typedef.
21786 SCOPE is the scope through which TYPE_DECL is accessed.
21787 LOCATION is the location of the usage point of TYPE_DECL.
21788
21789 This function is a subroutine of
21790 append_type_to_template_for_access_check. */
21791
21792 static void
21793 append_type_to_template_for_access_check_1 (tree t,
21794 tree type_decl,
21795 tree scope,
21796 location_t location)
21797 {
21798 qualified_typedef_usage_t typedef_usage;
21799 tree ti;
21800
21801 if (!t || t == error_mark_node)
21802 return;
21803
21804 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21805 || CLASS_TYPE_P (t))
21806 && type_decl
21807 && TREE_CODE (type_decl) == TYPE_DECL
21808 && scope);
21809
21810 if (!(ti = get_template_info (t)))
21811 return;
21812
21813 gcc_assert (TI_TEMPLATE (ti));
21814
21815 typedef_usage.typedef_decl = type_decl;
21816 typedef_usage.context = scope;
21817 typedef_usage.locus = location;
21818
21819 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21820 }
21821
21822 /* Append TYPE_DECL to the template TEMPL.
21823 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21824 At TEMPL instanciation time, TYPE_DECL will be checked to see
21825 if it can be accessed through SCOPE.
21826 LOCATION is the location of the usage point of TYPE_DECL.
21827
21828 e.g. consider the following code snippet:
21829
21830 class C
21831 {
21832 typedef int myint;
21833 };
21834
21835 template<class U> struct S
21836 {
21837 C::myint mi; // <-- usage point of the typedef C::myint
21838 };
21839
21840 S<char> s;
21841
21842 At S<char> instantiation time, we need to check the access of C::myint
21843 In other words, we need to check the access of the myint typedef through
21844 the C scope. For that purpose, this function will add the myint typedef
21845 and the scope C through which its being accessed to a list of typedefs
21846 tied to the template S. That list will be walked at template instantiation
21847 time and access check performed on each typedefs it contains.
21848 Note that this particular code snippet should yield an error because
21849 myint is private to C. */
21850
21851 void
21852 append_type_to_template_for_access_check (tree templ,
21853 tree type_decl,
21854 tree scope,
21855 location_t location)
21856 {
21857 qualified_typedef_usage_t *iter;
21858 unsigned i;
21859
21860 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21861
21862 /* Make sure we don't append the type to the template twice. */
21863 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21864 if (iter->typedef_decl == type_decl && scope == iter->context)
21865 return;
21866
21867 append_type_to_template_for_access_check_1 (templ, type_decl,
21868 scope, location);
21869 }
21870
21871 /* Convert the generic type parameters in PARM that match the types given in the
21872 range [START_IDX, END_IDX) from the current_template_parms into generic type
21873 packs. */
21874
21875 tree
21876 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21877 {
21878 tree current = current_template_parms;
21879 int depth = TMPL_PARMS_DEPTH (current);
21880 current = INNERMOST_TEMPLATE_PARMS (current);
21881 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21882
21883 for (int i = 0; i < start_idx; ++i)
21884 TREE_VEC_ELT (replacement, i)
21885 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21886
21887 for (int i = start_idx; i < end_idx; ++i)
21888 {
21889 /* Create a distinct parameter pack type from the current parm and add it
21890 to the replacement args to tsubst below into the generic function
21891 parameter. */
21892
21893 tree o = TREE_TYPE (TREE_VALUE
21894 (TREE_VEC_ELT (current, i)));
21895 tree t = copy_type (o);
21896 TEMPLATE_TYPE_PARM_INDEX (t)
21897 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21898 o, 0, 0, tf_none);
21899 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21900 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21901 TYPE_MAIN_VARIANT (t) = t;
21902 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21903 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21904 TREE_VEC_ELT (replacement, i) = t;
21905 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21906 }
21907
21908 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21909 TREE_VEC_ELT (replacement, i)
21910 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21911
21912 /* If there are more levels then build up the replacement with the outer
21913 template parms. */
21914 if (depth > 1)
21915 replacement = add_to_template_args (template_parms_to_args
21916 (TREE_CHAIN (current_template_parms)),
21917 replacement);
21918
21919 return tsubst (parm, replacement, tf_none, NULL_TREE);
21920 }
21921
21922
21923 /* Set up the hash tables for template instantiations. */
21924
21925 void
21926 init_template_processing (void)
21927 {
21928 decl_specializations = htab_create_ggc (37,
21929 hash_specialization,
21930 eq_specializations,
21931 ggc_free);
21932 type_specializations = htab_create_ggc (37,
21933 hash_specialization,
21934 eq_specializations,
21935 ggc_free);
21936 }
21937
21938 /* Print stats about the template hash tables for -fstats. */
21939
21940 void
21941 print_template_statistics (void)
21942 {
21943 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21944 "%f collisions\n", (long) htab_size (decl_specializations),
21945 (long) htab_elements (decl_specializations),
21946 htab_collisions (decl_specializations));
21947 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21948 "%f collisions\n", (long) htab_size (type_specializations),
21949 (long) htab_elements (type_specializations),
21950 htab_collisions (type_specializations));
21951 }
21952
21953 #include "gt-cp-pt.h"