re PR other/33426 (Support of #pragma ivdep)
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "intl.h"
33 #include "pointer-set.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "toplev.h"
42 #include "timevar.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
56 };
57
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
60
61 int processing_template_parmlist;
62 static int template_header_count;
63
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
66
67 static GTY(()) struct tinst_level *current_tinst_level;
68
69 static GTY(()) tree saved_access_scope;
70
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
75
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
78 local variables. */
79 static struct pointer_map_t *local_specializations;
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);
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, 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 (DECL_P (t) && DECL_LANG_SPECIFIC (t))
324 tinfo = DECL_TEMPLATE_INFO (t);
325
326 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
327 t = TREE_TYPE (t);
328
329 if (OVERLOAD_TYPE_P (t))
330 tinfo = TYPE_TEMPLATE_INFO (t);
331 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
332 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
333
334 return tinfo;
335 }
336
337 /* Returns the template nesting level of the indicated class TYPE.
338
339 For example, in:
340 template <class T>
341 struct A
342 {
343 template <class U>
344 struct B {};
345 };
346
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
350
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
353 always safe. */
354
355 int
356 template_class_depth (tree type)
357 {
358 int depth;
359
360 for (depth = 0;
361 type && TREE_CODE (type) != NAMESPACE_DECL;
362 type = (TREE_CODE (type) == FUNCTION_DECL)
363 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
364 {
365 tree tinfo = get_template_info (type);
366
367 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
369 ++depth;
370 }
371
372 return depth;
373 }
374
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
377
378 static bool
379 inline_needs_template_parms (tree decl)
380 {
381 if (! DECL_TEMPLATE_INFO (decl))
382 return false;
383
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
386 }
387
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
391 innermost first. */
392
393 static void
394 push_inline_template_parms_recursive (tree parmlist, int levels)
395 {
396 tree parms = TREE_VALUE (parmlist);
397 int i;
398
399 if (levels > 1)
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
401
402 ++processing_template_decl;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl),
405 parms, current_template_parms);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
407
408 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
409 NULL);
410 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
411 {
412 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
413
414 if (parm == error_mark_node)
415 continue;
416
417 gcc_assert (DECL_P (parm));
418
419 switch (TREE_CODE (parm))
420 {
421 case TYPE_DECL:
422 case TEMPLATE_DECL:
423 pushdecl (parm);
424 break;
425
426 case PARM_DECL:
427 {
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
431 available. */
432 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
433 CONST_DECL, DECL_NAME (parm),
434 TREE_TYPE (parm));
435 DECL_ARTIFICIAL (decl) = 1;
436 TREE_CONSTANT (decl) = 1;
437 TREE_READONLY (decl) = 1;
438 DECL_INITIAL (decl) = DECL_INITIAL (parm);
439 SET_DECL_TEMPLATE_PARM_P (decl);
440 pushdecl (decl);
441 }
442 break;
443
444 default:
445 gcc_unreachable ();
446 }
447 }
448 }
449
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
452
453 void
454 maybe_begin_member_template_processing (tree decl)
455 {
456 tree parms;
457 int levels = 0;
458
459 if (inline_needs_template_parms (decl))
460 {
461 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
462 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
463
464 if (DECL_TEMPLATE_SPECIALIZATION (decl))
465 {
466 --levels;
467 parms = TREE_CHAIN (parms);
468 }
469
470 push_inline_template_parms_recursive (parms, levels);
471 }
472
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 inline_parm_levels.safe_push (levels);
476 }
477
478 /* Undo the effects of maybe_begin_member_template_processing. */
479
480 void
481 maybe_end_member_template_processing (void)
482 {
483 int i;
484 int last;
485
486 if (inline_parm_levels.length () == 0)
487 return;
488
489 last = inline_parm_levels.pop ();
490 for (i = 0; i < last; ++i)
491 {
492 --processing_template_decl;
493 current_template_parms = TREE_CHAIN (current_template_parms);
494 poplevel (0, 0, 0);
495 }
496 }
497
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
500
501 static tree
502 add_to_template_args (tree args, tree extra_args)
503 {
504 tree new_args;
505 int extra_depth;
506 int i;
507 int j;
508
509 if (args == NULL_TREE || extra_args == error_mark_node)
510 return extra_args;
511
512 extra_depth = TMPL_ARGS_DEPTH (extra_args);
513 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
514
515 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
517
518 for (j = 1; j <= extra_depth; ++j, ++i)
519 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
520
521 return new_args;
522 }
523
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
530
531 static tree
532 add_outermost_template_args (tree args, tree extra_args)
533 {
534 tree new_args;
535
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
539
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
541 them. */
542 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
543 return extra_args;
544
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
547
548 new_args = add_to_template_args (args, extra_args);
549
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
552
553 return new_args;
554 }
555
556 /* Return the N levels of innermost template arguments from the ARGS. */
557
558 tree
559 get_innermost_template_args (tree args, int n)
560 {
561 tree new_args;
562 int extra_levels;
563 int i;
564
565 gcc_assert (n >= 0);
566
567 /* If N is 1, just return the innermost set of template arguments. */
568 if (n == 1)
569 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
570
571 /* If we're not removing anything, just return the arguments we were
572 given. */
573 extra_levels = TMPL_ARGS_DEPTH (args) - n;
574 gcc_assert (extra_levels >= 0);
575 if (extra_levels == 0)
576 return args;
577
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args = make_tree_vec (n);
580 for (i = 1; i <= n; ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i,
582 TMPL_ARGS_LEVEL (args, i + extra_levels));
583
584 return new_args;
585 }
586
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
589
590 static tree
591 strip_innermost_template_args (tree args, int extra_levels)
592 {
593 tree new_args;
594 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
595 int i;
596
597 gcc_assert (n >= 0);
598
599 /* If N is 1, just return the outermost set of template arguments. */
600 if (n == 1)
601 return TMPL_ARGS_LEVEL (args, 1);
602
603 /* If we're not removing anything, just return the arguments we were
604 given. */
605 gcc_assert (extra_levels >= 0);
606 if (extra_levels == 0)
607 return args;
608
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args = make_tree_vec (n);
611 for (i = 1; i <= n; ++i)
612 SET_TMPL_ARGS_LEVEL (new_args, i,
613 TMPL_ARGS_LEVEL (args, i));
614
615 return new_args;
616 }
617
618 /* We've got a template header coming up; push to a new level for storing
619 the parms. */
620
621 void
622 begin_template_parm_list (void)
623 {
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
630 e.g.:
631
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
634 };
635
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms, NULL);
639 ++processing_template_decl;
640 ++processing_template_parmlist;
641 note_template_header (0);
642 }
643
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
647
648 static bool
649 check_specialization_scope (void)
650 {
651 tree scope = current_scope ();
652
653 /* [temp.expl.spec]
654
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
661 is a member. */
662 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
663 {
664 error ("explicit specialization in non-namespace scope %qD", scope);
665 return false;
666 }
667
668 /* [temp.expl.spec]
669
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms)
677 {
678 error ("enclosing class templates are not explicitly specialized");
679 return false;
680 }
681
682 return true;
683 }
684
685 /* We've just seen template <>. */
686
687 bool
688 begin_specialization (void)
689 {
690 begin_scope (sk_template_spec, NULL);
691 note_template_header (1);
692 return check_specialization_scope ();
693 }
694
695 /* Called at then end of processing a declaration preceded by
696 template<>. */
697
698 void
699 end_specialization (void)
700 {
701 finish_scope ();
702 reset_specialization ();
703 }
704
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
707
708 void
709 reset_specialization (void)
710 {
711 processing_specialization = 0;
712 template_header_count = 0;
713 }
714
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
717
718 static void
719 note_template_header (int specialization)
720 {
721 processing_specialization = specialization;
722 template_header_count++;
723 }
724
725 /* We're beginning an explicit instantiation. */
726
727 void
728 begin_explicit_instantiation (void)
729 {
730 gcc_assert (!processing_explicit_instantiation);
731 processing_explicit_instantiation = true;
732 }
733
734
735 void
736 end_explicit_instantiation (void)
737 {
738 gcc_assert (processing_explicit_instantiation);
739 processing_explicit_instantiation = false;
740 }
741
742 /* An explicit specialization or partial specialization of TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
746
747 static bool
748 check_specialization_namespace (tree tmpl)
749 {
750 tree tpl_ns = decl_namespace_context (tmpl);
751
752 /* [tmpl.expl.spec]
753
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
760 a member. */
761 if (current_scope() != DECL_CONTEXT (tmpl)
762 && !at_namespace_scope_p ())
763 {
764 error ("specialization of %qD must appear at namespace scope", tmpl);
765 return false;
766 }
767 if (is_associated_namespace (current_namespace, tpl_ns))
768 /* Same or super-using namespace. */
769 return true;
770 else
771 {
772 permerror (input_location, "specialization of %qD in different namespace", tmpl);
773 permerror (input_location, " from definition of %q+#D", tmpl);
774 return false;
775 }
776 }
777
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
780
781 static void
782 check_explicit_instantiation_namespace (tree spec)
783 {
784 tree ns;
785
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns = decl_namespace_context (spec);
789 if (!is_ancestor (current_namespace, ns))
790 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec, current_namespace, ns);
793 }
794
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
797
798 tree
799 maybe_process_partial_specialization (tree type)
800 {
801 tree context;
802
803 if (type == error_mark_node)
804 return error_mark_node;
805
806 /* A lambda that appears in specialization context is not itself a
807 specialization. */
808 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
809 return type;
810
811 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
812 {
813 error ("name of class shadows template template parameter %qD",
814 TYPE_NAME (type));
815 return error_mark_node;
816 }
817
818 context = TYPE_CONTEXT (type);
819
820 if (TYPE_ALIAS_P (type))
821 {
822 if (TYPE_TEMPLATE_INFO (type)
823 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
824 error ("specialization of alias template %qD",
825 TYPE_TI_TEMPLATE (type));
826 else
827 error ("explicit specialization of non-template %qT", type);
828 return error_mark_node;
829 }
830 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
831 {
832 /* This is for ordinary explicit specialization and partial
833 specialization of a template class such as:
834
835 template <> class C<int>;
836
837 or:
838
839 template <class T> class C<T*>;
840
841 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
842
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
844 && !COMPLETE_TYPE_P (type))
845 {
846 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
847 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
848 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
849 if (processing_template_decl)
850 {
851 if (push_template_decl (TYPE_MAIN_DECL (type))
852 == error_mark_node)
853 return error_mark_node;
854 }
855 }
856 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
857 error ("specialization of %qT after instantiation", type);
858 else if (errorcount && !processing_specialization
859 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
860 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
861 /* Trying to define a specialization either without a template<> header
862 or in an inappropriate place. We've already given an error, so just
863 bail now so we don't actually define the specialization. */
864 return error_mark_node;
865 }
866 else if (CLASS_TYPE_P (type)
867 && !CLASSTYPE_USE_TEMPLATE (type)
868 && CLASSTYPE_TEMPLATE_INFO (type)
869 && context && CLASS_TYPE_P (context)
870 && CLASSTYPE_TEMPLATE_INFO (context))
871 {
872 /* This is for an explicit specialization of member class
873 template according to [temp.expl.spec/18]:
874
875 template <> template <class U> class C<int>::D;
876
877 The context `C<int>' must be an implicit instantiation.
878 Otherwise this is just a member class template declared
879 earlier like:
880
881 template <> class C<int> { template <class U> class D; };
882 template <> template <class U> class C<int>::D;
883
884 In the first case, `C<int>::D' is a specialization of `C<T>::D'
885 while in the second case, `C<int>::D' is a primary template
886 and `C<T>::D' may not exist. */
887
888 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
889 && !COMPLETE_TYPE_P (type))
890 {
891 tree t;
892 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
893
894 if (current_namespace
895 != decl_namespace_context (tmpl))
896 {
897 permerror (input_location, "specializing %q#T in different namespace", type);
898 permerror (input_location, " from definition of %q+#D", tmpl);
899 }
900
901 /* Check for invalid specialization after instantiation:
902
903 template <> template <> class C<int>::D<int>;
904 template <> template <class U> class C<int>::D; */
905
906 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
907 t; t = TREE_CHAIN (t))
908 {
909 tree inst = TREE_VALUE (t);
910 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
911 {
912 /* We already have a full specialization of this partial
913 instantiation. Reassign it to the new member
914 specialization template. */
915 spec_entry elt;
916 spec_entry *entry;
917 void **slot;
918
919 elt.tmpl = most_general_template (tmpl);
920 elt.args = CLASSTYPE_TI_ARGS (inst);
921 elt.spec = inst;
922
923 htab_remove_elt (type_specializations, &elt);
924
925 elt.tmpl = tmpl;
926 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
927
928 slot = htab_find_slot (type_specializations, &elt, INSERT);
929 entry = ggc_alloc_spec_entry ();
930 *entry = elt;
931 *slot = entry;
932 }
933 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
934 /* But if we've had an implicit instantiation, that's a
935 problem ([temp.expl.spec]/6). */
936 error ("specialization %qT after instantiation %qT",
937 type, inst);
938 }
939
940 /* Mark TYPE as a specialization. And as a result, we only
941 have one level of template argument for the innermost
942 class template. */
943 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
944 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
945 CLASSTYPE_TI_ARGS (type)
946 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
947 }
948 }
949 else if (processing_specialization)
950 {
951 /* Someday C++0x may allow for enum template specialization. */
952 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
953 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
954 pedwarn (input_location, OPT_Wpedantic, "template specialization "
955 "of %qD not allowed by ISO C++", type);
956 else
957 {
958 error ("explicit specialization of non-template %qT", type);
959 return error_mark_node;
960 }
961 }
962
963 return type;
964 }
965
966 /* Returns nonzero if we can optimize the retrieval of specializations
967 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
968 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
969
970 static inline bool
971 optimize_specialization_lookup_p (tree tmpl)
972 {
973 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
974 && DECL_CLASS_SCOPE_P (tmpl)
975 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
976 parameter. */
977 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
978 /* The optimized lookup depends on the fact that the
979 template arguments for the member function template apply
980 purely to the containing class, which is not true if the
981 containing class is an explicit or partial
982 specialization. */
983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
984 && !DECL_MEMBER_TEMPLATE_P (tmpl)
985 && !DECL_CONV_FN_P (tmpl)
986 /* It is possible to have a template that is not a member
987 template and is not a member of a template class:
988
989 template <typename T>
990 struct S { friend A::f(); };
991
992 Here, the friend function is a template, but the context does
993 not have template information. The optimized lookup relies
994 on having ARGS be the template arguments for both the class
995 and the function template. */
996 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
997 }
998
999 /* Retrieve the specialization (in the sense of [temp.spec] - a
1000 specialization is either an instantiation or an explicit
1001 specialization) of TMPL for the given template ARGS. If there is
1002 no such specialization, return NULL_TREE. The ARGS are a vector of
1003 arguments, or a vector of vectors of arguments, in the case of
1004 templates with more than one level of parameters.
1005
1006 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1007 then we search for a partial specialization matching ARGS. This
1008 parameter is ignored if TMPL is not a class template.
1009
1010 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1011 result is a NONTYPE_ARGUMENT_PACK. */
1012
1013 static tree
1014 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1015 {
1016 if (tmpl == NULL_TREE)
1017 return NULL_TREE;
1018
1019 if (args == error_mark_node)
1020 return NULL_TREE;
1021
1022 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1023 || TREE_CODE (tmpl) == FIELD_DECL);
1024
1025 /* There should be as many levels of arguments as there are
1026 levels of parameters. */
1027 gcc_assert (TMPL_ARGS_DEPTH (args)
1028 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1029 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1030 : template_class_depth (DECL_CONTEXT (tmpl))));
1031
1032 if (optimize_specialization_lookup_p (tmpl))
1033 {
1034 tree class_template;
1035 tree class_specialization;
1036 vec<tree, va_gc> *methods;
1037 tree fns;
1038 int idx;
1039
1040 /* The template arguments actually apply to the containing
1041 class. Find the class specialization with those
1042 arguments. */
1043 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1044 class_specialization
1045 = retrieve_specialization (class_template, args, 0);
1046 if (!class_specialization)
1047 return NULL_TREE;
1048 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1049 for the specialization. */
1050 idx = class_method_index_for_fn (class_specialization, tmpl);
1051 if (idx == -1)
1052 return NULL_TREE;
1053 /* Iterate through the methods with the indicated name, looking
1054 for the one that has an instance of TMPL. */
1055 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1056 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1057 {
1058 tree fn = OVL_CURRENT (fns);
1059 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1060 /* using-declarations can add base methods to the method vec,
1061 and we don't want those here. */
1062 && DECL_CONTEXT (fn) == class_specialization)
1063 return fn;
1064 }
1065 return NULL_TREE;
1066 }
1067 else
1068 {
1069 spec_entry *found;
1070 spec_entry elt;
1071 htab_t specializations;
1072
1073 elt.tmpl = tmpl;
1074 elt.args = args;
1075 elt.spec = NULL_TREE;
1076
1077 if (DECL_CLASS_TEMPLATE_P (tmpl))
1078 specializations = type_specializations;
1079 else
1080 specializations = decl_specializations;
1081
1082 if (hash == 0)
1083 hash = hash_specialization (&elt);
1084 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1085 if (found)
1086 return found->spec;
1087 }
1088
1089 return NULL_TREE;
1090 }
1091
1092 /* Like retrieve_specialization, but for local declarations. */
1093
1094 static tree
1095 retrieve_local_specialization (tree tmpl)
1096 {
1097 void **slot;
1098
1099 if (local_specializations == NULL)
1100 return NULL_TREE;
1101
1102 slot = pointer_map_contains (local_specializations, tmpl);
1103 return slot ? (tree) *slot : NULL_TREE;
1104 }
1105
1106 /* Returns nonzero iff DECL is a specialization of TMPL. */
1107
1108 int
1109 is_specialization_of (tree decl, tree tmpl)
1110 {
1111 tree t;
1112
1113 if (TREE_CODE (decl) == FUNCTION_DECL)
1114 {
1115 for (t = decl;
1116 t != NULL_TREE;
1117 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1118 if (t == tmpl)
1119 return 1;
1120 }
1121 else
1122 {
1123 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1124
1125 for (t = TREE_TYPE (decl);
1126 t != NULL_TREE;
1127 t = CLASSTYPE_USE_TEMPLATE (t)
1128 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1129 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1130 return 1;
1131 }
1132
1133 return 0;
1134 }
1135
1136 /* Returns nonzero iff DECL is a specialization of friend declaration
1137 FRIEND_DECL according to [temp.friend]. */
1138
1139 bool
1140 is_specialization_of_friend (tree decl, tree friend_decl)
1141 {
1142 bool need_template = true;
1143 int template_depth;
1144
1145 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1146 || TREE_CODE (decl) == TYPE_DECL);
1147
1148 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1149 of a template class, we want to check if DECL is a specialization
1150 if this. */
1151 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1152 && DECL_TEMPLATE_INFO (friend_decl)
1153 && !DECL_USE_TEMPLATE (friend_decl))
1154 {
1155 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1156 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1157 need_template = false;
1158 }
1159 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1160 && !PRIMARY_TEMPLATE_P (friend_decl))
1161 need_template = false;
1162
1163 /* There is nothing to do if this is not a template friend. */
1164 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1165 return false;
1166
1167 if (is_specialization_of (decl, friend_decl))
1168 return true;
1169
1170 /* [temp.friend/6]
1171 A member of a class template may be declared to be a friend of a
1172 non-template class. In this case, the corresponding member of
1173 every specialization of the class template is a friend of the
1174 class granting friendship.
1175
1176 For example, given a template friend declaration
1177
1178 template <class T> friend void A<T>::f();
1179
1180 the member function below is considered a friend
1181
1182 template <> struct A<int> {
1183 void f();
1184 };
1185
1186 For this type of template friend, TEMPLATE_DEPTH below will be
1187 nonzero. To determine if DECL is a friend of FRIEND, we first
1188 check if the enclosing class is a specialization of another. */
1189
1190 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1191 if (template_depth
1192 && DECL_CLASS_SCOPE_P (decl)
1193 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1194 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1195 {
1196 /* Next, we check the members themselves. In order to handle
1197 a few tricky cases, such as when FRIEND_DECL's are
1198
1199 template <class T> friend void A<T>::g(T t);
1200 template <class T> template <T t> friend void A<T>::h();
1201
1202 and DECL's are
1203
1204 void A<int>::g(int);
1205 template <int> void A<int>::h();
1206
1207 we need to figure out ARGS, the template arguments from
1208 the context of DECL. This is required for template substitution
1209 of `T' in the function parameter of `g' and template parameter
1210 of `h' in the above examples. Here ARGS corresponds to `int'. */
1211
1212 tree context = DECL_CONTEXT (decl);
1213 tree args = NULL_TREE;
1214 int current_depth = 0;
1215
1216 while (current_depth < template_depth)
1217 {
1218 if (CLASSTYPE_TEMPLATE_INFO (context))
1219 {
1220 if (current_depth == 0)
1221 args = TYPE_TI_ARGS (context);
1222 else
1223 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1224 current_depth++;
1225 }
1226 context = TYPE_CONTEXT (context);
1227 }
1228
1229 if (TREE_CODE (decl) == FUNCTION_DECL)
1230 {
1231 bool is_template;
1232 tree friend_type;
1233 tree decl_type;
1234 tree friend_args_type;
1235 tree decl_args_type;
1236
1237 /* Make sure that both DECL and FRIEND_DECL are templates or
1238 non-templates. */
1239 is_template = DECL_TEMPLATE_INFO (decl)
1240 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1241 if (need_template ^ is_template)
1242 return false;
1243 else if (is_template)
1244 {
1245 /* If both are templates, check template parameter list. */
1246 tree friend_parms
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1248 args, tf_none);
1249 if (!comp_template_parms
1250 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1251 friend_parms))
1252 return false;
1253
1254 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1255 }
1256 else
1257 decl_type = TREE_TYPE (decl);
1258
1259 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1260 tf_none, NULL_TREE);
1261 if (friend_type == error_mark_node)
1262 return false;
1263
1264 /* Check if return types match. */
1265 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1266 return false;
1267
1268 /* Check if function parameter types match, ignoring the
1269 `this' parameter. */
1270 friend_args_type = TYPE_ARG_TYPES (friend_type);
1271 decl_args_type = TYPE_ARG_TYPES (decl_type);
1272 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1273 friend_args_type = TREE_CHAIN (friend_args_type);
1274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1275 decl_args_type = TREE_CHAIN (decl_args_type);
1276
1277 return compparms (decl_args_type, friend_args_type);
1278 }
1279 else
1280 {
1281 /* DECL is a TYPE_DECL */
1282 bool is_template;
1283 tree decl_type = TREE_TYPE (decl);
1284
1285 /* Make sure that both DECL and FRIEND_DECL are templates or
1286 non-templates. */
1287 is_template
1288 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1290
1291 if (need_template ^ is_template)
1292 return false;
1293 else if (is_template)
1294 {
1295 tree friend_parms;
1296 /* If both are templates, check the name of the two
1297 TEMPLATE_DECL's first because is_friend didn't. */
1298 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1299 != DECL_NAME (friend_decl))
1300 return false;
1301
1302 /* Now check template parameter list. */
1303 friend_parms
1304 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1305 args, tf_none);
1306 return comp_template_parms
1307 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1308 friend_parms);
1309 }
1310 else
1311 return (DECL_NAME (decl)
1312 == DECL_NAME (friend_decl));
1313 }
1314 }
1315 return false;
1316 }
1317
1318 /* Register the specialization SPEC as a specialization of TMPL with
1319 the indicated ARGS. IS_FRIEND indicates whether the specialization
1320 is actually just a friend declaration. Returns SPEC, or an
1321 equivalent prior declaration, if available.
1322
1323 We also store instantiations of field packs in the hash table, even
1324 though they are not themselves templates, to make lookup easier. */
1325
1326 static tree
1327 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1328 hashval_t hash)
1329 {
1330 tree fn;
1331 void **slot = NULL;
1332 spec_entry elt;
1333
1334 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1335 || (TREE_CODE (tmpl) == FIELD_DECL
1336 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1337
1338 if (TREE_CODE (spec) == FUNCTION_DECL
1339 && uses_template_parms (DECL_TI_ARGS (spec)))
1340 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1341 register it; we want the corresponding TEMPLATE_DECL instead.
1342 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1343 the more obvious `uses_template_parms (spec)' to avoid problems
1344 with default function arguments. In particular, given
1345 something like this:
1346
1347 template <class T> void f(T t1, T t = T())
1348
1349 the default argument expression is not substituted for in an
1350 instantiation unless and until it is actually needed. */
1351 return spec;
1352
1353 if (optimize_specialization_lookup_p (tmpl))
1354 /* We don't put these specializations in the hash table, but we might
1355 want to give an error about a mismatch. */
1356 fn = retrieve_specialization (tmpl, args, 0);
1357 else
1358 {
1359 elt.tmpl = tmpl;
1360 elt.args = args;
1361 elt.spec = spec;
1362
1363 if (hash == 0)
1364 hash = hash_specialization (&elt);
1365
1366 slot =
1367 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1368 if (*slot)
1369 fn = ((spec_entry *) *slot)->spec;
1370 else
1371 fn = NULL_TREE;
1372 }
1373
1374 /* We can sometimes try to re-register a specialization that we've
1375 already got. In particular, regenerate_decl_from_template calls
1376 duplicate_decls which will update the specialization list. But,
1377 we'll still get called again here anyhow. It's more convenient
1378 to simply allow this than to try to prevent it. */
1379 if (fn == spec)
1380 return spec;
1381 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1382 {
1383 if (DECL_TEMPLATE_INSTANTIATION (fn))
1384 {
1385 if (DECL_ODR_USED (fn)
1386 || DECL_EXPLICIT_INSTANTIATION (fn))
1387 {
1388 error ("specialization of %qD after instantiation",
1389 fn);
1390 return error_mark_node;
1391 }
1392 else
1393 {
1394 tree clone;
1395 /* This situation should occur only if the first
1396 specialization is an implicit instantiation, the
1397 second is an explicit specialization, and the
1398 implicit instantiation has not yet been used. That
1399 situation can occur if we have implicitly
1400 instantiated a member function and then specialized
1401 it later.
1402
1403 We can also wind up here if a friend declaration that
1404 looked like an instantiation turns out to be a
1405 specialization:
1406
1407 template <class T> void foo(T);
1408 class S { friend void foo<>(int) };
1409 template <> void foo(int);
1410
1411 We transform the existing DECL in place so that any
1412 pointers to it become pointers to the updated
1413 declaration.
1414
1415 If there was a definition for the template, but not
1416 for the specialization, we want this to look as if
1417 there were no definition, and vice versa. */
1418 DECL_INITIAL (fn) = NULL_TREE;
1419 duplicate_decls (spec, fn, is_friend);
1420 /* The call to duplicate_decls will have applied
1421 [temp.expl.spec]:
1422
1423 An explicit specialization of a function template
1424 is inline only if it is explicitly declared to be,
1425 and independently of whether its function template
1426 is.
1427
1428 to the primary function; now copy the inline bits to
1429 the various clones. */
1430 FOR_EACH_CLONE (clone, fn)
1431 {
1432 DECL_DECLARED_INLINE_P (clone)
1433 = DECL_DECLARED_INLINE_P (fn);
1434 DECL_SOURCE_LOCATION (clone)
1435 = DECL_SOURCE_LOCATION (fn);
1436 }
1437 check_specialization_namespace (tmpl);
1438
1439 return fn;
1440 }
1441 }
1442 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1443 {
1444 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1445 /* Dup decl failed, but this is a new definition. Set the
1446 line number so any errors match this new
1447 definition. */
1448 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1449
1450 return fn;
1451 }
1452 }
1453 else if (fn)
1454 return duplicate_decls (spec, fn, is_friend);
1455
1456 /* A specialization must be declared in the same namespace as the
1457 template it is specializing. */
1458 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1459 && !check_specialization_namespace (tmpl))
1460 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1461
1462 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1463 {
1464 spec_entry *entry = ggc_alloc_spec_entry ();
1465 gcc_assert (tmpl && args && spec);
1466 *entry = elt;
1467 *slot = entry;
1468 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1469 && PRIMARY_TEMPLATE_P (tmpl)
1470 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1471 /* TMPL is a forward declaration of a template function; keep a list
1472 of all specializations in case we need to reassign them to a friend
1473 template later in tsubst_friend_function. */
1474 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1475 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1476 }
1477
1478 return spec;
1479 }
1480
1481 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1482 TMPL and ARGS members, ignores SPEC. */
1483
1484 int comparing_specializations;
1485
1486 static int
1487 eq_specializations (const void *p1, const void *p2)
1488 {
1489 const spec_entry *e1 = (const spec_entry *)p1;
1490 const spec_entry *e2 = (const spec_entry *)p2;
1491 int equal;
1492
1493 ++comparing_specializations;
1494 equal = (e1->tmpl == e2->tmpl
1495 && comp_template_args (e1->args, e2->args));
1496 --comparing_specializations;
1497
1498 return equal;
1499 }
1500
1501 /* Returns a hash for a template TMPL and template arguments ARGS. */
1502
1503 static hashval_t
1504 hash_tmpl_and_args (tree tmpl, tree args)
1505 {
1506 hashval_t val = DECL_UID (tmpl);
1507 return iterative_hash_template_arg (args, val);
1508 }
1509
1510 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1511 ignoring SPEC. */
1512
1513 static hashval_t
1514 hash_specialization (const void *p)
1515 {
1516 const spec_entry *e = (const spec_entry *)p;
1517 return hash_tmpl_and_args (e->tmpl, e->args);
1518 }
1519
1520 /* Recursively calculate a hash value for a template argument ARG, for use
1521 in the hash tables of template specializations. */
1522
1523 hashval_t
1524 iterative_hash_template_arg (tree arg, hashval_t val)
1525 {
1526 unsigned HOST_WIDE_INT i;
1527 enum tree_code code;
1528 char tclass;
1529
1530 if (arg == NULL_TREE)
1531 return iterative_hash_object (arg, val);
1532
1533 if (!TYPE_P (arg))
1534 STRIP_NOPS (arg);
1535
1536 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1537 /* We can get one of these when re-hashing a previous entry in the middle
1538 of substituting into a pack expansion. Just look through it. */
1539 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1540
1541 code = TREE_CODE (arg);
1542 tclass = TREE_CODE_CLASS (code);
1543
1544 val = iterative_hash_object (code, val);
1545
1546 switch (code)
1547 {
1548 case ERROR_MARK:
1549 return val;
1550
1551 case IDENTIFIER_NODE:
1552 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1553
1554 case TREE_VEC:
1555 {
1556 int i, len = TREE_VEC_LENGTH (arg);
1557 for (i = 0; i < len; ++i)
1558 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1559 return val;
1560 }
1561
1562 case TYPE_PACK_EXPANSION:
1563 case EXPR_PACK_EXPANSION:
1564 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1565 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1566
1567 case TYPE_ARGUMENT_PACK:
1568 case NONTYPE_ARGUMENT_PACK:
1569 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1570
1571 case TREE_LIST:
1572 for (; arg; arg = TREE_CHAIN (arg))
1573 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1574 return val;
1575
1576 case OVERLOAD:
1577 for (; arg; arg = OVL_NEXT (arg))
1578 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1579 return val;
1580
1581 case CONSTRUCTOR:
1582 {
1583 tree field, value;
1584 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1585 {
1586 val = iterative_hash_template_arg (field, val);
1587 val = iterative_hash_template_arg (value, val);
1588 }
1589 return val;
1590 }
1591
1592 case PARM_DECL:
1593 if (!DECL_ARTIFICIAL (arg))
1594 {
1595 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1596 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1597 }
1598 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1599
1600 case TARGET_EXPR:
1601 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1602
1603 case PTRMEM_CST:
1604 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1605 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1606
1607 case TEMPLATE_PARM_INDEX:
1608 val = iterative_hash_template_arg
1609 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1610 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1611 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1612
1613 case TRAIT_EXPR:
1614 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1615 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1616 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1617
1618 case BASELINK:
1619 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1620 val);
1621 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1622 val);
1623
1624 case MODOP_EXPR:
1625 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1626 code = TREE_CODE (TREE_OPERAND (arg, 1));
1627 val = iterative_hash_object (code, val);
1628 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1629
1630 case LAMBDA_EXPR:
1631 /* A lambda can't appear in a template arg, but don't crash on
1632 erroneous input. */
1633 gcc_assert (seen_error ());
1634 return val;
1635
1636 case CAST_EXPR:
1637 case IMPLICIT_CONV_EXPR:
1638 case STATIC_CAST_EXPR:
1639 case REINTERPRET_CAST_EXPR:
1640 case CONST_CAST_EXPR:
1641 case DYNAMIC_CAST_EXPR:
1642 case NEW_EXPR:
1643 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1644 /* Now hash operands as usual. */
1645 break;
1646
1647 default:
1648 break;
1649 }
1650
1651 switch (tclass)
1652 {
1653 case tcc_type:
1654 if (TYPE_CANONICAL (arg))
1655 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1656 val);
1657 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1658 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1659 /* Otherwise just compare the types during lookup. */
1660 return val;
1661
1662 case tcc_declaration:
1663 case tcc_constant:
1664 return iterative_hash_expr (arg, val);
1665
1666 default:
1667 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1668 {
1669 unsigned n = cp_tree_operand_length (arg);
1670 for (i = 0; i < n; ++i)
1671 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1672 return val;
1673 }
1674 }
1675 gcc_unreachable ();
1676 return 0;
1677 }
1678
1679 /* Unregister the specialization SPEC as a specialization of TMPL.
1680 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1681 if the SPEC was listed as a specialization of TMPL.
1682
1683 Note that SPEC has been ggc_freed, so we can't look inside it. */
1684
1685 bool
1686 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1687 {
1688 spec_entry *entry;
1689 spec_entry elt;
1690
1691 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1692 elt.args = TI_ARGS (tinfo);
1693 elt.spec = NULL_TREE;
1694
1695 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1696 if (entry != NULL)
1697 {
1698 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1699 gcc_assert (new_spec != NULL_TREE);
1700 entry->spec = new_spec;
1701 return 1;
1702 }
1703
1704 return 0;
1705 }
1706
1707 /* Like register_specialization, but for local declarations. We are
1708 registering SPEC, an instantiation of TMPL. */
1709
1710 static void
1711 register_local_specialization (tree spec, tree tmpl)
1712 {
1713 void **slot;
1714
1715 slot = pointer_map_insert (local_specializations, tmpl);
1716 *slot = spec;
1717 }
1718
1719 /* TYPE is a class type. Returns true if TYPE is an explicitly
1720 specialized class. */
1721
1722 bool
1723 explicit_class_specialization_p (tree type)
1724 {
1725 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1726 return false;
1727 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1728 }
1729
1730 /* Print the list of functions at FNS, going through all the overloads
1731 for each element of the list. Alternatively, FNS can not be a
1732 TREE_LIST, in which case it will be printed together with all the
1733 overloads.
1734
1735 MORE and *STR should respectively be FALSE and NULL when the function
1736 is called from the outside. They are used internally on recursive
1737 calls. print_candidates manages the two parameters and leaves NULL
1738 in *STR when it ends. */
1739
1740 static void
1741 print_candidates_1 (tree fns, bool more, const char **str)
1742 {
1743 tree fn, fn2;
1744 char *spaces = NULL;
1745
1746 for (fn = fns; fn; fn = OVL_NEXT (fn))
1747 if (TREE_CODE (fn) == TREE_LIST)
1748 {
1749 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1750 print_candidates_1 (TREE_VALUE (fn2),
1751 TREE_CHAIN (fn2) || more, str);
1752 }
1753 else
1754 {
1755 tree cand = OVL_CURRENT (fn);
1756 if (!*str)
1757 {
1758 /* Pick the prefix string. */
1759 if (!more && !OVL_NEXT (fns))
1760 {
1761 inform (DECL_SOURCE_LOCATION (cand),
1762 "candidate is: %#D", cand);
1763 continue;
1764 }
1765
1766 *str = _("candidates are:");
1767 spaces = get_spaces (*str);
1768 }
1769 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1770 *str = spaces ? spaces : *str;
1771 }
1772
1773 if (!more)
1774 {
1775 free (spaces);
1776 *str = NULL;
1777 }
1778 }
1779
1780 /* Print the list of candidate FNS in an error message. FNS can also
1781 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1782
1783 void
1784 print_candidates (tree fns)
1785 {
1786 const char *str = NULL;
1787 print_candidates_1 (fns, false, &str);
1788 gcc_assert (str == NULL);
1789 }
1790
1791 /* Returns the template (one of the functions given by TEMPLATE_ID)
1792 which can be specialized to match the indicated DECL with the
1793 explicit template args given in TEMPLATE_ID. The DECL may be
1794 NULL_TREE if none is available. In that case, the functions in
1795 TEMPLATE_ID are non-members.
1796
1797 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1798 specialization of a member template.
1799
1800 The TEMPLATE_COUNT is the number of references to qualifying
1801 template classes that appeared in the name of the function. See
1802 check_explicit_specialization for a more accurate description.
1803
1804 TSK indicates what kind of template declaration (if any) is being
1805 declared. TSK_TEMPLATE indicates that the declaration given by
1806 DECL, though a FUNCTION_DECL, has template parameters, and is
1807 therefore a template function.
1808
1809 The template args (those explicitly specified and those deduced)
1810 are output in a newly created vector *TARGS_OUT.
1811
1812 If it is impossible to determine the result, an error message is
1813 issued. The error_mark_node is returned to indicate failure. */
1814
1815 static tree
1816 determine_specialization (tree template_id,
1817 tree decl,
1818 tree* targs_out,
1819 int need_member_template,
1820 int template_count,
1821 tmpl_spec_kind tsk)
1822 {
1823 tree fns;
1824 tree targs;
1825 tree explicit_targs;
1826 tree candidates = NULL_TREE;
1827 /* A TREE_LIST of templates of which DECL may be a specialization.
1828 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1829 corresponding TREE_PURPOSE is the set of template arguments that,
1830 when used to instantiate the template, would produce a function
1831 with the signature of DECL. */
1832 tree templates = NULL_TREE;
1833 int header_count;
1834 cp_binding_level *b;
1835
1836 *targs_out = NULL_TREE;
1837
1838 if (template_id == error_mark_node || decl == error_mark_node)
1839 return error_mark_node;
1840
1841 /* We shouldn't be specializing a member template of an
1842 unspecialized class template; we already gave an error in
1843 check_specialization_scope, now avoid crashing. */
1844 if (template_count && DECL_CLASS_SCOPE_P (decl)
1845 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1846 {
1847 gcc_assert (errorcount);
1848 return error_mark_node;
1849 }
1850
1851 fns = TREE_OPERAND (template_id, 0);
1852 explicit_targs = TREE_OPERAND (template_id, 1);
1853
1854 if (fns == error_mark_node)
1855 return error_mark_node;
1856
1857 /* Check for baselinks. */
1858 if (BASELINK_P (fns))
1859 fns = BASELINK_FUNCTIONS (fns);
1860
1861 if (!is_overloaded_fn (fns))
1862 {
1863 error ("%qD is not a function template", fns);
1864 return error_mark_node;
1865 }
1866
1867 /* Count the number of template headers specified for this
1868 specialization. */
1869 header_count = 0;
1870 for (b = current_binding_level;
1871 b->kind == sk_template_parms;
1872 b = b->level_chain)
1873 ++header_count;
1874
1875 for (; fns; fns = OVL_NEXT (fns))
1876 {
1877 tree fn = OVL_CURRENT (fns);
1878
1879 if (TREE_CODE (fn) == TEMPLATE_DECL)
1880 {
1881 tree decl_arg_types;
1882 tree fn_arg_types;
1883 tree insttype;
1884
1885 /* In case of explicit specialization, we need to check if
1886 the number of template headers appearing in the specialization
1887 is correct. This is usually done in check_explicit_specialization,
1888 but the check done there cannot be exhaustive when specializing
1889 member functions. Consider the following code:
1890
1891 template <> void A<int>::f(int);
1892 template <> template <> void A<int>::f(int);
1893
1894 Assuming that A<int> is not itself an explicit specialization
1895 already, the first line specializes "f" which is a non-template
1896 member function, whilst the second line specializes "f" which
1897 is a template member function. So both lines are syntactically
1898 correct, and check_explicit_specialization does not reject
1899 them.
1900
1901 Here, we can do better, as we are matching the specialization
1902 against the declarations. We count the number of template
1903 headers, and we check if they match TEMPLATE_COUNT + 1
1904 (TEMPLATE_COUNT is the number of qualifying template classes,
1905 plus there must be another header for the member template
1906 itself).
1907
1908 Notice that if header_count is zero, this is not a
1909 specialization but rather a template instantiation, so there
1910 is no check we can perform here. */
1911 if (header_count && header_count != template_count + 1)
1912 continue;
1913
1914 /* Check that the number of template arguments at the
1915 innermost level for DECL is the same as for FN. */
1916 if (current_binding_level->kind == sk_template_parms
1917 && !current_binding_level->explicit_spec_p
1918 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1919 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1920 (current_template_parms))))
1921 continue;
1922
1923 /* DECL might be a specialization of FN. */
1924 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1925 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1926
1927 /* For a non-static member function, we need to make sure
1928 that the const qualification is the same. Since
1929 get_bindings does not try to merge the "this" parameter,
1930 we must do the comparison explicitly. */
1931 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1932 && !same_type_p (TREE_VALUE (fn_arg_types),
1933 TREE_VALUE (decl_arg_types)))
1934 continue;
1935
1936 /* Skip the "this" parameter and, for constructors of
1937 classes with virtual bases, the VTT parameter. A
1938 full specialization of a constructor will have a VTT
1939 parameter, but a template never will. */
1940 decl_arg_types
1941 = skip_artificial_parms_for (decl, decl_arg_types);
1942 fn_arg_types
1943 = skip_artificial_parms_for (fn, fn_arg_types);
1944
1945 /* Function templates cannot be specializations; there are
1946 no partial specializations of functions. Therefore, if
1947 the type of DECL does not match FN, there is no
1948 match. */
1949 if (tsk == tsk_template)
1950 {
1951 if (compparms (fn_arg_types, decl_arg_types))
1952 candidates = tree_cons (NULL_TREE, fn, candidates);
1953 continue;
1954 }
1955
1956 /* See whether this function might be a specialization of this
1957 template. Suppress access control because we might be trying
1958 to make this specialization a friend, and we have already done
1959 access control for the declaration of the specialization. */
1960 push_deferring_access_checks (dk_no_check);
1961 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1962 pop_deferring_access_checks ();
1963
1964 if (!targs)
1965 /* We cannot deduce template arguments that when used to
1966 specialize TMPL will produce DECL. */
1967 continue;
1968
1969 /* Make sure that the deduced arguments actually work. */
1970 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1971 if (insttype == error_mark_node)
1972 continue;
1973 fn_arg_types
1974 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1975 if (!compparms (fn_arg_types, decl_arg_types))
1976 continue;
1977
1978 /* Save this template, and the arguments deduced. */
1979 templates = tree_cons (targs, fn, templates);
1980 }
1981 else if (need_member_template)
1982 /* FN is an ordinary member function, and we need a
1983 specialization of a member template. */
1984 ;
1985 else if (TREE_CODE (fn) != FUNCTION_DECL)
1986 /* We can get IDENTIFIER_NODEs here in certain erroneous
1987 cases. */
1988 ;
1989 else if (!DECL_FUNCTION_MEMBER_P (fn))
1990 /* This is just an ordinary non-member function. Nothing can
1991 be a specialization of that. */
1992 ;
1993 else if (DECL_ARTIFICIAL (fn))
1994 /* Cannot specialize functions that are created implicitly. */
1995 ;
1996 else
1997 {
1998 tree decl_arg_types;
1999
2000 /* This is an ordinary member function. However, since
2001 we're here, we can assume its enclosing class is a
2002 template class. For example,
2003
2004 template <typename T> struct S { void f(); };
2005 template <> void S<int>::f() {}
2006
2007 Here, S<int>::f is a non-template, but S<int> is a
2008 template class. If FN has the same type as DECL, we
2009 might be in business. */
2010
2011 if (!DECL_TEMPLATE_INFO (fn))
2012 /* Its enclosing class is an explicit specialization
2013 of a template class. This is not a candidate. */
2014 continue;
2015
2016 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2017 TREE_TYPE (TREE_TYPE (fn))))
2018 /* The return types differ. */
2019 continue;
2020
2021 /* Adjust the type of DECL in case FN is a static member. */
2022 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2023 if (DECL_STATIC_FUNCTION_P (fn)
2024 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2025 decl_arg_types = TREE_CHAIN (decl_arg_types);
2026
2027 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2028 decl_arg_types))
2029 /* They match! */
2030 candidates = tree_cons (NULL_TREE, fn, candidates);
2031 }
2032 }
2033
2034 if (templates && TREE_CHAIN (templates))
2035 {
2036 /* We have:
2037
2038 [temp.expl.spec]
2039
2040 It is possible for a specialization with a given function
2041 signature to be instantiated from more than one function
2042 template. In such cases, explicit specification of the
2043 template arguments must be used to uniquely identify the
2044 function template specialization being specialized.
2045
2046 Note that here, there's no suggestion that we're supposed to
2047 determine which of the candidate templates is most
2048 specialized. However, we, also have:
2049
2050 [temp.func.order]
2051
2052 Partial ordering of overloaded function template
2053 declarations is used in the following contexts to select
2054 the function template to which a function template
2055 specialization refers:
2056
2057 -- when an explicit specialization refers to a function
2058 template.
2059
2060 So, we do use the partial ordering rules, at least for now.
2061 This extension can only serve to make invalid programs valid,
2062 so it's safe. And, there is strong anecdotal evidence that
2063 the committee intended the partial ordering rules to apply;
2064 the EDG front end has that behavior, and John Spicer claims
2065 that the committee simply forgot to delete the wording in
2066 [temp.expl.spec]. */
2067 tree tmpl = most_specialized_instantiation (templates);
2068 if (tmpl != error_mark_node)
2069 {
2070 templates = tmpl;
2071 TREE_CHAIN (templates) = NULL_TREE;
2072 }
2073 }
2074
2075 if (templates == NULL_TREE && candidates == NULL_TREE)
2076 {
2077 error ("template-id %qD for %q+D does not match any template "
2078 "declaration", template_id, decl);
2079 if (header_count && header_count != template_count + 1)
2080 inform (input_location, "saw %d %<template<>%>, need %d for "
2081 "specializing a member function template",
2082 header_count, template_count + 1);
2083 return error_mark_node;
2084 }
2085 else if ((templates && TREE_CHAIN (templates))
2086 || (candidates && TREE_CHAIN (candidates))
2087 || (templates && candidates))
2088 {
2089 error ("ambiguous template specialization %qD for %q+D",
2090 template_id, decl);
2091 candidates = chainon (candidates, templates);
2092 print_candidates (candidates);
2093 return error_mark_node;
2094 }
2095
2096 /* We have one, and exactly one, match. */
2097 if (candidates)
2098 {
2099 tree fn = TREE_VALUE (candidates);
2100 *targs_out = copy_node (DECL_TI_ARGS (fn));
2101 /* DECL is a re-declaration or partial instantiation of a template
2102 function. */
2103 if (TREE_CODE (fn) == TEMPLATE_DECL)
2104 return fn;
2105 /* It was a specialization of an ordinary member function in a
2106 template class. */
2107 return DECL_TI_TEMPLATE (fn);
2108 }
2109
2110 /* It was a specialization of a template. */
2111 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2112 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2113 {
2114 *targs_out = copy_node (targs);
2115 SET_TMPL_ARGS_LEVEL (*targs_out,
2116 TMPL_ARGS_DEPTH (*targs_out),
2117 TREE_PURPOSE (templates));
2118 }
2119 else
2120 *targs_out = TREE_PURPOSE (templates);
2121 return TREE_VALUE (templates);
2122 }
2123
2124 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2125 but with the default argument values filled in from those in the
2126 TMPL_TYPES. */
2127
2128 static tree
2129 copy_default_args_to_explicit_spec_1 (tree spec_types,
2130 tree tmpl_types)
2131 {
2132 tree new_spec_types;
2133
2134 if (!spec_types)
2135 return NULL_TREE;
2136
2137 if (spec_types == void_list_node)
2138 return void_list_node;
2139
2140 /* Substitute into the rest of the list. */
2141 new_spec_types =
2142 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2143 TREE_CHAIN (tmpl_types));
2144
2145 /* Add the default argument for this parameter. */
2146 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2147 TREE_VALUE (spec_types),
2148 new_spec_types);
2149 }
2150
2151 /* DECL is an explicit specialization. Replicate default arguments
2152 from the template it specializes. (That way, code like:
2153
2154 template <class T> void f(T = 3);
2155 template <> void f(double);
2156 void g () { f (); }
2157
2158 works, as required.) An alternative approach would be to look up
2159 the correct default arguments at the call-site, but this approach
2160 is consistent with how implicit instantiations are handled. */
2161
2162 static void
2163 copy_default_args_to_explicit_spec (tree decl)
2164 {
2165 tree tmpl;
2166 tree spec_types;
2167 tree tmpl_types;
2168 tree new_spec_types;
2169 tree old_type;
2170 tree new_type;
2171 tree t;
2172 tree object_type = NULL_TREE;
2173 tree in_charge = NULL_TREE;
2174 tree vtt = NULL_TREE;
2175
2176 /* See if there's anything we need to do. */
2177 tmpl = DECL_TI_TEMPLATE (decl);
2178 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2179 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2180 if (TREE_PURPOSE (t))
2181 break;
2182 if (!t)
2183 return;
2184
2185 old_type = TREE_TYPE (decl);
2186 spec_types = TYPE_ARG_TYPES (old_type);
2187
2188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2189 {
2190 /* Remove the this pointer, but remember the object's type for
2191 CV quals. */
2192 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2193 spec_types = TREE_CHAIN (spec_types);
2194 tmpl_types = TREE_CHAIN (tmpl_types);
2195
2196 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2197 {
2198 /* DECL may contain more parameters than TMPL due to the extra
2199 in-charge parameter in constructors and destructors. */
2200 in_charge = spec_types;
2201 spec_types = TREE_CHAIN (spec_types);
2202 }
2203 if (DECL_HAS_VTT_PARM_P (decl))
2204 {
2205 vtt = spec_types;
2206 spec_types = TREE_CHAIN (spec_types);
2207 }
2208 }
2209
2210 /* Compute the merged default arguments. */
2211 new_spec_types =
2212 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2213
2214 /* Compute the new FUNCTION_TYPE. */
2215 if (object_type)
2216 {
2217 if (vtt)
2218 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2219 TREE_VALUE (vtt),
2220 new_spec_types);
2221
2222 if (in_charge)
2223 /* Put the in-charge parameter back. */
2224 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2225 TREE_VALUE (in_charge),
2226 new_spec_types);
2227
2228 new_type = build_method_type_directly (object_type,
2229 TREE_TYPE (old_type),
2230 new_spec_types);
2231 }
2232 else
2233 new_type = build_function_type (TREE_TYPE (old_type),
2234 new_spec_types);
2235 new_type = cp_build_type_attribute_variant (new_type,
2236 TYPE_ATTRIBUTES (old_type));
2237 new_type = build_exception_variant (new_type,
2238 TYPE_RAISES_EXCEPTIONS (old_type));
2239 TREE_TYPE (decl) = new_type;
2240 }
2241
2242 /* Return the number of template headers we expect to see for a definition
2243 or specialization of CTYPE or one of its non-template members. */
2244
2245 int
2246 num_template_headers_for_class (tree ctype)
2247 {
2248 int num_templates = 0;
2249
2250 while (ctype && CLASS_TYPE_P (ctype))
2251 {
2252 /* You're supposed to have one `template <...>' for every
2253 template class, but you don't need one for a full
2254 specialization. For example:
2255
2256 template <class T> struct S{};
2257 template <> struct S<int> { void f(); };
2258 void S<int>::f () {}
2259
2260 is correct; there shouldn't be a `template <>' for the
2261 definition of `S<int>::f'. */
2262 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2263 /* If CTYPE does not have template information of any
2264 kind, then it is not a template, nor is it nested
2265 within a template. */
2266 break;
2267 if (explicit_class_specialization_p (ctype))
2268 break;
2269 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2270 ++num_templates;
2271
2272 ctype = TYPE_CONTEXT (ctype);
2273 }
2274
2275 return num_templates;
2276 }
2277
2278 /* Do a simple sanity check on the template headers that precede the
2279 variable declaration DECL. */
2280
2281 void
2282 check_template_variable (tree decl)
2283 {
2284 tree ctx = CP_DECL_CONTEXT (decl);
2285 int wanted = num_template_headers_for_class (ctx);
2286 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2287 permerror (DECL_SOURCE_LOCATION (decl),
2288 "%qD is not a static data member of a class template", decl);
2289 else if (template_header_count > wanted)
2290 {
2291 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2292 "too many template headers for %D (should be %d)",
2293 decl, wanted);
2294 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2295 inform (DECL_SOURCE_LOCATION (decl),
2296 "members of an explicitly specialized class are defined "
2297 "without a template header");
2298 }
2299 }
2300
2301 /* Check to see if the function just declared, as indicated in
2302 DECLARATOR, and in DECL, is a specialization of a function
2303 template. We may also discover that the declaration is an explicit
2304 instantiation at this point.
2305
2306 Returns DECL, or an equivalent declaration that should be used
2307 instead if all goes well. Issues an error message if something is
2308 amiss. Returns error_mark_node if the error is not easily
2309 recoverable.
2310
2311 FLAGS is a bitmask consisting of the following flags:
2312
2313 2: The function has a definition.
2314 4: The function is a friend.
2315
2316 The TEMPLATE_COUNT is the number of references to qualifying
2317 template classes that appeared in the name of the function. For
2318 example, in
2319
2320 template <class T> struct S { void f(); };
2321 void S<int>::f();
2322
2323 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2324 classes are not counted in the TEMPLATE_COUNT, so that in
2325
2326 template <class T> struct S {};
2327 template <> struct S<int> { void f(); }
2328 template <> void S<int>::f();
2329
2330 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2331 invalid; there should be no template <>.)
2332
2333 If the function is a specialization, it is marked as such via
2334 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2335 is set up correctly, and it is added to the list of specializations
2336 for that template. */
2337
2338 tree
2339 check_explicit_specialization (tree declarator,
2340 tree decl,
2341 int template_count,
2342 int flags)
2343 {
2344 int have_def = flags & 2;
2345 int is_friend = flags & 4;
2346 int specialization = 0;
2347 int explicit_instantiation = 0;
2348 int member_specialization = 0;
2349 tree ctype = DECL_CLASS_CONTEXT (decl);
2350 tree dname = DECL_NAME (decl);
2351 tmpl_spec_kind tsk;
2352
2353 if (is_friend)
2354 {
2355 if (!processing_specialization)
2356 tsk = tsk_none;
2357 else
2358 tsk = tsk_excessive_parms;
2359 }
2360 else
2361 tsk = current_tmpl_spec_kind (template_count);
2362
2363 switch (tsk)
2364 {
2365 case tsk_none:
2366 if (processing_specialization)
2367 {
2368 specialization = 1;
2369 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2370 }
2371 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2372 {
2373 if (is_friend)
2374 /* This could be something like:
2375
2376 template <class T> void f(T);
2377 class S { friend void f<>(int); } */
2378 specialization = 1;
2379 else
2380 {
2381 /* This case handles bogus declarations like template <>
2382 template <class T> void f<int>(); */
2383
2384 error ("template-id %qD in declaration of primary template",
2385 declarator);
2386 return decl;
2387 }
2388 }
2389 break;
2390
2391 case tsk_invalid_member_spec:
2392 /* The error has already been reported in
2393 check_specialization_scope. */
2394 return error_mark_node;
2395
2396 case tsk_invalid_expl_inst:
2397 error ("template parameter list used in explicit instantiation");
2398
2399 /* Fall through. */
2400
2401 case tsk_expl_inst:
2402 if (have_def)
2403 error ("definition provided for explicit instantiation");
2404
2405 explicit_instantiation = 1;
2406 break;
2407
2408 case tsk_excessive_parms:
2409 case tsk_insufficient_parms:
2410 if (tsk == tsk_excessive_parms)
2411 error ("too many template parameter lists in declaration of %qD",
2412 decl);
2413 else if (template_header_count)
2414 error("too few template parameter lists in declaration of %qD", decl);
2415 else
2416 error("explicit specialization of %qD must be introduced by "
2417 "%<template <>%>", decl);
2418
2419 /* Fall through. */
2420 case tsk_expl_spec:
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2422 if (ctype)
2423 member_specialization = 1;
2424 else
2425 specialization = 1;
2426 break;
2427
2428 case tsk_template:
2429 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2430 {
2431 /* This case handles bogus declarations like template <>
2432 template <class T> void f<int>(); */
2433
2434 if (uses_template_parms (declarator))
2435 error ("function template partial specialization %qD "
2436 "is not allowed", declarator);
2437 else
2438 error ("template-id %qD in declaration of primary template",
2439 declarator);
2440 return decl;
2441 }
2442
2443 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2444 /* This is a specialization of a member template, without
2445 specialization the containing class. Something like:
2446
2447 template <class T> struct S {
2448 template <class U> void f (U);
2449 };
2450 template <> template <class U> void S<int>::f(U) {}
2451
2452 That's a specialization -- but of the entire template. */
2453 specialization = 1;
2454 break;
2455
2456 default:
2457 gcc_unreachable ();
2458 }
2459
2460 if (specialization || member_specialization)
2461 {
2462 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2463 for (; t; t = TREE_CHAIN (t))
2464 if (TREE_PURPOSE (t))
2465 {
2466 permerror (input_location,
2467 "default argument specified in explicit specialization");
2468 break;
2469 }
2470 }
2471
2472 if (specialization || member_specialization || explicit_instantiation)
2473 {
2474 tree tmpl = NULL_TREE;
2475 tree targs = NULL_TREE;
2476
2477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2478 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2479 {
2480 tree fns;
2481
2482 gcc_assert (identifier_p (declarator));
2483 if (ctype)
2484 fns = dname;
2485 else
2486 {
2487 /* If there is no class context, the explicit instantiation
2488 must be at namespace scope. */
2489 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2490
2491 /* Find the namespace binding, using the declaration
2492 context. */
2493 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2494 false, true);
2495 if (fns == error_mark_node || !is_overloaded_fn (fns))
2496 {
2497 error ("%qD is not a template function", dname);
2498 fns = error_mark_node;
2499 }
2500 else
2501 {
2502 tree fn = OVL_CURRENT (fns);
2503 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2504 CP_DECL_CONTEXT (fn)))
2505 error ("%qD is not declared in %qD",
2506 decl, current_namespace);
2507 }
2508 }
2509
2510 declarator = lookup_template_function (fns, NULL_TREE);
2511 }
2512
2513 if (declarator == error_mark_node)
2514 return error_mark_node;
2515
2516 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2517 {
2518 if (!explicit_instantiation)
2519 /* A specialization in class scope. This is invalid,
2520 but the error will already have been flagged by
2521 check_specialization_scope. */
2522 return error_mark_node;
2523 else
2524 {
2525 /* It's not valid to write an explicit instantiation in
2526 class scope, e.g.:
2527
2528 class C { template void f(); }
2529
2530 This case is caught by the parser. However, on
2531 something like:
2532
2533 template class C { void f(); };
2534
2535 (which is invalid) we can get here. The error will be
2536 issued later. */
2537 ;
2538 }
2539
2540 return decl;
2541 }
2542 else if (ctype != NULL_TREE
2543 && (identifier_p (TREE_OPERAND (declarator, 0))))
2544 {
2545 /* Find the list of functions in ctype that have the same
2546 name as the declared function. */
2547 tree name = TREE_OPERAND (declarator, 0);
2548 tree fns = NULL_TREE;
2549 int idx;
2550
2551 if (constructor_name_p (name, ctype))
2552 {
2553 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2554
2555 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2556 : !CLASSTYPE_DESTRUCTORS (ctype))
2557 {
2558 /* From [temp.expl.spec]:
2559
2560 If such an explicit specialization for the member
2561 of a class template names an implicitly-declared
2562 special member function (clause _special_), the
2563 program is ill-formed.
2564
2565 Similar language is found in [temp.explicit]. */
2566 error ("specialization of implicitly-declared special member function");
2567 return error_mark_node;
2568 }
2569
2570 name = is_constructor ? ctor_identifier : dtor_identifier;
2571 }
2572
2573 if (!DECL_CONV_FN_P (decl))
2574 {
2575 idx = lookup_fnfields_1 (ctype, name);
2576 if (idx >= 0)
2577 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2578 }
2579 else
2580 {
2581 vec<tree, va_gc> *methods;
2582 tree ovl;
2583
2584 /* For a type-conversion operator, we cannot do a
2585 name-based lookup. We might be looking for `operator
2586 int' which will be a specialization of `operator T'.
2587 So, we find *all* the conversion operators, and then
2588 select from them. */
2589 fns = NULL_TREE;
2590
2591 methods = CLASSTYPE_METHOD_VEC (ctype);
2592 if (methods)
2593 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2594 methods->iterate (idx, &ovl);
2595 ++idx)
2596 {
2597 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2598 /* There are no more conversion functions. */
2599 break;
2600
2601 /* Glue all these conversion functions together
2602 with those we already have. */
2603 for (; ovl; ovl = OVL_NEXT (ovl))
2604 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2605 }
2606 }
2607
2608 if (fns == NULL_TREE)
2609 {
2610 error ("no member function %qD declared in %qT", name, ctype);
2611 return error_mark_node;
2612 }
2613 else
2614 TREE_OPERAND (declarator, 0) = fns;
2615 }
2616
2617 /* Figure out what exactly is being specialized at this point.
2618 Note that for an explicit instantiation, even one for a
2619 member function, we cannot tell apriori whether the
2620 instantiation is for a member template, or just a member
2621 function of a template class. Even if a member template is
2622 being instantiated, the member template arguments may be
2623 elided if they can be deduced from the rest of the
2624 declaration. */
2625 tmpl = determine_specialization (declarator, decl,
2626 &targs,
2627 member_specialization,
2628 template_count,
2629 tsk);
2630
2631 if (!tmpl || tmpl == error_mark_node)
2632 /* We couldn't figure out what this declaration was
2633 specializing. */
2634 return error_mark_node;
2635 else
2636 {
2637 tree gen_tmpl = most_general_template (tmpl);
2638
2639 if (explicit_instantiation)
2640 {
2641 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2642 is done by do_decl_instantiation later. */
2643
2644 int arg_depth = TMPL_ARGS_DEPTH (targs);
2645 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2646
2647 if (arg_depth > parm_depth)
2648 {
2649 /* If TMPL is not the most general template (for
2650 example, if TMPL is a friend template that is
2651 injected into namespace scope), then there will
2652 be too many levels of TARGS. Remove some of them
2653 here. */
2654 int i;
2655 tree new_targs;
2656
2657 new_targs = make_tree_vec (parm_depth);
2658 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2659 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2660 = TREE_VEC_ELT (targs, i);
2661 targs = new_targs;
2662 }
2663
2664 return instantiate_template (tmpl, targs, tf_error);
2665 }
2666
2667 /* If we thought that the DECL was a member function, but it
2668 turns out to be specializing a static member function,
2669 make DECL a static member function as well. */
2670 if (DECL_STATIC_FUNCTION_P (tmpl)
2671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2672 revert_static_member_fn (decl);
2673
2674 /* If this is a specialization of a member template of a
2675 template class, we want to return the TEMPLATE_DECL, not
2676 the specialization of it. */
2677 if (tsk == tsk_template)
2678 {
2679 tree result = DECL_TEMPLATE_RESULT (tmpl);
2680 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2681 DECL_INITIAL (result) = NULL_TREE;
2682 if (have_def)
2683 {
2684 tree parm;
2685 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2686 DECL_SOURCE_LOCATION (result)
2687 = DECL_SOURCE_LOCATION (decl);
2688 /* We want to use the argument list specified in the
2689 definition, not in the original declaration. */
2690 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2691 for (parm = DECL_ARGUMENTS (result); parm;
2692 parm = DECL_CHAIN (parm))
2693 DECL_CONTEXT (parm) = result;
2694 }
2695 return register_specialization (tmpl, gen_tmpl, targs,
2696 is_friend, 0);
2697 }
2698
2699 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2700 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2701
2702 /* Inherit default function arguments from the template
2703 DECL is specializing. */
2704 copy_default_args_to_explicit_spec (decl);
2705
2706 /* This specialization has the same protection as the
2707 template it specializes. */
2708 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2709 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2710
2711 /* 7.1.1-1 [dcl.stc]
2712
2713 A storage-class-specifier shall not be specified in an
2714 explicit specialization...
2715
2716 The parser rejects these, so unless action is taken here,
2717 explicit function specializations will always appear with
2718 global linkage.
2719
2720 The action recommended by the C++ CWG in response to C++
2721 defect report 605 is to make the storage class and linkage
2722 of the explicit specialization match the templated function:
2723
2724 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2725 */
2726 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2727 {
2728 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2729 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2730
2731 /* This specialization has the same linkage and visibility as
2732 the function template it specializes. */
2733 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2734 if (! TREE_PUBLIC (decl))
2735 {
2736 DECL_INTERFACE_KNOWN (decl) = 1;
2737 DECL_NOT_REALLY_EXTERN (decl) = 1;
2738 }
2739 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2740 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2741 {
2742 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2743 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2744 }
2745 }
2746
2747 /* If DECL is a friend declaration, declared using an
2748 unqualified name, the namespace associated with DECL may
2749 have been set incorrectly. For example, in:
2750
2751 template <typename T> void f(T);
2752 namespace N {
2753 struct S { friend void f<int>(int); }
2754 }
2755
2756 we will have set the DECL_CONTEXT for the friend
2757 declaration to N, rather than to the global namespace. */
2758 if (DECL_NAMESPACE_SCOPE_P (decl))
2759 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2760
2761 if (is_friend && !have_def)
2762 /* This is not really a declaration of a specialization.
2763 It's just the name of an instantiation. But, it's not
2764 a request for an instantiation, either. */
2765 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2766 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2767 /* This is indeed a specialization. In case of constructors
2768 and destructors, we need in-charge and not-in-charge
2769 versions in V3 ABI. */
2770 clone_function_decl (decl, /*update_method_vec_p=*/0);
2771
2772 /* Register this specialization so that we can find it
2773 again. */
2774 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2775 }
2776 }
2777
2778 return decl;
2779 }
2780
2781 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2782 parameters. These are represented in the same format used for
2783 DECL_TEMPLATE_PARMS. */
2784
2785 int
2786 comp_template_parms (const_tree parms1, const_tree parms2)
2787 {
2788 const_tree p1;
2789 const_tree p2;
2790
2791 if (parms1 == parms2)
2792 return 1;
2793
2794 for (p1 = parms1, p2 = parms2;
2795 p1 != NULL_TREE && p2 != NULL_TREE;
2796 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2797 {
2798 tree t1 = TREE_VALUE (p1);
2799 tree t2 = TREE_VALUE (p2);
2800 int i;
2801
2802 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2803 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2804
2805 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2806 return 0;
2807
2808 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2809 {
2810 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2811 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2812
2813 /* If either of the template parameters are invalid, assume
2814 they match for the sake of error recovery. */
2815 if (parm1 == error_mark_node || parm2 == error_mark_node)
2816 return 1;
2817
2818 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2819 return 0;
2820
2821 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2822 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2823 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2824 continue;
2825 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2826 return 0;
2827 }
2828 }
2829
2830 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2831 /* One set of parameters has more parameters lists than the
2832 other. */
2833 return 0;
2834
2835 return 1;
2836 }
2837
2838 /* Determine whether PARM is a parameter pack. */
2839
2840 bool
2841 template_parameter_pack_p (const_tree parm)
2842 {
2843 /* Determine if we have a non-type template parameter pack. */
2844 if (TREE_CODE (parm) == PARM_DECL)
2845 return (DECL_TEMPLATE_PARM_P (parm)
2846 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2847 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2848 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2849
2850 /* If this is a list of template parameters, we could get a
2851 TYPE_DECL or a TEMPLATE_DECL. */
2852 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2853 parm = TREE_TYPE (parm);
2854
2855 /* Otherwise it must be a type template parameter. */
2856 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2857 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2858 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2859 }
2860
2861 /* Determine if T is a function parameter pack. */
2862
2863 bool
2864 function_parameter_pack_p (const_tree t)
2865 {
2866 if (t && TREE_CODE (t) == PARM_DECL)
2867 return DECL_PACK_P (t);
2868 return false;
2869 }
2870
2871 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2872 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2873
2874 tree
2875 get_function_template_decl (const_tree primary_func_tmpl_inst)
2876 {
2877 if (! primary_func_tmpl_inst
2878 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2879 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2880 return NULL;
2881
2882 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2883 }
2884
2885 /* Return true iff the function parameter PARAM_DECL was expanded
2886 from the function parameter pack PACK. */
2887
2888 bool
2889 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2890 {
2891 if (DECL_ARTIFICIAL (param_decl)
2892 || !function_parameter_pack_p (pack))
2893 return false;
2894
2895 /* The parameter pack and its pack arguments have the same
2896 DECL_PARM_INDEX. */
2897 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2898 }
2899
2900 /* Determine whether ARGS describes a variadic template args list,
2901 i.e., one that is terminated by a template argument pack. */
2902
2903 static bool
2904 template_args_variadic_p (tree args)
2905 {
2906 int nargs;
2907 tree last_parm;
2908
2909 if (args == NULL_TREE)
2910 return false;
2911
2912 args = INNERMOST_TEMPLATE_ARGS (args);
2913 nargs = TREE_VEC_LENGTH (args);
2914
2915 if (nargs == 0)
2916 return false;
2917
2918 last_parm = TREE_VEC_ELT (args, nargs - 1);
2919
2920 return ARGUMENT_PACK_P (last_parm);
2921 }
2922
2923 /* Generate a new name for the parameter pack name NAME (an
2924 IDENTIFIER_NODE) that incorporates its */
2925
2926 static tree
2927 make_ith_pack_parameter_name (tree name, int i)
2928 {
2929 /* Munge the name to include the parameter index. */
2930 #define NUMBUF_LEN 128
2931 char numbuf[NUMBUF_LEN];
2932 char* newname;
2933 int newname_len;
2934
2935 if (name == NULL_TREE)
2936 return name;
2937 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2938 newname_len = IDENTIFIER_LENGTH (name)
2939 + strlen (numbuf) + 2;
2940 newname = (char*)alloca (newname_len);
2941 snprintf (newname, newname_len,
2942 "%s#%i", IDENTIFIER_POINTER (name), i);
2943 return get_identifier (newname);
2944 }
2945
2946 /* Return true if T is a primary function, class or alias template
2947 instantiation. */
2948
2949 bool
2950 primary_template_instantiation_p (const_tree t)
2951 {
2952 if (!t)
2953 return false;
2954
2955 if (TREE_CODE (t) == FUNCTION_DECL)
2956 return DECL_LANG_SPECIFIC (t)
2957 && DECL_TEMPLATE_INSTANTIATION (t)
2958 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2959 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2960 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2961 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2962 else if (alias_template_specialization_p (t))
2963 return true;
2964 return false;
2965 }
2966
2967 /* Return true if PARM is a template template parameter. */
2968
2969 bool
2970 template_template_parameter_p (const_tree parm)
2971 {
2972 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2973 }
2974
2975 /* Return true iff PARM is a DECL representing a type template
2976 parameter. */
2977
2978 bool
2979 template_type_parameter_p (const_tree parm)
2980 {
2981 return (parm
2982 && (TREE_CODE (parm) == TYPE_DECL
2983 || TREE_CODE (parm) == TEMPLATE_DECL)
2984 && DECL_TEMPLATE_PARM_P (parm));
2985 }
2986
2987 /* Return the template parameters of T if T is a
2988 primary template instantiation, NULL otherwise. */
2989
2990 tree
2991 get_primary_template_innermost_parameters (const_tree t)
2992 {
2993 tree parms = NULL, template_info = NULL;
2994
2995 if ((template_info = get_template_info (t))
2996 && primary_template_instantiation_p (t))
2997 parms = INNERMOST_TEMPLATE_PARMS
2998 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2999
3000 return parms;
3001 }
3002
3003 /* Return the template parameters of the LEVELth level from the full list
3004 of template parameters PARMS. */
3005
3006 tree
3007 get_template_parms_at_level (tree parms, int level)
3008 {
3009 tree p;
3010 if (!parms
3011 || TREE_CODE (parms) != TREE_LIST
3012 || level > TMPL_PARMS_DEPTH (parms))
3013 return NULL_TREE;
3014
3015 for (p = parms; p; p = TREE_CHAIN (p))
3016 if (TMPL_PARMS_DEPTH (p) == level)
3017 return p;
3018
3019 return NULL_TREE;
3020 }
3021
3022 /* Returns the template arguments of T if T is a template instantiation,
3023 NULL otherwise. */
3024
3025 tree
3026 get_template_innermost_arguments (const_tree t)
3027 {
3028 tree args = NULL, template_info = NULL;
3029
3030 if ((template_info = get_template_info (t))
3031 && TI_ARGS (template_info))
3032 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3033
3034 return args;
3035 }
3036
3037 /* Return the argument pack elements of T if T is a template argument pack,
3038 NULL otherwise. */
3039
3040 tree
3041 get_template_argument_pack_elems (const_tree t)
3042 {
3043 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3044 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3045 return NULL;
3046
3047 return ARGUMENT_PACK_ARGS (t);
3048 }
3049
3050 /* Structure used to track the progress of find_parameter_packs_r. */
3051 struct find_parameter_pack_data
3052 {
3053 /* TREE_LIST that will contain all of the parameter packs found by
3054 the traversal. */
3055 tree* parameter_packs;
3056
3057 /* Set of AST nodes that have been visited by the traversal. */
3058 struct pointer_set_t *visited;
3059 };
3060
3061 /* Identifies all of the argument packs that occur in a template
3062 argument and appends them to the TREE_LIST inside DATA, which is a
3063 find_parameter_pack_data structure. This is a subroutine of
3064 make_pack_expansion and uses_parameter_packs. */
3065 static tree
3066 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3067 {
3068 tree t = *tp;
3069 struct find_parameter_pack_data* ppd =
3070 (struct find_parameter_pack_data*)data;
3071 bool parameter_pack_p = false;
3072
3073 /* Handle type aliases/typedefs. */
3074 if (TYPE_ALIAS_P (t))
3075 {
3076 if (TYPE_TEMPLATE_INFO (t))
3077 cp_walk_tree (&TYPE_TI_ARGS (t),
3078 &find_parameter_packs_r,
3079 ppd, ppd->visited);
3080 *walk_subtrees = 0;
3081 return NULL_TREE;
3082 }
3083
3084 /* Identify whether this is a parameter pack or not. */
3085 switch (TREE_CODE (t))
3086 {
3087 case TEMPLATE_PARM_INDEX:
3088 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3089 parameter_pack_p = true;
3090 break;
3091
3092 case TEMPLATE_TYPE_PARM:
3093 t = TYPE_MAIN_VARIANT (t);
3094 case TEMPLATE_TEMPLATE_PARM:
3095 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3096 parameter_pack_p = true;
3097 break;
3098
3099 case FIELD_DECL:
3100 case PARM_DECL:
3101 if (DECL_PACK_P (t))
3102 {
3103 /* We don't want to walk into the type of a PARM_DECL,
3104 because we don't want to see the type parameter pack. */
3105 *walk_subtrees = 0;
3106 parameter_pack_p = true;
3107 }
3108 break;
3109
3110 /* Look through a lambda capture proxy to the field pack. */
3111 case VAR_DECL:
3112 if (DECL_HAS_VALUE_EXPR_P (t))
3113 {
3114 tree v = DECL_VALUE_EXPR (t);
3115 cp_walk_tree (&v,
3116 &find_parameter_packs_r,
3117 ppd, ppd->visited);
3118 *walk_subtrees = 0;
3119 }
3120 break;
3121
3122 case BASES:
3123 parameter_pack_p = true;
3124 break;
3125 default:
3126 /* Not a parameter pack. */
3127 break;
3128 }
3129
3130 if (parameter_pack_p)
3131 {
3132 /* Add this parameter pack to the list. */
3133 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3134 }
3135
3136 if (TYPE_P (t))
3137 cp_walk_tree (&TYPE_CONTEXT (t),
3138 &find_parameter_packs_r, ppd, ppd->visited);
3139
3140 /* This switch statement will return immediately if we don't find a
3141 parameter pack. */
3142 switch (TREE_CODE (t))
3143 {
3144 case TEMPLATE_PARM_INDEX:
3145 return NULL_TREE;
3146
3147 case BOUND_TEMPLATE_TEMPLATE_PARM:
3148 /* Check the template itself. */
3149 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3150 &find_parameter_packs_r, ppd, ppd->visited);
3151 /* Check the template arguments. */
3152 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3153 ppd->visited);
3154 *walk_subtrees = 0;
3155 return NULL_TREE;
3156
3157 case TEMPLATE_TYPE_PARM:
3158 case TEMPLATE_TEMPLATE_PARM:
3159 return NULL_TREE;
3160
3161 case PARM_DECL:
3162 return NULL_TREE;
3163
3164 case RECORD_TYPE:
3165 if (TYPE_PTRMEMFUNC_P (t))
3166 return NULL_TREE;
3167 /* Fall through. */
3168
3169 case UNION_TYPE:
3170 case ENUMERAL_TYPE:
3171 if (TYPE_TEMPLATE_INFO (t))
3172 cp_walk_tree (&TYPE_TI_ARGS (t),
3173 &find_parameter_packs_r, ppd, ppd->visited);
3174
3175 *walk_subtrees = 0;
3176 return NULL_TREE;
3177
3178 case CONSTRUCTOR:
3179 case TEMPLATE_DECL:
3180 cp_walk_tree (&TREE_TYPE (t),
3181 &find_parameter_packs_r, ppd, ppd->visited);
3182 return NULL_TREE;
3183
3184 case TYPENAME_TYPE:
3185 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3186 ppd, ppd->visited);
3187 *walk_subtrees = 0;
3188 return NULL_TREE;
3189
3190 case TYPE_PACK_EXPANSION:
3191 case EXPR_PACK_EXPANSION:
3192 *walk_subtrees = 0;
3193 return NULL_TREE;
3194
3195 case INTEGER_TYPE:
3196 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3197 ppd, ppd->visited);
3198 *walk_subtrees = 0;
3199 return NULL_TREE;
3200
3201 case IDENTIFIER_NODE:
3202 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3203 ppd->visited);
3204 *walk_subtrees = 0;
3205 return NULL_TREE;
3206
3207 default:
3208 return NULL_TREE;
3209 }
3210
3211 return NULL_TREE;
3212 }
3213
3214 /* Determines if the expression or type T uses any parameter packs. */
3215 bool
3216 uses_parameter_packs (tree t)
3217 {
3218 tree parameter_packs = NULL_TREE;
3219 struct find_parameter_pack_data ppd;
3220 ppd.parameter_packs = &parameter_packs;
3221 ppd.visited = pointer_set_create ();
3222 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3223 pointer_set_destroy (ppd.visited);
3224 return parameter_packs != NULL_TREE;
3225 }
3226
3227 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3228 representation a base-class initializer into a parameter pack
3229 expansion. If all goes well, the resulting node will be an
3230 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3231 respectively. */
3232 tree
3233 make_pack_expansion (tree arg)
3234 {
3235 tree result;
3236 tree parameter_packs = NULL_TREE;
3237 bool for_types = false;
3238 struct find_parameter_pack_data ppd;
3239
3240 if (!arg || arg == error_mark_node)
3241 return arg;
3242
3243 if (TREE_CODE (arg) == TREE_LIST)
3244 {
3245 /* The only time we will see a TREE_LIST here is for a base
3246 class initializer. In this case, the TREE_PURPOSE will be a
3247 _TYPE node (representing the base class expansion we're
3248 initializing) and the TREE_VALUE will be a TREE_LIST
3249 containing the initialization arguments.
3250
3251 The resulting expansion looks somewhat different from most
3252 expansions. Rather than returning just one _EXPANSION, we
3253 return a TREE_LIST whose TREE_PURPOSE is a
3254 TYPE_PACK_EXPANSION containing the bases that will be
3255 initialized. The TREE_VALUE will be identical to the
3256 original TREE_VALUE, which is a list of arguments that will
3257 be passed to each base. We do not introduce any new pack
3258 expansion nodes into the TREE_VALUE (although it is possible
3259 that some already exist), because the TREE_PURPOSE and
3260 TREE_VALUE all need to be expanded together with the same
3261 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3262 resulting TREE_PURPOSE will mention the parameter packs in
3263 both the bases and the arguments to the bases. */
3264 tree purpose;
3265 tree value;
3266 tree parameter_packs = NULL_TREE;
3267
3268 /* Determine which parameter packs will be used by the base
3269 class expansion. */
3270 ppd.visited = pointer_set_create ();
3271 ppd.parameter_packs = &parameter_packs;
3272 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3273 &ppd, ppd.visited);
3274
3275 if (parameter_packs == NULL_TREE)
3276 {
3277 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3278 pointer_set_destroy (ppd.visited);
3279 return error_mark_node;
3280 }
3281
3282 if (TREE_VALUE (arg) != void_type_node)
3283 {
3284 /* Collect the sets of parameter packs used in each of the
3285 initialization arguments. */
3286 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3287 {
3288 /* Determine which parameter packs will be expanded in this
3289 argument. */
3290 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3291 &ppd, ppd.visited);
3292 }
3293 }
3294
3295 pointer_set_destroy (ppd.visited);
3296
3297 /* Create the pack expansion type for the base type. */
3298 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3299 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3300 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3301
3302 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3303 they will rarely be compared to anything. */
3304 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3305
3306 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3307 }
3308
3309 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3310 for_types = true;
3311
3312 /* Build the PACK_EXPANSION_* node. */
3313 result = for_types
3314 ? cxx_make_type (TYPE_PACK_EXPANSION)
3315 : make_node (EXPR_PACK_EXPANSION);
3316 SET_PACK_EXPANSION_PATTERN (result, arg);
3317 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3318 {
3319 /* Propagate type and const-expression information. */
3320 TREE_TYPE (result) = TREE_TYPE (arg);
3321 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3322 }
3323 else
3324 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3325 they will rarely be compared to anything. */
3326 SET_TYPE_STRUCTURAL_EQUALITY (result);
3327
3328 /* Determine which parameter packs will be expanded. */
3329 ppd.parameter_packs = &parameter_packs;
3330 ppd.visited = pointer_set_create ();
3331 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3332 pointer_set_destroy (ppd.visited);
3333
3334 /* Make sure we found some parameter packs. */
3335 if (parameter_packs == NULL_TREE)
3336 {
3337 if (TYPE_P (arg))
3338 error ("expansion pattern %<%T%> contains no argument packs", arg);
3339 else
3340 error ("expansion pattern %<%E%> contains no argument packs", arg);
3341 return error_mark_node;
3342 }
3343 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3344
3345 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3346
3347 return result;
3348 }
3349
3350 /* Checks T for any "bare" parameter packs, which have not yet been
3351 expanded, and issues an error if any are found. This operation can
3352 only be done on full expressions or types (e.g., an expression
3353 statement, "if" condition, etc.), because we could have expressions like:
3354
3355 foo(f(g(h(args)))...)
3356
3357 where "args" is a parameter pack. check_for_bare_parameter_packs
3358 should not be called for the subexpressions args, h(args),
3359 g(h(args)), or f(g(h(args))), because we would produce erroneous
3360 error messages.
3361
3362 Returns TRUE and emits an error if there were bare parameter packs,
3363 returns FALSE otherwise. */
3364 bool
3365 check_for_bare_parameter_packs (tree t)
3366 {
3367 tree parameter_packs = NULL_TREE;
3368 struct find_parameter_pack_data ppd;
3369
3370 if (!processing_template_decl || !t || t == error_mark_node)
3371 return false;
3372
3373 if (TREE_CODE (t) == TYPE_DECL)
3374 t = TREE_TYPE (t);
3375
3376 ppd.parameter_packs = &parameter_packs;
3377 ppd.visited = pointer_set_create ();
3378 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3379 pointer_set_destroy (ppd.visited);
3380
3381 if (parameter_packs)
3382 {
3383 error ("parameter packs not expanded with %<...%>:");
3384 while (parameter_packs)
3385 {
3386 tree pack = TREE_VALUE (parameter_packs);
3387 tree name = NULL_TREE;
3388
3389 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3390 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3391 name = TYPE_NAME (pack);
3392 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3393 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3394 else
3395 name = DECL_NAME (pack);
3396
3397 if (name)
3398 inform (input_location, " %qD", name);
3399 else
3400 inform (input_location, " <anonymous>");
3401
3402 parameter_packs = TREE_CHAIN (parameter_packs);
3403 }
3404
3405 return true;
3406 }
3407
3408 return false;
3409 }
3410
3411 /* Expand any parameter packs that occur in the template arguments in
3412 ARGS. */
3413 tree
3414 expand_template_argument_pack (tree args)
3415 {
3416 tree result_args = NULL_TREE;
3417 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3418 int num_result_args = -1;
3419 int non_default_args_count = -1;
3420
3421 /* First, determine if we need to expand anything, and the number of
3422 slots we'll need. */
3423 for (in_arg = 0; in_arg < nargs; ++in_arg)
3424 {
3425 tree arg = TREE_VEC_ELT (args, in_arg);
3426 if (arg == NULL_TREE)
3427 return args;
3428 if (ARGUMENT_PACK_P (arg))
3429 {
3430 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3431 if (num_result_args < 0)
3432 num_result_args = in_arg + num_packed;
3433 else
3434 num_result_args += num_packed;
3435 }
3436 else
3437 {
3438 if (num_result_args >= 0)
3439 num_result_args++;
3440 }
3441 }
3442
3443 /* If no expansion is necessary, we're done. */
3444 if (num_result_args < 0)
3445 return args;
3446
3447 /* Expand arguments. */
3448 result_args = make_tree_vec (num_result_args);
3449 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3450 non_default_args_count =
3451 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3452 for (in_arg = 0; in_arg < nargs; ++in_arg)
3453 {
3454 tree arg = TREE_VEC_ELT (args, in_arg);
3455 if (ARGUMENT_PACK_P (arg))
3456 {
3457 tree packed = ARGUMENT_PACK_ARGS (arg);
3458 int i, num_packed = TREE_VEC_LENGTH (packed);
3459 for (i = 0; i < num_packed; ++i, ++out_arg)
3460 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3461 if (non_default_args_count > 0)
3462 non_default_args_count += num_packed;
3463 }
3464 else
3465 {
3466 TREE_VEC_ELT (result_args, out_arg) = arg;
3467 ++out_arg;
3468 }
3469 }
3470 if (non_default_args_count >= 0)
3471 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3472 return result_args;
3473 }
3474
3475 /* Checks if DECL shadows a template parameter.
3476
3477 [temp.local]: A template-parameter shall not be redeclared within its
3478 scope (including nested scopes).
3479
3480 Emits an error and returns TRUE if the DECL shadows a parameter,
3481 returns FALSE otherwise. */
3482
3483 bool
3484 check_template_shadow (tree decl)
3485 {
3486 tree olddecl;
3487
3488 /* If we're not in a template, we can't possibly shadow a template
3489 parameter. */
3490 if (!current_template_parms)
3491 return true;
3492
3493 /* Figure out what we're shadowing. */
3494 if (TREE_CODE (decl) == OVERLOAD)
3495 decl = OVL_CURRENT (decl);
3496 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3497
3498 /* If there's no previous binding for this name, we're not shadowing
3499 anything, let alone a template parameter. */
3500 if (!olddecl)
3501 return true;
3502
3503 /* If we're not shadowing a template parameter, we're done. Note
3504 that OLDDECL might be an OVERLOAD (or perhaps even an
3505 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3506 node. */
3507 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3508 return true;
3509
3510 /* We check for decl != olddecl to avoid bogus errors for using a
3511 name inside a class. We check TPFI to avoid duplicate errors for
3512 inline member templates. */
3513 if (decl == olddecl
3514 || (DECL_TEMPLATE_PARM_P (decl)
3515 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3516 return true;
3517
3518 error ("declaration of %q+#D", decl);
3519 error (" shadows template parm %q+#D", olddecl);
3520 return false;
3521 }
3522
3523 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3524 ORIG_LEVEL, DECL, and TYPE. */
3525
3526 static tree
3527 build_template_parm_index (int index,
3528 int level,
3529 int orig_level,
3530 tree decl,
3531 tree type)
3532 {
3533 tree t = make_node (TEMPLATE_PARM_INDEX);
3534 TEMPLATE_PARM_IDX (t) = index;
3535 TEMPLATE_PARM_LEVEL (t) = level;
3536 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3537 TEMPLATE_PARM_DECL (t) = decl;
3538 TREE_TYPE (t) = type;
3539 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3540 TREE_READONLY (t) = TREE_READONLY (decl);
3541
3542 return t;
3543 }
3544
3545 /* Find the canonical type parameter for the given template type
3546 parameter. Returns the canonical type parameter, which may be TYPE
3547 if no such parameter existed. */
3548
3549 static tree
3550 canonical_type_parameter (tree type)
3551 {
3552 tree list;
3553 int idx = TEMPLATE_TYPE_IDX (type);
3554 if (!canonical_template_parms)
3555 vec_alloc (canonical_template_parms, idx+1);
3556
3557 while (canonical_template_parms->length () <= (unsigned)idx)
3558 vec_safe_push (canonical_template_parms, NULL_TREE);
3559
3560 list = (*canonical_template_parms)[idx];
3561 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3562 list = TREE_CHAIN (list);
3563
3564 if (list)
3565 return TREE_VALUE (list);
3566 else
3567 {
3568 (*canonical_template_parms)[idx]
3569 = tree_cons (NULL_TREE, type,
3570 (*canonical_template_parms)[idx]);
3571 return type;
3572 }
3573 }
3574
3575 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3576 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3577 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3578 new one is created. */
3579
3580 static tree
3581 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3582 tsubst_flags_t complain)
3583 {
3584 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3585 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3586 != TEMPLATE_PARM_LEVEL (index) - levels)
3587 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3588 {
3589 tree orig_decl = TEMPLATE_PARM_DECL (index);
3590 tree decl, t;
3591
3592 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3593 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3594 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3595 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3596 DECL_ARTIFICIAL (decl) = 1;
3597 SET_DECL_TEMPLATE_PARM_P (decl);
3598
3599 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3600 TEMPLATE_PARM_LEVEL (index) - levels,
3601 TEMPLATE_PARM_ORIG_LEVEL (index),
3602 decl, type);
3603 TEMPLATE_PARM_DESCENDANTS (index) = t;
3604 TEMPLATE_PARM_PARAMETER_PACK (t)
3605 = TEMPLATE_PARM_PARAMETER_PACK (index);
3606
3607 /* Template template parameters need this. */
3608 if (TREE_CODE (decl) == TEMPLATE_DECL)
3609 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3610 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3611 args, complain);
3612 }
3613
3614 return TEMPLATE_PARM_DESCENDANTS (index);
3615 }
3616
3617 /* Process information from new template parameter PARM and append it
3618 to the LIST being built. This new parameter is a non-type
3619 parameter iff IS_NON_TYPE is true. This new parameter is a
3620 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3621 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3622 parameter list PARM belongs to. This is used used to create a
3623 proper canonical type for the type of PARM that is to be created,
3624 iff PARM is a type. If the size is not known, this parameter shall
3625 be set to 0. */
3626
3627 tree
3628 process_template_parm (tree list, location_t parm_loc, tree parm,
3629 bool is_non_type, bool is_parameter_pack)
3630 {
3631 tree decl = 0;
3632 tree defval;
3633 tree err_parm_list;
3634 int idx = 0;
3635
3636 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3637 defval = TREE_PURPOSE (parm);
3638
3639 if (list)
3640 {
3641 tree p = tree_last (list);
3642
3643 if (p && TREE_VALUE (p) != error_mark_node)
3644 {
3645 p = TREE_VALUE (p);
3646 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3647 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3648 else
3649 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3650 }
3651
3652 ++idx;
3653 }
3654 else
3655 idx = 0;
3656
3657 if (is_non_type)
3658 {
3659 parm = TREE_VALUE (parm);
3660
3661 SET_DECL_TEMPLATE_PARM_P (parm);
3662
3663 if (TREE_TYPE (parm) == error_mark_node)
3664 {
3665 err_parm_list = build_tree_list (defval, parm);
3666 TREE_VALUE (err_parm_list) = error_mark_node;
3667 return chainon (list, err_parm_list);
3668 }
3669 else
3670 {
3671 /* [temp.param]
3672
3673 The top-level cv-qualifiers on the template-parameter are
3674 ignored when determining its type. */
3675 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3676 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3677 {
3678 err_parm_list = build_tree_list (defval, parm);
3679 TREE_VALUE (err_parm_list) = error_mark_node;
3680 return chainon (list, err_parm_list);
3681 }
3682
3683 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3684 {
3685 /* This template parameter is not a parameter pack, but it
3686 should be. Complain about "bare" parameter packs. */
3687 check_for_bare_parameter_packs (TREE_TYPE (parm));
3688
3689 /* Recover by calling this a parameter pack. */
3690 is_parameter_pack = true;
3691 }
3692 }
3693
3694 /* A template parameter is not modifiable. */
3695 TREE_CONSTANT (parm) = 1;
3696 TREE_READONLY (parm) = 1;
3697 decl = build_decl (parm_loc,
3698 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3699 TREE_CONSTANT (decl) = 1;
3700 TREE_READONLY (decl) = 1;
3701 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3702 = build_template_parm_index (idx, processing_template_decl,
3703 processing_template_decl,
3704 decl, TREE_TYPE (parm));
3705
3706 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3707 = is_parameter_pack;
3708 }
3709 else
3710 {
3711 tree t;
3712 parm = TREE_VALUE (TREE_VALUE (parm));
3713
3714 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3715 {
3716 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3717 /* This is for distinguishing between real templates and template
3718 template parameters */
3719 TREE_TYPE (parm) = t;
3720 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3721 decl = parm;
3722 }
3723 else
3724 {
3725 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3726 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3727 decl = build_decl (parm_loc,
3728 TYPE_DECL, parm, t);
3729 }
3730
3731 TYPE_NAME (t) = decl;
3732 TYPE_STUB_DECL (t) = decl;
3733 parm = decl;
3734 TEMPLATE_TYPE_PARM_INDEX (t)
3735 = build_template_parm_index (idx, processing_template_decl,
3736 processing_template_decl,
3737 decl, TREE_TYPE (parm));
3738 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3739 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3740 }
3741 DECL_ARTIFICIAL (decl) = 1;
3742 SET_DECL_TEMPLATE_PARM_P (decl);
3743 pushdecl (decl);
3744 parm = build_tree_list (defval, parm);
3745 return chainon (list, parm);
3746 }
3747
3748 /* The end of a template parameter list has been reached. Process the
3749 tree list into a parameter vector, converting each parameter into a more
3750 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3751 as PARM_DECLs. */
3752
3753 tree
3754 end_template_parm_list (tree parms)
3755 {
3756 int nparms;
3757 tree parm, next;
3758 tree saved_parmlist = make_tree_vec (list_length (parms));
3759
3760 current_template_parms
3761 = tree_cons (size_int (processing_template_decl),
3762 saved_parmlist, current_template_parms);
3763
3764 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3765 {
3766 next = TREE_CHAIN (parm);
3767 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3768 TREE_CHAIN (parm) = NULL_TREE;
3769 }
3770
3771 --processing_template_parmlist;
3772
3773 return saved_parmlist;
3774 }
3775
3776 /* end_template_decl is called after a template declaration is seen. */
3777
3778 void
3779 end_template_decl (void)
3780 {
3781 reset_specialization ();
3782
3783 if (! processing_template_decl)
3784 return;
3785
3786 /* This matches the pushlevel in begin_template_parm_list. */
3787 finish_scope ();
3788
3789 --processing_template_decl;
3790 current_template_parms = TREE_CHAIN (current_template_parms);
3791 }
3792
3793 /* Takes a TREE_LIST representing a template parameter and convert it
3794 into an argument suitable to be passed to the type substitution
3795 functions. Note that If the TREE_LIST contains an error_mark
3796 node, the returned argument is error_mark_node. */
3797
3798 static tree
3799 template_parm_to_arg (tree t)
3800 {
3801
3802 if (t == NULL_TREE
3803 || TREE_CODE (t) != TREE_LIST)
3804 return t;
3805
3806 if (error_operand_p (TREE_VALUE (t)))
3807 return error_mark_node;
3808
3809 t = TREE_VALUE (t);
3810
3811 if (TREE_CODE (t) == TYPE_DECL
3812 || TREE_CODE (t) == TEMPLATE_DECL)
3813 {
3814 t = TREE_TYPE (t);
3815
3816 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3817 {
3818 /* Turn this argument into a TYPE_ARGUMENT_PACK
3819 with a single element, which expands T. */
3820 tree vec = make_tree_vec (1);
3821 #ifdef ENABLE_CHECKING
3822 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3823 (vec, TREE_VEC_LENGTH (vec));
3824 #endif
3825 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3826
3827 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3828 SET_ARGUMENT_PACK_ARGS (t, vec);
3829 }
3830 }
3831 else
3832 {
3833 t = DECL_INITIAL (t);
3834
3835 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3836 {
3837 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3838 with a single element, which expands T. */
3839 tree vec = make_tree_vec (1);
3840 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3841 #ifdef ENABLE_CHECKING
3842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3843 (vec, TREE_VEC_LENGTH (vec));
3844 #endif
3845 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3846
3847 t = make_node (NONTYPE_ARGUMENT_PACK);
3848 SET_ARGUMENT_PACK_ARGS (t, vec);
3849 TREE_TYPE (t) = type;
3850 }
3851 }
3852 return t;
3853 }
3854
3855 /* Given a set of template parameters, return them as a set of template
3856 arguments. The template parameters are represented as a TREE_VEC, in
3857 the form documented in cp-tree.h for template arguments. */
3858
3859 static tree
3860 template_parms_to_args (tree parms)
3861 {
3862 tree header;
3863 tree args = NULL_TREE;
3864 int length = TMPL_PARMS_DEPTH (parms);
3865 int l = length;
3866
3867 /* If there is only one level of template parameters, we do not
3868 create a TREE_VEC of TREE_VECs. Instead, we return a single
3869 TREE_VEC containing the arguments. */
3870 if (length > 1)
3871 args = make_tree_vec (length);
3872
3873 for (header = parms; header; header = TREE_CHAIN (header))
3874 {
3875 tree a = copy_node (TREE_VALUE (header));
3876 int i;
3877
3878 TREE_TYPE (a) = NULL_TREE;
3879 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3880 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3881
3882 #ifdef ENABLE_CHECKING
3883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3884 #endif
3885
3886 if (length > 1)
3887 TREE_VEC_ELT (args, --l) = a;
3888 else
3889 args = a;
3890 }
3891
3892 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3893 /* This can happen for template parms of a template template
3894 parameter, e.g:
3895
3896 template<template<class T, class U> class TT> struct S;
3897
3898 Consider the level of the parms of TT; T and U both have
3899 level 2; TT has no template parm of level 1. So in this case
3900 the first element of full_template_args is NULL_TREE. If we
3901 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3902 of 2. This will make tsubst wrongly consider that T and U
3903 have level 1. Instead, let's create a dummy vector as the
3904 first element of full_template_args so that TMPL_ARGS_DEPTH
3905 returns the correct depth for args. */
3906 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3907 return args;
3908 }
3909
3910 /* Within the declaration of a template, return the currently active
3911 template parameters as an argument TREE_VEC. */
3912
3913 static tree
3914 current_template_args (void)
3915 {
3916 return template_parms_to_args (current_template_parms);
3917 }
3918
3919 /* Update the declared TYPE by doing any lookups which were thought to be
3920 dependent, but are not now that we know the SCOPE of the declarator. */
3921
3922 tree
3923 maybe_update_decl_type (tree orig_type, tree scope)
3924 {
3925 tree type = orig_type;
3926
3927 if (type == NULL_TREE)
3928 return type;
3929
3930 if (TREE_CODE (orig_type) == TYPE_DECL)
3931 type = TREE_TYPE (type);
3932
3933 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3934 && dependent_type_p (type)
3935 /* Don't bother building up the args in this case. */
3936 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3937 {
3938 /* tsubst in the args corresponding to the template parameters,
3939 including auto if present. Most things will be unchanged, but
3940 make_typename_type and tsubst_qualified_id will resolve
3941 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3942 tree args = current_template_args ();
3943 tree auto_node = type_uses_auto (type);
3944 tree pushed;
3945 if (auto_node)
3946 {
3947 tree auto_vec = make_tree_vec (1);
3948 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3949 args = add_to_template_args (args, auto_vec);
3950 }
3951 pushed = push_scope (scope);
3952 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3953 if (pushed)
3954 pop_scope (scope);
3955 }
3956
3957 if (type == error_mark_node)
3958 return orig_type;
3959
3960 if (TREE_CODE (orig_type) == TYPE_DECL)
3961 {
3962 if (same_type_p (type, TREE_TYPE (orig_type)))
3963 type = orig_type;
3964 else
3965 type = TYPE_NAME (type);
3966 }
3967 return type;
3968 }
3969
3970 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3971 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3972 a member template. Used by push_template_decl below. */
3973
3974 static tree
3975 build_template_decl (tree decl, tree parms, bool member_template_p)
3976 {
3977 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3978 DECL_TEMPLATE_PARMS (tmpl) = parms;
3979 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3980 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3981 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3982
3983 return tmpl;
3984 }
3985
3986 struct template_parm_data
3987 {
3988 /* The level of the template parameters we are currently
3989 processing. */
3990 int level;
3991
3992 /* The index of the specialization argument we are currently
3993 processing. */
3994 int current_arg;
3995
3996 /* An array whose size is the number of template parameters. The
3997 elements are nonzero if the parameter has been used in any one
3998 of the arguments processed so far. */
3999 int* parms;
4000
4001 /* An array whose size is the number of template arguments. The
4002 elements are nonzero if the argument makes use of template
4003 parameters of this level. */
4004 int* arg_uses_template_parms;
4005 };
4006
4007 /* Subroutine of push_template_decl used to see if each template
4008 parameter in a partial specialization is used in the explicit
4009 argument list. If T is of the LEVEL given in DATA (which is
4010 treated as a template_parm_data*), then DATA->PARMS is marked
4011 appropriately. */
4012
4013 static int
4014 mark_template_parm (tree t, void* data)
4015 {
4016 int level;
4017 int idx;
4018 struct template_parm_data* tpd = (struct template_parm_data*) data;
4019
4020 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4021 {
4022 level = TEMPLATE_PARM_LEVEL (t);
4023 idx = TEMPLATE_PARM_IDX (t);
4024 }
4025 else
4026 {
4027 level = TEMPLATE_TYPE_LEVEL (t);
4028 idx = TEMPLATE_TYPE_IDX (t);
4029 }
4030
4031 if (level == tpd->level)
4032 {
4033 tpd->parms[idx] = 1;
4034 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4035 }
4036
4037 /* Return zero so that for_each_template_parm will continue the
4038 traversal of the tree; we want to mark *every* template parm. */
4039 return 0;
4040 }
4041
4042 /* Process the partial specialization DECL. */
4043
4044 static tree
4045 process_partial_specialization (tree decl)
4046 {
4047 tree type = TREE_TYPE (decl);
4048 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4049 tree specargs = CLASSTYPE_TI_ARGS (type);
4050 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4051 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4052 tree inner_parms;
4053 tree inst;
4054 int nargs = TREE_VEC_LENGTH (inner_args);
4055 int ntparms;
4056 int i;
4057 bool did_error_intro = false;
4058 struct template_parm_data tpd;
4059 struct template_parm_data tpd2;
4060
4061 gcc_assert (current_template_parms);
4062
4063 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4064 ntparms = TREE_VEC_LENGTH (inner_parms);
4065
4066 /* We check that each of the template parameters given in the
4067 partial specialization is used in the argument list to the
4068 specialization. For example:
4069
4070 template <class T> struct S;
4071 template <class T> struct S<T*>;
4072
4073 The second declaration is OK because `T*' uses the template
4074 parameter T, whereas
4075
4076 template <class T> struct S<int>;
4077
4078 is no good. Even trickier is:
4079
4080 template <class T>
4081 struct S1
4082 {
4083 template <class U>
4084 struct S2;
4085 template <class U>
4086 struct S2<T>;
4087 };
4088
4089 The S2<T> declaration is actually invalid; it is a
4090 full-specialization. Of course,
4091
4092 template <class U>
4093 struct S2<T (*)(U)>;
4094
4095 or some such would have been OK. */
4096 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4097 tpd.parms = XALLOCAVEC (int, ntparms);
4098 memset (tpd.parms, 0, sizeof (int) * ntparms);
4099
4100 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4101 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4102 for (i = 0; i < nargs; ++i)
4103 {
4104 tpd.current_arg = i;
4105 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4106 &mark_template_parm,
4107 &tpd,
4108 NULL,
4109 /*include_nondeduced_p=*/false);
4110 }
4111 for (i = 0; i < ntparms; ++i)
4112 if (tpd.parms[i] == 0)
4113 {
4114 /* One of the template parms was not used in the
4115 specialization. */
4116 if (!did_error_intro)
4117 {
4118 error ("template parameters not used in partial specialization:");
4119 did_error_intro = true;
4120 }
4121
4122 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4123 }
4124
4125 if (did_error_intro)
4126 return error_mark_node;
4127
4128 /* [temp.class.spec]
4129
4130 The argument list of the specialization shall not be identical to
4131 the implicit argument list of the primary template. */
4132 if (comp_template_args
4133 (inner_args,
4134 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4135 (maintmpl)))))
4136 error ("partial specialization %qT does not specialize any template arguments", type);
4137
4138 /* A partial specialization that replaces multiple parameters of the
4139 primary template with a pack expansion is less specialized for those
4140 parameters. */
4141 if (nargs < DECL_NTPARMS (maintmpl))
4142 {
4143 error ("partial specialization is not more specialized than the "
4144 "primary template because it replaces multiple parameters "
4145 "with a pack expansion");
4146 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4147 return decl;
4148 }
4149
4150 /* [temp.class.spec]
4151
4152 A partially specialized non-type argument expression shall not
4153 involve template parameters of the partial specialization except
4154 when the argument expression is a simple identifier.
4155
4156 The type of a template parameter corresponding to a specialized
4157 non-type argument shall not be dependent on a parameter of the
4158 specialization.
4159
4160 Also, we verify that pack expansions only occur at the
4161 end of the argument list. */
4162 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4163 tpd2.parms = 0;
4164 for (i = 0; i < nargs; ++i)
4165 {
4166 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4167 tree arg = TREE_VEC_ELT (inner_args, i);
4168 tree packed_args = NULL_TREE;
4169 int j, len = 1;
4170
4171 if (ARGUMENT_PACK_P (arg))
4172 {
4173 /* Extract the arguments from the argument pack. We'll be
4174 iterating over these in the following loop. */
4175 packed_args = ARGUMENT_PACK_ARGS (arg);
4176 len = TREE_VEC_LENGTH (packed_args);
4177 }
4178
4179 for (j = 0; j < len; j++)
4180 {
4181 if (packed_args)
4182 /* Get the Jth argument in the parameter pack. */
4183 arg = TREE_VEC_ELT (packed_args, j);
4184
4185 if (PACK_EXPANSION_P (arg))
4186 {
4187 /* Pack expansions must come at the end of the
4188 argument list. */
4189 if ((packed_args && j < len - 1)
4190 || (!packed_args && i < nargs - 1))
4191 {
4192 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4193 error ("parameter pack argument %qE must be at the "
4194 "end of the template argument list", arg);
4195 else
4196 error ("parameter pack argument %qT must be at the "
4197 "end of the template argument list", arg);
4198 }
4199 }
4200
4201 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4202 /* We only care about the pattern. */
4203 arg = PACK_EXPANSION_PATTERN (arg);
4204
4205 if (/* These first two lines are the `non-type' bit. */
4206 !TYPE_P (arg)
4207 && TREE_CODE (arg) != TEMPLATE_DECL
4208 /* This next line is the `argument expression is not just a
4209 simple identifier' condition and also the `specialized
4210 non-type argument' bit. */
4211 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4212 {
4213 if ((!packed_args && tpd.arg_uses_template_parms[i])
4214 || (packed_args && uses_template_parms (arg)))
4215 error ("template argument %qE involves template parameter(s)",
4216 arg);
4217 else
4218 {
4219 /* Look at the corresponding template parameter,
4220 marking which template parameters its type depends
4221 upon. */
4222 tree type = TREE_TYPE (parm);
4223
4224 if (!tpd2.parms)
4225 {
4226 /* We haven't yet initialized TPD2. Do so now. */
4227 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4228 /* The number of parameters here is the number in the
4229 main template, which, as checked in the assertion
4230 above, is NARGS. */
4231 tpd2.parms = XALLOCAVEC (int, nargs);
4232 tpd2.level =
4233 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4234 }
4235
4236 /* Mark the template parameters. But this time, we're
4237 looking for the template parameters of the main
4238 template, not in the specialization. */
4239 tpd2.current_arg = i;
4240 tpd2.arg_uses_template_parms[i] = 0;
4241 memset (tpd2.parms, 0, sizeof (int) * nargs);
4242 for_each_template_parm (type,
4243 &mark_template_parm,
4244 &tpd2,
4245 NULL,
4246 /*include_nondeduced_p=*/false);
4247
4248 if (tpd2.arg_uses_template_parms [i])
4249 {
4250 /* The type depended on some template parameters.
4251 If they are fully specialized in the
4252 specialization, that's OK. */
4253 int j;
4254 int count = 0;
4255 for (j = 0; j < nargs; ++j)
4256 if (tpd2.parms[j] != 0
4257 && tpd.arg_uses_template_parms [j])
4258 ++count;
4259 if (count != 0)
4260 error_n (input_location, count,
4261 "type %qT of template argument %qE depends "
4262 "on a template parameter",
4263 "type %qT of template argument %qE depends "
4264 "on template parameters",
4265 type,
4266 arg);
4267 }
4268 }
4269 }
4270 }
4271 }
4272
4273 /* We should only get here once. */
4274 gcc_assert (!COMPLETE_TYPE_P (type));
4275
4276 tree tmpl = build_template_decl (decl, current_template_parms,
4277 DECL_MEMBER_TEMPLATE_P (maintmpl));
4278 TREE_TYPE (tmpl) = type;
4279 DECL_TEMPLATE_RESULT (tmpl) = decl;
4280 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4281 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4282 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4283
4284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4285 = tree_cons (specargs, tmpl,
4286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4288
4289 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4290 inst = TREE_CHAIN (inst))
4291 {
4292 tree inst_type = TREE_VALUE (inst);
4293 if (COMPLETE_TYPE_P (inst_type)
4294 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4295 {
4296 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4297 if (spec && TREE_TYPE (spec) == type)
4298 permerror (input_location,
4299 "partial specialization of %qT after instantiation "
4300 "of %qT", type, inst_type);
4301 }
4302 }
4303
4304 return decl;
4305 }
4306
4307 /* Check that a template declaration's use of default arguments and
4308 parameter packs is not invalid. Here, PARMS are the template
4309 parameters. IS_PRIMARY is true if DECL is the thing declared by
4310 a primary template. IS_PARTIAL is true if DECL is a partial
4311 specialization.
4312
4313 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4314 declaration (but not a definition); 1 indicates a declaration, 2
4315 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4316 emitted for extraneous default arguments.
4317
4318 Returns TRUE if there were no errors found, FALSE otherwise. */
4319
4320 bool
4321 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4322 bool is_partial, int is_friend_decl)
4323 {
4324 const char *msg;
4325 int last_level_to_check;
4326 tree parm_level;
4327 bool no_errors = true;
4328
4329 /* [temp.param]
4330
4331 A default template-argument shall not be specified in a
4332 function template declaration or a function template definition, nor
4333 in the template-parameter-list of the definition of a member of a
4334 class template. */
4335
4336 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4337 /* You can't have a function template declaration in a local
4338 scope, nor you can you define a member of a class template in a
4339 local scope. */
4340 return true;
4341
4342 if (TREE_CODE (decl) == TYPE_DECL
4343 && TREE_TYPE (decl)
4344 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4345 /* A lambda doesn't have an explicit declaration; don't complain
4346 about the parms of the enclosing class. */
4347 return true;
4348
4349 if (current_class_type
4350 && !TYPE_BEING_DEFINED (current_class_type)
4351 && DECL_LANG_SPECIFIC (decl)
4352 && DECL_DECLARES_FUNCTION_P (decl)
4353 /* If this is either a friend defined in the scope of the class
4354 or a member function. */
4355 && (DECL_FUNCTION_MEMBER_P (decl)
4356 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4357 : DECL_FRIEND_CONTEXT (decl)
4358 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4359 : false)
4360 /* And, if it was a member function, it really was defined in
4361 the scope of the class. */
4362 && (!DECL_FUNCTION_MEMBER_P (decl)
4363 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4364 /* We already checked these parameters when the template was
4365 declared, so there's no need to do it again now. This function
4366 was defined in class scope, but we're processing its body now
4367 that the class is complete. */
4368 return true;
4369
4370 /* Core issue 226 (C++0x only): the following only applies to class
4371 templates. */
4372 if (is_primary
4373 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4374 {
4375 /* [temp.param]
4376
4377 If a template-parameter has a default template-argument, all
4378 subsequent template-parameters shall have a default
4379 template-argument supplied. */
4380 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4381 {
4382 tree inner_parms = TREE_VALUE (parm_level);
4383 int ntparms = TREE_VEC_LENGTH (inner_parms);
4384 int seen_def_arg_p = 0;
4385 int i;
4386
4387 for (i = 0; i < ntparms; ++i)
4388 {
4389 tree parm = TREE_VEC_ELT (inner_parms, i);
4390
4391 if (parm == error_mark_node)
4392 continue;
4393
4394 if (TREE_PURPOSE (parm))
4395 seen_def_arg_p = 1;
4396 else if (seen_def_arg_p
4397 && !template_parameter_pack_p (TREE_VALUE (parm)))
4398 {
4399 error ("no default argument for %qD", TREE_VALUE (parm));
4400 /* For better subsequent error-recovery, we indicate that
4401 there should have been a default argument. */
4402 TREE_PURPOSE (parm) = error_mark_node;
4403 no_errors = false;
4404 }
4405 else if (!is_partial
4406 && !is_friend_decl
4407 /* Don't complain about an enclosing partial
4408 specialization. */
4409 && parm_level == parms
4410 && TREE_CODE (decl) == TYPE_DECL
4411 && i < ntparms - 1
4412 && template_parameter_pack_p (TREE_VALUE (parm)))
4413 {
4414 /* A primary class template can only have one
4415 parameter pack, at the end of the template
4416 parameter list. */
4417
4418 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4419 error ("parameter pack %qE must be at the end of the"
4420 " template parameter list", TREE_VALUE (parm));
4421 else
4422 error ("parameter pack %qT must be at the end of the"
4423 " template parameter list",
4424 TREE_TYPE (TREE_VALUE (parm)));
4425
4426 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4427 = error_mark_node;
4428 no_errors = false;
4429 }
4430 }
4431 }
4432 }
4433
4434 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4435 || is_partial
4436 || !is_primary
4437 || is_friend_decl)
4438 /* For an ordinary class template, default template arguments are
4439 allowed at the innermost level, e.g.:
4440 template <class T = int>
4441 struct S {};
4442 but, in a partial specialization, they're not allowed even
4443 there, as we have in [temp.class.spec]:
4444
4445 The template parameter list of a specialization shall not
4446 contain default template argument values.
4447
4448 So, for a partial specialization, or for a function template
4449 (in C++98/C++03), we look at all of them. */
4450 ;
4451 else
4452 /* But, for a primary class template that is not a partial
4453 specialization we look at all template parameters except the
4454 innermost ones. */
4455 parms = TREE_CHAIN (parms);
4456
4457 /* Figure out what error message to issue. */
4458 if (is_friend_decl == 2)
4459 msg = G_("default template arguments may not be used in function template "
4460 "friend re-declaration");
4461 else if (is_friend_decl)
4462 msg = G_("default template arguments may not be used in function template "
4463 "friend declarations");
4464 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4465 msg = G_("default template arguments may not be used in function templates "
4466 "without -std=c++11 or -std=gnu++11");
4467 else if (is_partial)
4468 msg = G_("default template arguments may not be used in "
4469 "partial specializations");
4470 else
4471 msg = G_("default argument for template parameter for class enclosing %qD");
4472
4473 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4474 /* If we're inside a class definition, there's no need to
4475 examine the parameters to the class itself. On the one
4476 hand, they will be checked when the class is defined, and,
4477 on the other, default arguments are valid in things like:
4478 template <class T = double>
4479 struct S { template <class U> void f(U); };
4480 Here the default argument for `S' has no bearing on the
4481 declaration of `f'. */
4482 last_level_to_check = template_class_depth (current_class_type) + 1;
4483 else
4484 /* Check everything. */
4485 last_level_to_check = 0;
4486
4487 for (parm_level = parms;
4488 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4489 parm_level = TREE_CHAIN (parm_level))
4490 {
4491 tree inner_parms = TREE_VALUE (parm_level);
4492 int i;
4493 int ntparms;
4494
4495 ntparms = TREE_VEC_LENGTH (inner_parms);
4496 for (i = 0; i < ntparms; ++i)
4497 {
4498 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4499 continue;
4500
4501 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4502 {
4503 if (msg)
4504 {
4505 no_errors = false;
4506 if (is_friend_decl == 2)
4507 return no_errors;
4508
4509 error (msg, decl);
4510 msg = 0;
4511 }
4512
4513 /* Clear out the default argument so that we are not
4514 confused later. */
4515 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4516 }
4517 }
4518
4519 /* At this point, if we're still interested in issuing messages,
4520 they must apply to classes surrounding the object declared. */
4521 if (msg)
4522 msg = G_("default argument for template parameter for class "
4523 "enclosing %qD");
4524 }
4525
4526 return no_errors;
4527 }
4528
4529 /* Worker for push_template_decl_real, called via
4530 for_each_template_parm. DATA is really an int, indicating the
4531 level of the parameters we are interested in. If T is a template
4532 parameter of that level, return nonzero. */
4533
4534 static int
4535 template_parm_this_level_p (tree t, void* data)
4536 {
4537 int this_level = *(int *)data;
4538 int level;
4539
4540 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4541 level = TEMPLATE_PARM_LEVEL (t);
4542 else
4543 level = TEMPLATE_TYPE_LEVEL (t);
4544 return level == this_level;
4545 }
4546
4547 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4548 parameters given by current_template_args, or reuses a
4549 previously existing one, if appropriate. Returns the DECL, or an
4550 equivalent one, if it is replaced via a call to duplicate_decls.
4551
4552 If IS_FRIEND is true, DECL is a friend declaration. */
4553
4554 tree
4555 push_template_decl_real (tree decl, bool is_friend)
4556 {
4557 tree tmpl;
4558 tree args;
4559 tree info;
4560 tree ctx;
4561 bool is_primary;
4562 bool is_partial;
4563 int new_template_p = 0;
4564 /* True if the template is a member template, in the sense of
4565 [temp.mem]. */
4566 bool member_template_p = false;
4567
4568 if (decl == error_mark_node || !current_template_parms)
4569 return error_mark_node;
4570
4571 /* See if this is a partial specialization. */
4572 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4573 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4574 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4575
4576 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4577 is_friend = true;
4578
4579 if (is_friend)
4580 /* For a friend, we want the context of the friend function, not
4581 the type of which it is a friend. */
4582 ctx = CP_DECL_CONTEXT (decl);
4583 else if (CP_DECL_CONTEXT (decl)
4584 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4585 /* In the case of a virtual function, we want the class in which
4586 it is defined. */
4587 ctx = CP_DECL_CONTEXT (decl);
4588 else
4589 /* Otherwise, if we're currently defining some class, the DECL
4590 is assumed to be a member of the class. */
4591 ctx = current_scope ();
4592
4593 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4594 ctx = NULL_TREE;
4595
4596 if (!DECL_CONTEXT (decl))
4597 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4598
4599 /* See if this is a primary template. */
4600 if (is_friend && ctx)
4601 /* A friend template that specifies a class context, i.e.
4602 template <typename T> friend void A<T>::f();
4603 is not primary. */
4604 is_primary = false;
4605 else
4606 is_primary = template_parm_scope_p ();
4607
4608 if (is_primary)
4609 {
4610 if (DECL_CLASS_SCOPE_P (decl))
4611 member_template_p = true;
4612 if (TREE_CODE (decl) == TYPE_DECL
4613 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4614 {
4615 error ("template class without a name");
4616 return error_mark_node;
4617 }
4618 else if (TREE_CODE (decl) == FUNCTION_DECL)
4619 {
4620 if (DECL_DESTRUCTOR_P (decl))
4621 {
4622 /* [temp.mem]
4623
4624 A destructor shall not be a member template. */
4625 error ("destructor %qD declared as member template", decl);
4626 return error_mark_node;
4627 }
4628 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4629 && (!prototype_p (TREE_TYPE (decl))
4630 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4631 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4632 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4633 == void_list_node)))
4634 {
4635 /* [basic.stc.dynamic.allocation]
4636
4637 An allocation function can be a function
4638 template. ... Template allocation functions shall
4639 have two or more parameters. */
4640 error ("invalid template declaration of %qD", decl);
4641 return error_mark_node;
4642 }
4643 }
4644 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4645 && CLASS_TYPE_P (TREE_TYPE (decl)))
4646 /* OK */;
4647 else if (TREE_CODE (decl) == TYPE_DECL
4648 && TYPE_DECL_ALIAS_P (decl))
4649 /* alias-declaration */
4650 gcc_assert (!DECL_ARTIFICIAL (decl));
4651 else
4652 {
4653 error ("template declaration of %q#D", decl);
4654 return error_mark_node;
4655 }
4656 }
4657
4658 /* Check to see that the rules regarding the use of default
4659 arguments are not being violated. */
4660 check_default_tmpl_args (decl, current_template_parms,
4661 is_primary, is_partial, /*is_friend_decl=*/0);
4662
4663 /* Ensure that there are no parameter packs in the type of this
4664 declaration that have not been expanded. */
4665 if (TREE_CODE (decl) == FUNCTION_DECL)
4666 {
4667 /* Check each of the arguments individually to see if there are
4668 any bare parameter packs. */
4669 tree type = TREE_TYPE (decl);
4670 tree arg = DECL_ARGUMENTS (decl);
4671 tree argtype = TYPE_ARG_TYPES (type);
4672
4673 while (arg && argtype)
4674 {
4675 if (!DECL_PACK_P (arg)
4676 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4677 {
4678 /* This is a PARM_DECL that contains unexpanded parameter
4679 packs. We have already complained about this in the
4680 check_for_bare_parameter_packs call, so just replace
4681 these types with ERROR_MARK_NODE. */
4682 TREE_TYPE (arg) = error_mark_node;
4683 TREE_VALUE (argtype) = error_mark_node;
4684 }
4685
4686 arg = DECL_CHAIN (arg);
4687 argtype = TREE_CHAIN (argtype);
4688 }
4689
4690 /* Check for bare parameter packs in the return type and the
4691 exception specifiers. */
4692 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4693 /* Errors were already issued, set return type to int
4694 as the frontend doesn't expect error_mark_node as
4695 the return type. */
4696 TREE_TYPE (type) = integer_type_node;
4697 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4698 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4699 }
4700 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4701 && TYPE_DECL_ALIAS_P (decl))
4702 ? DECL_ORIGINAL_TYPE (decl)
4703 : TREE_TYPE (decl)))
4704 {
4705 TREE_TYPE (decl) = error_mark_node;
4706 return error_mark_node;
4707 }
4708
4709 if (is_partial)
4710 return process_partial_specialization (decl);
4711
4712 args = current_template_args ();
4713
4714 if (!ctx
4715 || TREE_CODE (ctx) == FUNCTION_DECL
4716 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4717 || (TREE_CODE (decl) == TYPE_DECL
4718 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4719 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4720 {
4721 if (DECL_LANG_SPECIFIC (decl)
4722 && DECL_TEMPLATE_INFO (decl)
4723 && DECL_TI_TEMPLATE (decl))
4724 tmpl = DECL_TI_TEMPLATE (decl);
4725 /* If DECL is a TYPE_DECL for a class-template, then there won't
4726 be DECL_LANG_SPECIFIC. The information equivalent to
4727 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4728 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4729 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4730 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4731 {
4732 /* Since a template declaration already existed for this
4733 class-type, we must be redeclaring it here. Make sure
4734 that the redeclaration is valid. */
4735 redeclare_class_template (TREE_TYPE (decl),
4736 current_template_parms);
4737 /* We don't need to create a new TEMPLATE_DECL; just use the
4738 one we already had. */
4739 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4740 }
4741 else
4742 {
4743 tmpl = build_template_decl (decl, current_template_parms,
4744 member_template_p);
4745 new_template_p = 1;
4746
4747 if (DECL_LANG_SPECIFIC (decl)
4748 && DECL_TEMPLATE_SPECIALIZATION (decl))
4749 {
4750 /* A specialization of a member template of a template
4751 class. */
4752 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4753 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4754 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4755 }
4756 }
4757 }
4758 else
4759 {
4760 tree a, t, current, parms;
4761 int i;
4762 tree tinfo = get_template_info (decl);
4763
4764 if (!tinfo)
4765 {
4766 error ("template definition of non-template %q#D", decl);
4767 return error_mark_node;
4768 }
4769
4770 tmpl = TI_TEMPLATE (tinfo);
4771
4772 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4773 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4774 && DECL_TEMPLATE_SPECIALIZATION (decl)
4775 && DECL_MEMBER_TEMPLATE_P (tmpl))
4776 {
4777 tree new_tmpl;
4778
4779 /* The declaration is a specialization of a member
4780 template, declared outside the class. Therefore, the
4781 innermost template arguments will be NULL, so we
4782 replace them with the arguments determined by the
4783 earlier call to check_explicit_specialization. */
4784 args = DECL_TI_ARGS (decl);
4785
4786 new_tmpl
4787 = build_template_decl (decl, current_template_parms,
4788 member_template_p);
4789 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4790 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4791 DECL_TI_TEMPLATE (decl) = new_tmpl;
4792 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4793 DECL_TEMPLATE_INFO (new_tmpl)
4794 = build_template_info (tmpl, args);
4795
4796 register_specialization (new_tmpl,
4797 most_general_template (tmpl),
4798 args,
4799 is_friend, 0);
4800 return decl;
4801 }
4802
4803 /* Make sure the template headers we got make sense. */
4804
4805 parms = DECL_TEMPLATE_PARMS (tmpl);
4806 i = TMPL_PARMS_DEPTH (parms);
4807 if (TMPL_ARGS_DEPTH (args) != i)
4808 {
4809 error ("expected %d levels of template parms for %q#D, got %d",
4810 i, decl, TMPL_ARGS_DEPTH (args));
4811 }
4812 else
4813 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4814 {
4815 a = TMPL_ARGS_LEVEL (args, i);
4816 t = INNERMOST_TEMPLATE_PARMS (parms);
4817
4818 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4819 {
4820 if (current == decl)
4821 error ("got %d template parameters for %q#D",
4822 TREE_VEC_LENGTH (a), decl);
4823 else
4824 error ("got %d template parameters for %q#T",
4825 TREE_VEC_LENGTH (a), current);
4826 error (" but %d required", TREE_VEC_LENGTH (t));
4827 /* Avoid crash in import_export_decl. */
4828 DECL_INTERFACE_KNOWN (decl) = 1;
4829 return error_mark_node;
4830 }
4831
4832 if (current == decl)
4833 current = ctx;
4834 else if (current == NULL_TREE)
4835 /* Can happen in erroneous input. */
4836 break;
4837 else
4838 current = get_containing_scope (current);
4839 }
4840
4841 /* Check that the parms are used in the appropriate qualifying scopes
4842 in the declarator. */
4843 if (!comp_template_args
4844 (TI_ARGS (tinfo),
4845 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4846 {
4847 error ("\
4848 template arguments to %qD do not match original template %qD",
4849 decl, DECL_TEMPLATE_RESULT (tmpl));
4850 if (!uses_template_parms (TI_ARGS (tinfo)))
4851 inform (input_location, "use template<> for an explicit specialization");
4852 /* Avoid crash in import_export_decl. */
4853 DECL_INTERFACE_KNOWN (decl) = 1;
4854 return error_mark_node;
4855 }
4856 }
4857
4858 DECL_TEMPLATE_RESULT (tmpl) = decl;
4859 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4860
4861 /* Push template declarations for global functions and types. Note
4862 that we do not try to push a global template friend declared in a
4863 template class; such a thing may well depend on the template
4864 parameters of the class. */
4865 if (new_template_p && !ctx
4866 && !(is_friend && template_class_depth (current_class_type) > 0))
4867 {
4868 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4869 if (tmpl == error_mark_node)
4870 return error_mark_node;
4871
4872 /* Hide template friend classes that haven't been declared yet. */
4873 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4874 {
4875 DECL_ANTICIPATED (tmpl) = 1;
4876 DECL_FRIEND_P (tmpl) = 1;
4877 }
4878 }
4879
4880 if (is_primary)
4881 {
4882 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4883 int i;
4884
4885 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4886 if (DECL_CONV_FN_P (tmpl))
4887 {
4888 int depth = TMPL_PARMS_DEPTH (parms);
4889
4890 /* It is a conversion operator. See if the type converted to
4891 depends on innermost template operands. */
4892
4893 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4894 depth))
4895 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4896 }
4897
4898 /* Give template template parms a DECL_CONTEXT of the template
4899 for which they are a parameter. */
4900 parms = INNERMOST_TEMPLATE_PARMS (parms);
4901 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4902 {
4903 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4904 if (TREE_CODE (parm) == TEMPLATE_DECL)
4905 DECL_CONTEXT (parm) = tmpl;
4906 }
4907 }
4908
4909 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4910 back to its most general template. If TMPL is a specialization,
4911 ARGS may only have the innermost set of arguments. Add the missing
4912 argument levels if necessary. */
4913 if (DECL_TEMPLATE_INFO (tmpl))
4914 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4915
4916 info = build_template_info (tmpl, args);
4917
4918 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4919 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4920 else
4921 {
4922 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4923 retrofit_lang_decl (decl);
4924 if (DECL_LANG_SPECIFIC (decl))
4925 DECL_TEMPLATE_INFO (decl) = info;
4926 }
4927
4928 return DECL_TEMPLATE_RESULT (tmpl);
4929 }
4930
4931 tree
4932 push_template_decl (tree decl)
4933 {
4934 return push_template_decl_real (decl, false);
4935 }
4936
4937 /* FN is an inheriting constructor that inherits from the constructor
4938 template INHERITED; turn FN into a constructor template with a matching
4939 template header. */
4940
4941 tree
4942 add_inherited_template_parms (tree fn, tree inherited)
4943 {
4944 tree inner_parms
4945 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4946 inner_parms = copy_node (inner_parms);
4947 tree parms
4948 = tree_cons (size_int (processing_template_decl + 1),
4949 inner_parms, current_template_parms);
4950 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4951 tree args = template_parms_to_args (parms);
4952 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4953 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4954 DECL_TEMPLATE_RESULT (tmpl) = fn;
4955 DECL_ARTIFICIAL (tmpl) = true;
4956 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4957 return tmpl;
4958 }
4959
4960 /* Called when a class template TYPE is redeclared with the indicated
4961 template PARMS, e.g.:
4962
4963 template <class T> struct S;
4964 template <class T> struct S {}; */
4965
4966 bool
4967 redeclare_class_template (tree type, tree parms)
4968 {
4969 tree tmpl;
4970 tree tmpl_parms;
4971 int i;
4972
4973 if (!TYPE_TEMPLATE_INFO (type))
4974 {
4975 error ("%qT is not a template type", type);
4976 return false;
4977 }
4978
4979 tmpl = TYPE_TI_TEMPLATE (type);
4980 if (!PRIMARY_TEMPLATE_P (tmpl))
4981 /* The type is nested in some template class. Nothing to worry
4982 about here; there are no new template parameters for the nested
4983 type. */
4984 return true;
4985
4986 if (!parms)
4987 {
4988 error ("template specifiers not specified in declaration of %qD",
4989 tmpl);
4990 return false;
4991 }
4992
4993 parms = INNERMOST_TEMPLATE_PARMS (parms);
4994 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4995
4996 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4997 {
4998 error_n (input_location, TREE_VEC_LENGTH (parms),
4999 "redeclared with %d template parameter",
5000 "redeclared with %d template parameters",
5001 TREE_VEC_LENGTH (parms));
5002 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5003 "previous declaration %q+D used %d template parameter",
5004 "previous declaration %q+D used %d template parameters",
5005 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5006 return false;
5007 }
5008
5009 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5010 {
5011 tree tmpl_parm;
5012 tree parm;
5013 tree tmpl_default;
5014 tree parm_default;
5015
5016 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5017 || TREE_VEC_ELT (parms, i) == error_mark_node)
5018 continue;
5019
5020 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5021 if (tmpl_parm == error_mark_node)
5022 return false;
5023
5024 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5025 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5026 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5027
5028 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5029 TEMPLATE_DECL. */
5030 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5031 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5032 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5033 || (TREE_CODE (tmpl_parm) != PARM_DECL
5034 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5035 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5036 || (TREE_CODE (tmpl_parm) == PARM_DECL
5037 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5038 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5039 {
5040 error ("template parameter %q+#D", tmpl_parm);
5041 error ("redeclared here as %q#D", parm);
5042 return false;
5043 }
5044
5045 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5046 {
5047 /* We have in [temp.param]:
5048
5049 A template-parameter may not be given default arguments
5050 by two different declarations in the same scope. */
5051 error_at (input_location, "redefinition of default argument for %q#D", parm);
5052 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5053 "original definition appeared here");
5054 return false;
5055 }
5056
5057 if (parm_default != NULL_TREE)
5058 /* Update the previous template parameters (which are the ones
5059 that will really count) with the new default value. */
5060 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5061 else if (tmpl_default != NULL_TREE)
5062 /* Update the new parameters, too; they'll be used as the
5063 parameters for any members. */
5064 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5065 }
5066
5067 return true;
5068 }
5069
5070 /* Simplify EXPR if it is a non-dependent expression. Returns the
5071 (possibly simplified) expression. */
5072
5073 tree
5074 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5075 {
5076 if (expr == NULL_TREE)
5077 return NULL_TREE;
5078
5079 /* If we're in a template, but EXPR isn't value dependent, simplify
5080 it. We're supposed to treat:
5081
5082 template <typename T> void f(T[1 + 1]);
5083 template <typename T> void f(T[2]);
5084
5085 as two declarations of the same function, for example. */
5086 if (processing_template_decl
5087 && !instantiation_dependent_expression_p (expr)
5088 && potential_constant_expression (expr))
5089 {
5090 HOST_WIDE_INT saved_processing_template_decl;
5091
5092 saved_processing_template_decl = processing_template_decl;
5093 processing_template_decl = 0;
5094 expr = tsubst_copy_and_build (expr,
5095 /*args=*/NULL_TREE,
5096 complain,
5097 /*in_decl=*/NULL_TREE,
5098 /*function_p=*/false,
5099 /*integral_constant_expression_p=*/true);
5100 processing_template_decl = saved_processing_template_decl;
5101 }
5102 return expr;
5103 }
5104
5105 tree
5106 fold_non_dependent_expr (tree expr)
5107 {
5108 return fold_non_dependent_expr_sfinae (expr, tf_error);
5109 }
5110
5111 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5112 template declaration, or a TYPE_DECL for an alias declaration. */
5113
5114 bool
5115 alias_type_or_template_p (tree t)
5116 {
5117 if (t == NULL_TREE)
5118 return false;
5119 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5120 || (TYPE_P (t)
5121 && TYPE_NAME (t)
5122 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5123 || DECL_ALIAS_TEMPLATE_P (t));
5124 }
5125
5126 /* Return TRUE iff is a specialization of an alias template. */
5127
5128 bool
5129 alias_template_specialization_p (const_tree t)
5130 {
5131 if (t == NULL_TREE)
5132 return false;
5133
5134 return (TYPE_P (t)
5135 && TYPE_TEMPLATE_INFO (t)
5136 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5137 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5138 }
5139
5140 /* Return either TMPL or another template that it is equivalent to under DR
5141 1286: An alias that just changes the name of a template is equivalent to
5142 the other template. */
5143
5144 static tree
5145 get_underlying_template (tree tmpl)
5146 {
5147 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5148 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5149 {
5150 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5151 if (TYPE_TEMPLATE_INFO (result))
5152 {
5153 tree sub = TYPE_TI_TEMPLATE (result);
5154 if (PRIMARY_TEMPLATE_P (sub)
5155 && same_type_p (result, TREE_TYPE (sub)))
5156 {
5157 /* The alias type is equivalent to the pattern of the
5158 underlying template, so strip the alias. */
5159 tmpl = sub;
5160 continue;
5161 }
5162 }
5163 break;
5164 }
5165 return tmpl;
5166 }
5167
5168 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5169 must be a function or a pointer-to-function type, as specified
5170 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5171 and check that the resulting function has external linkage. */
5172
5173 static tree
5174 convert_nontype_argument_function (tree type, tree expr)
5175 {
5176 tree fns = expr;
5177 tree fn, fn_no_ptr;
5178 linkage_kind linkage;
5179
5180 fn = instantiate_type (type, fns, tf_none);
5181 if (fn == error_mark_node)
5182 return error_mark_node;
5183
5184 fn_no_ptr = fn;
5185 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5186 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5187 if (BASELINK_P (fn_no_ptr))
5188 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5189
5190 /* [temp.arg.nontype]/1
5191
5192 A template-argument for a non-type, non-template template-parameter
5193 shall be one of:
5194 [...]
5195 -- the address of an object or function with external [C++11: or
5196 internal] linkage. */
5197
5198 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5199 {
5200 error ("%qE is not a valid template argument for type %qT", expr, type);
5201 if (TYPE_PTR_P (type))
5202 error ("it must be the address of a function with external linkage");
5203 else
5204 error ("it must be the name of a function with external linkage");
5205 return NULL_TREE;
5206 }
5207
5208 linkage = decl_linkage (fn_no_ptr);
5209 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5210 {
5211 if (cxx_dialect >= cxx11)
5212 error ("%qE is not a valid template argument for type %qT "
5213 "because %qD has no linkage",
5214 expr, type, fn_no_ptr);
5215 else
5216 error ("%qE is not a valid template argument for type %qT "
5217 "because %qD does not have external linkage",
5218 expr, type, fn_no_ptr);
5219 return NULL_TREE;
5220 }
5221
5222 return fn;
5223 }
5224
5225 /* Subroutine of convert_nontype_argument.
5226 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5227 Emit an error otherwise. */
5228
5229 static bool
5230 check_valid_ptrmem_cst_expr (tree type, tree expr,
5231 tsubst_flags_t complain)
5232 {
5233 STRIP_NOPS (expr);
5234 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5235 return true;
5236 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5237 return true;
5238 if (complain & tf_error)
5239 {
5240 error ("%qE is not a valid template argument for type %qT",
5241 expr, type);
5242 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5243 }
5244 return false;
5245 }
5246
5247 /* Returns TRUE iff the address of OP is value-dependent.
5248
5249 14.6.2.4 [temp.dep.temp]:
5250 A non-integral non-type template-argument is dependent if its type is
5251 dependent or it has either of the following forms
5252 qualified-id
5253 & qualified-id
5254 and contains a nested-name-specifier which specifies a class-name that
5255 names a dependent type.
5256
5257 We generalize this to just say that the address of a member of a
5258 dependent class is value-dependent; the above doesn't cover the
5259 address of a static data member named with an unqualified-id. */
5260
5261 static bool
5262 has_value_dependent_address (tree op)
5263 {
5264 /* We could use get_inner_reference here, but there's no need;
5265 this is only relevant for template non-type arguments, which
5266 can only be expressed as &id-expression. */
5267 if (DECL_P (op))
5268 {
5269 tree ctx = CP_DECL_CONTEXT (op);
5270 if (TYPE_P (ctx) && dependent_type_p (ctx))
5271 return true;
5272 }
5273
5274 return false;
5275 }
5276
5277 /* The next set of functions are used for providing helpful explanatory
5278 diagnostics for failed overload resolution. Their messages should be
5279 indented by two spaces for consistency with the messages in
5280 call.c */
5281
5282 static int
5283 unify_success (bool /*explain_p*/)
5284 {
5285 return 0;
5286 }
5287
5288 static int
5289 unify_parameter_deduction_failure (bool explain_p, tree parm)
5290 {
5291 if (explain_p)
5292 inform (input_location,
5293 " couldn't deduce template parameter %qD", parm);
5294 return 1;
5295 }
5296
5297 static int
5298 unify_invalid (bool /*explain_p*/)
5299 {
5300 return 1;
5301 }
5302
5303 static int
5304 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5305 {
5306 if (explain_p)
5307 inform (input_location,
5308 " types %qT and %qT have incompatible cv-qualifiers",
5309 parm, arg);
5310 return 1;
5311 }
5312
5313 static int
5314 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5315 {
5316 if (explain_p)
5317 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5318 return 1;
5319 }
5320
5321 static int
5322 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5323 {
5324 if (explain_p)
5325 inform (input_location,
5326 " template parameter %qD is not a parameter pack, but "
5327 "argument %qD is",
5328 parm, arg);
5329 return 1;
5330 }
5331
5332 static int
5333 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5334 {
5335 if (explain_p)
5336 inform (input_location,
5337 " template argument %qE does not match "
5338 "pointer-to-member constant %qE",
5339 arg, parm);
5340 return 1;
5341 }
5342
5343 static int
5344 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5345 {
5346 if (explain_p)
5347 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5348 return 1;
5349 }
5350
5351 static int
5352 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5353 {
5354 if (explain_p)
5355 inform (input_location,
5356 " inconsistent parameter pack deduction with %qT and %qT",
5357 old_arg, new_arg);
5358 return 1;
5359 }
5360
5361 static int
5362 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5363 {
5364 if (explain_p)
5365 {
5366 if (TYPE_P (parm))
5367 inform (input_location,
5368 " deduced conflicting types for parameter %qT (%qT and %qT)",
5369 parm, first, second);
5370 else
5371 inform (input_location,
5372 " deduced conflicting values for non-type parameter "
5373 "%qE (%qE and %qE)", parm, first, second);
5374 }
5375 return 1;
5376 }
5377
5378 static int
5379 unify_vla_arg (bool explain_p, tree arg)
5380 {
5381 if (explain_p)
5382 inform (input_location,
5383 " variable-sized array type %qT is not "
5384 "a valid template argument",
5385 arg);
5386 return 1;
5387 }
5388
5389 static int
5390 unify_method_type_error (bool explain_p, tree arg)
5391 {
5392 if (explain_p)
5393 inform (input_location,
5394 " member function type %qT is not a valid template argument",
5395 arg);
5396 return 1;
5397 }
5398
5399 static int
5400 unify_arity (bool explain_p, int have, int wanted)
5401 {
5402 if (explain_p)
5403 inform_n (input_location, wanted,
5404 " candidate expects %d argument, %d provided",
5405 " candidate expects %d arguments, %d provided",
5406 wanted, have);
5407 return 1;
5408 }
5409
5410 static int
5411 unify_too_many_arguments (bool explain_p, int have, int wanted)
5412 {
5413 return unify_arity (explain_p, have, wanted);
5414 }
5415
5416 static int
5417 unify_too_few_arguments (bool explain_p, int have, int wanted)
5418 {
5419 return unify_arity (explain_p, have, wanted);
5420 }
5421
5422 static int
5423 unify_arg_conversion (bool explain_p, tree to_type,
5424 tree from_type, tree arg)
5425 {
5426 if (explain_p)
5427 inform (EXPR_LOC_OR_HERE (arg),
5428 " cannot convert %qE (type %qT) to type %qT",
5429 arg, from_type, to_type);
5430 return 1;
5431 }
5432
5433 static int
5434 unify_no_common_base (bool explain_p, enum template_base_result r,
5435 tree parm, tree arg)
5436 {
5437 if (explain_p)
5438 switch (r)
5439 {
5440 case tbr_ambiguous_baseclass:
5441 inform (input_location, " %qT is an ambiguous base class of %qT",
5442 parm, arg);
5443 break;
5444 default:
5445 inform (input_location, " %qT is not derived from %qT", arg, parm);
5446 break;
5447 }
5448 return 1;
5449 }
5450
5451 static int
5452 unify_inconsistent_template_template_parameters (bool explain_p)
5453 {
5454 if (explain_p)
5455 inform (input_location,
5456 " template parameters of a template template argument are "
5457 "inconsistent with other deduced template arguments");
5458 return 1;
5459 }
5460
5461 static int
5462 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5463 {
5464 if (explain_p)
5465 inform (input_location,
5466 " can't deduce a template for %qT from non-template type %qT",
5467 parm, arg);
5468 return 1;
5469 }
5470
5471 static int
5472 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5473 {
5474 if (explain_p)
5475 inform (input_location,
5476 " template argument %qE does not match %qD", arg, parm);
5477 return 1;
5478 }
5479
5480 static int
5481 unify_overload_resolution_failure (bool explain_p, tree arg)
5482 {
5483 if (explain_p)
5484 inform (input_location,
5485 " could not resolve address from overloaded function %qE",
5486 arg);
5487 return 1;
5488 }
5489
5490 /* Attempt to convert the non-type template parameter EXPR to the
5491 indicated TYPE. If the conversion is successful, return the
5492 converted value. If the conversion is unsuccessful, return
5493 NULL_TREE if we issued an error message, or error_mark_node if we
5494 did not. We issue error messages for out-and-out bad template
5495 parameters, but not simply because the conversion failed, since we
5496 might be just trying to do argument deduction. Both TYPE and EXPR
5497 must be non-dependent.
5498
5499 The conversion follows the special rules described in
5500 [temp.arg.nontype], and it is much more strict than an implicit
5501 conversion.
5502
5503 This function is called twice for each template argument (see
5504 lookup_template_class for a more accurate description of this
5505 problem). This means that we need to handle expressions which
5506 are not valid in a C++ source, but can be created from the
5507 first call (for instance, casts to perform conversions). These
5508 hacks can go away after we fix the double coercion problem. */
5509
5510 static tree
5511 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5512 {
5513 tree expr_type;
5514
5515 /* Detect immediately string literals as invalid non-type argument.
5516 This special-case is not needed for correctness (we would easily
5517 catch this later), but only to provide better diagnostic for this
5518 common user mistake. As suggested by DR 100, we do not mention
5519 linkage issues in the diagnostic as this is not the point. */
5520 /* FIXME we're making this OK. */
5521 if (TREE_CODE (expr) == STRING_CST)
5522 {
5523 if (complain & tf_error)
5524 error ("%qE is not a valid template argument for type %qT "
5525 "because string literals can never be used in this context",
5526 expr, type);
5527 return NULL_TREE;
5528 }
5529
5530 /* Add the ADDR_EXPR now for the benefit of
5531 value_dependent_expression_p. */
5532 if (TYPE_PTROBV_P (type)
5533 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5534 {
5535 expr = decay_conversion (expr, complain);
5536 if (expr == error_mark_node)
5537 return error_mark_node;
5538 }
5539
5540 /* If we are in a template, EXPR may be non-dependent, but still
5541 have a syntactic, rather than semantic, form. For example, EXPR
5542 might be a SCOPE_REF, rather than the VAR_DECL to which the
5543 SCOPE_REF refers. Preserving the qualifying scope is necessary
5544 so that access checking can be performed when the template is
5545 instantiated -- but here we need the resolved form so that we can
5546 convert the argument. */
5547 if (TYPE_REF_OBJ_P (type)
5548 && has_value_dependent_address (expr))
5549 /* If we want the address and it's value-dependent, don't fold. */;
5550 else if (!type_unknown_p (expr))
5551 expr = fold_non_dependent_expr_sfinae (expr, complain);
5552 if (error_operand_p (expr))
5553 return error_mark_node;
5554 expr_type = TREE_TYPE (expr);
5555 if (TREE_CODE (type) == REFERENCE_TYPE)
5556 expr = mark_lvalue_use (expr);
5557 else
5558 expr = mark_rvalue_use (expr);
5559
5560 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5561 to a non-type argument of "nullptr". */
5562 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5563 expr = convert (type, expr);
5564
5565 /* In C++11, integral or enumeration non-type template arguments can be
5566 arbitrary constant expressions. Pointer and pointer to
5567 member arguments can be general constant expressions that evaluate
5568 to a null value, but otherwise still need to be of a specific form. */
5569 if (cxx_dialect >= cxx11)
5570 {
5571 if (TREE_CODE (expr) == PTRMEM_CST)
5572 /* A PTRMEM_CST is already constant, and a valid template
5573 argument for a parameter of pointer to member type, we just want
5574 to leave it in that form rather than lower it to a
5575 CONSTRUCTOR. */;
5576 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5577 expr = maybe_constant_value (expr);
5578 else if (TYPE_PTR_OR_PTRMEM_P (type))
5579 {
5580 tree folded = maybe_constant_value (expr);
5581 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5582 : null_member_pointer_value_p (folded))
5583 expr = folded;
5584 }
5585 }
5586
5587 /* HACK: Due to double coercion, we can get a
5588 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5589 which is the tree that we built on the first call (see
5590 below when coercing to reference to object or to reference to
5591 function). We just strip everything and get to the arg.
5592 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5593 for examples. */
5594 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5595 {
5596 tree probe_type, probe = expr;
5597 if (REFERENCE_REF_P (probe))
5598 probe = TREE_OPERAND (probe, 0);
5599 probe_type = TREE_TYPE (probe);
5600 if (TREE_CODE (probe) == NOP_EXPR)
5601 {
5602 /* ??? Maybe we could use convert_from_reference here, but we
5603 would need to relax its constraints because the NOP_EXPR
5604 could actually change the type to something more cv-qualified,
5605 and this is not folded by convert_from_reference. */
5606 tree addr = TREE_OPERAND (probe, 0);
5607 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5608 && TREE_CODE (addr) == ADDR_EXPR
5609 && TYPE_PTR_P (TREE_TYPE (addr))
5610 && (same_type_ignoring_top_level_qualifiers_p
5611 (TREE_TYPE (probe_type),
5612 TREE_TYPE (TREE_TYPE (addr)))))
5613 {
5614 expr = TREE_OPERAND (addr, 0);
5615 expr_type = TREE_TYPE (probe_type);
5616 }
5617 }
5618 }
5619
5620 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5621 parameter is a pointer to object, through decay and
5622 qualification conversion. Let's strip everything. */
5623 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5624 {
5625 tree probe = expr;
5626 STRIP_NOPS (probe);
5627 if (TREE_CODE (probe) == ADDR_EXPR
5628 && TYPE_PTR_P (TREE_TYPE (probe)))
5629 {
5630 /* Skip the ADDR_EXPR only if it is part of the decay for
5631 an array. Otherwise, it is part of the original argument
5632 in the source code. */
5633 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5634 probe = TREE_OPERAND (probe, 0);
5635 expr = probe;
5636 expr_type = TREE_TYPE (expr);
5637 }
5638 }
5639
5640 /* [temp.arg.nontype]/5, bullet 1
5641
5642 For a non-type template-parameter of integral or enumeration type,
5643 integral promotions (_conv.prom_) and integral conversions
5644 (_conv.integral_) are applied. */
5645 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5646 {
5647 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5648 t = maybe_constant_value (t);
5649 if (t != error_mark_node)
5650 expr = t;
5651
5652 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5653 return error_mark_node;
5654
5655 /* Notice that there are constant expressions like '4 % 0' which
5656 do not fold into integer constants. */
5657 if (TREE_CODE (expr) != INTEGER_CST)
5658 {
5659 if (complain & tf_error)
5660 {
5661 int errs = errorcount, warns = warningcount + werrorcount;
5662 if (processing_template_decl
5663 && !require_potential_constant_expression (expr))
5664 return NULL_TREE;
5665 expr = cxx_constant_value (expr);
5666 if (errorcount > errs || warningcount + werrorcount > warns)
5667 inform (EXPR_LOC_OR_HERE (expr),
5668 "in template argument for type %qT ", type);
5669 if (expr == error_mark_node)
5670 return NULL_TREE;
5671 /* else cxx_constant_value complained but gave us
5672 a real constant, so go ahead. */
5673 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5674 }
5675 else
5676 return NULL_TREE;
5677 }
5678
5679 /* Avoid typedef problems. */
5680 if (TREE_TYPE (expr) != type)
5681 expr = fold_convert (type, expr);
5682 }
5683 /* [temp.arg.nontype]/5, bullet 2
5684
5685 For a non-type template-parameter of type pointer to object,
5686 qualification conversions (_conv.qual_) and the array-to-pointer
5687 conversion (_conv.array_) are applied. */
5688 else if (TYPE_PTROBV_P (type))
5689 {
5690 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5691
5692 A template-argument for a non-type, non-template template-parameter
5693 shall be one of: [...]
5694
5695 -- the name of a non-type template-parameter;
5696 -- the address of an object or function with external linkage, [...]
5697 expressed as "& id-expression" where the & is optional if the name
5698 refers to a function or array, or if the corresponding
5699 template-parameter is a reference.
5700
5701 Here, we do not care about functions, as they are invalid anyway
5702 for a parameter of type pointer-to-object. */
5703
5704 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5705 /* Non-type template parameters are OK. */
5706 ;
5707 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5708 /* Null pointer values are OK in C++11. */;
5709 else if (TREE_CODE (expr) != ADDR_EXPR
5710 && TREE_CODE (expr_type) != ARRAY_TYPE)
5711 {
5712 if (VAR_P (expr))
5713 {
5714 error ("%qD is not a valid template argument "
5715 "because %qD is a variable, not the address of "
5716 "a variable",
5717 expr, expr);
5718 return NULL_TREE;
5719 }
5720 if (POINTER_TYPE_P (expr_type))
5721 {
5722 error ("%qE is not a valid template argument for %qT "
5723 "because it is not the address of a variable",
5724 expr, type);
5725 return NULL_TREE;
5726 }
5727 /* Other values, like integer constants, might be valid
5728 non-type arguments of some other type. */
5729 return error_mark_node;
5730 }
5731 else
5732 {
5733 tree decl;
5734
5735 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5736 ? TREE_OPERAND (expr, 0) : expr);
5737 if (!VAR_P (decl))
5738 {
5739 error ("%qE is not a valid template argument of type %qT "
5740 "because %qE is not a variable",
5741 expr, type, decl);
5742 return NULL_TREE;
5743 }
5744 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5745 {
5746 error ("%qE is not a valid template argument of type %qT "
5747 "because %qD does not have external linkage",
5748 expr, type, decl);
5749 return NULL_TREE;
5750 }
5751 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5752 {
5753 error ("%qE is not a valid template argument of type %qT "
5754 "because %qD has no linkage",
5755 expr, type, decl);
5756 return NULL_TREE;
5757 }
5758 }
5759
5760 expr = decay_conversion (expr, complain);
5761 if (expr == error_mark_node)
5762 return error_mark_node;
5763
5764 expr = perform_qualification_conversions (type, expr);
5765 if (expr == error_mark_node)
5766 return error_mark_node;
5767 }
5768 /* [temp.arg.nontype]/5, bullet 3
5769
5770 For a non-type template-parameter of type reference to object, no
5771 conversions apply. The type referred to by the reference may be more
5772 cv-qualified than the (otherwise identical) type of the
5773 template-argument. The template-parameter is bound directly to the
5774 template-argument, which must be an lvalue. */
5775 else if (TYPE_REF_OBJ_P (type))
5776 {
5777 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5778 expr_type))
5779 return error_mark_node;
5780
5781 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5782 {
5783 error ("%qE is not a valid template argument for type %qT "
5784 "because of conflicts in cv-qualification", expr, type);
5785 return NULL_TREE;
5786 }
5787
5788 if (!real_lvalue_p (expr))
5789 {
5790 error ("%qE is not a valid template argument for type %qT "
5791 "because it is not an lvalue", expr, type);
5792 return NULL_TREE;
5793 }
5794
5795 /* [temp.arg.nontype]/1
5796
5797 A template-argument for a non-type, non-template template-parameter
5798 shall be one of: [...]
5799
5800 -- the address of an object or function with external linkage. */
5801 if (INDIRECT_REF_P (expr)
5802 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5803 {
5804 expr = TREE_OPERAND (expr, 0);
5805 if (DECL_P (expr))
5806 {
5807 error ("%q#D is not a valid template argument for type %qT "
5808 "because a reference variable does not have a constant "
5809 "address", expr, type);
5810 return NULL_TREE;
5811 }
5812 }
5813
5814 if (!DECL_P (expr))
5815 {
5816 error ("%qE is not a valid template argument for type %qT "
5817 "because it is not an object with external linkage",
5818 expr, type);
5819 return NULL_TREE;
5820 }
5821
5822 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5823 {
5824 error ("%qE is not a valid template argument for type %qT "
5825 "because object %qD has not external linkage",
5826 expr, type, expr);
5827 return NULL_TREE;
5828 }
5829
5830 expr = build_nop (type, build_address (expr));
5831 }
5832 /* [temp.arg.nontype]/5, bullet 4
5833
5834 For a non-type template-parameter of type pointer to function, only
5835 the function-to-pointer conversion (_conv.func_) is applied. If the
5836 template-argument represents a set of overloaded functions (or a
5837 pointer to such), the matching function is selected from the set
5838 (_over.over_). */
5839 else if (TYPE_PTRFN_P (type))
5840 {
5841 /* If the argument is a template-id, we might not have enough
5842 context information to decay the pointer. */
5843 if (!type_unknown_p (expr_type))
5844 {
5845 expr = decay_conversion (expr, complain);
5846 if (expr == error_mark_node)
5847 return error_mark_node;
5848 }
5849
5850 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5851 /* Null pointer values are OK in C++11. */
5852 return perform_qualification_conversions (type, expr);
5853
5854 expr = convert_nontype_argument_function (type, expr);
5855 if (!expr || expr == error_mark_node)
5856 return expr;
5857 }
5858 /* [temp.arg.nontype]/5, bullet 5
5859
5860 For a non-type template-parameter of type reference to function, no
5861 conversions apply. If the template-argument represents a set of
5862 overloaded functions, the matching function is selected from the set
5863 (_over.over_). */
5864 else if (TYPE_REFFN_P (type))
5865 {
5866 if (TREE_CODE (expr) == ADDR_EXPR)
5867 {
5868 error ("%qE is not a valid template argument for type %qT "
5869 "because it is a pointer", expr, type);
5870 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5871 return NULL_TREE;
5872 }
5873
5874 expr = convert_nontype_argument_function (type, expr);
5875 if (!expr || expr == error_mark_node)
5876 return expr;
5877
5878 expr = build_nop (type, build_address (expr));
5879 }
5880 /* [temp.arg.nontype]/5, bullet 6
5881
5882 For a non-type template-parameter of type pointer to member function,
5883 no conversions apply. If the template-argument represents a set of
5884 overloaded member functions, the matching member function is selected
5885 from the set (_over.over_). */
5886 else if (TYPE_PTRMEMFUNC_P (type))
5887 {
5888 expr = instantiate_type (type, expr, tf_none);
5889 if (expr == error_mark_node)
5890 return error_mark_node;
5891
5892 /* [temp.arg.nontype] bullet 1 says the pointer to member
5893 expression must be a pointer-to-member constant. */
5894 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5895 return error_mark_node;
5896
5897 /* There is no way to disable standard conversions in
5898 resolve_address_of_overloaded_function (called by
5899 instantiate_type). It is possible that the call succeeded by
5900 converting &B::I to &D::I (where B is a base of D), so we need
5901 to reject this conversion here.
5902
5903 Actually, even if there was a way to disable standard conversions,
5904 it would still be better to reject them here so that we can
5905 provide a superior diagnostic. */
5906 if (!same_type_p (TREE_TYPE (expr), type))
5907 {
5908 error ("%qE is not a valid template argument for type %qT "
5909 "because it is of type %qT", expr, type,
5910 TREE_TYPE (expr));
5911 /* If we are just one standard conversion off, explain. */
5912 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5913 inform (input_location,
5914 "standard conversions are not allowed in this context");
5915 return NULL_TREE;
5916 }
5917 }
5918 /* [temp.arg.nontype]/5, bullet 7
5919
5920 For a non-type template-parameter of type pointer to data member,
5921 qualification conversions (_conv.qual_) are applied. */
5922 else if (TYPE_PTRDATAMEM_P (type))
5923 {
5924 /* [temp.arg.nontype] bullet 1 says the pointer to member
5925 expression must be a pointer-to-member constant. */
5926 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5927 return error_mark_node;
5928
5929 expr = perform_qualification_conversions (type, expr);
5930 if (expr == error_mark_node)
5931 return expr;
5932 }
5933 else if (NULLPTR_TYPE_P (type))
5934 {
5935 if (expr != nullptr_node)
5936 {
5937 error ("%qE is not a valid template argument for type %qT "
5938 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5939 return NULL_TREE;
5940 }
5941 return expr;
5942 }
5943 /* A template non-type parameter must be one of the above. */
5944 else
5945 gcc_unreachable ();
5946
5947 /* Sanity check: did we actually convert the argument to the
5948 right type? */
5949 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5950 (type, TREE_TYPE (expr)));
5951 return expr;
5952 }
5953
5954 /* Subroutine of coerce_template_template_parms, which returns 1 if
5955 PARM_PARM and ARG_PARM match using the rule for the template
5956 parameters of template template parameters. Both PARM and ARG are
5957 template parameters; the rest of the arguments are the same as for
5958 coerce_template_template_parms.
5959 */
5960 static int
5961 coerce_template_template_parm (tree parm,
5962 tree arg,
5963 tsubst_flags_t complain,
5964 tree in_decl,
5965 tree outer_args)
5966 {
5967 if (arg == NULL_TREE || arg == error_mark_node
5968 || parm == NULL_TREE || parm == error_mark_node)
5969 return 0;
5970
5971 if (TREE_CODE (arg) != TREE_CODE (parm))
5972 return 0;
5973
5974 switch (TREE_CODE (parm))
5975 {
5976 case TEMPLATE_DECL:
5977 /* We encounter instantiations of templates like
5978 template <template <template <class> class> class TT>
5979 class C; */
5980 {
5981 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5982 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5983
5984 if (!coerce_template_template_parms
5985 (parmparm, argparm, complain, in_decl, outer_args))
5986 return 0;
5987 }
5988 /* Fall through. */
5989
5990 case TYPE_DECL:
5991 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5992 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5993 /* Argument is a parameter pack but parameter is not. */
5994 return 0;
5995 break;
5996
5997 case PARM_DECL:
5998 /* The tsubst call is used to handle cases such as
5999
6000 template <int> class C {};
6001 template <class T, template <T> class TT> class D {};
6002 D<int, C> d;
6003
6004 i.e. the parameter list of TT depends on earlier parameters. */
6005 if (!uses_template_parms (TREE_TYPE (arg))
6006 && !same_type_p
6007 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6008 TREE_TYPE (arg)))
6009 return 0;
6010
6011 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6012 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6013 /* Argument is a parameter pack but parameter is not. */
6014 return 0;
6015
6016 break;
6017
6018 default:
6019 gcc_unreachable ();
6020 }
6021
6022 return 1;
6023 }
6024
6025
6026 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6027 template template parameters. Both PARM_PARMS and ARG_PARMS are
6028 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6029 or PARM_DECL.
6030
6031 Consider the example:
6032 template <class T> class A;
6033 template<template <class U> class TT> class B;
6034
6035 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6036 the parameters to A, and OUTER_ARGS contains A. */
6037
6038 static int
6039 coerce_template_template_parms (tree parm_parms,
6040 tree arg_parms,
6041 tsubst_flags_t complain,
6042 tree in_decl,
6043 tree outer_args)
6044 {
6045 int nparms, nargs, i;
6046 tree parm, arg;
6047 int variadic_p = 0;
6048
6049 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6050 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6051
6052 nparms = TREE_VEC_LENGTH (parm_parms);
6053 nargs = TREE_VEC_LENGTH (arg_parms);
6054
6055 /* Determine whether we have a parameter pack at the end of the
6056 template template parameter's template parameter list. */
6057 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6058 {
6059 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6060
6061 if (parm == error_mark_node)
6062 return 0;
6063
6064 switch (TREE_CODE (parm))
6065 {
6066 case TEMPLATE_DECL:
6067 case TYPE_DECL:
6068 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6069 variadic_p = 1;
6070 break;
6071
6072 case PARM_DECL:
6073 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6074 variadic_p = 1;
6075 break;
6076
6077 default:
6078 gcc_unreachable ();
6079 }
6080 }
6081
6082 if (nargs != nparms
6083 && !(variadic_p && nargs >= nparms - 1))
6084 return 0;
6085
6086 /* Check all of the template parameters except the parameter pack at
6087 the end (if any). */
6088 for (i = 0; i < nparms - variadic_p; ++i)
6089 {
6090 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6091 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6092 continue;
6093
6094 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6095 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6096
6097 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6098 outer_args))
6099 return 0;
6100
6101 }
6102
6103 if (variadic_p)
6104 {
6105 /* Check each of the template parameters in the template
6106 argument against the template parameter pack at the end of
6107 the template template parameter. */
6108 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6109 return 0;
6110
6111 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6112
6113 for (; i < nargs; ++i)
6114 {
6115 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6116 continue;
6117
6118 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6119
6120 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6121 outer_args))
6122 return 0;
6123 }
6124 }
6125
6126 return 1;
6127 }
6128
6129 /* Verifies that the deduced template arguments (in TARGS) for the
6130 template template parameters (in TPARMS) represent valid bindings,
6131 by comparing the template parameter list of each template argument
6132 to the template parameter list of its corresponding template
6133 template parameter, in accordance with DR150. This
6134 routine can only be called after all template arguments have been
6135 deduced. It will return TRUE if all of the template template
6136 parameter bindings are okay, FALSE otherwise. */
6137 bool
6138 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6139 {
6140 int i, ntparms = TREE_VEC_LENGTH (tparms);
6141 bool ret = true;
6142
6143 /* We're dealing with template parms in this process. */
6144 ++processing_template_decl;
6145
6146 targs = INNERMOST_TEMPLATE_ARGS (targs);
6147
6148 for (i = 0; i < ntparms; ++i)
6149 {
6150 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6151 tree targ = TREE_VEC_ELT (targs, i);
6152
6153 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6154 {
6155 tree packed_args = NULL_TREE;
6156 int idx, len = 1;
6157
6158 if (ARGUMENT_PACK_P (targ))
6159 {
6160 /* Look inside the argument pack. */
6161 packed_args = ARGUMENT_PACK_ARGS (targ);
6162 len = TREE_VEC_LENGTH (packed_args);
6163 }
6164
6165 for (idx = 0; idx < len; ++idx)
6166 {
6167 tree targ_parms = NULL_TREE;
6168
6169 if (packed_args)
6170 /* Extract the next argument from the argument
6171 pack. */
6172 targ = TREE_VEC_ELT (packed_args, idx);
6173
6174 if (PACK_EXPANSION_P (targ))
6175 /* Look at the pattern of the pack expansion. */
6176 targ = PACK_EXPANSION_PATTERN (targ);
6177
6178 /* Extract the template parameters from the template
6179 argument. */
6180 if (TREE_CODE (targ) == TEMPLATE_DECL)
6181 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6182 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6183 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6184
6185 /* Verify that we can coerce the template template
6186 parameters from the template argument to the template
6187 parameter. This requires an exact match. */
6188 if (targ_parms
6189 && !coerce_template_template_parms
6190 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6191 targ_parms,
6192 tf_none,
6193 tparm,
6194 targs))
6195 {
6196 ret = false;
6197 goto out;
6198 }
6199 }
6200 }
6201 }
6202
6203 out:
6204
6205 --processing_template_decl;
6206 return ret;
6207 }
6208
6209 /* Since type attributes aren't mangled, we need to strip them from
6210 template type arguments. */
6211
6212 static tree
6213 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6214 {
6215 tree mv;
6216 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6217 return arg;
6218 mv = TYPE_MAIN_VARIANT (arg);
6219 arg = strip_typedefs (arg);
6220 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6221 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6222 {
6223 if (complain & tf_warning)
6224 warning (0, "ignoring attributes on template argument %qT", arg);
6225 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6226 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6227 }
6228 return arg;
6229 }
6230
6231 /* Convert the indicated template ARG as necessary to match the
6232 indicated template PARM. Returns the converted ARG, or
6233 error_mark_node if the conversion was unsuccessful. Error and
6234 warning messages are issued under control of COMPLAIN. This
6235 conversion is for the Ith parameter in the parameter list. ARGS is
6236 the full set of template arguments deduced so far. */
6237
6238 static tree
6239 convert_template_argument (tree parm,
6240 tree arg,
6241 tree args,
6242 tsubst_flags_t complain,
6243 int i,
6244 tree in_decl)
6245 {
6246 tree orig_arg;
6247 tree val;
6248 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6249
6250 if (TREE_CODE (arg) == TREE_LIST
6251 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6252 {
6253 /* The template argument was the name of some
6254 member function. That's usually
6255 invalid, but static members are OK. In any
6256 case, grab the underlying fields/functions
6257 and issue an error later if required. */
6258 orig_arg = TREE_VALUE (arg);
6259 TREE_TYPE (arg) = unknown_type_node;
6260 }
6261
6262 orig_arg = arg;
6263
6264 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6265 requires_type = (TREE_CODE (parm) == TYPE_DECL
6266 || requires_tmpl_type);
6267
6268 /* When determining whether an argument pack expansion is a template,
6269 look at the pattern. */
6270 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6271 arg = PACK_EXPANSION_PATTERN (arg);
6272
6273 /* Deal with an injected-class-name used as a template template arg. */
6274 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6275 {
6276 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6277 if (TREE_CODE (t) == TEMPLATE_DECL)
6278 {
6279 if (cxx_dialect >= cxx11)
6280 /* OK under DR 1004. */;
6281 else if (complain & tf_warning_or_error)
6282 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6283 " used as template template argument", TYPE_NAME (arg));
6284 else if (flag_pedantic_errors)
6285 t = arg;
6286
6287 arg = t;
6288 }
6289 }
6290
6291 is_tmpl_type =
6292 ((TREE_CODE (arg) == TEMPLATE_DECL
6293 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6294 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6295 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6296 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6297
6298 if (is_tmpl_type
6299 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6300 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6301 arg = TYPE_STUB_DECL (arg);
6302
6303 is_type = TYPE_P (arg) || is_tmpl_type;
6304
6305 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6306 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6307 {
6308 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6309 {
6310 if (complain & tf_error)
6311 error ("invalid use of destructor %qE as a type", orig_arg);
6312 return error_mark_node;
6313 }
6314
6315 permerror (input_location,
6316 "to refer to a type member of a template parameter, "
6317 "use %<typename %E%>", orig_arg);
6318
6319 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6320 TREE_OPERAND (arg, 1),
6321 typename_type,
6322 complain);
6323 arg = orig_arg;
6324 is_type = 1;
6325 }
6326 if (is_type != requires_type)
6327 {
6328 if (in_decl)
6329 {
6330 if (complain & tf_error)
6331 {
6332 error ("type/value mismatch at argument %d in template "
6333 "parameter list for %qD",
6334 i + 1, in_decl);
6335 if (is_type)
6336 error (" expected a constant of type %qT, got %qT",
6337 TREE_TYPE (parm),
6338 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6339 else if (requires_tmpl_type)
6340 error (" expected a class template, got %qE", orig_arg);
6341 else
6342 error (" expected a type, got %qE", orig_arg);
6343 }
6344 }
6345 return error_mark_node;
6346 }
6347 if (is_tmpl_type ^ requires_tmpl_type)
6348 {
6349 if (in_decl && (complain & tf_error))
6350 {
6351 error ("type/value mismatch at argument %d in template "
6352 "parameter list for %qD",
6353 i + 1, in_decl);
6354 if (is_tmpl_type)
6355 error (" expected a type, got %qT", DECL_NAME (arg));
6356 else
6357 error (" expected a class template, got %qT", orig_arg);
6358 }
6359 return error_mark_node;
6360 }
6361
6362 if (is_type)
6363 {
6364 if (requires_tmpl_type)
6365 {
6366 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6367 val = orig_arg;
6368 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6369 /* The number of argument required is not known yet.
6370 Just accept it for now. */
6371 val = TREE_TYPE (arg);
6372 else
6373 {
6374 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6375 tree argparm;
6376
6377 /* Strip alias templates that are equivalent to another
6378 template. */
6379 arg = get_underlying_template (arg);
6380 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6381
6382 if (coerce_template_template_parms (parmparm, argparm,
6383 complain, in_decl,
6384 args))
6385 {
6386 val = arg;
6387
6388 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6389 TEMPLATE_DECL. */
6390 if (val != error_mark_node)
6391 {
6392 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6393 val = TREE_TYPE (val);
6394 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6395 val = make_pack_expansion (val);
6396 }
6397 }
6398 else
6399 {
6400 if (in_decl && (complain & tf_error))
6401 {
6402 error ("type/value mismatch at argument %d in "
6403 "template parameter list for %qD",
6404 i + 1, in_decl);
6405 error (" expected a template of type %qD, got %qT",
6406 parm, orig_arg);
6407 }
6408
6409 val = error_mark_node;
6410 }
6411 }
6412 }
6413 else
6414 val = orig_arg;
6415 /* We only form one instance of each template specialization.
6416 Therefore, if we use a non-canonical variant (i.e., a
6417 typedef), any future messages referring to the type will use
6418 the typedef, which is confusing if those future uses do not
6419 themselves also use the typedef. */
6420 if (TYPE_P (val))
6421 val = canonicalize_type_argument (val, complain);
6422 }
6423 else
6424 {
6425 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6426
6427 if (invalid_nontype_parm_type_p (t, complain))
6428 return error_mark_node;
6429
6430 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6431 {
6432 if (same_type_p (t, TREE_TYPE (orig_arg)))
6433 val = orig_arg;
6434 else
6435 {
6436 /* Not sure if this is reachable, but it doesn't hurt
6437 to be robust. */
6438 error ("type mismatch in nontype parameter pack");
6439 val = error_mark_node;
6440 }
6441 }
6442 else if (!dependent_template_arg_p (orig_arg)
6443 && !uses_template_parms (t))
6444 /* We used to call digest_init here. However, digest_init
6445 will report errors, which we don't want when complain
6446 is zero. More importantly, digest_init will try too
6447 hard to convert things: for example, `0' should not be
6448 converted to pointer type at this point according to
6449 the standard. Accepting this is not merely an
6450 extension, since deciding whether or not these
6451 conversions can occur is part of determining which
6452 function template to call, or whether a given explicit
6453 argument specification is valid. */
6454 val = convert_nontype_argument (t, orig_arg, complain);
6455 else
6456 val = strip_typedefs_expr (orig_arg);
6457
6458 if (val == NULL_TREE)
6459 val = error_mark_node;
6460 else if (val == error_mark_node && (complain & tf_error))
6461 error ("could not convert template argument %qE to %qT", orig_arg, t);
6462
6463 if (TREE_CODE (val) == SCOPE_REF)
6464 {
6465 /* Strip typedefs from the SCOPE_REF. */
6466 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6467 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6468 complain);
6469 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6470 QUALIFIED_NAME_IS_TEMPLATE (val));
6471 }
6472 }
6473
6474 return val;
6475 }
6476
6477 /* Coerces the remaining template arguments in INNER_ARGS (from
6478 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6479 Returns the coerced argument pack. PARM_IDX is the position of this
6480 parameter in the template parameter list. ARGS is the original
6481 template argument list. */
6482 static tree
6483 coerce_template_parameter_pack (tree parms,
6484 int parm_idx,
6485 tree args,
6486 tree inner_args,
6487 int arg_idx,
6488 tree new_args,
6489 int* lost,
6490 tree in_decl,
6491 tsubst_flags_t complain)
6492 {
6493 tree parm = TREE_VEC_ELT (parms, parm_idx);
6494 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6495 tree packed_args;
6496 tree argument_pack;
6497 tree packed_types = NULL_TREE;
6498
6499 if (arg_idx > nargs)
6500 arg_idx = nargs;
6501
6502 packed_args = make_tree_vec (nargs - arg_idx);
6503
6504 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6505 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6506 {
6507 /* When the template parameter is a non-type template
6508 parameter pack whose type uses parameter packs, we need
6509 to look at each of the template arguments
6510 separately. Build a vector of the types for these
6511 non-type template parameters in PACKED_TYPES. */
6512 tree expansion
6513 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6514 packed_types = tsubst_pack_expansion (expansion, args,
6515 complain, in_decl);
6516
6517 if (packed_types == error_mark_node)
6518 return error_mark_node;
6519
6520 /* Check that we have the right number of arguments. */
6521 if (arg_idx < nargs
6522 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6523 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6524 {
6525 int needed_parms
6526 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6527 error ("wrong number of template arguments (%d, should be %d)",
6528 nargs, needed_parms);
6529 return error_mark_node;
6530 }
6531
6532 /* If we aren't able to check the actual arguments now
6533 (because they haven't been expanded yet), we can at least
6534 verify that all of the types used for the non-type
6535 template parameter pack are, in fact, valid for non-type
6536 template parameters. */
6537 if (arg_idx < nargs
6538 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6539 {
6540 int j, len = TREE_VEC_LENGTH (packed_types);
6541 for (j = 0; j < len; ++j)
6542 {
6543 tree t = TREE_VEC_ELT (packed_types, j);
6544 if (invalid_nontype_parm_type_p (t, complain))
6545 return error_mark_node;
6546 }
6547 }
6548 }
6549
6550 /* Convert the remaining arguments, which will be a part of the
6551 parameter pack "parm". */
6552 for (; arg_idx < nargs; ++arg_idx)
6553 {
6554 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6555 tree actual_parm = TREE_VALUE (parm);
6556
6557 if (packed_types && !PACK_EXPANSION_P (arg))
6558 {
6559 /* When we have a vector of types (corresponding to the
6560 non-type template parameter pack that uses parameter
6561 packs in its type, as mention above), and the
6562 argument is not an expansion (which expands to a
6563 currently unknown number of arguments), clone the
6564 parm and give it the next type in PACKED_TYPES. */
6565 actual_parm = copy_node (actual_parm);
6566 TREE_TYPE (actual_parm) =
6567 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6568 }
6569
6570 if (arg == error_mark_node)
6571 {
6572 if (complain & tf_error)
6573 error ("template argument %d is invalid", arg_idx + 1);
6574 }
6575 else
6576 arg = convert_template_argument (actual_parm,
6577 arg, new_args, complain, parm_idx,
6578 in_decl);
6579 if (arg == error_mark_node)
6580 (*lost)++;
6581 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6582 }
6583
6584 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6585 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6586 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6587 else
6588 {
6589 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6590 TREE_TYPE (argument_pack)
6591 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6592 TREE_CONSTANT (argument_pack) = 1;
6593 }
6594
6595 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6596 #ifdef ENABLE_CHECKING
6597 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6598 TREE_VEC_LENGTH (packed_args));
6599 #endif
6600 return argument_pack;
6601 }
6602
6603 /* Returns the number of pack expansions in the template argument vector
6604 ARGS. */
6605
6606 static int
6607 pack_expansion_args_count (tree args)
6608 {
6609 int i;
6610 int count = 0;
6611 if (args)
6612 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6613 {
6614 tree elt = TREE_VEC_ELT (args, i);
6615 if (elt && PACK_EXPANSION_P (elt))
6616 ++count;
6617 }
6618 return count;
6619 }
6620
6621 /* Convert all template arguments to their appropriate types, and
6622 return a vector containing the innermost resulting template
6623 arguments. If any error occurs, return error_mark_node. Error and
6624 warning messages are issued under control of COMPLAIN.
6625
6626 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6627 for arguments not specified in ARGS. Otherwise, if
6628 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6629 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6630 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6631 ARGS. */
6632
6633 static tree
6634 coerce_template_parms (tree parms,
6635 tree args,
6636 tree in_decl,
6637 tsubst_flags_t complain,
6638 bool require_all_args,
6639 bool use_default_args)
6640 {
6641 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6642 tree inner_args;
6643 tree new_args;
6644 tree new_inner_args;
6645 int saved_unevaluated_operand;
6646 int saved_inhibit_evaluation_warnings;
6647
6648 /* When used as a boolean value, indicates whether this is a
6649 variadic template parameter list. Since it's an int, we can also
6650 subtract it from nparms to get the number of non-variadic
6651 parameters. */
6652 int variadic_p = 0;
6653 int variadic_args_p = 0;
6654 int post_variadic_parms = 0;
6655
6656 if (args == error_mark_node)
6657 return error_mark_node;
6658
6659 nparms = TREE_VEC_LENGTH (parms);
6660
6661 /* Determine if there are any parameter packs. */
6662 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6663 {
6664 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6665 if (variadic_p)
6666 ++post_variadic_parms;
6667 if (template_parameter_pack_p (tparm))
6668 ++variadic_p;
6669 }
6670
6671 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6672 /* If there are no parameters that follow a parameter pack, we need to
6673 expand any argument packs so that we can deduce a parameter pack from
6674 some non-packed args followed by an argument pack, as in variadic85.C.
6675 If there are such parameters, we need to leave argument packs intact
6676 so the arguments are assigned properly. This can happen when dealing
6677 with a nested class inside a partial specialization of a class
6678 template, as in variadic92.C, or when deducing a template parameter pack
6679 from a sub-declarator, as in variadic114.C. */
6680 if (!post_variadic_parms)
6681 inner_args = expand_template_argument_pack (inner_args);
6682
6683 /* Count any pack expansion args. */
6684 variadic_args_p = pack_expansion_args_count (inner_args);
6685
6686 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6687 if ((nargs > nparms && !variadic_p)
6688 || (nargs < nparms - variadic_p
6689 && require_all_args
6690 && !variadic_args_p
6691 && (!use_default_args
6692 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6693 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6694 {
6695 if (complain & tf_error)
6696 {
6697 if (variadic_p)
6698 {
6699 nparms -= variadic_p;
6700 error ("wrong number of template arguments "
6701 "(%d, should be %d or more)", nargs, nparms);
6702 }
6703 else
6704 error ("wrong number of template arguments "
6705 "(%d, should be %d)", nargs, nparms);
6706
6707 if (in_decl)
6708 error ("provided for %q+D", in_decl);
6709 }
6710
6711 return error_mark_node;
6712 }
6713 /* We can't pass a pack expansion to a non-pack parameter of an alias
6714 template (DR 1430). */
6715 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6716 && variadic_args_p
6717 && nargs - variadic_args_p < nparms - variadic_p)
6718 {
6719 if (complain & tf_error)
6720 {
6721 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6722 {
6723 tree arg = TREE_VEC_ELT (inner_args, i);
6724 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6725
6726 if (PACK_EXPANSION_P (arg)
6727 && !template_parameter_pack_p (parm))
6728 {
6729 error ("pack expansion argument for non-pack parameter "
6730 "%qD of alias template %qD", parm, in_decl);
6731 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6732 goto found;
6733 }
6734 }
6735 gcc_unreachable ();
6736 found:;
6737 }
6738 return error_mark_node;
6739 }
6740
6741 /* We need to evaluate the template arguments, even though this
6742 template-id may be nested within a "sizeof". */
6743 saved_unevaluated_operand = cp_unevaluated_operand;
6744 cp_unevaluated_operand = 0;
6745 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6746 c_inhibit_evaluation_warnings = 0;
6747 new_inner_args = make_tree_vec (nparms);
6748 new_args = add_outermost_template_args (args, new_inner_args);
6749 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6750 {
6751 tree arg;
6752 tree parm;
6753
6754 /* Get the Ith template parameter. */
6755 parm = TREE_VEC_ELT (parms, parm_idx);
6756
6757 if (parm == error_mark_node)
6758 {
6759 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6760 continue;
6761 }
6762
6763 /* Calculate the next argument. */
6764 if (arg_idx < nargs)
6765 arg = TREE_VEC_ELT (inner_args, arg_idx);
6766 else
6767 arg = NULL_TREE;
6768
6769 if (template_parameter_pack_p (TREE_VALUE (parm))
6770 && !(arg && ARGUMENT_PACK_P (arg)))
6771 {
6772 /* All remaining arguments will be placed in the
6773 template parameter pack PARM. */
6774 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6775 inner_args, arg_idx,
6776 new_args, &lost,
6777 in_decl, complain);
6778
6779 /* Store this argument. */
6780 if (arg == error_mark_node)
6781 lost++;
6782 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6783
6784 /* We are done with all of the arguments. */
6785 arg_idx = nargs;
6786
6787 continue;
6788 }
6789 else if (arg)
6790 {
6791 if (PACK_EXPANSION_P (arg))
6792 {
6793 /* We don't know how many args we have yet, just
6794 use the unconverted ones for now. */
6795 new_inner_args = inner_args;
6796 break;
6797 }
6798 }
6799 else if (require_all_args)
6800 {
6801 /* There must be a default arg in this case. */
6802 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6803 complain, in_decl);
6804 /* The position of the first default template argument,
6805 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6806 Record that. */
6807 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6808 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6809 }
6810 else
6811 break;
6812
6813 if (arg == error_mark_node)
6814 {
6815 if (complain & tf_error)
6816 error ("template argument %d is invalid", arg_idx + 1);
6817 }
6818 else if (!arg)
6819 /* This only occurs if there was an error in the template
6820 parameter list itself (which we would already have
6821 reported) that we are trying to recover from, e.g., a class
6822 template with a parameter list such as
6823 template<typename..., typename>. */
6824 ++lost;
6825 else
6826 arg = convert_template_argument (TREE_VALUE (parm),
6827 arg, new_args, complain,
6828 parm_idx, in_decl);
6829
6830 if (arg == error_mark_node)
6831 lost++;
6832 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6833 }
6834 cp_unevaluated_operand = saved_unevaluated_operand;
6835 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6836
6837 if (lost)
6838 return error_mark_node;
6839
6840 #ifdef ENABLE_CHECKING
6841 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6843 TREE_VEC_LENGTH (new_inner_args));
6844 #endif
6845
6846 return new_inner_args;
6847 }
6848
6849 /* Like coerce_template_parms. If PARMS represents all template
6850 parameters levels, this function returns a vector of vectors
6851 representing all the resulting argument levels. Note that in this
6852 case, only the innermost arguments are coerced because the
6853 outermost ones are supposed to have been coerced already.
6854
6855 Otherwise, if PARMS represents only (the innermost) vector of
6856 parameters, this function returns a vector containing just the
6857 innermost resulting arguments. */
6858
6859 static tree
6860 coerce_innermost_template_parms (tree parms,
6861 tree args,
6862 tree in_decl,
6863 tsubst_flags_t complain,
6864 bool require_all_args,
6865 bool use_default_args)
6866 {
6867 int parms_depth = TMPL_PARMS_DEPTH (parms);
6868 int args_depth = TMPL_ARGS_DEPTH (args);
6869 tree coerced_args;
6870
6871 if (parms_depth > 1)
6872 {
6873 coerced_args = make_tree_vec (parms_depth);
6874 tree level;
6875 int cur_depth;
6876
6877 for (level = parms, cur_depth = parms_depth;
6878 parms_depth > 0 && level != NULL_TREE;
6879 level = TREE_CHAIN (level), --cur_depth)
6880 {
6881 tree l;
6882 if (cur_depth == args_depth)
6883 l = coerce_template_parms (TREE_VALUE (level),
6884 args, in_decl, complain,
6885 require_all_args,
6886 use_default_args);
6887 else
6888 l = TMPL_ARGS_LEVEL (args, cur_depth);
6889
6890 if (l == error_mark_node)
6891 return error_mark_node;
6892
6893 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6894 }
6895 }
6896 else
6897 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6898 args, in_decl, complain,
6899 require_all_args,
6900 use_default_args);
6901 return coerced_args;
6902 }
6903
6904 /* Returns 1 if template args OT and NT are equivalent. */
6905
6906 static int
6907 template_args_equal (tree ot, tree nt)
6908 {
6909 if (nt == ot)
6910 return 1;
6911 if (nt == NULL_TREE || ot == NULL_TREE)
6912 return false;
6913
6914 if (TREE_CODE (nt) == TREE_VEC)
6915 /* For member templates */
6916 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6917 else if (PACK_EXPANSION_P (ot))
6918 return (PACK_EXPANSION_P (nt)
6919 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6920 PACK_EXPANSION_PATTERN (nt))
6921 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6922 PACK_EXPANSION_EXTRA_ARGS (nt)));
6923 else if (ARGUMENT_PACK_P (ot))
6924 {
6925 int i, len;
6926 tree opack, npack;
6927
6928 if (!ARGUMENT_PACK_P (nt))
6929 return 0;
6930
6931 opack = ARGUMENT_PACK_ARGS (ot);
6932 npack = ARGUMENT_PACK_ARGS (nt);
6933 len = TREE_VEC_LENGTH (opack);
6934 if (TREE_VEC_LENGTH (npack) != len)
6935 return 0;
6936 for (i = 0; i < len; ++i)
6937 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6938 TREE_VEC_ELT (npack, i)))
6939 return 0;
6940 return 1;
6941 }
6942 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6943 {
6944 /* We get here probably because we are in the middle of substituting
6945 into the pattern of a pack expansion. In that case the
6946 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6947 interested in. So we want to use the initial pack argument for
6948 the comparison. */
6949 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6950 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6951 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6952 return template_args_equal (ot, nt);
6953 }
6954 else if (TYPE_P (nt))
6955 return TYPE_P (ot) && same_type_p (ot, nt);
6956 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6957 return 0;
6958 else
6959 return cp_tree_equal (ot, nt);
6960 }
6961
6962 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6963 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6964 NEWARG_PTR with the offending arguments if they are non-NULL. */
6965
6966 static int
6967 comp_template_args_with_info (tree oldargs, tree newargs,
6968 tree *oldarg_ptr, tree *newarg_ptr)
6969 {
6970 int i;
6971
6972 if (oldargs == newargs)
6973 return 1;
6974
6975 if (!oldargs || !newargs)
6976 return 0;
6977
6978 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6979 return 0;
6980
6981 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6982 {
6983 tree nt = TREE_VEC_ELT (newargs, i);
6984 tree ot = TREE_VEC_ELT (oldargs, i);
6985
6986 if (! template_args_equal (ot, nt))
6987 {
6988 if (oldarg_ptr != NULL)
6989 *oldarg_ptr = ot;
6990 if (newarg_ptr != NULL)
6991 *newarg_ptr = nt;
6992 return 0;
6993 }
6994 }
6995 return 1;
6996 }
6997
6998 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6999 of template arguments. Returns 0 otherwise. */
7000
7001 int
7002 comp_template_args (tree oldargs, tree newargs)
7003 {
7004 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7005 }
7006
7007 static void
7008 add_pending_template (tree d)
7009 {
7010 tree ti = (TYPE_P (d)
7011 ? CLASSTYPE_TEMPLATE_INFO (d)
7012 : DECL_TEMPLATE_INFO (d));
7013 struct pending_template *pt;
7014 int level;
7015
7016 if (TI_PENDING_TEMPLATE_FLAG (ti))
7017 return;
7018
7019 /* We are called both from instantiate_decl, where we've already had a
7020 tinst_level pushed, and instantiate_template, where we haven't.
7021 Compensate. */
7022 level = !current_tinst_level || current_tinst_level->decl != d;
7023
7024 if (level)
7025 push_tinst_level (d);
7026
7027 pt = ggc_alloc_pending_template ();
7028 pt->next = NULL;
7029 pt->tinst = current_tinst_level;
7030 if (last_pending_template)
7031 last_pending_template->next = pt;
7032 else
7033 pending_templates = pt;
7034
7035 last_pending_template = pt;
7036
7037 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7038
7039 if (level)
7040 pop_tinst_level ();
7041 }
7042
7043
7044 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7045 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7046 documentation for TEMPLATE_ID_EXPR. */
7047
7048 tree
7049 lookup_template_function (tree fns, tree arglist)
7050 {
7051 tree type;
7052
7053 if (fns == error_mark_node || arglist == error_mark_node)
7054 return error_mark_node;
7055
7056 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7057
7058 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7059 {
7060 error ("%q#D is not a function template", fns);
7061 return error_mark_node;
7062 }
7063
7064 if (BASELINK_P (fns))
7065 {
7066 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7067 unknown_type_node,
7068 BASELINK_FUNCTIONS (fns),
7069 arglist);
7070 return fns;
7071 }
7072
7073 type = TREE_TYPE (fns);
7074 if (TREE_CODE (fns) == OVERLOAD || !type)
7075 type = unknown_type_node;
7076
7077 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7078 }
7079
7080 /* Within the scope of a template class S<T>, the name S gets bound
7081 (in build_self_reference) to a TYPE_DECL for the class, not a
7082 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7083 or one of its enclosing classes, and that type is a template,
7084 return the associated TEMPLATE_DECL. Otherwise, the original
7085 DECL is returned.
7086
7087 Also handle the case when DECL is a TREE_LIST of ambiguous
7088 injected-class-names from different bases. */
7089
7090 tree
7091 maybe_get_template_decl_from_type_decl (tree decl)
7092 {
7093 if (decl == NULL_TREE)
7094 return decl;
7095
7096 /* DR 176: A lookup that finds an injected-class-name (10.2
7097 [class.member.lookup]) can result in an ambiguity in certain cases
7098 (for example, if it is found in more than one base class). If all of
7099 the injected-class-names that are found refer to specializations of
7100 the same class template, and if the name is followed by a
7101 template-argument-list, the reference refers to the class template
7102 itself and not a specialization thereof, and is not ambiguous. */
7103 if (TREE_CODE (decl) == TREE_LIST)
7104 {
7105 tree t, tmpl = NULL_TREE;
7106 for (t = decl; t; t = TREE_CHAIN (t))
7107 {
7108 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7109 if (!tmpl)
7110 tmpl = elt;
7111 else if (tmpl != elt)
7112 break;
7113 }
7114 if (tmpl && t == NULL_TREE)
7115 return tmpl;
7116 else
7117 return decl;
7118 }
7119
7120 return (decl != NULL_TREE
7121 && DECL_SELF_REFERENCE_P (decl)
7122 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7123 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7124 }
7125
7126 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7127 parameters, find the desired type.
7128
7129 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7130
7131 IN_DECL, if non-NULL, is the template declaration we are trying to
7132 instantiate.
7133
7134 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7135 the class we are looking up.
7136
7137 Issue error and warning messages under control of COMPLAIN.
7138
7139 If the template class is really a local class in a template
7140 function, then the FUNCTION_CONTEXT is the function in which it is
7141 being instantiated.
7142
7143 ??? Note that this function is currently called *twice* for each
7144 template-id: the first time from the parser, while creating the
7145 incomplete type (finish_template_type), and the second type during the
7146 real instantiation (instantiate_template_class). This is surely something
7147 that we want to avoid. It also causes some problems with argument
7148 coercion (see convert_nontype_argument for more information on this). */
7149
7150 static tree
7151 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7152 int entering_scope, tsubst_flags_t complain)
7153 {
7154 tree templ = NULL_TREE, parmlist;
7155 tree t;
7156 void **slot;
7157 spec_entry *entry;
7158 spec_entry elt;
7159 hashval_t hash;
7160
7161 if (identifier_p (d1))
7162 {
7163 tree value = innermost_non_namespace_value (d1);
7164 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7165 templ = value;
7166 else
7167 {
7168 if (context)
7169 push_decl_namespace (context);
7170 templ = lookup_name (d1);
7171 templ = maybe_get_template_decl_from_type_decl (templ);
7172 if (context)
7173 pop_decl_namespace ();
7174 }
7175 if (templ)
7176 context = DECL_CONTEXT (templ);
7177 }
7178 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7179 {
7180 tree type = TREE_TYPE (d1);
7181
7182 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7183 an implicit typename for the second A. Deal with it. */
7184 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7185 type = TREE_TYPE (type);
7186
7187 if (CLASSTYPE_TEMPLATE_INFO (type))
7188 {
7189 templ = CLASSTYPE_TI_TEMPLATE (type);
7190 d1 = DECL_NAME (templ);
7191 }
7192 }
7193 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7194 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7195 {
7196 templ = TYPE_TI_TEMPLATE (d1);
7197 d1 = DECL_NAME (templ);
7198 }
7199 else if (TREE_CODE (d1) == TEMPLATE_DECL
7200 && DECL_TEMPLATE_RESULT (d1)
7201 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7202 {
7203 templ = d1;
7204 d1 = DECL_NAME (templ);
7205 context = DECL_CONTEXT (templ);
7206 }
7207 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7208 {
7209 templ = d1;
7210 d1 = DECL_NAME (templ);
7211 }
7212
7213 /* Issue an error message if we didn't find a template. */
7214 if (! templ)
7215 {
7216 if (complain & tf_error)
7217 error ("%qT is not a template", d1);
7218 return error_mark_node;
7219 }
7220
7221 if (TREE_CODE (templ) != TEMPLATE_DECL
7222 /* Make sure it's a user visible template, if it was named by
7223 the user. */
7224 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7225 && !PRIMARY_TEMPLATE_P (templ)))
7226 {
7227 if (complain & tf_error)
7228 {
7229 error ("non-template type %qT used as a template", d1);
7230 if (in_decl)
7231 error ("for template declaration %q+D", in_decl);
7232 }
7233 return error_mark_node;
7234 }
7235
7236 complain &= ~tf_user;
7237
7238 /* An alias that just changes the name of a template is equivalent to the
7239 other template, so if any of the arguments are pack expansions, strip
7240 the alias to avoid problems with a pack expansion passed to a non-pack
7241 alias template parameter (DR 1430). */
7242 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7243 templ = get_underlying_template (templ);
7244
7245 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7246 {
7247 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7248 template arguments */
7249
7250 tree parm;
7251 tree arglist2;
7252 tree outer;
7253
7254 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7255
7256 /* Consider an example where a template template parameter declared as
7257
7258 template <class T, class U = std::allocator<T> > class TT
7259
7260 The template parameter level of T and U are one level larger than
7261 of TT. To proper process the default argument of U, say when an
7262 instantiation `TT<int>' is seen, we need to build the full
7263 arguments containing {int} as the innermost level. Outer levels,
7264 available when not appearing as default template argument, can be
7265 obtained from the arguments of the enclosing template.
7266
7267 Suppose that TT is later substituted with std::vector. The above
7268 instantiation is `TT<int, std::allocator<T> >' with TT at
7269 level 1, and T at level 2, while the template arguments at level 1
7270 becomes {std::vector} and the inner level 2 is {int}. */
7271
7272 outer = DECL_CONTEXT (templ);
7273 if (outer)
7274 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7275 else if (current_template_parms)
7276 /* This is an argument of the current template, so we haven't set
7277 DECL_CONTEXT yet. */
7278 outer = current_template_args ();
7279
7280 if (outer)
7281 arglist = add_to_template_args (outer, arglist);
7282
7283 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7284 complain,
7285 /*require_all_args=*/true,
7286 /*use_default_args=*/true);
7287 if (arglist2 == error_mark_node
7288 || (!uses_template_parms (arglist2)
7289 && check_instantiated_args (templ, arglist2, complain)))
7290 return error_mark_node;
7291
7292 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7293 return parm;
7294 }
7295 else
7296 {
7297 tree template_type = TREE_TYPE (templ);
7298 tree gen_tmpl;
7299 tree type_decl;
7300 tree found = NULL_TREE;
7301 int arg_depth;
7302 int parm_depth;
7303 int is_dependent_type;
7304 int use_partial_inst_tmpl = false;
7305
7306 if (template_type == error_mark_node)
7307 /* An error occurred while building the template TEMPL, and a
7308 diagnostic has most certainly been emitted for that
7309 already. Let's propagate that error. */
7310 return error_mark_node;
7311
7312 gen_tmpl = most_general_template (templ);
7313 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7314 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7315 arg_depth = TMPL_ARGS_DEPTH (arglist);
7316
7317 if (arg_depth == 1 && parm_depth > 1)
7318 {
7319 /* We've been given an incomplete set of template arguments.
7320 For example, given:
7321
7322 template <class T> struct S1 {
7323 template <class U> struct S2 {};
7324 template <class U> struct S2<U*> {};
7325 };
7326
7327 we will be called with an ARGLIST of `U*', but the
7328 TEMPLATE will be `template <class T> template
7329 <class U> struct S1<T>::S2'. We must fill in the missing
7330 arguments. */
7331 arglist
7332 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7333 arglist);
7334 arg_depth = TMPL_ARGS_DEPTH (arglist);
7335 }
7336
7337 /* Now we should have enough arguments. */
7338 gcc_assert (parm_depth == arg_depth);
7339
7340 /* From here on, we're only interested in the most general
7341 template. */
7342
7343 /* Calculate the BOUND_ARGS. These will be the args that are
7344 actually tsubst'd into the definition to create the
7345 instantiation. */
7346 if (parm_depth > 1)
7347 {
7348 /* We have multiple levels of arguments to coerce, at once. */
7349 int i;
7350 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7351
7352 tree bound_args = make_tree_vec (parm_depth);
7353
7354 for (i = saved_depth,
7355 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7356 i > 0 && t != NULL_TREE;
7357 --i, t = TREE_CHAIN (t))
7358 {
7359 tree a;
7360 if (i == saved_depth)
7361 a = coerce_template_parms (TREE_VALUE (t),
7362 arglist, gen_tmpl,
7363 complain,
7364 /*require_all_args=*/true,
7365 /*use_default_args=*/true);
7366 else
7367 /* Outer levels should have already been coerced. */
7368 a = TMPL_ARGS_LEVEL (arglist, i);
7369
7370 /* Don't process further if one of the levels fails. */
7371 if (a == error_mark_node)
7372 {
7373 /* Restore the ARGLIST to its full size. */
7374 TREE_VEC_LENGTH (arglist) = saved_depth;
7375 return error_mark_node;
7376 }
7377
7378 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7379
7380 /* We temporarily reduce the length of the ARGLIST so
7381 that coerce_template_parms will see only the arguments
7382 corresponding to the template parameters it is
7383 examining. */
7384 TREE_VEC_LENGTH (arglist)--;
7385 }
7386
7387 /* Restore the ARGLIST to its full size. */
7388 TREE_VEC_LENGTH (arglist) = saved_depth;
7389
7390 arglist = bound_args;
7391 }
7392 else
7393 arglist
7394 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7395 INNERMOST_TEMPLATE_ARGS (arglist),
7396 gen_tmpl,
7397 complain,
7398 /*require_all_args=*/true,
7399 /*use_default_args=*/true);
7400
7401 if (arglist == error_mark_node)
7402 /* We were unable to bind the arguments. */
7403 return error_mark_node;
7404
7405 /* In the scope of a template class, explicit references to the
7406 template class refer to the type of the template, not any
7407 instantiation of it. For example, in:
7408
7409 template <class T> class C { void f(C<T>); }
7410
7411 the `C<T>' is just the same as `C'. Outside of the
7412 class, however, such a reference is an instantiation. */
7413 if ((entering_scope
7414 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7415 || currently_open_class (template_type))
7416 /* comp_template_args is expensive, check it last. */
7417 && comp_template_args (TYPE_TI_ARGS (template_type),
7418 arglist))
7419 return template_type;
7420
7421 /* If we already have this specialization, return it. */
7422 elt.tmpl = gen_tmpl;
7423 elt.args = arglist;
7424 hash = hash_specialization (&elt);
7425 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7426 &elt, hash);
7427
7428 if (entry)
7429 return entry->spec;
7430
7431 is_dependent_type = uses_template_parms (arglist);
7432
7433 /* If the deduced arguments are invalid, then the binding
7434 failed. */
7435 if (!is_dependent_type
7436 && check_instantiated_args (gen_tmpl,
7437 INNERMOST_TEMPLATE_ARGS (arglist),
7438 complain))
7439 return error_mark_node;
7440
7441 if (!is_dependent_type
7442 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7443 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7444 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7445 {
7446 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7447 DECL_NAME (gen_tmpl),
7448 /*tag_scope=*/ts_global);
7449 return found;
7450 }
7451
7452 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7453 complain, in_decl);
7454 if (context == error_mark_node)
7455 return error_mark_node;
7456
7457 if (!context)
7458 context = global_namespace;
7459
7460 /* Create the type. */
7461 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7462 {
7463 if (!is_dependent_type)
7464 {
7465 set_current_access_from_decl (TYPE_NAME (template_type));
7466 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7467 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7468 arglist, complain, in_decl),
7469 SCOPED_ENUM_P (template_type), NULL);
7470 }
7471 else
7472 {
7473 /* We don't want to call start_enum for this type, since
7474 the values for the enumeration constants may involve
7475 template parameters. And, no one should be interested
7476 in the enumeration constants for such a type. */
7477 t = cxx_make_type (ENUMERAL_TYPE);
7478 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7479 }
7480 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7481 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7482 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7483 }
7484 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7485 {
7486 /* The user referred to a specialization of an alias
7487 template represented by GEN_TMPL.
7488
7489 [temp.alias]/2 says:
7490
7491 When a template-id refers to the specialization of an
7492 alias template, it is equivalent to the associated
7493 type obtained by substitution of its
7494 template-arguments for the template-parameters in the
7495 type-id of the alias template. */
7496
7497 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7498 /* Note that the call above (by indirectly calling
7499 register_specialization in tsubst_decl) registers the
7500 TYPE_DECL representing the specialization of the alias
7501 template. So next time someone substitutes ARGLIST for
7502 the template parms into the alias template (GEN_TMPL),
7503 she'll get that TYPE_DECL back. */
7504
7505 if (t == error_mark_node)
7506 return t;
7507 }
7508 else if (CLASS_TYPE_P (template_type))
7509 {
7510 t = make_class_type (TREE_CODE (template_type));
7511 CLASSTYPE_DECLARED_CLASS (t)
7512 = CLASSTYPE_DECLARED_CLASS (template_type);
7513 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7514 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7515
7516 /* A local class. Make sure the decl gets registered properly. */
7517 if (context == current_function_decl)
7518 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7519
7520 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7521 /* This instantiation is another name for the primary
7522 template type. Set the TYPE_CANONICAL field
7523 appropriately. */
7524 TYPE_CANONICAL (t) = template_type;
7525 else if (any_template_arguments_need_structural_equality_p (arglist))
7526 /* Some of the template arguments require structural
7527 equality testing, so this template class requires
7528 structural equality testing. */
7529 SET_TYPE_STRUCTURAL_EQUALITY (t);
7530 }
7531 else
7532 gcc_unreachable ();
7533
7534 /* If we called start_enum or pushtag above, this information
7535 will already be set up. */
7536 if (!TYPE_NAME (t))
7537 {
7538 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7539
7540 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7541 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7542 DECL_SOURCE_LOCATION (type_decl)
7543 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7544 }
7545 else
7546 type_decl = TYPE_NAME (t);
7547
7548 if (CLASS_TYPE_P (template_type))
7549 {
7550 TREE_PRIVATE (type_decl)
7551 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7552 TREE_PROTECTED (type_decl)
7553 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7554 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7555 {
7556 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7557 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7558 }
7559 }
7560
7561 /* Let's consider the explicit specialization of a member
7562 of a class template specialization that is implicitely instantiated,
7563 e.g.:
7564 template<class T>
7565 struct S
7566 {
7567 template<class U> struct M {}; //#0
7568 };
7569
7570 template<>
7571 template<>
7572 struct S<int>::M<char> //#1
7573 {
7574 int i;
7575 };
7576 [temp.expl.spec]/4 says this is valid.
7577
7578 In this case, when we write:
7579 S<int>::M<char> m;
7580
7581 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7582 the one of #0.
7583
7584 When we encounter #1, we want to store the partial instantiation
7585 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7586
7587 For all cases other than this "explicit specialization of member of a
7588 class template", we just want to store the most general template into
7589 the CLASSTYPE_TI_TEMPLATE of M.
7590
7591 This case of "explicit specialization of member of a class template"
7592 only happens when:
7593 1/ the enclosing class is an instantiation of, and therefore not
7594 the same as, the context of the most general template, and
7595 2/ we aren't looking at the partial instantiation itself, i.e.
7596 the innermost arguments are not the same as the innermost parms of
7597 the most general template.
7598
7599 So it's only when 1/ and 2/ happens that we want to use the partial
7600 instantiation of the member template in lieu of its most general
7601 template. */
7602
7603 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7604 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7605 /* the enclosing class must be an instantiation... */
7606 && CLASS_TYPE_P (context)
7607 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7608 {
7609 tree partial_inst_args;
7610 TREE_VEC_LENGTH (arglist)--;
7611 ++processing_template_decl;
7612 partial_inst_args =
7613 tsubst (INNERMOST_TEMPLATE_ARGS
7614 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7615 arglist, complain, NULL_TREE);
7616 --processing_template_decl;
7617 TREE_VEC_LENGTH (arglist)++;
7618 use_partial_inst_tmpl =
7619 /*...and we must not be looking at the partial instantiation
7620 itself. */
7621 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7622 partial_inst_args);
7623 }
7624
7625 if (!use_partial_inst_tmpl)
7626 /* This case is easy; there are no member templates involved. */
7627 found = gen_tmpl;
7628 else
7629 {
7630 /* This is a full instantiation of a member template. Find
7631 the partial instantiation of which this is an instance. */
7632
7633 /* Temporarily reduce by one the number of levels in the ARGLIST
7634 so as to avoid comparing the last set of arguments. */
7635 TREE_VEC_LENGTH (arglist)--;
7636 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7637 TREE_VEC_LENGTH (arglist)++;
7638 /* FOUND is either a proper class type, or an alias
7639 template specialization. In the later case, it's a
7640 TYPE_DECL, resulting from the substituting of arguments
7641 for parameters in the TYPE_DECL of the alias template
7642 done earlier. So be careful while getting the template
7643 of FOUND. */
7644 found = TREE_CODE (found) == TYPE_DECL
7645 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7646 : CLASSTYPE_TI_TEMPLATE (found);
7647 }
7648
7649 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7650
7651 elt.spec = t;
7652 slot = htab_find_slot_with_hash (type_specializations,
7653 &elt, hash, INSERT);
7654 entry = ggc_alloc_spec_entry ();
7655 *entry = elt;
7656 *slot = entry;
7657
7658 /* Note this use of the partial instantiation so we can check it
7659 later in maybe_process_partial_specialization. */
7660 DECL_TEMPLATE_INSTANTIATIONS (templ)
7661 = tree_cons (arglist, t,
7662 DECL_TEMPLATE_INSTANTIATIONS (templ));
7663
7664 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7665 /* Now that the type has been registered on the instantiations
7666 list, we set up the enumerators. Because the enumeration
7667 constants may involve the enumeration type itself, we make
7668 sure to register the type first, and then create the
7669 constants. That way, doing tsubst_expr for the enumeration
7670 constants won't result in recursive calls here; we'll find
7671 the instantiation and exit above. */
7672 tsubst_enum (template_type, t, arglist);
7673
7674 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7675 /* If the type makes use of template parameters, the
7676 code that generates debugging information will crash. */
7677 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7678
7679 /* Possibly limit visibility based on template args. */
7680 TREE_PUBLIC (type_decl) = 1;
7681 determine_visibility (type_decl);
7682
7683 return t;
7684 }
7685 }
7686
7687 /* Wrapper for lookup_template_class_1. */
7688
7689 tree
7690 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7691 int entering_scope, tsubst_flags_t complain)
7692 {
7693 tree ret;
7694 timevar_push (TV_TEMPLATE_INST);
7695 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7696 entering_scope, complain);
7697 timevar_pop (TV_TEMPLATE_INST);
7698 return ret;
7699 }
7700 \f
7701 struct pair_fn_data
7702 {
7703 tree_fn_t fn;
7704 void *data;
7705 /* True when we should also visit template parameters that occur in
7706 non-deduced contexts. */
7707 bool include_nondeduced_p;
7708 struct pointer_set_t *visited;
7709 };
7710
7711 /* Called from for_each_template_parm via walk_tree. */
7712
7713 static tree
7714 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7715 {
7716 tree t = *tp;
7717 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7718 tree_fn_t fn = pfd->fn;
7719 void *data = pfd->data;
7720
7721 if (TYPE_P (t)
7722 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7723 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7724 pfd->include_nondeduced_p))
7725 return error_mark_node;
7726
7727 switch (TREE_CODE (t))
7728 {
7729 case RECORD_TYPE:
7730 if (TYPE_PTRMEMFUNC_P (t))
7731 break;
7732 /* Fall through. */
7733
7734 case UNION_TYPE:
7735 case ENUMERAL_TYPE:
7736 if (!TYPE_TEMPLATE_INFO (t))
7737 *walk_subtrees = 0;
7738 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7739 fn, data, pfd->visited,
7740 pfd->include_nondeduced_p))
7741 return error_mark_node;
7742 break;
7743
7744 case INTEGER_TYPE:
7745 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7746 fn, data, pfd->visited,
7747 pfd->include_nondeduced_p)
7748 || for_each_template_parm (TYPE_MAX_VALUE (t),
7749 fn, data, pfd->visited,
7750 pfd->include_nondeduced_p))
7751 return error_mark_node;
7752 break;
7753
7754 case METHOD_TYPE:
7755 /* Since we're not going to walk subtrees, we have to do this
7756 explicitly here. */
7757 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7758 pfd->visited, pfd->include_nondeduced_p))
7759 return error_mark_node;
7760 /* Fall through. */
7761
7762 case FUNCTION_TYPE:
7763 /* Check the return type. */
7764 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7765 pfd->include_nondeduced_p))
7766 return error_mark_node;
7767
7768 /* Check the parameter types. Since default arguments are not
7769 instantiated until they are needed, the TYPE_ARG_TYPES may
7770 contain expressions that involve template parameters. But,
7771 no-one should be looking at them yet. And, once they're
7772 instantiated, they don't contain template parameters, so
7773 there's no point in looking at them then, either. */
7774 {
7775 tree parm;
7776
7777 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7778 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7779 pfd->visited, pfd->include_nondeduced_p))
7780 return error_mark_node;
7781
7782 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7783 want walk_tree walking into them itself. */
7784 *walk_subtrees = 0;
7785 }
7786 break;
7787
7788 case TYPEOF_TYPE:
7789 case UNDERLYING_TYPE:
7790 if (pfd->include_nondeduced_p
7791 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7792 pfd->visited,
7793 pfd->include_nondeduced_p))
7794 return error_mark_node;
7795 break;
7796
7797 case FUNCTION_DECL:
7798 case VAR_DECL:
7799 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7800 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7801 pfd->visited, pfd->include_nondeduced_p))
7802 return error_mark_node;
7803 /* Fall through. */
7804
7805 case PARM_DECL:
7806 case CONST_DECL:
7807 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7808 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7809 pfd->visited, pfd->include_nondeduced_p))
7810 return error_mark_node;
7811 if (DECL_CONTEXT (t)
7812 && pfd->include_nondeduced_p
7813 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7814 pfd->visited, pfd->include_nondeduced_p))
7815 return error_mark_node;
7816 break;
7817
7818 case BOUND_TEMPLATE_TEMPLATE_PARM:
7819 /* Record template parameters such as `T' inside `TT<T>'. */
7820 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7821 pfd->include_nondeduced_p))
7822 return error_mark_node;
7823 /* Fall through. */
7824
7825 case TEMPLATE_TEMPLATE_PARM:
7826 case TEMPLATE_TYPE_PARM:
7827 case TEMPLATE_PARM_INDEX:
7828 if (fn && (*fn)(t, data))
7829 return error_mark_node;
7830 else if (!fn)
7831 return error_mark_node;
7832 break;
7833
7834 case TEMPLATE_DECL:
7835 /* A template template parameter is encountered. */
7836 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7837 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7838 pfd->include_nondeduced_p))
7839 return error_mark_node;
7840
7841 /* Already substituted template template parameter */
7842 *walk_subtrees = 0;
7843 break;
7844
7845 case TYPENAME_TYPE:
7846 if (!fn
7847 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7848 data, pfd->visited,
7849 pfd->include_nondeduced_p))
7850 return error_mark_node;
7851 break;
7852
7853 case CONSTRUCTOR:
7854 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7855 && pfd->include_nondeduced_p
7856 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7857 (TREE_TYPE (t)), fn, data,
7858 pfd->visited, pfd->include_nondeduced_p))
7859 return error_mark_node;
7860 break;
7861
7862 case INDIRECT_REF:
7863 case COMPONENT_REF:
7864 /* If there's no type, then this thing must be some expression
7865 involving template parameters. */
7866 if (!fn && !TREE_TYPE (t))
7867 return error_mark_node;
7868 break;
7869
7870 case MODOP_EXPR:
7871 case CAST_EXPR:
7872 case IMPLICIT_CONV_EXPR:
7873 case REINTERPRET_CAST_EXPR:
7874 case CONST_CAST_EXPR:
7875 case STATIC_CAST_EXPR:
7876 case DYNAMIC_CAST_EXPR:
7877 case ARROW_EXPR:
7878 case DOTSTAR_EXPR:
7879 case TYPEID_EXPR:
7880 case PSEUDO_DTOR_EXPR:
7881 if (!fn)
7882 return error_mark_node;
7883 break;
7884
7885 default:
7886 break;
7887 }
7888
7889 /* We didn't find any template parameters we liked. */
7890 return NULL_TREE;
7891 }
7892
7893 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7894 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7895 call FN with the parameter and the DATA.
7896 If FN returns nonzero, the iteration is terminated, and
7897 for_each_template_parm returns 1. Otherwise, the iteration
7898 continues. If FN never returns a nonzero value, the value
7899 returned by for_each_template_parm is 0. If FN is NULL, it is
7900 considered to be the function which always returns 1.
7901
7902 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7903 parameters that occur in non-deduced contexts. When false, only
7904 visits those template parameters that can be deduced. */
7905
7906 static int
7907 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7908 struct pointer_set_t *visited,
7909 bool include_nondeduced_p)
7910 {
7911 struct pair_fn_data pfd;
7912 int result;
7913
7914 /* Set up. */
7915 pfd.fn = fn;
7916 pfd.data = data;
7917 pfd.include_nondeduced_p = include_nondeduced_p;
7918
7919 /* Walk the tree. (Conceptually, we would like to walk without
7920 duplicates, but for_each_template_parm_r recursively calls
7921 for_each_template_parm, so we would need to reorganize a fair
7922 bit to use walk_tree_without_duplicates, so we keep our own
7923 visited list.) */
7924 if (visited)
7925 pfd.visited = visited;
7926 else
7927 pfd.visited = pointer_set_create ();
7928 result = cp_walk_tree (&t,
7929 for_each_template_parm_r,
7930 &pfd,
7931 pfd.visited) != NULL_TREE;
7932
7933 /* Clean up. */
7934 if (!visited)
7935 {
7936 pointer_set_destroy (pfd.visited);
7937 pfd.visited = 0;
7938 }
7939
7940 return result;
7941 }
7942
7943 /* Returns true if T depends on any template parameter. */
7944
7945 int
7946 uses_template_parms (tree t)
7947 {
7948 bool dependent_p;
7949 int saved_processing_template_decl;
7950
7951 saved_processing_template_decl = processing_template_decl;
7952 if (!saved_processing_template_decl)
7953 processing_template_decl = 1;
7954 if (TYPE_P (t))
7955 dependent_p = dependent_type_p (t);
7956 else if (TREE_CODE (t) == TREE_VEC)
7957 dependent_p = any_dependent_template_arguments_p (t);
7958 else if (TREE_CODE (t) == TREE_LIST)
7959 dependent_p = (uses_template_parms (TREE_VALUE (t))
7960 || uses_template_parms (TREE_CHAIN (t)));
7961 else if (TREE_CODE (t) == TYPE_DECL)
7962 dependent_p = dependent_type_p (TREE_TYPE (t));
7963 else if (DECL_P (t)
7964 || EXPR_P (t)
7965 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7966 || TREE_CODE (t) == OVERLOAD
7967 || BASELINK_P (t)
7968 || identifier_p (t)
7969 || TREE_CODE (t) == TRAIT_EXPR
7970 || TREE_CODE (t) == CONSTRUCTOR
7971 || CONSTANT_CLASS_P (t))
7972 dependent_p = (type_dependent_expression_p (t)
7973 || value_dependent_expression_p (t));
7974 else
7975 {
7976 gcc_assert (t == error_mark_node);
7977 dependent_p = false;
7978 }
7979
7980 processing_template_decl = saved_processing_template_decl;
7981
7982 return dependent_p;
7983 }
7984
7985 /* Returns true iff current_function_decl is an incompletely instantiated
7986 template. Useful instead of processing_template_decl because the latter
7987 is set to 0 during fold_non_dependent_expr. */
7988
7989 bool
7990 in_template_function (void)
7991 {
7992 tree fn = current_function_decl;
7993 bool ret;
7994 ++processing_template_decl;
7995 ret = (fn && DECL_LANG_SPECIFIC (fn)
7996 && DECL_TEMPLATE_INFO (fn)
7997 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7998 --processing_template_decl;
7999 return ret;
8000 }
8001
8002 /* Returns true if T depends on any template parameter with level LEVEL. */
8003
8004 int
8005 uses_template_parms_level (tree t, int level)
8006 {
8007 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8008 /*include_nondeduced_p=*/true);
8009 }
8010
8011 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8012 ill-formed translation unit, i.e. a variable or function that isn't
8013 usable in a constant expression. */
8014
8015 static inline bool
8016 neglectable_inst_p (tree d)
8017 {
8018 return (DECL_P (d)
8019 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8020 : decl_maybe_constant_var_p (d)));
8021 }
8022
8023 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8024 neglectable and instantiated from within an erroneous instantiation. */
8025
8026 static bool
8027 limit_bad_template_recursion (tree decl)
8028 {
8029 struct tinst_level *lev = current_tinst_level;
8030 int errs = errorcount + sorrycount;
8031 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8032 return false;
8033
8034 for (; lev; lev = lev->next)
8035 if (neglectable_inst_p (lev->decl))
8036 break;
8037
8038 return (lev && errs > lev->errors);
8039 }
8040
8041 static int tinst_depth;
8042 extern int max_tinst_depth;
8043 int depth_reached;
8044
8045 static GTY(()) struct tinst_level *last_error_tinst_level;
8046
8047 /* We're starting to instantiate D; record the template instantiation context
8048 for diagnostics and to restore it later. */
8049
8050 int
8051 push_tinst_level (tree d)
8052 {
8053 struct tinst_level *new_level;
8054
8055 if (tinst_depth >= max_tinst_depth)
8056 {
8057 last_error_tinst_level = current_tinst_level;
8058 if (TREE_CODE (d) == TREE_LIST)
8059 error ("template instantiation depth exceeds maximum of %d (use "
8060 "-ftemplate-depth= to increase the maximum) substituting %qS",
8061 max_tinst_depth, d);
8062 else
8063 error ("template instantiation depth exceeds maximum of %d (use "
8064 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8065 max_tinst_depth, d);
8066
8067 print_instantiation_context ();
8068
8069 return 0;
8070 }
8071
8072 /* If the current instantiation caused problems, don't let it instantiate
8073 anything else. Do allow deduction substitution and decls usable in
8074 constant expressions. */
8075 if (limit_bad_template_recursion (d))
8076 return 0;
8077
8078 new_level = ggc_alloc_tinst_level ();
8079 new_level->decl = d;
8080 new_level->locus = input_location;
8081 new_level->errors = errorcount+sorrycount;
8082 new_level->in_system_header_p = in_system_header;
8083 new_level->next = current_tinst_level;
8084 current_tinst_level = new_level;
8085
8086 ++tinst_depth;
8087 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8088 depth_reached = tinst_depth;
8089
8090 return 1;
8091 }
8092
8093 /* We're done instantiating this template; return to the instantiation
8094 context. */
8095
8096 void
8097 pop_tinst_level (void)
8098 {
8099 /* Restore the filename and line number stashed away when we started
8100 this instantiation. */
8101 input_location = current_tinst_level->locus;
8102 current_tinst_level = current_tinst_level->next;
8103 --tinst_depth;
8104 }
8105
8106 /* We're instantiating a deferred template; restore the template
8107 instantiation context in which the instantiation was requested, which
8108 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8109
8110 static tree
8111 reopen_tinst_level (struct tinst_level *level)
8112 {
8113 struct tinst_level *t;
8114
8115 tinst_depth = 0;
8116 for (t = level; t; t = t->next)
8117 ++tinst_depth;
8118
8119 current_tinst_level = level;
8120 pop_tinst_level ();
8121 if (current_tinst_level)
8122 current_tinst_level->errors = errorcount+sorrycount;
8123 return level->decl;
8124 }
8125
8126 /* Returns the TINST_LEVEL which gives the original instantiation
8127 context. */
8128
8129 struct tinst_level *
8130 outermost_tinst_level (void)
8131 {
8132 struct tinst_level *level = current_tinst_level;
8133 if (level)
8134 while (level->next)
8135 level = level->next;
8136 return level;
8137 }
8138
8139 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8140 vector of template arguments, as for tsubst.
8141
8142 Returns an appropriate tsubst'd friend declaration. */
8143
8144 static tree
8145 tsubst_friend_function (tree decl, tree args)
8146 {
8147 tree new_friend;
8148
8149 if (TREE_CODE (decl) == FUNCTION_DECL
8150 && DECL_TEMPLATE_INSTANTIATION (decl)
8151 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8152 /* This was a friend declared with an explicit template
8153 argument list, e.g.:
8154
8155 friend void f<>(T);
8156
8157 to indicate that f was a template instantiation, not a new
8158 function declaration. Now, we have to figure out what
8159 instantiation of what template. */
8160 {
8161 tree template_id, arglist, fns;
8162 tree new_args;
8163 tree tmpl;
8164 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8165
8166 /* Friend functions are looked up in the containing namespace scope.
8167 We must enter that scope, to avoid finding member functions of the
8168 current class with same name. */
8169 push_nested_namespace (ns);
8170 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8171 tf_warning_or_error, NULL_TREE,
8172 /*integral_constant_expression_p=*/false);
8173 pop_nested_namespace (ns);
8174 arglist = tsubst (DECL_TI_ARGS (decl), args,
8175 tf_warning_or_error, NULL_TREE);
8176 template_id = lookup_template_function (fns, arglist);
8177
8178 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8179 tmpl = determine_specialization (template_id, new_friend,
8180 &new_args,
8181 /*need_member_template=*/0,
8182 TREE_VEC_LENGTH (args),
8183 tsk_none);
8184 return instantiate_template (tmpl, new_args, tf_error);
8185 }
8186
8187 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8188
8189 /* The NEW_FRIEND will look like an instantiation, to the
8190 compiler, but is not an instantiation from the point of view of
8191 the language. For example, we might have had:
8192
8193 template <class T> struct S {
8194 template <class U> friend void f(T, U);
8195 };
8196
8197 Then, in S<int>, template <class U> void f(int, U) is not an
8198 instantiation of anything. */
8199 if (new_friend == error_mark_node)
8200 return error_mark_node;
8201
8202 DECL_USE_TEMPLATE (new_friend) = 0;
8203 if (TREE_CODE (decl) == TEMPLATE_DECL)
8204 {
8205 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8206 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8207 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8208 }
8209
8210 /* The mangled name for the NEW_FRIEND is incorrect. The function
8211 is not a template instantiation and should not be mangled like
8212 one. Therefore, we forget the mangling here; we'll recompute it
8213 later if we need it. */
8214 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8215 {
8216 SET_DECL_RTL (new_friend, NULL);
8217 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8218 }
8219
8220 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8221 {
8222 tree old_decl;
8223 tree new_friend_template_info;
8224 tree new_friend_result_template_info;
8225 tree ns;
8226 int new_friend_is_defn;
8227
8228 /* We must save some information from NEW_FRIEND before calling
8229 duplicate decls since that function will free NEW_FRIEND if
8230 possible. */
8231 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8232 new_friend_is_defn =
8233 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8234 (template_for_substitution (new_friend)))
8235 != NULL_TREE);
8236 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8237 {
8238 /* This declaration is a `primary' template. */
8239 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8240
8241 new_friend_result_template_info
8242 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8243 }
8244 else
8245 new_friend_result_template_info = NULL_TREE;
8246
8247 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8248 if (new_friend_is_defn)
8249 DECL_INITIAL (new_friend) = error_mark_node;
8250
8251 /* Inside pushdecl_namespace_level, we will push into the
8252 current namespace. However, the friend function should go
8253 into the namespace of the template. */
8254 ns = decl_namespace_context (new_friend);
8255 push_nested_namespace (ns);
8256 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8257 pop_nested_namespace (ns);
8258
8259 if (old_decl == error_mark_node)
8260 return error_mark_node;
8261
8262 if (old_decl != new_friend)
8263 {
8264 /* This new friend declaration matched an existing
8265 declaration. For example, given:
8266
8267 template <class T> void f(T);
8268 template <class U> class C {
8269 template <class T> friend void f(T) {}
8270 };
8271
8272 the friend declaration actually provides the definition
8273 of `f', once C has been instantiated for some type. So,
8274 old_decl will be the out-of-class template declaration,
8275 while new_friend is the in-class definition.
8276
8277 But, if `f' was called before this point, the
8278 instantiation of `f' will have DECL_TI_ARGS corresponding
8279 to `T' but not to `U', references to which might appear
8280 in the definition of `f'. Previously, the most general
8281 template for an instantiation of `f' was the out-of-class
8282 version; now it is the in-class version. Therefore, we
8283 run through all specialization of `f', adding to their
8284 DECL_TI_ARGS appropriately. In particular, they need a
8285 new set of outer arguments, corresponding to the
8286 arguments for this class instantiation.
8287
8288 The same situation can arise with something like this:
8289
8290 friend void f(int);
8291 template <class T> class C {
8292 friend void f(T) {}
8293 };
8294
8295 when `C<int>' is instantiated. Now, `f(int)' is defined
8296 in the class. */
8297
8298 if (!new_friend_is_defn)
8299 /* On the other hand, if the in-class declaration does
8300 *not* provide a definition, then we don't want to alter
8301 existing definitions. We can just leave everything
8302 alone. */
8303 ;
8304 else
8305 {
8306 tree new_template = TI_TEMPLATE (new_friend_template_info);
8307 tree new_args = TI_ARGS (new_friend_template_info);
8308
8309 /* Overwrite whatever template info was there before, if
8310 any, with the new template information pertaining to
8311 the declaration. */
8312 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8313
8314 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8315 {
8316 /* We should have called reregister_specialization in
8317 duplicate_decls. */
8318 gcc_assert (retrieve_specialization (new_template,
8319 new_args, 0)
8320 == old_decl);
8321
8322 /* Instantiate it if the global has already been used. */
8323 if (DECL_ODR_USED (old_decl))
8324 instantiate_decl (old_decl, /*defer_ok=*/true,
8325 /*expl_inst_class_mem_p=*/false);
8326 }
8327 else
8328 {
8329 tree t;
8330
8331 /* Indicate that the old function template is a partial
8332 instantiation. */
8333 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8334 = new_friend_result_template_info;
8335
8336 gcc_assert (new_template
8337 == most_general_template (new_template));
8338 gcc_assert (new_template != old_decl);
8339
8340 /* Reassign any specializations already in the hash table
8341 to the new more general template, and add the
8342 additional template args. */
8343 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8344 t != NULL_TREE;
8345 t = TREE_CHAIN (t))
8346 {
8347 tree spec = TREE_VALUE (t);
8348 spec_entry elt;
8349
8350 elt.tmpl = old_decl;
8351 elt.args = DECL_TI_ARGS (spec);
8352 elt.spec = NULL_TREE;
8353
8354 htab_remove_elt (decl_specializations, &elt);
8355
8356 DECL_TI_ARGS (spec)
8357 = add_outermost_template_args (new_args,
8358 DECL_TI_ARGS (spec));
8359
8360 register_specialization
8361 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8362
8363 }
8364 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8365 }
8366 }
8367
8368 /* The information from NEW_FRIEND has been merged into OLD_DECL
8369 by duplicate_decls. */
8370 new_friend = old_decl;
8371 }
8372 }
8373 else
8374 {
8375 tree context = DECL_CONTEXT (new_friend);
8376 bool dependent_p;
8377
8378 /* In the code
8379 template <class T> class C {
8380 template <class U> friend void C1<U>::f (); // case 1
8381 friend void C2<T>::f (); // case 2
8382 };
8383 we only need to make sure CONTEXT is a complete type for
8384 case 2. To distinguish between the two cases, we note that
8385 CONTEXT of case 1 remains dependent type after tsubst while
8386 this isn't true for case 2. */
8387 ++processing_template_decl;
8388 dependent_p = dependent_type_p (context);
8389 --processing_template_decl;
8390
8391 if (!dependent_p
8392 && !complete_type_or_else (context, NULL_TREE))
8393 return error_mark_node;
8394
8395 if (COMPLETE_TYPE_P (context))
8396 {
8397 /* Check to see that the declaration is really present, and,
8398 possibly obtain an improved declaration. */
8399 tree fn = check_classfn (context,
8400 new_friend, NULL_TREE);
8401
8402 if (fn)
8403 new_friend = fn;
8404 }
8405 }
8406
8407 return new_friend;
8408 }
8409
8410 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8411 template arguments, as for tsubst.
8412
8413 Returns an appropriate tsubst'd friend type or error_mark_node on
8414 failure. */
8415
8416 static tree
8417 tsubst_friend_class (tree friend_tmpl, tree args)
8418 {
8419 tree friend_type;
8420 tree tmpl;
8421 tree context;
8422
8423 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8424 {
8425 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8426 return TREE_TYPE (t);
8427 }
8428
8429 context = CP_DECL_CONTEXT (friend_tmpl);
8430
8431 if (context != global_namespace)
8432 {
8433 if (TREE_CODE (context) == NAMESPACE_DECL)
8434 push_nested_namespace (context);
8435 else
8436 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8437 }
8438
8439 /* Look for a class template declaration. We look for hidden names
8440 because two friend declarations of the same template are the
8441 same. For example, in:
8442
8443 struct A {
8444 template <typename> friend class F;
8445 };
8446 template <typename> struct B {
8447 template <typename> friend class F;
8448 };
8449
8450 both F templates are the same. */
8451 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8452 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8453
8454 /* But, if we don't find one, it might be because we're in a
8455 situation like this:
8456
8457 template <class T>
8458 struct S {
8459 template <class U>
8460 friend struct S;
8461 };
8462
8463 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8464 for `S<int>', not the TEMPLATE_DECL. */
8465 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8466 {
8467 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8468 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8469 }
8470
8471 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8472 {
8473 /* The friend template has already been declared. Just
8474 check to see that the declarations match, and install any new
8475 default parameters. We must tsubst the default parameters,
8476 of course. We only need the innermost template parameters
8477 because that is all that redeclare_class_template will look
8478 at. */
8479 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8480 > TMPL_ARGS_DEPTH (args))
8481 {
8482 tree parms;
8483 location_t saved_input_location;
8484 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8485 args, tf_warning_or_error);
8486
8487 saved_input_location = input_location;
8488 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8489 redeclare_class_template (TREE_TYPE (tmpl), parms);
8490 input_location = saved_input_location;
8491
8492 }
8493
8494 friend_type = TREE_TYPE (tmpl);
8495 }
8496 else
8497 {
8498 /* The friend template has not already been declared. In this
8499 case, the instantiation of the template class will cause the
8500 injection of this template into the global scope. */
8501 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8502 if (tmpl == error_mark_node)
8503 return error_mark_node;
8504
8505 /* The new TMPL is not an instantiation of anything, so we
8506 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8507 the new type because that is supposed to be the corresponding
8508 template decl, i.e., TMPL. */
8509 DECL_USE_TEMPLATE (tmpl) = 0;
8510 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8511 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8512 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8513 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8514
8515 /* Inject this template into the global scope. */
8516 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8517 }
8518
8519 if (context != global_namespace)
8520 {
8521 if (TREE_CODE (context) == NAMESPACE_DECL)
8522 pop_nested_namespace (context);
8523 else
8524 pop_nested_class ();
8525 }
8526
8527 return friend_type;
8528 }
8529
8530 /* Returns zero if TYPE cannot be completed later due to circularity.
8531 Otherwise returns one. */
8532
8533 static int
8534 can_complete_type_without_circularity (tree type)
8535 {
8536 if (type == NULL_TREE || type == error_mark_node)
8537 return 0;
8538 else if (COMPLETE_TYPE_P (type))
8539 return 1;
8540 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8541 return can_complete_type_without_circularity (TREE_TYPE (type));
8542 else if (CLASS_TYPE_P (type)
8543 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8544 return 0;
8545 else
8546 return 1;
8547 }
8548
8549 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8550
8551 /* Apply any attributes which had to be deferred until instantiation
8552 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8553 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8554
8555 static void
8556 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8557 tree args, tsubst_flags_t complain, tree in_decl)
8558 {
8559 tree last_dep = NULL_TREE;
8560 tree t;
8561 tree *p;
8562
8563 for (t = attributes; t; t = TREE_CHAIN (t))
8564 if (ATTR_IS_DEPENDENT (t))
8565 {
8566 last_dep = t;
8567 attributes = copy_list (attributes);
8568 break;
8569 }
8570
8571 if (DECL_P (*decl_p))
8572 {
8573 if (TREE_TYPE (*decl_p) == error_mark_node)
8574 return;
8575 p = &DECL_ATTRIBUTES (*decl_p);
8576 }
8577 else
8578 p = &TYPE_ATTRIBUTES (*decl_p);
8579
8580 if (last_dep)
8581 {
8582 tree late_attrs = NULL_TREE;
8583 tree *q = &late_attrs;
8584
8585 for (*p = attributes; *p; )
8586 {
8587 t = *p;
8588 if (ATTR_IS_DEPENDENT (t))
8589 {
8590 *p = TREE_CHAIN (t);
8591 TREE_CHAIN (t) = NULL_TREE;
8592 if (flag_openmp
8593 && is_attribute_p ("omp declare simd",
8594 get_attribute_name (t))
8595 && TREE_VALUE (t))
8596 {
8597 tree clauses = TREE_VALUE (TREE_VALUE (t));
8598 clauses = tsubst_omp_clauses (clauses, true, args,
8599 complain, in_decl);
8600 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8601 clauses = finish_omp_clauses (clauses);
8602 tree parms = DECL_ARGUMENTS (*decl_p);
8603 clauses
8604 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8605 if (clauses)
8606 TREE_VALUE (TREE_VALUE (t)) = clauses;
8607 else
8608 TREE_VALUE (t) = NULL_TREE;
8609 }
8610 /* If the first attribute argument is an identifier, don't
8611 pass it through tsubst. Attributes like mode, format,
8612 cleanup and several target specific attributes expect it
8613 unmodified. */
8614 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8615 {
8616 tree chain
8617 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8618 in_decl,
8619 /*integral_constant_expression_p=*/false);
8620 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8621 TREE_VALUE (t)
8622 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8623 chain);
8624 }
8625 else
8626 TREE_VALUE (t)
8627 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8628 /*integral_constant_expression_p=*/false);
8629 *q = t;
8630 q = &TREE_CHAIN (t);
8631 }
8632 else
8633 p = &TREE_CHAIN (t);
8634 }
8635
8636 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8637 }
8638 }
8639
8640 /* Perform (or defer) access check for typedefs that were referenced
8641 from within the template TMPL code.
8642 This is a subroutine of instantiate_decl and instantiate_class_template.
8643 TMPL is the template to consider and TARGS is the list of arguments of
8644 that template. */
8645
8646 static void
8647 perform_typedefs_access_check (tree tmpl, tree targs)
8648 {
8649 location_t saved_location;
8650 unsigned i;
8651 qualified_typedef_usage_t *iter;
8652
8653 if (!tmpl
8654 || (!CLASS_TYPE_P (tmpl)
8655 && TREE_CODE (tmpl) != FUNCTION_DECL))
8656 return;
8657
8658 saved_location = input_location;
8659 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8660 {
8661 tree type_decl = iter->typedef_decl;
8662 tree type_scope = iter->context;
8663
8664 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8665 continue;
8666
8667 if (uses_template_parms (type_decl))
8668 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8669 if (uses_template_parms (type_scope))
8670 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8671
8672 /* Make access check error messages point to the location
8673 of the use of the typedef. */
8674 input_location = iter->locus;
8675 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8676 type_decl, type_decl,
8677 tf_warning_or_error);
8678 }
8679 input_location = saved_location;
8680 }
8681
8682 static tree
8683 instantiate_class_template_1 (tree type)
8684 {
8685 tree templ, args, pattern, t, member;
8686 tree typedecl;
8687 tree pbinfo;
8688 tree base_list;
8689 unsigned int saved_maximum_field_alignment;
8690 tree fn_context;
8691
8692 if (type == error_mark_node)
8693 return error_mark_node;
8694
8695 if (COMPLETE_OR_OPEN_TYPE_P (type)
8696 || uses_template_parms (type))
8697 return type;
8698
8699 /* Figure out which template is being instantiated. */
8700 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8701 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8702
8703 /* Determine what specialization of the original template to
8704 instantiate. */
8705 t = most_specialized_class (type, templ, tf_warning_or_error);
8706 if (t == error_mark_node)
8707 {
8708 TYPE_BEING_DEFINED (type) = 1;
8709 return error_mark_node;
8710 }
8711 else if (t)
8712 {
8713 /* This TYPE is actually an instantiation of a partial
8714 specialization. We replace the innermost set of ARGS with
8715 the arguments appropriate for substitution. For example,
8716 given:
8717
8718 template <class T> struct S {};
8719 template <class T> struct S<T*> {};
8720
8721 and supposing that we are instantiating S<int*>, ARGS will
8722 presently be {int*} -- but we need {int}. */
8723 pattern = TREE_TYPE (t);
8724 args = TREE_PURPOSE (t);
8725 }
8726 else
8727 {
8728 pattern = TREE_TYPE (templ);
8729 args = CLASSTYPE_TI_ARGS (type);
8730 }
8731
8732 /* If the template we're instantiating is incomplete, then clearly
8733 there's nothing we can do. */
8734 if (!COMPLETE_TYPE_P (pattern))
8735 return type;
8736
8737 /* If we've recursively instantiated too many templates, stop. */
8738 if (! push_tinst_level (type))
8739 return type;
8740
8741 /* Now we're really doing the instantiation. Mark the type as in
8742 the process of being defined. */
8743 TYPE_BEING_DEFINED (type) = 1;
8744
8745 /* We may be in the middle of deferred access check. Disable
8746 it now. */
8747 push_deferring_access_checks (dk_no_deferred);
8748
8749 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8750 if (!fn_context)
8751 push_to_top_level ();
8752 /* Use #pragma pack from the template context. */
8753 saved_maximum_field_alignment = maximum_field_alignment;
8754 maximum_field_alignment = TYPE_PRECISION (pattern);
8755
8756 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8757
8758 /* Set the input location to the most specialized template definition.
8759 This is needed if tsubsting causes an error. */
8760 typedecl = TYPE_MAIN_DECL (pattern);
8761 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8762 DECL_SOURCE_LOCATION (typedecl);
8763
8764 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8765 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8766 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8767 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8768 if (ANON_AGGR_TYPE_P (pattern))
8769 SET_ANON_AGGR_TYPE_P (type);
8770 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8771 {
8772 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8773 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8774 /* Adjust visibility for template arguments. */
8775 determine_visibility (TYPE_MAIN_DECL (type));
8776 }
8777 if (CLASS_TYPE_P (type))
8778 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8779
8780 pbinfo = TYPE_BINFO (pattern);
8781
8782 /* We should never instantiate a nested class before its enclosing
8783 class; we need to look up the nested class by name before we can
8784 instantiate it, and that lookup should instantiate the enclosing
8785 class. */
8786 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8787 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8788
8789 base_list = NULL_TREE;
8790 if (BINFO_N_BASE_BINFOS (pbinfo))
8791 {
8792 tree pbase_binfo;
8793 tree pushed_scope;
8794 int i;
8795
8796 /* We must enter the scope containing the type, as that is where
8797 the accessibility of types named in dependent bases are
8798 looked up from. */
8799 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8800
8801 /* Substitute into each of the bases to determine the actual
8802 basetypes. */
8803 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8804 {
8805 tree base;
8806 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8807 tree expanded_bases = NULL_TREE;
8808 int idx, len = 1;
8809
8810 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8811 {
8812 expanded_bases =
8813 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8814 args, tf_error, NULL_TREE);
8815 if (expanded_bases == error_mark_node)
8816 continue;
8817
8818 len = TREE_VEC_LENGTH (expanded_bases);
8819 }
8820
8821 for (idx = 0; idx < len; idx++)
8822 {
8823 if (expanded_bases)
8824 /* Extract the already-expanded base class. */
8825 base = TREE_VEC_ELT (expanded_bases, idx);
8826 else
8827 /* Substitute to figure out the base class. */
8828 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8829 NULL_TREE);
8830
8831 if (base == error_mark_node)
8832 continue;
8833
8834 base_list = tree_cons (access, base, base_list);
8835 if (BINFO_VIRTUAL_P (pbase_binfo))
8836 TREE_TYPE (base_list) = integer_type_node;
8837 }
8838 }
8839
8840 /* The list is now in reverse order; correct that. */
8841 base_list = nreverse (base_list);
8842
8843 if (pushed_scope)
8844 pop_scope (pushed_scope);
8845 }
8846 /* Now call xref_basetypes to set up all the base-class
8847 information. */
8848 xref_basetypes (type, base_list);
8849
8850 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8851 (int) ATTR_FLAG_TYPE_IN_PLACE,
8852 args, tf_error, NULL_TREE);
8853 fixup_attribute_variants (type);
8854
8855 /* Now that our base classes are set up, enter the scope of the
8856 class, so that name lookups into base classes, etc. will work
8857 correctly. This is precisely analogous to what we do in
8858 begin_class_definition when defining an ordinary non-template
8859 class, except we also need to push the enclosing classes. */
8860 push_nested_class (type);
8861
8862 /* Now members are processed in the order of declaration. */
8863 for (member = CLASSTYPE_DECL_LIST (pattern);
8864 member; member = TREE_CHAIN (member))
8865 {
8866 tree t = TREE_VALUE (member);
8867
8868 if (TREE_PURPOSE (member))
8869 {
8870 if (TYPE_P (t))
8871 {
8872 /* Build new CLASSTYPE_NESTED_UTDS. */
8873
8874 tree newtag;
8875 bool class_template_p;
8876
8877 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8878 && TYPE_LANG_SPECIFIC (t)
8879 && CLASSTYPE_IS_TEMPLATE (t));
8880 /* If the member is a class template, then -- even after
8881 substitution -- there may be dependent types in the
8882 template argument list for the class. We increment
8883 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8884 that function will assume that no types are dependent
8885 when outside of a template. */
8886 if (class_template_p)
8887 ++processing_template_decl;
8888 newtag = tsubst (t, args, tf_error, NULL_TREE);
8889 if (class_template_p)
8890 --processing_template_decl;
8891 if (newtag == error_mark_node)
8892 continue;
8893
8894 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8895 {
8896 tree name = TYPE_IDENTIFIER (t);
8897
8898 if (class_template_p)
8899 /* Unfortunately, lookup_template_class sets
8900 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8901 instantiation (i.e., for the type of a member
8902 template class nested within a template class.)
8903 This behavior is required for
8904 maybe_process_partial_specialization to work
8905 correctly, but is not accurate in this case;
8906 the TAG is not an instantiation of anything.
8907 (The corresponding TEMPLATE_DECL is an
8908 instantiation, but the TYPE is not.) */
8909 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8910
8911 /* Now, we call pushtag to put this NEWTAG into the scope of
8912 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8913 pushtag calling push_template_decl. We don't have to do
8914 this for enums because it will already have been done in
8915 tsubst_enum. */
8916 if (name)
8917 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8918 pushtag (name, newtag, /*tag_scope=*/ts_current);
8919 }
8920 }
8921 else if (DECL_DECLARES_FUNCTION_P (t))
8922 {
8923 /* Build new TYPE_METHODS. */
8924 tree r;
8925
8926 if (TREE_CODE (t) == TEMPLATE_DECL)
8927 ++processing_template_decl;
8928 r = tsubst (t, args, tf_error, NULL_TREE);
8929 if (TREE_CODE (t) == TEMPLATE_DECL)
8930 --processing_template_decl;
8931 set_current_access_from_decl (r);
8932 finish_member_declaration (r);
8933 /* Instantiate members marked with attribute used. */
8934 if (r != error_mark_node && DECL_PRESERVE_P (r))
8935 mark_used (r);
8936 if (TREE_CODE (r) == FUNCTION_DECL
8937 && DECL_OMP_DECLARE_REDUCTION_P (r))
8938 cp_check_omp_declare_reduction (r);
8939 }
8940 else
8941 {
8942 /* Build new TYPE_FIELDS. */
8943 if (TREE_CODE (t) == STATIC_ASSERT)
8944 {
8945 tree condition;
8946
8947 ++c_inhibit_evaluation_warnings;
8948 condition =
8949 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8950 tf_warning_or_error, NULL_TREE,
8951 /*integral_constant_expression_p=*/true);
8952 --c_inhibit_evaluation_warnings;
8953
8954 finish_static_assert (condition,
8955 STATIC_ASSERT_MESSAGE (t),
8956 STATIC_ASSERT_SOURCE_LOCATION (t),
8957 /*member_p=*/true);
8958 }
8959 else if (TREE_CODE (t) != CONST_DECL)
8960 {
8961 tree r;
8962 tree vec = NULL_TREE;
8963 int len = 1;
8964
8965 /* The file and line for this declaration, to
8966 assist in error message reporting. Since we
8967 called push_tinst_level above, we don't need to
8968 restore these. */
8969 input_location = DECL_SOURCE_LOCATION (t);
8970
8971 if (TREE_CODE (t) == TEMPLATE_DECL)
8972 ++processing_template_decl;
8973 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8974 if (TREE_CODE (t) == TEMPLATE_DECL)
8975 --processing_template_decl;
8976
8977 if (TREE_CODE (r) == TREE_VEC)
8978 {
8979 /* A capture pack became multiple fields. */
8980 vec = r;
8981 len = TREE_VEC_LENGTH (vec);
8982 }
8983
8984 for (int i = 0; i < len; ++i)
8985 {
8986 if (vec)
8987 r = TREE_VEC_ELT (vec, i);
8988 if (VAR_P (r))
8989 {
8990 /* In [temp.inst]:
8991
8992 [t]he initialization (and any associated
8993 side-effects) of a static data member does
8994 not occur unless the static data member is
8995 itself used in a way that requires the
8996 definition of the static data member to
8997 exist.
8998
8999 Therefore, we do not substitute into the
9000 initialized for the static data member here. */
9001 finish_static_data_member_decl
9002 (r,
9003 /*init=*/NULL_TREE,
9004 /*init_const_expr_p=*/false,
9005 /*asmspec_tree=*/NULL_TREE,
9006 /*flags=*/0);
9007 /* Instantiate members marked with attribute used. */
9008 if (r != error_mark_node && DECL_PRESERVE_P (r))
9009 mark_used (r);
9010 }
9011 else if (TREE_CODE (r) == FIELD_DECL)
9012 {
9013 /* Determine whether R has a valid type and can be
9014 completed later. If R is invalid, then its type
9015 is replaced by error_mark_node. */
9016 tree rtype = TREE_TYPE (r);
9017 if (can_complete_type_without_circularity (rtype))
9018 complete_type (rtype);
9019
9020 if (!COMPLETE_TYPE_P (rtype))
9021 {
9022 cxx_incomplete_type_error (r, rtype);
9023 TREE_TYPE (r) = error_mark_node;
9024 }
9025 }
9026
9027 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9028 such a thing will already have been added to the field
9029 list by tsubst_enum in finish_member_declaration in the
9030 CLASSTYPE_NESTED_UTDS case above. */
9031 if (!(TREE_CODE (r) == TYPE_DECL
9032 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9033 && DECL_ARTIFICIAL (r)))
9034 {
9035 set_current_access_from_decl (r);
9036 finish_member_declaration (r);
9037 }
9038 }
9039 }
9040 }
9041 }
9042 else
9043 {
9044 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9045 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9046 {
9047 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9048
9049 tree friend_type = t;
9050 bool adjust_processing_template_decl = false;
9051
9052 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9053 {
9054 /* template <class T> friend class C; */
9055 friend_type = tsubst_friend_class (friend_type, args);
9056 adjust_processing_template_decl = true;
9057 }
9058 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9059 {
9060 /* template <class T> friend class C::D; */
9061 friend_type = tsubst (friend_type, args,
9062 tf_warning_or_error, NULL_TREE);
9063 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9064 friend_type = TREE_TYPE (friend_type);
9065 adjust_processing_template_decl = true;
9066 }
9067 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9068 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9069 {
9070 /* This could be either
9071
9072 friend class T::C;
9073
9074 when dependent_type_p is false or
9075
9076 template <class U> friend class T::C;
9077
9078 otherwise. */
9079 friend_type = tsubst (friend_type, args,
9080 tf_warning_or_error, NULL_TREE);
9081 /* Bump processing_template_decl for correct
9082 dependent_type_p calculation. */
9083 ++processing_template_decl;
9084 if (dependent_type_p (friend_type))
9085 adjust_processing_template_decl = true;
9086 --processing_template_decl;
9087 }
9088 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9089 && hidden_name_p (TYPE_NAME (friend_type)))
9090 {
9091 /* friend class C;
9092
9093 where C hasn't been declared yet. Let's lookup name
9094 from namespace scope directly, bypassing any name that
9095 come from dependent base class. */
9096 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9097
9098 /* The call to xref_tag_from_type does injection for friend
9099 classes. */
9100 push_nested_namespace (ns);
9101 friend_type =
9102 xref_tag_from_type (friend_type, NULL_TREE,
9103 /*tag_scope=*/ts_current);
9104 pop_nested_namespace (ns);
9105 }
9106 else if (uses_template_parms (friend_type))
9107 /* friend class C<T>; */
9108 friend_type = tsubst (friend_type, args,
9109 tf_warning_or_error, NULL_TREE);
9110 /* Otherwise it's
9111
9112 friend class C;
9113
9114 where C is already declared or
9115
9116 friend class C<int>;
9117
9118 We don't have to do anything in these cases. */
9119
9120 if (adjust_processing_template_decl)
9121 /* Trick make_friend_class into realizing that the friend
9122 we're adding is a template, not an ordinary class. It's
9123 important that we use make_friend_class since it will
9124 perform some error-checking and output cross-reference
9125 information. */
9126 ++processing_template_decl;
9127
9128 if (friend_type != error_mark_node)
9129 make_friend_class (type, friend_type, /*complain=*/false);
9130
9131 if (adjust_processing_template_decl)
9132 --processing_template_decl;
9133 }
9134 else
9135 {
9136 /* Build new DECL_FRIENDLIST. */
9137 tree r;
9138
9139 /* The file and line for this declaration, to
9140 assist in error message reporting. Since we
9141 called push_tinst_level above, we don't need to
9142 restore these. */
9143 input_location = DECL_SOURCE_LOCATION (t);
9144
9145 if (TREE_CODE (t) == TEMPLATE_DECL)
9146 {
9147 ++processing_template_decl;
9148 push_deferring_access_checks (dk_no_check);
9149 }
9150
9151 r = tsubst_friend_function (t, args);
9152 add_friend (type, r, /*complain=*/false);
9153 if (TREE_CODE (t) == TEMPLATE_DECL)
9154 {
9155 pop_deferring_access_checks ();
9156 --processing_template_decl;
9157 }
9158 }
9159 }
9160 }
9161
9162 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9163 {
9164 tree decl = lambda_function (type);
9165 if (decl)
9166 {
9167 if (!DECL_TEMPLATE_INFO (decl)
9168 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9169 instantiate_decl (decl, false, false);
9170
9171 /* We need to instantiate the capture list from the template
9172 after we've instantiated the closure members, but before we
9173 consider adding the conversion op. Also keep any captures
9174 that may have been added during instantiation of the op(). */
9175 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9176 tree tmpl_cap
9177 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9178 args, tf_warning_or_error, NULL_TREE,
9179 false, false);
9180
9181 LAMBDA_EXPR_CAPTURE_LIST (expr)
9182 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9183
9184 maybe_add_lambda_conv_op (type);
9185 }
9186 else
9187 gcc_assert (errorcount);
9188 }
9189
9190 /* Set the file and line number information to whatever is given for
9191 the class itself. This puts error messages involving generated
9192 implicit functions at a predictable point, and the same point
9193 that would be used for non-template classes. */
9194 input_location = DECL_SOURCE_LOCATION (typedecl);
9195
9196 unreverse_member_declarations (type);
9197 finish_struct_1 (type);
9198 TYPE_BEING_DEFINED (type) = 0;
9199
9200 /* We don't instantiate default arguments for member functions. 14.7.1:
9201
9202 The implicit instantiation of a class template specialization causes
9203 the implicit instantiation of the declarations, but not of the
9204 definitions or default arguments, of the class member functions,
9205 member classes, static data members and member templates.... */
9206
9207 /* Some typedefs referenced from within the template code need to be access
9208 checked at template instantiation time, i.e now. These types were
9209 added to the template at parsing time. Let's get those and perform
9210 the access checks then. */
9211 perform_typedefs_access_check (pattern, args);
9212 perform_deferred_access_checks (tf_warning_or_error);
9213 pop_nested_class ();
9214 maximum_field_alignment = saved_maximum_field_alignment;
9215 if (!fn_context)
9216 pop_from_top_level ();
9217 pop_deferring_access_checks ();
9218 pop_tinst_level ();
9219
9220 /* The vtable for a template class can be emitted in any translation
9221 unit in which the class is instantiated. When there is no key
9222 method, however, finish_struct_1 will already have added TYPE to
9223 the keyed_classes list. */
9224 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9225 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9226
9227 return type;
9228 }
9229
9230 /* Wrapper for instantiate_class_template_1. */
9231
9232 tree
9233 instantiate_class_template (tree type)
9234 {
9235 tree ret;
9236 timevar_push (TV_TEMPLATE_INST);
9237 ret = instantiate_class_template_1 (type);
9238 timevar_pop (TV_TEMPLATE_INST);
9239 return ret;
9240 }
9241
9242 static tree
9243 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9244 {
9245 tree r;
9246
9247 if (!t)
9248 r = t;
9249 else if (TYPE_P (t))
9250 r = tsubst (t, args, complain, in_decl);
9251 else
9252 {
9253 if (!(complain & tf_warning))
9254 ++c_inhibit_evaluation_warnings;
9255 r = tsubst_expr (t, args, complain, in_decl,
9256 /*integral_constant_expression_p=*/true);
9257 if (!(complain & tf_warning))
9258 --c_inhibit_evaluation_warnings;
9259 /* Preserve the raw-reference nature of T. */
9260 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9261 && REFERENCE_REF_P (r))
9262 r = TREE_OPERAND (r, 0);
9263 }
9264 return r;
9265 }
9266
9267 /* Given a function parameter pack TMPL_PARM and some function parameters
9268 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9269 and set *SPEC_P to point at the next point in the list. */
9270
9271 static tree
9272 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9273 {
9274 /* Collect all of the extra "packed" parameters into an
9275 argument pack. */
9276 tree parmvec;
9277 tree parmtypevec;
9278 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9279 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9280 tree spec_parm = *spec_p;
9281 int i, len;
9282
9283 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9284 if (tmpl_parm
9285 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9286 break;
9287
9288 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9289 parmvec = make_tree_vec (len);
9290 parmtypevec = make_tree_vec (len);
9291 spec_parm = *spec_p;
9292 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9293 {
9294 TREE_VEC_ELT (parmvec, i) = spec_parm;
9295 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9296 }
9297
9298 /* Build the argument packs. */
9299 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9300 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9301 TREE_TYPE (argpack) = argtypepack;
9302 *spec_p = spec_parm;
9303
9304 return argpack;
9305 }
9306
9307 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9308 NONTYPE_ARGUMENT_PACK. */
9309
9310 static tree
9311 make_fnparm_pack (tree spec_parm)
9312 {
9313 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9314 }
9315
9316 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9317 pack expansion. */
9318
9319 static bool
9320 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9321 {
9322 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9323 if (i >= TREE_VEC_LENGTH (vec))
9324 return false;
9325 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9326 }
9327
9328
9329 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9330
9331 static tree
9332 make_argument_pack_select (tree arg_pack, unsigned index)
9333 {
9334 tree aps = make_node (ARGUMENT_PACK_SELECT);
9335
9336 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9337 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9338
9339 return aps;
9340 }
9341
9342 /* This is a subroutine of tsubst_pack_expansion.
9343
9344 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9345 mechanism to store the (non complete list of) arguments of the
9346 substitution and return a non substituted pack expansion, in order
9347 to wait for when we have enough arguments to really perform the
9348 substitution. */
9349
9350 static bool
9351 use_pack_expansion_extra_args_p (tree parm_packs,
9352 int arg_pack_len,
9353 bool has_empty_arg)
9354 {
9355 /* If one pack has an expansion and another pack has a normal
9356 argument or if one pack has an empty argument and an another
9357 one hasn't then tsubst_pack_expansion cannot perform the
9358 substitution and need to fall back on the
9359 PACK_EXPANSION_EXTRA mechanism. */
9360 if (parm_packs == NULL_TREE)
9361 return false;
9362 else if (has_empty_arg)
9363 return true;
9364
9365 bool has_expansion_arg = false;
9366 for (int i = 0 ; i < arg_pack_len; ++i)
9367 {
9368 bool has_non_expansion_arg = false;
9369 for (tree parm_pack = parm_packs;
9370 parm_pack;
9371 parm_pack = TREE_CHAIN (parm_pack))
9372 {
9373 tree arg = TREE_VALUE (parm_pack);
9374
9375 if (argument_pack_element_is_expansion_p (arg, i))
9376 has_expansion_arg = true;
9377 else
9378 has_non_expansion_arg = true;
9379 }
9380
9381 if (has_expansion_arg && has_non_expansion_arg)
9382 return true;
9383 }
9384 return false;
9385 }
9386
9387 /* [temp.variadic]/6 says that:
9388
9389 The instantiation of a pack expansion [...]
9390 produces a list E1,E2, ..., En, where N is the number of elements
9391 in the pack expansion parameters.
9392
9393 This subroutine of tsubst_pack_expansion produces one of these Ei.
9394
9395 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9396 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9397 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9398 INDEX is the index 'i' of the element Ei to produce. ARGS,
9399 COMPLAIN, and IN_DECL are the same parameters as for the
9400 tsubst_pack_expansion function.
9401
9402 The function returns the resulting Ei upon successful completion,
9403 or error_mark_node.
9404
9405 Note that this function possibly modifies the ARGS parameter, so
9406 it's the responsibility of the caller to restore it. */
9407
9408 static tree
9409 gen_elem_of_pack_expansion_instantiation (tree pattern,
9410 tree parm_packs,
9411 unsigned index,
9412 tree args /* This parm gets
9413 modified. */,
9414 tsubst_flags_t complain,
9415 tree in_decl)
9416 {
9417 tree t;
9418 bool ith_elem_is_expansion = false;
9419
9420 /* For each parameter pack, change the substitution of the parameter
9421 pack to the ith argument in its argument pack, then expand the
9422 pattern. */
9423 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9424 {
9425 tree parm = TREE_PURPOSE (pack);
9426 tree arg_pack = TREE_VALUE (pack);
9427 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9428
9429 ith_elem_is_expansion |=
9430 argument_pack_element_is_expansion_p (arg_pack, index);
9431
9432 /* Select the Ith argument from the pack. */
9433 if (TREE_CODE (parm) == PARM_DECL
9434 || TREE_CODE (parm) == FIELD_DECL)
9435 {
9436 if (index == 0)
9437 {
9438 aps = make_argument_pack_select (arg_pack, index);
9439 mark_used (parm);
9440 register_local_specialization (aps, parm);
9441 }
9442 else
9443 aps = retrieve_local_specialization (parm);
9444 }
9445 else
9446 {
9447 int idx, level;
9448 template_parm_level_and_index (parm, &level, &idx);
9449
9450 if (index == 0)
9451 {
9452 aps = make_argument_pack_select (arg_pack, index);
9453 /* Update the corresponding argument. */
9454 TMPL_ARG (args, level, idx) = aps;
9455 }
9456 else
9457 /* Re-use the ARGUMENT_PACK_SELECT. */
9458 aps = TMPL_ARG (args, level, idx);
9459 }
9460 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9461 }
9462
9463 /* Substitute into the PATTERN with the (possibly altered)
9464 arguments. */
9465 if (!TYPE_P (pattern))
9466 t = tsubst_expr (pattern, args, complain, in_decl,
9467 /*integral_constant_expression_p=*/false);
9468 else
9469 t = tsubst (pattern, args, complain, in_decl);
9470
9471 /* If the Ith argument pack element is a pack expansion, then
9472 the Ith element resulting from the substituting is going to
9473 be a pack expansion as well. */
9474 if (ith_elem_is_expansion)
9475 t = make_pack_expansion (t);
9476
9477 return t;
9478 }
9479
9480 /* Substitute ARGS into T, which is an pack expansion
9481 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9482 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9483 (if only a partial substitution could be performed) or
9484 ERROR_MARK_NODE if there was an error. */
9485 tree
9486 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9487 tree in_decl)
9488 {
9489 tree pattern;
9490 tree pack, packs = NULL_TREE;
9491 bool unsubstituted_packs = false;
9492 int i, len = -1;
9493 tree result;
9494 struct pointer_map_t *saved_local_specializations = NULL;
9495 bool need_local_specializations = false;
9496 int levels;
9497
9498 gcc_assert (PACK_EXPANSION_P (t));
9499 pattern = PACK_EXPANSION_PATTERN (t);
9500
9501 /* Add in any args remembered from an earlier partial instantiation. */
9502 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9503
9504 levels = TMPL_ARGS_DEPTH (args);
9505
9506 /* Determine the argument packs that will instantiate the parameter
9507 packs used in the expansion expression. While we're at it,
9508 compute the number of arguments to be expanded and make sure it
9509 is consistent. */
9510 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9511 pack = TREE_CHAIN (pack))
9512 {
9513 tree parm_pack = TREE_VALUE (pack);
9514 tree arg_pack = NULL_TREE;
9515 tree orig_arg = NULL_TREE;
9516 int level = 0;
9517
9518 if (TREE_CODE (parm_pack) == BASES)
9519 {
9520 if (BASES_DIRECT (parm_pack))
9521 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9522 args, complain, in_decl, false));
9523 else
9524 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9525 args, complain, in_decl, false));
9526 }
9527 if (TREE_CODE (parm_pack) == PARM_DECL)
9528 {
9529 if (PACK_EXPANSION_LOCAL_P (t))
9530 arg_pack = retrieve_local_specialization (parm_pack);
9531 else
9532 {
9533 /* We can't rely on local_specializations for a parameter
9534 name used later in a function declaration (such as in a
9535 late-specified return type). Even if it exists, it might
9536 have the wrong value for a recursive call. Just make a
9537 dummy decl, since it's only used for its type. */
9538 arg_pack = tsubst_decl (parm_pack, args, complain);
9539 if (arg_pack && DECL_PACK_P (arg_pack))
9540 /* Partial instantiation of the parm_pack, we can't build
9541 up an argument pack yet. */
9542 arg_pack = NULL_TREE;
9543 else
9544 arg_pack = make_fnparm_pack (arg_pack);
9545 need_local_specializations = true;
9546 }
9547 }
9548 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9549 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9550 else
9551 {
9552 int idx;
9553 template_parm_level_and_index (parm_pack, &level, &idx);
9554
9555 if (level <= levels)
9556 arg_pack = TMPL_ARG (args, level, idx);
9557 }
9558
9559 orig_arg = arg_pack;
9560 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9561 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9562
9563 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9564 /* This can only happen if we forget to expand an argument
9565 pack somewhere else. Just return an error, silently. */
9566 {
9567 result = make_tree_vec (1);
9568 TREE_VEC_ELT (result, 0) = error_mark_node;
9569 return result;
9570 }
9571
9572 if (arg_pack)
9573 {
9574 int my_len =
9575 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9576
9577 /* Don't bother trying to do a partial substitution with
9578 incomplete packs; we'll try again after deduction. */
9579 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9580 return t;
9581
9582 if (len < 0)
9583 len = my_len;
9584 else if (len != my_len)
9585 {
9586 if (!(complain & tf_error))
9587 /* Fail quietly. */;
9588 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9589 error ("mismatched argument pack lengths while expanding "
9590 "%<%T%>",
9591 pattern);
9592 else
9593 error ("mismatched argument pack lengths while expanding "
9594 "%<%E%>",
9595 pattern);
9596 return error_mark_node;
9597 }
9598
9599 /* Keep track of the parameter packs and their corresponding
9600 argument packs. */
9601 packs = tree_cons (parm_pack, arg_pack, packs);
9602 TREE_TYPE (packs) = orig_arg;
9603 }
9604 else
9605 {
9606 /* We can't substitute for this parameter pack. We use a flag as
9607 well as the missing_level counter because function parameter
9608 packs don't have a level. */
9609 unsubstituted_packs = true;
9610 }
9611 }
9612
9613 /* We cannot expand this expansion expression, because we don't have
9614 all of the argument packs we need. */
9615 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9616 {
9617 /* We got some full packs, but we can't substitute them in until we
9618 have values for all the packs. So remember these until then. */
9619
9620 t = make_pack_expansion (pattern);
9621 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9622 return t;
9623 }
9624 else if (unsubstituted_packs)
9625 {
9626 /* There were no real arguments, we're just replacing a parameter
9627 pack with another version of itself. Substitute into the
9628 pattern and return a PACK_EXPANSION_*. The caller will need to
9629 deal with that. */
9630 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
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 t = make_pack_expansion (t);
9636 return t;
9637 }
9638
9639 gcc_assert (len >= 0);
9640
9641 if (need_local_specializations)
9642 {
9643 /* We're in a late-specified return type, so create our own local
9644 specializations map; the current map is either NULL or (in the
9645 case of recursive unification) might have bindings that we don't
9646 want to use or alter. */
9647 saved_local_specializations = local_specializations;
9648 local_specializations = pointer_map_create ();
9649 }
9650
9651 /* For each argument in each argument pack, substitute into the
9652 pattern. */
9653 result = make_tree_vec (len);
9654 for (i = 0; i < len; ++i)
9655 {
9656 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9657 i,
9658 args, complain,
9659 in_decl);
9660 TREE_VEC_ELT (result, i) = t;
9661 if (t == error_mark_node)
9662 {
9663 result = error_mark_node;
9664 break;
9665 }
9666 }
9667
9668 /* Update ARGS to restore the substitution from parameter packs to
9669 their argument packs. */
9670 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9671 {
9672 tree parm = TREE_PURPOSE (pack);
9673
9674 if (TREE_CODE (parm) == PARM_DECL
9675 || TREE_CODE (parm) == FIELD_DECL)
9676 register_local_specialization (TREE_TYPE (pack), parm);
9677 else
9678 {
9679 int idx, level;
9680
9681 if (TREE_VALUE (pack) == NULL_TREE)
9682 continue;
9683
9684 template_parm_level_and_index (parm, &level, &idx);
9685
9686 /* Update the corresponding argument. */
9687 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9688 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9689 TREE_TYPE (pack);
9690 else
9691 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9692 }
9693 }
9694
9695 if (need_local_specializations)
9696 {
9697 pointer_map_destroy (local_specializations);
9698 local_specializations = saved_local_specializations;
9699 }
9700
9701 return result;
9702 }
9703
9704 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9705 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9706 parameter packs; all parms generated from a function parameter pack will
9707 have the same DECL_PARM_INDEX. */
9708
9709 tree
9710 get_pattern_parm (tree parm, tree tmpl)
9711 {
9712 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9713 tree patparm;
9714
9715 if (DECL_ARTIFICIAL (parm))
9716 {
9717 for (patparm = DECL_ARGUMENTS (pattern);
9718 patparm; patparm = DECL_CHAIN (patparm))
9719 if (DECL_ARTIFICIAL (patparm)
9720 && DECL_NAME (parm) == DECL_NAME (patparm))
9721 break;
9722 }
9723 else
9724 {
9725 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9726 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9727 gcc_assert (DECL_PARM_INDEX (patparm)
9728 == DECL_PARM_INDEX (parm));
9729 }
9730
9731 return patparm;
9732 }
9733
9734 /* Substitute ARGS into the vector or list of template arguments T. */
9735
9736 static tree
9737 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9738 {
9739 tree orig_t = t;
9740 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9741 tree *elts;
9742
9743 if (t == error_mark_node)
9744 return error_mark_node;
9745
9746 len = TREE_VEC_LENGTH (t);
9747 elts = XALLOCAVEC (tree, len);
9748
9749 for (i = 0; i < len; i++)
9750 {
9751 tree orig_arg = TREE_VEC_ELT (t, i);
9752 tree new_arg;
9753
9754 if (TREE_CODE (orig_arg) == TREE_VEC)
9755 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9756 else if (PACK_EXPANSION_P (orig_arg))
9757 {
9758 /* Substitute into an expansion expression. */
9759 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9760
9761 if (TREE_CODE (new_arg) == TREE_VEC)
9762 /* Add to the expanded length adjustment the number of
9763 expanded arguments. We subtract one from this
9764 measurement, because the argument pack expression
9765 itself is already counted as 1 in
9766 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9767 the argument pack is empty. */
9768 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9769 }
9770 else if (ARGUMENT_PACK_P (orig_arg))
9771 {
9772 /* Substitute into each of the arguments. */
9773 new_arg = TYPE_P (orig_arg)
9774 ? cxx_make_type (TREE_CODE (orig_arg))
9775 : make_node (TREE_CODE (orig_arg));
9776
9777 SET_ARGUMENT_PACK_ARGS (
9778 new_arg,
9779 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9780 args, complain, in_decl));
9781
9782 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9783 new_arg = error_mark_node;
9784
9785 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9786 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9787 complain, in_decl);
9788 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9789
9790 if (TREE_TYPE (new_arg) == error_mark_node)
9791 new_arg = error_mark_node;
9792 }
9793 }
9794 else
9795 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9796
9797 if (new_arg == error_mark_node)
9798 return error_mark_node;
9799
9800 elts[i] = new_arg;
9801 if (new_arg != orig_arg)
9802 need_new = 1;
9803 }
9804
9805 if (!need_new)
9806 return t;
9807
9808 /* Make space for the expanded arguments coming from template
9809 argument packs. */
9810 t = make_tree_vec (len + expanded_len_adjust);
9811 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9812 arguments for a member template.
9813 In that case each TREE_VEC in ORIG_T represents a level of template
9814 arguments, and ORIG_T won't carry any non defaulted argument count.
9815 It will rather be the nested TREE_VECs that will carry one.
9816 In other words, ORIG_T carries a non defaulted argument count only
9817 if it doesn't contain any nested TREE_VEC. */
9818 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9819 {
9820 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9821 count += expanded_len_adjust;
9822 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9823 }
9824 for (i = 0, out = 0; i < len; i++)
9825 {
9826 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9827 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9828 && TREE_CODE (elts[i]) == TREE_VEC)
9829 {
9830 int idx;
9831
9832 /* Now expand the template argument pack "in place". */
9833 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9834 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9835 }
9836 else
9837 {
9838 TREE_VEC_ELT (t, out) = elts[i];
9839 out++;
9840 }
9841 }
9842
9843 return t;
9844 }
9845
9846 /* Return the result of substituting ARGS into the template parameters
9847 given by PARMS. If there are m levels of ARGS and m + n levels of
9848 PARMS, then the result will contain n levels of PARMS. For
9849 example, if PARMS is `template <class T> template <class U>
9850 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9851 result will be `template <int*, double, class V>'. */
9852
9853 static tree
9854 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9855 {
9856 tree r = NULL_TREE;
9857 tree* new_parms;
9858
9859 /* When substituting into a template, we must set
9860 PROCESSING_TEMPLATE_DECL as the template parameters may be
9861 dependent if they are based on one-another, and the dependency
9862 predicates are short-circuit outside of templates. */
9863 ++processing_template_decl;
9864
9865 for (new_parms = &r;
9866 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9867 new_parms = &(TREE_CHAIN (*new_parms)),
9868 parms = TREE_CHAIN (parms))
9869 {
9870 tree new_vec =
9871 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9872 int i;
9873
9874 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9875 {
9876 tree tuple;
9877
9878 if (parms == error_mark_node)
9879 continue;
9880
9881 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9882
9883 if (tuple == error_mark_node)
9884 continue;
9885
9886 TREE_VEC_ELT (new_vec, i) =
9887 tsubst_template_parm (tuple, args, complain);
9888 }
9889
9890 *new_parms =
9891 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9892 - TMPL_ARGS_DEPTH (args)),
9893 new_vec, NULL_TREE);
9894 }
9895
9896 --processing_template_decl;
9897
9898 return r;
9899 }
9900
9901 /* Return the result of substituting ARGS into one template parameter
9902 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9903 parameter and which TREE_PURPOSE is the default argument of the
9904 template parameter. */
9905
9906 static tree
9907 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9908 {
9909 tree default_value, parm_decl;
9910
9911 if (args == NULL_TREE
9912 || t == NULL_TREE
9913 || t == error_mark_node)
9914 return t;
9915
9916 gcc_assert (TREE_CODE (t) == TREE_LIST);
9917
9918 default_value = TREE_PURPOSE (t);
9919 parm_decl = TREE_VALUE (t);
9920
9921 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9922 if (TREE_CODE (parm_decl) == PARM_DECL
9923 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9924 parm_decl = error_mark_node;
9925 default_value = tsubst_template_arg (default_value, args,
9926 complain, NULL_TREE);
9927
9928 return build_tree_list (default_value, parm_decl);
9929 }
9930
9931 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9932 type T. If T is not an aggregate or enumeration type, it is
9933 handled as if by tsubst. IN_DECL is as for tsubst. If
9934 ENTERING_SCOPE is nonzero, T is the context for a template which
9935 we are presently tsubst'ing. Return the substituted value. */
9936
9937 static tree
9938 tsubst_aggr_type (tree t,
9939 tree args,
9940 tsubst_flags_t complain,
9941 tree in_decl,
9942 int entering_scope)
9943 {
9944 if (t == NULL_TREE)
9945 return NULL_TREE;
9946
9947 switch (TREE_CODE (t))
9948 {
9949 case RECORD_TYPE:
9950 if (TYPE_PTRMEMFUNC_P (t))
9951 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9952
9953 /* Else fall through. */
9954 case ENUMERAL_TYPE:
9955 case UNION_TYPE:
9956 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9957 {
9958 tree argvec;
9959 tree context;
9960 tree r;
9961 int saved_unevaluated_operand;
9962 int saved_inhibit_evaluation_warnings;
9963
9964 /* In "sizeof(X<I>)" we need to evaluate "I". */
9965 saved_unevaluated_operand = cp_unevaluated_operand;
9966 cp_unevaluated_operand = 0;
9967 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9968 c_inhibit_evaluation_warnings = 0;
9969
9970 /* First, determine the context for the type we are looking
9971 up. */
9972 context = TYPE_CONTEXT (t);
9973 if (context && TYPE_P (context))
9974 {
9975 context = tsubst_aggr_type (context, args, complain,
9976 in_decl, /*entering_scope=*/1);
9977 /* If context is a nested class inside a class template,
9978 it may still need to be instantiated (c++/33959). */
9979 context = complete_type (context);
9980 }
9981
9982 /* Then, figure out what arguments are appropriate for the
9983 type we are trying to find. For example, given:
9984
9985 template <class T> struct S;
9986 template <class T, class U> void f(T, U) { S<U> su; }
9987
9988 and supposing that we are instantiating f<int, double>,
9989 then our ARGS will be {int, double}, but, when looking up
9990 S we only want {double}. */
9991 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9992 complain, in_decl);
9993 if (argvec == error_mark_node)
9994 r = error_mark_node;
9995 else
9996 {
9997 r = lookup_template_class (t, argvec, in_decl, context,
9998 entering_scope, complain);
9999 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10000 }
10001
10002 cp_unevaluated_operand = saved_unevaluated_operand;
10003 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10004
10005 return r;
10006 }
10007 else
10008 /* This is not a template type, so there's nothing to do. */
10009 return t;
10010
10011 default:
10012 return tsubst (t, args, complain, in_decl);
10013 }
10014 }
10015
10016 /* Substitute into the default argument ARG (a default argument for
10017 FN), which has the indicated TYPE. */
10018
10019 tree
10020 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10021 {
10022 tree saved_class_ptr = NULL_TREE;
10023 tree saved_class_ref = NULL_TREE;
10024 int errs = errorcount + sorrycount;
10025
10026 /* This can happen in invalid code. */
10027 if (TREE_CODE (arg) == DEFAULT_ARG)
10028 return arg;
10029
10030 /* This default argument came from a template. Instantiate the
10031 default argument here, not in tsubst. In the case of
10032 something like:
10033
10034 template <class T>
10035 struct S {
10036 static T t();
10037 void f(T = t());
10038 };
10039
10040 we must be careful to do name lookup in the scope of S<T>,
10041 rather than in the current class. */
10042 push_access_scope (fn);
10043 /* The "this" pointer is not valid in a default argument. */
10044 if (cfun)
10045 {
10046 saved_class_ptr = current_class_ptr;
10047 cp_function_chain->x_current_class_ptr = NULL_TREE;
10048 saved_class_ref = current_class_ref;
10049 cp_function_chain->x_current_class_ref = NULL_TREE;
10050 }
10051
10052 push_deferring_access_checks(dk_no_deferred);
10053 /* The default argument expression may cause implicitly defined
10054 member functions to be synthesized, which will result in garbage
10055 collection. We must treat this situation as if we were within
10056 the body of function so as to avoid collecting live data on the
10057 stack. */
10058 ++function_depth;
10059 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10060 complain, NULL_TREE,
10061 /*integral_constant_expression_p=*/false);
10062 --function_depth;
10063 pop_deferring_access_checks();
10064
10065 /* Restore the "this" pointer. */
10066 if (cfun)
10067 {
10068 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10069 cp_function_chain->x_current_class_ref = saved_class_ref;
10070 }
10071
10072 if (errorcount+sorrycount > errs
10073 && (complain & tf_warning_or_error))
10074 inform (input_location,
10075 " when instantiating default argument for call to %D", fn);
10076
10077 /* Make sure the default argument is reasonable. */
10078 arg = check_default_argument (type, arg, complain);
10079
10080 pop_access_scope (fn);
10081
10082 return arg;
10083 }
10084
10085 /* Substitute into all the default arguments for FN. */
10086
10087 static void
10088 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10089 {
10090 tree arg;
10091 tree tmpl_args;
10092
10093 tmpl_args = DECL_TI_ARGS (fn);
10094
10095 /* If this function is not yet instantiated, we certainly don't need
10096 its default arguments. */
10097 if (uses_template_parms (tmpl_args))
10098 return;
10099 /* Don't do this again for clones. */
10100 if (DECL_CLONED_FUNCTION_P (fn))
10101 return;
10102
10103 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10104 arg;
10105 arg = TREE_CHAIN (arg))
10106 if (TREE_PURPOSE (arg))
10107 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10108 TREE_VALUE (arg),
10109 TREE_PURPOSE (arg),
10110 complain);
10111 }
10112
10113 /* Substitute the ARGS into the T, which is a _DECL. Return the
10114 result of the substitution. Issue error and warning messages under
10115 control of COMPLAIN. */
10116
10117 static tree
10118 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10119 {
10120 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10121 location_t saved_loc;
10122 tree r = NULL_TREE;
10123 tree in_decl = t;
10124 hashval_t hash = 0;
10125
10126 /* Set the filename and linenumber to improve error-reporting. */
10127 saved_loc = input_location;
10128 input_location = DECL_SOURCE_LOCATION (t);
10129
10130 switch (TREE_CODE (t))
10131 {
10132 case TEMPLATE_DECL:
10133 {
10134 /* We can get here when processing a member function template,
10135 member class template, or template template parameter. */
10136 tree decl = DECL_TEMPLATE_RESULT (t);
10137 tree spec;
10138 tree tmpl_args;
10139 tree full_args;
10140
10141 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10142 {
10143 /* Template template parameter is treated here. */
10144 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10145 if (new_type == error_mark_node)
10146 RETURN (error_mark_node);
10147 /* If we get a real template back, return it. This can happen in
10148 the context of most_specialized_class. */
10149 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10150 return new_type;
10151
10152 r = copy_decl (t);
10153 DECL_CHAIN (r) = NULL_TREE;
10154 TREE_TYPE (r) = new_type;
10155 DECL_TEMPLATE_RESULT (r)
10156 = build_decl (DECL_SOURCE_LOCATION (decl),
10157 TYPE_DECL, DECL_NAME (decl), new_type);
10158 DECL_TEMPLATE_PARMS (r)
10159 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10160 complain);
10161 TYPE_NAME (new_type) = r;
10162 break;
10163 }
10164
10165 /* We might already have an instance of this template.
10166 The ARGS are for the surrounding class type, so the
10167 full args contain the tsubst'd args for the context,
10168 plus the innermost args from the template decl. */
10169 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10170 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10171 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10172 /* Because this is a template, the arguments will still be
10173 dependent, even after substitution. If
10174 PROCESSING_TEMPLATE_DECL is not set, the dependency
10175 predicates will short-circuit. */
10176 ++processing_template_decl;
10177 full_args = tsubst_template_args (tmpl_args, args,
10178 complain, in_decl);
10179 --processing_template_decl;
10180 if (full_args == error_mark_node)
10181 RETURN (error_mark_node);
10182
10183 /* If this is a default template template argument,
10184 tsubst might not have changed anything. */
10185 if (full_args == tmpl_args)
10186 RETURN (t);
10187
10188 hash = hash_tmpl_and_args (t, full_args);
10189 spec = retrieve_specialization (t, full_args, hash);
10190 if (spec != NULL_TREE)
10191 {
10192 r = spec;
10193 break;
10194 }
10195
10196 /* Make a new template decl. It will be similar to the
10197 original, but will record the current template arguments.
10198 We also create a new function declaration, which is just
10199 like the old one, but points to this new template, rather
10200 than the old one. */
10201 r = copy_decl (t);
10202 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10203 DECL_CHAIN (r) = NULL_TREE;
10204
10205 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10206
10207 if (TREE_CODE (decl) == TYPE_DECL
10208 && !TYPE_DECL_ALIAS_P (decl))
10209 {
10210 tree new_type;
10211 ++processing_template_decl;
10212 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10213 --processing_template_decl;
10214 if (new_type == error_mark_node)
10215 RETURN (error_mark_node);
10216
10217 TREE_TYPE (r) = new_type;
10218 /* For a partial specialization, we need to keep pointing to
10219 the primary template. */
10220 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10221 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10222 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10223 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10224 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10225 }
10226 else
10227 {
10228 tree new_decl;
10229 ++processing_template_decl;
10230 new_decl = tsubst (decl, args, complain, in_decl);
10231 --processing_template_decl;
10232 if (new_decl == error_mark_node)
10233 RETURN (error_mark_node);
10234
10235 DECL_TEMPLATE_RESULT (r) = new_decl;
10236 DECL_TI_TEMPLATE (new_decl) = r;
10237 TREE_TYPE (r) = TREE_TYPE (new_decl);
10238 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10239 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10240 }
10241
10242 SET_DECL_IMPLICIT_INSTANTIATION (r);
10243 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10244 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10245
10246 /* The template parameters for this new template are all the
10247 template parameters for the old template, except the
10248 outermost level of parameters. */
10249 DECL_TEMPLATE_PARMS (r)
10250 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10251 complain);
10252
10253 if (PRIMARY_TEMPLATE_P (t))
10254 DECL_PRIMARY_TEMPLATE (r) = r;
10255
10256 if (TREE_CODE (decl) != TYPE_DECL)
10257 /* Record this non-type partial instantiation. */
10258 register_specialization (r, t,
10259 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10260 false, hash);
10261 }
10262 break;
10263
10264 case FUNCTION_DECL:
10265 {
10266 tree ctx;
10267 tree argvec = NULL_TREE;
10268 tree *friends;
10269 tree gen_tmpl;
10270 tree type;
10271 int member;
10272 int args_depth;
10273 int parms_depth;
10274
10275 /* Nobody should be tsubst'ing into non-template functions. */
10276 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10277
10278 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10279 {
10280 tree spec;
10281 bool dependent_p;
10282
10283 /* If T is not dependent, just return it. We have to
10284 increment PROCESSING_TEMPLATE_DECL because
10285 value_dependent_expression_p assumes that nothing is
10286 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10287 ++processing_template_decl;
10288 dependent_p = value_dependent_expression_p (t);
10289 --processing_template_decl;
10290 if (!dependent_p)
10291 RETURN (t);
10292
10293 /* Calculate the most general template of which R is a
10294 specialization, and the complete set of arguments used to
10295 specialize R. */
10296 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10297 argvec = tsubst_template_args (DECL_TI_ARGS
10298 (DECL_TEMPLATE_RESULT
10299 (DECL_TI_TEMPLATE (t))),
10300 args, complain, in_decl);
10301 if (argvec == error_mark_node)
10302 RETURN (error_mark_node);
10303
10304 /* Check to see if we already have this specialization. */
10305 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10306 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10307
10308 if (spec)
10309 {
10310 r = spec;
10311 break;
10312 }
10313
10314 /* We can see more levels of arguments than parameters if
10315 there was a specialization of a member template, like
10316 this:
10317
10318 template <class T> struct S { template <class U> void f(); }
10319 template <> template <class U> void S<int>::f(U);
10320
10321 Here, we'll be substituting into the specialization,
10322 because that's where we can find the code we actually
10323 want to generate, but we'll have enough arguments for
10324 the most general template.
10325
10326 We also deal with the peculiar case:
10327
10328 template <class T> struct S {
10329 template <class U> friend void f();
10330 };
10331 template <class U> void f() {}
10332 template S<int>;
10333 template void f<double>();
10334
10335 Here, the ARGS for the instantiation of will be {int,
10336 double}. But, we only need as many ARGS as there are
10337 levels of template parameters in CODE_PATTERN. We are
10338 careful not to get fooled into reducing the ARGS in
10339 situations like:
10340
10341 template <class T> struct S { template <class U> void f(U); }
10342 template <class T> template <> void S<T>::f(int) {}
10343
10344 which we can spot because the pattern will be a
10345 specialization in this case. */
10346 args_depth = TMPL_ARGS_DEPTH (args);
10347 parms_depth =
10348 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10349 if (args_depth > parms_depth
10350 && !DECL_TEMPLATE_SPECIALIZATION (t))
10351 args = get_innermost_template_args (args, parms_depth);
10352 }
10353 else
10354 {
10355 /* This special case arises when we have something like this:
10356
10357 template <class T> struct S {
10358 friend void f<int>(int, double);
10359 };
10360
10361 Here, the DECL_TI_TEMPLATE for the friend declaration
10362 will be an IDENTIFIER_NODE. We are being called from
10363 tsubst_friend_function, and we want only to create a
10364 new decl (R) with appropriate types so that we can call
10365 determine_specialization. */
10366 gen_tmpl = NULL_TREE;
10367 }
10368
10369 if (DECL_CLASS_SCOPE_P (t))
10370 {
10371 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10372 member = 2;
10373 else
10374 member = 1;
10375 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10376 complain, t, /*entering_scope=*/1);
10377 }
10378 else
10379 {
10380 member = 0;
10381 ctx = DECL_CONTEXT (t);
10382 }
10383 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10384 if (type == error_mark_node)
10385 RETURN (error_mark_node);
10386
10387 /* If we hit excessive deduction depth, the type is bogus even if
10388 it isn't error_mark_node, so don't build a decl. */
10389 if (excessive_deduction_depth)
10390 RETURN (error_mark_node);
10391
10392 /* We do NOT check for matching decls pushed separately at this
10393 point, as they may not represent instantiations of this
10394 template, and in any case are considered separate under the
10395 discrete model. */
10396 r = copy_decl (t);
10397 DECL_USE_TEMPLATE (r) = 0;
10398 TREE_TYPE (r) = type;
10399 /* Clear out the mangled name and RTL for the instantiation. */
10400 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10401 SET_DECL_RTL (r, NULL);
10402 /* Leave DECL_INITIAL set on deleted instantiations. */
10403 if (!DECL_DELETED_FN (r))
10404 DECL_INITIAL (r) = NULL_TREE;
10405 DECL_CONTEXT (r) = ctx;
10406
10407 /* OpenMP UDRs have the only argument a reference to the declared
10408 type. We want to diagnose if the declared type is a reference,
10409 which is invalid, but as references to references are usually
10410 quietly merged, diagnose it here. */
10411 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10412 {
10413 tree argtype
10414 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10415 argtype = tsubst (argtype, args, complain, in_decl);
10416 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10417 error_at (DECL_SOURCE_LOCATION (t),
10418 "reference type %qT in "
10419 "%<#pragma omp declare reduction%>", argtype);
10420 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10421 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10422 argtype);
10423 }
10424
10425 if (member && DECL_CONV_FN_P (r))
10426 /* Type-conversion operator. Reconstruct the name, in
10427 case it's the name of one of the template's parameters. */
10428 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10429
10430 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10431 complain, t);
10432 DECL_RESULT (r) = NULL_TREE;
10433
10434 TREE_STATIC (r) = 0;
10435 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10436 DECL_EXTERNAL (r) = 1;
10437 /* If this is an instantiation of a function with internal
10438 linkage, we already know what object file linkage will be
10439 assigned to the instantiation. */
10440 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10441 DECL_DEFER_OUTPUT (r) = 0;
10442 DECL_CHAIN (r) = NULL_TREE;
10443 DECL_PENDING_INLINE_INFO (r) = 0;
10444 DECL_PENDING_INLINE_P (r) = 0;
10445 DECL_SAVED_TREE (r) = NULL_TREE;
10446 DECL_STRUCT_FUNCTION (r) = NULL;
10447 TREE_USED (r) = 0;
10448 /* We'll re-clone as appropriate in instantiate_template. */
10449 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10450
10451 /* If we aren't complaining now, return on error before we register
10452 the specialization so that we'll complain eventually. */
10453 if ((complain & tf_error) == 0
10454 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10455 && !grok_op_properties (r, /*complain=*/false))
10456 RETURN (error_mark_node);
10457
10458 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10459 this in the special friend case mentioned above where
10460 GEN_TMPL is NULL. */
10461 if (gen_tmpl)
10462 {
10463 DECL_TEMPLATE_INFO (r)
10464 = build_template_info (gen_tmpl, argvec);
10465 SET_DECL_IMPLICIT_INSTANTIATION (r);
10466
10467 tree new_r
10468 = register_specialization (r, gen_tmpl, argvec, false, hash);
10469 if (new_r != r)
10470 /* We instantiated this while substituting into
10471 the type earlier (template/friend54.C). */
10472 RETURN (new_r);
10473
10474 /* We're not supposed to instantiate default arguments
10475 until they are called, for a template. But, for a
10476 declaration like:
10477
10478 template <class T> void f ()
10479 { extern void g(int i = T()); }
10480
10481 we should do the substitution when the template is
10482 instantiated. We handle the member function case in
10483 instantiate_class_template since the default arguments
10484 might refer to other members of the class. */
10485 if (!member
10486 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10487 && !uses_template_parms (argvec))
10488 tsubst_default_arguments (r, complain);
10489 }
10490 else
10491 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10492
10493 /* Copy the list of befriending classes. */
10494 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10495 *friends;
10496 friends = &TREE_CHAIN (*friends))
10497 {
10498 *friends = copy_node (*friends);
10499 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10500 args, complain,
10501 in_decl);
10502 }
10503
10504 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10505 {
10506 maybe_retrofit_in_chrg (r);
10507 if (DECL_CONSTRUCTOR_P (r))
10508 grok_ctor_properties (ctx, r);
10509 if (DECL_INHERITED_CTOR_BASE (r))
10510 deduce_inheriting_ctor (r);
10511 /* If this is an instantiation of a member template, clone it.
10512 If it isn't, that'll be handled by
10513 clone_constructors_and_destructors. */
10514 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10515 clone_function_decl (r, /*update_method_vec_p=*/0);
10516 }
10517 else if ((complain & tf_error) != 0
10518 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10519 && !grok_op_properties (r, /*complain=*/true))
10520 RETURN (error_mark_node);
10521
10522 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10523 SET_DECL_FRIEND_CONTEXT (r,
10524 tsubst (DECL_FRIEND_CONTEXT (t),
10525 args, complain, in_decl));
10526
10527 /* Possibly limit visibility based on template args. */
10528 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10529 if (DECL_VISIBILITY_SPECIFIED (t))
10530 {
10531 DECL_VISIBILITY_SPECIFIED (r) = 0;
10532 DECL_ATTRIBUTES (r)
10533 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10534 }
10535 determine_visibility (r);
10536 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10537 && !processing_template_decl)
10538 defaulted_late_check (r);
10539
10540 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10541 args, complain, in_decl);
10542 }
10543 break;
10544
10545 case PARM_DECL:
10546 {
10547 tree type = NULL_TREE;
10548 int i, len = 1;
10549 tree expanded_types = NULL_TREE;
10550 tree prev_r = NULL_TREE;
10551 tree first_r = NULL_TREE;
10552
10553 if (DECL_PACK_P (t))
10554 {
10555 /* If there is a local specialization that isn't a
10556 parameter pack, it means that we're doing a "simple"
10557 substitution from inside tsubst_pack_expansion. Just
10558 return the local specialization (which will be a single
10559 parm). */
10560 tree spec = retrieve_local_specialization (t);
10561 if (spec
10562 && TREE_CODE (spec) == PARM_DECL
10563 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10564 RETURN (spec);
10565
10566 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10567 the parameters in this function parameter pack. */
10568 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10569 complain, in_decl);
10570 if (TREE_CODE (expanded_types) == TREE_VEC)
10571 {
10572 len = TREE_VEC_LENGTH (expanded_types);
10573
10574 /* Zero-length parameter packs are boring. Just substitute
10575 into the chain. */
10576 if (len == 0)
10577 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10578 TREE_CHAIN (t)));
10579 }
10580 else
10581 {
10582 /* All we did was update the type. Make a note of that. */
10583 type = expanded_types;
10584 expanded_types = NULL_TREE;
10585 }
10586 }
10587
10588 /* Loop through all of the parameters we'll build. When T is
10589 a function parameter pack, LEN is the number of expanded
10590 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10591 r = NULL_TREE;
10592 for (i = 0; i < len; ++i)
10593 {
10594 prev_r = r;
10595 r = copy_node (t);
10596 if (DECL_TEMPLATE_PARM_P (t))
10597 SET_DECL_TEMPLATE_PARM_P (r);
10598
10599 if (expanded_types)
10600 /* We're on the Ith parameter of the function parameter
10601 pack. */
10602 {
10603 /* Get the Ith type. */
10604 type = TREE_VEC_ELT (expanded_types, i);
10605
10606 /* Rename the parameter to include the index. */
10607 DECL_NAME (r)
10608 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10609 }
10610 else if (!type)
10611 /* We're dealing with a normal parameter. */
10612 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10613
10614 type = type_decays_to (type);
10615 TREE_TYPE (r) = type;
10616 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10617
10618 if (DECL_INITIAL (r))
10619 {
10620 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10621 DECL_INITIAL (r) = TREE_TYPE (r);
10622 else
10623 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10624 complain, in_decl);
10625 }
10626
10627 DECL_CONTEXT (r) = NULL_TREE;
10628
10629 if (!DECL_TEMPLATE_PARM_P (r))
10630 DECL_ARG_TYPE (r) = type_passed_as (type);
10631
10632 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10633 args, complain, in_decl);
10634
10635 /* Keep track of the first new parameter we
10636 generate. That's what will be returned to the
10637 caller. */
10638 if (!first_r)
10639 first_r = r;
10640
10641 /* Build a proper chain of parameters when substituting
10642 into a function parameter pack. */
10643 if (prev_r)
10644 DECL_CHAIN (prev_r) = r;
10645 }
10646
10647 /* If cp_unevaluated_operand is set, we're just looking for a
10648 single dummy parameter, so don't keep going. */
10649 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10650 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10651 complain, DECL_CHAIN (t));
10652
10653 /* FIRST_R contains the start of the chain we've built. */
10654 r = first_r;
10655 }
10656 break;
10657
10658 case FIELD_DECL:
10659 {
10660 tree type = NULL_TREE;
10661 tree vec = NULL_TREE;
10662 tree expanded_types = NULL_TREE;
10663 int len = 1;
10664
10665 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10666 {
10667 /* This field is a lambda capture pack. Return a TREE_VEC of
10668 the expanded fields to instantiate_class_template_1 and
10669 store them in the specializations hash table as a
10670 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10671 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10672 complain, in_decl);
10673 if (TREE_CODE (expanded_types) == TREE_VEC)
10674 {
10675 len = TREE_VEC_LENGTH (expanded_types);
10676 vec = make_tree_vec (len);
10677 }
10678 else
10679 {
10680 /* All we did was update the type. Make a note of that. */
10681 type = expanded_types;
10682 expanded_types = NULL_TREE;
10683 }
10684 }
10685
10686 for (int i = 0; i < len; ++i)
10687 {
10688 r = copy_decl (t);
10689 if (expanded_types)
10690 {
10691 type = TREE_VEC_ELT (expanded_types, i);
10692 DECL_NAME (r)
10693 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10694 }
10695 else if (!type)
10696 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10697
10698 if (type == error_mark_node)
10699 RETURN (error_mark_node);
10700 TREE_TYPE (r) = type;
10701 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10702
10703 if (DECL_C_BIT_FIELD (r))
10704 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10705 non-bit-fields DECL_INITIAL is a non-static data member
10706 initializer, which gets deferred instantiation. */
10707 DECL_INITIAL (r)
10708 = tsubst_expr (DECL_INITIAL (t), args,
10709 complain, in_decl,
10710 /*integral_constant_expression_p=*/true);
10711 else if (DECL_INITIAL (t))
10712 {
10713 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10714 NSDMI in perform_member_init. Still set DECL_INITIAL
10715 so that we know there is one. */
10716 DECL_INITIAL (r) = void_zero_node;
10717 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10718 retrofit_lang_decl (r);
10719 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10720 }
10721 /* We don't have to set DECL_CONTEXT here; it is set by
10722 finish_member_declaration. */
10723 DECL_CHAIN (r) = NULL_TREE;
10724
10725 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10726 args, complain, in_decl);
10727
10728 if (vec)
10729 TREE_VEC_ELT (vec, i) = r;
10730 }
10731
10732 if (vec)
10733 {
10734 r = vec;
10735 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10736 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10737 SET_ARGUMENT_PACK_ARGS (pack, vec);
10738 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10739 TREE_TYPE (pack) = tpack;
10740 register_specialization (pack, t, args, false, 0);
10741 }
10742 }
10743 break;
10744
10745 case USING_DECL:
10746 /* We reach here only for member using decls. We also need to check
10747 uses_template_parms because DECL_DEPENDENT_P is not set for a
10748 using-declaration that designates a member of the current
10749 instantiation (c++/53549). */
10750 if (DECL_DEPENDENT_P (t)
10751 || uses_template_parms (USING_DECL_SCOPE (t)))
10752 {
10753 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10754 complain, in_decl);
10755 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10756 r = do_class_using_decl (inst_scope, name);
10757 if (!r)
10758 r = error_mark_node;
10759 else
10760 {
10761 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10762 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10763 }
10764 }
10765 else
10766 {
10767 r = copy_node (t);
10768 DECL_CHAIN (r) = NULL_TREE;
10769 }
10770 break;
10771
10772 case TYPE_DECL:
10773 case VAR_DECL:
10774 {
10775 tree argvec = NULL_TREE;
10776 tree gen_tmpl = NULL_TREE;
10777 tree spec;
10778 tree tmpl = NULL_TREE;
10779 tree ctx;
10780 tree type = NULL_TREE;
10781 bool local_p;
10782
10783 if (TREE_CODE (t) == TYPE_DECL
10784 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10785 {
10786 /* If this is the canonical decl, we don't have to
10787 mess with instantiations, and often we can't (for
10788 typename, template type parms and such). Note that
10789 TYPE_NAME is not correct for the above test if
10790 we've copied the type for a typedef. */
10791 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10792 if (type == error_mark_node)
10793 RETURN (error_mark_node);
10794 r = TYPE_NAME (type);
10795 break;
10796 }
10797
10798 /* Check to see if we already have the specialization we
10799 need. */
10800 spec = NULL_TREE;
10801 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10802 {
10803 /* T is a static data member or namespace-scope entity.
10804 We have to substitute into namespace-scope variables
10805 (even though such entities are never templates) because
10806 of cases like:
10807
10808 template <class T> void f() { extern T t; }
10809
10810 where the entity referenced is not known until
10811 instantiation time. */
10812 local_p = false;
10813 ctx = DECL_CONTEXT (t);
10814 if (DECL_CLASS_SCOPE_P (t))
10815 {
10816 ctx = tsubst_aggr_type (ctx, args,
10817 complain,
10818 in_decl, /*entering_scope=*/1);
10819 /* If CTX is unchanged, then T is in fact the
10820 specialization we want. That situation occurs when
10821 referencing a static data member within in its own
10822 class. We can use pointer equality, rather than
10823 same_type_p, because DECL_CONTEXT is always
10824 canonical... */
10825 if (ctx == DECL_CONTEXT (t)
10826 && (TREE_CODE (t) != TYPE_DECL
10827 /* ... unless T is a member template; in which
10828 case our caller can be willing to create a
10829 specialization of that template represented
10830 by T. */
10831 || !(DECL_TI_TEMPLATE (t)
10832 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10833 spec = t;
10834 }
10835
10836 if (!spec)
10837 {
10838 tmpl = DECL_TI_TEMPLATE (t);
10839 gen_tmpl = most_general_template (tmpl);
10840 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10841 if (argvec == error_mark_node)
10842 RETURN (error_mark_node);
10843 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10844 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10845 }
10846 }
10847 else
10848 {
10849 /* A local variable. */
10850 local_p = true;
10851 /* Subsequent calls to pushdecl will fill this in. */
10852 ctx = NULL_TREE;
10853 spec = retrieve_local_specialization (t);
10854 }
10855 /* If we already have the specialization we need, there is
10856 nothing more to do. */
10857 if (spec)
10858 {
10859 r = spec;
10860 break;
10861 }
10862
10863 /* Create a new node for the specialization we need. */
10864 r = copy_decl (t);
10865 if (type == NULL_TREE)
10866 {
10867 if (is_typedef_decl (t))
10868 type = DECL_ORIGINAL_TYPE (t);
10869 else
10870 type = TREE_TYPE (t);
10871 if (VAR_P (t)
10872 && VAR_HAD_UNKNOWN_BOUND (t)
10873 && type != error_mark_node)
10874 type = strip_array_domain (type);
10875 type = tsubst (type, args, complain, in_decl);
10876 }
10877 if (VAR_P (r))
10878 {
10879 /* Even if the original location is out of scope, the
10880 newly substituted one is not. */
10881 DECL_DEAD_FOR_LOCAL (r) = 0;
10882 DECL_INITIALIZED_P (r) = 0;
10883 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10884 if (type == error_mark_node)
10885 RETURN (error_mark_node);
10886 if (TREE_CODE (type) == FUNCTION_TYPE)
10887 {
10888 /* It may seem that this case cannot occur, since:
10889
10890 typedef void f();
10891 void g() { f x; }
10892
10893 declares a function, not a variable. However:
10894
10895 typedef void f();
10896 template <typename T> void g() { T t; }
10897 template void g<f>();
10898
10899 is an attempt to declare a variable with function
10900 type. */
10901 error ("variable %qD has function type",
10902 /* R is not yet sufficiently initialized, so we
10903 just use its name. */
10904 DECL_NAME (r));
10905 RETURN (error_mark_node);
10906 }
10907 type = complete_type (type);
10908 /* Wait until cp_finish_decl to set this again, to handle
10909 circular dependency (template/instantiate6.C). */
10910 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10911 type = check_var_type (DECL_NAME (r), type);
10912
10913 if (DECL_HAS_VALUE_EXPR_P (t))
10914 {
10915 tree ve = DECL_VALUE_EXPR (t);
10916 ve = tsubst_expr (ve, args, complain, in_decl,
10917 /*constant_expression_p=*/false);
10918 if (REFERENCE_REF_P (ve))
10919 {
10920 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10921 ve = TREE_OPERAND (ve, 0);
10922 }
10923 SET_DECL_VALUE_EXPR (r, ve);
10924 }
10925 }
10926 else if (DECL_SELF_REFERENCE_P (t))
10927 SET_DECL_SELF_REFERENCE_P (r);
10928 TREE_TYPE (r) = type;
10929 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10930 DECL_CONTEXT (r) = ctx;
10931 /* Clear out the mangled name and RTL for the instantiation. */
10932 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10933 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10934 SET_DECL_RTL (r, NULL);
10935 /* The initializer must not be expanded until it is required;
10936 see [temp.inst]. */
10937 DECL_INITIAL (r) = NULL_TREE;
10938 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10939 SET_DECL_RTL (r, NULL);
10940 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10941 if (VAR_P (r))
10942 {
10943 /* Possibly limit visibility based on template args. */
10944 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10945 if (DECL_VISIBILITY_SPECIFIED (t))
10946 {
10947 DECL_VISIBILITY_SPECIFIED (r) = 0;
10948 DECL_ATTRIBUTES (r)
10949 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10950 }
10951 determine_visibility (r);
10952 }
10953
10954 if (!local_p)
10955 {
10956 /* A static data member declaration is always marked
10957 external when it is declared in-class, even if an
10958 initializer is present. We mimic the non-template
10959 processing here. */
10960 DECL_EXTERNAL (r) = 1;
10961
10962 register_specialization (r, gen_tmpl, argvec, false, hash);
10963 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10964 SET_DECL_IMPLICIT_INSTANTIATION (r);
10965 }
10966 else if (cp_unevaluated_operand)
10967 gcc_unreachable ();
10968 else
10969 register_local_specialization (r, t);
10970
10971 DECL_CHAIN (r) = NULL_TREE;
10972
10973 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10974 /*flags=*/0,
10975 args, complain, in_decl);
10976
10977 /* Preserve a typedef that names a type. */
10978 if (is_typedef_decl (r))
10979 {
10980 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10981 set_underlying_type (r);
10982 }
10983
10984 layout_decl (r, 0);
10985 }
10986 break;
10987
10988 default:
10989 gcc_unreachable ();
10990 }
10991 #undef RETURN
10992
10993 out:
10994 /* Restore the file and line information. */
10995 input_location = saved_loc;
10996
10997 return r;
10998 }
10999
11000 /* Substitute into the ARG_TYPES of a function type.
11001 If END is a TREE_CHAIN, leave it and any following types
11002 un-substituted. */
11003
11004 static tree
11005 tsubst_arg_types (tree arg_types,
11006 tree args,
11007 tree end,
11008 tsubst_flags_t complain,
11009 tree in_decl)
11010 {
11011 tree remaining_arg_types;
11012 tree type = NULL_TREE;
11013 int i = 1;
11014 tree expanded_args = NULL_TREE;
11015 tree default_arg;
11016
11017 if (!arg_types || arg_types == void_list_node || arg_types == end)
11018 return arg_types;
11019
11020 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11021 args, end, complain, in_decl);
11022 if (remaining_arg_types == error_mark_node)
11023 return error_mark_node;
11024
11025 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11026 {
11027 /* For a pack expansion, perform substitution on the
11028 entire expression. Later on, we'll handle the arguments
11029 one-by-one. */
11030 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11031 args, complain, in_decl);
11032
11033 if (TREE_CODE (expanded_args) == TREE_VEC)
11034 /* So that we'll spin through the parameters, one by one. */
11035 i = TREE_VEC_LENGTH (expanded_args);
11036 else
11037 {
11038 /* We only partially substituted into the parameter
11039 pack. Our type is TYPE_PACK_EXPANSION. */
11040 type = expanded_args;
11041 expanded_args = NULL_TREE;
11042 }
11043 }
11044
11045 while (i > 0) {
11046 --i;
11047
11048 if (expanded_args)
11049 type = TREE_VEC_ELT (expanded_args, i);
11050 else if (!type)
11051 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11052
11053 if (type == error_mark_node)
11054 return error_mark_node;
11055 if (VOID_TYPE_P (type))
11056 {
11057 if (complain & tf_error)
11058 {
11059 error ("invalid parameter type %qT", type);
11060 if (in_decl)
11061 error ("in declaration %q+D", in_decl);
11062 }
11063 return error_mark_node;
11064 }
11065 /* DR 657. */
11066 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11067 return error_mark_node;
11068
11069 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11070 top-level qualifiers as required. */
11071 type = cv_unqualified (type_decays_to (type));
11072
11073 /* We do not substitute into default arguments here. The standard
11074 mandates that they be instantiated only when needed, which is
11075 done in build_over_call. */
11076 default_arg = TREE_PURPOSE (arg_types);
11077
11078 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11079 {
11080 /* We've instantiated a template before its default arguments
11081 have been parsed. This can happen for a nested template
11082 class, and is not an error unless we require the default
11083 argument in a call of this function. */
11084 remaining_arg_types =
11085 tree_cons (default_arg, type, remaining_arg_types);
11086 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11087 }
11088 else
11089 remaining_arg_types =
11090 hash_tree_cons (default_arg, type, remaining_arg_types);
11091 }
11092
11093 return remaining_arg_types;
11094 }
11095
11096 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11097 *not* handle the exception-specification for FNTYPE, because the
11098 initial substitution of explicitly provided template parameters
11099 during argument deduction forbids substitution into the
11100 exception-specification:
11101
11102 [temp.deduct]
11103
11104 All references in the function type of the function template to the
11105 corresponding template parameters are replaced by the specified tem-
11106 plate argument values. If a substitution in a template parameter or
11107 in the function type of the function template results in an invalid
11108 type, type deduction fails. [Note: The equivalent substitution in
11109 exception specifications is done only when the function is instanti-
11110 ated, at which point a program is ill-formed if the substitution
11111 results in an invalid type.] */
11112
11113 static tree
11114 tsubst_function_type (tree t,
11115 tree args,
11116 tsubst_flags_t complain,
11117 tree in_decl)
11118 {
11119 tree return_type;
11120 tree arg_types;
11121 tree fntype;
11122
11123 /* The TYPE_CONTEXT is not used for function/method types. */
11124 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11125
11126 /* Substitute the return type. */
11127 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11128 if (return_type == error_mark_node)
11129 return error_mark_node;
11130 /* DR 486 clarifies that creation of a function type with an
11131 invalid return type is a deduction failure. */
11132 if (TREE_CODE (return_type) == ARRAY_TYPE
11133 || TREE_CODE (return_type) == FUNCTION_TYPE)
11134 {
11135 if (complain & tf_error)
11136 {
11137 if (TREE_CODE (return_type) == ARRAY_TYPE)
11138 error ("function returning an array");
11139 else
11140 error ("function returning a function");
11141 }
11142 return error_mark_node;
11143 }
11144 /* And DR 657. */
11145 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11146 return error_mark_node;
11147
11148 /* Substitute the argument types. */
11149 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11150 complain, in_decl);
11151 if (arg_types == error_mark_node)
11152 return error_mark_node;
11153
11154 /* Construct a new type node and return it. */
11155 if (TREE_CODE (t) == FUNCTION_TYPE)
11156 {
11157 fntype = build_function_type (return_type, arg_types);
11158 fntype = apply_memfn_quals (fntype,
11159 type_memfn_quals (t),
11160 type_memfn_rqual (t));
11161 }
11162 else
11163 {
11164 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11165 if (! MAYBE_CLASS_TYPE_P (r))
11166 {
11167 /* [temp.deduct]
11168
11169 Type deduction may fail for any of the following
11170 reasons:
11171
11172 -- Attempting to create "pointer to member of T" when T
11173 is not a class type. */
11174 if (complain & tf_error)
11175 error ("creating pointer to member function of non-class type %qT",
11176 r);
11177 return error_mark_node;
11178 }
11179
11180 fntype = build_method_type_directly (r, return_type,
11181 TREE_CHAIN (arg_types));
11182 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11183 }
11184 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11185
11186 return fntype;
11187 }
11188
11189 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11190 ARGS into that specification, and return the substituted
11191 specification. If there is no specification, return NULL_TREE. */
11192
11193 static tree
11194 tsubst_exception_specification (tree fntype,
11195 tree args,
11196 tsubst_flags_t complain,
11197 tree in_decl,
11198 bool defer_ok)
11199 {
11200 tree specs;
11201 tree new_specs;
11202
11203 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11204 new_specs = NULL_TREE;
11205 if (specs && TREE_PURPOSE (specs))
11206 {
11207 /* A noexcept-specifier. */
11208 tree expr = TREE_PURPOSE (specs);
11209 if (TREE_CODE (expr) == INTEGER_CST)
11210 new_specs = expr;
11211 else if (defer_ok)
11212 {
11213 /* Defer instantiation of noexcept-specifiers to avoid
11214 excessive instantiations (c++/49107). */
11215 new_specs = make_node (DEFERRED_NOEXCEPT);
11216 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11217 {
11218 /* We already partially instantiated this member template,
11219 so combine the new args with the old. */
11220 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11221 = DEFERRED_NOEXCEPT_PATTERN (expr);
11222 DEFERRED_NOEXCEPT_ARGS (new_specs)
11223 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11224 }
11225 else
11226 {
11227 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11228 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11229 }
11230 }
11231 else
11232 new_specs = tsubst_copy_and_build
11233 (expr, args, complain, in_decl, /*function_p=*/false,
11234 /*integral_constant_expression_p=*/true);
11235 new_specs = build_noexcept_spec (new_specs, complain);
11236 }
11237 else if (specs)
11238 {
11239 if (! TREE_VALUE (specs))
11240 new_specs = specs;
11241 else
11242 while (specs)
11243 {
11244 tree spec;
11245 int i, len = 1;
11246 tree expanded_specs = NULL_TREE;
11247
11248 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11249 {
11250 /* Expand the pack expansion type. */
11251 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11252 args, complain,
11253 in_decl);
11254
11255 if (expanded_specs == error_mark_node)
11256 return error_mark_node;
11257 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11258 len = TREE_VEC_LENGTH (expanded_specs);
11259 else
11260 {
11261 /* We're substituting into a member template, so
11262 we got a TYPE_PACK_EXPANSION back. Add that
11263 expansion and move on. */
11264 gcc_assert (TREE_CODE (expanded_specs)
11265 == TYPE_PACK_EXPANSION);
11266 new_specs = add_exception_specifier (new_specs,
11267 expanded_specs,
11268 complain);
11269 specs = TREE_CHAIN (specs);
11270 continue;
11271 }
11272 }
11273
11274 for (i = 0; i < len; ++i)
11275 {
11276 if (expanded_specs)
11277 spec = TREE_VEC_ELT (expanded_specs, i);
11278 else
11279 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11280 if (spec == error_mark_node)
11281 return spec;
11282 new_specs = add_exception_specifier (new_specs, spec,
11283 complain);
11284 }
11285
11286 specs = TREE_CHAIN (specs);
11287 }
11288 }
11289 return new_specs;
11290 }
11291
11292 /* Take the tree structure T and replace template parameters used
11293 therein with the argument vector ARGS. IN_DECL is an associated
11294 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11295 Issue error and warning messages under control of COMPLAIN. Note
11296 that we must be relatively non-tolerant of extensions here, in
11297 order to preserve conformance; if we allow substitutions that
11298 should not be allowed, we may allow argument deductions that should
11299 not succeed, and therefore report ambiguous overload situations
11300 where there are none. In theory, we could allow the substitution,
11301 but indicate that it should have failed, and allow our caller to
11302 make sure that the right thing happens, but we don't try to do this
11303 yet.
11304
11305 This function is used for dealing with types, decls and the like;
11306 for expressions, use tsubst_expr or tsubst_copy. */
11307
11308 tree
11309 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11310 {
11311 enum tree_code code;
11312 tree type, r = NULL_TREE;
11313
11314 if (t == NULL_TREE || t == error_mark_node
11315 || t == integer_type_node
11316 || t == void_type_node
11317 || t == char_type_node
11318 || t == unknown_type_node
11319 || TREE_CODE (t) == NAMESPACE_DECL
11320 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11321 return t;
11322
11323 if (DECL_P (t))
11324 return tsubst_decl (t, args, complain);
11325
11326 if (args == NULL_TREE)
11327 return t;
11328
11329 code = TREE_CODE (t);
11330
11331 if (code == IDENTIFIER_NODE)
11332 type = IDENTIFIER_TYPE_VALUE (t);
11333 else
11334 type = TREE_TYPE (t);
11335
11336 gcc_assert (type != unknown_type_node);
11337
11338 /* Reuse typedefs. We need to do this to handle dependent attributes,
11339 such as attribute aligned. */
11340 if (TYPE_P (t)
11341 && typedef_variant_p (t))
11342 {
11343 tree decl = TYPE_NAME (t);
11344
11345 if (alias_template_specialization_p (t))
11346 {
11347 /* DECL represents an alias template and we want to
11348 instantiate it. */
11349 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11350 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11351 r = instantiate_alias_template (tmpl, gen_args, complain);
11352 }
11353 else if (DECL_CLASS_SCOPE_P (decl)
11354 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11355 && uses_template_parms (DECL_CONTEXT (decl)))
11356 {
11357 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11358 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11359 r = retrieve_specialization (tmpl, gen_args, 0);
11360 }
11361 else if (DECL_FUNCTION_SCOPE_P (decl)
11362 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11363 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11364 r = retrieve_local_specialization (decl);
11365 else
11366 /* The typedef is from a non-template context. */
11367 return t;
11368
11369 if (r)
11370 {
11371 r = TREE_TYPE (r);
11372 r = cp_build_qualified_type_real
11373 (r, cp_type_quals (t) | cp_type_quals (r),
11374 complain | tf_ignore_bad_quals);
11375 return r;
11376 }
11377 else
11378 {
11379 /* We don't have an instantiation yet, so drop the typedef. */
11380 int quals = cp_type_quals (t);
11381 t = DECL_ORIGINAL_TYPE (decl);
11382 t = cp_build_qualified_type_real (t, quals,
11383 complain | tf_ignore_bad_quals);
11384 }
11385 }
11386
11387 if (type
11388 && code != TYPENAME_TYPE
11389 && code != TEMPLATE_TYPE_PARM
11390 && code != IDENTIFIER_NODE
11391 && code != FUNCTION_TYPE
11392 && code != METHOD_TYPE)
11393 type = tsubst (type, args, complain, in_decl);
11394 if (type == error_mark_node)
11395 return error_mark_node;
11396
11397 switch (code)
11398 {
11399 case RECORD_TYPE:
11400 case UNION_TYPE:
11401 case ENUMERAL_TYPE:
11402 return tsubst_aggr_type (t, args, complain, in_decl,
11403 /*entering_scope=*/0);
11404
11405 case ERROR_MARK:
11406 case IDENTIFIER_NODE:
11407 case VOID_TYPE:
11408 case REAL_TYPE:
11409 case COMPLEX_TYPE:
11410 case VECTOR_TYPE:
11411 case BOOLEAN_TYPE:
11412 case NULLPTR_TYPE:
11413 case LANG_TYPE:
11414 return t;
11415
11416 case INTEGER_TYPE:
11417 if (t == integer_type_node)
11418 return t;
11419
11420 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11421 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11422 return t;
11423
11424 {
11425 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11426
11427 max = tsubst_expr (omax, args, complain, in_decl,
11428 /*integral_constant_expression_p=*/false);
11429
11430 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11431 needed. */
11432 if (TREE_CODE (max) == NOP_EXPR
11433 && TREE_SIDE_EFFECTS (omax)
11434 && !TREE_TYPE (max))
11435 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11436
11437 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11438 with TREE_SIDE_EFFECTS that indicates this is not an integral
11439 constant expression. */
11440 if (processing_template_decl
11441 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11442 {
11443 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11444 TREE_SIDE_EFFECTS (max) = 1;
11445 }
11446
11447 return compute_array_index_type (NULL_TREE, max, complain);
11448 }
11449
11450 case TEMPLATE_TYPE_PARM:
11451 case TEMPLATE_TEMPLATE_PARM:
11452 case BOUND_TEMPLATE_TEMPLATE_PARM:
11453 case TEMPLATE_PARM_INDEX:
11454 {
11455 int idx;
11456 int level;
11457 int levels;
11458 tree arg = NULL_TREE;
11459
11460 r = NULL_TREE;
11461
11462 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11463 template_parm_level_and_index (t, &level, &idx);
11464
11465 levels = TMPL_ARGS_DEPTH (args);
11466 if (level <= levels)
11467 {
11468 arg = TMPL_ARG (args, level, idx);
11469
11470 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11471 {
11472 /* See through ARGUMENT_PACK_SELECT arguments. */
11473 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11474 /* If the selected argument is an expansion E, that most
11475 likely means we were called from
11476 gen_elem_of_pack_expansion_instantiation during the
11477 substituting of pack an argument pack (which Ith
11478 element is a pack expansion, where I is
11479 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11480 In this case, the Ith element resulting from this
11481 substituting is going to be a pack expansion, which
11482 pattern is the pattern of E. Let's return the
11483 pattern of E, and
11484 gen_elem_of_pack_expansion_instantiation will
11485 build the resulting pack expansion from it. */
11486 if (PACK_EXPANSION_P (arg))
11487 arg = PACK_EXPANSION_PATTERN (arg);
11488 }
11489 }
11490
11491 if (arg == error_mark_node)
11492 return error_mark_node;
11493 else if (arg != NULL_TREE)
11494 {
11495 if (ARGUMENT_PACK_P (arg))
11496 /* If ARG is an argument pack, we don't actually want to
11497 perform a substitution here, because substitutions
11498 for argument packs are only done
11499 element-by-element. We can get to this point when
11500 substituting the type of a non-type template
11501 parameter pack, when that type actually contains
11502 template parameter packs from an outer template, e.g.,
11503
11504 template<typename... Types> struct A {
11505 template<Types... Values> struct B { };
11506 }; */
11507 return t;
11508
11509 if (code == TEMPLATE_TYPE_PARM)
11510 {
11511 int quals;
11512 gcc_assert (TYPE_P (arg));
11513
11514 quals = cp_type_quals (arg) | cp_type_quals (t);
11515
11516 return cp_build_qualified_type_real
11517 (arg, quals, complain | tf_ignore_bad_quals);
11518 }
11519 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11520 {
11521 /* We are processing a type constructed from a
11522 template template parameter. */
11523 tree argvec = tsubst (TYPE_TI_ARGS (t),
11524 args, complain, in_decl);
11525 if (argvec == error_mark_node)
11526 return error_mark_node;
11527
11528 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11529 || TREE_CODE (arg) == TEMPLATE_DECL
11530 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11531
11532 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11533 /* Consider this code:
11534
11535 template <template <class> class Template>
11536 struct Internal {
11537 template <class Arg> using Bind = Template<Arg>;
11538 };
11539
11540 template <template <class> class Template, class Arg>
11541 using Instantiate = Template<Arg>; //#0
11542
11543 template <template <class> class Template,
11544 class Argument>
11545 using Bind =
11546 Instantiate<Internal<Template>::template Bind,
11547 Argument>; //#1
11548
11549 When #1 is parsed, the
11550 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11551 parameter `Template' in #0 matches the
11552 UNBOUND_CLASS_TEMPLATE representing the argument
11553 `Internal<Template>::template Bind'; We then want
11554 to assemble the type `Bind<Argument>' that can't
11555 be fully created right now, because
11556 `Internal<Template>' not being complete, the Bind
11557 template cannot be looked up in that context. So
11558 we need to "store" `Bind<Argument>' for later
11559 when the context of Bind becomes complete. Let's
11560 store that in a TYPENAME_TYPE. */
11561 return make_typename_type (TYPE_CONTEXT (arg),
11562 build_nt (TEMPLATE_ID_EXPR,
11563 TYPE_IDENTIFIER (arg),
11564 argvec),
11565 typename_type,
11566 complain);
11567
11568 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11569 are resolving nested-types in the signature of a
11570 member function templates. Otherwise ARG is a
11571 TEMPLATE_DECL and is the real template to be
11572 instantiated. */
11573 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11574 arg = TYPE_NAME (arg);
11575
11576 r = lookup_template_class (arg,
11577 argvec, in_decl,
11578 DECL_CONTEXT (arg),
11579 /*entering_scope=*/0,
11580 complain);
11581 return cp_build_qualified_type_real
11582 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11583 }
11584 else
11585 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11586 return convert_from_reference (unshare_expr (arg));
11587 }
11588
11589 if (level == 1)
11590 /* This can happen during the attempted tsubst'ing in
11591 unify. This means that we don't yet have any information
11592 about the template parameter in question. */
11593 return t;
11594
11595 /* Early in template argument deduction substitution, we don't
11596 want to reduce the level of 'auto', or it will be confused
11597 with a normal template parm in subsequent deduction. */
11598 if (is_auto (t) && (complain & tf_partial))
11599 return t;
11600
11601 /* If we get here, we must have been looking at a parm for a
11602 more deeply nested template. Make a new version of this
11603 template parameter, but with a lower level. */
11604 switch (code)
11605 {
11606 case TEMPLATE_TYPE_PARM:
11607 case TEMPLATE_TEMPLATE_PARM:
11608 case BOUND_TEMPLATE_TEMPLATE_PARM:
11609 if (cp_type_quals (t))
11610 {
11611 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11612 r = cp_build_qualified_type_real
11613 (r, cp_type_quals (t),
11614 complain | (code == TEMPLATE_TYPE_PARM
11615 ? tf_ignore_bad_quals : 0));
11616 }
11617 else
11618 {
11619 r = copy_type (t);
11620 TEMPLATE_TYPE_PARM_INDEX (r)
11621 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11622 r, levels, args, complain);
11623 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11624 TYPE_MAIN_VARIANT (r) = r;
11625 TYPE_POINTER_TO (r) = NULL_TREE;
11626 TYPE_REFERENCE_TO (r) = NULL_TREE;
11627
11628 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11629 /* We have reduced the level of the template
11630 template parameter, but not the levels of its
11631 template parameters, so canonical_type_parameter
11632 will not be able to find the canonical template
11633 template parameter for this level. Thus, we
11634 require structural equality checking to compare
11635 TEMPLATE_TEMPLATE_PARMs. */
11636 SET_TYPE_STRUCTURAL_EQUALITY (r);
11637 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11638 SET_TYPE_STRUCTURAL_EQUALITY (r);
11639 else
11640 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11641
11642 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11643 {
11644 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11645 complain, in_decl);
11646 if (argvec == error_mark_node)
11647 return error_mark_node;
11648
11649 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11650 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11651 }
11652 }
11653 break;
11654
11655 case TEMPLATE_PARM_INDEX:
11656 r = reduce_template_parm_level (t, type, levels, args, complain);
11657 break;
11658
11659 default:
11660 gcc_unreachable ();
11661 }
11662
11663 return r;
11664 }
11665
11666 case TREE_LIST:
11667 {
11668 tree purpose, value, chain;
11669
11670 if (t == void_list_node)
11671 return t;
11672
11673 purpose = TREE_PURPOSE (t);
11674 if (purpose)
11675 {
11676 purpose = tsubst (purpose, args, complain, in_decl);
11677 if (purpose == error_mark_node)
11678 return error_mark_node;
11679 }
11680 value = TREE_VALUE (t);
11681 if (value)
11682 {
11683 value = tsubst (value, args, complain, in_decl);
11684 if (value == error_mark_node)
11685 return error_mark_node;
11686 }
11687 chain = TREE_CHAIN (t);
11688 if (chain && chain != void_type_node)
11689 {
11690 chain = tsubst (chain, args, complain, in_decl);
11691 if (chain == error_mark_node)
11692 return error_mark_node;
11693 }
11694 if (purpose == TREE_PURPOSE (t)
11695 && value == TREE_VALUE (t)
11696 && chain == TREE_CHAIN (t))
11697 return t;
11698 return hash_tree_cons (purpose, value, chain);
11699 }
11700
11701 case TREE_BINFO:
11702 /* We should never be tsubsting a binfo. */
11703 gcc_unreachable ();
11704
11705 case TREE_VEC:
11706 /* A vector of template arguments. */
11707 gcc_assert (!type);
11708 return tsubst_template_args (t, args, complain, in_decl);
11709
11710 case POINTER_TYPE:
11711 case REFERENCE_TYPE:
11712 {
11713 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11714 return t;
11715
11716 /* [temp.deduct]
11717
11718 Type deduction may fail for any of the following
11719 reasons:
11720
11721 -- Attempting to create a pointer to reference type.
11722 -- Attempting to create a reference to a reference type or
11723 a reference to void.
11724
11725 Core issue 106 says that creating a reference to a reference
11726 during instantiation is no longer a cause for failure. We
11727 only enforce this check in strict C++98 mode. */
11728 if ((TREE_CODE (type) == REFERENCE_TYPE
11729 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11730 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11731 {
11732 static location_t last_loc;
11733
11734 /* We keep track of the last time we issued this error
11735 message to avoid spewing a ton of messages during a
11736 single bad template instantiation. */
11737 if (complain & tf_error
11738 && last_loc != input_location)
11739 {
11740 if (VOID_TYPE_P (type))
11741 error ("forming reference to void");
11742 else if (code == POINTER_TYPE)
11743 error ("forming pointer to reference type %qT", type);
11744 else
11745 error ("forming reference to reference type %qT", type);
11746 last_loc = input_location;
11747 }
11748
11749 return error_mark_node;
11750 }
11751 else if (TREE_CODE (type) == FUNCTION_TYPE
11752 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11753 || type_memfn_rqual (type) != REF_QUAL_NONE))
11754 {
11755 if (complain & tf_error)
11756 {
11757 if (code == POINTER_TYPE)
11758 error ("forming pointer to qualified function type %qT",
11759 type);
11760 else
11761 error ("forming reference to qualified function type %qT",
11762 type);
11763 }
11764 return error_mark_node;
11765 }
11766 else if (code == POINTER_TYPE)
11767 {
11768 r = build_pointer_type (type);
11769 if (TREE_CODE (type) == METHOD_TYPE)
11770 r = build_ptrmemfunc_type (r);
11771 }
11772 else if (TREE_CODE (type) == REFERENCE_TYPE)
11773 /* In C++0x, during template argument substitution, when there is an
11774 attempt to create a reference to a reference type, reference
11775 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11776
11777 "If a template-argument for a template-parameter T names a type
11778 that is a reference to a type A, an attempt to create the type
11779 'lvalue reference to cv T' creates the type 'lvalue reference to
11780 A,' while an attempt to create the type type rvalue reference to
11781 cv T' creates the type T"
11782 */
11783 r = cp_build_reference_type
11784 (TREE_TYPE (type),
11785 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11786 else
11787 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11788 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11789
11790 if (cxx_dialect >= cxx1y
11791 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11792 && array_of_runtime_bound_p (type))
11793 {
11794 if (complain & tf_warning_or_error)
11795 pedwarn
11796 (input_location, OPT_Wvla,
11797 code == REFERENCE_TYPE
11798 ? G_("cannot declare reference to array of runtime bound")
11799 : G_("cannot declare pointer to array of runtime bound"));
11800 else
11801 r = error_mark_node;
11802 }
11803
11804 if (r != error_mark_node)
11805 /* Will this ever be needed for TYPE_..._TO values? */
11806 layout_type (r);
11807
11808 return r;
11809 }
11810 case OFFSET_TYPE:
11811 {
11812 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11813 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11814 {
11815 /* [temp.deduct]
11816
11817 Type deduction may fail for any of the following
11818 reasons:
11819
11820 -- Attempting to create "pointer to member of T" when T
11821 is not a class type. */
11822 if (complain & tf_error)
11823 error ("creating pointer to member of non-class type %qT", r);
11824 return error_mark_node;
11825 }
11826 if (TREE_CODE (type) == REFERENCE_TYPE)
11827 {
11828 if (complain & tf_error)
11829 error ("creating pointer to member reference type %qT", type);
11830 return error_mark_node;
11831 }
11832 if (VOID_TYPE_P (type))
11833 {
11834 if (complain & tf_error)
11835 error ("creating pointer to member of type void");
11836 return error_mark_node;
11837 }
11838 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11839 if (TREE_CODE (type) == FUNCTION_TYPE)
11840 {
11841 /* The type of the implicit object parameter gets its
11842 cv-qualifiers from the FUNCTION_TYPE. */
11843 tree memptr;
11844 tree method_type
11845 = build_memfn_type (type, r, type_memfn_quals (type),
11846 type_memfn_rqual (type));
11847 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11848 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11849 complain);
11850 }
11851 else
11852 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11853 cp_type_quals (t),
11854 complain);
11855 }
11856 case FUNCTION_TYPE:
11857 case METHOD_TYPE:
11858 {
11859 tree fntype;
11860 tree specs;
11861 fntype = tsubst_function_type (t, args, complain, in_decl);
11862 if (fntype == error_mark_node)
11863 return error_mark_node;
11864
11865 /* Substitute the exception specification. */
11866 specs = tsubst_exception_specification (t, args, complain,
11867 in_decl, /*defer_ok*/true);
11868 if (specs == error_mark_node)
11869 return error_mark_node;
11870 if (specs)
11871 fntype = build_exception_variant (fntype, specs);
11872 return fntype;
11873 }
11874 case ARRAY_TYPE:
11875 {
11876 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11877 if (domain == error_mark_node)
11878 return error_mark_node;
11879
11880 /* As an optimization, we avoid regenerating the array type if
11881 it will obviously be the same as T. */
11882 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11883 return t;
11884
11885 /* These checks should match the ones in grokdeclarator.
11886
11887 [temp.deduct]
11888
11889 The deduction may fail for any of the following reasons:
11890
11891 -- Attempting to create an array with an element type that
11892 is void, a function type, or a reference type, or [DR337]
11893 an abstract class type. */
11894 if (VOID_TYPE_P (type)
11895 || TREE_CODE (type) == FUNCTION_TYPE
11896 || TREE_CODE (type) == REFERENCE_TYPE)
11897 {
11898 if (complain & tf_error)
11899 error ("creating array of %qT", type);
11900 return error_mark_node;
11901 }
11902
11903 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11904 return error_mark_node;
11905
11906 r = build_cplus_array_type (type, domain);
11907
11908 if (TYPE_USER_ALIGN (t))
11909 {
11910 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11911 TYPE_USER_ALIGN (r) = 1;
11912 }
11913
11914 return r;
11915 }
11916
11917 case TYPENAME_TYPE:
11918 {
11919 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11920 in_decl, /*entering_scope=*/1);
11921 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11922 complain, in_decl);
11923
11924 if (ctx == error_mark_node || f == error_mark_node)
11925 return error_mark_node;
11926
11927 if (!MAYBE_CLASS_TYPE_P (ctx))
11928 {
11929 if (complain & tf_error)
11930 error ("%qT is not a class, struct, or union type", ctx);
11931 return error_mark_node;
11932 }
11933 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11934 {
11935 /* Normally, make_typename_type does not require that the CTX
11936 have complete type in order to allow things like:
11937
11938 template <class T> struct S { typename S<T>::X Y; };
11939
11940 But, such constructs have already been resolved by this
11941 point, so here CTX really should have complete type, unless
11942 it's a partial instantiation. */
11943 ctx = complete_type (ctx);
11944 if (!COMPLETE_TYPE_P (ctx))
11945 {
11946 if (complain & tf_error)
11947 cxx_incomplete_type_error (NULL_TREE, ctx);
11948 return error_mark_node;
11949 }
11950 }
11951
11952 f = make_typename_type (ctx, f, typename_type,
11953 complain | tf_keep_type_decl);
11954 if (f == error_mark_node)
11955 return f;
11956 if (TREE_CODE (f) == TYPE_DECL)
11957 {
11958 complain |= tf_ignore_bad_quals;
11959 f = TREE_TYPE (f);
11960 }
11961
11962 if (TREE_CODE (f) != TYPENAME_TYPE)
11963 {
11964 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11965 {
11966 if (complain & tf_error)
11967 error ("%qT resolves to %qT, which is not an enumeration type",
11968 t, f);
11969 else
11970 return error_mark_node;
11971 }
11972 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11973 {
11974 if (complain & tf_error)
11975 error ("%qT resolves to %qT, which is is not a class type",
11976 t, f);
11977 else
11978 return error_mark_node;
11979 }
11980 }
11981
11982 return cp_build_qualified_type_real
11983 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11984 }
11985
11986 case UNBOUND_CLASS_TEMPLATE:
11987 {
11988 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11989 in_decl, /*entering_scope=*/1);
11990 tree name = TYPE_IDENTIFIER (t);
11991 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11992
11993 if (ctx == error_mark_node || name == error_mark_node)
11994 return error_mark_node;
11995
11996 if (parm_list)
11997 parm_list = tsubst_template_parms (parm_list, args, complain);
11998 return make_unbound_class_template (ctx, name, parm_list, complain);
11999 }
12000
12001 case TYPEOF_TYPE:
12002 {
12003 tree type;
12004
12005 ++cp_unevaluated_operand;
12006 ++c_inhibit_evaluation_warnings;
12007
12008 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12009 complain, in_decl,
12010 /*integral_constant_expression_p=*/false);
12011
12012 --cp_unevaluated_operand;
12013 --c_inhibit_evaluation_warnings;
12014
12015 type = finish_typeof (type);
12016 return cp_build_qualified_type_real (type,
12017 cp_type_quals (t)
12018 | cp_type_quals (type),
12019 complain);
12020 }
12021
12022 case DECLTYPE_TYPE:
12023 {
12024 tree type;
12025
12026 ++cp_unevaluated_operand;
12027 ++c_inhibit_evaluation_warnings;
12028
12029 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12030 complain|tf_decltype, in_decl,
12031 /*function_p*/false,
12032 /*integral_constant_expression*/false);
12033
12034 --cp_unevaluated_operand;
12035 --c_inhibit_evaluation_warnings;
12036
12037 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12038 type = lambda_capture_field_type (type,
12039 DECLTYPE_FOR_INIT_CAPTURE (t));
12040 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12041 type = lambda_proxy_type (type);
12042 else
12043 {
12044 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12045 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12046 && EXPR_P (type))
12047 /* In a template ~id could be either a complement expression
12048 or an unqualified-id naming a destructor; if instantiating
12049 it produces an expression, it's not an id-expression or
12050 member access. */
12051 id = false;
12052 type = finish_decltype_type (type, id, complain);
12053 }
12054 return cp_build_qualified_type_real (type,
12055 cp_type_quals (t)
12056 | cp_type_quals (type),
12057 complain);
12058 }
12059
12060 case UNDERLYING_TYPE:
12061 {
12062 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12063 complain, in_decl);
12064 return finish_underlying_type (type);
12065 }
12066
12067 case TYPE_ARGUMENT_PACK:
12068 case NONTYPE_ARGUMENT_PACK:
12069 {
12070 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12071 tree packed_out =
12072 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12073 args,
12074 complain,
12075 in_decl);
12076 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12077
12078 /* For template nontype argument packs, also substitute into
12079 the type. */
12080 if (code == NONTYPE_ARGUMENT_PACK)
12081 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12082
12083 return r;
12084 }
12085 break;
12086
12087 case INTEGER_CST:
12088 case REAL_CST:
12089 case STRING_CST:
12090 case PLUS_EXPR:
12091 case MINUS_EXPR:
12092 case NEGATE_EXPR:
12093 case NOP_EXPR:
12094 case INDIRECT_REF:
12095 case ADDR_EXPR:
12096 case CALL_EXPR:
12097 case ARRAY_REF:
12098 case SCOPE_REF:
12099 /* We should use one of the expression tsubsts for these codes. */
12100 gcc_unreachable ();
12101
12102 default:
12103 sorry ("use of %qs in template", get_tree_code_name (code));
12104 return error_mark_node;
12105 }
12106 }
12107
12108 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12109 type of the expression on the left-hand side of the "." or "->"
12110 operator. */
12111
12112 static tree
12113 tsubst_baselink (tree baselink, tree object_type,
12114 tree args, tsubst_flags_t complain, tree in_decl)
12115 {
12116 tree name;
12117 tree qualifying_scope;
12118 tree fns;
12119 tree optype;
12120 tree template_args = 0;
12121 bool template_id_p = false;
12122 bool qualified = BASELINK_QUALIFIED_P (baselink);
12123
12124 /* A baselink indicates a function from a base class. Both the
12125 BASELINK_ACCESS_BINFO and the base class referenced may
12126 indicate bases of the template class, rather than the
12127 instantiated class. In addition, lookups that were not
12128 ambiguous before may be ambiguous now. Therefore, we perform
12129 the lookup again. */
12130 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12131 qualifying_scope = tsubst (qualifying_scope, args,
12132 complain, in_decl);
12133 fns = BASELINK_FUNCTIONS (baselink);
12134 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12135 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12136 {
12137 template_id_p = true;
12138 template_args = TREE_OPERAND (fns, 1);
12139 fns = TREE_OPERAND (fns, 0);
12140 if (template_args)
12141 template_args = tsubst_template_args (template_args, args,
12142 complain, in_decl);
12143 }
12144 name = DECL_NAME (get_first_fn (fns));
12145 if (IDENTIFIER_TYPENAME_P (name))
12146 name = mangle_conv_op_name_for_type (optype);
12147 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12148 if (!baselink)
12149 return error_mark_node;
12150
12151 /* If lookup found a single function, mark it as used at this
12152 point. (If it lookup found multiple functions the one selected
12153 later by overload resolution will be marked as used at that
12154 point.) */
12155 if (BASELINK_P (baselink))
12156 fns = BASELINK_FUNCTIONS (baselink);
12157 if (!template_id_p && !really_overloaded_fn (fns))
12158 mark_used (OVL_CURRENT (fns));
12159
12160 /* Add back the template arguments, if present. */
12161 if (BASELINK_P (baselink) && template_id_p)
12162 BASELINK_FUNCTIONS (baselink)
12163 = build_nt (TEMPLATE_ID_EXPR,
12164 BASELINK_FUNCTIONS (baselink),
12165 template_args);
12166 /* Update the conversion operator type. */
12167 BASELINK_OPTYPE (baselink) = optype;
12168
12169 if (!object_type)
12170 object_type = current_class_type;
12171
12172 if (qualified)
12173 baselink = adjust_result_of_qualified_name_lookup (baselink,
12174 qualifying_scope,
12175 object_type);
12176 return baselink;
12177 }
12178
12179 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12180 true if the qualified-id will be a postfix-expression in-and-of
12181 itself; false if more of the postfix-expression follows the
12182 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12183 of "&". */
12184
12185 static tree
12186 tsubst_qualified_id (tree qualified_id, tree args,
12187 tsubst_flags_t complain, tree in_decl,
12188 bool done, bool address_p)
12189 {
12190 tree expr;
12191 tree scope;
12192 tree name;
12193 bool is_template;
12194 tree template_args;
12195 location_t loc = UNKNOWN_LOCATION;
12196
12197 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12198
12199 /* Figure out what name to look up. */
12200 name = TREE_OPERAND (qualified_id, 1);
12201 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12202 {
12203 is_template = true;
12204 loc = EXPR_LOCATION (name);
12205 template_args = TREE_OPERAND (name, 1);
12206 if (template_args)
12207 template_args = tsubst_template_args (template_args, args,
12208 complain, in_decl);
12209 name = TREE_OPERAND (name, 0);
12210 }
12211 else
12212 {
12213 is_template = false;
12214 template_args = NULL_TREE;
12215 }
12216
12217 /* Substitute into the qualifying scope. When there are no ARGS, we
12218 are just trying to simplify a non-dependent expression. In that
12219 case the qualifying scope may be dependent, and, in any case,
12220 substituting will not help. */
12221 scope = TREE_OPERAND (qualified_id, 0);
12222 if (args)
12223 {
12224 scope = tsubst (scope, args, complain, in_decl);
12225 expr = tsubst_copy (name, args, complain, in_decl);
12226 }
12227 else
12228 expr = name;
12229
12230 if (dependent_scope_p (scope))
12231 {
12232 if (is_template)
12233 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12234 return build_qualified_name (NULL_TREE, scope, expr,
12235 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12236 }
12237
12238 if (!BASELINK_P (name) && !DECL_P (expr))
12239 {
12240 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12241 {
12242 /* A BIT_NOT_EXPR is used to represent a destructor. */
12243 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12244 {
12245 error ("qualifying type %qT does not match destructor name ~%qT",
12246 scope, TREE_OPERAND (expr, 0));
12247 expr = error_mark_node;
12248 }
12249 else
12250 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12251 /*is_type_p=*/0, false);
12252 }
12253 else
12254 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12255 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12256 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12257 {
12258 if (complain & tf_error)
12259 {
12260 error ("dependent-name %qE is parsed as a non-type, but "
12261 "instantiation yields a type", qualified_id);
12262 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12263 }
12264 return error_mark_node;
12265 }
12266 }
12267
12268 if (DECL_P (expr))
12269 {
12270 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12271 scope);
12272 /* Remember that there was a reference to this entity. */
12273 mark_used (expr);
12274 }
12275
12276 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12277 {
12278 if (complain & tf_error)
12279 qualified_name_lookup_error (scope,
12280 TREE_OPERAND (qualified_id, 1),
12281 expr, input_location);
12282 return error_mark_node;
12283 }
12284
12285 if (is_template)
12286 expr = lookup_template_function (expr, template_args);
12287
12288 if (expr == error_mark_node && complain & tf_error)
12289 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12290 expr, input_location);
12291 else if (TYPE_P (scope))
12292 {
12293 expr = (adjust_result_of_qualified_name_lookup
12294 (expr, scope, current_nonlambda_class_type ()));
12295 expr = (finish_qualified_id_expr
12296 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12297 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12298 /*template_arg_p=*/false, complain));
12299 }
12300
12301 /* Expressions do not generally have reference type. */
12302 if (TREE_CODE (expr) != SCOPE_REF
12303 /* However, if we're about to form a pointer-to-member, we just
12304 want the referenced member referenced. */
12305 && TREE_CODE (expr) != OFFSET_REF)
12306 expr = convert_from_reference (expr);
12307
12308 return expr;
12309 }
12310
12311 /* Like tsubst, but deals with expressions. This function just replaces
12312 template parms; to finish processing the resultant expression, use
12313 tsubst_copy_and_build or tsubst_expr. */
12314
12315 static tree
12316 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12317 {
12318 enum tree_code code;
12319 tree r;
12320
12321 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12322 return t;
12323
12324 code = TREE_CODE (t);
12325
12326 switch (code)
12327 {
12328 case PARM_DECL:
12329 r = retrieve_local_specialization (t);
12330
12331 if (r == NULL_TREE)
12332 {
12333 /* We get here for a use of 'this' in an NSDMI. */
12334 if (DECL_NAME (t) == this_identifier
12335 && at_function_scope_p ()
12336 && DECL_CONSTRUCTOR_P (current_function_decl))
12337 return current_class_ptr;
12338
12339 /* This can happen for a parameter name used later in a function
12340 declaration (such as in a late-specified return type). Just
12341 make a dummy decl, since it's only used for its type. */
12342 gcc_assert (cp_unevaluated_operand != 0);
12343 r = tsubst_decl (t, args, complain);
12344 /* Give it the template pattern as its context; its true context
12345 hasn't been instantiated yet and this is good enough for
12346 mangling. */
12347 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12348 }
12349
12350 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12351 r = ARGUMENT_PACK_SELECT_ARG (r);
12352 mark_used (r);
12353 return r;
12354
12355 case CONST_DECL:
12356 {
12357 tree enum_type;
12358 tree v;
12359
12360 if (DECL_TEMPLATE_PARM_P (t))
12361 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12362 /* There is no need to substitute into namespace-scope
12363 enumerators. */
12364 if (DECL_NAMESPACE_SCOPE_P (t))
12365 return t;
12366 /* If ARGS is NULL, then T is known to be non-dependent. */
12367 if (args == NULL_TREE)
12368 return integral_constant_value (t);
12369
12370 /* Unfortunately, we cannot just call lookup_name here.
12371 Consider:
12372
12373 template <int I> int f() {
12374 enum E { a = I };
12375 struct S { void g() { E e = a; } };
12376 };
12377
12378 When we instantiate f<7>::S::g(), say, lookup_name is not
12379 clever enough to find f<7>::a. */
12380 enum_type
12381 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12382 /*entering_scope=*/0);
12383
12384 for (v = TYPE_VALUES (enum_type);
12385 v != NULL_TREE;
12386 v = TREE_CHAIN (v))
12387 if (TREE_PURPOSE (v) == DECL_NAME (t))
12388 return TREE_VALUE (v);
12389
12390 /* We didn't find the name. That should never happen; if
12391 name-lookup found it during preliminary parsing, we
12392 should find it again here during instantiation. */
12393 gcc_unreachable ();
12394 }
12395 return t;
12396
12397 case FIELD_DECL:
12398 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12399 {
12400 /* Check for a local specialization set up by
12401 tsubst_pack_expansion. */
12402 tree r = retrieve_local_specialization (t);
12403 if (r)
12404 {
12405 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12406 r = ARGUMENT_PACK_SELECT_ARG (r);
12407 return r;
12408 }
12409
12410 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12411 tsubst_decl put in the hash table. */
12412 return retrieve_specialization (t, args, 0);
12413 }
12414
12415 if (DECL_CONTEXT (t))
12416 {
12417 tree ctx;
12418
12419 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12420 /*entering_scope=*/1);
12421 if (ctx != DECL_CONTEXT (t))
12422 {
12423 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12424 if (!r)
12425 {
12426 if (complain & tf_error)
12427 error ("using invalid field %qD", t);
12428 return error_mark_node;
12429 }
12430 return r;
12431 }
12432 }
12433
12434 return t;
12435
12436 case VAR_DECL:
12437 case FUNCTION_DECL:
12438 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12439 r = tsubst (t, args, complain, in_decl);
12440 else if (local_variable_p (t))
12441 {
12442 r = retrieve_local_specialization (t);
12443 if (r == NULL_TREE)
12444 {
12445 if (DECL_ANON_UNION_VAR_P (t))
12446 {
12447 /* Just use name lookup to find a member alias for an
12448 anonymous union, but then add it to the hash table. */
12449 r = lookup_name (DECL_NAME (t));
12450 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12451 register_local_specialization (r, t);
12452 }
12453 else
12454 {
12455 gcc_assert (errorcount || sorrycount);
12456 return error_mark_node;
12457 }
12458 }
12459 }
12460 else
12461 r = t;
12462 mark_used (r);
12463 return r;
12464
12465 case NAMESPACE_DECL:
12466 return t;
12467
12468 case OVERLOAD:
12469 /* An OVERLOAD will always be a non-dependent overload set; an
12470 overload set from function scope will just be represented with an
12471 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12472 gcc_assert (!uses_template_parms (t));
12473 return t;
12474
12475 case BASELINK:
12476 return tsubst_baselink (t, current_nonlambda_class_type (),
12477 args, complain, in_decl);
12478
12479 case TEMPLATE_DECL:
12480 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12481 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12482 args, complain, in_decl);
12483 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12484 return tsubst (t, args, complain, in_decl);
12485 else if (DECL_CLASS_SCOPE_P (t)
12486 && uses_template_parms (DECL_CONTEXT (t)))
12487 {
12488 /* Template template argument like the following example need
12489 special treatment:
12490
12491 template <template <class> class TT> struct C {};
12492 template <class T> struct D {
12493 template <class U> struct E {};
12494 C<E> c; // #1
12495 };
12496 D<int> d; // #2
12497
12498 We are processing the template argument `E' in #1 for
12499 the template instantiation #2. Originally, `E' is a
12500 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12501 have to substitute this with one having context `D<int>'. */
12502
12503 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12504 return lookup_field (context, DECL_NAME(t), 0, false);
12505 }
12506 else
12507 /* Ordinary template template argument. */
12508 return t;
12509
12510 case CAST_EXPR:
12511 case REINTERPRET_CAST_EXPR:
12512 case CONST_CAST_EXPR:
12513 case STATIC_CAST_EXPR:
12514 case DYNAMIC_CAST_EXPR:
12515 case IMPLICIT_CONV_EXPR:
12516 case CONVERT_EXPR:
12517 case NOP_EXPR:
12518 return build1
12519 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12520 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12521
12522 case SIZEOF_EXPR:
12523 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12524 {
12525
12526 tree expanded, op = TREE_OPERAND (t, 0);
12527 int len = 0;
12528
12529 if (SIZEOF_EXPR_TYPE_P (t))
12530 op = TREE_TYPE (op);
12531
12532 ++cp_unevaluated_operand;
12533 ++c_inhibit_evaluation_warnings;
12534 /* We only want to compute the number of arguments. */
12535 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12536 --cp_unevaluated_operand;
12537 --c_inhibit_evaluation_warnings;
12538
12539 if (TREE_CODE (expanded) == TREE_VEC)
12540 len = TREE_VEC_LENGTH (expanded);
12541
12542 if (expanded == error_mark_node)
12543 return error_mark_node;
12544 else if (PACK_EXPANSION_P (expanded)
12545 || (TREE_CODE (expanded) == TREE_VEC
12546 && len > 0
12547 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12548 {
12549 if (TREE_CODE (expanded) == TREE_VEC)
12550 expanded = TREE_VEC_ELT (expanded, len - 1);
12551
12552 if (TYPE_P (expanded))
12553 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12554 complain & tf_error);
12555 else
12556 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12557 complain & tf_error);
12558 }
12559 else
12560 return build_int_cst (size_type_node, len);
12561 }
12562 if (SIZEOF_EXPR_TYPE_P (t))
12563 {
12564 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12565 args, complain, in_decl);
12566 r = build1 (NOP_EXPR, r, error_mark_node);
12567 r = build1 (SIZEOF_EXPR,
12568 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12569 SIZEOF_EXPR_TYPE_P (r) = 1;
12570 return r;
12571 }
12572 /* Fall through */
12573
12574 case INDIRECT_REF:
12575 case NEGATE_EXPR:
12576 case TRUTH_NOT_EXPR:
12577 case BIT_NOT_EXPR:
12578 case ADDR_EXPR:
12579 case UNARY_PLUS_EXPR: /* Unary + */
12580 case ALIGNOF_EXPR:
12581 case AT_ENCODE_EXPR:
12582 case ARROW_EXPR:
12583 case THROW_EXPR:
12584 case TYPEID_EXPR:
12585 case REALPART_EXPR:
12586 case IMAGPART_EXPR:
12587 case PAREN_EXPR:
12588 return build1
12589 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12590 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12591
12592 case COMPONENT_REF:
12593 {
12594 tree object;
12595 tree name;
12596
12597 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12598 name = TREE_OPERAND (t, 1);
12599 if (TREE_CODE (name) == BIT_NOT_EXPR)
12600 {
12601 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12602 complain, in_decl);
12603 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12604 }
12605 else if (TREE_CODE (name) == SCOPE_REF
12606 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12607 {
12608 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12609 complain, in_decl);
12610 name = TREE_OPERAND (name, 1);
12611 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12612 complain, in_decl);
12613 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12614 name = build_qualified_name (/*type=*/NULL_TREE,
12615 base, name,
12616 /*template_p=*/false);
12617 }
12618 else if (BASELINK_P (name))
12619 name = tsubst_baselink (name,
12620 non_reference (TREE_TYPE (object)),
12621 args, complain,
12622 in_decl);
12623 else
12624 name = tsubst_copy (name, args, complain, in_decl);
12625 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12626 }
12627
12628 case PLUS_EXPR:
12629 case MINUS_EXPR:
12630 case MULT_EXPR:
12631 case TRUNC_DIV_EXPR:
12632 case CEIL_DIV_EXPR:
12633 case FLOOR_DIV_EXPR:
12634 case ROUND_DIV_EXPR:
12635 case EXACT_DIV_EXPR:
12636 case BIT_AND_EXPR:
12637 case BIT_IOR_EXPR:
12638 case BIT_XOR_EXPR:
12639 case TRUNC_MOD_EXPR:
12640 case FLOOR_MOD_EXPR:
12641 case TRUTH_ANDIF_EXPR:
12642 case TRUTH_ORIF_EXPR:
12643 case TRUTH_AND_EXPR:
12644 case TRUTH_OR_EXPR:
12645 case RSHIFT_EXPR:
12646 case LSHIFT_EXPR:
12647 case RROTATE_EXPR:
12648 case LROTATE_EXPR:
12649 case EQ_EXPR:
12650 case NE_EXPR:
12651 case MAX_EXPR:
12652 case MIN_EXPR:
12653 case LE_EXPR:
12654 case GE_EXPR:
12655 case LT_EXPR:
12656 case GT_EXPR:
12657 case COMPOUND_EXPR:
12658 case DOTSTAR_EXPR:
12659 case MEMBER_REF:
12660 case PREDECREMENT_EXPR:
12661 case PREINCREMENT_EXPR:
12662 case POSTDECREMENT_EXPR:
12663 case POSTINCREMENT_EXPR:
12664 return build_nt
12665 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12666 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12667
12668 case SCOPE_REF:
12669 return build_qualified_name (/*type=*/NULL_TREE,
12670 tsubst_copy (TREE_OPERAND (t, 0),
12671 args, complain, in_decl),
12672 tsubst_copy (TREE_OPERAND (t, 1),
12673 args, complain, in_decl),
12674 QUALIFIED_NAME_IS_TEMPLATE (t));
12675
12676 case ARRAY_REF:
12677 return build_nt
12678 (ARRAY_REF,
12679 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12680 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12681 NULL_TREE, NULL_TREE);
12682
12683 case CALL_EXPR:
12684 {
12685 int n = VL_EXP_OPERAND_LENGTH (t);
12686 tree result = build_vl_exp (CALL_EXPR, n);
12687 int i;
12688 for (i = 0; i < n; i++)
12689 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12690 complain, in_decl);
12691 return result;
12692 }
12693
12694 case COND_EXPR:
12695 case MODOP_EXPR:
12696 case PSEUDO_DTOR_EXPR:
12697 case VEC_PERM_EXPR:
12698 {
12699 r = build_nt
12700 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12701 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12702 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12703 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12704 return r;
12705 }
12706
12707 case NEW_EXPR:
12708 {
12709 r = build_nt
12710 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12711 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12712 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12713 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12714 return r;
12715 }
12716
12717 case DELETE_EXPR:
12718 {
12719 r = build_nt
12720 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12721 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12722 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12723 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12724 return r;
12725 }
12726
12727 case TEMPLATE_ID_EXPR:
12728 {
12729 /* Substituted template arguments */
12730 tree fn = TREE_OPERAND (t, 0);
12731 tree targs = TREE_OPERAND (t, 1);
12732
12733 fn = tsubst_copy (fn, args, complain, in_decl);
12734 if (targs)
12735 targs = tsubst_template_args (targs, args, complain, in_decl);
12736
12737 return lookup_template_function (fn, targs);
12738 }
12739
12740 case TREE_LIST:
12741 {
12742 tree purpose, value, chain;
12743
12744 if (t == void_list_node)
12745 return t;
12746
12747 purpose = TREE_PURPOSE (t);
12748 if (purpose)
12749 purpose = tsubst_copy (purpose, args, complain, in_decl);
12750 value = TREE_VALUE (t);
12751 if (value)
12752 value = tsubst_copy (value, args, complain, in_decl);
12753 chain = TREE_CHAIN (t);
12754 if (chain && chain != void_type_node)
12755 chain = tsubst_copy (chain, args, complain, in_decl);
12756 if (purpose == TREE_PURPOSE (t)
12757 && value == TREE_VALUE (t)
12758 && chain == TREE_CHAIN (t))
12759 return t;
12760 return tree_cons (purpose, value, chain);
12761 }
12762
12763 case RECORD_TYPE:
12764 case UNION_TYPE:
12765 case ENUMERAL_TYPE:
12766 case INTEGER_TYPE:
12767 case TEMPLATE_TYPE_PARM:
12768 case TEMPLATE_TEMPLATE_PARM:
12769 case BOUND_TEMPLATE_TEMPLATE_PARM:
12770 case TEMPLATE_PARM_INDEX:
12771 case POINTER_TYPE:
12772 case REFERENCE_TYPE:
12773 case OFFSET_TYPE:
12774 case FUNCTION_TYPE:
12775 case METHOD_TYPE:
12776 case ARRAY_TYPE:
12777 case TYPENAME_TYPE:
12778 case UNBOUND_CLASS_TEMPLATE:
12779 case TYPEOF_TYPE:
12780 case DECLTYPE_TYPE:
12781 case TYPE_DECL:
12782 return tsubst (t, args, complain, in_decl);
12783
12784 case USING_DECL:
12785 t = DECL_NAME (t);
12786 /* Fall through. */
12787 case IDENTIFIER_NODE:
12788 if (IDENTIFIER_TYPENAME_P (t))
12789 {
12790 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12791 return mangle_conv_op_name_for_type (new_type);
12792 }
12793 else
12794 return t;
12795
12796 case CONSTRUCTOR:
12797 /* This is handled by tsubst_copy_and_build. */
12798 gcc_unreachable ();
12799
12800 case VA_ARG_EXPR:
12801 return build_x_va_arg (EXPR_LOCATION (t),
12802 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12803 in_decl),
12804 tsubst (TREE_TYPE (t), args, complain, in_decl));
12805
12806 case CLEANUP_POINT_EXPR:
12807 /* We shouldn't have built any of these during initial template
12808 generation. Instead, they should be built during instantiation
12809 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12810 gcc_unreachable ();
12811
12812 case OFFSET_REF:
12813 r = build2
12814 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12815 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12816 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12817 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12818 mark_used (TREE_OPERAND (r, 1));
12819 return r;
12820
12821 case EXPR_PACK_EXPANSION:
12822 error ("invalid use of pack expansion expression");
12823 return error_mark_node;
12824
12825 case NONTYPE_ARGUMENT_PACK:
12826 error ("use %<...%> to expand argument pack");
12827 return error_mark_node;
12828
12829 case INTEGER_CST:
12830 case REAL_CST:
12831 case STRING_CST:
12832 case COMPLEX_CST:
12833 {
12834 /* Instantiate any typedefs in the type. */
12835 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12836 r = fold_convert (type, t);
12837 gcc_assert (TREE_CODE (r) == code);
12838 return r;
12839 }
12840
12841 case PTRMEM_CST:
12842 /* These can sometimes show up in a partial instantiation, but never
12843 involve template parms. */
12844 gcc_assert (!uses_template_parms (t));
12845 return t;
12846
12847 default:
12848 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12849 gcc_checking_assert (false);
12850 return t;
12851 }
12852 }
12853
12854 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12855
12856 static tree
12857 tsubst_omp_clauses (tree clauses, bool declare_simd,
12858 tree args, tsubst_flags_t complain, tree in_decl)
12859 {
12860 tree new_clauses = NULL, nc, oc;
12861
12862 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12863 {
12864 nc = copy_node (oc);
12865 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12866 new_clauses = nc;
12867
12868 switch (OMP_CLAUSE_CODE (nc))
12869 {
12870 case OMP_CLAUSE_LASTPRIVATE:
12871 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12872 {
12873 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12874 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12875 in_decl, /*integral_constant_expression_p=*/false);
12876 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12877 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12878 }
12879 /* FALLTHRU */
12880 case OMP_CLAUSE_PRIVATE:
12881 case OMP_CLAUSE_SHARED:
12882 case OMP_CLAUSE_FIRSTPRIVATE:
12883 case OMP_CLAUSE_COPYIN:
12884 case OMP_CLAUSE_COPYPRIVATE:
12885 case OMP_CLAUSE_IF:
12886 case OMP_CLAUSE_NUM_THREADS:
12887 case OMP_CLAUSE_SCHEDULE:
12888 case OMP_CLAUSE_COLLAPSE:
12889 case OMP_CLAUSE_FINAL:
12890 case OMP_CLAUSE_DEPEND:
12891 case OMP_CLAUSE_FROM:
12892 case OMP_CLAUSE_TO:
12893 case OMP_CLAUSE_UNIFORM:
12894 case OMP_CLAUSE_MAP:
12895 case OMP_CLAUSE_DEVICE:
12896 case OMP_CLAUSE_DIST_SCHEDULE:
12897 case OMP_CLAUSE_NUM_TEAMS:
12898 case OMP_CLAUSE_THREAD_LIMIT:
12899 case OMP_CLAUSE_SAFELEN:
12900 case OMP_CLAUSE_SIMDLEN:
12901 OMP_CLAUSE_OPERAND (nc, 0)
12902 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12903 in_decl, /*integral_constant_expression_p=*/false);
12904 break;
12905 case OMP_CLAUSE_REDUCTION:
12906 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12907 {
12908 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12909 if (TREE_CODE (placeholder) == SCOPE_REF)
12910 {
12911 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12912 complain, in_decl);
12913 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12914 = build_qualified_name (NULL_TREE, scope,
12915 TREE_OPERAND (placeholder, 1),
12916 false);
12917 }
12918 else
12919 gcc_assert (identifier_p (placeholder));
12920 }
12921 OMP_CLAUSE_OPERAND (nc, 0)
12922 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12923 in_decl, /*integral_constant_expression_p=*/false);
12924 break;
12925 case OMP_CLAUSE_LINEAR:
12926 case OMP_CLAUSE_ALIGNED:
12927 OMP_CLAUSE_OPERAND (nc, 0)
12928 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12929 in_decl, /*integral_constant_expression_p=*/false);
12930 OMP_CLAUSE_OPERAND (nc, 1)
12931 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12932 in_decl, /*integral_constant_expression_p=*/false);
12933 break;
12934
12935 case OMP_CLAUSE_NOWAIT:
12936 case OMP_CLAUSE_ORDERED:
12937 case OMP_CLAUSE_DEFAULT:
12938 case OMP_CLAUSE_UNTIED:
12939 case OMP_CLAUSE_MERGEABLE:
12940 case OMP_CLAUSE_INBRANCH:
12941 case OMP_CLAUSE_NOTINBRANCH:
12942 case OMP_CLAUSE_PROC_BIND:
12943 case OMP_CLAUSE_FOR:
12944 case OMP_CLAUSE_PARALLEL:
12945 case OMP_CLAUSE_SECTIONS:
12946 case OMP_CLAUSE_TASKGROUP:
12947 break;
12948 default:
12949 gcc_unreachable ();
12950 }
12951 }
12952
12953 new_clauses = nreverse (new_clauses);
12954 if (!declare_simd)
12955 new_clauses = finish_omp_clauses (new_clauses);
12956 return new_clauses;
12957 }
12958
12959 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12960
12961 static tree
12962 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12963 tree in_decl)
12964 {
12965 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12966
12967 tree purpose, value, chain;
12968
12969 if (t == NULL)
12970 return t;
12971
12972 if (TREE_CODE (t) != TREE_LIST)
12973 return tsubst_copy_and_build (t, args, complain, in_decl,
12974 /*function_p=*/false,
12975 /*integral_constant_expression_p=*/false);
12976
12977 if (t == void_list_node)
12978 return t;
12979
12980 purpose = TREE_PURPOSE (t);
12981 if (purpose)
12982 purpose = RECUR (purpose);
12983 value = TREE_VALUE (t);
12984 if (value)
12985 {
12986 if (TREE_CODE (value) != LABEL_DECL)
12987 value = RECUR (value);
12988 else
12989 {
12990 value = lookup_label (DECL_NAME (value));
12991 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12992 TREE_USED (value) = 1;
12993 }
12994 }
12995 chain = TREE_CHAIN (t);
12996 if (chain && chain != void_type_node)
12997 chain = RECUR (chain);
12998 return tree_cons (purpose, value, chain);
12999 #undef RECUR
13000 }
13001
13002 /* Substitute one OMP_FOR iterator. */
13003
13004 static void
13005 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13006 tree condv, tree incrv, tree *clauses,
13007 tree args, tsubst_flags_t complain, tree in_decl,
13008 bool integral_constant_expression_p)
13009 {
13010 #define RECUR(NODE) \
13011 tsubst_expr ((NODE), args, complain, in_decl, \
13012 integral_constant_expression_p)
13013 tree decl, init, cond, incr;
13014 bool init_decl;
13015
13016 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13017 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13018 decl = TREE_OPERAND (init, 0);
13019 init = TREE_OPERAND (init, 1);
13020 /* Do this before substituting into decl to handle 'auto'. */
13021 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13022 init = RECUR (init);
13023 decl = RECUR (decl);
13024 if (init_decl)
13025 {
13026 gcc_assert (!processing_template_decl);
13027 init = DECL_INITIAL (decl);
13028 DECL_INITIAL (decl) = NULL_TREE;
13029 }
13030
13031 gcc_assert (!type_dependent_expression_p (decl));
13032
13033 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13034 {
13035 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13036 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13037 if (TREE_CODE (incr) == MODIFY_EXPR)
13038 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13039 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13040 RECUR (TREE_OPERAND (incr, 1)),
13041 complain);
13042 else
13043 incr = RECUR (incr);
13044 TREE_VEC_ELT (declv, i) = decl;
13045 TREE_VEC_ELT (initv, i) = init;
13046 TREE_VEC_ELT (condv, i) = cond;
13047 TREE_VEC_ELT (incrv, i) = incr;
13048 return;
13049 }
13050
13051 if (init && !init_decl)
13052 {
13053 tree c;
13054 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13055 {
13056 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13057 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13058 && OMP_CLAUSE_DECL (c) == decl)
13059 break;
13060 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13061 && OMP_CLAUSE_DECL (c) == decl)
13062 error ("iteration variable %qD should not be firstprivate", decl);
13063 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13064 && OMP_CLAUSE_DECL (c) == decl)
13065 error ("iteration variable %qD should not be reduction", decl);
13066 }
13067 if (c == NULL)
13068 {
13069 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13070 OMP_CLAUSE_DECL (c) = decl;
13071 c = finish_omp_clauses (c);
13072 if (c)
13073 {
13074 OMP_CLAUSE_CHAIN (c) = *clauses;
13075 *clauses = c;
13076 }
13077 }
13078 }
13079 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13080 if (COMPARISON_CLASS_P (cond))
13081 cond = build2 (TREE_CODE (cond), boolean_type_node,
13082 RECUR (TREE_OPERAND (cond, 0)),
13083 RECUR (TREE_OPERAND (cond, 1)));
13084 else
13085 cond = RECUR (cond);
13086 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13087 switch (TREE_CODE (incr))
13088 {
13089 case PREINCREMENT_EXPR:
13090 case PREDECREMENT_EXPR:
13091 case POSTINCREMENT_EXPR:
13092 case POSTDECREMENT_EXPR:
13093 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13094 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13095 break;
13096 case MODIFY_EXPR:
13097 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13098 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13099 {
13100 tree rhs = TREE_OPERAND (incr, 1);
13101 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13102 RECUR (TREE_OPERAND (incr, 0)),
13103 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13104 RECUR (TREE_OPERAND (rhs, 0)),
13105 RECUR (TREE_OPERAND (rhs, 1))));
13106 }
13107 else
13108 incr = RECUR (incr);
13109 break;
13110 case MODOP_EXPR:
13111 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13112 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13113 {
13114 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13115 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13116 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13117 TREE_TYPE (decl), lhs,
13118 RECUR (TREE_OPERAND (incr, 2))));
13119 }
13120 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13121 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13122 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13123 {
13124 tree rhs = TREE_OPERAND (incr, 2);
13125 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13126 RECUR (TREE_OPERAND (incr, 0)),
13127 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13128 RECUR (TREE_OPERAND (rhs, 0)),
13129 RECUR (TREE_OPERAND (rhs, 1))));
13130 }
13131 else
13132 incr = RECUR (incr);
13133 break;
13134 default:
13135 incr = RECUR (incr);
13136 break;
13137 }
13138
13139 TREE_VEC_ELT (declv, i) = decl;
13140 TREE_VEC_ELT (initv, i) = init;
13141 TREE_VEC_ELT (condv, i) = cond;
13142 TREE_VEC_ELT (incrv, i) = incr;
13143 #undef RECUR
13144 }
13145
13146 /* Like tsubst_copy for expressions, etc. but also does semantic
13147 processing. */
13148
13149 static tree
13150 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13151 bool integral_constant_expression_p)
13152 {
13153 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13154 #define RECUR(NODE) \
13155 tsubst_expr ((NODE), args, complain, in_decl, \
13156 integral_constant_expression_p)
13157
13158 tree stmt, tmp;
13159 tree r;
13160 location_t loc;
13161
13162 if (t == NULL_TREE || t == error_mark_node)
13163 return t;
13164
13165 loc = input_location;
13166 if (EXPR_HAS_LOCATION (t))
13167 input_location = EXPR_LOCATION (t);
13168 if (STATEMENT_CODE_P (TREE_CODE (t)))
13169 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13170
13171 switch (TREE_CODE (t))
13172 {
13173 case STATEMENT_LIST:
13174 {
13175 tree_stmt_iterator i;
13176 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13177 RECUR (tsi_stmt (i));
13178 break;
13179 }
13180
13181 case CTOR_INITIALIZER:
13182 finish_mem_initializers (tsubst_initializer_list
13183 (TREE_OPERAND (t, 0), args));
13184 break;
13185
13186 case RETURN_EXPR:
13187 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13188 break;
13189
13190 case EXPR_STMT:
13191 tmp = RECUR (EXPR_STMT_EXPR (t));
13192 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13193 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13194 else
13195 finish_expr_stmt (tmp);
13196 break;
13197
13198 case USING_STMT:
13199 do_using_directive (USING_STMT_NAMESPACE (t));
13200 break;
13201
13202 case DECL_EXPR:
13203 {
13204 tree decl, pattern_decl;
13205 tree init;
13206
13207 pattern_decl = decl = DECL_EXPR_DECL (t);
13208 if (TREE_CODE (decl) == LABEL_DECL)
13209 finish_label_decl (DECL_NAME (decl));
13210 else if (TREE_CODE (decl) == USING_DECL)
13211 {
13212 tree scope = USING_DECL_SCOPE (decl);
13213 tree name = DECL_NAME (decl);
13214 tree decl;
13215
13216 scope = tsubst (scope, args, complain, in_decl);
13217 decl = lookup_qualified_name (scope, name,
13218 /*is_type_p=*/false,
13219 /*complain=*/false);
13220 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13221 qualified_name_lookup_error (scope, name, decl, input_location);
13222 else
13223 do_local_using_decl (decl, scope, name);
13224 }
13225 else if (DECL_PACK_P (decl))
13226 {
13227 /* Don't build up decls for a variadic capture proxy, we'll
13228 instantiate the elements directly as needed. */
13229 break;
13230 }
13231 else
13232 {
13233 init = DECL_INITIAL (decl);
13234 decl = tsubst (decl, args, complain, in_decl);
13235 if (decl != error_mark_node)
13236 {
13237 /* By marking the declaration as instantiated, we avoid
13238 trying to instantiate it. Since instantiate_decl can't
13239 handle local variables, and since we've already done
13240 all that needs to be done, that's the right thing to
13241 do. */
13242 if (VAR_P (decl))
13243 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13244 if (VAR_P (decl)
13245 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13246 /* Anonymous aggregates are a special case. */
13247 finish_anon_union (decl);
13248 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13249 {
13250 DECL_CONTEXT (decl) = current_function_decl;
13251 if (DECL_NAME (decl) == this_identifier)
13252 {
13253 tree lam = DECL_CONTEXT (current_function_decl);
13254 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13255 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13256 }
13257 insert_capture_proxy (decl);
13258 }
13259 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13260 /* We already did a pushtag. */;
13261 else if (TREE_CODE (decl) == FUNCTION_DECL
13262 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13263 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13264 {
13265 DECL_CONTEXT (decl) = NULL_TREE;
13266 pushdecl (decl);
13267 DECL_CONTEXT (decl) = current_function_decl;
13268 cp_check_omp_declare_reduction (decl);
13269 }
13270 else
13271 {
13272 int const_init = false;
13273 maybe_push_decl (decl);
13274 if (VAR_P (decl)
13275 && DECL_PRETTY_FUNCTION_P (decl))
13276 {
13277 /* For __PRETTY_FUNCTION__ we have to adjust the
13278 initializer. */
13279 const char *const name
13280 = cxx_printable_name (current_function_decl, 2);
13281 init = cp_fname_init (name, &TREE_TYPE (decl));
13282 }
13283 else
13284 {
13285 tree t = RECUR (init);
13286
13287 if (init && !t)
13288 {
13289 /* If we had an initializer but it
13290 instantiated to nothing,
13291 value-initialize the object. This will
13292 only occur when the initializer was a
13293 pack expansion where the parameter packs
13294 used in that expansion were of length
13295 zero. */
13296 init = build_value_init (TREE_TYPE (decl),
13297 complain);
13298 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13299 init = get_target_expr_sfinae (init, complain);
13300 }
13301 else
13302 init = t;
13303 }
13304
13305 if (VAR_P (decl))
13306 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13307 (pattern_decl));
13308 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13309 }
13310 }
13311 }
13312
13313 break;
13314 }
13315
13316 case FOR_STMT:
13317 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13318 RECUR (FOR_INIT_STMT (t));
13319 finish_for_init_stmt (stmt);
13320 tmp = RECUR (FOR_COND (t));
13321 finish_for_cond (tmp, stmt, false);
13322 tmp = RECUR (FOR_EXPR (t));
13323 finish_for_expr (tmp, stmt);
13324 RECUR (FOR_BODY (t));
13325 finish_for_stmt (stmt);
13326 break;
13327
13328 case RANGE_FOR_STMT:
13329 {
13330 tree decl, expr;
13331 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13332 decl = RANGE_FOR_DECL (t);
13333 decl = tsubst (decl, args, complain, in_decl);
13334 maybe_push_decl (decl);
13335 expr = RECUR (RANGE_FOR_EXPR (t));
13336 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13337 RECUR (RANGE_FOR_BODY (t));
13338 finish_for_stmt (stmt);
13339 }
13340 break;
13341
13342 case WHILE_STMT:
13343 stmt = begin_while_stmt ();
13344 tmp = RECUR (WHILE_COND (t));
13345 finish_while_stmt_cond (tmp, stmt, false);
13346 RECUR (WHILE_BODY (t));
13347 finish_while_stmt (stmt);
13348 break;
13349
13350 case DO_STMT:
13351 stmt = begin_do_stmt ();
13352 RECUR (DO_BODY (t));
13353 finish_do_body (stmt);
13354 tmp = RECUR (DO_COND (t));
13355 finish_do_stmt (tmp, stmt, false);
13356 break;
13357
13358 case IF_STMT:
13359 stmt = begin_if_stmt ();
13360 tmp = RECUR (IF_COND (t));
13361 finish_if_stmt_cond (tmp, stmt);
13362 RECUR (THEN_CLAUSE (t));
13363 finish_then_clause (stmt);
13364
13365 if (ELSE_CLAUSE (t))
13366 {
13367 begin_else_clause (stmt);
13368 RECUR (ELSE_CLAUSE (t));
13369 finish_else_clause (stmt);
13370 }
13371
13372 finish_if_stmt (stmt);
13373 break;
13374
13375 case BIND_EXPR:
13376 if (BIND_EXPR_BODY_BLOCK (t))
13377 stmt = begin_function_body ();
13378 else
13379 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13380 ? BCS_TRY_BLOCK : 0);
13381
13382 RECUR (BIND_EXPR_BODY (t));
13383
13384 if (BIND_EXPR_BODY_BLOCK (t))
13385 finish_function_body (stmt);
13386 else
13387 finish_compound_stmt (stmt);
13388 break;
13389
13390 case BREAK_STMT:
13391 finish_break_stmt ();
13392 break;
13393
13394 case CONTINUE_STMT:
13395 finish_continue_stmt ();
13396 break;
13397
13398 case SWITCH_STMT:
13399 stmt = begin_switch_stmt ();
13400 tmp = RECUR (SWITCH_STMT_COND (t));
13401 finish_switch_cond (tmp, stmt);
13402 RECUR (SWITCH_STMT_BODY (t));
13403 finish_switch_stmt (stmt);
13404 break;
13405
13406 case CASE_LABEL_EXPR:
13407 finish_case_label (EXPR_LOCATION (t),
13408 RECUR (CASE_LOW (t)),
13409 RECUR (CASE_HIGH (t)));
13410 break;
13411
13412 case LABEL_EXPR:
13413 {
13414 tree decl = LABEL_EXPR_LABEL (t);
13415 tree label;
13416
13417 label = finish_label_stmt (DECL_NAME (decl));
13418 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13419 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13420 }
13421 break;
13422
13423 case GOTO_EXPR:
13424 tmp = GOTO_DESTINATION (t);
13425 if (TREE_CODE (tmp) != LABEL_DECL)
13426 /* Computed goto's must be tsubst'd into. On the other hand,
13427 non-computed gotos must not be; the identifier in question
13428 will have no binding. */
13429 tmp = RECUR (tmp);
13430 else
13431 tmp = DECL_NAME (tmp);
13432 finish_goto_stmt (tmp);
13433 break;
13434
13435 case ASM_EXPR:
13436 tmp = finish_asm_stmt
13437 (ASM_VOLATILE_P (t),
13438 RECUR (ASM_STRING (t)),
13439 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13440 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13441 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13442 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13443 {
13444 tree asm_expr = tmp;
13445 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13446 asm_expr = TREE_OPERAND (asm_expr, 0);
13447 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13448 }
13449 break;
13450
13451 case TRY_BLOCK:
13452 if (CLEANUP_P (t))
13453 {
13454 stmt = begin_try_block ();
13455 RECUR (TRY_STMTS (t));
13456 finish_cleanup_try_block (stmt);
13457 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13458 }
13459 else
13460 {
13461 tree compound_stmt = NULL_TREE;
13462
13463 if (FN_TRY_BLOCK_P (t))
13464 stmt = begin_function_try_block (&compound_stmt);
13465 else
13466 stmt = begin_try_block ();
13467
13468 RECUR (TRY_STMTS (t));
13469
13470 if (FN_TRY_BLOCK_P (t))
13471 finish_function_try_block (stmt);
13472 else
13473 finish_try_block (stmt);
13474
13475 RECUR (TRY_HANDLERS (t));
13476 if (FN_TRY_BLOCK_P (t))
13477 finish_function_handler_sequence (stmt, compound_stmt);
13478 else
13479 finish_handler_sequence (stmt);
13480 }
13481 break;
13482
13483 case HANDLER:
13484 {
13485 tree decl = HANDLER_PARMS (t);
13486
13487 if (decl)
13488 {
13489 decl = tsubst (decl, args, complain, in_decl);
13490 /* Prevent instantiate_decl from trying to instantiate
13491 this variable. We've already done all that needs to be
13492 done. */
13493 if (decl != error_mark_node)
13494 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13495 }
13496 stmt = begin_handler ();
13497 finish_handler_parms (decl, stmt);
13498 RECUR (HANDLER_BODY (t));
13499 finish_handler (stmt);
13500 }
13501 break;
13502
13503 case TAG_DEFN:
13504 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13505 if (CLASS_TYPE_P (tmp))
13506 {
13507 /* Local classes are not independent templates; they are
13508 instantiated along with their containing function. And this
13509 way we don't have to deal with pushing out of one local class
13510 to instantiate a member of another local class. */
13511 tree fn;
13512 /* Closures are handled by the LAMBDA_EXPR. */
13513 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13514 complete_type (tmp);
13515 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13516 if (!DECL_ARTIFICIAL (fn))
13517 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13518 }
13519 break;
13520
13521 case STATIC_ASSERT:
13522 {
13523 tree condition;
13524
13525 ++c_inhibit_evaluation_warnings;
13526 condition =
13527 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13528 args,
13529 complain, in_decl,
13530 /*integral_constant_expression_p=*/true);
13531 --c_inhibit_evaluation_warnings;
13532
13533 finish_static_assert (condition,
13534 STATIC_ASSERT_MESSAGE (t),
13535 STATIC_ASSERT_SOURCE_LOCATION (t),
13536 /*member_p=*/false);
13537 }
13538 break;
13539
13540 case OMP_PARALLEL:
13541 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13542 args, complain, in_decl);
13543 stmt = begin_omp_parallel ();
13544 RECUR (OMP_PARALLEL_BODY (t));
13545 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13546 = OMP_PARALLEL_COMBINED (t);
13547 break;
13548
13549 case OMP_TASK:
13550 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13551 args, complain, in_decl);
13552 stmt = begin_omp_task ();
13553 RECUR (OMP_TASK_BODY (t));
13554 finish_omp_task (tmp, stmt);
13555 break;
13556
13557 case OMP_FOR:
13558 case OMP_SIMD:
13559 case OMP_DISTRIBUTE:
13560 {
13561 tree clauses, body, pre_body;
13562 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13563 tree incrv = NULL_TREE;
13564 int i;
13565
13566 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13567 args, complain, in_decl);
13568 if (OMP_FOR_INIT (t) != NULL_TREE)
13569 {
13570 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13571 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13572 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13573 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13574 }
13575
13576 stmt = begin_omp_structured_block ();
13577
13578 pre_body = push_stmt_list ();
13579 RECUR (OMP_FOR_PRE_BODY (t));
13580 pre_body = pop_stmt_list (pre_body);
13581
13582 if (OMP_FOR_INIT (t) != NULL_TREE)
13583 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13584 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13585 &clauses, args, complain, in_decl,
13586 integral_constant_expression_p);
13587
13588 body = push_stmt_list ();
13589 RECUR (OMP_FOR_BODY (t));
13590 body = pop_stmt_list (body);
13591
13592 if (OMP_FOR_INIT (t) != NULL_TREE)
13593 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13594 condv, incrv, body, pre_body, clauses);
13595 else
13596 {
13597 t = make_node (TREE_CODE (t));
13598 TREE_TYPE (t) = void_type_node;
13599 OMP_FOR_BODY (t) = body;
13600 OMP_FOR_PRE_BODY (t) = pre_body;
13601 OMP_FOR_CLAUSES (t) = clauses;
13602 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13603 add_stmt (t);
13604 }
13605
13606 add_stmt (finish_omp_structured_block (stmt));
13607 }
13608 break;
13609
13610 case OMP_SECTIONS:
13611 case OMP_SINGLE:
13612 case OMP_TEAMS:
13613 case OMP_TARGET_DATA:
13614 case OMP_TARGET:
13615 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13616 args, complain, in_decl);
13617 stmt = push_stmt_list ();
13618 RECUR (OMP_BODY (t));
13619 stmt = pop_stmt_list (stmt);
13620
13621 t = copy_node (t);
13622 OMP_BODY (t) = stmt;
13623 OMP_CLAUSES (t) = tmp;
13624 add_stmt (t);
13625 break;
13626
13627 case OMP_TARGET_UPDATE:
13628 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13629 args, complain, in_decl);
13630 t = copy_node (t);
13631 OMP_CLAUSES (t) = tmp;
13632 add_stmt (t);
13633 break;
13634
13635 case OMP_SECTION:
13636 case OMP_CRITICAL:
13637 case OMP_MASTER:
13638 case OMP_TASKGROUP:
13639 case OMP_ORDERED:
13640 stmt = push_stmt_list ();
13641 RECUR (OMP_BODY (t));
13642 stmt = pop_stmt_list (stmt);
13643
13644 t = copy_node (t);
13645 OMP_BODY (t) = stmt;
13646 add_stmt (t);
13647 break;
13648
13649 case OMP_ATOMIC:
13650 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13651 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13652 {
13653 tree op1 = TREE_OPERAND (t, 1);
13654 tree rhs1 = NULL_TREE;
13655 tree lhs, rhs;
13656 if (TREE_CODE (op1) == COMPOUND_EXPR)
13657 {
13658 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13659 op1 = TREE_OPERAND (op1, 1);
13660 }
13661 lhs = RECUR (TREE_OPERAND (op1, 0));
13662 rhs = RECUR (TREE_OPERAND (op1, 1));
13663 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13664 NULL_TREE, NULL_TREE, rhs1,
13665 OMP_ATOMIC_SEQ_CST (t));
13666 }
13667 else
13668 {
13669 tree op1 = TREE_OPERAND (t, 1);
13670 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13671 tree rhs1 = NULL_TREE;
13672 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13673 enum tree_code opcode = NOP_EXPR;
13674 if (code == OMP_ATOMIC_READ)
13675 {
13676 v = RECUR (TREE_OPERAND (op1, 0));
13677 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13678 }
13679 else if (code == OMP_ATOMIC_CAPTURE_OLD
13680 || code == OMP_ATOMIC_CAPTURE_NEW)
13681 {
13682 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13683 v = RECUR (TREE_OPERAND (op1, 0));
13684 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13685 if (TREE_CODE (op11) == COMPOUND_EXPR)
13686 {
13687 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13688 op11 = TREE_OPERAND (op11, 1);
13689 }
13690 lhs = RECUR (TREE_OPERAND (op11, 0));
13691 rhs = RECUR (TREE_OPERAND (op11, 1));
13692 opcode = TREE_CODE (op11);
13693 if (opcode == MODIFY_EXPR)
13694 opcode = NOP_EXPR;
13695 }
13696 else
13697 {
13698 code = OMP_ATOMIC;
13699 lhs = RECUR (TREE_OPERAND (op1, 0));
13700 rhs = RECUR (TREE_OPERAND (op1, 1));
13701 }
13702 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13703 OMP_ATOMIC_SEQ_CST (t));
13704 }
13705 break;
13706
13707 case TRANSACTION_EXPR:
13708 {
13709 int flags = 0;
13710 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13711 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13712
13713 if (TRANSACTION_EXPR_IS_STMT (t))
13714 {
13715 tree body = TRANSACTION_EXPR_BODY (t);
13716 tree noex = NULL_TREE;
13717 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13718 {
13719 noex = MUST_NOT_THROW_COND (body);
13720 if (noex == NULL_TREE)
13721 noex = boolean_true_node;
13722 body = TREE_OPERAND (body, 0);
13723 }
13724 stmt = begin_transaction_stmt (input_location, NULL, flags);
13725 RECUR (body);
13726 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13727 }
13728 else
13729 {
13730 stmt = build_transaction_expr (EXPR_LOCATION (t),
13731 RECUR (TRANSACTION_EXPR_BODY (t)),
13732 flags, NULL_TREE);
13733 RETURN (stmt);
13734 }
13735 }
13736 break;
13737
13738 case MUST_NOT_THROW_EXPR:
13739 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13740 RECUR (MUST_NOT_THROW_COND (t))));
13741
13742 case EXPR_PACK_EXPANSION:
13743 error ("invalid use of pack expansion expression");
13744 RETURN (error_mark_node);
13745
13746 case NONTYPE_ARGUMENT_PACK:
13747 error ("use %<...%> to expand argument pack");
13748 RETURN (error_mark_node);
13749
13750 case COMPOUND_EXPR:
13751 tmp = RECUR (TREE_OPERAND (t, 0));
13752 if (tmp == NULL_TREE)
13753 /* If the first operand was a statement, we're done with it. */
13754 RETURN (RECUR (TREE_OPERAND (t, 1)));
13755 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13756 RECUR (TREE_OPERAND (t, 1)),
13757 complain));
13758
13759 default:
13760 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13761
13762 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13763 /*function_p=*/false,
13764 integral_constant_expression_p));
13765 }
13766
13767 RETURN (NULL_TREE);
13768 out:
13769 input_location = loc;
13770 return r;
13771 #undef RECUR
13772 #undef RETURN
13773 }
13774
13775 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13776 function. For description of the body see comment above
13777 cp_parser_omp_declare_reduction_exprs. */
13778
13779 static void
13780 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13781 {
13782 if (t == NULL_TREE || t == error_mark_node)
13783 return;
13784
13785 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13786
13787 tree_stmt_iterator tsi;
13788 int i;
13789 tree stmts[7];
13790 memset (stmts, 0, sizeof stmts);
13791 for (i = 0, tsi = tsi_start (t);
13792 i < 7 && !tsi_end_p (tsi);
13793 i++, tsi_next (&tsi))
13794 stmts[i] = tsi_stmt (tsi);
13795 gcc_assert (tsi_end_p (tsi));
13796
13797 if (i >= 3)
13798 {
13799 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13800 && TREE_CODE (stmts[1]) == DECL_EXPR);
13801 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13802 args, complain, in_decl);
13803 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13804 args, complain, in_decl);
13805 DECL_CONTEXT (omp_out) = current_function_decl;
13806 DECL_CONTEXT (omp_in) = current_function_decl;
13807 keep_next_level (true);
13808 tree block = begin_omp_structured_block ();
13809 tsubst_expr (stmts[2], args, complain, in_decl, false);
13810 block = finish_omp_structured_block (block);
13811 block = maybe_cleanup_point_expr_void (block);
13812 add_decl_expr (omp_out);
13813 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13814 TREE_NO_WARNING (omp_out) = 1;
13815 add_decl_expr (omp_in);
13816 finish_expr_stmt (block);
13817 }
13818 if (i >= 6)
13819 {
13820 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13821 && TREE_CODE (stmts[4]) == DECL_EXPR);
13822 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13823 args, complain, in_decl);
13824 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13825 args, complain, in_decl);
13826 DECL_CONTEXT (omp_priv) = current_function_decl;
13827 DECL_CONTEXT (omp_orig) = current_function_decl;
13828 keep_next_level (true);
13829 tree block = begin_omp_structured_block ();
13830 tsubst_expr (stmts[5], args, complain, in_decl, false);
13831 block = finish_omp_structured_block (block);
13832 block = maybe_cleanup_point_expr_void (block);
13833 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13834 add_decl_expr (omp_priv);
13835 add_decl_expr (omp_orig);
13836 finish_expr_stmt (block);
13837 if (i == 7)
13838 add_decl_expr (omp_orig);
13839 }
13840 }
13841
13842 /* T is a postfix-expression that is not being used in a function
13843 call. Return the substituted version of T. */
13844
13845 static tree
13846 tsubst_non_call_postfix_expression (tree t, tree args,
13847 tsubst_flags_t complain,
13848 tree in_decl)
13849 {
13850 if (TREE_CODE (t) == SCOPE_REF)
13851 t = tsubst_qualified_id (t, args, complain, in_decl,
13852 /*done=*/false, /*address_p=*/false);
13853 else
13854 t = tsubst_copy_and_build (t, args, complain, in_decl,
13855 /*function_p=*/false,
13856 /*integral_constant_expression_p=*/false);
13857
13858 return t;
13859 }
13860
13861 /* Like tsubst but deals with expressions and performs semantic
13862 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13863
13864 tree
13865 tsubst_copy_and_build (tree t,
13866 tree args,
13867 tsubst_flags_t complain,
13868 tree in_decl,
13869 bool function_p,
13870 bool integral_constant_expression_p)
13871 {
13872 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13873 #define RECUR(NODE) \
13874 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13875 /*function_p=*/false, \
13876 integral_constant_expression_p)
13877
13878 tree retval, op1;
13879 location_t loc;
13880
13881 if (t == NULL_TREE || t == error_mark_node)
13882 return t;
13883
13884 loc = input_location;
13885 if (EXPR_HAS_LOCATION (t))
13886 input_location = EXPR_LOCATION (t);
13887
13888 /* N3276 decltype magic only applies to calls at the top level or on the
13889 right side of a comma. */
13890 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13891 complain &= ~tf_decltype;
13892
13893 switch (TREE_CODE (t))
13894 {
13895 case USING_DECL:
13896 t = DECL_NAME (t);
13897 /* Fall through. */
13898 case IDENTIFIER_NODE:
13899 {
13900 tree decl;
13901 cp_id_kind idk;
13902 bool non_integral_constant_expression_p;
13903 const char *error_msg;
13904
13905 if (IDENTIFIER_TYPENAME_P (t))
13906 {
13907 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13908 t = mangle_conv_op_name_for_type (new_type);
13909 }
13910
13911 /* Look up the name. */
13912 decl = lookup_name (t);
13913
13914 /* By convention, expressions use ERROR_MARK_NODE to indicate
13915 failure, not NULL_TREE. */
13916 if (decl == NULL_TREE)
13917 decl = error_mark_node;
13918
13919 decl = finish_id_expression (t, decl, NULL_TREE,
13920 &idk,
13921 integral_constant_expression_p,
13922 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13923 &non_integral_constant_expression_p,
13924 /*template_p=*/false,
13925 /*done=*/true,
13926 /*address_p=*/false,
13927 /*template_arg_p=*/false,
13928 &error_msg,
13929 input_location);
13930 if (error_msg)
13931 error (error_msg);
13932 if (!function_p && identifier_p (decl))
13933 {
13934 if (complain & tf_error)
13935 unqualified_name_lookup_error (decl);
13936 decl = error_mark_node;
13937 }
13938 RETURN (decl);
13939 }
13940
13941 case TEMPLATE_ID_EXPR:
13942 {
13943 tree object;
13944 tree templ = RECUR (TREE_OPERAND (t, 0));
13945 tree targs = TREE_OPERAND (t, 1);
13946
13947 if (targs)
13948 targs = tsubst_template_args (targs, args, complain, in_decl);
13949
13950 if (TREE_CODE (templ) == COMPONENT_REF)
13951 {
13952 object = TREE_OPERAND (templ, 0);
13953 templ = TREE_OPERAND (templ, 1);
13954 }
13955 else
13956 object = NULL_TREE;
13957 templ = lookup_template_function (templ, targs);
13958
13959 if (object)
13960 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13961 object, templ, NULL_TREE));
13962 else
13963 RETURN (baselink_for_fns (templ));
13964 }
13965
13966 case INDIRECT_REF:
13967 {
13968 tree r = RECUR (TREE_OPERAND (t, 0));
13969
13970 if (REFERENCE_REF_P (t))
13971 {
13972 /* A type conversion to reference type will be enclosed in
13973 such an indirect ref, but the substitution of the cast
13974 will have also added such an indirect ref. */
13975 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13976 r = convert_from_reference (r);
13977 }
13978 else
13979 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13980 complain|decltype_flag);
13981 RETURN (r);
13982 }
13983
13984 case NOP_EXPR:
13985 RETURN (build_nop
13986 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13987 RECUR (TREE_OPERAND (t, 0))));
13988
13989 case IMPLICIT_CONV_EXPR:
13990 {
13991 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13992 tree expr = RECUR (TREE_OPERAND (t, 0));
13993 int flags = LOOKUP_IMPLICIT;
13994 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13995 flags = LOOKUP_NORMAL;
13996 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13997 flags));
13998 }
13999
14000 case CONVERT_EXPR:
14001 RETURN (build1
14002 (CONVERT_EXPR,
14003 tsubst (TREE_TYPE (t), args, complain, in_decl),
14004 RECUR (TREE_OPERAND (t, 0))));
14005
14006 case CAST_EXPR:
14007 case REINTERPRET_CAST_EXPR:
14008 case CONST_CAST_EXPR:
14009 case DYNAMIC_CAST_EXPR:
14010 case STATIC_CAST_EXPR:
14011 {
14012 tree type;
14013 tree op, r = NULL_TREE;
14014
14015 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14016 if (integral_constant_expression_p
14017 && !cast_valid_in_integral_constant_expression_p (type))
14018 {
14019 if (complain & tf_error)
14020 error ("a cast to a type other than an integral or "
14021 "enumeration type cannot appear in a constant-expression");
14022 RETURN (error_mark_node);
14023 }
14024
14025 op = RECUR (TREE_OPERAND (t, 0));
14026
14027 ++c_inhibit_evaluation_warnings;
14028 switch (TREE_CODE (t))
14029 {
14030 case CAST_EXPR:
14031 r = build_functional_cast (type, op, complain);
14032 break;
14033 case REINTERPRET_CAST_EXPR:
14034 r = build_reinterpret_cast (type, op, complain);
14035 break;
14036 case CONST_CAST_EXPR:
14037 r = build_const_cast (type, op, complain);
14038 break;
14039 case DYNAMIC_CAST_EXPR:
14040 r = build_dynamic_cast (type, op, complain);
14041 break;
14042 case STATIC_CAST_EXPR:
14043 r = build_static_cast (type, op, complain);
14044 break;
14045 default:
14046 gcc_unreachable ();
14047 }
14048 --c_inhibit_evaluation_warnings;
14049
14050 RETURN (r);
14051 }
14052
14053 case POSTDECREMENT_EXPR:
14054 case POSTINCREMENT_EXPR:
14055 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14056 args, complain, in_decl);
14057 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14058 complain|decltype_flag));
14059
14060 case PREDECREMENT_EXPR:
14061 case PREINCREMENT_EXPR:
14062 case NEGATE_EXPR:
14063 case BIT_NOT_EXPR:
14064 case ABS_EXPR:
14065 case TRUTH_NOT_EXPR:
14066 case UNARY_PLUS_EXPR: /* Unary + */
14067 case REALPART_EXPR:
14068 case IMAGPART_EXPR:
14069 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14070 RECUR (TREE_OPERAND (t, 0)),
14071 complain|decltype_flag));
14072
14073 case FIX_TRUNC_EXPR:
14074 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14075 0, complain));
14076
14077 case ADDR_EXPR:
14078 op1 = TREE_OPERAND (t, 0);
14079 if (TREE_CODE (op1) == LABEL_DECL)
14080 RETURN (finish_label_address_expr (DECL_NAME (op1),
14081 EXPR_LOCATION (op1)));
14082 if (TREE_CODE (op1) == SCOPE_REF)
14083 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14084 /*done=*/true, /*address_p=*/true);
14085 else
14086 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14087 in_decl);
14088 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14089 complain|decltype_flag));
14090
14091 case PLUS_EXPR:
14092 case MINUS_EXPR:
14093 case MULT_EXPR:
14094 case TRUNC_DIV_EXPR:
14095 case CEIL_DIV_EXPR:
14096 case FLOOR_DIV_EXPR:
14097 case ROUND_DIV_EXPR:
14098 case EXACT_DIV_EXPR:
14099 case BIT_AND_EXPR:
14100 case BIT_IOR_EXPR:
14101 case BIT_XOR_EXPR:
14102 case TRUNC_MOD_EXPR:
14103 case FLOOR_MOD_EXPR:
14104 case TRUTH_ANDIF_EXPR:
14105 case TRUTH_ORIF_EXPR:
14106 case TRUTH_AND_EXPR:
14107 case TRUTH_OR_EXPR:
14108 case RSHIFT_EXPR:
14109 case LSHIFT_EXPR:
14110 case RROTATE_EXPR:
14111 case LROTATE_EXPR:
14112 case EQ_EXPR:
14113 case NE_EXPR:
14114 case MAX_EXPR:
14115 case MIN_EXPR:
14116 case LE_EXPR:
14117 case GE_EXPR:
14118 case LT_EXPR:
14119 case GT_EXPR:
14120 case MEMBER_REF:
14121 case DOTSTAR_EXPR:
14122 {
14123 tree r;
14124
14125 ++c_inhibit_evaluation_warnings;
14126
14127 r = build_x_binary_op
14128 (input_location, TREE_CODE (t),
14129 RECUR (TREE_OPERAND (t, 0)),
14130 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14131 ? ERROR_MARK
14132 : TREE_CODE (TREE_OPERAND (t, 0))),
14133 RECUR (TREE_OPERAND (t, 1)),
14134 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14135 ? ERROR_MARK
14136 : TREE_CODE (TREE_OPERAND (t, 1))),
14137 /*overload=*/NULL,
14138 complain|decltype_flag);
14139 if (EXPR_P (r) && TREE_NO_WARNING (t))
14140 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14141
14142 --c_inhibit_evaluation_warnings;
14143
14144 RETURN (r);
14145 }
14146
14147 case SCOPE_REF:
14148 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14149 /*address_p=*/false));
14150 case ARRAY_REF:
14151 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14152 args, complain, in_decl);
14153 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14154 RECUR (TREE_OPERAND (t, 1)),
14155 complain|decltype_flag));
14156
14157 case ARRAY_NOTATION_REF:
14158 {
14159 tree start_index, length, stride;
14160 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14161 args, complain, in_decl);
14162 start_index = RECUR (ARRAY_NOTATION_START (t));
14163 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14164 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14165 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14166 length, stride, TREE_TYPE (op1)));
14167 }
14168 case SIZEOF_EXPR:
14169 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14170 RETURN (tsubst_copy (t, args, complain, in_decl));
14171 /* Fall through */
14172
14173 case ALIGNOF_EXPR:
14174 {
14175 tree r;
14176
14177 op1 = TREE_OPERAND (t, 0);
14178 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14179 op1 = TREE_TYPE (op1);
14180 if (!args)
14181 {
14182 /* When there are no ARGS, we are trying to evaluate a
14183 non-dependent expression from the parser. Trying to do
14184 the substitutions may not work. */
14185 if (!TYPE_P (op1))
14186 op1 = TREE_TYPE (op1);
14187 }
14188 else
14189 {
14190 ++cp_unevaluated_operand;
14191 ++c_inhibit_evaluation_warnings;
14192 if (TYPE_P (op1))
14193 op1 = tsubst (op1, args, complain, in_decl);
14194 else
14195 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14196 /*function_p=*/false,
14197 /*integral_constant_expression_p=*/
14198 false);
14199 --cp_unevaluated_operand;
14200 --c_inhibit_evaluation_warnings;
14201 }
14202 if (TYPE_P (op1))
14203 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14204 complain & tf_error);
14205 else
14206 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14207 complain & tf_error);
14208 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14209 {
14210 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14211 {
14212 if (!processing_template_decl && TYPE_P (op1))
14213 {
14214 r = build_min (SIZEOF_EXPR, size_type_node,
14215 build1 (NOP_EXPR, op1, error_mark_node));
14216 SIZEOF_EXPR_TYPE_P (r) = 1;
14217 }
14218 else
14219 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14220 TREE_SIDE_EFFECTS (r) = 0;
14221 TREE_READONLY (r) = 1;
14222 }
14223 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14224 }
14225 RETURN (r);
14226 }
14227
14228 case AT_ENCODE_EXPR:
14229 {
14230 op1 = TREE_OPERAND (t, 0);
14231 ++cp_unevaluated_operand;
14232 ++c_inhibit_evaluation_warnings;
14233 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14234 /*function_p=*/false,
14235 /*integral_constant_expression_p=*/false);
14236 --cp_unevaluated_operand;
14237 --c_inhibit_evaluation_warnings;
14238 RETURN (objc_build_encode_expr (op1));
14239 }
14240
14241 case NOEXCEPT_EXPR:
14242 op1 = TREE_OPERAND (t, 0);
14243 ++cp_unevaluated_operand;
14244 ++c_inhibit_evaluation_warnings;
14245 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14246 /*function_p=*/false,
14247 /*integral_constant_expression_p=*/false);
14248 --cp_unevaluated_operand;
14249 --c_inhibit_evaluation_warnings;
14250 RETURN (finish_noexcept_expr (op1, complain));
14251
14252 case MODOP_EXPR:
14253 {
14254 tree r;
14255
14256 ++c_inhibit_evaluation_warnings;
14257
14258 r = build_x_modify_expr
14259 (EXPR_LOCATION (t),
14260 RECUR (TREE_OPERAND (t, 0)),
14261 TREE_CODE (TREE_OPERAND (t, 1)),
14262 RECUR (TREE_OPERAND (t, 2)),
14263 complain|decltype_flag);
14264 /* TREE_NO_WARNING must be set if either the expression was
14265 parenthesized or it uses an operator such as >>= rather
14266 than plain assignment. In the former case, it was already
14267 set and must be copied. In the latter case,
14268 build_x_modify_expr sets it and it must not be reset
14269 here. */
14270 if (TREE_NO_WARNING (t))
14271 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14272
14273 --c_inhibit_evaluation_warnings;
14274
14275 RETURN (r);
14276 }
14277
14278 case ARROW_EXPR:
14279 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14280 args, complain, in_decl);
14281 /* Remember that there was a reference to this entity. */
14282 if (DECL_P (op1))
14283 mark_used (op1);
14284 RETURN (build_x_arrow (input_location, op1, complain));
14285
14286 case NEW_EXPR:
14287 {
14288 tree placement = RECUR (TREE_OPERAND (t, 0));
14289 tree init = RECUR (TREE_OPERAND (t, 3));
14290 vec<tree, va_gc> *placement_vec;
14291 vec<tree, va_gc> *init_vec;
14292 tree ret;
14293
14294 if (placement == NULL_TREE)
14295 placement_vec = NULL;
14296 else
14297 {
14298 placement_vec = make_tree_vector ();
14299 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14300 vec_safe_push (placement_vec, TREE_VALUE (placement));
14301 }
14302
14303 /* If there was an initializer in the original tree, but it
14304 instantiated to an empty list, then we should pass a
14305 non-NULL empty vector to tell build_new that it was an
14306 empty initializer() rather than no initializer. This can
14307 only happen when the initializer is a pack expansion whose
14308 parameter packs are of length zero. */
14309 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14310 init_vec = NULL;
14311 else
14312 {
14313 init_vec = make_tree_vector ();
14314 if (init == void_zero_node)
14315 gcc_assert (init_vec != NULL);
14316 else
14317 {
14318 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14319 vec_safe_push (init_vec, TREE_VALUE (init));
14320 }
14321 }
14322
14323 ret = build_new (&placement_vec,
14324 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14325 RECUR (TREE_OPERAND (t, 2)),
14326 &init_vec,
14327 NEW_EXPR_USE_GLOBAL (t),
14328 complain);
14329
14330 if (placement_vec != NULL)
14331 release_tree_vector (placement_vec);
14332 if (init_vec != NULL)
14333 release_tree_vector (init_vec);
14334
14335 RETURN (ret);
14336 }
14337
14338 case DELETE_EXPR:
14339 RETURN (delete_sanity
14340 (RECUR (TREE_OPERAND (t, 0)),
14341 RECUR (TREE_OPERAND (t, 1)),
14342 DELETE_EXPR_USE_VEC (t),
14343 DELETE_EXPR_USE_GLOBAL (t),
14344 complain));
14345
14346 case COMPOUND_EXPR:
14347 {
14348 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14349 complain & ~tf_decltype, in_decl,
14350 /*function_p=*/false,
14351 integral_constant_expression_p);
14352 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14353 op0,
14354 RECUR (TREE_OPERAND (t, 1)),
14355 complain|decltype_flag));
14356 }
14357
14358 case CALL_EXPR:
14359 {
14360 tree function;
14361 vec<tree, va_gc> *call_args;
14362 unsigned int nargs, i;
14363 bool qualified_p;
14364 bool koenig_p;
14365 tree ret;
14366
14367 function = CALL_EXPR_FN (t);
14368 /* When we parsed the expression, we determined whether or
14369 not Koenig lookup should be performed. */
14370 koenig_p = KOENIG_LOOKUP_P (t);
14371 if (TREE_CODE (function) == SCOPE_REF)
14372 {
14373 qualified_p = true;
14374 function = tsubst_qualified_id (function, args, complain, in_decl,
14375 /*done=*/false,
14376 /*address_p=*/false);
14377 }
14378 else if (koenig_p && identifier_p (function))
14379 {
14380 /* Do nothing; calling tsubst_copy_and_build on an identifier
14381 would incorrectly perform unqualified lookup again.
14382
14383 Note that we can also have an IDENTIFIER_NODE if the earlier
14384 unqualified lookup found a member function; in that case
14385 koenig_p will be false and we do want to do the lookup
14386 again to find the instantiated member function.
14387
14388 FIXME but doing that causes c++/15272, so we need to stop
14389 using IDENTIFIER_NODE in that situation. */
14390 qualified_p = false;
14391 }
14392 else
14393 {
14394 if (TREE_CODE (function) == COMPONENT_REF)
14395 {
14396 tree op = TREE_OPERAND (function, 1);
14397
14398 qualified_p = (TREE_CODE (op) == SCOPE_REF
14399 || (BASELINK_P (op)
14400 && BASELINK_QUALIFIED_P (op)));
14401 }
14402 else
14403 qualified_p = false;
14404
14405 if (TREE_CODE (function) == ADDR_EXPR
14406 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14407 /* Avoid error about taking the address of a constructor. */
14408 function = TREE_OPERAND (function, 0);
14409
14410 function = tsubst_copy_and_build (function, args, complain,
14411 in_decl,
14412 !qualified_p,
14413 integral_constant_expression_p);
14414
14415 if (BASELINK_P (function))
14416 qualified_p = true;
14417 }
14418
14419 nargs = call_expr_nargs (t);
14420 call_args = make_tree_vector ();
14421 for (i = 0; i < nargs; ++i)
14422 {
14423 tree arg = CALL_EXPR_ARG (t, i);
14424
14425 if (!PACK_EXPANSION_P (arg))
14426 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14427 else
14428 {
14429 /* Expand the pack expansion and push each entry onto
14430 CALL_ARGS. */
14431 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14432 if (TREE_CODE (arg) == TREE_VEC)
14433 {
14434 unsigned int len, j;
14435
14436 len = TREE_VEC_LENGTH (arg);
14437 for (j = 0; j < len; ++j)
14438 {
14439 tree value = TREE_VEC_ELT (arg, j);
14440 if (value != NULL_TREE)
14441 value = convert_from_reference (value);
14442 vec_safe_push (call_args, value);
14443 }
14444 }
14445 else
14446 {
14447 /* A partial substitution. Add one entry. */
14448 vec_safe_push (call_args, arg);
14449 }
14450 }
14451 }
14452
14453 /* We do not perform argument-dependent lookup if normal
14454 lookup finds a non-function, in accordance with the
14455 expected resolution of DR 218. */
14456 if (koenig_p
14457 && ((is_overloaded_fn (function)
14458 /* If lookup found a member function, the Koenig lookup is
14459 not appropriate, even if an unqualified-name was used
14460 to denote the function. */
14461 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14462 || identifier_p (function))
14463 /* Only do this when substitution turns a dependent call
14464 into a non-dependent call. */
14465 && type_dependent_expression_p_push (t)
14466 && !any_type_dependent_arguments_p (call_args))
14467 function = perform_koenig_lookup (function, call_args, false,
14468 tf_none);
14469
14470 if (identifier_p (function)
14471 && !any_type_dependent_arguments_p (call_args))
14472 {
14473 if (koenig_p && (complain & tf_warning_or_error))
14474 {
14475 /* For backwards compatibility and good diagnostics, try
14476 the unqualified lookup again if we aren't in SFINAE
14477 context. */
14478 tree unq = (tsubst_copy_and_build
14479 (function, args, complain, in_decl, true,
14480 integral_constant_expression_p));
14481 if (unq == error_mark_node)
14482 RETURN (error_mark_node);
14483
14484 if (unq != function)
14485 {
14486 tree fn = unq;
14487 if (INDIRECT_REF_P (fn))
14488 fn = TREE_OPERAND (fn, 0);
14489 if (TREE_CODE (fn) == COMPONENT_REF)
14490 fn = TREE_OPERAND (fn, 1);
14491 if (is_overloaded_fn (fn))
14492 fn = get_first_fn (fn);
14493 if (permerror (EXPR_LOC_OR_HERE (t),
14494 "%qD was not declared in this scope, "
14495 "and no declarations were found by "
14496 "argument-dependent lookup at the point "
14497 "of instantiation", function))
14498 {
14499 if (!DECL_P (fn))
14500 /* Can't say anything more. */;
14501 else if (DECL_CLASS_SCOPE_P (fn))
14502 {
14503 inform (EXPR_LOC_OR_HERE (t),
14504 "declarations in dependent base %qT are "
14505 "not found by unqualified lookup",
14506 DECL_CLASS_CONTEXT (fn));
14507 if (current_class_ptr)
14508 inform (EXPR_LOC_OR_HERE (t),
14509 "use %<this->%D%> instead", function);
14510 else
14511 inform (EXPR_LOC_OR_HERE (t),
14512 "use %<%T::%D%> instead",
14513 current_class_name, function);
14514 }
14515 else
14516 inform (0, "%q+D declared here, later in the "
14517 "translation unit", fn);
14518 }
14519 function = unq;
14520 }
14521 }
14522 if (identifier_p (function))
14523 {
14524 if (complain & tf_error)
14525 unqualified_name_lookup_error (function);
14526 release_tree_vector (call_args);
14527 RETURN (error_mark_node);
14528 }
14529 }
14530
14531 /* Remember that there was a reference to this entity. */
14532 if (DECL_P (function))
14533 mark_used (function);
14534
14535 /* Put back tf_decltype for the actual call. */
14536 complain |= decltype_flag;
14537
14538 if (TREE_CODE (function) == OFFSET_REF)
14539 ret = build_offset_ref_call_from_tree (function, &call_args,
14540 complain);
14541 else if (TREE_CODE (function) == COMPONENT_REF)
14542 {
14543 tree instance = TREE_OPERAND (function, 0);
14544 tree fn = TREE_OPERAND (function, 1);
14545
14546 if (processing_template_decl
14547 && (type_dependent_expression_p (instance)
14548 || (!BASELINK_P (fn)
14549 && TREE_CODE (fn) != FIELD_DECL)
14550 || type_dependent_expression_p (fn)
14551 || any_type_dependent_arguments_p (call_args)))
14552 ret = build_nt_call_vec (function, call_args);
14553 else if (!BASELINK_P (fn))
14554 ret = finish_call_expr (function, &call_args,
14555 /*disallow_virtual=*/false,
14556 /*koenig_p=*/false,
14557 complain);
14558 else
14559 ret = (build_new_method_call
14560 (instance, fn,
14561 &call_args, NULL_TREE,
14562 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14563 /*fn_p=*/NULL,
14564 complain));
14565 }
14566 else
14567 ret = finish_call_expr (function, &call_args,
14568 /*disallow_virtual=*/qualified_p,
14569 koenig_p,
14570 complain);
14571
14572 release_tree_vector (call_args);
14573
14574 RETURN (ret);
14575 }
14576
14577 case COND_EXPR:
14578 {
14579 tree cond = RECUR (TREE_OPERAND (t, 0));
14580 tree exp1, exp2;
14581
14582 if (TREE_CODE (cond) == INTEGER_CST)
14583 {
14584 if (integer_zerop (cond))
14585 {
14586 ++c_inhibit_evaluation_warnings;
14587 exp1 = RECUR (TREE_OPERAND (t, 1));
14588 --c_inhibit_evaluation_warnings;
14589 exp2 = RECUR (TREE_OPERAND (t, 2));
14590 }
14591 else
14592 {
14593 exp1 = RECUR (TREE_OPERAND (t, 1));
14594 ++c_inhibit_evaluation_warnings;
14595 exp2 = RECUR (TREE_OPERAND (t, 2));
14596 --c_inhibit_evaluation_warnings;
14597 }
14598 }
14599 else
14600 {
14601 exp1 = RECUR (TREE_OPERAND (t, 1));
14602 exp2 = RECUR (TREE_OPERAND (t, 2));
14603 }
14604
14605 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14606 cond, exp1, exp2, complain));
14607 }
14608
14609 case PSEUDO_DTOR_EXPR:
14610 RETURN (finish_pseudo_destructor_expr
14611 (RECUR (TREE_OPERAND (t, 0)),
14612 RECUR (TREE_OPERAND (t, 1)),
14613 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14614 input_location));
14615
14616 case TREE_LIST:
14617 {
14618 tree purpose, value, chain;
14619
14620 if (t == void_list_node)
14621 RETURN (t);
14622
14623 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14624 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14625 {
14626 /* We have pack expansions, so expand those and
14627 create a new list out of it. */
14628 tree purposevec = NULL_TREE;
14629 tree valuevec = NULL_TREE;
14630 tree chain;
14631 int i, len = -1;
14632
14633 /* Expand the argument expressions. */
14634 if (TREE_PURPOSE (t))
14635 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14636 complain, in_decl);
14637 if (TREE_VALUE (t))
14638 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14639 complain, in_decl);
14640
14641 /* Build the rest of the list. */
14642 chain = TREE_CHAIN (t);
14643 if (chain && chain != void_type_node)
14644 chain = RECUR (chain);
14645
14646 /* Determine the number of arguments. */
14647 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14648 {
14649 len = TREE_VEC_LENGTH (purposevec);
14650 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14651 }
14652 else if (TREE_CODE (valuevec) == TREE_VEC)
14653 len = TREE_VEC_LENGTH (valuevec);
14654 else
14655 {
14656 /* Since we only performed a partial substitution into
14657 the argument pack, we only RETURN (a single list
14658 node. */
14659 if (purposevec == TREE_PURPOSE (t)
14660 && valuevec == TREE_VALUE (t)
14661 && chain == TREE_CHAIN (t))
14662 RETURN (t);
14663
14664 RETURN (tree_cons (purposevec, valuevec, chain));
14665 }
14666
14667 /* Convert the argument vectors into a TREE_LIST */
14668 i = len;
14669 while (i > 0)
14670 {
14671 /* Grab the Ith values. */
14672 i--;
14673 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14674 : NULL_TREE;
14675 value
14676 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14677 : NULL_TREE;
14678
14679 /* Build the list (backwards). */
14680 chain = tree_cons (purpose, value, chain);
14681 }
14682
14683 RETURN (chain);
14684 }
14685
14686 purpose = TREE_PURPOSE (t);
14687 if (purpose)
14688 purpose = RECUR (purpose);
14689 value = TREE_VALUE (t);
14690 if (value)
14691 value = RECUR (value);
14692 chain = TREE_CHAIN (t);
14693 if (chain && chain != void_type_node)
14694 chain = RECUR (chain);
14695 if (purpose == TREE_PURPOSE (t)
14696 && value == TREE_VALUE (t)
14697 && chain == TREE_CHAIN (t))
14698 RETURN (t);
14699 RETURN (tree_cons (purpose, value, chain));
14700 }
14701
14702 case COMPONENT_REF:
14703 {
14704 tree object;
14705 tree object_type;
14706 tree member;
14707
14708 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14709 args, complain, in_decl);
14710 /* Remember that there was a reference to this entity. */
14711 if (DECL_P (object))
14712 mark_used (object);
14713 object_type = TREE_TYPE (object);
14714
14715 member = TREE_OPERAND (t, 1);
14716 if (BASELINK_P (member))
14717 member = tsubst_baselink (member,
14718 non_reference (TREE_TYPE (object)),
14719 args, complain, in_decl);
14720 else
14721 member = tsubst_copy (member, args, complain, in_decl);
14722 if (member == error_mark_node)
14723 RETURN (error_mark_node);
14724
14725 if (type_dependent_expression_p (object))
14726 /* We can't do much here. */;
14727 else if (!CLASS_TYPE_P (object_type))
14728 {
14729 if (scalarish_type_p (object_type))
14730 {
14731 tree s = NULL_TREE;
14732 tree dtor = member;
14733
14734 if (TREE_CODE (dtor) == SCOPE_REF)
14735 {
14736 s = TREE_OPERAND (dtor, 0);
14737 dtor = TREE_OPERAND (dtor, 1);
14738 }
14739 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14740 {
14741 dtor = TREE_OPERAND (dtor, 0);
14742 if (TYPE_P (dtor))
14743 RETURN (finish_pseudo_destructor_expr
14744 (object, s, dtor, input_location));
14745 }
14746 }
14747 }
14748 else if (TREE_CODE (member) == SCOPE_REF
14749 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14750 {
14751 /* Lookup the template functions now that we know what the
14752 scope is. */
14753 tree scope = TREE_OPERAND (member, 0);
14754 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14755 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14756 member = lookup_qualified_name (scope, tmpl,
14757 /*is_type_p=*/false,
14758 /*complain=*/false);
14759 if (BASELINK_P (member))
14760 {
14761 BASELINK_FUNCTIONS (member)
14762 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14763 args);
14764 member = (adjust_result_of_qualified_name_lookup
14765 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14766 object_type));
14767 }
14768 else
14769 {
14770 qualified_name_lookup_error (scope, tmpl, member,
14771 input_location);
14772 RETURN (error_mark_node);
14773 }
14774 }
14775 else if (TREE_CODE (member) == SCOPE_REF
14776 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14777 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14778 {
14779 if (complain & tf_error)
14780 {
14781 if (TYPE_P (TREE_OPERAND (member, 0)))
14782 error ("%qT is not a class or namespace",
14783 TREE_OPERAND (member, 0));
14784 else
14785 error ("%qD is not a class or namespace",
14786 TREE_OPERAND (member, 0));
14787 }
14788 RETURN (error_mark_node);
14789 }
14790 else if (TREE_CODE (member) == FIELD_DECL)
14791 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14792
14793 RETURN (finish_class_member_access_expr (object, member,
14794 /*template_p=*/false,
14795 complain));
14796 }
14797
14798 case THROW_EXPR:
14799 RETURN (build_throw
14800 (RECUR (TREE_OPERAND (t, 0))));
14801
14802 case CONSTRUCTOR:
14803 {
14804 vec<constructor_elt, va_gc> *n;
14805 constructor_elt *ce;
14806 unsigned HOST_WIDE_INT idx;
14807 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14808 bool process_index_p;
14809 int newlen;
14810 bool need_copy_p = false;
14811 tree r;
14812
14813 if (type == error_mark_node)
14814 RETURN (error_mark_node);
14815
14816 /* digest_init will do the wrong thing if we let it. */
14817 if (type && TYPE_PTRMEMFUNC_P (type))
14818 RETURN (t);
14819
14820 /* We do not want to process the index of aggregate
14821 initializers as they are identifier nodes which will be
14822 looked up by digest_init. */
14823 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14824
14825 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14826 newlen = vec_safe_length (n);
14827 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14828 {
14829 if (ce->index && process_index_p
14830 /* An identifier index is looked up in the type
14831 being initialized, not the current scope. */
14832 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14833 ce->index = RECUR (ce->index);
14834
14835 if (PACK_EXPANSION_P (ce->value))
14836 {
14837 /* Substitute into the pack expansion. */
14838 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14839 in_decl);
14840
14841 if (ce->value == error_mark_node
14842 || PACK_EXPANSION_P (ce->value))
14843 ;
14844 else if (TREE_VEC_LENGTH (ce->value) == 1)
14845 /* Just move the argument into place. */
14846 ce->value = TREE_VEC_ELT (ce->value, 0);
14847 else
14848 {
14849 /* Update the length of the final CONSTRUCTOR
14850 arguments vector, and note that we will need to
14851 copy.*/
14852 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14853 need_copy_p = true;
14854 }
14855 }
14856 else
14857 ce->value = RECUR (ce->value);
14858 }
14859
14860 if (need_copy_p)
14861 {
14862 vec<constructor_elt, va_gc> *old_n = n;
14863
14864 vec_alloc (n, newlen);
14865 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14866 {
14867 if (TREE_CODE (ce->value) == TREE_VEC)
14868 {
14869 int i, len = TREE_VEC_LENGTH (ce->value);
14870 for (i = 0; i < len; ++i)
14871 CONSTRUCTOR_APPEND_ELT (n, 0,
14872 TREE_VEC_ELT (ce->value, i));
14873 }
14874 else
14875 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14876 }
14877 }
14878
14879 r = build_constructor (init_list_type_node, n);
14880 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14881
14882 if (TREE_HAS_CONSTRUCTOR (t))
14883 RETURN (finish_compound_literal (type, r, complain));
14884
14885 TREE_TYPE (r) = type;
14886 RETURN (r);
14887 }
14888
14889 case TYPEID_EXPR:
14890 {
14891 tree operand_0 = TREE_OPERAND (t, 0);
14892 if (TYPE_P (operand_0))
14893 {
14894 operand_0 = tsubst (operand_0, args, complain, in_decl);
14895 RETURN (get_typeid (operand_0, complain));
14896 }
14897 else
14898 {
14899 operand_0 = RECUR (operand_0);
14900 RETURN (build_typeid (operand_0, complain));
14901 }
14902 }
14903
14904 case VAR_DECL:
14905 if (!args)
14906 RETURN (t);
14907 else if (DECL_PACK_P (t))
14908 {
14909 /* We don't build decls for an instantiation of a
14910 variadic capture proxy, we instantiate the elements
14911 when needed. */
14912 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14913 return RECUR (DECL_VALUE_EXPR (t));
14914 }
14915 /* Fall through */
14916
14917 case PARM_DECL:
14918 {
14919 tree r = tsubst_copy (t, args, complain, in_decl);
14920
14921 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14922 /* If the original type was a reference, we'll be wrapped in
14923 the appropriate INDIRECT_REF. */
14924 r = convert_from_reference (r);
14925 RETURN (r);
14926 }
14927
14928 case VA_ARG_EXPR:
14929 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14930 RECUR (TREE_OPERAND (t, 0)),
14931 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14932
14933 case OFFSETOF_EXPR:
14934 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14935
14936 case TRAIT_EXPR:
14937 {
14938 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14939 complain, in_decl);
14940
14941 tree type2 = TRAIT_EXPR_TYPE2 (t);
14942 if (type2)
14943 type2 = tsubst_copy (type2, args, complain, in_decl);
14944
14945 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14946 }
14947
14948 case STMT_EXPR:
14949 {
14950 tree old_stmt_expr = cur_stmt_expr;
14951 tree stmt_expr = begin_stmt_expr ();
14952
14953 cur_stmt_expr = stmt_expr;
14954 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14955 integral_constant_expression_p);
14956 stmt_expr = finish_stmt_expr (stmt_expr, false);
14957 cur_stmt_expr = old_stmt_expr;
14958
14959 /* If the resulting list of expression statement is empty,
14960 fold it further into void_zero_node. */
14961 if (empty_expr_stmt_p (stmt_expr))
14962 stmt_expr = void_zero_node;
14963
14964 RETURN (stmt_expr);
14965 }
14966
14967 case LAMBDA_EXPR:
14968 {
14969 tree r = build_lambda_expr ();
14970
14971 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14972 LAMBDA_EXPR_CLOSURE (r) = type;
14973 CLASSTYPE_LAMBDA_EXPR (type) = r;
14974
14975 LAMBDA_EXPR_LOCATION (r)
14976 = LAMBDA_EXPR_LOCATION (t);
14977 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14978 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14979 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14980 LAMBDA_EXPR_DISCRIMINATOR (r)
14981 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14982 /* For a function scope, we want to use tsubst so that we don't
14983 complain about referring to an auto function before its return
14984 type has been deduced. Otherwise, we want to use tsubst_copy so
14985 that we look up the existing field/parameter/variable rather
14986 than build a new one. */
14987 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14988 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14989 scope = tsubst (scope, args, complain, in_decl);
14990 else if (scope && TREE_CODE (scope) == PARM_DECL)
14991 {
14992 /* Look up the parameter we want directly, as tsubst_copy
14993 doesn't do what we need. */
14994 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14995 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14996 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14997 parm = DECL_CHAIN (parm);
14998 scope = parm;
14999 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15000 if (DECL_CONTEXT (scope) == NULL_TREE)
15001 DECL_CONTEXT (scope) = fn;
15002 }
15003 else
15004 scope = RECUR (scope);
15005 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15006 LAMBDA_EXPR_RETURN_TYPE (r)
15007 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15008
15009 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15010 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15011
15012 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15013 determine_visibility (TYPE_NAME (type));
15014 /* Now that we know visibility, instantiate the type so we have a
15015 declaration of the op() for later calls to lambda_function. */
15016 complete_type (type);
15017
15018 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15019
15020 RETURN (build_lambda_object (r));
15021 }
15022
15023 case TARGET_EXPR:
15024 /* We can get here for a constant initializer of non-dependent type.
15025 FIXME stop folding in cp_parser_initializer_clause. */
15026 {
15027 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15028 complain);
15029 RETURN (r);
15030 }
15031
15032 case TRANSACTION_EXPR:
15033 RETURN (tsubst_expr(t, args, complain, in_decl,
15034 integral_constant_expression_p));
15035
15036 case PAREN_EXPR:
15037 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15038
15039 case VEC_PERM_EXPR:
15040 RETURN (build_x_vec_perm_expr (input_location,
15041 RECUR (TREE_OPERAND (t, 0)),
15042 RECUR (TREE_OPERAND (t, 1)),
15043 RECUR (TREE_OPERAND (t, 2)),
15044 complain));
15045
15046 default:
15047 /* Handle Objective-C++ constructs, if appropriate. */
15048 {
15049 tree subst
15050 = objcp_tsubst_copy_and_build (t, args, complain,
15051 in_decl, /*function_p=*/false);
15052 if (subst)
15053 RETURN (subst);
15054 }
15055 RETURN (tsubst_copy (t, args, complain, in_decl));
15056 }
15057
15058 #undef RECUR
15059 #undef RETURN
15060 out:
15061 input_location = loc;
15062 return retval;
15063 }
15064
15065 /* Verify that the instantiated ARGS are valid. For type arguments,
15066 make sure that the type's linkage is ok. For non-type arguments,
15067 make sure they are constants if they are integral or enumerations.
15068 Emit an error under control of COMPLAIN, and return TRUE on error. */
15069
15070 static bool
15071 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15072 {
15073 if (dependent_template_arg_p (t))
15074 return false;
15075 if (ARGUMENT_PACK_P (t))
15076 {
15077 tree vec = ARGUMENT_PACK_ARGS (t);
15078 int len = TREE_VEC_LENGTH (vec);
15079 bool result = false;
15080 int i;
15081
15082 for (i = 0; i < len; ++i)
15083 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15084 result = true;
15085 return result;
15086 }
15087 else if (TYPE_P (t))
15088 {
15089 /* [basic.link]: A name with no linkage (notably, the name
15090 of a class or enumeration declared in a local scope)
15091 shall not be used to declare an entity with linkage.
15092 This implies that names with no linkage cannot be used as
15093 template arguments
15094
15095 DR 757 relaxes this restriction for C++0x. */
15096 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15097 : no_linkage_check (t, /*relaxed_p=*/false));
15098
15099 if (nt)
15100 {
15101 /* DR 488 makes use of a type with no linkage cause
15102 type deduction to fail. */
15103 if (complain & tf_error)
15104 {
15105 if (TYPE_ANONYMOUS_P (nt))
15106 error ("%qT is/uses anonymous type", t);
15107 else
15108 error ("template argument for %qD uses local type %qT",
15109 tmpl, t);
15110 }
15111 return true;
15112 }
15113 /* In order to avoid all sorts of complications, we do not
15114 allow variably-modified types as template arguments. */
15115 else if (variably_modified_type_p (t, NULL_TREE))
15116 {
15117 if (complain & tf_error)
15118 error ("%qT is a variably modified type", t);
15119 return true;
15120 }
15121 }
15122 /* Class template and alias template arguments should be OK. */
15123 else if (DECL_TYPE_TEMPLATE_P (t))
15124 ;
15125 /* A non-type argument of integral or enumerated type must be a
15126 constant. */
15127 else if (TREE_TYPE (t)
15128 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15129 && !TREE_CONSTANT (t))
15130 {
15131 if (complain & tf_error)
15132 error ("integral expression %qE is not constant", t);
15133 return true;
15134 }
15135 return false;
15136 }
15137
15138 static bool
15139 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15140 {
15141 int ix, len = DECL_NTPARMS (tmpl);
15142 bool result = false;
15143
15144 for (ix = 0; ix != len; ix++)
15145 {
15146 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15147 result = true;
15148 }
15149 if (result && (complain & tf_error))
15150 error (" trying to instantiate %qD", tmpl);
15151 return result;
15152 }
15153
15154 /* We're out of SFINAE context now, so generate diagnostics for the access
15155 errors we saw earlier when instantiating D from TMPL and ARGS. */
15156
15157 static void
15158 recheck_decl_substitution (tree d, tree tmpl, tree args)
15159 {
15160 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15161 tree type = TREE_TYPE (pattern);
15162 location_t loc = input_location;
15163
15164 push_access_scope (d);
15165 push_deferring_access_checks (dk_no_deferred);
15166 input_location = DECL_SOURCE_LOCATION (pattern);
15167 tsubst (type, args, tf_warning_or_error, d);
15168 input_location = loc;
15169 pop_deferring_access_checks ();
15170 pop_access_scope (d);
15171 }
15172
15173 /* Instantiate the indicated variable, function, or alias template TMPL with
15174 the template arguments in TARG_PTR. */
15175
15176 static tree
15177 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15178 {
15179 tree targ_ptr = orig_args;
15180 tree fndecl;
15181 tree gen_tmpl;
15182 tree spec;
15183 bool access_ok = true;
15184
15185 if (tmpl == error_mark_node)
15186 return error_mark_node;
15187
15188 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15189
15190 /* If this function is a clone, handle it specially. */
15191 if (DECL_CLONED_FUNCTION_P (tmpl))
15192 {
15193 tree spec;
15194 tree clone;
15195
15196 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15197 DECL_CLONED_FUNCTION. */
15198 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15199 targ_ptr, complain);
15200 if (spec == error_mark_node)
15201 return error_mark_node;
15202
15203 /* Look for the clone. */
15204 FOR_EACH_CLONE (clone, spec)
15205 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15206 return clone;
15207 /* We should always have found the clone by now. */
15208 gcc_unreachable ();
15209 return NULL_TREE;
15210 }
15211
15212 /* Check to see if we already have this specialization. */
15213 gen_tmpl = most_general_template (tmpl);
15214 if (tmpl != gen_tmpl)
15215 /* The TMPL is a partial instantiation. To get a full set of
15216 arguments we must add the arguments used to perform the
15217 partial instantiation. */
15218 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15219 targ_ptr);
15220
15221 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15222 but it doesn't seem to be on the hot path. */
15223 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15224
15225 gcc_assert (tmpl == gen_tmpl
15226 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15227 == spec)
15228 || fndecl == NULL_TREE);
15229
15230 if (spec != NULL_TREE)
15231 {
15232 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15233 {
15234 if (complain & tf_error)
15235 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15236 return error_mark_node;
15237 }
15238 return spec;
15239 }
15240
15241 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15242 complain))
15243 return error_mark_node;
15244
15245 /* We are building a FUNCTION_DECL, during which the access of its
15246 parameters and return types have to be checked. However this
15247 FUNCTION_DECL which is the desired context for access checking
15248 is not built yet. We solve this chicken-and-egg problem by
15249 deferring all checks until we have the FUNCTION_DECL. */
15250 push_deferring_access_checks (dk_deferred);
15251
15252 /* Instantiation of the function happens in the context of the function
15253 template, not the context of the overload resolution we're doing. */
15254 push_to_top_level ();
15255 /* If there are dependent arguments, e.g. because we're doing partial
15256 ordering, make sure processing_template_decl stays set. */
15257 if (uses_template_parms (targ_ptr))
15258 ++processing_template_decl;
15259 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15260 {
15261 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15262 complain, gen_tmpl);
15263 push_nested_class (ctx);
15264 }
15265 /* Substitute template parameters to obtain the specialization. */
15266 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15267 targ_ptr, complain, gen_tmpl);
15268 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15269 pop_nested_class ();
15270 pop_from_top_level ();
15271
15272 if (fndecl == error_mark_node)
15273 {
15274 pop_deferring_access_checks ();
15275 return error_mark_node;
15276 }
15277
15278 /* The DECL_TI_TEMPLATE should always be the immediate parent
15279 template, not the most general template. */
15280 DECL_TI_TEMPLATE (fndecl) = tmpl;
15281
15282 /* Now we know the specialization, compute access previously
15283 deferred. */
15284 push_access_scope (fndecl);
15285 if (!perform_deferred_access_checks (complain))
15286 access_ok = false;
15287 pop_access_scope (fndecl);
15288 pop_deferring_access_checks ();
15289
15290 /* If we've just instantiated the main entry point for a function,
15291 instantiate all the alternate entry points as well. We do this
15292 by cloning the instantiation of the main entry point, not by
15293 instantiating the template clones. */
15294 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15295 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15296
15297 if (!access_ok)
15298 {
15299 if (!(complain & tf_error))
15300 {
15301 /* Remember to reinstantiate when we're out of SFINAE so the user
15302 can see the errors. */
15303 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15304 }
15305 return error_mark_node;
15306 }
15307 return fndecl;
15308 }
15309
15310 /* Wrapper for instantiate_template_1. */
15311
15312 tree
15313 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15314 {
15315 tree ret;
15316 timevar_push (TV_TEMPLATE_INST);
15317 ret = instantiate_template_1 (tmpl, orig_args, complain);
15318 timevar_pop (TV_TEMPLATE_INST);
15319 return ret;
15320 }
15321
15322 /* Instantiate the alias template TMPL with ARGS. Also push a template
15323 instantiation level, which instantiate_template doesn't do because
15324 functions and variables have sufficient context established by the
15325 callers. */
15326
15327 static tree
15328 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15329 {
15330 struct pending_template *old_last_pend = last_pending_template;
15331 struct tinst_level *old_error_tinst = last_error_tinst_level;
15332 if (tmpl == error_mark_node || args == error_mark_node)
15333 return error_mark_node;
15334 tree tinst = build_tree_list (tmpl, args);
15335 if (!push_tinst_level (tinst))
15336 {
15337 ggc_free (tinst);
15338 return error_mark_node;
15339 }
15340
15341 args =
15342 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15343 args, tmpl, complain,
15344 /*require_all_args=*/true,
15345 /*use_default_args=*/true);
15346
15347 tree r = instantiate_template (tmpl, args, complain);
15348 pop_tinst_level ();
15349 /* We can't free this if a pending_template entry or last_error_tinst_level
15350 is pointing at it. */
15351 if (last_pending_template == old_last_pend
15352 && last_error_tinst_level == old_error_tinst)
15353 ggc_free (tinst);
15354
15355 return r;
15356 }
15357
15358 /* PARM is a template parameter pack for FN. Returns true iff
15359 PARM is used in a deducible way in the argument list of FN. */
15360
15361 static bool
15362 pack_deducible_p (tree parm, tree fn)
15363 {
15364 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15365 for (; t; t = TREE_CHAIN (t))
15366 {
15367 tree type = TREE_VALUE (t);
15368 tree packs;
15369 if (!PACK_EXPANSION_P (type))
15370 continue;
15371 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15372 packs; packs = TREE_CHAIN (packs))
15373 if (TREE_VALUE (packs) == parm)
15374 {
15375 /* The template parameter pack is used in a function parameter
15376 pack. If this is the end of the parameter list, the
15377 template parameter pack is deducible. */
15378 if (TREE_CHAIN (t) == void_list_node)
15379 return true;
15380 else
15381 /* Otherwise, not. Well, it could be deduced from
15382 a non-pack parameter, but doing so would end up with
15383 a deduction mismatch, so don't bother. */
15384 return false;
15385 }
15386 }
15387 /* The template parameter pack isn't used in any function parameter
15388 packs, but it might be used deeper, e.g. tuple<Args...>. */
15389 return true;
15390 }
15391
15392 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15393 NARGS elements of the arguments that are being used when calling
15394 it. TARGS is a vector into which the deduced template arguments
15395 are placed.
15396
15397 Return zero for success, 2 for an incomplete match that doesn't resolve
15398 all the types, and 1 for complete failure. An error message will be
15399 printed only for an incomplete match.
15400
15401 If FN is a conversion operator, or we are trying to produce a specific
15402 specialization, RETURN_TYPE is the return type desired.
15403
15404 The EXPLICIT_TARGS are explicit template arguments provided via a
15405 template-id.
15406
15407 The parameter STRICT is one of:
15408
15409 DEDUCE_CALL:
15410 We are deducing arguments for a function call, as in
15411 [temp.deduct.call].
15412
15413 DEDUCE_CONV:
15414 We are deducing arguments for a conversion function, as in
15415 [temp.deduct.conv].
15416
15417 DEDUCE_EXACT:
15418 We are deducing arguments when doing an explicit instantiation
15419 as in [temp.explicit], when determining an explicit specialization
15420 as in [temp.expl.spec], or when taking the address of a function
15421 template, as in [temp.deduct.funcaddr]. */
15422
15423 tree
15424 fn_type_unification (tree fn,
15425 tree explicit_targs,
15426 tree targs,
15427 const tree *args,
15428 unsigned int nargs,
15429 tree return_type,
15430 unification_kind_t strict,
15431 int flags,
15432 bool explain_p,
15433 bool decltype_p)
15434 {
15435 tree parms;
15436 tree fntype;
15437 tree decl = NULL_TREE;
15438 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15439 bool ok;
15440 static int deduction_depth;
15441 struct pending_template *old_last_pend = last_pending_template;
15442 struct tinst_level *old_error_tinst = last_error_tinst_level;
15443 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15444 tree tinst;
15445 tree r = error_mark_node;
15446
15447 if (decltype_p)
15448 complain |= tf_decltype;
15449
15450 /* In C++0x, it's possible to have a function template whose type depends
15451 on itself recursively. This is most obvious with decltype, but can also
15452 occur with enumeration scope (c++/48969). So we need to catch infinite
15453 recursion and reject the substitution at deduction time; this function
15454 will return error_mark_node for any repeated substitution.
15455
15456 This also catches excessive recursion such as when f<N> depends on
15457 f<N-1> across all integers, and returns error_mark_node for all the
15458 substitutions back up to the initial one.
15459
15460 This is, of course, not reentrant. */
15461 if (excessive_deduction_depth)
15462 return error_mark_node;
15463 tinst = build_tree_list (fn, NULL_TREE);
15464 ++deduction_depth;
15465
15466 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15467
15468 fntype = TREE_TYPE (fn);
15469 if (explicit_targs)
15470 {
15471 /* [temp.deduct]
15472
15473 The specified template arguments must match the template
15474 parameters in kind (i.e., type, nontype, template), and there
15475 must not be more arguments than there are parameters;
15476 otherwise type deduction fails.
15477
15478 Nontype arguments must match the types of the corresponding
15479 nontype template parameters, or must be convertible to the
15480 types of the corresponding nontype parameters as specified in
15481 _temp.arg.nontype_, otherwise type deduction fails.
15482
15483 All references in the function type of the function template
15484 to the corresponding template parameters are replaced by the
15485 specified template argument values. If a substitution in a
15486 template parameter or in the function type of the function
15487 template results in an invalid type, type deduction fails. */
15488 int i, len = TREE_VEC_LENGTH (tparms);
15489 location_t loc = input_location;
15490 bool incomplete = false;
15491
15492 /* Adjust any explicit template arguments before entering the
15493 substitution context. */
15494 explicit_targs
15495 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15496 complain,
15497 /*require_all_args=*/false,
15498 /*use_default_args=*/false));
15499 if (explicit_targs == error_mark_node)
15500 goto fail;
15501
15502 /* Substitute the explicit args into the function type. This is
15503 necessary so that, for instance, explicitly declared function
15504 arguments can match null pointed constants. If we were given
15505 an incomplete set of explicit args, we must not do semantic
15506 processing during substitution as we could create partial
15507 instantiations. */
15508 for (i = 0; i < len; i++)
15509 {
15510 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15511 bool parameter_pack = false;
15512 tree targ = TREE_VEC_ELT (explicit_targs, i);
15513
15514 /* Dig out the actual parm. */
15515 if (TREE_CODE (parm) == TYPE_DECL
15516 || TREE_CODE (parm) == TEMPLATE_DECL)
15517 {
15518 parm = TREE_TYPE (parm);
15519 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15520 }
15521 else if (TREE_CODE (parm) == PARM_DECL)
15522 {
15523 parm = DECL_INITIAL (parm);
15524 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15525 }
15526
15527 if (!parameter_pack && targ == NULL_TREE)
15528 /* No explicit argument for this template parameter. */
15529 incomplete = true;
15530
15531 if (parameter_pack && pack_deducible_p (parm, fn))
15532 {
15533 /* Mark the argument pack as "incomplete". We could
15534 still deduce more arguments during unification.
15535 We remove this mark in type_unification_real. */
15536 if (targ)
15537 {
15538 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15539 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15540 = ARGUMENT_PACK_ARGS (targ);
15541 }
15542
15543 /* We have some incomplete argument packs. */
15544 incomplete = true;
15545 }
15546 }
15547
15548 TREE_VALUE (tinst) = explicit_targs;
15549 if (!push_tinst_level (tinst))
15550 {
15551 excessive_deduction_depth = true;
15552 goto fail;
15553 }
15554 processing_template_decl += incomplete;
15555 input_location = DECL_SOURCE_LOCATION (fn);
15556 /* Ignore any access checks; we'll see them again in
15557 instantiate_template and they might have the wrong
15558 access path at this point. */
15559 push_deferring_access_checks (dk_deferred);
15560 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15561 complain | tf_partial, NULL_TREE);
15562 pop_deferring_access_checks ();
15563 input_location = loc;
15564 processing_template_decl -= incomplete;
15565 pop_tinst_level ();
15566
15567 if (fntype == error_mark_node)
15568 goto fail;
15569
15570 /* Place the explicitly specified arguments in TARGS. */
15571 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15572 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15573 }
15574
15575 /* Never do unification on the 'this' parameter. */
15576 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15577
15578 if (return_type)
15579 {
15580 tree *new_args;
15581
15582 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15583 new_args = XALLOCAVEC (tree, nargs + 1);
15584 new_args[0] = return_type;
15585 memcpy (new_args + 1, args, nargs * sizeof (tree));
15586 args = new_args;
15587 ++nargs;
15588 }
15589
15590 /* We allow incomplete unification without an error message here
15591 because the standard doesn't seem to explicitly prohibit it. Our
15592 callers must be ready to deal with unification failures in any
15593 event. */
15594
15595 TREE_VALUE (tinst) = targs;
15596 /* If we aren't explaining yet, push tinst context so we can see where
15597 any errors (e.g. from class instantiations triggered by instantiation
15598 of default template arguments) come from. If we are explaining, this
15599 context is redundant. */
15600 if (!explain_p && !push_tinst_level (tinst))
15601 {
15602 excessive_deduction_depth = true;
15603 goto fail;
15604 }
15605
15606 /* type_unification_real will pass back any access checks from default
15607 template argument substitution. */
15608 vec<deferred_access_check, va_gc> *checks;
15609 checks = NULL;
15610
15611 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15612 targs, parms, args, nargs, /*subr=*/0,
15613 strict, flags, &checks, explain_p);
15614 if (!explain_p)
15615 pop_tinst_level ();
15616 if (!ok)
15617 goto fail;
15618
15619 /* Now that we have bindings for all of the template arguments,
15620 ensure that the arguments deduced for the template template
15621 parameters have compatible template parameter lists. We cannot
15622 check this property before we have deduced all template
15623 arguments, because the template parameter types of a template
15624 template parameter might depend on prior template parameters
15625 deduced after the template template parameter. The following
15626 ill-formed example illustrates this issue:
15627
15628 template<typename T, template<T> class C> void f(C<5>, T);
15629
15630 template<int N> struct X {};
15631
15632 void g() {
15633 f(X<5>(), 5l); // error: template argument deduction fails
15634 }
15635
15636 The template parameter list of 'C' depends on the template type
15637 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15638 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15639 time that we deduce 'C'. */
15640 if (!template_template_parm_bindings_ok_p
15641 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15642 {
15643 unify_inconsistent_template_template_parameters (explain_p);
15644 goto fail;
15645 }
15646
15647 /* All is well so far. Now, check:
15648
15649 [temp.deduct]
15650
15651 When all template arguments have been deduced, all uses of
15652 template parameters in nondeduced contexts are replaced with
15653 the corresponding deduced argument values. If the
15654 substitution results in an invalid type, as described above,
15655 type deduction fails. */
15656 TREE_VALUE (tinst) = targs;
15657 if (!push_tinst_level (tinst))
15658 {
15659 excessive_deduction_depth = true;
15660 goto fail;
15661 }
15662
15663 /* Also collect access checks from the instantiation. */
15664 reopen_deferring_access_checks (checks);
15665
15666 decl = instantiate_template (fn, targs, complain);
15667
15668 checks = get_deferred_access_checks ();
15669 pop_deferring_access_checks ();
15670
15671 pop_tinst_level ();
15672
15673 if (decl == error_mark_node)
15674 goto fail;
15675
15676 /* Now perform any access checks encountered during substitution. */
15677 push_access_scope (decl);
15678 ok = perform_access_checks (checks, complain);
15679 pop_access_scope (decl);
15680 if (!ok)
15681 goto fail;
15682
15683 /* If we're looking for an exact match, check that what we got
15684 is indeed an exact match. It might not be if some template
15685 parameters are used in non-deduced contexts. */
15686 if (strict == DEDUCE_EXACT)
15687 {
15688 tree substed = TREE_TYPE (decl);
15689 unsigned int i;
15690
15691 tree sarg
15692 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15693 if (return_type)
15694 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15695 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15696 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15697 {
15698 unify_type_mismatch (explain_p, args[i],
15699 TREE_VALUE (sarg));
15700 goto fail;
15701 }
15702 }
15703
15704 r = decl;
15705
15706 fail:
15707 --deduction_depth;
15708 if (excessive_deduction_depth)
15709 {
15710 if (deduction_depth == 0)
15711 /* Reset once we're all the way out. */
15712 excessive_deduction_depth = false;
15713 }
15714
15715 /* We can't free this if a pending_template entry or last_error_tinst_level
15716 is pointing at it. */
15717 if (last_pending_template == old_last_pend
15718 && last_error_tinst_level == old_error_tinst)
15719 ggc_free (tinst);
15720
15721 return r;
15722 }
15723
15724 /* Adjust types before performing type deduction, as described in
15725 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15726 sections are symmetric. PARM is the type of a function parameter
15727 or the return type of the conversion function. ARG is the type of
15728 the argument passed to the call, or the type of the value
15729 initialized with the result of the conversion function.
15730 ARG_EXPR is the original argument expression, which may be null. */
15731
15732 static int
15733 maybe_adjust_types_for_deduction (unification_kind_t strict,
15734 tree* parm,
15735 tree* arg,
15736 tree arg_expr)
15737 {
15738 int result = 0;
15739
15740 switch (strict)
15741 {
15742 case DEDUCE_CALL:
15743 break;
15744
15745 case DEDUCE_CONV:
15746 {
15747 /* Swap PARM and ARG throughout the remainder of this
15748 function; the handling is precisely symmetric since PARM
15749 will initialize ARG rather than vice versa. */
15750 tree* temp = parm;
15751 parm = arg;
15752 arg = temp;
15753 break;
15754 }
15755
15756 case DEDUCE_EXACT:
15757 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15758 too, but here handle it by stripping the reference from PARM
15759 rather than by adding it to ARG. */
15760 if (TREE_CODE (*parm) == REFERENCE_TYPE
15761 && TYPE_REF_IS_RVALUE (*parm)
15762 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15763 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15764 && TREE_CODE (*arg) == REFERENCE_TYPE
15765 && !TYPE_REF_IS_RVALUE (*arg))
15766 *parm = TREE_TYPE (*parm);
15767 /* Nothing else to do in this case. */
15768 return 0;
15769
15770 default:
15771 gcc_unreachable ();
15772 }
15773
15774 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15775 {
15776 /* [temp.deduct.call]
15777
15778 If P is not a reference type:
15779
15780 --If A is an array type, the pointer type produced by the
15781 array-to-pointer standard conversion (_conv.array_) is
15782 used in place of A for type deduction; otherwise,
15783
15784 --If A is a function type, the pointer type produced by
15785 the function-to-pointer standard conversion
15786 (_conv.func_) is used in place of A for type deduction;
15787 otherwise,
15788
15789 --If A is a cv-qualified type, the top level
15790 cv-qualifiers of A's type are ignored for type
15791 deduction. */
15792 if (TREE_CODE (*arg) == ARRAY_TYPE)
15793 *arg = build_pointer_type (TREE_TYPE (*arg));
15794 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15795 *arg = build_pointer_type (*arg);
15796 else
15797 *arg = TYPE_MAIN_VARIANT (*arg);
15798 }
15799
15800 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15801 of the form T&&, where T is a template parameter, and the argument
15802 is an lvalue, T is deduced as A& */
15803 if (TREE_CODE (*parm) == REFERENCE_TYPE
15804 && TYPE_REF_IS_RVALUE (*parm)
15805 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15806 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15807 && (arg_expr ? real_lvalue_p (arg_expr)
15808 /* try_one_overload doesn't provide an arg_expr, but
15809 functions are always lvalues. */
15810 : TREE_CODE (*arg) == FUNCTION_TYPE))
15811 *arg = build_reference_type (*arg);
15812
15813 /* [temp.deduct.call]
15814
15815 If P is a cv-qualified type, the top level cv-qualifiers
15816 of P's type are ignored for type deduction. If P is a
15817 reference type, the type referred to by P is used for
15818 type deduction. */
15819 *parm = TYPE_MAIN_VARIANT (*parm);
15820 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15821 {
15822 *parm = TREE_TYPE (*parm);
15823 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15824 }
15825
15826 /* DR 322. For conversion deduction, remove a reference type on parm
15827 too (which has been swapped into ARG). */
15828 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15829 *arg = TREE_TYPE (*arg);
15830
15831 return result;
15832 }
15833
15834 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15835 template which does contain any deducible template parameters; check if
15836 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15837 unify_one_argument. */
15838
15839 static int
15840 check_non_deducible_conversion (tree parm, tree arg, int strict,
15841 int flags, bool explain_p)
15842 {
15843 tree type;
15844
15845 if (!TYPE_P (arg))
15846 type = TREE_TYPE (arg);
15847 else
15848 type = arg;
15849
15850 if (same_type_p (parm, type))
15851 return unify_success (explain_p);
15852
15853 if (strict == DEDUCE_CONV)
15854 {
15855 if (can_convert_arg (type, parm, NULL_TREE, flags,
15856 explain_p ? tf_warning_or_error : tf_none))
15857 return unify_success (explain_p);
15858 }
15859 else if (strict != DEDUCE_EXACT)
15860 {
15861 if (can_convert_arg (parm, type,
15862 TYPE_P (arg) ? NULL_TREE : arg,
15863 flags, explain_p ? tf_warning_or_error : tf_none))
15864 return unify_success (explain_p);
15865 }
15866
15867 if (strict == DEDUCE_EXACT)
15868 return unify_type_mismatch (explain_p, parm, arg);
15869 else
15870 return unify_arg_conversion (explain_p, parm, type, arg);
15871 }
15872
15873 static bool uses_deducible_template_parms (tree type);
15874
15875 /* Returns true iff the expression EXPR is one from which a template
15876 argument can be deduced. In other words, if it's an undecorated
15877 use of a template non-type parameter. */
15878
15879 static bool
15880 deducible_expression (tree expr)
15881 {
15882 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15883 }
15884
15885 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15886 deducible way; that is, if it has a max value of <PARM> - 1. */
15887
15888 static bool
15889 deducible_array_bound (tree domain)
15890 {
15891 if (domain == NULL_TREE)
15892 return false;
15893
15894 tree max = TYPE_MAX_VALUE (domain);
15895 if (TREE_CODE (max) != MINUS_EXPR)
15896 return false;
15897
15898 return deducible_expression (TREE_OPERAND (max, 0));
15899 }
15900
15901 /* Returns true iff the template arguments ARGS use a template parameter
15902 in a deducible way. */
15903
15904 static bool
15905 deducible_template_args (tree args)
15906 {
15907 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15908 {
15909 bool deducible;
15910 tree elt = TREE_VEC_ELT (args, i);
15911 if (ARGUMENT_PACK_P (elt))
15912 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15913 else
15914 {
15915 if (PACK_EXPANSION_P (elt))
15916 elt = PACK_EXPANSION_PATTERN (elt);
15917 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15918 deducible = true;
15919 else if (TYPE_P (elt))
15920 deducible = uses_deducible_template_parms (elt);
15921 else
15922 deducible = deducible_expression (elt);
15923 }
15924 if (deducible)
15925 return true;
15926 }
15927 return false;
15928 }
15929
15930 /* Returns true iff TYPE contains any deducible references to template
15931 parameters, as per 14.8.2.5. */
15932
15933 static bool
15934 uses_deducible_template_parms (tree type)
15935 {
15936 if (PACK_EXPANSION_P (type))
15937 type = PACK_EXPANSION_PATTERN (type);
15938
15939 /* T
15940 cv-list T
15941 TT<T>
15942 TT<i>
15943 TT<> */
15944 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15945 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15946 return true;
15947
15948 /* T*
15949 T&
15950 T&& */
15951 if (POINTER_TYPE_P (type))
15952 return uses_deducible_template_parms (TREE_TYPE (type));
15953
15954 /* T[integer-constant ]
15955 type [i] */
15956 if (TREE_CODE (type) == ARRAY_TYPE)
15957 return (uses_deducible_template_parms (TREE_TYPE (type))
15958 || deducible_array_bound (TYPE_DOMAIN (type)));
15959
15960 /* T type ::*
15961 type T::*
15962 T T::*
15963 T (type ::*)()
15964 type (T::*)()
15965 type (type ::*)(T)
15966 type (T::*)(T)
15967 T (type ::*)(T)
15968 T (T::*)()
15969 T (T::*)(T) */
15970 if (TYPE_PTRMEM_P (type))
15971 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15972 || (uses_deducible_template_parms
15973 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15974
15975 /* template-name <T> (where template-name refers to a class template)
15976 template-name <i> (where template-name refers to a class template) */
15977 if (CLASS_TYPE_P (type)
15978 && CLASSTYPE_TEMPLATE_INFO (type)
15979 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15980 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15981 (CLASSTYPE_TI_ARGS (type)));
15982
15983 /* type (T)
15984 T()
15985 T(T) */
15986 if (TREE_CODE (type) == FUNCTION_TYPE
15987 || TREE_CODE (type) == METHOD_TYPE)
15988 {
15989 if (uses_deducible_template_parms (TREE_TYPE (type)))
15990 return true;
15991 tree parm = TYPE_ARG_TYPES (type);
15992 if (TREE_CODE (type) == METHOD_TYPE)
15993 parm = TREE_CHAIN (parm);
15994 for (; parm; parm = TREE_CHAIN (parm))
15995 if (uses_deducible_template_parms (TREE_VALUE (parm)))
15996 return true;
15997 }
15998
15999 return false;
16000 }
16001
16002 /* Subroutine of type_unification_real and unify_pack_expansion to
16003 handle unification of a single P/A pair. Parameters are as
16004 for those functions. */
16005
16006 static int
16007 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16008 int subr, unification_kind_t strict, int flags,
16009 bool explain_p)
16010 {
16011 tree arg_expr = NULL_TREE;
16012 int arg_strict;
16013
16014 if (arg == error_mark_node || parm == error_mark_node)
16015 return unify_invalid (explain_p);
16016 if (arg == unknown_type_node)
16017 /* We can't deduce anything from this, but we might get all the
16018 template args from other function args. */
16019 return unify_success (explain_p);
16020
16021 /* Implicit conversions (Clause 4) will be performed on a function
16022 argument to convert it to the type of the corresponding function
16023 parameter if the parameter type contains no template-parameters that
16024 participate in template argument deduction. */
16025 if (TYPE_P (parm) && !uses_template_parms (parm))
16026 /* For function parameters that contain no template-parameters at all,
16027 we have historically checked for convertibility in order to shortcut
16028 consideration of this candidate. */
16029 return check_non_deducible_conversion (parm, arg, strict, flags,
16030 explain_p);
16031 else if (strict == DEDUCE_CALL
16032 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16033 /* For function parameters with only non-deducible template parameters,
16034 just return. */
16035 return unify_success (explain_p);
16036
16037 switch (strict)
16038 {
16039 case DEDUCE_CALL:
16040 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16041 | UNIFY_ALLOW_MORE_CV_QUAL
16042 | UNIFY_ALLOW_DERIVED);
16043 break;
16044
16045 case DEDUCE_CONV:
16046 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16047 break;
16048
16049 case DEDUCE_EXACT:
16050 arg_strict = UNIFY_ALLOW_NONE;
16051 break;
16052
16053 default:
16054 gcc_unreachable ();
16055 }
16056
16057 /* We only do these transformations if this is the top-level
16058 parameter_type_list in a call or declaration matching; in other
16059 situations (nested function declarators, template argument lists) we
16060 won't be comparing a type to an expression, and we don't do any type
16061 adjustments. */
16062 if (!subr)
16063 {
16064 if (!TYPE_P (arg))
16065 {
16066 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16067 if (type_unknown_p (arg))
16068 {
16069 /* [temp.deduct.type] A template-argument can be
16070 deduced from a pointer to function or pointer
16071 to member function argument if the set of
16072 overloaded functions does not contain function
16073 templates and at most one of a set of
16074 overloaded functions provides a unique
16075 match. */
16076
16077 if (resolve_overloaded_unification
16078 (tparms, targs, parm, arg, strict,
16079 arg_strict, explain_p))
16080 return unify_success (explain_p);
16081 return unify_overload_resolution_failure (explain_p, arg);
16082 }
16083
16084 arg_expr = arg;
16085 arg = unlowered_expr_type (arg);
16086 if (arg == error_mark_node)
16087 return unify_invalid (explain_p);
16088 }
16089
16090 arg_strict |=
16091 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16092 }
16093 else
16094 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16095 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16096
16097 /* For deduction from an init-list we need the actual list. */
16098 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16099 arg = arg_expr;
16100 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16101 }
16102
16103 /* Most parms like fn_type_unification.
16104
16105 If SUBR is 1, we're being called recursively (to unify the
16106 arguments of a function or method parameter of a function
16107 template).
16108
16109 CHECKS is a pointer to a vector of access checks encountered while
16110 substituting default template arguments. */
16111
16112 static int
16113 type_unification_real (tree tparms,
16114 tree targs,
16115 tree xparms,
16116 const tree *xargs,
16117 unsigned int xnargs,
16118 int subr,
16119 unification_kind_t strict,
16120 int flags,
16121 vec<deferred_access_check, va_gc> **checks,
16122 bool explain_p)
16123 {
16124 tree parm, arg;
16125 int i;
16126 int ntparms = TREE_VEC_LENGTH (tparms);
16127 int saw_undeduced = 0;
16128 tree parms;
16129 const tree *args;
16130 unsigned int nargs;
16131 unsigned int ia;
16132
16133 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16134 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16135 gcc_assert (ntparms > 0);
16136
16137 /* Reset the number of non-defaulted template arguments contained
16138 in TARGS. */
16139 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16140
16141 again:
16142 parms = xparms;
16143 args = xargs;
16144 nargs = xnargs;
16145
16146 ia = 0;
16147 while (parms && parms != void_list_node
16148 && ia < nargs)
16149 {
16150 parm = TREE_VALUE (parms);
16151
16152 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16153 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16154 /* For a function parameter pack that occurs at the end of the
16155 parameter-declaration-list, the type A of each remaining
16156 argument of the call is compared with the type P of the
16157 declarator-id of the function parameter pack. */
16158 break;
16159
16160 parms = TREE_CHAIN (parms);
16161
16162 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16163 /* For a function parameter pack that does not occur at the
16164 end of the parameter-declaration-list, the type of the
16165 parameter pack is a non-deduced context. */
16166 continue;
16167
16168 arg = args[ia];
16169 ++ia;
16170
16171 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16172 flags, explain_p))
16173 return 1;
16174 }
16175
16176 if (parms
16177 && parms != void_list_node
16178 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16179 {
16180 /* Unify the remaining arguments with the pack expansion type. */
16181 tree argvec;
16182 tree parmvec = make_tree_vec (1);
16183
16184 /* Allocate a TREE_VEC and copy in all of the arguments */
16185 argvec = make_tree_vec (nargs - ia);
16186 for (i = 0; ia < nargs; ++ia, ++i)
16187 TREE_VEC_ELT (argvec, i) = args[ia];
16188
16189 /* Copy the parameter into parmvec. */
16190 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16191 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16192 /*subr=*/subr, explain_p))
16193 return 1;
16194
16195 /* Advance to the end of the list of parameters. */
16196 parms = TREE_CHAIN (parms);
16197 }
16198
16199 /* Fail if we've reached the end of the parm list, and more args
16200 are present, and the parm list isn't variadic. */
16201 if (ia < nargs && parms == void_list_node)
16202 return unify_too_many_arguments (explain_p, nargs, ia);
16203 /* Fail if parms are left and they don't have default values. */
16204 if (parms && parms != void_list_node
16205 && TREE_PURPOSE (parms) == NULL_TREE)
16206 {
16207 unsigned int count = nargs;
16208 tree p = parms;
16209 while (p && p != void_list_node)
16210 {
16211 count++;
16212 p = TREE_CHAIN (p);
16213 }
16214 return unify_too_few_arguments (explain_p, ia, count);
16215 }
16216
16217 if (!subr)
16218 {
16219 tsubst_flags_t complain = (explain_p
16220 ? tf_warning_or_error
16221 : tf_none);
16222
16223 for (i = 0; i < ntparms; i++)
16224 {
16225 tree targ = TREE_VEC_ELT (targs, i);
16226 tree tparm = TREE_VEC_ELT (tparms, i);
16227
16228 /* Clear the "incomplete" flags on all argument packs now so that
16229 substituting them into later default arguments works. */
16230 if (targ && ARGUMENT_PACK_P (targ))
16231 {
16232 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16233 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16234 }
16235
16236 if (targ || tparm == error_mark_node)
16237 continue;
16238 tparm = TREE_VALUE (tparm);
16239
16240 /* If this is an undeduced nontype parameter that depends on
16241 a type parameter, try another pass; its type may have been
16242 deduced from a later argument than the one from which
16243 this parameter can be deduced. */
16244 if (TREE_CODE (tparm) == PARM_DECL
16245 && uses_template_parms (TREE_TYPE (tparm))
16246 && !saw_undeduced++)
16247 goto again;
16248
16249 /* Core issue #226 (C++0x) [temp.deduct]:
16250
16251 If a template argument has not been deduced, its
16252 default template argument, if any, is used.
16253
16254 When we are in C++98 mode, TREE_PURPOSE will either
16255 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16256 to explicitly check cxx_dialect here. */
16257 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16258 {
16259 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16260 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16261 reopen_deferring_access_checks (*checks);
16262 location_t save_loc = input_location;
16263 if (DECL_P (parm))
16264 input_location = DECL_SOURCE_LOCATION (parm);
16265 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16266 arg = convert_template_argument (parm, arg, targs, complain,
16267 i, NULL_TREE);
16268 input_location = save_loc;
16269 *checks = get_deferred_access_checks ();
16270 pop_deferring_access_checks ();
16271 if (arg == error_mark_node)
16272 return 1;
16273 else
16274 {
16275 TREE_VEC_ELT (targs, i) = arg;
16276 /* The position of the first default template argument,
16277 is also the number of non-defaulted arguments in TARGS.
16278 Record that. */
16279 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16280 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16281 continue;
16282 }
16283 }
16284
16285 /* If the type parameter is a parameter pack, then it will
16286 be deduced to an empty parameter pack. */
16287 if (template_parameter_pack_p (tparm))
16288 {
16289 tree arg;
16290
16291 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16292 {
16293 arg = make_node (NONTYPE_ARGUMENT_PACK);
16294 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16295 TREE_CONSTANT (arg) = 1;
16296 }
16297 else
16298 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16299
16300 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16301
16302 TREE_VEC_ELT (targs, i) = arg;
16303 continue;
16304 }
16305
16306 return unify_parameter_deduction_failure (explain_p, tparm);
16307 }
16308 }
16309 #ifdef ENABLE_CHECKING
16310 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16312 #endif
16313
16314 return unify_success (explain_p);
16315 }
16316
16317 /* Subroutine of type_unification_real. Args are like the variables
16318 at the call site. ARG is an overloaded function (or template-id);
16319 we try deducing template args from each of the overloads, and if
16320 only one succeeds, we go with that. Modifies TARGS and returns
16321 true on success. */
16322
16323 static bool
16324 resolve_overloaded_unification (tree tparms,
16325 tree targs,
16326 tree parm,
16327 tree arg,
16328 unification_kind_t strict,
16329 int sub_strict,
16330 bool explain_p)
16331 {
16332 tree tempargs = copy_node (targs);
16333 int good = 0;
16334 tree goodfn = NULL_TREE;
16335 bool addr_p;
16336
16337 if (TREE_CODE (arg) == ADDR_EXPR)
16338 {
16339 arg = TREE_OPERAND (arg, 0);
16340 addr_p = true;
16341 }
16342 else
16343 addr_p = false;
16344
16345 if (TREE_CODE (arg) == COMPONENT_REF)
16346 /* Handle `&x' where `x' is some static or non-static member
16347 function name. */
16348 arg = TREE_OPERAND (arg, 1);
16349
16350 if (TREE_CODE (arg) == OFFSET_REF)
16351 arg = TREE_OPERAND (arg, 1);
16352
16353 /* Strip baselink information. */
16354 if (BASELINK_P (arg))
16355 arg = BASELINK_FUNCTIONS (arg);
16356
16357 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16358 {
16359 /* If we got some explicit template args, we need to plug them into
16360 the affected templates before we try to unify, in case the
16361 explicit args will completely resolve the templates in question. */
16362
16363 int ok = 0;
16364 tree expl_subargs = TREE_OPERAND (arg, 1);
16365 arg = TREE_OPERAND (arg, 0);
16366
16367 for (; arg; arg = OVL_NEXT (arg))
16368 {
16369 tree fn = OVL_CURRENT (arg);
16370 tree subargs, elem;
16371
16372 if (TREE_CODE (fn) != TEMPLATE_DECL)
16373 continue;
16374
16375 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16376 expl_subargs, NULL_TREE, tf_none,
16377 /*require_all_args=*/true,
16378 /*use_default_args=*/true);
16379 if (subargs != error_mark_node
16380 && !any_dependent_template_arguments_p (subargs))
16381 {
16382 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16383 if (try_one_overload (tparms, targs, tempargs, parm,
16384 elem, strict, sub_strict, addr_p, explain_p)
16385 && (!goodfn || !same_type_p (goodfn, elem)))
16386 {
16387 goodfn = elem;
16388 ++good;
16389 }
16390 }
16391 else if (subargs)
16392 ++ok;
16393 }
16394 /* If no templates (or more than one) are fully resolved by the
16395 explicit arguments, this template-id is a non-deduced context; it
16396 could still be OK if we deduce all template arguments for the
16397 enclosing call through other arguments. */
16398 if (good != 1)
16399 good = ok;
16400 }
16401 else if (TREE_CODE (arg) != OVERLOAD
16402 && TREE_CODE (arg) != FUNCTION_DECL)
16403 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16404 -- but the deduction does not succeed because the expression is
16405 not just the function on its own. */
16406 return false;
16407 else
16408 for (; arg; arg = OVL_NEXT (arg))
16409 if (try_one_overload (tparms, targs, tempargs, parm,
16410 TREE_TYPE (OVL_CURRENT (arg)),
16411 strict, sub_strict, addr_p, explain_p)
16412 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16413 {
16414 goodfn = OVL_CURRENT (arg);
16415 ++good;
16416 }
16417
16418 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16419 to function or pointer to member function argument if the set of
16420 overloaded functions does not contain function templates and at most
16421 one of a set of overloaded functions provides a unique match.
16422
16423 So if we found multiple possibilities, we return success but don't
16424 deduce anything. */
16425
16426 if (good == 1)
16427 {
16428 int i = TREE_VEC_LENGTH (targs);
16429 for (; i--; )
16430 if (TREE_VEC_ELT (tempargs, i))
16431 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16432 }
16433 if (good)
16434 return true;
16435
16436 return false;
16437 }
16438
16439 /* Core DR 115: In contexts where deduction is done and fails, or in
16440 contexts where deduction is not done, if a template argument list is
16441 specified and it, along with any default template arguments, identifies
16442 a single function template specialization, then the template-id is an
16443 lvalue for the function template specialization. */
16444
16445 tree
16446 resolve_nondeduced_context (tree orig_expr)
16447 {
16448 tree expr, offset, baselink;
16449 bool addr;
16450
16451 if (!type_unknown_p (orig_expr))
16452 return orig_expr;
16453
16454 expr = orig_expr;
16455 addr = false;
16456 offset = NULL_TREE;
16457 baselink = NULL_TREE;
16458
16459 if (TREE_CODE (expr) == ADDR_EXPR)
16460 {
16461 expr = TREE_OPERAND (expr, 0);
16462 addr = true;
16463 }
16464 if (TREE_CODE (expr) == OFFSET_REF)
16465 {
16466 offset = expr;
16467 expr = TREE_OPERAND (expr, 1);
16468 }
16469 if (BASELINK_P (expr))
16470 {
16471 baselink = expr;
16472 expr = BASELINK_FUNCTIONS (expr);
16473 }
16474
16475 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16476 {
16477 int good = 0;
16478 tree goodfn = NULL_TREE;
16479
16480 /* If we got some explicit template args, we need to plug them into
16481 the affected templates before we try to unify, in case the
16482 explicit args will completely resolve the templates in question. */
16483
16484 tree expl_subargs = TREE_OPERAND (expr, 1);
16485 tree arg = TREE_OPERAND (expr, 0);
16486 tree badfn = NULL_TREE;
16487 tree badargs = NULL_TREE;
16488
16489 for (; arg; arg = OVL_NEXT (arg))
16490 {
16491 tree fn = OVL_CURRENT (arg);
16492 tree subargs, elem;
16493
16494 if (TREE_CODE (fn) != TEMPLATE_DECL)
16495 continue;
16496
16497 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16498 expl_subargs, NULL_TREE, tf_none,
16499 /*require_all_args=*/true,
16500 /*use_default_args=*/true);
16501 if (subargs != error_mark_node
16502 && !any_dependent_template_arguments_p (subargs))
16503 {
16504 elem = instantiate_template (fn, subargs, tf_none);
16505 if (elem == error_mark_node)
16506 {
16507 badfn = fn;
16508 badargs = subargs;
16509 }
16510 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16511 {
16512 goodfn = elem;
16513 ++good;
16514 }
16515 }
16516 }
16517 if (good == 1)
16518 {
16519 mark_used (goodfn);
16520 expr = goodfn;
16521 if (baselink)
16522 expr = build_baselink (BASELINK_BINFO (baselink),
16523 BASELINK_ACCESS_BINFO (baselink),
16524 expr, BASELINK_OPTYPE (baselink));
16525 if (offset)
16526 {
16527 tree base
16528 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16529 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16530 }
16531 if (addr)
16532 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16533 return expr;
16534 }
16535 else if (good == 0 && badargs)
16536 /* There were no good options and at least one bad one, so let the
16537 user know what the problem is. */
16538 instantiate_template (badfn, badargs, tf_warning_or_error);
16539 }
16540 return orig_expr;
16541 }
16542
16543 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16544 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16545 different overloads deduce different arguments for a given parm.
16546 ADDR_P is true if the expression for which deduction is being
16547 performed was of the form "& fn" rather than simply "fn".
16548
16549 Returns 1 on success. */
16550
16551 static int
16552 try_one_overload (tree tparms,
16553 tree orig_targs,
16554 tree targs,
16555 tree parm,
16556 tree arg,
16557 unification_kind_t strict,
16558 int sub_strict,
16559 bool addr_p,
16560 bool explain_p)
16561 {
16562 int nargs;
16563 tree tempargs;
16564 int i;
16565
16566 if (arg == error_mark_node)
16567 return 0;
16568
16569 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16570 to function or pointer to member function argument if the set of
16571 overloaded functions does not contain function templates and at most
16572 one of a set of overloaded functions provides a unique match.
16573
16574 So if this is a template, just return success. */
16575
16576 if (uses_template_parms (arg))
16577 return 1;
16578
16579 if (TREE_CODE (arg) == METHOD_TYPE)
16580 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16581 else if (addr_p)
16582 arg = build_pointer_type (arg);
16583
16584 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16585
16586 /* We don't copy orig_targs for this because if we have already deduced
16587 some template args from previous args, unify would complain when we
16588 try to deduce a template parameter for the same argument, even though
16589 there isn't really a conflict. */
16590 nargs = TREE_VEC_LENGTH (targs);
16591 tempargs = make_tree_vec (nargs);
16592
16593 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16594 return 0;
16595
16596 /* First make sure we didn't deduce anything that conflicts with
16597 explicitly specified args. */
16598 for (i = nargs; i--; )
16599 {
16600 tree elt = TREE_VEC_ELT (tempargs, i);
16601 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16602
16603 if (!elt)
16604 /*NOP*/;
16605 else if (uses_template_parms (elt))
16606 /* Since we're unifying against ourselves, we will fill in
16607 template args used in the function parm list with our own
16608 template parms. Discard them. */
16609 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16610 else if (oldelt && !template_args_equal (oldelt, elt))
16611 return 0;
16612 }
16613
16614 for (i = nargs; i--; )
16615 {
16616 tree elt = TREE_VEC_ELT (tempargs, i);
16617
16618 if (elt)
16619 TREE_VEC_ELT (targs, i) = elt;
16620 }
16621
16622 return 1;
16623 }
16624
16625 /* PARM is a template class (perhaps with unbound template
16626 parameters). ARG is a fully instantiated type. If ARG can be
16627 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16628 TARGS are as for unify. */
16629
16630 static tree
16631 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16632 bool explain_p)
16633 {
16634 tree copy_of_targs;
16635
16636 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16637 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16638 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16639 return NULL_TREE;
16640
16641 /* We need to make a new template argument vector for the call to
16642 unify. If we used TARGS, we'd clutter it up with the result of
16643 the attempted unification, even if this class didn't work out.
16644 We also don't want to commit ourselves to all the unifications
16645 we've already done, since unification is supposed to be done on
16646 an argument-by-argument basis. In other words, consider the
16647 following pathological case:
16648
16649 template <int I, int J, int K>
16650 struct S {};
16651
16652 template <int I, int J>
16653 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16654
16655 template <int I, int J, int K>
16656 void f(S<I, J, K>, S<I, I, I>);
16657
16658 void g() {
16659 S<0, 0, 0> s0;
16660 S<0, 1, 2> s2;
16661
16662 f(s0, s2);
16663 }
16664
16665 Now, by the time we consider the unification involving `s2', we
16666 already know that we must have `f<0, 0, 0>'. But, even though
16667 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16668 because there are two ways to unify base classes of S<0, 1, 2>
16669 with S<I, I, I>. If we kept the already deduced knowledge, we
16670 would reject the possibility I=1. */
16671 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16672
16673 /* If unification failed, we're done. */
16674 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16675 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16676 return NULL_TREE;
16677
16678 return arg;
16679 }
16680
16681 /* Given a template type PARM and a class type ARG, find the unique
16682 base type in ARG that is an instance of PARM. We do not examine
16683 ARG itself; only its base-classes. If there is not exactly one
16684 appropriate base class, return NULL_TREE. PARM may be the type of
16685 a partial specialization, as well as a plain template type. Used
16686 by unify. */
16687
16688 static enum template_base_result
16689 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16690 bool explain_p, tree *result)
16691 {
16692 tree rval = NULL_TREE;
16693 tree binfo;
16694
16695 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16696
16697 binfo = TYPE_BINFO (complete_type (arg));
16698 if (!binfo)
16699 {
16700 /* The type could not be completed. */
16701 *result = NULL_TREE;
16702 return tbr_incomplete_type;
16703 }
16704
16705 /* Walk in inheritance graph order. The search order is not
16706 important, and this avoids multiple walks of virtual bases. */
16707 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16708 {
16709 tree r = try_class_unification (tparms, targs, parm,
16710 BINFO_TYPE (binfo), explain_p);
16711
16712 if (r)
16713 {
16714 /* If there is more than one satisfactory baseclass, then:
16715
16716 [temp.deduct.call]
16717
16718 If they yield more than one possible deduced A, the type
16719 deduction fails.
16720
16721 applies. */
16722 if (rval && !same_type_p (r, rval))
16723 {
16724 *result = NULL_TREE;
16725 return tbr_ambiguous_baseclass;
16726 }
16727
16728 rval = r;
16729 }
16730 }
16731
16732 *result = rval;
16733 return tbr_success;
16734 }
16735
16736 /* Returns the level of DECL, which declares a template parameter. */
16737
16738 static int
16739 template_decl_level (tree decl)
16740 {
16741 switch (TREE_CODE (decl))
16742 {
16743 case TYPE_DECL:
16744 case TEMPLATE_DECL:
16745 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16746
16747 case PARM_DECL:
16748 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16749
16750 default:
16751 gcc_unreachable ();
16752 }
16753 return 0;
16754 }
16755
16756 /* Decide whether ARG can be unified with PARM, considering only the
16757 cv-qualifiers of each type, given STRICT as documented for unify.
16758 Returns nonzero iff the unification is OK on that basis. */
16759
16760 static int
16761 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16762 {
16763 int arg_quals = cp_type_quals (arg);
16764 int parm_quals = cp_type_quals (parm);
16765
16766 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16767 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16768 {
16769 /* Although a CVR qualifier is ignored when being applied to a
16770 substituted template parameter ([8.3.2]/1 for example), that
16771 does not allow us to unify "const T" with "int&" because both
16772 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16773 It is ok when we're allowing additional CV qualifiers
16774 at the outer level [14.8.2.1]/3,1st bullet. */
16775 if ((TREE_CODE (arg) == REFERENCE_TYPE
16776 || TREE_CODE (arg) == FUNCTION_TYPE
16777 || TREE_CODE (arg) == METHOD_TYPE)
16778 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16779 return 0;
16780
16781 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16782 && (parm_quals & TYPE_QUAL_RESTRICT))
16783 return 0;
16784 }
16785
16786 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16787 && (arg_quals & parm_quals) != parm_quals)
16788 return 0;
16789
16790 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16791 && (parm_quals & arg_quals) != arg_quals)
16792 return 0;
16793
16794 return 1;
16795 }
16796
16797 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16798 void
16799 template_parm_level_and_index (tree parm, int* level, int* index)
16800 {
16801 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16802 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16803 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16804 {
16805 *index = TEMPLATE_TYPE_IDX (parm);
16806 *level = TEMPLATE_TYPE_LEVEL (parm);
16807 }
16808 else
16809 {
16810 *index = TEMPLATE_PARM_IDX (parm);
16811 *level = TEMPLATE_PARM_LEVEL (parm);
16812 }
16813 }
16814
16815 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16816 do { \
16817 if (unify (TP, TA, P, A, S, EP)) \
16818 return 1; \
16819 } while (0);
16820
16821 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16822 expansion at the end of PACKED_PARMS. Returns 0 if the type
16823 deduction succeeds, 1 otherwise. STRICT is the same as in
16824 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16825 call argument list. We'll need to adjust the arguments to make them
16826 types. SUBR tells us if this is from a recursive call to
16827 type_unification_real, or for comparing two template argument
16828 lists. */
16829
16830 static int
16831 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16832 tree packed_args, unification_kind_t strict,
16833 bool subr, bool explain_p)
16834 {
16835 tree parm
16836 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16837 tree pattern = PACK_EXPANSION_PATTERN (parm);
16838 tree pack, packs = NULL_TREE;
16839 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16840 int len = TREE_VEC_LENGTH (packed_args);
16841
16842 /* Determine the parameter packs we will be deducing from the
16843 pattern, and record their current deductions. */
16844 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16845 pack; pack = TREE_CHAIN (pack))
16846 {
16847 tree parm_pack = TREE_VALUE (pack);
16848 int idx, level;
16849
16850 /* Determine the index and level of this parameter pack. */
16851 template_parm_level_and_index (parm_pack, &level, &idx);
16852
16853 /* Keep track of the parameter packs and their corresponding
16854 argument packs. */
16855 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16856 TREE_TYPE (packs) = make_tree_vec (len - start);
16857 }
16858
16859 /* Loop through all of the arguments that have not yet been
16860 unified and unify each with the pattern. */
16861 for (i = start; i < len; i++)
16862 {
16863 tree parm;
16864 bool any_explicit = false;
16865 tree arg = TREE_VEC_ELT (packed_args, i);
16866
16867 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16868 or the element of its argument pack at the current index if
16869 this argument was explicitly specified. */
16870 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16871 {
16872 int idx, level;
16873 tree arg, pargs;
16874 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16875
16876 arg = NULL_TREE;
16877 if (TREE_VALUE (pack)
16878 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16879 && (i - start < TREE_VEC_LENGTH (pargs)))
16880 {
16881 any_explicit = true;
16882 arg = TREE_VEC_ELT (pargs, i - start);
16883 }
16884 TMPL_ARG (targs, level, idx) = arg;
16885 }
16886
16887 /* If we had explicit template arguments, substitute them into the
16888 pattern before deduction. */
16889 if (any_explicit)
16890 {
16891 /* Some arguments might still be unspecified or dependent. */
16892 bool dependent;
16893 ++processing_template_decl;
16894 dependent = any_dependent_template_arguments_p (targs);
16895 if (!dependent)
16896 --processing_template_decl;
16897 parm = tsubst (pattern, targs,
16898 explain_p ? tf_warning_or_error : tf_none,
16899 NULL_TREE);
16900 if (dependent)
16901 --processing_template_decl;
16902 if (parm == error_mark_node)
16903 return 1;
16904 }
16905 else
16906 parm = pattern;
16907
16908 /* Unify the pattern with the current argument. */
16909 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16910 LOOKUP_IMPLICIT, explain_p))
16911 return 1;
16912
16913 /* For each parameter pack, collect the deduced value. */
16914 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16915 {
16916 int idx, level;
16917 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16918
16919 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16920 TMPL_ARG (targs, level, idx);
16921 }
16922 }
16923
16924 /* Verify that the results of unification with the parameter packs
16925 produce results consistent with what we've seen before, and make
16926 the deduced argument packs available. */
16927 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16928 {
16929 tree old_pack = TREE_VALUE (pack);
16930 tree new_args = TREE_TYPE (pack);
16931 int i, len = TREE_VEC_LENGTH (new_args);
16932 int idx, level;
16933 bool nondeduced_p = false;
16934
16935 /* By default keep the original deduced argument pack.
16936 If necessary, more specific code is going to update the
16937 resulting deduced argument later down in this function. */
16938 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16939 TMPL_ARG (targs, level, idx) = old_pack;
16940
16941 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16942 actually deduce anything. */
16943 for (i = 0; i < len && !nondeduced_p; ++i)
16944 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16945 nondeduced_p = true;
16946 if (nondeduced_p)
16947 continue;
16948
16949 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16950 {
16951 /* If we had fewer function args than explicit template args,
16952 just use the explicits. */
16953 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16954 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16955 if (len < explicit_len)
16956 new_args = explicit_args;
16957 }
16958
16959 if (!old_pack)
16960 {
16961 tree result;
16962 /* Build the deduced *_ARGUMENT_PACK. */
16963 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16964 {
16965 result = make_node (NONTYPE_ARGUMENT_PACK);
16966 TREE_TYPE (result) =
16967 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16968 TREE_CONSTANT (result) = 1;
16969 }
16970 else
16971 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16972
16973 SET_ARGUMENT_PACK_ARGS (result, new_args);
16974
16975 /* Note the deduced argument packs for this parameter
16976 pack. */
16977 TMPL_ARG (targs, level, idx) = result;
16978 }
16979 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16980 && (ARGUMENT_PACK_ARGS (old_pack)
16981 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16982 {
16983 /* We only had the explicitly-provided arguments before, but
16984 now we have a complete set of arguments. */
16985 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16986
16987 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16988 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16989 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16990 }
16991 else
16992 {
16993 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16994 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16995
16996 if (!comp_template_args_with_info (old_args, new_args,
16997 &bad_old_arg, &bad_new_arg))
16998 /* Inconsistent unification of this parameter pack. */
16999 return unify_parameter_pack_inconsistent (explain_p,
17000 bad_old_arg,
17001 bad_new_arg);
17002 }
17003 }
17004
17005 return unify_success (explain_p);
17006 }
17007
17008 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17009 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17010 parameters and return value are as for unify. */
17011
17012 static int
17013 unify_array_domain (tree tparms, tree targs,
17014 tree parm_dom, tree arg_dom,
17015 bool explain_p)
17016 {
17017 tree parm_max;
17018 tree arg_max;
17019 bool parm_cst;
17020 bool arg_cst;
17021
17022 /* Our representation of array types uses "N - 1" as the
17023 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17024 not an integer constant. We cannot unify arbitrarily
17025 complex expressions, so we eliminate the MINUS_EXPRs
17026 here. */
17027 parm_max = TYPE_MAX_VALUE (parm_dom);
17028 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17029 if (!parm_cst)
17030 {
17031 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17032 parm_max = TREE_OPERAND (parm_max, 0);
17033 }
17034 arg_max = TYPE_MAX_VALUE (arg_dom);
17035 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17036 if (!arg_cst)
17037 {
17038 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17039 trying to unify the type of a variable with the type
17040 of a template parameter. For example:
17041
17042 template <unsigned int N>
17043 void f (char (&) [N]);
17044 int g();
17045 void h(int i) {
17046 char a[g(i)];
17047 f(a);
17048 }
17049
17050 Here, the type of the ARG will be "int [g(i)]", and
17051 may be a SAVE_EXPR, etc. */
17052 if (TREE_CODE (arg_max) != MINUS_EXPR)
17053 return unify_vla_arg (explain_p, arg_dom);
17054 arg_max = TREE_OPERAND (arg_max, 0);
17055 }
17056
17057 /* If only one of the bounds used a MINUS_EXPR, compensate
17058 by adding one to the other bound. */
17059 if (parm_cst && !arg_cst)
17060 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17061 integer_type_node,
17062 parm_max,
17063 integer_one_node);
17064 else if (arg_cst && !parm_cst)
17065 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17066 integer_type_node,
17067 arg_max,
17068 integer_one_node);
17069
17070 return unify (tparms, targs, parm_max, arg_max,
17071 UNIFY_ALLOW_INTEGER, explain_p);
17072 }
17073
17074 /* Deduce the value of template parameters. TPARMS is the (innermost)
17075 set of template parameters to a template. TARGS is the bindings
17076 for those template parameters, as determined thus far; TARGS may
17077 include template arguments for outer levels of template parameters
17078 as well. PARM is a parameter to a template function, or a
17079 subcomponent of that parameter; ARG is the corresponding argument.
17080 This function attempts to match PARM with ARG in a manner
17081 consistent with the existing assignments in TARGS. If more values
17082 are deduced, then TARGS is updated.
17083
17084 Returns 0 if the type deduction succeeds, 1 otherwise. The
17085 parameter STRICT is a bitwise or of the following flags:
17086
17087 UNIFY_ALLOW_NONE:
17088 Require an exact match between PARM and ARG.
17089 UNIFY_ALLOW_MORE_CV_QUAL:
17090 Allow the deduced ARG to be more cv-qualified (by qualification
17091 conversion) than ARG.
17092 UNIFY_ALLOW_LESS_CV_QUAL:
17093 Allow the deduced ARG to be less cv-qualified than ARG.
17094 UNIFY_ALLOW_DERIVED:
17095 Allow the deduced ARG to be a template base class of ARG,
17096 or a pointer to a template base class of the type pointed to by
17097 ARG.
17098 UNIFY_ALLOW_INTEGER:
17099 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17100 case for more information.
17101 UNIFY_ALLOW_OUTER_LEVEL:
17102 This is the outermost level of a deduction. Used to determine validity
17103 of qualification conversions. A valid qualification conversion must
17104 have const qualified pointers leading up to the inner type which
17105 requires additional CV quals, except at the outer level, where const
17106 is not required [conv.qual]. It would be normal to set this flag in
17107 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17108 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17109 This is the outermost level of a deduction, and PARM can be more CV
17110 qualified at this point.
17111 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17112 This is the outermost level of a deduction, and PARM can be less CV
17113 qualified at this point. */
17114
17115 static int
17116 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17117 bool explain_p)
17118 {
17119 int idx;
17120 tree targ;
17121 tree tparm;
17122 int strict_in = strict;
17123
17124 /* I don't think this will do the right thing with respect to types.
17125 But the only case I've seen it in so far has been array bounds, where
17126 signedness is the only information lost, and I think that will be
17127 okay. */
17128 while (TREE_CODE (parm) == NOP_EXPR)
17129 parm = TREE_OPERAND (parm, 0);
17130
17131 if (arg == error_mark_node)
17132 return unify_invalid (explain_p);
17133 if (arg == unknown_type_node
17134 || arg == init_list_type_node)
17135 /* We can't deduce anything from this, but we might get all the
17136 template args from other function args. */
17137 return unify_success (explain_p);
17138
17139 /* If PARM uses template parameters, then we can't bail out here,
17140 even if ARG == PARM, since we won't record unifications for the
17141 template parameters. We might need them if we're trying to
17142 figure out which of two things is more specialized. */
17143 if (arg == parm && !uses_template_parms (parm))
17144 return unify_success (explain_p);
17145
17146 /* Handle init lists early, so the rest of the function can assume
17147 we're dealing with a type. */
17148 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17149 {
17150 tree elt, elttype;
17151 unsigned i;
17152 tree orig_parm = parm;
17153
17154 /* Replace T with std::initializer_list<T> for deduction. */
17155 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17156 && flag_deduce_init_list)
17157 parm = listify (parm);
17158
17159 if (!is_std_init_list (parm)
17160 && TREE_CODE (parm) != ARRAY_TYPE)
17161 /* We can only deduce from an initializer list argument if the
17162 parameter is std::initializer_list or an array; otherwise this
17163 is a non-deduced context. */
17164 return unify_success (explain_p);
17165
17166 if (TREE_CODE (parm) == ARRAY_TYPE)
17167 elttype = TREE_TYPE (parm);
17168 else
17169 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17170
17171 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17172 {
17173 int elt_strict = strict;
17174
17175 if (elt == error_mark_node)
17176 return unify_invalid (explain_p);
17177
17178 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17179 {
17180 tree type = TREE_TYPE (elt);
17181 /* It should only be possible to get here for a call. */
17182 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17183 elt_strict |= maybe_adjust_types_for_deduction
17184 (DEDUCE_CALL, &elttype, &type, elt);
17185 elt = type;
17186 }
17187
17188 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17189 explain_p);
17190 }
17191
17192 if (TREE_CODE (parm) == ARRAY_TYPE)
17193 {
17194 /* Also deduce from the length of the initializer list. */
17195 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17196 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17197 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17198 idx, explain_p);
17199 }
17200
17201 /* If the std::initializer_list<T> deduction worked, replace the
17202 deduced A with std::initializer_list<A>. */
17203 if (orig_parm != parm)
17204 {
17205 idx = TEMPLATE_TYPE_IDX (orig_parm);
17206 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17207 targ = listify (targ);
17208 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17209 }
17210 return unify_success (explain_p);
17211 }
17212
17213 /* Immediately reject some pairs that won't unify because of
17214 cv-qualification mismatches. */
17215 if (TREE_CODE (arg) == TREE_CODE (parm)
17216 && TYPE_P (arg)
17217 /* It is the elements of the array which hold the cv quals of an array
17218 type, and the elements might be template type parms. We'll check
17219 when we recurse. */
17220 && TREE_CODE (arg) != ARRAY_TYPE
17221 /* We check the cv-qualifiers when unifying with template type
17222 parameters below. We want to allow ARG `const T' to unify with
17223 PARM `T' for example, when computing which of two templates
17224 is more specialized, for example. */
17225 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17226 && !check_cv_quals_for_unify (strict_in, arg, parm))
17227 return unify_cv_qual_mismatch (explain_p, parm, arg);
17228
17229 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17230 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17231 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17232 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17233 strict &= ~UNIFY_ALLOW_DERIVED;
17234 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17235 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17236
17237 switch (TREE_CODE (parm))
17238 {
17239 case TYPENAME_TYPE:
17240 case SCOPE_REF:
17241 case UNBOUND_CLASS_TEMPLATE:
17242 /* In a type which contains a nested-name-specifier, template
17243 argument values cannot be deduced for template parameters used
17244 within the nested-name-specifier. */
17245 return unify_success (explain_p);
17246
17247 case TEMPLATE_TYPE_PARM:
17248 case TEMPLATE_TEMPLATE_PARM:
17249 case BOUND_TEMPLATE_TEMPLATE_PARM:
17250 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17251 if (tparm == error_mark_node)
17252 return unify_invalid (explain_p);
17253
17254 if (TEMPLATE_TYPE_LEVEL (parm)
17255 != template_decl_level (tparm))
17256 /* The PARM is not one we're trying to unify. Just check
17257 to see if it matches ARG. */
17258 {
17259 if (TREE_CODE (arg) == TREE_CODE (parm)
17260 && (is_auto (parm) ? is_auto (arg)
17261 : same_type_p (parm, arg)))
17262 return unify_success (explain_p);
17263 else
17264 return unify_type_mismatch (explain_p, parm, arg);
17265 }
17266 idx = TEMPLATE_TYPE_IDX (parm);
17267 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17268 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17269 if (tparm == error_mark_node)
17270 return unify_invalid (explain_p);
17271
17272 /* Check for mixed types and values. */
17273 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17274 && TREE_CODE (tparm) != TYPE_DECL)
17275 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17276 && TREE_CODE (tparm) != TEMPLATE_DECL))
17277 gcc_unreachable ();
17278
17279 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17280 {
17281 /* ARG must be constructed from a template class or a template
17282 template parameter. */
17283 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17284 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17285 return unify_template_deduction_failure (explain_p, parm, arg);
17286
17287 {
17288 tree parmvec = TYPE_TI_ARGS (parm);
17289 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17290 tree full_argvec = add_to_template_args (targs, argvec);
17291 tree parm_parms
17292 = DECL_INNERMOST_TEMPLATE_PARMS
17293 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17294 int i, len;
17295 int parm_variadic_p = 0;
17296
17297 /* The resolution to DR150 makes clear that default
17298 arguments for an N-argument may not be used to bind T
17299 to a template template parameter with fewer than N
17300 parameters. It is not safe to permit the binding of
17301 default arguments as an extension, as that may change
17302 the meaning of a conforming program. Consider:
17303
17304 struct Dense { static const unsigned int dim = 1; };
17305
17306 template <template <typename> class View,
17307 typename Block>
17308 void operator+(float, View<Block> const&);
17309
17310 template <typename Block,
17311 unsigned int Dim = Block::dim>
17312 struct Lvalue_proxy { operator float() const; };
17313
17314 void
17315 test_1d (void) {
17316 Lvalue_proxy<Dense> p;
17317 float b;
17318 b + p;
17319 }
17320
17321 Here, if Lvalue_proxy is permitted to bind to View, then
17322 the global operator+ will be used; if they are not, the
17323 Lvalue_proxy will be converted to float. */
17324 if (coerce_template_parms (parm_parms,
17325 full_argvec,
17326 TYPE_TI_TEMPLATE (parm),
17327 (explain_p
17328 ? tf_warning_or_error
17329 : tf_none),
17330 /*require_all_args=*/true,
17331 /*use_default_args=*/false)
17332 == error_mark_node)
17333 return 1;
17334
17335 /* Deduce arguments T, i from TT<T> or TT<i>.
17336 We check each element of PARMVEC and ARGVEC individually
17337 rather than the whole TREE_VEC since they can have
17338 different number of elements. */
17339
17340 parmvec = expand_template_argument_pack (parmvec);
17341 argvec = expand_template_argument_pack (argvec);
17342
17343 len = TREE_VEC_LENGTH (parmvec);
17344
17345 /* Check if the parameters end in a pack, making them
17346 variadic. */
17347 if (len > 0
17348 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17349 parm_variadic_p = 1;
17350
17351 for (i = 0; i < len - parm_variadic_p; ++i)
17352 /* If the template argument list of P contains a pack
17353 expansion that is not the last template argument, the
17354 entire template argument list is a non-deduced
17355 context. */
17356 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17357 return unify_success (explain_p);
17358
17359 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17360 return unify_too_few_arguments (explain_p,
17361 TREE_VEC_LENGTH (argvec), len);
17362
17363 for (i = 0; i < len - parm_variadic_p; ++i)
17364 {
17365 RECUR_AND_CHECK_FAILURE (tparms, targs,
17366 TREE_VEC_ELT (parmvec, i),
17367 TREE_VEC_ELT (argvec, i),
17368 UNIFY_ALLOW_NONE, explain_p);
17369 }
17370
17371 if (parm_variadic_p
17372 && unify_pack_expansion (tparms, targs,
17373 parmvec, argvec,
17374 DEDUCE_EXACT,
17375 /*subr=*/true, explain_p))
17376 return 1;
17377 }
17378 arg = TYPE_TI_TEMPLATE (arg);
17379
17380 /* Fall through to deduce template name. */
17381 }
17382
17383 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17384 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17385 {
17386 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17387
17388 /* Simple cases: Value already set, does match or doesn't. */
17389 if (targ != NULL_TREE && template_args_equal (targ, arg))
17390 return unify_success (explain_p);
17391 else if (targ)
17392 return unify_inconsistency (explain_p, parm, targ, arg);
17393 }
17394 else
17395 {
17396 /* If PARM is `const T' and ARG is only `int', we don't have
17397 a match unless we are allowing additional qualification.
17398 If ARG is `const int' and PARM is just `T' that's OK;
17399 that binds `const int' to `T'. */
17400 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17401 arg, parm))
17402 return unify_cv_qual_mismatch (explain_p, parm, arg);
17403
17404 /* Consider the case where ARG is `const volatile int' and
17405 PARM is `const T'. Then, T should be `volatile int'. */
17406 arg = cp_build_qualified_type_real
17407 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17408 if (arg == error_mark_node)
17409 return unify_invalid (explain_p);
17410
17411 /* Simple cases: Value already set, does match or doesn't. */
17412 if (targ != NULL_TREE && same_type_p (targ, arg))
17413 return unify_success (explain_p);
17414 else if (targ)
17415 return unify_inconsistency (explain_p, parm, targ, arg);
17416
17417 /* Make sure that ARG is not a variable-sized array. (Note
17418 that were talking about variable-sized arrays (like
17419 `int[n]'), rather than arrays of unknown size (like
17420 `int[]').) We'll get very confused by such a type since
17421 the bound of the array is not constant, and therefore
17422 not mangleable. Besides, such types are not allowed in
17423 ISO C++, so we can do as we please here. We do allow
17424 them for 'auto' deduction, since that isn't ABI-exposed. */
17425 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17426 return unify_vla_arg (explain_p, arg);
17427
17428 /* Strip typedefs as in convert_template_argument. */
17429 arg = canonicalize_type_argument (arg, tf_none);
17430 }
17431
17432 /* If ARG is a parameter pack or an expansion, we cannot unify
17433 against it unless PARM is also a parameter pack. */
17434 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17435 && !template_parameter_pack_p (parm))
17436 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17437
17438 /* If the argument deduction results is a METHOD_TYPE,
17439 then there is a problem.
17440 METHOD_TYPE doesn't map to any real C++ type the result of
17441 the deduction can not be of that type. */
17442 if (TREE_CODE (arg) == METHOD_TYPE)
17443 return unify_method_type_error (explain_p, arg);
17444
17445 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17446 return unify_success (explain_p);
17447
17448 case TEMPLATE_PARM_INDEX:
17449 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17450 if (tparm == error_mark_node)
17451 return unify_invalid (explain_p);
17452
17453 if (TEMPLATE_PARM_LEVEL (parm)
17454 != template_decl_level (tparm))
17455 {
17456 /* The PARM is not one we're trying to unify. Just check
17457 to see if it matches ARG. */
17458 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17459 && cp_tree_equal (parm, arg));
17460 if (result)
17461 unify_expression_unequal (explain_p, parm, arg);
17462 return result;
17463 }
17464
17465 idx = TEMPLATE_PARM_IDX (parm);
17466 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17467
17468 if (targ)
17469 {
17470 int x = !cp_tree_equal (targ, arg);
17471 if (x)
17472 unify_inconsistency (explain_p, parm, targ, arg);
17473 return x;
17474 }
17475
17476 /* [temp.deduct.type] If, in the declaration of a function template
17477 with a non-type template-parameter, the non-type
17478 template-parameter is used in an expression in the function
17479 parameter-list and, if the corresponding template-argument is
17480 deduced, the template-argument type shall match the type of the
17481 template-parameter exactly, except that a template-argument
17482 deduced from an array bound may be of any integral type.
17483 The non-type parameter might use already deduced type parameters. */
17484 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17485 if (!TREE_TYPE (arg))
17486 /* Template-parameter dependent expression. Just accept it for now.
17487 It will later be processed in convert_template_argument. */
17488 ;
17489 else if (same_type_p (TREE_TYPE (arg), tparm))
17490 /* OK */;
17491 else if ((strict & UNIFY_ALLOW_INTEGER)
17492 && CP_INTEGRAL_TYPE_P (tparm))
17493 /* Convert the ARG to the type of PARM; the deduced non-type
17494 template argument must exactly match the types of the
17495 corresponding parameter. */
17496 arg = fold (build_nop (tparm, arg));
17497 else if (uses_template_parms (tparm))
17498 /* We haven't deduced the type of this parameter yet. Try again
17499 later. */
17500 return unify_success (explain_p);
17501 else
17502 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17503
17504 /* If ARG is a parameter pack or an expansion, we cannot unify
17505 against it unless PARM is also a parameter pack. */
17506 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17507 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17508 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17509
17510 arg = strip_typedefs_expr (arg);
17511 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17512 return unify_success (explain_p);
17513
17514 case PTRMEM_CST:
17515 {
17516 /* A pointer-to-member constant can be unified only with
17517 another constant. */
17518 if (TREE_CODE (arg) != PTRMEM_CST)
17519 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17520
17521 /* Just unify the class member. It would be useless (and possibly
17522 wrong, depending on the strict flags) to unify also
17523 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17524 arg refer to the same variable, even if through different
17525 classes. For instance:
17526
17527 struct A { int x; };
17528 struct B : A { };
17529
17530 Unification of &A::x and &B::x must succeed. */
17531 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17532 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17533 }
17534
17535 case POINTER_TYPE:
17536 {
17537 if (!TYPE_PTR_P (arg))
17538 return unify_type_mismatch (explain_p, parm, arg);
17539
17540 /* [temp.deduct.call]
17541
17542 A can be another pointer or pointer to member type that can
17543 be converted to the deduced A via a qualification
17544 conversion (_conv.qual_).
17545
17546 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17547 This will allow for additional cv-qualification of the
17548 pointed-to types if appropriate. */
17549
17550 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17551 /* The derived-to-base conversion only persists through one
17552 level of pointers. */
17553 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17554
17555 return unify (tparms, targs, TREE_TYPE (parm),
17556 TREE_TYPE (arg), strict, explain_p);
17557 }
17558
17559 case REFERENCE_TYPE:
17560 if (TREE_CODE (arg) != REFERENCE_TYPE)
17561 return unify_type_mismatch (explain_p, parm, arg);
17562 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17563 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17564
17565 case ARRAY_TYPE:
17566 if (TREE_CODE (arg) != ARRAY_TYPE)
17567 return unify_type_mismatch (explain_p, parm, arg);
17568 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17569 != (TYPE_DOMAIN (arg) == NULL_TREE))
17570 return unify_type_mismatch (explain_p, parm, arg);
17571 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17572 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17573 if (TYPE_DOMAIN (parm) != NULL_TREE)
17574 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17575 TYPE_DOMAIN (arg), explain_p);
17576 return unify_success (explain_p);
17577
17578 case REAL_TYPE:
17579 case COMPLEX_TYPE:
17580 case VECTOR_TYPE:
17581 case INTEGER_TYPE:
17582 case BOOLEAN_TYPE:
17583 case ENUMERAL_TYPE:
17584 case VOID_TYPE:
17585 case NULLPTR_TYPE:
17586 if (TREE_CODE (arg) != TREE_CODE (parm))
17587 return unify_type_mismatch (explain_p, parm, arg);
17588
17589 /* We have already checked cv-qualification at the top of the
17590 function. */
17591 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17592 return unify_type_mismatch (explain_p, parm, arg);
17593
17594 /* As far as unification is concerned, this wins. Later checks
17595 will invalidate it if necessary. */
17596 return unify_success (explain_p);
17597
17598 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17599 /* Type INTEGER_CST can come from ordinary constant template args. */
17600 case INTEGER_CST:
17601 while (TREE_CODE (arg) == NOP_EXPR)
17602 arg = TREE_OPERAND (arg, 0);
17603
17604 if (TREE_CODE (arg) != INTEGER_CST)
17605 return unify_template_argument_mismatch (explain_p, parm, arg);
17606 return (tree_int_cst_equal (parm, arg)
17607 ? unify_success (explain_p)
17608 : unify_template_argument_mismatch (explain_p, parm, arg));
17609
17610 case TREE_VEC:
17611 {
17612 int i, len, argslen;
17613 int parm_variadic_p = 0;
17614
17615 if (TREE_CODE (arg) != TREE_VEC)
17616 return unify_template_argument_mismatch (explain_p, parm, arg);
17617
17618 len = TREE_VEC_LENGTH (parm);
17619 argslen = TREE_VEC_LENGTH (arg);
17620
17621 /* Check for pack expansions in the parameters. */
17622 for (i = 0; i < len; ++i)
17623 {
17624 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17625 {
17626 if (i == len - 1)
17627 /* We can unify against something with a trailing
17628 parameter pack. */
17629 parm_variadic_p = 1;
17630 else
17631 /* [temp.deduct.type]/9: If the template argument list of
17632 P contains a pack expansion that is not the last
17633 template argument, the entire template argument list
17634 is a non-deduced context. */
17635 return unify_success (explain_p);
17636 }
17637 }
17638
17639 /* If we don't have enough arguments to satisfy the parameters
17640 (not counting the pack expression at the end), or we have
17641 too many arguments for a parameter list that doesn't end in
17642 a pack expression, we can't unify. */
17643 if (parm_variadic_p
17644 ? argslen < len - parm_variadic_p
17645 : argslen != len)
17646 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17647
17648 /* Unify all of the parameters that precede the (optional)
17649 pack expression. */
17650 for (i = 0; i < len - parm_variadic_p; ++i)
17651 {
17652 RECUR_AND_CHECK_FAILURE (tparms, targs,
17653 TREE_VEC_ELT (parm, i),
17654 TREE_VEC_ELT (arg, i),
17655 UNIFY_ALLOW_NONE, explain_p);
17656 }
17657 if (parm_variadic_p)
17658 return unify_pack_expansion (tparms, targs, parm, arg,
17659 DEDUCE_EXACT,
17660 /*subr=*/true, explain_p);
17661 return unify_success (explain_p);
17662 }
17663
17664 case RECORD_TYPE:
17665 case UNION_TYPE:
17666 if (TREE_CODE (arg) != TREE_CODE (parm))
17667 return unify_type_mismatch (explain_p, parm, arg);
17668
17669 if (TYPE_PTRMEMFUNC_P (parm))
17670 {
17671 if (!TYPE_PTRMEMFUNC_P (arg))
17672 return unify_type_mismatch (explain_p, parm, arg);
17673
17674 return unify (tparms, targs,
17675 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17676 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17677 strict, explain_p);
17678 }
17679
17680 if (CLASSTYPE_TEMPLATE_INFO (parm))
17681 {
17682 tree t = NULL_TREE;
17683
17684 if (strict_in & UNIFY_ALLOW_DERIVED)
17685 {
17686 /* First, we try to unify the PARM and ARG directly. */
17687 t = try_class_unification (tparms, targs,
17688 parm, arg, explain_p);
17689
17690 if (!t)
17691 {
17692 /* Fallback to the special case allowed in
17693 [temp.deduct.call]:
17694
17695 If P is a class, and P has the form
17696 template-id, then A can be a derived class of
17697 the deduced A. Likewise, if P is a pointer to
17698 a class of the form template-id, A can be a
17699 pointer to a derived class pointed to by the
17700 deduced A. */
17701 enum template_base_result r;
17702 r = get_template_base (tparms, targs, parm, arg,
17703 explain_p, &t);
17704
17705 if (!t)
17706 return unify_no_common_base (explain_p, r, parm, arg);
17707 }
17708 }
17709 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17710 && (CLASSTYPE_TI_TEMPLATE (parm)
17711 == CLASSTYPE_TI_TEMPLATE (arg)))
17712 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17713 Then, we should unify `int' and `U'. */
17714 t = arg;
17715 else
17716 /* There's no chance of unification succeeding. */
17717 return unify_type_mismatch (explain_p, parm, arg);
17718
17719 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17720 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17721 }
17722 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17723 return unify_type_mismatch (explain_p, parm, arg);
17724 return unify_success (explain_p);
17725
17726 case METHOD_TYPE:
17727 case FUNCTION_TYPE:
17728 {
17729 unsigned int nargs;
17730 tree *args;
17731 tree a;
17732 unsigned int i;
17733
17734 if (TREE_CODE (arg) != TREE_CODE (parm))
17735 return unify_type_mismatch (explain_p, parm, arg);
17736
17737 /* CV qualifications for methods can never be deduced, they must
17738 match exactly. We need to check them explicitly here,
17739 because type_unification_real treats them as any other
17740 cv-qualified parameter. */
17741 if (TREE_CODE (parm) == METHOD_TYPE
17742 && (!check_cv_quals_for_unify
17743 (UNIFY_ALLOW_NONE,
17744 class_of_this_parm (arg),
17745 class_of_this_parm (parm))))
17746 return unify_cv_qual_mismatch (explain_p, parm, arg);
17747
17748 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17749 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17750
17751 nargs = list_length (TYPE_ARG_TYPES (arg));
17752 args = XALLOCAVEC (tree, nargs);
17753 for (a = TYPE_ARG_TYPES (arg), i = 0;
17754 a != NULL_TREE && a != void_list_node;
17755 a = TREE_CHAIN (a), ++i)
17756 args[i] = TREE_VALUE (a);
17757 nargs = i;
17758
17759 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17760 args, nargs, 1, DEDUCE_EXACT,
17761 LOOKUP_NORMAL, NULL, explain_p);
17762 }
17763
17764 case OFFSET_TYPE:
17765 /* Unify a pointer to member with a pointer to member function, which
17766 deduces the type of the member as a function type. */
17767 if (TYPE_PTRMEMFUNC_P (arg))
17768 {
17769 /* Check top-level cv qualifiers */
17770 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17771 return unify_cv_qual_mismatch (explain_p, parm, arg);
17772
17773 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17774 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17775 UNIFY_ALLOW_NONE, explain_p);
17776
17777 /* Determine the type of the function we are unifying against. */
17778 tree fntype = static_fn_type (arg);
17779
17780 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17781 }
17782
17783 if (TREE_CODE (arg) != OFFSET_TYPE)
17784 return unify_type_mismatch (explain_p, parm, arg);
17785 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17786 TYPE_OFFSET_BASETYPE (arg),
17787 UNIFY_ALLOW_NONE, explain_p);
17788 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17789 strict, explain_p);
17790
17791 case CONST_DECL:
17792 if (DECL_TEMPLATE_PARM_P (parm))
17793 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17794 if (arg != integral_constant_value (parm))
17795 return unify_template_argument_mismatch (explain_p, parm, arg);
17796 return unify_success (explain_p);
17797
17798 case FIELD_DECL:
17799 case TEMPLATE_DECL:
17800 /* Matched cases are handled by the ARG == PARM test above. */
17801 return unify_template_argument_mismatch (explain_p, parm, arg);
17802
17803 case VAR_DECL:
17804 /* A non-type template parameter that is a variable should be a
17805 an integral constant, in which case, it whould have been
17806 folded into its (constant) value. So we should not be getting
17807 a variable here. */
17808 gcc_unreachable ();
17809
17810 case TYPE_ARGUMENT_PACK:
17811 case NONTYPE_ARGUMENT_PACK:
17812 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17813 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17814
17815 case TYPEOF_TYPE:
17816 case DECLTYPE_TYPE:
17817 case UNDERLYING_TYPE:
17818 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17819 or UNDERLYING_TYPE nodes. */
17820 return unify_success (explain_p);
17821
17822 case ERROR_MARK:
17823 /* Unification fails if we hit an error node. */
17824 return unify_invalid (explain_p);
17825
17826 default:
17827 /* An unresolved overload is a nondeduced context. */
17828 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17829 return unify_success (explain_p);
17830 gcc_assert (EXPR_P (parm));
17831
17832 /* We must be looking at an expression. This can happen with
17833 something like:
17834
17835 template <int I>
17836 void foo(S<I>, S<I + 2>);
17837
17838 This is a "nondeduced context":
17839
17840 [deduct.type]
17841
17842 The nondeduced contexts are:
17843
17844 --A type that is a template-id in which one or more of
17845 the template-arguments is an expression that references
17846 a template-parameter.
17847
17848 In these cases, we assume deduction succeeded, but don't
17849 actually infer any unifications. */
17850
17851 if (!uses_template_parms (parm)
17852 && !template_args_equal (parm, arg))
17853 return unify_expression_unequal (explain_p, parm, arg);
17854 else
17855 return unify_success (explain_p);
17856 }
17857 }
17858 #undef RECUR_AND_CHECK_FAILURE
17859 \f
17860 /* Note that DECL can be defined in this translation unit, if
17861 required. */
17862
17863 static void
17864 mark_definable (tree decl)
17865 {
17866 tree clone;
17867 DECL_NOT_REALLY_EXTERN (decl) = 1;
17868 FOR_EACH_CLONE (clone, decl)
17869 DECL_NOT_REALLY_EXTERN (clone) = 1;
17870 }
17871
17872 /* Called if RESULT is explicitly instantiated, or is a member of an
17873 explicitly instantiated class. */
17874
17875 void
17876 mark_decl_instantiated (tree result, int extern_p)
17877 {
17878 SET_DECL_EXPLICIT_INSTANTIATION (result);
17879
17880 /* If this entity has already been written out, it's too late to
17881 make any modifications. */
17882 if (TREE_ASM_WRITTEN (result))
17883 return;
17884
17885 /* For anonymous namespace we don't need to do anything. */
17886 if (decl_anon_ns_mem_p (result))
17887 {
17888 gcc_assert (!TREE_PUBLIC (result));
17889 return;
17890 }
17891
17892 if (TREE_CODE (result) != FUNCTION_DECL)
17893 /* The TREE_PUBLIC flag for function declarations will have been
17894 set correctly by tsubst. */
17895 TREE_PUBLIC (result) = 1;
17896
17897 /* This might have been set by an earlier implicit instantiation. */
17898 DECL_COMDAT (result) = 0;
17899
17900 if (extern_p)
17901 DECL_NOT_REALLY_EXTERN (result) = 0;
17902 else
17903 {
17904 mark_definable (result);
17905 /* Always make artificials weak. */
17906 if (DECL_ARTIFICIAL (result) && flag_weak)
17907 comdat_linkage (result);
17908 /* For WIN32 we also want to put explicit instantiations in
17909 linkonce sections. */
17910 else if (TREE_PUBLIC (result))
17911 maybe_make_one_only (result);
17912 }
17913
17914 /* If EXTERN_P, then this function will not be emitted -- unless
17915 followed by an explicit instantiation, at which point its linkage
17916 will be adjusted. If !EXTERN_P, then this function will be
17917 emitted here. In neither circumstance do we want
17918 import_export_decl to adjust the linkage. */
17919 DECL_INTERFACE_KNOWN (result) = 1;
17920 }
17921
17922 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17923 important template arguments. If any are missing, we check whether
17924 they're important by using error_mark_node for substituting into any
17925 args that were used for partial ordering (the ones between ARGS and END)
17926 and seeing if it bubbles up. */
17927
17928 static bool
17929 check_undeduced_parms (tree targs, tree args, tree end)
17930 {
17931 bool found = false;
17932 int i;
17933 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17934 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17935 {
17936 found = true;
17937 TREE_VEC_ELT (targs, i) = error_mark_node;
17938 }
17939 if (found)
17940 {
17941 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17942 if (substed == error_mark_node)
17943 return true;
17944 }
17945 return false;
17946 }
17947
17948 /* Given two function templates PAT1 and PAT2, return:
17949
17950 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17951 -1 if PAT2 is more specialized than PAT1.
17952 0 if neither is more specialized.
17953
17954 LEN indicates the number of parameters we should consider
17955 (defaulted parameters should not be considered).
17956
17957 The 1998 std underspecified function template partial ordering, and
17958 DR214 addresses the issue. We take pairs of arguments, one from
17959 each of the templates, and deduce them against each other. One of
17960 the templates will be more specialized if all the *other*
17961 template's arguments deduce against its arguments and at least one
17962 of its arguments *does* *not* deduce against the other template's
17963 corresponding argument. Deduction is done as for class templates.
17964 The arguments used in deduction have reference and top level cv
17965 qualifiers removed. Iff both arguments were originally reference
17966 types *and* deduction succeeds in both directions, an lvalue reference
17967 wins against an rvalue reference and otherwise the template
17968 with the more cv-qualified argument wins for that pairing (if
17969 neither is more cv-qualified, they both are equal). Unlike regular
17970 deduction, after all the arguments have been deduced in this way,
17971 we do *not* verify the deduced template argument values can be
17972 substituted into non-deduced contexts.
17973
17974 The logic can be a bit confusing here, because we look at deduce1 and
17975 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17976 can find template arguments for pat1 to make arg1 look like arg2, that
17977 means that arg2 is at least as specialized as arg1. */
17978
17979 int
17980 more_specialized_fn (tree pat1, tree pat2, int len)
17981 {
17982 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17983 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17984 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17985 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17986 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17987 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17988 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17989 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17990 tree origs1, origs2;
17991 bool lose1 = false;
17992 bool lose2 = false;
17993
17994 /* Remove the this parameter from non-static member functions. If
17995 one is a non-static member function and the other is not a static
17996 member function, remove the first parameter from that function
17997 also. This situation occurs for operator functions where we
17998 locate both a member function (with this pointer) and non-member
17999 operator (with explicit first operand). */
18000 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18001 {
18002 len--; /* LEN is the number of significant arguments for DECL1 */
18003 args1 = TREE_CHAIN (args1);
18004 if (!DECL_STATIC_FUNCTION_P (decl2))
18005 args2 = TREE_CHAIN (args2);
18006 }
18007 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18008 {
18009 args2 = TREE_CHAIN (args2);
18010 if (!DECL_STATIC_FUNCTION_P (decl1))
18011 {
18012 len--;
18013 args1 = TREE_CHAIN (args1);
18014 }
18015 }
18016
18017 /* If only one is a conversion operator, they are unordered. */
18018 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18019 return 0;
18020
18021 /* Consider the return type for a conversion function */
18022 if (DECL_CONV_FN_P (decl1))
18023 {
18024 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18025 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18026 len++;
18027 }
18028
18029 processing_template_decl++;
18030
18031 origs1 = args1;
18032 origs2 = args2;
18033
18034 while (len--
18035 /* Stop when an ellipsis is seen. */
18036 && args1 != NULL_TREE && args2 != NULL_TREE)
18037 {
18038 tree arg1 = TREE_VALUE (args1);
18039 tree arg2 = TREE_VALUE (args2);
18040 int deduce1, deduce2;
18041 int quals1 = -1;
18042 int quals2 = -1;
18043 int ref1 = 0;
18044 int ref2 = 0;
18045
18046 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18047 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18048 {
18049 /* When both arguments are pack expansions, we need only
18050 unify the patterns themselves. */
18051 arg1 = PACK_EXPANSION_PATTERN (arg1);
18052 arg2 = PACK_EXPANSION_PATTERN (arg2);
18053
18054 /* This is the last comparison we need to do. */
18055 len = 0;
18056 }
18057
18058 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18059 {
18060 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18061 arg1 = TREE_TYPE (arg1);
18062 quals1 = cp_type_quals (arg1);
18063 }
18064
18065 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18066 {
18067 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18068 arg2 = TREE_TYPE (arg2);
18069 quals2 = cp_type_quals (arg2);
18070 }
18071
18072 arg1 = TYPE_MAIN_VARIANT (arg1);
18073 arg2 = TYPE_MAIN_VARIANT (arg2);
18074
18075 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18076 {
18077 int i, len2 = list_length (args2);
18078 tree parmvec = make_tree_vec (1);
18079 tree argvec = make_tree_vec (len2);
18080 tree ta = args2;
18081
18082 /* Setup the parameter vector, which contains only ARG1. */
18083 TREE_VEC_ELT (parmvec, 0) = arg1;
18084
18085 /* Setup the argument vector, which contains the remaining
18086 arguments. */
18087 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18088 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18089
18090 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18091 argvec, DEDUCE_EXACT,
18092 /*subr=*/true, /*explain_p=*/false)
18093 == 0);
18094
18095 /* We cannot deduce in the other direction, because ARG1 is
18096 a pack expansion but ARG2 is not. */
18097 deduce2 = 0;
18098 }
18099 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18100 {
18101 int i, len1 = list_length (args1);
18102 tree parmvec = make_tree_vec (1);
18103 tree argvec = make_tree_vec (len1);
18104 tree ta = args1;
18105
18106 /* Setup the parameter vector, which contains only ARG1. */
18107 TREE_VEC_ELT (parmvec, 0) = arg2;
18108
18109 /* Setup the argument vector, which contains the remaining
18110 arguments. */
18111 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18112 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18113
18114 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18115 argvec, DEDUCE_EXACT,
18116 /*subr=*/true, /*explain_p=*/false)
18117 == 0);
18118
18119 /* We cannot deduce in the other direction, because ARG2 is
18120 a pack expansion but ARG1 is not.*/
18121 deduce1 = 0;
18122 }
18123
18124 else
18125 {
18126 /* The normal case, where neither argument is a pack
18127 expansion. */
18128 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18129 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18130 == 0);
18131 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18132 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18133 == 0);
18134 }
18135
18136 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18137 arg2, then arg2 is not as specialized as arg1. */
18138 if (!deduce1)
18139 lose2 = true;
18140 if (!deduce2)
18141 lose1 = true;
18142
18143 /* "If, for a given type, deduction succeeds in both directions
18144 (i.e., the types are identical after the transformations above)
18145 and both P and A were reference types (before being replaced with
18146 the type referred to above):
18147 - if the type from the argument template was an lvalue reference and
18148 the type from the parameter template was not, the argument type is
18149 considered to be more specialized than the other; otherwise,
18150 - if the type from the argument template is more cv-qualified
18151 than the type from the parameter template (as described above),
18152 the argument type is considered to be more specialized than the other;
18153 otherwise,
18154 - neither type is more specialized than the other." */
18155
18156 if (deduce1 && deduce2)
18157 {
18158 if (ref1 && ref2 && ref1 != ref2)
18159 {
18160 if (ref1 > ref2)
18161 lose1 = true;
18162 else
18163 lose2 = true;
18164 }
18165 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18166 {
18167 if ((quals1 & quals2) == quals2)
18168 lose2 = true;
18169 if ((quals1 & quals2) == quals1)
18170 lose1 = true;
18171 }
18172 }
18173
18174 if (lose1 && lose2)
18175 /* We've failed to deduce something in either direction.
18176 These must be unordered. */
18177 break;
18178
18179 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18180 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18181 /* We have already processed all of the arguments in our
18182 handing of the pack expansion type. */
18183 len = 0;
18184
18185 args1 = TREE_CHAIN (args1);
18186 args2 = TREE_CHAIN (args2);
18187 }
18188
18189 /* "In most cases, all template parameters must have values in order for
18190 deduction to succeed, but for partial ordering purposes a template
18191 parameter may remain without a value provided it is not used in the
18192 types being used for partial ordering."
18193
18194 Thus, if we are missing any of the targs1 we need to substitute into
18195 origs1, then pat2 is not as specialized as pat1. This can happen when
18196 there is a nondeduced context. */
18197 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18198 lose2 = true;
18199 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18200 lose1 = true;
18201
18202 processing_template_decl--;
18203
18204 /* All things being equal, if the next argument is a pack expansion
18205 for one function but not for the other, prefer the
18206 non-variadic function. FIXME this is bogus; see c++/41958. */
18207 if (lose1 == lose2
18208 && args1 && TREE_VALUE (args1)
18209 && args2 && TREE_VALUE (args2))
18210 {
18211 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18212 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18213 }
18214
18215 if (lose1 == lose2)
18216 return 0;
18217 else if (!lose1)
18218 return 1;
18219 else
18220 return -1;
18221 }
18222
18223 /* Determine which of two partial specializations of MAIN_TMPL is more
18224 specialized.
18225
18226 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18227 to the first partial specialization. The TREE_VALUE is the
18228 innermost set of template parameters for the partial
18229 specialization. PAT2 is similar, but for the second template.
18230
18231 Return 1 if the first partial specialization is more specialized;
18232 -1 if the second is more specialized; 0 if neither is more
18233 specialized.
18234
18235 See [temp.class.order] for information about determining which of
18236 two templates is more specialized. */
18237
18238 static int
18239 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18240 {
18241 tree targs;
18242 tree tmpl1, tmpl2;
18243 int winner = 0;
18244 bool any_deductions = false;
18245
18246 tmpl1 = TREE_TYPE (pat1);
18247 tmpl2 = TREE_TYPE (pat2);
18248
18249 /* Just like what happens for functions, if we are ordering between
18250 different class template specializations, we may encounter dependent
18251 types in the arguments, and we need our dependency check functions
18252 to behave correctly. */
18253 ++processing_template_decl;
18254 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18255 CLASSTYPE_TI_ARGS (tmpl1),
18256 CLASSTYPE_TI_ARGS (tmpl2));
18257 if (targs)
18258 {
18259 --winner;
18260 any_deductions = true;
18261 }
18262
18263 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18264 CLASSTYPE_TI_ARGS (tmpl2),
18265 CLASSTYPE_TI_ARGS (tmpl1));
18266 if (targs)
18267 {
18268 ++winner;
18269 any_deductions = true;
18270 }
18271 --processing_template_decl;
18272
18273 /* In the case of a tie where at least one of the class templates
18274 has a parameter pack at the end, the template with the most
18275 non-packed parameters wins. */
18276 if (winner == 0
18277 && any_deductions
18278 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18279 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18280 {
18281 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18282 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18283 int len1 = TREE_VEC_LENGTH (args1);
18284 int len2 = TREE_VEC_LENGTH (args2);
18285
18286 /* We don't count the pack expansion at the end. */
18287 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18288 --len1;
18289 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18290 --len2;
18291
18292 if (len1 > len2)
18293 return 1;
18294 else if (len1 < len2)
18295 return -1;
18296 }
18297
18298 return winner;
18299 }
18300
18301 /* Return the template arguments that will produce the function signature
18302 DECL from the function template FN, with the explicit template
18303 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18304 also match. Return NULL_TREE if no satisfactory arguments could be
18305 found. */
18306
18307 static tree
18308 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18309 {
18310 int ntparms = DECL_NTPARMS (fn);
18311 tree targs = make_tree_vec (ntparms);
18312 tree decl_type = TREE_TYPE (decl);
18313 tree decl_arg_types;
18314 tree *args;
18315 unsigned int nargs, ix;
18316 tree arg;
18317
18318 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18319
18320 /* Never do unification on the 'this' parameter. */
18321 decl_arg_types = skip_artificial_parms_for (decl,
18322 TYPE_ARG_TYPES (decl_type));
18323
18324 nargs = list_length (decl_arg_types);
18325 args = XALLOCAVEC (tree, nargs);
18326 for (arg = decl_arg_types, ix = 0;
18327 arg != NULL_TREE && arg != void_list_node;
18328 arg = TREE_CHAIN (arg), ++ix)
18329 args[ix] = TREE_VALUE (arg);
18330
18331 if (fn_type_unification (fn, explicit_args, targs,
18332 args, ix,
18333 (check_rettype || DECL_CONV_FN_P (fn)
18334 ? TREE_TYPE (decl_type) : NULL_TREE),
18335 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18336 /*decltype*/false)
18337 == error_mark_node)
18338 return NULL_TREE;
18339
18340 return targs;
18341 }
18342
18343 /* Return the innermost template arguments that, when applied to a partial
18344 specialization of MAIN_TMPL whose innermost template parameters are
18345 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18346 ARGS.
18347
18348 For example, suppose we have:
18349
18350 template <class T, class U> struct S {};
18351 template <class T> struct S<T*, int> {};
18352
18353 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18354 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18355 int}. The resulting vector will be {double}, indicating that `T'
18356 is bound to `double'. */
18357
18358 static tree
18359 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18360 {
18361 int i, ntparms = TREE_VEC_LENGTH (tparms);
18362 tree deduced_args;
18363 tree innermost_deduced_args;
18364
18365 innermost_deduced_args = make_tree_vec (ntparms);
18366 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18367 {
18368 deduced_args = copy_node (args);
18369 SET_TMPL_ARGS_LEVEL (deduced_args,
18370 TMPL_ARGS_DEPTH (deduced_args),
18371 innermost_deduced_args);
18372 }
18373 else
18374 deduced_args = innermost_deduced_args;
18375
18376 if (unify (tparms, deduced_args,
18377 INNERMOST_TEMPLATE_ARGS (spec_args),
18378 INNERMOST_TEMPLATE_ARGS (args),
18379 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18380 return NULL_TREE;
18381
18382 for (i = 0; i < ntparms; ++i)
18383 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18384 return NULL_TREE;
18385
18386 /* Verify that nondeduced template arguments agree with the type
18387 obtained from argument deduction.
18388
18389 For example:
18390
18391 struct A { typedef int X; };
18392 template <class T, class U> struct C {};
18393 template <class T> struct C<T, typename T::X> {};
18394
18395 Then with the instantiation `C<A, int>', we can deduce that
18396 `T' is `A' but unify () does not check whether `typename T::X'
18397 is `int'. */
18398 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18399 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18400 spec_args, main_tmpl,
18401 tf_none, false, false);
18402 if (spec_args == error_mark_node
18403 /* We only need to check the innermost arguments; the other
18404 arguments will always agree. */
18405 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18406 INNERMOST_TEMPLATE_ARGS (args)))
18407 return NULL_TREE;
18408
18409 /* Now that we have bindings for all of the template arguments,
18410 ensure that the arguments deduced for the template template
18411 parameters have compatible template parameter lists. See the use
18412 of template_template_parm_bindings_ok_p in fn_type_unification
18413 for more information. */
18414 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18415 return NULL_TREE;
18416
18417 return deduced_args;
18418 }
18419
18420 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18421 Return the TREE_LIST node with the most specialized template, if
18422 any. If there is no most specialized template, the error_mark_node
18423 is returned.
18424
18425 Note that this function does not look at, or modify, the
18426 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18427 returned is one of the elements of INSTANTIATIONS, callers may
18428 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18429 and retrieve it from the value returned. */
18430
18431 tree
18432 most_specialized_instantiation (tree templates)
18433 {
18434 tree fn, champ;
18435
18436 ++processing_template_decl;
18437
18438 champ = templates;
18439 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18440 {
18441 int fate = 0;
18442
18443 if (get_bindings (TREE_VALUE (champ),
18444 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18445 NULL_TREE, /*check_ret=*/true))
18446 fate--;
18447
18448 if (get_bindings (TREE_VALUE (fn),
18449 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18450 NULL_TREE, /*check_ret=*/true))
18451 fate++;
18452
18453 if (fate == -1)
18454 champ = fn;
18455 else if (!fate)
18456 {
18457 /* Equally specialized, move to next function. If there
18458 is no next function, nothing's most specialized. */
18459 fn = TREE_CHAIN (fn);
18460 champ = fn;
18461 if (!fn)
18462 break;
18463 }
18464 }
18465
18466 if (champ)
18467 /* Now verify that champ is better than everything earlier in the
18468 instantiation list. */
18469 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18470 if (get_bindings (TREE_VALUE (champ),
18471 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18472 NULL_TREE, /*check_ret=*/true)
18473 || !get_bindings (TREE_VALUE (fn),
18474 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18475 NULL_TREE, /*check_ret=*/true))
18476 {
18477 champ = NULL_TREE;
18478 break;
18479 }
18480
18481 processing_template_decl--;
18482
18483 if (!champ)
18484 return error_mark_node;
18485
18486 return champ;
18487 }
18488
18489 /* If DECL is a specialization of some template, return the most
18490 general such template. Otherwise, returns NULL_TREE.
18491
18492 For example, given:
18493
18494 template <class T> struct S { template <class U> void f(U); };
18495
18496 if TMPL is `template <class U> void S<int>::f(U)' this will return
18497 the full template. This function will not trace past partial
18498 specializations, however. For example, given in addition:
18499
18500 template <class T> struct S<T*> { template <class U> void f(U); };
18501
18502 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18503 `template <class T> template <class U> S<T*>::f(U)'. */
18504
18505 tree
18506 most_general_template (tree decl)
18507 {
18508 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18509 an immediate specialization. */
18510 if (TREE_CODE (decl) == FUNCTION_DECL)
18511 {
18512 if (DECL_TEMPLATE_INFO (decl)) {
18513 decl = DECL_TI_TEMPLATE (decl);
18514
18515 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18516 template friend. */
18517 if (TREE_CODE (decl) != TEMPLATE_DECL)
18518 return NULL_TREE;
18519 } else
18520 return NULL_TREE;
18521 }
18522
18523 /* Look for more and more general templates. */
18524 while (DECL_TEMPLATE_INFO (decl))
18525 {
18526 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18527 (See cp-tree.h for details.) */
18528 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18529 break;
18530
18531 if (CLASS_TYPE_P (TREE_TYPE (decl))
18532 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18533 break;
18534
18535 /* Stop if we run into an explicitly specialized class template. */
18536 if (!DECL_NAMESPACE_SCOPE_P (decl)
18537 && DECL_CONTEXT (decl)
18538 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18539 break;
18540
18541 decl = DECL_TI_TEMPLATE (decl);
18542 }
18543
18544 return decl;
18545 }
18546
18547 /* Return the most specialized of the class template partial
18548 specializations of TMPL which can produce TYPE, a specialization of
18549 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18550 a _TYPE node corresponding to the partial specialization, while the
18551 TREE_PURPOSE is the set of template arguments that must be
18552 substituted into the TREE_TYPE in order to generate TYPE.
18553
18554 If the choice of partial specialization is ambiguous, a diagnostic
18555 is issued, and the error_mark_node is returned. If there are no
18556 partial specializations of TMPL matching TYPE, then NULL_TREE is
18557 returned. */
18558
18559 static tree
18560 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18561 {
18562 tree list = NULL_TREE;
18563 tree t;
18564 tree champ;
18565 int fate;
18566 bool ambiguous_p;
18567 tree args;
18568 tree outer_args = NULL_TREE;
18569
18570 tmpl = most_general_template (tmpl);
18571 args = CLASSTYPE_TI_ARGS (type);
18572
18573 /* For determining which partial specialization to use, only the
18574 innermost args are interesting. */
18575 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18576 {
18577 outer_args = strip_innermost_template_args (args, 1);
18578 args = INNERMOST_TEMPLATE_ARGS (args);
18579 }
18580
18581 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18582 {
18583 tree partial_spec_args;
18584 tree spec_args;
18585 tree spec_tmpl = TREE_VALUE (t);
18586 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18587
18588 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18589
18590 ++processing_template_decl;
18591
18592 if (outer_args)
18593 {
18594 /* Discard the outer levels of args, and then substitute in the
18595 template args from the enclosing class. */
18596 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18597 partial_spec_args = tsubst_template_args
18598 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18599
18600 /* And the same for the partial specialization TEMPLATE_DECL. */
18601 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18602 }
18603
18604 partial_spec_args =
18605 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18606 add_to_template_args (outer_args,
18607 partial_spec_args),
18608 tmpl, tf_none,
18609 /*require_all_args=*/true,
18610 /*use_default_args=*/true);
18611
18612 --processing_template_decl;
18613
18614 if (partial_spec_args == error_mark_node)
18615 return error_mark_node;
18616 if (spec_tmpl == error_mark_node)
18617 return error_mark_node;
18618
18619 ++processing_template_decl;
18620
18621 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18622 spec_args = get_class_bindings (tmpl, parms,
18623 partial_spec_args,
18624 args);
18625
18626 --processing_template_decl;
18627
18628 if (spec_args)
18629 {
18630 if (outer_args)
18631 spec_args = add_to_template_args (outer_args, spec_args);
18632 list = tree_cons (spec_args, orig_parms, list);
18633 TREE_TYPE (list) = TREE_TYPE (t);
18634 }
18635 }
18636
18637 if (! list)
18638 return NULL_TREE;
18639
18640 ambiguous_p = false;
18641 t = list;
18642 champ = t;
18643 t = TREE_CHAIN (t);
18644 for (; t; t = TREE_CHAIN (t))
18645 {
18646 fate = more_specialized_class (tmpl, champ, t);
18647 if (fate == 1)
18648 ;
18649 else
18650 {
18651 if (fate == 0)
18652 {
18653 t = TREE_CHAIN (t);
18654 if (! t)
18655 {
18656 ambiguous_p = true;
18657 break;
18658 }
18659 }
18660 champ = t;
18661 }
18662 }
18663
18664 if (!ambiguous_p)
18665 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18666 {
18667 fate = more_specialized_class (tmpl, champ, t);
18668 if (fate != 1)
18669 {
18670 ambiguous_p = true;
18671 break;
18672 }
18673 }
18674
18675 if (ambiguous_p)
18676 {
18677 const char *str;
18678 char *spaces = NULL;
18679 if (!(complain & tf_error))
18680 return error_mark_node;
18681 error ("ambiguous class template instantiation for %q#T", type);
18682 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18683 for (t = list; t; t = TREE_CHAIN (t))
18684 {
18685 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18686 spaces = spaces ? spaces : get_spaces (str);
18687 }
18688 free (spaces);
18689 return error_mark_node;
18690 }
18691
18692 return champ;
18693 }
18694
18695 /* Explicitly instantiate DECL. */
18696
18697 void
18698 do_decl_instantiation (tree decl, tree storage)
18699 {
18700 tree result = NULL_TREE;
18701 int extern_p = 0;
18702
18703 if (!decl || decl == error_mark_node)
18704 /* An error occurred, for which grokdeclarator has already issued
18705 an appropriate message. */
18706 return;
18707 else if (! DECL_LANG_SPECIFIC (decl))
18708 {
18709 error ("explicit instantiation of non-template %q#D", decl);
18710 return;
18711 }
18712 else if (VAR_P (decl))
18713 {
18714 /* There is an asymmetry here in the way VAR_DECLs and
18715 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18716 the latter, the DECL we get back will be marked as a
18717 template instantiation, and the appropriate
18718 DECL_TEMPLATE_INFO will be set up. This does not happen for
18719 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18720 should handle VAR_DECLs as it currently handles
18721 FUNCTION_DECLs. */
18722 if (!DECL_CLASS_SCOPE_P (decl))
18723 {
18724 error ("%qD is not a static data member of a class template", decl);
18725 return;
18726 }
18727 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18728 if (!result || !VAR_P (result))
18729 {
18730 error ("no matching template for %qD found", decl);
18731 return;
18732 }
18733 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18734 {
18735 error ("type %qT for explicit instantiation %qD does not match "
18736 "declared type %qT", TREE_TYPE (result), decl,
18737 TREE_TYPE (decl));
18738 return;
18739 }
18740 }
18741 else if (TREE_CODE (decl) != FUNCTION_DECL)
18742 {
18743 error ("explicit instantiation of %q#D", decl);
18744 return;
18745 }
18746 else
18747 result = decl;
18748
18749 /* Check for various error cases. Note that if the explicit
18750 instantiation is valid the RESULT will currently be marked as an
18751 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18752 until we get here. */
18753
18754 if (DECL_TEMPLATE_SPECIALIZATION (result))
18755 {
18756 /* DR 259 [temp.spec].
18757
18758 Both an explicit instantiation and a declaration of an explicit
18759 specialization shall not appear in a program unless the explicit
18760 instantiation follows a declaration of the explicit specialization.
18761
18762 For a given set of template parameters, if an explicit
18763 instantiation of a template appears after a declaration of an
18764 explicit specialization for that template, the explicit
18765 instantiation has no effect. */
18766 return;
18767 }
18768 else if (DECL_EXPLICIT_INSTANTIATION (result))
18769 {
18770 /* [temp.spec]
18771
18772 No program shall explicitly instantiate any template more
18773 than once.
18774
18775 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18776 the first instantiation was `extern' and the second is not,
18777 and EXTERN_P for the opposite case. */
18778 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18779 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18780 /* If an "extern" explicit instantiation follows an ordinary
18781 explicit instantiation, the template is instantiated. */
18782 if (extern_p)
18783 return;
18784 }
18785 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18786 {
18787 error ("no matching template for %qD found", result);
18788 return;
18789 }
18790 else if (!DECL_TEMPLATE_INFO (result))
18791 {
18792 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18793 return;
18794 }
18795
18796 if (storage == NULL_TREE)
18797 ;
18798 else if (storage == ridpointers[(int) RID_EXTERN])
18799 {
18800 if (!in_system_header && (cxx_dialect == cxx98))
18801 pedwarn (input_location, OPT_Wpedantic,
18802 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18803 "instantiations");
18804 extern_p = 1;
18805 }
18806 else
18807 error ("storage class %qD applied to template instantiation", storage);
18808
18809 check_explicit_instantiation_namespace (result);
18810 mark_decl_instantiated (result, extern_p);
18811 if (! extern_p)
18812 instantiate_decl (result, /*defer_ok=*/1,
18813 /*expl_inst_class_mem_p=*/false);
18814 }
18815
18816 static void
18817 mark_class_instantiated (tree t, int extern_p)
18818 {
18819 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18820 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18821 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18822 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18823 if (! extern_p)
18824 {
18825 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18826 rest_of_type_compilation (t, 1);
18827 }
18828 }
18829
18830 /* Called from do_type_instantiation through binding_table_foreach to
18831 do recursive instantiation for the type bound in ENTRY. */
18832 static void
18833 bt_instantiate_type_proc (binding_entry entry, void *data)
18834 {
18835 tree storage = *(tree *) data;
18836
18837 if (MAYBE_CLASS_TYPE_P (entry->type)
18838 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18839 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18840 }
18841
18842 /* Called from do_type_instantiation to instantiate a member
18843 (a member function or a static member variable) of an
18844 explicitly instantiated class template. */
18845 static void
18846 instantiate_class_member (tree decl, int extern_p)
18847 {
18848 mark_decl_instantiated (decl, extern_p);
18849 if (! extern_p)
18850 instantiate_decl (decl, /*defer_ok=*/1,
18851 /*expl_inst_class_mem_p=*/true);
18852 }
18853
18854 /* Perform an explicit instantiation of template class T. STORAGE, if
18855 non-null, is the RID for extern, inline or static. COMPLAIN is
18856 nonzero if this is called from the parser, zero if called recursively,
18857 since the standard is unclear (as detailed below). */
18858
18859 void
18860 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18861 {
18862 int extern_p = 0;
18863 int nomem_p = 0;
18864 int static_p = 0;
18865 int previous_instantiation_extern_p = 0;
18866
18867 if (TREE_CODE (t) == TYPE_DECL)
18868 t = TREE_TYPE (t);
18869
18870 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18871 {
18872 tree tmpl =
18873 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18874 if (tmpl)
18875 error ("explicit instantiation of non-class template %qD", tmpl);
18876 else
18877 error ("explicit instantiation of non-template type %qT", t);
18878 return;
18879 }
18880
18881 complete_type (t);
18882
18883 if (!COMPLETE_TYPE_P (t))
18884 {
18885 if (complain & tf_error)
18886 error ("explicit instantiation of %q#T before definition of template",
18887 t);
18888 return;
18889 }
18890
18891 if (storage != NULL_TREE)
18892 {
18893 if (!in_system_header)
18894 {
18895 if (storage == ridpointers[(int) RID_EXTERN])
18896 {
18897 if (cxx_dialect == cxx98)
18898 pedwarn (input_location, OPT_Wpedantic,
18899 "ISO C++ 1998 forbids the use of %<extern%> on "
18900 "explicit instantiations");
18901 }
18902 else
18903 pedwarn (input_location, OPT_Wpedantic,
18904 "ISO C++ forbids the use of %qE"
18905 " on explicit instantiations", storage);
18906 }
18907
18908 if (storage == ridpointers[(int) RID_INLINE])
18909 nomem_p = 1;
18910 else if (storage == ridpointers[(int) RID_EXTERN])
18911 extern_p = 1;
18912 else if (storage == ridpointers[(int) RID_STATIC])
18913 static_p = 1;
18914 else
18915 {
18916 error ("storage class %qD applied to template instantiation",
18917 storage);
18918 extern_p = 0;
18919 }
18920 }
18921
18922 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18923 {
18924 /* DR 259 [temp.spec].
18925
18926 Both an explicit instantiation and a declaration of an explicit
18927 specialization shall not appear in a program unless the explicit
18928 instantiation follows a declaration of the explicit specialization.
18929
18930 For a given set of template parameters, if an explicit
18931 instantiation of a template appears after a declaration of an
18932 explicit specialization for that template, the explicit
18933 instantiation has no effect. */
18934 return;
18935 }
18936 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18937 {
18938 /* [temp.spec]
18939
18940 No program shall explicitly instantiate any template more
18941 than once.
18942
18943 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18944 instantiation was `extern'. If EXTERN_P then the second is.
18945 These cases are OK. */
18946 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18947
18948 if (!previous_instantiation_extern_p && !extern_p
18949 && (complain & tf_error))
18950 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18951
18952 /* If we've already instantiated the template, just return now. */
18953 if (!CLASSTYPE_INTERFACE_ONLY (t))
18954 return;
18955 }
18956
18957 check_explicit_instantiation_namespace (TYPE_NAME (t));
18958 mark_class_instantiated (t, extern_p);
18959
18960 if (nomem_p)
18961 return;
18962
18963 {
18964 tree tmp;
18965
18966 /* In contrast to implicit instantiation, where only the
18967 declarations, and not the definitions, of members are
18968 instantiated, we have here:
18969
18970 [temp.explicit]
18971
18972 The explicit instantiation of a class template specialization
18973 implies the instantiation of all of its members not
18974 previously explicitly specialized in the translation unit
18975 containing the explicit instantiation.
18976
18977 Of course, we can't instantiate member template classes, since
18978 we don't have any arguments for them. Note that the standard
18979 is unclear on whether the instantiation of the members are
18980 *explicit* instantiations or not. However, the most natural
18981 interpretation is that it should be an explicit instantiation. */
18982
18983 if (! static_p)
18984 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18985 if (TREE_CODE (tmp) == FUNCTION_DECL
18986 && DECL_TEMPLATE_INSTANTIATION (tmp))
18987 instantiate_class_member (tmp, extern_p);
18988
18989 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18990 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
18991 instantiate_class_member (tmp, extern_p);
18992
18993 if (CLASSTYPE_NESTED_UTDS (t))
18994 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18995 bt_instantiate_type_proc, &storage);
18996 }
18997 }
18998
18999 /* Given a function DECL, which is a specialization of TMPL, modify
19000 DECL to be a re-instantiation of TMPL with the same template
19001 arguments. TMPL should be the template into which tsubst'ing
19002 should occur for DECL, not the most general template.
19003
19004 One reason for doing this is a scenario like this:
19005
19006 template <class T>
19007 void f(const T&, int i);
19008
19009 void g() { f(3, 7); }
19010
19011 template <class T>
19012 void f(const T& t, const int i) { }
19013
19014 Note that when the template is first instantiated, with
19015 instantiate_template, the resulting DECL will have no name for the
19016 first parameter, and the wrong type for the second. So, when we go
19017 to instantiate the DECL, we regenerate it. */
19018
19019 static void
19020 regenerate_decl_from_template (tree decl, tree tmpl)
19021 {
19022 /* The arguments used to instantiate DECL, from the most general
19023 template. */
19024 tree args;
19025 tree code_pattern;
19026
19027 args = DECL_TI_ARGS (decl);
19028 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19029
19030 /* Make sure that we can see identifiers, and compute access
19031 correctly. */
19032 push_access_scope (decl);
19033
19034 if (TREE_CODE (decl) == FUNCTION_DECL)
19035 {
19036 tree decl_parm;
19037 tree pattern_parm;
19038 tree specs;
19039 int args_depth;
19040 int parms_depth;
19041
19042 args_depth = TMPL_ARGS_DEPTH (args);
19043 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19044 if (args_depth > parms_depth)
19045 args = get_innermost_template_args (args, parms_depth);
19046
19047 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19048 args, tf_error, NULL_TREE,
19049 /*defer_ok*/false);
19050 if (specs && specs != error_mark_node)
19051 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19052 specs);
19053
19054 /* Merge parameter declarations. */
19055 decl_parm = skip_artificial_parms_for (decl,
19056 DECL_ARGUMENTS (decl));
19057 pattern_parm
19058 = skip_artificial_parms_for (code_pattern,
19059 DECL_ARGUMENTS (code_pattern));
19060 while (decl_parm && !DECL_PACK_P (pattern_parm))
19061 {
19062 tree parm_type;
19063 tree attributes;
19064
19065 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19066 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19067 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19068 NULL_TREE);
19069 parm_type = type_decays_to (parm_type);
19070 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19071 TREE_TYPE (decl_parm) = parm_type;
19072 attributes = DECL_ATTRIBUTES (pattern_parm);
19073 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19074 {
19075 DECL_ATTRIBUTES (decl_parm) = attributes;
19076 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19077 }
19078 decl_parm = DECL_CHAIN (decl_parm);
19079 pattern_parm = DECL_CHAIN (pattern_parm);
19080 }
19081 /* Merge any parameters that match with the function parameter
19082 pack. */
19083 if (pattern_parm && DECL_PACK_P (pattern_parm))
19084 {
19085 int i, len;
19086 tree expanded_types;
19087 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19088 the parameters in this function parameter pack. */
19089 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19090 args, tf_error, NULL_TREE);
19091 len = TREE_VEC_LENGTH (expanded_types);
19092 for (i = 0; i < len; i++)
19093 {
19094 tree parm_type;
19095 tree attributes;
19096
19097 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19098 /* Rename the parameter to include the index. */
19099 DECL_NAME (decl_parm) =
19100 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19101 parm_type = TREE_VEC_ELT (expanded_types, i);
19102 parm_type = type_decays_to (parm_type);
19103 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19104 TREE_TYPE (decl_parm) = parm_type;
19105 attributes = DECL_ATTRIBUTES (pattern_parm);
19106 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19107 {
19108 DECL_ATTRIBUTES (decl_parm) = attributes;
19109 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19110 }
19111 decl_parm = DECL_CHAIN (decl_parm);
19112 }
19113 }
19114 /* Merge additional specifiers from the CODE_PATTERN. */
19115 if (DECL_DECLARED_INLINE_P (code_pattern)
19116 && !DECL_DECLARED_INLINE_P (decl))
19117 DECL_DECLARED_INLINE_P (decl) = 1;
19118 }
19119 else if (VAR_P (decl))
19120 {
19121 DECL_INITIAL (decl) =
19122 tsubst_expr (DECL_INITIAL (code_pattern), args,
19123 tf_error, DECL_TI_TEMPLATE (decl),
19124 /*integral_constant_expression_p=*/false);
19125 if (VAR_HAD_UNKNOWN_BOUND (decl))
19126 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19127 tf_error, DECL_TI_TEMPLATE (decl));
19128 }
19129 else
19130 gcc_unreachable ();
19131
19132 pop_access_scope (decl);
19133 }
19134
19135 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19136 substituted to get DECL. */
19137
19138 tree
19139 template_for_substitution (tree decl)
19140 {
19141 tree tmpl = DECL_TI_TEMPLATE (decl);
19142
19143 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19144 for the instantiation. This is not always the most general
19145 template. Consider, for example:
19146
19147 template <class T>
19148 struct S { template <class U> void f();
19149 template <> void f<int>(); };
19150
19151 and an instantiation of S<double>::f<int>. We want TD to be the
19152 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19153 while (/* An instantiation cannot have a definition, so we need a
19154 more general template. */
19155 DECL_TEMPLATE_INSTANTIATION (tmpl)
19156 /* We must also deal with friend templates. Given:
19157
19158 template <class T> struct S {
19159 template <class U> friend void f() {};
19160 };
19161
19162 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19163 so far as the language is concerned, but that's still
19164 where we get the pattern for the instantiation from. On
19165 other hand, if the definition comes outside the class, say:
19166
19167 template <class T> struct S {
19168 template <class U> friend void f();
19169 };
19170 template <class U> friend void f() {}
19171
19172 we don't need to look any further. That's what the check for
19173 DECL_INITIAL is for. */
19174 || (TREE_CODE (decl) == FUNCTION_DECL
19175 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19176 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19177 {
19178 /* The present template, TD, should not be a definition. If it
19179 were a definition, we should be using it! Note that we
19180 cannot restructure the loop to just keep going until we find
19181 a template with a definition, since that might go too far if
19182 a specialization was declared, but not defined. */
19183 gcc_assert (!VAR_P (decl)
19184 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19185
19186 /* Fetch the more general template. */
19187 tmpl = DECL_TI_TEMPLATE (tmpl);
19188 }
19189
19190 return tmpl;
19191 }
19192
19193 /* Returns true if we need to instantiate this template instance even if we
19194 know we aren't going to emit it.. */
19195
19196 bool
19197 always_instantiate_p (tree decl)
19198 {
19199 /* We always instantiate inline functions so that we can inline them. An
19200 explicit instantiation declaration prohibits implicit instantiation of
19201 non-inline functions. With high levels of optimization, we would
19202 normally inline non-inline functions -- but we're not allowed to do
19203 that for "extern template" functions. Therefore, we check
19204 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19205 return ((TREE_CODE (decl) == FUNCTION_DECL
19206 && (DECL_DECLARED_INLINE_P (decl)
19207 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19208 /* And we need to instantiate static data members so that
19209 their initializers are available in integral constant
19210 expressions. */
19211 || (VAR_P (decl)
19212 && decl_maybe_constant_var_p (decl)));
19213 }
19214
19215 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19216 instantiate it now, modifying TREE_TYPE (fn). */
19217
19218 void
19219 maybe_instantiate_noexcept (tree fn)
19220 {
19221 tree fntype, spec, noex, clone;
19222
19223 if (DECL_CLONED_FUNCTION_P (fn))
19224 fn = DECL_CLONED_FUNCTION (fn);
19225 fntype = TREE_TYPE (fn);
19226 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19227
19228 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19229 return;
19230
19231 noex = TREE_PURPOSE (spec);
19232
19233 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19234 {
19235 if (push_tinst_level (fn))
19236 {
19237 push_access_scope (fn);
19238 push_deferring_access_checks (dk_no_deferred);
19239 input_location = DECL_SOURCE_LOCATION (fn);
19240 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19241 DEFERRED_NOEXCEPT_ARGS (noex),
19242 tf_warning_or_error, fn,
19243 /*function_p=*/false,
19244 /*integral_constant_expression_p=*/true);
19245 pop_deferring_access_checks ();
19246 pop_access_scope (fn);
19247 pop_tinst_level ();
19248 spec = build_noexcept_spec (noex, tf_warning_or_error);
19249 if (spec == error_mark_node)
19250 spec = noexcept_false_spec;
19251 }
19252 else
19253 spec = noexcept_false_spec;
19254 }
19255 else
19256 {
19257 /* This is an implicitly declared function, so NOEX is a list of
19258 other functions to evaluate and merge. */
19259 tree elt;
19260 spec = noexcept_true_spec;
19261 for (elt = noex; elt; elt = OVL_NEXT (elt))
19262 {
19263 tree fn = OVL_CURRENT (elt);
19264 tree subspec;
19265 maybe_instantiate_noexcept (fn);
19266 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19267 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19268 }
19269 }
19270
19271 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19272
19273 FOR_EACH_CLONE (clone, fn)
19274 {
19275 if (TREE_TYPE (clone) == fntype)
19276 TREE_TYPE (clone) = TREE_TYPE (fn);
19277 else
19278 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19279 }
19280 }
19281
19282 /* Produce the definition of D, a _DECL generated from a template. If
19283 DEFER_OK is nonzero, then we don't have to actually do the
19284 instantiation now; we just have to do it sometime. Normally it is
19285 an error if this is an explicit instantiation but D is undefined.
19286 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19287 explicitly instantiated class template. */
19288
19289 tree
19290 instantiate_decl (tree d, int defer_ok,
19291 bool expl_inst_class_mem_p)
19292 {
19293 tree tmpl = DECL_TI_TEMPLATE (d);
19294 tree gen_args;
19295 tree args;
19296 tree td;
19297 tree code_pattern;
19298 tree spec;
19299 tree gen_tmpl;
19300 bool pattern_defined;
19301 location_t saved_loc = input_location;
19302 int saved_unevaluated_operand = cp_unevaluated_operand;
19303 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19304 bool external_p;
19305 tree fn_context;
19306 bool nested;
19307
19308 /* This function should only be used to instantiate templates for
19309 functions and static member variables. */
19310 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19311
19312 /* Variables are never deferred; if instantiation is required, they
19313 are instantiated right away. That allows for better code in the
19314 case that an expression refers to the value of the variable --
19315 if the variable has a constant value the referring expression can
19316 take advantage of that fact. */
19317 if (VAR_P (d)
19318 || DECL_DECLARED_CONSTEXPR_P (d))
19319 defer_ok = 0;
19320
19321 /* Don't instantiate cloned functions. Instead, instantiate the
19322 functions they cloned. */
19323 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19324 d = DECL_CLONED_FUNCTION (d);
19325
19326 if (DECL_TEMPLATE_INSTANTIATED (d)
19327 || (TREE_CODE (d) == FUNCTION_DECL
19328 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19329 || DECL_TEMPLATE_SPECIALIZATION (d))
19330 /* D has already been instantiated or explicitly specialized, so
19331 there's nothing for us to do here.
19332
19333 It might seem reasonable to check whether or not D is an explicit
19334 instantiation, and, if so, stop here. But when an explicit
19335 instantiation is deferred until the end of the compilation,
19336 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19337 the instantiation. */
19338 return d;
19339
19340 /* Check to see whether we know that this template will be
19341 instantiated in some other file, as with "extern template"
19342 extension. */
19343 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19344
19345 /* In general, we do not instantiate such templates. */
19346 if (external_p && !always_instantiate_p (d))
19347 return d;
19348
19349 gen_tmpl = most_general_template (tmpl);
19350 gen_args = DECL_TI_ARGS (d);
19351
19352 if (tmpl != gen_tmpl)
19353 /* We should already have the extra args. */
19354 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19355 == TMPL_ARGS_DEPTH (gen_args));
19356 /* And what's in the hash table should match D. */
19357 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19358 || spec == NULL_TREE);
19359
19360 /* This needs to happen before any tsubsting. */
19361 if (! push_tinst_level (d))
19362 return d;
19363
19364 timevar_push (TV_TEMPLATE_INST);
19365
19366 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19367 for the instantiation. */
19368 td = template_for_substitution (d);
19369 code_pattern = DECL_TEMPLATE_RESULT (td);
19370
19371 /* We should never be trying to instantiate a member of a class
19372 template or partial specialization. */
19373 gcc_assert (d != code_pattern);
19374
19375 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19376 || DECL_TEMPLATE_SPECIALIZATION (td))
19377 /* In the case of a friend template whose definition is provided
19378 outside the class, we may have too many arguments. Drop the
19379 ones we don't need. The same is true for specializations. */
19380 args = get_innermost_template_args
19381 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19382 else
19383 args = gen_args;
19384
19385 if (TREE_CODE (d) == FUNCTION_DECL)
19386 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19387 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19388 else
19389 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19390
19391 /* We may be in the middle of deferred access check. Disable it now. */
19392 push_deferring_access_checks (dk_no_deferred);
19393
19394 /* Unless an explicit instantiation directive has already determined
19395 the linkage of D, remember that a definition is available for
19396 this entity. */
19397 if (pattern_defined
19398 && !DECL_INTERFACE_KNOWN (d)
19399 && !DECL_NOT_REALLY_EXTERN (d))
19400 mark_definable (d);
19401
19402 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19403 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19404 input_location = DECL_SOURCE_LOCATION (d);
19405
19406 /* If D is a member of an explicitly instantiated class template,
19407 and no definition is available, treat it like an implicit
19408 instantiation. */
19409 if (!pattern_defined && expl_inst_class_mem_p
19410 && DECL_EXPLICIT_INSTANTIATION (d))
19411 {
19412 /* Leave linkage flags alone on instantiations with anonymous
19413 visibility. */
19414 if (TREE_PUBLIC (d))
19415 {
19416 DECL_NOT_REALLY_EXTERN (d) = 0;
19417 DECL_INTERFACE_KNOWN (d) = 0;
19418 }
19419 SET_DECL_IMPLICIT_INSTANTIATION (d);
19420 }
19421
19422 if (TREE_CODE (d) == FUNCTION_DECL)
19423 maybe_instantiate_noexcept (d);
19424
19425 /* Defer all other templates, unless we have been explicitly
19426 forbidden from doing so. */
19427 if (/* If there is no definition, we cannot instantiate the
19428 template. */
19429 ! pattern_defined
19430 /* If it's OK to postpone instantiation, do so. */
19431 || defer_ok
19432 /* If this is a static data member that will be defined
19433 elsewhere, we don't want to instantiate the entire data
19434 member, but we do want to instantiate the initializer so that
19435 we can substitute that elsewhere. */
19436 || (external_p && VAR_P (d)))
19437 {
19438 /* The definition of the static data member is now required so
19439 we must substitute the initializer. */
19440 if (VAR_P (d)
19441 && !DECL_INITIAL (d)
19442 && DECL_INITIAL (code_pattern))
19443 {
19444 tree ns;
19445 tree init;
19446 bool const_init = false;
19447
19448 ns = decl_namespace_context (d);
19449 push_nested_namespace (ns);
19450 push_nested_class (DECL_CONTEXT (d));
19451 init = tsubst_expr (DECL_INITIAL (code_pattern),
19452 args,
19453 tf_warning_or_error, NULL_TREE,
19454 /*integral_constant_expression_p=*/false);
19455 /* Make sure the initializer is still constant, in case of
19456 circular dependency (template/instantiate6.C). */
19457 const_init
19458 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19459 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19460 /*asmspec_tree=*/NULL_TREE,
19461 LOOKUP_ONLYCONVERTING);
19462 pop_nested_class ();
19463 pop_nested_namespace (ns);
19464 }
19465
19466 /* We restore the source position here because it's used by
19467 add_pending_template. */
19468 input_location = saved_loc;
19469
19470 if (at_eof && !pattern_defined
19471 && DECL_EXPLICIT_INSTANTIATION (d)
19472 && DECL_NOT_REALLY_EXTERN (d))
19473 /* [temp.explicit]
19474
19475 The definition of a non-exported function template, a
19476 non-exported member function template, or a non-exported
19477 member function or static data member of a class template
19478 shall be present in every translation unit in which it is
19479 explicitly instantiated. */
19480 permerror (input_location, "explicit instantiation of %qD "
19481 "but no definition available", d);
19482
19483 /* If we're in unevaluated context, we just wanted to get the
19484 constant value; this isn't an odr use, so don't queue
19485 a full instantiation. */
19486 if (cp_unevaluated_operand != 0)
19487 goto out;
19488 /* ??? Historically, we have instantiated inline functions, even
19489 when marked as "extern template". */
19490 if (!(external_p && VAR_P (d)))
19491 add_pending_template (d);
19492 goto out;
19493 }
19494 /* Tell the repository that D is available in this translation unit
19495 -- and see if it is supposed to be instantiated here. */
19496 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19497 {
19498 /* In a PCH file, despite the fact that the repository hasn't
19499 requested instantiation in the PCH it is still possible that
19500 an instantiation will be required in a file that includes the
19501 PCH. */
19502 if (pch_file)
19503 add_pending_template (d);
19504 /* Instantiate inline functions so that the inliner can do its
19505 job, even though we'll not be emitting a copy of this
19506 function. */
19507 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19508 goto out;
19509 }
19510
19511 fn_context = decl_function_context (d);
19512 nested = (current_function_decl != NULL_TREE);
19513 if (!fn_context)
19514 push_to_top_level ();
19515 else
19516 {
19517 if (nested)
19518 push_function_context ();
19519 cp_unevaluated_operand = 0;
19520 c_inhibit_evaluation_warnings = 0;
19521 }
19522
19523 /* Mark D as instantiated so that recursive calls to
19524 instantiate_decl do not try to instantiate it again. */
19525 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19526
19527 /* Regenerate the declaration in case the template has been modified
19528 by a subsequent redeclaration. */
19529 regenerate_decl_from_template (d, td);
19530
19531 /* We already set the file and line above. Reset them now in case
19532 they changed as a result of calling regenerate_decl_from_template. */
19533 input_location = DECL_SOURCE_LOCATION (d);
19534
19535 if (VAR_P (d))
19536 {
19537 tree init;
19538 bool const_init = false;
19539
19540 /* Clear out DECL_RTL; whatever was there before may not be right
19541 since we've reset the type of the declaration. */
19542 SET_DECL_RTL (d, NULL);
19543 DECL_IN_AGGR_P (d) = 0;
19544
19545 /* The initializer is placed in DECL_INITIAL by
19546 regenerate_decl_from_template so we don't need to
19547 push/pop_access_scope again here. Pull it out so that
19548 cp_finish_decl can process it. */
19549 init = DECL_INITIAL (d);
19550 DECL_INITIAL (d) = NULL_TREE;
19551 DECL_INITIALIZED_P (d) = 0;
19552
19553 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19554 initializer. That function will defer actual emission until
19555 we have a chance to determine linkage. */
19556 DECL_EXTERNAL (d) = 0;
19557
19558 /* Enter the scope of D so that access-checking works correctly. */
19559 push_nested_class (DECL_CONTEXT (d));
19560 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19561 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19562 pop_nested_class ();
19563 }
19564 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19565 synthesize_method (d);
19566 else if (TREE_CODE (d) == FUNCTION_DECL)
19567 {
19568 struct pointer_map_t *saved_local_specializations;
19569 tree subst_decl;
19570 tree tmpl_parm;
19571 tree spec_parm;
19572 tree block = NULL_TREE;
19573
19574 /* Save away the current list, in case we are instantiating one
19575 template from within the body of another. */
19576 saved_local_specializations = local_specializations;
19577
19578 /* Set up the list of local specializations. */
19579 local_specializations = pointer_map_create ();
19580
19581 /* Set up context. */
19582 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19583 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19584 block = push_stmt_list ();
19585 else
19586 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19587
19588 /* Some typedefs referenced from within the template code need to be
19589 access checked at template instantiation time, i.e now. These
19590 types were added to the template at parsing time. Let's get those
19591 and perform the access checks then. */
19592 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19593 gen_args);
19594
19595 /* Create substitution entries for the parameters. */
19596 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19597 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19598 spec_parm = DECL_ARGUMENTS (d);
19599 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19600 {
19601 register_local_specialization (spec_parm, tmpl_parm);
19602 spec_parm = skip_artificial_parms_for (d, spec_parm);
19603 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19604 }
19605 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19606 {
19607 if (!DECL_PACK_P (tmpl_parm))
19608 {
19609 register_local_specialization (spec_parm, tmpl_parm);
19610 spec_parm = DECL_CHAIN (spec_parm);
19611 }
19612 else
19613 {
19614 /* Register the (value) argument pack as a specialization of
19615 TMPL_PARM, then move on. */
19616 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19617 register_local_specialization (argpack, tmpl_parm);
19618 }
19619 }
19620 gcc_assert (!spec_parm);
19621
19622 /* Substitute into the body of the function. */
19623 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19624 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19625 tf_warning_or_error, tmpl);
19626 else
19627 {
19628 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19629 tf_warning_or_error, tmpl,
19630 /*integral_constant_expression_p=*/false);
19631
19632 /* Set the current input_location to the end of the function
19633 so that finish_function knows where we are. */
19634 input_location
19635 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19636 }
19637
19638 /* We don't need the local specializations any more. */
19639 pointer_map_destroy (local_specializations);
19640 local_specializations = saved_local_specializations;
19641
19642 /* Finish the function. */
19643 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19644 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19645 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19646 else
19647 {
19648 d = finish_function (0);
19649 expand_or_defer_fn (d);
19650 }
19651
19652 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19653 cp_check_omp_declare_reduction (d);
19654 }
19655
19656 /* We're not deferring instantiation any more. */
19657 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19658
19659 if (!fn_context)
19660 pop_from_top_level ();
19661 else if (nested)
19662 pop_function_context ();
19663
19664 out:
19665 input_location = saved_loc;
19666 cp_unevaluated_operand = saved_unevaluated_operand;
19667 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19668 pop_deferring_access_checks ();
19669 pop_tinst_level ();
19670
19671 timevar_pop (TV_TEMPLATE_INST);
19672
19673 return d;
19674 }
19675
19676 /* Run through the list of templates that we wish we could
19677 instantiate, and instantiate any we can. RETRIES is the
19678 number of times we retry pending template instantiation. */
19679
19680 void
19681 instantiate_pending_templates (int retries)
19682 {
19683 int reconsider;
19684 location_t saved_loc = input_location;
19685
19686 /* Instantiating templates may trigger vtable generation. This in turn
19687 may require further template instantiations. We place a limit here
19688 to avoid infinite loop. */
19689 if (pending_templates && retries >= max_tinst_depth)
19690 {
19691 tree decl = pending_templates->tinst->decl;
19692
19693 error ("template instantiation depth exceeds maximum of %d"
19694 " instantiating %q+D, possibly from virtual table generation"
19695 " (use -ftemplate-depth= to increase the maximum)",
19696 max_tinst_depth, decl);
19697 if (TREE_CODE (decl) == FUNCTION_DECL)
19698 /* Pretend that we defined it. */
19699 DECL_INITIAL (decl) = error_mark_node;
19700 return;
19701 }
19702
19703 do
19704 {
19705 struct pending_template **t = &pending_templates;
19706 struct pending_template *last = NULL;
19707 reconsider = 0;
19708 while (*t)
19709 {
19710 tree instantiation = reopen_tinst_level ((*t)->tinst);
19711 bool complete = false;
19712
19713 if (TYPE_P (instantiation))
19714 {
19715 tree fn;
19716
19717 if (!COMPLETE_TYPE_P (instantiation))
19718 {
19719 instantiate_class_template (instantiation);
19720 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19721 for (fn = TYPE_METHODS (instantiation);
19722 fn;
19723 fn = TREE_CHAIN (fn))
19724 if (! DECL_ARTIFICIAL (fn))
19725 instantiate_decl (fn,
19726 /*defer_ok=*/0,
19727 /*expl_inst_class_mem_p=*/false);
19728 if (COMPLETE_TYPE_P (instantiation))
19729 reconsider = 1;
19730 }
19731
19732 complete = COMPLETE_TYPE_P (instantiation);
19733 }
19734 else
19735 {
19736 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19737 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19738 {
19739 instantiation
19740 = instantiate_decl (instantiation,
19741 /*defer_ok=*/0,
19742 /*expl_inst_class_mem_p=*/false);
19743 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19744 reconsider = 1;
19745 }
19746
19747 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19748 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19749 }
19750
19751 if (complete)
19752 /* If INSTANTIATION has been instantiated, then we don't
19753 need to consider it again in the future. */
19754 *t = (*t)->next;
19755 else
19756 {
19757 last = *t;
19758 t = &(*t)->next;
19759 }
19760 tinst_depth = 0;
19761 current_tinst_level = NULL;
19762 }
19763 last_pending_template = last;
19764 }
19765 while (reconsider);
19766
19767 input_location = saved_loc;
19768 }
19769
19770 /* Substitute ARGVEC into T, which is a list of initializers for
19771 either base class or a non-static data member. The TREE_PURPOSEs
19772 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19773 instantiate_decl. */
19774
19775 static tree
19776 tsubst_initializer_list (tree t, tree argvec)
19777 {
19778 tree inits = NULL_TREE;
19779
19780 for (; t; t = TREE_CHAIN (t))
19781 {
19782 tree decl;
19783 tree init;
19784 tree expanded_bases = NULL_TREE;
19785 tree expanded_arguments = NULL_TREE;
19786 int i, len = 1;
19787
19788 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19789 {
19790 tree expr;
19791 tree arg;
19792
19793 /* Expand the base class expansion type into separate base
19794 classes. */
19795 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19796 tf_warning_or_error,
19797 NULL_TREE);
19798 if (expanded_bases == error_mark_node)
19799 continue;
19800
19801 /* We'll be building separate TREE_LISTs of arguments for
19802 each base. */
19803 len = TREE_VEC_LENGTH (expanded_bases);
19804 expanded_arguments = make_tree_vec (len);
19805 for (i = 0; i < len; i++)
19806 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19807
19808 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19809 expand each argument in the TREE_VALUE of t. */
19810 expr = make_node (EXPR_PACK_EXPANSION);
19811 PACK_EXPANSION_LOCAL_P (expr) = true;
19812 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19813 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19814
19815 if (TREE_VALUE (t) == void_type_node)
19816 /* VOID_TYPE_NODE is used to indicate
19817 value-initialization. */
19818 {
19819 for (i = 0; i < len; i++)
19820 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19821 }
19822 else
19823 {
19824 /* Substitute parameter packs into each argument in the
19825 TREE_LIST. */
19826 in_base_initializer = 1;
19827 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19828 {
19829 tree expanded_exprs;
19830
19831 /* Expand the argument. */
19832 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19833 expanded_exprs
19834 = tsubst_pack_expansion (expr, argvec,
19835 tf_warning_or_error,
19836 NULL_TREE);
19837 if (expanded_exprs == error_mark_node)
19838 continue;
19839
19840 /* Prepend each of the expanded expressions to the
19841 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19842 for (i = 0; i < len; i++)
19843 {
19844 TREE_VEC_ELT (expanded_arguments, i) =
19845 tree_cons (NULL_TREE,
19846 TREE_VEC_ELT (expanded_exprs, i),
19847 TREE_VEC_ELT (expanded_arguments, i));
19848 }
19849 }
19850 in_base_initializer = 0;
19851
19852 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19853 since we built them backwards. */
19854 for (i = 0; i < len; i++)
19855 {
19856 TREE_VEC_ELT (expanded_arguments, i) =
19857 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19858 }
19859 }
19860 }
19861
19862 for (i = 0; i < len; ++i)
19863 {
19864 if (expanded_bases)
19865 {
19866 decl = TREE_VEC_ELT (expanded_bases, i);
19867 decl = expand_member_init (decl);
19868 init = TREE_VEC_ELT (expanded_arguments, i);
19869 }
19870 else
19871 {
19872 tree tmp;
19873 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19874 tf_warning_or_error, NULL_TREE);
19875
19876 decl = expand_member_init (decl);
19877 if (decl && !DECL_P (decl))
19878 in_base_initializer = 1;
19879
19880 init = TREE_VALUE (t);
19881 tmp = init;
19882 if (init != void_type_node)
19883 init = tsubst_expr (init, argvec,
19884 tf_warning_or_error, NULL_TREE,
19885 /*integral_constant_expression_p=*/false);
19886 if (init == NULL_TREE && tmp != NULL_TREE)
19887 /* If we had an initializer but it instantiated to nothing,
19888 value-initialize the object. This will only occur when
19889 the initializer was a pack expansion where the parameter
19890 packs used in that expansion were of length zero. */
19891 init = void_type_node;
19892 in_base_initializer = 0;
19893 }
19894
19895 if (decl)
19896 {
19897 init = build_tree_list (decl, init);
19898 TREE_CHAIN (init) = inits;
19899 inits = init;
19900 }
19901 }
19902 }
19903 return inits;
19904 }
19905
19906 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19907
19908 static void
19909 set_current_access_from_decl (tree decl)
19910 {
19911 if (TREE_PRIVATE (decl))
19912 current_access_specifier = access_private_node;
19913 else if (TREE_PROTECTED (decl))
19914 current_access_specifier = access_protected_node;
19915 else
19916 current_access_specifier = access_public_node;
19917 }
19918
19919 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19920 is the instantiation (which should have been created with
19921 start_enum) and ARGS are the template arguments to use. */
19922
19923 static void
19924 tsubst_enum (tree tag, tree newtag, tree args)
19925 {
19926 tree e;
19927
19928 if (SCOPED_ENUM_P (newtag))
19929 begin_scope (sk_scoped_enum, newtag);
19930
19931 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19932 {
19933 tree value;
19934 tree decl;
19935
19936 decl = TREE_VALUE (e);
19937 /* Note that in a template enum, the TREE_VALUE is the
19938 CONST_DECL, not the corresponding INTEGER_CST. */
19939 value = tsubst_expr (DECL_INITIAL (decl),
19940 args, tf_warning_or_error, NULL_TREE,
19941 /*integral_constant_expression_p=*/true);
19942
19943 /* Give this enumeration constant the correct access. */
19944 set_current_access_from_decl (decl);
19945
19946 /* Actually build the enumerator itself. */
19947 build_enumerator
19948 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19949 }
19950
19951 if (SCOPED_ENUM_P (newtag))
19952 finish_scope ();
19953
19954 finish_enum_value_list (newtag);
19955 finish_enum (newtag);
19956
19957 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19958 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19959 }
19960
19961 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19962 its type -- but without substituting the innermost set of template
19963 arguments. So, innermost set of template parameters will appear in
19964 the type. */
19965
19966 tree
19967 get_mostly_instantiated_function_type (tree decl)
19968 {
19969 tree fn_type;
19970 tree tmpl;
19971 tree targs;
19972 tree tparms;
19973 int parm_depth;
19974
19975 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19976 targs = DECL_TI_ARGS (decl);
19977 tparms = DECL_TEMPLATE_PARMS (tmpl);
19978 parm_depth = TMPL_PARMS_DEPTH (tparms);
19979
19980 /* There should be as many levels of arguments as there are levels
19981 of parameters. */
19982 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19983
19984 fn_type = TREE_TYPE (tmpl);
19985
19986 if (parm_depth == 1)
19987 /* No substitution is necessary. */
19988 ;
19989 else
19990 {
19991 int i;
19992 tree partial_args;
19993
19994 /* Replace the innermost level of the TARGS with NULL_TREEs to
19995 let tsubst know not to substitute for those parameters. */
19996 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19997 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19998 SET_TMPL_ARGS_LEVEL (partial_args, i,
19999 TMPL_ARGS_LEVEL (targs, i));
20000 SET_TMPL_ARGS_LEVEL (partial_args,
20001 TMPL_ARGS_DEPTH (targs),
20002 make_tree_vec (DECL_NTPARMS (tmpl)));
20003
20004 /* Make sure that we can see identifiers, and compute access
20005 correctly. */
20006 push_access_scope (decl);
20007
20008 ++processing_template_decl;
20009 /* Now, do the (partial) substitution to figure out the
20010 appropriate function type. */
20011 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20012 --processing_template_decl;
20013
20014 /* Substitute into the template parameters to obtain the real
20015 innermost set of parameters. This step is important if the
20016 innermost set of template parameters contains value
20017 parameters whose types depend on outer template parameters. */
20018 TREE_VEC_LENGTH (partial_args)--;
20019 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20020
20021 pop_access_scope (decl);
20022 }
20023
20024 return fn_type;
20025 }
20026
20027 /* Return truthvalue if we're processing a template different from
20028 the last one involved in diagnostics. */
20029 int
20030 problematic_instantiation_changed (void)
20031 {
20032 return current_tinst_level != last_error_tinst_level;
20033 }
20034
20035 /* Remember current template involved in diagnostics. */
20036 void
20037 record_last_problematic_instantiation (void)
20038 {
20039 last_error_tinst_level = current_tinst_level;
20040 }
20041
20042 struct tinst_level *
20043 current_instantiation (void)
20044 {
20045 return current_tinst_level;
20046 }
20047
20048 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20049 type. Return zero for ok, nonzero for disallowed. Issue error and
20050 warning messages under control of COMPLAIN. */
20051
20052 static int
20053 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20054 {
20055 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20056 return 0;
20057 else if (POINTER_TYPE_P (type))
20058 return 0;
20059 else if (TYPE_PTRMEM_P (type))
20060 return 0;
20061 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20062 return 0;
20063 else if (TREE_CODE (type) == TYPENAME_TYPE)
20064 return 0;
20065 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20066 return 0;
20067 else if (TREE_CODE (type) == NULLPTR_TYPE)
20068 return 0;
20069
20070 if (complain & tf_error)
20071 {
20072 if (type == error_mark_node)
20073 inform (input_location, "invalid template non-type parameter");
20074 else
20075 error ("%q#T is not a valid type for a template non-type parameter",
20076 type);
20077 }
20078 return 1;
20079 }
20080
20081 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20082 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20083
20084 static bool
20085 dependent_type_p_r (tree type)
20086 {
20087 tree scope;
20088
20089 /* [temp.dep.type]
20090
20091 A type is dependent if it is:
20092
20093 -- a template parameter. Template template parameters are types
20094 for us (since TYPE_P holds true for them) so we handle
20095 them here. */
20096 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20097 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20098 return true;
20099 /* -- a qualified-id with a nested-name-specifier which contains a
20100 class-name that names a dependent type or whose unqualified-id
20101 names a dependent type. */
20102 if (TREE_CODE (type) == TYPENAME_TYPE)
20103 return true;
20104 /* -- a cv-qualified type where the cv-unqualified type is
20105 dependent. */
20106 type = TYPE_MAIN_VARIANT (type);
20107 /* -- a compound type constructed from any dependent type. */
20108 if (TYPE_PTRMEM_P (type))
20109 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20110 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20111 (type)));
20112 else if (TYPE_PTR_P (type)
20113 || TREE_CODE (type) == REFERENCE_TYPE)
20114 return dependent_type_p (TREE_TYPE (type));
20115 else if (TREE_CODE (type) == FUNCTION_TYPE
20116 || TREE_CODE (type) == METHOD_TYPE)
20117 {
20118 tree arg_type;
20119
20120 if (dependent_type_p (TREE_TYPE (type)))
20121 return true;
20122 for (arg_type = TYPE_ARG_TYPES (type);
20123 arg_type;
20124 arg_type = TREE_CHAIN (arg_type))
20125 if (dependent_type_p (TREE_VALUE (arg_type)))
20126 return true;
20127 return false;
20128 }
20129 /* -- an array type constructed from any dependent type or whose
20130 size is specified by a constant expression that is
20131 value-dependent.
20132
20133 We checked for type- and value-dependence of the bounds in
20134 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20135 if (TREE_CODE (type) == ARRAY_TYPE)
20136 {
20137 if (TYPE_DOMAIN (type)
20138 && dependent_type_p (TYPE_DOMAIN (type)))
20139 return true;
20140 return dependent_type_p (TREE_TYPE (type));
20141 }
20142
20143 /* -- a template-id in which either the template name is a template
20144 parameter ... */
20145 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20146 return true;
20147 /* ... or any of the template arguments is a dependent type or
20148 an expression that is type-dependent or value-dependent. */
20149 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20150 && (any_dependent_template_arguments_p
20151 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20152 return true;
20153
20154 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20155 dependent; if the argument of the `typeof' expression is not
20156 type-dependent, then it should already been have resolved. */
20157 if (TREE_CODE (type) == TYPEOF_TYPE
20158 || TREE_CODE (type) == DECLTYPE_TYPE
20159 || TREE_CODE (type) == UNDERLYING_TYPE)
20160 return true;
20161
20162 /* A template argument pack is dependent if any of its packed
20163 arguments are. */
20164 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20165 {
20166 tree args = ARGUMENT_PACK_ARGS (type);
20167 int i, len = TREE_VEC_LENGTH (args);
20168 for (i = 0; i < len; ++i)
20169 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20170 return true;
20171 }
20172
20173 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20174 be template parameters. */
20175 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20176 return true;
20177
20178 /* The standard does not specifically mention types that are local
20179 to template functions or local classes, but they should be
20180 considered dependent too. For example:
20181
20182 template <int I> void f() {
20183 enum E { a = I };
20184 S<sizeof (E)> s;
20185 }
20186
20187 The size of `E' cannot be known until the value of `I' has been
20188 determined. Therefore, `E' must be considered dependent. */
20189 scope = TYPE_CONTEXT (type);
20190 if (scope && TYPE_P (scope))
20191 return dependent_type_p (scope);
20192 /* Don't use type_dependent_expression_p here, as it can lead
20193 to infinite recursion trying to determine whether a lambda
20194 nested in a lambda is dependent (c++/47687). */
20195 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20196 && DECL_LANG_SPECIFIC (scope)
20197 && DECL_TEMPLATE_INFO (scope)
20198 && (any_dependent_template_arguments_p
20199 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20200 return true;
20201
20202 /* Other types are non-dependent. */
20203 return false;
20204 }
20205
20206 /* Returns TRUE if TYPE is dependent, in the sense of
20207 [temp.dep.type]. Note that a NULL type is considered dependent. */
20208
20209 bool
20210 dependent_type_p (tree type)
20211 {
20212 /* If there are no template parameters in scope, then there can't be
20213 any dependent types. */
20214 if (!processing_template_decl)
20215 {
20216 /* If we are not processing a template, then nobody should be
20217 providing us with a dependent type. */
20218 gcc_assert (type);
20219 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20220 return false;
20221 }
20222
20223 /* If the type is NULL, we have not computed a type for the entity
20224 in question; in that case, the type is dependent. */
20225 if (!type)
20226 return true;
20227
20228 /* Erroneous types can be considered non-dependent. */
20229 if (type == error_mark_node)
20230 return false;
20231
20232 /* If we have not already computed the appropriate value for TYPE,
20233 do so now. */
20234 if (!TYPE_DEPENDENT_P_VALID (type))
20235 {
20236 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20237 TYPE_DEPENDENT_P_VALID (type) = 1;
20238 }
20239
20240 return TYPE_DEPENDENT_P (type);
20241 }
20242
20243 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20244 lookup. In other words, a dependent type that is not the current
20245 instantiation. */
20246
20247 bool
20248 dependent_scope_p (tree scope)
20249 {
20250 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20251 && !currently_open_class (scope));
20252 }
20253
20254 /* T is a SCOPE_REF; return whether we need to consider it
20255 instantiation-dependent so that we can check access at instantiation
20256 time even though we know which member it resolves to. */
20257
20258 static bool
20259 instantiation_dependent_scope_ref_p (tree t)
20260 {
20261 if (DECL_P (TREE_OPERAND (t, 1))
20262 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20263 && accessible_in_template_p (TREE_OPERAND (t, 0),
20264 TREE_OPERAND (t, 1)))
20265 return false;
20266 else
20267 return true;
20268 }
20269
20270 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20271 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20272 expression. */
20273
20274 /* Note that this predicate is not appropriate for general expressions;
20275 only constant expressions (that satisfy potential_constant_expression)
20276 can be tested for value dependence. */
20277
20278 bool
20279 value_dependent_expression_p (tree expression)
20280 {
20281 if (!processing_template_decl)
20282 return false;
20283
20284 /* A name declared with a dependent type. */
20285 if (DECL_P (expression) && type_dependent_expression_p (expression))
20286 return true;
20287
20288 switch (TREE_CODE (expression))
20289 {
20290 case IDENTIFIER_NODE:
20291 /* A name that has not been looked up -- must be dependent. */
20292 return true;
20293
20294 case TEMPLATE_PARM_INDEX:
20295 /* A non-type template parm. */
20296 return true;
20297
20298 case CONST_DECL:
20299 /* A non-type template parm. */
20300 if (DECL_TEMPLATE_PARM_P (expression))
20301 return true;
20302 return value_dependent_expression_p (DECL_INITIAL (expression));
20303
20304 case VAR_DECL:
20305 /* A constant with literal type and is initialized
20306 with an expression that is value-dependent.
20307
20308 Note that a non-dependent parenthesized initializer will have
20309 already been replaced with its constant value, so if we see
20310 a TREE_LIST it must be dependent. */
20311 if (DECL_INITIAL (expression)
20312 && decl_constant_var_p (expression)
20313 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20314 || value_dependent_expression_p (DECL_INITIAL (expression))))
20315 return true;
20316 return false;
20317
20318 case DYNAMIC_CAST_EXPR:
20319 case STATIC_CAST_EXPR:
20320 case CONST_CAST_EXPR:
20321 case REINTERPRET_CAST_EXPR:
20322 case CAST_EXPR:
20323 /* These expressions are value-dependent if the type to which
20324 the cast occurs is dependent or the expression being casted
20325 is value-dependent. */
20326 {
20327 tree type = TREE_TYPE (expression);
20328
20329 if (dependent_type_p (type))
20330 return true;
20331
20332 /* A functional cast has a list of operands. */
20333 expression = TREE_OPERAND (expression, 0);
20334 if (!expression)
20335 {
20336 /* If there are no operands, it must be an expression such
20337 as "int()". This should not happen for aggregate types
20338 because it would form non-constant expressions. */
20339 gcc_assert (cxx_dialect >= cxx11
20340 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20341
20342 return false;
20343 }
20344
20345 if (TREE_CODE (expression) == TREE_LIST)
20346 return any_value_dependent_elements_p (expression);
20347
20348 return value_dependent_expression_p (expression);
20349 }
20350
20351 case SIZEOF_EXPR:
20352 if (SIZEOF_EXPR_TYPE_P (expression))
20353 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20354 /* FALLTHRU */
20355 case ALIGNOF_EXPR:
20356 case TYPEID_EXPR:
20357 /* A `sizeof' expression is value-dependent if the operand is
20358 type-dependent or is a pack expansion. */
20359 expression = TREE_OPERAND (expression, 0);
20360 if (PACK_EXPANSION_P (expression))
20361 return true;
20362 else if (TYPE_P (expression))
20363 return dependent_type_p (expression);
20364 return instantiation_dependent_expression_p (expression);
20365
20366 case AT_ENCODE_EXPR:
20367 /* An 'encode' expression is value-dependent if the operand is
20368 type-dependent. */
20369 expression = TREE_OPERAND (expression, 0);
20370 return dependent_type_p (expression);
20371
20372 case NOEXCEPT_EXPR:
20373 expression = TREE_OPERAND (expression, 0);
20374 return instantiation_dependent_expression_p (expression);
20375
20376 case SCOPE_REF:
20377 /* All instantiation-dependent expressions should also be considered
20378 value-dependent. */
20379 return instantiation_dependent_scope_ref_p (expression);
20380
20381 case COMPONENT_REF:
20382 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20383 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20384
20385 case NONTYPE_ARGUMENT_PACK:
20386 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20387 is value-dependent. */
20388 {
20389 tree values = ARGUMENT_PACK_ARGS (expression);
20390 int i, len = TREE_VEC_LENGTH (values);
20391
20392 for (i = 0; i < len; ++i)
20393 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20394 return true;
20395
20396 return false;
20397 }
20398
20399 case TRAIT_EXPR:
20400 {
20401 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20402 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20403 || (type2 ? dependent_type_p (type2) : false));
20404 }
20405
20406 case MODOP_EXPR:
20407 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20408 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20409
20410 case ARRAY_REF:
20411 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20412 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20413
20414 case ADDR_EXPR:
20415 {
20416 tree op = TREE_OPERAND (expression, 0);
20417 return (value_dependent_expression_p (op)
20418 || has_value_dependent_address (op));
20419 }
20420
20421 case CALL_EXPR:
20422 {
20423 tree fn = get_callee_fndecl (expression);
20424 int i, nargs;
20425 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20426 return true;
20427 nargs = call_expr_nargs (expression);
20428 for (i = 0; i < nargs; ++i)
20429 {
20430 tree op = CALL_EXPR_ARG (expression, i);
20431 /* In a call to a constexpr member function, look through the
20432 implicit ADDR_EXPR on the object argument so that it doesn't
20433 cause the call to be considered value-dependent. We also
20434 look through it in potential_constant_expression. */
20435 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20436 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20437 && TREE_CODE (op) == ADDR_EXPR)
20438 op = TREE_OPERAND (op, 0);
20439 if (value_dependent_expression_p (op))
20440 return true;
20441 }
20442 return false;
20443 }
20444
20445 case TEMPLATE_ID_EXPR:
20446 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20447 type-dependent. */
20448 return type_dependent_expression_p (expression);
20449
20450 case CONSTRUCTOR:
20451 {
20452 unsigned ix;
20453 tree val;
20454 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20455 if (value_dependent_expression_p (val))
20456 return true;
20457 return false;
20458 }
20459
20460 case STMT_EXPR:
20461 /* Treat a GNU statement expression as dependent to avoid crashing
20462 under fold_non_dependent_expr; it can't be constant. */
20463 return true;
20464
20465 default:
20466 /* A constant expression is value-dependent if any subexpression is
20467 value-dependent. */
20468 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20469 {
20470 case tcc_reference:
20471 case tcc_unary:
20472 case tcc_comparison:
20473 case tcc_binary:
20474 case tcc_expression:
20475 case tcc_vl_exp:
20476 {
20477 int i, len = cp_tree_operand_length (expression);
20478
20479 for (i = 0; i < len; i++)
20480 {
20481 tree t = TREE_OPERAND (expression, i);
20482
20483 /* In some cases, some of the operands may be missing.l
20484 (For example, in the case of PREDECREMENT_EXPR, the
20485 amount to increment by may be missing.) That doesn't
20486 make the expression dependent. */
20487 if (t && value_dependent_expression_p (t))
20488 return true;
20489 }
20490 }
20491 break;
20492 default:
20493 break;
20494 }
20495 break;
20496 }
20497
20498 /* The expression is not value-dependent. */
20499 return false;
20500 }
20501
20502 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20503 [temp.dep.expr]. Note that an expression with no type is
20504 considered dependent. Other parts of the compiler arrange for an
20505 expression with type-dependent subexpressions to have no type, so
20506 this function doesn't have to be fully recursive. */
20507
20508 bool
20509 type_dependent_expression_p (tree expression)
20510 {
20511 if (!processing_template_decl)
20512 return false;
20513
20514 if (expression == error_mark_node)
20515 return false;
20516
20517 /* An unresolved name is always dependent. */
20518 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20519 return true;
20520
20521 /* Some expression forms are never type-dependent. */
20522 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20523 || TREE_CODE (expression) == SIZEOF_EXPR
20524 || TREE_CODE (expression) == ALIGNOF_EXPR
20525 || TREE_CODE (expression) == AT_ENCODE_EXPR
20526 || TREE_CODE (expression) == NOEXCEPT_EXPR
20527 || TREE_CODE (expression) == TRAIT_EXPR
20528 || TREE_CODE (expression) == TYPEID_EXPR
20529 || TREE_CODE (expression) == DELETE_EXPR
20530 || TREE_CODE (expression) == VEC_DELETE_EXPR
20531 || TREE_CODE (expression) == THROW_EXPR)
20532 return false;
20533
20534 /* The types of these expressions depends only on the type to which
20535 the cast occurs. */
20536 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20537 || TREE_CODE (expression) == STATIC_CAST_EXPR
20538 || TREE_CODE (expression) == CONST_CAST_EXPR
20539 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20540 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20541 || TREE_CODE (expression) == CAST_EXPR)
20542 return dependent_type_p (TREE_TYPE (expression));
20543
20544 /* The types of these expressions depends only on the type created
20545 by the expression. */
20546 if (TREE_CODE (expression) == NEW_EXPR
20547 || TREE_CODE (expression) == VEC_NEW_EXPR)
20548 {
20549 /* For NEW_EXPR tree nodes created inside a template, either
20550 the object type itself or a TREE_LIST may appear as the
20551 operand 1. */
20552 tree type = TREE_OPERAND (expression, 1);
20553 if (TREE_CODE (type) == TREE_LIST)
20554 /* This is an array type. We need to check array dimensions
20555 as well. */
20556 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20557 || value_dependent_expression_p
20558 (TREE_OPERAND (TREE_VALUE (type), 1));
20559 else
20560 return dependent_type_p (type);
20561 }
20562
20563 if (TREE_CODE (expression) == SCOPE_REF)
20564 {
20565 tree scope = TREE_OPERAND (expression, 0);
20566 tree name = TREE_OPERAND (expression, 1);
20567
20568 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20569 contains an identifier associated by name lookup with one or more
20570 declarations declared with a dependent type, or...a
20571 nested-name-specifier or qualified-id that names a member of an
20572 unknown specialization. */
20573 return (type_dependent_expression_p (name)
20574 || dependent_scope_p (scope));
20575 }
20576
20577 if (TREE_CODE (expression) == FUNCTION_DECL
20578 && DECL_LANG_SPECIFIC (expression)
20579 && DECL_TEMPLATE_INFO (expression)
20580 && (any_dependent_template_arguments_p
20581 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20582 return true;
20583
20584 if (TREE_CODE (expression) == TEMPLATE_DECL
20585 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20586 return false;
20587
20588 if (TREE_CODE (expression) == STMT_EXPR)
20589 expression = stmt_expr_value_expr (expression);
20590
20591 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20592 {
20593 tree elt;
20594 unsigned i;
20595
20596 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20597 {
20598 if (type_dependent_expression_p (elt))
20599 return true;
20600 }
20601 return false;
20602 }
20603
20604 /* A static data member of the current instantiation with incomplete
20605 array type is type-dependent, as the definition and specializations
20606 can have different bounds. */
20607 if (VAR_P (expression)
20608 && DECL_CLASS_SCOPE_P (expression)
20609 && dependent_type_p (DECL_CONTEXT (expression))
20610 && VAR_HAD_UNKNOWN_BOUND (expression))
20611 return true;
20612
20613 /* An array of unknown bound depending on a variadic parameter, eg:
20614
20615 template<typename... Args>
20616 void foo (Args... args)
20617 {
20618 int arr[] = { args... };
20619 }
20620
20621 template<int... vals>
20622 void bar ()
20623 {
20624 int arr[] = { vals... };
20625 }
20626
20627 If the array has no length and has an initializer, it must be that
20628 we couldn't determine its length in cp_complete_array_type because
20629 it is dependent. */
20630 if (VAR_P (expression)
20631 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20632 && !TYPE_DOMAIN (TREE_TYPE (expression))
20633 && DECL_INITIAL (expression))
20634 return true;
20635
20636 if (TREE_TYPE (expression) == unknown_type_node)
20637 {
20638 if (TREE_CODE (expression) == ADDR_EXPR)
20639 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20640 if (TREE_CODE (expression) == COMPONENT_REF
20641 || TREE_CODE (expression) == OFFSET_REF)
20642 {
20643 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20644 return true;
20645 expression = TREE_OPERAND (expression, 1);
20646 if (identifier_p (expression))
20647 return false;
20648 }
20649 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20650 if (TREE_CODE (expression) == SCOPE_REF)
20651 return false;
20652
20653 /* Always dependent, on the number of arguments if nothing else. */
20654 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20655 return true;
20656
20657 if (BASELINK_P (expression))
20658 expression = BASELINK_FUNCTIONS (expression);
20659
20660 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20661 {
20662 if (any_dependent_template_arguments_p
20663 (TREE_OPERAND (expression, 1)))
20664 return true;
20665 expression = TREE_OPERAND (expression, 0);
20666 }
20667 gcc_assert (TREE_CODE (expression) == OVERLOAD
20668 || TREE_CODE (expression) == FUNCTION_DECL);
20669
20670 while (expression)
20671 {
20672 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20673 return true;
20674 expression = OVL_NEXT (expression);
20675 }
20676 return false;
20677 }
20678
20679 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20680
20681 return (dependent_type_p (TREE_TYPE (expression)));
20682 }
20683
20684 /* walk_tree callback function for instantiation_dependent_expression_p,
20685 below. Returns non-zero if a dependent subexpression is found. */
20686
20687 static tree
20688 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20689 void * /*data*/)
20690 {
20691 if (TYPE_P (*tp))
20692 {
20693 /* We don't have to worry about decltype currently because decltype
20694 of an instantiation-dependent expr is a dependent type. This
20695 might change depending on the resolution of DR 1172. */
20696 *walk_subtrees = false;
20697 return NULL_TREE;
20698 }
20699 enum tree_code code = TREE_CODE (*tp);
20700 switch (code)
20701 {
20702 /* Don't treat an argument list as dependent just because it has no
20703 TREE_TYPE. */
20704 case TREE_LIST:
20705 case TREE_VEC:
20706 return NULL_TREE;
20707
20708 case VAR_DECL:
20709 case CONST_DECL:
20710 /* A constant with a dependent initializer is dependent. */
20711 if (value_dependent_expression_p (*tp))
20712 return *tp;
20713 break;
20714
20715 case TEMPLATE_PARM_INDEX:
20716 return *tp;
20717
20718 /* Handle expressions with type operands. */
20719 case SIZEOF_EXPR:
20720 case ALIGNOF_EXPR:
20721 case TYPEID_EXPR:
20722 case AT_ENCODE_EXPR:
20723 {
20724 tree op = TREE_OPERAND (*tp, 0);
20725 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20726 op = TREE_TYPE (op);
20727 if (TYPE_P (op))
20728 {
20729 if (dependent_type_p (op))
20730 return *tp;
20731 else
20732 {
20733 *walk_subtrees = false;
20734 return NULL_TREE;
20735 }
20736 }
20737 break;
20738 }
20739
20740 case TRAIT_EXPR:
20741 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20742 || (TRAIT_EXPR_TYPE2 (*tp)
20743 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20744 return *tp;
20745 *walk_subtrees = false;
20746 return NULL_TREE;
20747
20748 case COMPONENT_REF:
20749 if (identifier_p (TREE_OPERAND (*tp, 1)))
20750 /* In a template, finish_class_member_access_expr creates a
20751 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20752 type-dependent, so that we can check access control at
20753 instantiation time (PR 42277). See also Core issue 1273. */
20754 return *tp;
20755 break;
20756
20757 case SCOPE_REF:
20758 if (instantiation_dependent_scope_ref_p (*tp))
20759 return *tp;
20760 else
20761 break;
20762
20763 /* Treat statement-expressions as dependent. */
20764 case BIND_EXPR:
20765 return *tp;
20766
20767 default:
20768 break;
20769 }
20770
20771 if (type_dependent_expression_p (*tp))
20772 return *tp;
20773 else
20774 return NULL_TREE;
20775 }
20776
20777 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20778 sense defined by the ABI:
20779
20780 "An expression is instantiation-dependent if it is type-dependent
20781 or value-dependent, or it has a subexpression that is type-dependent
20782 or value-dependent." */
20783
20784 bool
20785 instantiation_dependent_expression_p (tree expression)
20786 {
20787 tree result;
20788
20789 if (!processing_template_decl)
20790 return false;
20791
20792 if (expression == error_mark_node)
20793 return false;
20794
20795 result = cp_walk_tree_without_duplicates (&expression,
20796 instantiation_dependent_r, NULL);
20797 return result != NULL_TREE;
20798 }
20799
20800 /* Like type_dependent_expression_p, but it also works while not processing
20801 a template definition, i.e. during substitution or mangling. */
20802
20803 bool
20804 type_dependent_expression_p_push (tree expr)
20805 {
20806 bool b;
20807 ++processing_template_decl;
20808 b = type_dependent_expression_p (expr);
20809 --processing_template_decl;
20810 return b;
20811 }
20812
20813 /* Returns TRUE if ARGS contains a type-dependent expression. */
20814
20815 bool
20816 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20817 {
20818 unsigned int i;
20819 tree arg;
20820
20821 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20822 {
20823 if (type_dependent_expression_p (arg))
20824 return true;
20825 }
20826 return false;
20827 }
20828
20829 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20830 expressions) contains any type-dependent expressions. */
20831
20832 bool
20833 any_type_dependent_elements_p (const_tree list)
20834 {
20835 for (; list; list = TREE_CHAIN (list))
20836 if (type_dependent_expression_p (TREE_VALUE (list)))
20837 return true;
20838
20839 return false;
20840 }
20841
20842 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20843 expressions) contains any value-dependent expressions. */
20844
20845 bool
20846 any_value_dependent_elements_p (const_tree list)
20847 {
20848 for (; list; list = TREE_CHAIN (list))
20849 if (value_dependent_expression_p (TREE_VALUE (list)))
20850 return true;
20851
20852 return false;
20853 }
20854
20855 /* Returns TRUE if the ARG (a template argument) is dependent. */
20856
20857 bool
20858 dependent_template_arg_p (tree arg)
20859 {
20860 if (!processing_template_decl)
20861 return false;
20862
20863 /* Assume a template argument that was wrongly written by the user
20864 is dependent. This is consistent with what
20865 any_dependent_template_arguments_p [that calls this function]
20866 does. */
20867 if (!arg || arg == error_mark_node)
20868 return true;
20869
20870 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20871 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20872
20873 if (TREE_CODE (arg) == TEMPLATE_DECL
20874 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20875 return dependent_template_p (arg);
20876 else if (ARGUMENT_PACK_P (arg))
20877 {
20878 tree args = ARGUMENT_PACK_ARGS (arg);
20879 int i, len = TREE_VEC_LENGTH (args);
20880 for (i = 0; i < len; ++i)
20881 {
20882 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20883 return true;
20884 }
20885
20886 return false;
20887 }
20888 else if (TYPE_P (arg))
20889 return dependent_type_p (arg);
20890 else
20891 return (type_dependent_expression_p (arg)
20892 || value_dependent_expression_p (arg));
20893 }
20894
20895 /* Returns true if ARGS (a collection of template arguments) contains
20896 any types that require structural equality testing. */
20897
20898 bool
20899 any_template_arguments_need_structural_equality_p (tree args)
20900 {
20901 int i;
20902 int j;
20903
20904 if (!args)
20905 return false;
20906 if (args == error_mark_node)
20907 return true;
20908
20909 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20910 {
20911 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20912 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20913 {
20914 tree arg = TREE_VEC_ELT (level, j);
20915 tree packed_args = NULL_TREE;
20916 int k, len = 1;
20917
20918 if (ARGUMENT_PACK_P (arg))
20919 {
20920 /* Look inside the argument pack. */
20921 packed_args = ARGUMENT_PACK_ARGS (arg);
20922 len = TREE_VEC_LENGTH (packed_args);
20923 }
20924
20925 for (k = 0; k < len; ++k)
20926 {
20927 if (packed_args)
20928 arg = TREE_VEC_ELT (packed_args, k);
20929
20930 if (error_operand_p (arg))
20931 return true;
20932 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20933 continue;
20934 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20935 return true;
20936 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20937 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20938 return true;
20939 }
20940 }
20941 }
20942
20943 return false;
20944 }
20945
20946 /* Returns true if ARGS (a collection of template arguments) contains
20947 any dependent arguments. */
20948
20949 bool
20950 any_dependent_template_arguments_p (const_tree args)
20951 {
20952 int i;
20953 int j;
20954
20955 if (!args)
20956 return false;
20957 if (args == error_mark_node)
20958 return true;
20959
20960 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20961 {
20962 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20963 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20964 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20965 return true;
20966 }
20967
20968 return false;
20969 }
20970
20971 /* Returns TRUE if the template TMPL is dependent. */
20972
20973 bool
20974 dependent_template_p (tree tmpl)
20975 {
20976 if (TREE_CODE (tmpl) == OVERLOAD)
20977 {
20978 while (tmpl)
20979 {
20980 if (dependent_template_p (OVL_CURRENT (tmpl)))
20981 return true;
20982 tmpl = OVL_NEXT (tmpl);
20983 }
20984 return false;
20985 }
20986
20987 /* Template template parameters are dependent. */
20988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20989 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20990 return true;
20991 /* So are names that have not been looked up. */
20992 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
20993 return true;
20994 /* So are member templates of dependent classes. */
20995 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20996 return dependent_type_p (DECL_CONTEXT (tmpl));
20997 return false;
20998 }
20999
21000 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21001
21002 bool
21003 dependent_template_id_p (tree tmpl, tree args)
21004 {
21005 return (dependent_template_p (tmpl)
21006 || any_dependent_template_arguments_p (args));
21007 }
21008
21009 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21010 is dependent. */
21011
21012 bool
21013 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21014 {
21015 int i;
21016
21017 if (!processing_template_decl)
21018 return false;
21019
21020 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21021 {
21022 tree decl = TREE_VEC_ELT (declv, i);
21023 tree init = TREE_VEC_ELT (initv, i);
21024 tree cond = TREE_VEC_ELT (condv, i);
21025 tree incr = TREE_VEC_ELT (incrv, i);
21026
21027 if (type_dependent_expression_p (decl))
21028 return true;
21029
21030 if (init && type_dependent_expression_p (init))
21031 return true;
21032
21033 if (type_dependent_expression_p (cond))
21034 return true;
21035
21036 if (COMPARISON_CLASS_P (cond)
21037 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21038 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21039 return true;
21040
21041 if (TREE_CODE (incr) == MODOP_EXPR)
21042 {
21043 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21044 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21045 return true;
21046 }
21047 else if (type_dependent_expression_p (incr))
21048 return true;
21049 else if (TREE_CODE (incr) == MODIFY_EXPR)
21050 {
21051 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21052 return true;
21053 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21054 {
21055 tree t = TREE_OPERAND (incr, 1);
21056 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21057 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21058 return true;
21059 }
21060 }
21061 }
21062
21063 return false;
21064 }
21065
21066 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21067 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21068 no such TYPE can be found. Note that this function peers inside
21069 uninstantiated templates and therefore should be used only in
21070 extremely limited situations. ONLY_CURRENT_P restricts this
21071 peering to the currently open classes hierarchy (which is required
21072 when comparing types). */
21073
21074 tree
21075 resolve_typename_type (tree type, bool only_current_p)
21076 {
21077 tree scope;
21078 tree name;
21079 tree decl;
21080 int quals;
21081 tree pushed_scope;
21082 tree result;
21083
21084 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21085
21086 scope = TYPE_CONTEXT (type);
21087 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21088 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21089 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21090 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21091 identifier of the TYPENAME_TYPE anymore.
21092 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21093 TYPENAME_TYPE instead, we avoid messing up with a possible
21094 typedef variant case. */
21095 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21096
21097 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21098 it first before we can figure out what NAME refers to. */
21099 if (TREE_CODE (scope) == TYPENAME_TYPE)
21100 {
21101 if (TYPENAME_IS_RESOLVING_P (scope))
21102 /* Given a class template A with a dependent base with nested type C,
21103 typedef typename A::C::C C will land us here, as trying to resolve
21104 the initial A::C leads to the local C typedef, which leads back to
21105 A::C::C. So we break the recursion now. */
21106 return type;
21107 else
21108 scope = resolve_typename_type (scope, only_current_p);
21109 }
21110 /* If we don't know what SCOPE refers to, then we cannot resolve the
21111 TYPENAME_TYPE. */
21112 if (TREE_CODE (scope) == TYPENAME_TYPE)
21113 return type;
21114 /* If the SCOPE is a template type parameter, we have no way of
21115 resolving the name. */
21116 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21117 return type;
21118 /* If the SCOPE is not the current instantiation, there's no reason
21119 to look inside it. */
21120 if (only_current_p && !currently_open_class (scope))
21121 return type;
21122 /* If this is a typedef, we don't want to look inside (c++/11987). */
21123 if (typedef_variant_p (type))
21124 return type;
21125 /* If SCOPE isn't the template itself, it will not have a valid
21126 TYPE_FIELDS list. */
21127 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21128 /* scope is either the template itself or a compatible instantiation
21129 like X<T>, so look up the name in the original template. */
21130 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21131 else
21132 /* scope is a partial instantiation, so we can't do the lookup or we
21133 will lose the template arguments. */
21134 return type;
21135 /* Enter the SCOPE so that name lookup will be resolved as if we
21136 were in the class definition. In particular, SCOPE will no
21137 longer be considered a dependent type. */
21138 pushed_scope = push_scope (scope);
21139 /* Look up the declaration. */
21140 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21141 tf_warning_or_error);
21142
21143 result = NULL_TREE;
21144
21145 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21146 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21147 if (!decl)
21148 /*nop*/;
21149 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21150 && TREE_CODE (decl) == TYPE_DECL)
21151 {
21152 result = TREE_TYPE (decl);
21153 if (result == error_mark_node)
21154 result = NULL_TREE;
21155 }
21156 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21157 && DECL_CLASS_TEMPLATE_P (decl))
21158 {
21159 tree tmpl;
21160 tree args;
21161 /* Obtain the template and the arguments. */
21162 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21163 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21164 /* Instantiate the template. */
21165 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21166 /*entering_scope=*/0,
21167 tf_error | tf_user);
21168 if (result == error_mark_node)
21169 result = NULL_TREE;
21170 }
21171
21172 /* Leave the SCOPE. */
21173 if (pushed_scope)
21174 pop_scope (pushed_scope);
21175
21176 /* If we failed to resolve it, return the original typename. */
21177 if (!result)
21178 return type;
21179
21180 /* If lookup found a typename type, resolve that too. */
21181 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21182 {
21183 /* Ill-formed programs can cause infinite recursion here, so we
21184 must catch that. */
21185 TYPENAME_IS_RESOLVING_P (type) = 1;
21186 result = resolve_typename_type (result, only_current_p);
21187 TYPENAME_IS_RESOLVING_P (type) = 0;
21188 }
21189
21190 /* Qualify the resulting type. */
21191 quals = cp_type_quals (type);
21192 if (quals)
21193 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21194
21195 return result;
21196 }
21197
21198 /* EXPR is an expression which is not type-dependent. Return a proxy
21199 for EXPR that can be used to compute the types of larger
21200 expressions containing EXPR. */
21201
21202 tree
21203 build_non_dependent_expr (tree expr)
21204 {
21205 tree inner_expr;
21206
21207 #ifdef ENABLE_CHECKING
21208 /* Try to get a constant value for all non-dependent expressions in
21209 order to expose bugs in *_dependent_expression_p and constexpr. */
21210 if (cxx_dialect >= cxx11)
21211 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21212 #endif
21213
21214 /* Preserve OVERLOADs; the functions must be available to resolve
21215 types. */
21216 inner_expr = expr;
21217 if (TREE_CODE (inner_expr) == STMT_EXPR)
21218 inner_expr = stmt_expr_value_expr (inner_expr);
21219 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21220 inner_expr = TREE_OPERAND (inner_expr, 0);
21221 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21222 inner_expr = TREE_OPERAND (inner_expr, 1);
21223 if (is_overloaded_fn (inner_expr)
21224 || TREE_CODE (inner_expr) == OFFSET_REF)
21225 return expr;
21226 /* There is no need to return a proxy for a variable. */
21227 if (VAR_P (expr))
21228 return expr;
21229 /* Preserve string constants; conversions from string constants to
21230 "char *" are allowed, even though normally a "const char *"
21231 cannot be used to initialize a "char *". */
21232 if (TREE_CODE (expr) == STRING_CST)
21233 return expr;
21234 /* Preserve arithmetic constants, as an optimization -- there is no
21235 reason to create a new node. */
21236 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21237 return expr;
21238 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21239 There is at least one place where we want to know that a
21240 particular expression is a throw-expression: when checking a ?:
21241 expression, there are special rules if the second or third
21242 argument is a throw-expression. */
21243 if (TREE_CODE (expr) == THROW_EXPR)
21244 return expr;
21245
21246 /* Don't wrap an initializer list, we need to be able to look inside. */
21247 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21248 return expr;
21249
21250 /* Don't wrap a dummy object, we need to be able to test for it. */
21251 if (is_dummy_object (expr))
21252 return expr;
21253
21254 if (TREE_CODE (expr) == COND_EXPR)
21255 return build3 (COND_EXPR,
21256 TREE_TYPE (expr),
21257 TREE_OPERAND (expr, 0),
21258 (TREE_OPERAND (expr, 1)
21259 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21260 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21261 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21262 if (TREE_CODE (expr) == COMPOUND_EXPR
21263 && !COMPOUND_EXPR_OVERLOADED (expr))
21264 return build2 (COMPOUND_EXPR,
21265 TREE_TYPE (expr),
21266 TREE_OPERAND (expr, 0),
21267 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21268
21269 /* If the type is unknown, it can't really be non-dependent */
21270 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21271
21272 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21273 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21274 }
21275
21276 /* ARGS is a vector of expressions as arguments to a function call.
21277 Replace the arguments with equivalent non-dependent expressions.
21278 This modifies ARGS in place. */
21279
21280 void
21281 make_args_non_dependent (vec<tree, va_gc> *args)
21282 {
21283 unsigned int ix;
21284 tree arg;
21285
21286 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21287 {
21288 tree newarg = build_non_dependent_expr (arg);
21289 if (newarg != arg)
21290 (*args)[ix] = newarg;
21291 }
21292 }
21293
21294 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21295 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21296 parms. */
21297
21298 static tree
21299 make_auto_1 (tree name)
21300 {
21301 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21302 TYPE_NAME (au) = build_decl (input_location,
21303 TYPE_DECL, name, au);
21304 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21305 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21306 (0, processing_template_decl + 1, processing_template_decl + 1,
21307 TYPE_NAME (au), NULL_TREE);
21308 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21309 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21310 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21311
21312 return au;
21313 }
21314
21315 tree
21316 make_decltype_auto (void)
21317 {
21318 return make_auto_1 (get_identifier ("decltype(auto)"));
21319 }
21320
21321 tree
21322 make_auto (void)
21323 {
21324 return make_auto_1 (get_identifier ("auto"));
21325 }
21326
21327 /* Given type ARG, return std::initializer_list<ARG>. */
21328
21329 static tree
21330 listify (tree arg)
21331 {
21332 tree std_init_list = namespace_binding
21333 (get_identifier ("initializer_list"), std_node);
21334 tree argvec;
21335 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21336 {
21337 error ("deducing from brace-enclosed initializer list requires "
21338 "#include <initializer_list>");
21339 return error_mark_node;
21340 }
21341 argvec = make_tree_vec (1);
21342 TREE_VEC_ELT (argvec, 0) = arg;
21343 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21344 NULL_TREE, 0, tf_warning_or_error);
21345 }
21346
21347 /* Replace auto in TYPE with std::initializer_list<auto>. */
21348
21349 static tree
21350 listify_autos (tree type, tree auto_node)
21351 {
21352 tree init_auto = listify (auto_node);
21353 tree argvec = make_tree_vec (1);
21354 TREE_VEC_ELT (argvec, 0) = init_auto;
21355 if (processing_template_decl)
21356 argvec = add_to_template_args (current_template_args (), argvec);
21357 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21358 }
21359
21360 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21361 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21362
21363 tree
21364 do_auto_deduction (tree type, tree init, tree auto_node)
21365 {
21366 tree targs;
21367
21368 if (init == error_mark_node)
21369 return error_mark_node;
21370
21371 if (type_dependent_expression_p (init))
21372 /* Defining a subset of type-dependent expressions that we can deduce
21373 from ahead of time isn't worth the trouble. */
21374 return type;
21375
21376 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21377 with either a new invented type template parameter U or, if the
21378 initializer is a braced-init-list (8.5.4), with
21379 std::initializer_list<U>. */
21380 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21381 type = listify_autos (type, auto_node);
21382
21383 init = resolve_nondeduced_context (init);
21384
21385 targs = make_tree_vec (1);
21386 if (AUTO_IS_DECLTYPE (auto_node))
21387 {
21388 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21389 TREE_VEC_ELT (targs, 0)
21390 = finish_decltype_type (init, id, tf_warning_or_error);
21391 if (type != auto_node)
21392 {
21393 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21394 return error_mark_node;
21395 }
21396 }
21397 else
21398 {
21399 tree parms = build_tree_list (NULL_TREE, type);
21400 tree tparms = make_tree_vec (1);
21401 int val;
21402
21403 TREE_VEC_ELT (tparms, 0)
21404 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21405 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21406 DEDUCE_CALL, LOOKUP_NORMAL,
21407 NULL, /*explain_p=*/false);
21408 if (val > 0)
21409 {
21410 if (processing_template_decl)
21411 /* Try again at instantiation time. */
21412 return type;
21413 if (type && type != error_mark_node)
21414 /* If type is error_mark_node a diagnostic must have been
21415 emitted by now. Also, having a mention to '<type error>'
21416 in the diagnostic is not really useful to the user. */
21417 {
21418 if (cfun && auto_node == current_function_auto_return_pattern
21419 && LAMBDA_FUNCTION_P (current_function_decl))
21420 error ("unable to deduce lambda return type from %qE", init);
21421 else
21422 error ("unable to deduce %qT from %qE", type, init);
21423 }
21424 return error_mark_node;
21425 }
21426 }
21427
21428 /* If the list of declarators contains more than one declarator, the type
21429 of each declared variable is determined as described above. If the
21430 type deduced for the template parameter U is not the same in each
21431 deduction, the program is ill-formed. */
21432 if (TREE_TYPE (auto_node)
21433 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21434 {
21435 if (cfun && auto_node == current_function_auto_return_pattern
21436 && LAMBDA_FUNCTION_P (current_function_decl))
21437 error ("inconsistent types %qT and %qT deduced for "
21438 "lambda return type", TREE_TYPE (auto_node),
21439 TREE_VEC_ELT (targs, 0));
21440 else
21441 error ("inconsistent deduction for %qT: %qT and then %qT",
21442 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21443 return error_mark_node;
21444 }
21445 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21446
21447 if (processing_template_decl)
21448 targs = add_to_template_args (current_template_args (), targs);
21449 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21450 }
21451
21452 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21453 result. */
21454
21455 tree
21456 splice_late_return_type (tree type, tree late_return_type)
21457 {
21458 tree argvec;
21459
21460 if (late_return_type == NULL_TREE)
21461 return type;
21462 argvec = make_tree_vec (1);
21463 TREE_VEC_ELT (argvec, 0) = late_return_type;
21464 if (processing_template_parmlist)
21465 /* For a late-specified return type in a template type-parameter, we
21466 need to add a dummy argument level for its parmlist. */
21467 argvec = add_to_template_args
21468 (make_tree_vec (processing_template_parmlist), argvec);
21469 if (current_template_parms)
21470 argvec = add_to_template_args (current_template_args (), argvec);
21471 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21472 }
21473
21474 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21475 'decltype(auto)'. */
21476
21477 bool
21478 is_auto (const_tree type)
21479 {
21480 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21481 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21482 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21483 return true;
21484 else
21485 return false;
21486 }
21487
21488 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21489 a use of `auto'. Returns NULL_TREE otherwise. */
21490
21491 tree
21492 type_uses_auto (tree type)
21493 {
21494 return find_type_usage (type, is_auto);
21495 }
21496
21497 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21498 'decltype(auto)' or a concept. */
21499
21500 bool
21501 is_auto_or_concept (const_tree type)
21502 {
21503 return is_auto (type); // or concept
21504 }
21505
21506 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21507 a concept identifier) iff TYPE contains a use of a generic type. Returns
21508 NULL_TREE otherwise. */
21509
21510 tree
21511 type_uses_auto_or_concept (tree type)
21512 {
21513 return find_type_usage (type, is_auto_or_concept);
21514 }
21515
21516
21517 /* For a given template T, return the vector of typedefs referenced
21518 in T for which access check is needed at T instantiation time.
21519 T is either a FUNCTION_DECL or a RECORD_TYPE.
21520 Those typedefs were added to T by the function
21521 append_type_to_template_for_access_check. */
21522
21523 vec<qualified_typedef_usage_t, va_gc> *
21524 get_types_needing_access_check (tree t)
21525 {
21526 tree ti;
21527 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21528
21529 if (!t || t == error_mark_node)
21530 return NULL;
21531
21532 if (!(ti = get_template_info (t)))
21533 return NULL;
21534
21535 if (CLASS_TYPE_P (t)
21536 || TREE_CODE (t) == FUNCTION_DECL)
21537 {
21538 if (!TI_TEMPLATE (ti))
21539 return NULL;
21540
21541 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21542 }
21543
21544 return result;
21545 }
21546
21547 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21548 tied to T. That list of typedefs will be access checked at
21549 T instantiation time.
21550 T is either a FUNCTION_DECL or a RECORD_TYPE.
21551 TYPE_DECL is a TYPE_DECL node representing a typedef.
21552 SCOPE is the scope through which TYPE_DECL is accessed.
21553 LOCATION is the location of the usage point of TYPE_DECL.
21554
21555 This function is a subroutine of
21556 append_type_to_template_for_access_check. */
21557
21558 static void
21559 append_type_to_template_for_access_check_1 (tree t,
21560 tree type_decl,
21561 tree scope,
21562 location_t location)
21563 {
21564 qualified_typedef_usage_t typedef_usage;
21565 tree ti;
21566
21567 if (!t || t == error_mark_node)
21568 return;
21569
21570 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21571 || CLASS_TYPE_P (t))
21572 && type_decl
21573 && TREE_CODE (type_decl) == TYPE_DECL
21574 && scope);
21575
21576 if (!(ti = get_template_info (t)))
21577 return;
21578
21579 gcc_assert (TI_TEMPLATE (ti));
21580
21581 typedef_usage.typedef_decl = type_decl;
21582 typedef_usage.context = scope;
21583 typedef_usage.locus = location;
21584
21585 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21586 }
21587
21588 /* Append TYPE_DECL to the template TEMPL.
21589 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21590 At TEMPL instanciation time, TYPE_DECL will be checked to see
21591 if it can be accessed through SCOPE.
21592 LOCATION is the location of the usage point of TYPE_DECL.
21593
21594 e.g. consider the following code snippet:
21595
21596 class C
21597 {
21598 typedef int myint;
21599 };
21600
21601 template<class U> struct S
21602 {
21603 C::myint mi; // <-- usage point of the typedef C::myint
21604 };
21605
21606 S<char> s;
21607
21608 At S<char> instantiation time, we need to check the access of C::myint
21609 In other words, we need to check the access of the myint typedef through
21610 the C scope. For that purpose, this function will add the myint typedef
21611 and the scope C through which its being accessed to a list of typedefs
21612 tied to the template S. That list will be walked at template instantiation
21613 time and access check performed on each typedefs it contains.
21614 Note that this particular code snippet should yield an error because
21615 myint is private to C. */
21616
21617 void
21618 append_type_to_template_for_access_check (tree templ,
21619 tree type_decl,
21620 tree scope,
21621 location_t location)
21622 {
21623 qualified_typedef_usage_t *iter;
21624 unsigned i;
21625
21626 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21627
21628 /* Make sure we don't append the type to the template twice. */
21629 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21630 if (iter->typedef_decl == type_decl && scope == iter->context)
21631 return;
21632
21633 append_type_to_template_for_access_check_1 (templ, type_decl,
21634 scope, location);
21635 }
21636
21637 /* Set up the hash tables for template instantiations. */
21638
21639 void
21640 init_template_processing (void)
21641 {
21642 decl_specializations = htab_create_ggc (37,
21643 hash_specialization,
21644 eq_specializations,
21645 ggc_free);
21646 type_specializations = htab_create_ggc (37,
21647 hash_specialization,
21648 eq_specializations,
21649 ggc_free);
21650 }
21651
21652 /* Print stats about the template hash tables for -fstats. */
21653
21654 void
21655 print_template_statistics (void)
21656 {
21657 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21658 "%f collisions\n", (long) htab_size (decl_specializations),
21659 (long) htab_elements (decl_specializations),
21660 htab_collisions (decl_specializations));
21661 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21662 "%f collisions\n", (long) htab_size (type_specializations),
21663 (long) htab_elements (type_specializations),
21664 htab_collisions (type_specializations));
21665 }
21666
21667 #include "gt-cp-pt.h"