pt.c (apply_late_template_attributes): Use attribute_takes_identifier_p.
[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 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3515 return true;
3516
3517 error ("declaration of %q+#D", decl);
3518 error (" shadows template parm %q+#D", olddecl);
3519 return false;
3520 }
3521
3522 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3523 ORIG_LEVEL, DECL, and TYPE. */
3524
3525 static tree
3526 build_template_parm_index (int index,
3527 int level,
3528 int orig_level,
3529 tree decl,
3530 tree type)
3531 {
3532 tree t = make_node (TEMPLATE_PARM_INDEX);
3533 TEMPLATE_PARM_IDX (t) = index;
3534 TEMPLATE_PARM_LEVEL (t) = level;
3535 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3536 TEMPLATE_PARM_DECL (t) = decl;
3537 TREE_TYPE (t) = type;
3538 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3539 TREE_READONLY (t) = TREE_READONLY (decl);
3540
3541 return t;
3542 }
3543
3544 /* Find the canonical type parameter for the given template type
3545 parameter. Returns the canonical type parameter, which may be TYPE
3546 if no such parameter existed. */
3547
3548 static tree
3549 canonical_type_parameter (tree type)
3550 {
3551 tree list;
3552 int idx = TEMPLATE_TYPE_IDX (type);
3553 if (!canonical_template_parms)
3554 vec_alloc (canonical_template_parms, idx+1);
3555
3556 while (canonical_template_parms->length () <= (unsigned)idx)
3557 vec_safe_push (canonical_template_parms, NULL_TREE);
3558
3559 list = (*canonical_template_parms)[idx];
3560 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3561 list = TREE_CHAIN (list);
3562
3563 if (list)
3564 return TREE_VALUE (list);
3565 else
3566 {
3567 (*canonical_template_parms)[idx]
3568 = tree_cons (NULL_TREE, type,
3569 (*canonical_template_parms)[idx]);
3570 return type;
3571 }
3572 }
3573
3574 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3575 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3576 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3577 new one is created. */
3578
3579 static tree
3580 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3581 tsubst_flags_t complain)
3582 {
3583 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3584 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3585 != TEMPLATE_PARM_LEVEL (index) - levels)
3586 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3587 {
3588 tree orig_decl = TEMPLATE_PARM_DECL (index);
3589 tree decl, t;
3590
3591 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3592 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3593 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3594 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3595 DECL_ARTIFICIAL (decl) = 1;
3596 SET_DECL_TEMPLATE_PARM_P (decl);
3597
3598 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3599 TEMPLATE_PARM_LEVEL (index) - levels,
3600 TEMPLATE_PARM_ORIG_LEVEL (index),
3601 decl, type);
3602 TEMPLATE_PARM_DESCENDANTS (index) = t;
3603 TEMPLATE_PARM_PARAMETER_PACK (t)
3604 = TEMPLATE_PARM_PARAMETER_PACK (index);
3605
3606 /* Template template parameters need this. */
3607 if (TREE_CODE (decl) == TEMPLATE_DECL)
3608 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3609 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3610 args, complain);
3611 }
3612
3613 return TEMPLATE_PARM_DESCENDANTS (index);
3614 }
3615
3616 /* Process information from new template parameter PARM and append it
3617 to the LIST being built. This new parameter is a non-type
3618 parameter iff IS_NON_TYPE is true. This new parameter is a
3619 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3620 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3621 parameter list PARM belongs to. This is used used to create a
3622 proper canonical type for the type of PARM that is to be created,
3623 iff PARM is a type. If the size is not known, this parameter shall
3624 be set to 0. */
3625
3626 tree
3627 process_template_parm (tree list, location_t parm_loc, tree parm,
3628 bool is_non_type, bool is_parameter_pack)
3629 {
3630 tree decl = 0;
3631 tree defval;
3632 tree err_parm_list;
3633 int idx = 0;
3634
3635 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3636 defval = TREE_PURPOSE (parm);
3637
3638 if (list)
3639 {
3640 tree p = tree_last (list);
3641
3642 if (p && TREE_VALUE (p) != error_mark_node)
3643 {
3644 p = TREE_VALUE (p);
3645 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3646 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3647 else
3648 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3649 }
3650
3651 ++idx;
3652 }
3653 else
3654 idx = 0;
3655
3656 if (is_non_type)
3657 {
3658 parm = TREE_VALUE (parm);
3659
3660 SET_DECL_TEMPLATE_PARM_P (parm);
3661
3662 if (TREE_TYPE (parm) == error_mark_node)
3663 {
3664 err_parm_list = build_tree_list (defval, parm);
3665 TREE_VALUE (err_parm_list) = error_mark_node;
3666 return chainon (list, err_parm_list);
3667 }
3668 else
3669 {
3670 /* [temp.param]
3671
3672 The top-level cv-qualifiers on the template-parameter are
3673 ignored when determining its type. */
3674 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3675 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3676 {
3677 err_parm_list = build_tree_list (defval, parm);
3678 TREE_VALUE (err_parm_list) = error_mark_node;
3679 return chainon (list, err_parm_list);
3680 }
3681
3682 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3683 {
3684 /* This template parameter is not a parameter pack, but it
3685 should be. Complain about "bare" parameter packs. */
3686 check_for_bare_parameter_packs (TREE_TYPE (parm));
3687
3688 /* Recover by calling this a parameter pack. */
3689 is_parameter_pack = true;
3690 }
3691 }
3692
3693 /* A template parameter is not modifiable. */
3694 TREE_CONSTANT (parm) = 1;
3695 TREE_READONLY (parm) = 1;
3696 decl = build_decl (parm_loc,
3697 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3698 TREE_CONSTANT (decl) = 1;
3699 TREE_READONLY (decl) = 1;
3700 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3701 = build_template_parm_index (idx, processing_template_decl,
3702 processing_template_decl,
3703 decl, TREE_TYPE (parm));
3704
3705 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3706 = is_parameter_pack;
3707 }
3708 else
3709 {
3710 tree t;
3711 parm = TREE_VALUE (TREE_VALUE (parm));
3712
3713 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3714 {
3715 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3716 /* This is for distinguishing between real templates and template
3717 template parameters */
3718 TREE_TYPE (parm) = t;
3719 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3720 decl = parm;
3721 }
3722 else
3723 {
3724 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3725 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3726 decl = build_decl (parm_loc,
3727 TYPE_DECL, parm, t);
3728 }
3729
3730 TYPE_NAME (t) = decl;
3731 TYPE_STUB_DECL (t) = decl;
3732 parm = decl;
3733 TEMPLATE_TYPE_PARM_INDEX (t)
3734 = build_template_parm_index (idx, processing_template_decl,
3735 processing_template_decl,
3736 decl, TREE_TYPE (parm));
3737 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3738 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3739 }
3740 DECL_ARTIFICIAL (decl) = 1;
3741 SET_DECL_TEMPLATE_PARM_P (decl);
3742 pushdecl (decl);
3743 parm = build_tree_list (defval, parm);
3744 return chainon (list, parm);
3745 }
3746
3747 /* The end of a template parameter list has been reached. Process the
3748 tree list into a parameter vector, converting each parameter into a more
3749 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3750 as PARM_DECLs. */
3751
3752 tree
3753 end_template_parm_list (tree parms)
3754 {
3755 int nparms;
3756 tree parm, next;
3757 tree saved_parmlist = make_tree_vec (list_length (parms));
3758
3759 current_template_parms
3760 = tree_cons (size_int (processing_template_decl),
3761 saved_parmlist, current_template_parms);
3762
3763 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3764 {
3765 next = TREE_CHAIN (parm);
3766 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3767 TREE_CHAIN (parm) = NULL_TREE;
3768 }
3769
3770 --processing_template_parmlist;
3771
3772 return saved_parmlist;
3773 }
3774
3775 /* end_template_decl is called after a template declaration is seen. */
3776
3777 void
3778 end_template_decl (void)
3779 {
3780 reset_specialization ();
3781
3782 if (! processing_template_decl)
3783 return;
3784
3785 /* This matches the pushlevel in begin_template_parm_list. */
3786 finish_scope ();
3787
3788 --processing_template_decl;
3789 current_template_parms = TREE_CHAIN (current_template_parms);
3790 }
3791
3792 /* Takes a TREE_LIST representing a template parameter and convert it
3793 into an argument suitable to be passed to the type substitution
3794 functions. Note that If the TREE_LIST contains an error_mark
3795 node, the returned argument is error_mark_node. */
3796
3797 static tree
3798 template_parm_to_arg (tree t)
3799 {
3800
3801 if (t == NULL_TREE
3802 || TREE_CODE (t) != TREE_LIST)
3803 return t;
3804
3805 if (error_operand_p (TREE_VALUE (t)))
3806 return error_mark_node;
3807
3808 t = TREE_VALUE (t);
3809
3810 if (TREE_CODE (t) == TYPE_DECL
3811 || TREE_CODE (t) == TEMPLATE_DECL)
3812 {
3813 t = TREE_TYPE (t);
3814
3815 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3816 {
3817 /* Turn this argument into a TYPE_ARGUMENT_PACK
3818 with a single element, which expands T. */
3819 tree vec = make_tree_vec (1);
3820 #ifdef ENABLE_CHECKING
3821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3822 (vec, TREE_VEC_LENGTH (vec));
3823 #endif
3824 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3825
3826 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3827 SET_ARGUMENT_PACK_ARGS (t, vec);
3828 }
3829 }
3830 else
3831 {
3832 t = DECL_INITIAL (t);
3833
3834 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3835 {
3836 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3837 with a single element, which expands T. */
3838 tree vec = make_tree_vec (1);
3839 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3840 #ifdef ENABLE_CHECKING
3841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3842 (vec, TREE_VEC_LENGTH (vec));
3843 #endif
3844 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3845
3846 t = make_node (NONTYPE_ARGUMENT_PACK);
3847 SET_ARGUMENT_PACK_ARGS (t, vec);
3848 TREE_TYPE (t) = type;
3849 }
3850 }
3851 return t;
3852 }
3853
3854 /* Given a set of template parameters, return them as a set of template
3855 arguments. The template parameters are represented as a TREE_VEC, in
3856 the form documented in cp-tree.h for template arguments. */
3857
3858 static tree
3859 template_parms_to_args (tree parms)
3860 {
3861 tree header;
3862 tree args = NULL_TREE;
3863 int length = TMPL_PARMS_DEPTH (parms);
3864 int l = length;
3865
3866 /* If there is only one level of template parameters, we do not
3867 create a TREE_VEC of TREE_VECs. Instead, we return a single
3868 TREE_VEC containing the arguments. */
3869 if (length > 1)
3870 args = make_tree_vec (length);
3871
3872 for (header = parms; header; header = TREE_CHAIN (header))
3873 {
3874 tree a = copy_node (TREE_VALUE (header));
3875 int i;
3876
3877 TREE_TYPE (a) = NULL_TREE;
3878 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3879 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3880
3881 #ifdef ENABLE_CHECKING
3882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3883 #endif
3884
3885 if (length > 1)
3886 TREE_VEC_ELT (args, --l) = a;
3887 else
3888 args = a;
3889 }
3890
3891 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3892 /* This can happen for template parms of a template template
3893 parameter, e.g:
3894
3895 template<template<class T, class U> class TT> struct S;
3896
3897 Consider the level of the parms of TT; T and U both have
3898 level 2; TT has no template parm of level 1. So in this case
3899 the first element of full_template_args is NULL_TREE. If we
3900 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3901 of 2. This will make tsubst wrongly consider that T and U
3902 have level 1. Instead, let's create a dummy vector as the
3903 first element of full_template_args so that TMPL_ARGS_DEPTH
3904 returns the correct depth for args. */
3905 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3906 return args;
3907 }
3908
3909 /* Within the declaration of a template, return the currently active
3910 template parameters as an argument TREE_VEC. */
3911
3912 static tree
3913 current_template_args (void)
3914 {
3915 return template_parms_to_args (current_template_parms);
3916 }
3917
3918 /* Update the declared TYPE by doing any lookups which were thought to be
3919 dependent, but are not now that we know the SCOPE of the declarator. */
3920
3921 tree
3922 maybe_update_decl_type (tree orig_type, tree scope)
3923 {
3924 tree type = orig_type;
3925
3926 if (type == NULL_TREE)
3927 return type;
3928
3929 if (TREE_CODE (orig_type) == TYPE_DECL)
3930 type = TREE_TYPE (type);
3931
3932 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3933 && dependent_type_p (type)
3934 /* Don't bother building up the args in this case. */
3935 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3936 {
3937 /* tsubst in the args corresponding to the template parameters,
3938 including auto if present. Most things will be unchanged, but
3939 make_typename_type and tsubst_qualified_id will resolve
3940 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3941 tree args = current_template_args ();
3942 tree auto_node = type_uses_auto (type);
3943 tree pushed;
3944 if (auto_node)
3945 {
3946 tree auto_vec = make_tree_vec (1);
3947 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3948 args = add_to_template_args (args, auto_vec);
3949 }
3950 pushed = push_scope (scope);
3951 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3952 if (pushed)
3953 pop_scope (scope);
3954 }
3955
3956 if (type == error_mark_node)
3957 return orig_type;
3958
3959 if (TREE_CODE (orig_type) == TYPE_DECL)
3960 {
3961 if (same_type_p (type, TREE_TYPE (orig_type)))
3962 type = orig_type;
3963 else
3964 type = TYPE_NAME (type);
3965 }
3966 return type;
3967 }
3968
3969 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3970 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3971 a member template. Used by push_template_decl below. */
3972
3973 static tree
3974 build_template_decl (tree decl, tree parms, bool member_template_p)
3975 {
3976 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3977 DECL_TEMPLATE_PARMS (tmpl) = parms;
3978 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3979 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3980 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3981
3982 return tmpl;
3983 }
3984
3985 struct template_parm_data
3986 {
3987 /* The level of the template parameters we are currently
3988 processing. */
3989 int level;
3990
3991 /* The index of the specialization argument we are currently
3992 processing. */
3993 int current_arg;
3994
3995 /* An array whose size is the number of template parameters. The
3996 elements are nonzero if the parameter has been used in any one
3997 of the arguments processed so far. */
3998 int* parms;
3999
4000 /* An array whose size is the number of template arguments. The
4001 elements are nonzero if the argument makes use of template
4002 parameters of this level. */
4003 int* arg_uses_template_parms;
4004 };
4005
4006 /* Subroutine of push_template_decl used to see if each template
4007 parameter in a partial specialization is used in the explicit
4008 argument list. If T is of the LEVEL given in DATA (which is
4009 treated as a template_parm_data*), then DATA->PARMS is marked
4010 appropriately. */
4011
4012 static int
4013 mark_template_parm (tree t, void* data)
4014 {
4015 int level;
4016 int idx;
4017 struct template_parm_data* tpd = (struct template_parm_data*) data;
4018
4019 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4020 {
4021 level = TEMPLATE_PARM_LEVEL (t);
4022 idx = TEMPLATE_PARM_IDX (t);
4023 }
4024 else
4025 {
4026 level = TEMPLATE_TYPE_LEVEL (t);
4027 idx = TEMPLATE_TYPE_IDX (t);
4028 }
4029
4030 if (level == tpd->level)
4031 {
4032 tpd->parms[idx] = 1;
4033 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4034 }
4035
4036 /* Return zero so that for_each_template_parm will continue the
4037 traversal of the tree; we want to mark *every* template parm. */
4038 return 0;
4039 }
4040
4041 /* Process the partial specialization DECL. */
4042
4043 static tree
4044 process_partial_specialization (tree decl)
4045 {
4046 tree type = TREE_TYPE (decl);
4047 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4048 tree specargs = CLASSTYPE_TI_ARGS (type);
4049 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4050 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4051 tree inner_parms;
4052 tree inst;
4053 int nargs = TREE_VEC_LENGTH (inner_args);
4054 int ntparms;
4055 int i;
4056 bool did_error_intro = false;
4057 struct template_parm_data tpd;
4058 struct template_parm_data tpd2;
4059
4060 gcc_assert (current_template_parms);
4061
4062 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4063 ntparms = TREE_VEC_LENGTH (inner_parms);
4064
4065 /* We check that each of the template parameters given in the
4066 partial specialization is used in the argument list to the
4067 specialization. For example:
4068
4069 template <class T> struct S;
4070 template <class T> struct S<T*>;
4071
4072 The second declaration is OK because `T*' uses the template
4073 parameter T, whereas
4074
4075 template <class T> struct S<int>;
4076
4077 is no good. Even trickier is:
4078
4079 template <class T>
4080 struct S1
4081 {
4082 template <class U>
4083 struct S2;
4084 template <class U>
4085 struct S2<T>;
4086 };
4087
4088 The S2<T> declaration is actually invalid; it is a
4089 full-specialization. Of course,
4090
4091 template <class U>
4092 struct S2<T (*)(U)>;
4093
4094 or some such would have been OK. */
4095 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4096 tpd.parms = XALLOCAVEC (int, ntparms);
4097 memset (tpd.parms, 0, sizeof (int) * ntparms);
4098
4099 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4100 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4101 for (i = 0; i < nargs; ++i)
4102 {
4103 tpd.current_arg = i;
4104 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4105 &mark_template_parm,
4106 &tpd,
4107 NULL,
4108 /*include_nondeduced_p=*/false);
4109 }
4110 for (i = 0; i < ntparms; ++i)
4111 if (tpd.parms[i] == 0)
4112 {
4113 /* One of the template parms was not used in the
4114 specialization. */
4115 if (!did_error_intro)
4116 {
4117 error ("template parameters not used in partial specialization:");
4118 did_error_intro = true;
4119 }
4120
4121 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4122 }
4123
4124 if (did_error_intro)
4125 return error_mark_node;
4126
4127 /* [temp.class.spec]
4128
4129 The argument list of the specialization shall not be identical to
4130 the implicit argument list of the primary template. */
4131 if (comp_template_args
4132 (inner_args,
4133 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4134 (maintmpl)))))
4135 error ("partial specialization %qT does not specialize any template arguments", type);
4136
4137 /* A partial specialization that replaces multiple parameters of the
4138 primary template with a pack expansion is less specialized for those
4139 parameters. */
4140 if (nargs < DECL_NTPARMS (maintmpl))
4141 {
4142 error ("partial specialization is not more specialized than the "
4143 "primary template because it replaces multiple parameters "
4144 "with a pack expansion");
4145 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4146 return decl;
4147 }
4148
4149 /* [temp.class.spec]
4150
4151 A partially specialized non-type argument expression shall not
4152 involve template parameters of the partial specialization except
4153 when the argument expression is a simple identifier.
4154
4155 The type of a template parameter corresponding to a specialized
4156 non-type argument shall not be dependent on a parameter of the
4157 specialization.
4158
4159 Also, we verify that pack expansions only occur at the
4160 end of the argument list. */
4161 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4162 tpd2.parms = 0;
4163 for (i = 0; i < nargs; ++i)
4164 {
4165 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4166 tree arg = TREE_VEC_ELT (inner_args, i);
4167 tree packed_args = NULL_TREE;
4168 int j, len = 1;
4169
4170 if (ARGUMENT_PACK_P (arg))
4171 {
4172 /* Extract the arguments from the argument pack. We'll be
4173 iterating over these in the following loop. */
4174 packed_args = ARGUMENT_PACK_ARGS (arg);
4175 len = TREE_VEC_LENGTH (packed_args);
4176 }
4177
4178 for (j = 0; j < len; j++)
4179 {
4180 if (packed_args)
4181 /* Get the Jth argument in the parameter pack. */
4182 arg = TREE_VEC_ELT (packed_args, j);
4183
4184 if (PACK_EXPANSION_P (arg))
4185 {
4186 /* Pack expansions must come at the end of the
4187 argument list. */
4188 if ((packed_args && j < len - 1)
4189 || (!packed_args && i < nargs - 1))
4190 {
4191 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4192 error ("parameter pack argument %qE must be at the "
4193 "end of the template argument list", arg);
4194 else
4195 error ("parameter pack argument %qT must be at the "
4196 "end of the template argument list", arg);
4197 }
4198 }
4199
4200 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4201 /* We only care about the pattern. */
4202 arg = PACK_EXPANSION_PATTERN (arg);
4203
4204 if (/* These first two lines are the `non-type' bit. */
4205 !TYPE_P (arg)
4206 && TREE_CODE (arg) != TEMPLATE_DECL
4207 /* This next line is the `argument expression is not just a
4208 simple identifier' condition and also the `specialized
4209 non-type argument' bit. */
4210 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4211 {
4212 if ((!packed_args && tpd.arg_uses_template_parms[i])
4213 || (packed_args && uses_template_parms (arg)))
4214 error ("template argument %qE involves template parameter(s)",
4215 arg);
4216 else
4217 {
4218 /* Look at the corresponding template parameter,
4219 marking which template parameters its type depends
4220 upon. */
4221 tree type = TREE_TYPE (parm);
4222
4223 if (!tpd2.parms)
4224 {
4225 /* We haven't yet initialized TPD2. Do so now. */
4226 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4227 /* The number of parameters here is the number in the
4228 main template, which, as checked in the assertion
4229 above, is NARGS. */
4230 tpd2.parms = XALLOCAVEC (int, nargs);
4231 tpd2.level =
4232 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4233 }
4234
4235 /* Mark the template parameters. But this time, we're
4236 looking for the template parameters of the main
4237 template, not in the specialization. */
4238 tpd2.current_arg = i;
4239 tpd2.arg_uses_template_parms[i] = 0;
4240 memset (tpd2.parms, 0, sizeof (int) * nargs);
4241 for_each_template_parm (type,
4242 &mark_template_parm,
4243 &tpd2,
4244 NULL,
4245 /*include_nondeduced_p=*/false);
4246
4247 if (tpd2.arg_uses_template_parms [i])
4248 {
4249 /* The type depended on some template parameters.
4250 If they are fully specialized in the
4251 specialization, that's OK. */
4252 int j;
4253 int count = 0;
4254 for (j = 0; j < nargs; ++j)
4255 if (tpd2.parms[j] != 0
4256 && tpd.arg_uses_template_parms [j])
4257 ++count;
4258 if (count != 0)
4259 error_n (input_location, count,
4260 "type %qT of template argument %qE depends "
4261 "on a template parameter",
4262 "type %qT of template argument %qE depends "
4263 "on template parameters",
4264 type,
4265 arg);
4266 }
4267 }
4268 }
4269 }
4270 }
4271
4272 /* We should only get here once. */
4273 gcc_assert (!COMPLETE_TYPE_P (type));
4274
4275 tree tmpl = build_template_decl (decl, current_template_parms,
4276 DECL_MEMBER_TEMPLATE_P (maintmpl));
4277 TREE_TYPE (tmpl) = type;
4278 DECL_TEMPLATE_RESULT (tmpl) = decl;
4279 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4280 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4281 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4282
4283 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4284 = tree_cons (specargs, tmpl,
4285 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4286 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4287
4288 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4289 inst = TREE_CHAIN (inst))
4290 {
4291 tree inst_type = TREE_VALUE (inst);
4292 if (COMPLETE_TYPE_P (inst_type)
4293 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4294 {
4295 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4296 if (spec && TREE_TYPE (spec) == type)
4297 permerror (input_location,
4298 "partial specialization of %qT after instantiation "
4299 "of %qT", type, inst_type);
4300 }
4301 }
4302
4303 return decl;
4304 }
4305
4306 /* Check that a template declaration's use of default arguments and
4307 parameter packs is not invalid. Here, PARMS are the template
4308 parameters. IS_PRIMARY is true if DECL is the thing declared by
4309 a primary template. IS_PARTIAL is true if DECL is a partial
4310 specialization.
4311
4312 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4313 declaration (but not a definition); 1 indicates a declaration, 2
4314 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4315 emitted for extraneous default arguments.
4316
4317 Returns TRUE if there were no errors found, FALSE otherwise. */
4318
4319 bool
4320 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4321 bool is_partial, int is_friend_decl)
4322 {
4323 const char *msg;
4324 int last_level_to_check;
4325 tree parm_level;
4326 bool no_errors = true;
4327
4328 /* [temp.param]
4329
4330 A default template-argument shall not be specified in a
4331 function template declaration or a function template definition, nor
4332 in the template-parameter-list of the definition of a member of a
4333 class template. */
4334
4335 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4336 /* You can't have a function template declaration in a local
4337 scope, nor you can you define a member of a class template in a
4338 local scope. */
4339 return true;
4340
4341 if (TREE_CODE (decl) == TYPE_DECL
4342 && TREE_TYPE (decl)
4343 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4344 /* A lambda doesn't have an explicit declaration; don't complain
4345 about the parms of the enclosing class. */
4346 return true;
4347
4348 if (current_class_type
4349 && !TYPE_BEING_DEFINED (current_class_type)
4350 && DECL_LANG_SPECIFIC (decl)
4351 && DECL_DECLARES_FUNCTION_P (decl)
4352 /* If this is either a friend defined in the scope of the class
4353 or a member function. */
4354 && (DECL_FUNCTION_MEMBER_P (decl)
4355 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4356 : DECL_FRIEND_CONTEXT (decl)
4357 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4358 : false)
4359 /* And, if it was a member function, it really was defined in
4360 the scope of the class. */
4361 && (!DECL_FUNCTION_MEMBER_P (decl)
4362 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4363 /* We already checked these parameters when the template was
4364 declared, so there's no need to do it again now. This function
4365 was defined in class scope, but we're processing its body now
4366 that the class is complete. */
4367 return true;
4368
4369 /* Core issue 226 (C++0x only): the following only applies to class
4370 templates. */
4371 if (is_primary
4372 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4373 {
4374 /* [temp.param]
4375
4376 If a template-parameter has a default template-argument, all
4377 subsequent template-parameters shall have a default
4378 template-argument supplied. */
4379 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4380 {
4381 tree inner_parms = TREE_VALUE (parm_level);
4382 int ntparms = TREE_VEC_LENGTH (inner_parms);
4383 int seen_def_arg_p = 0;
4384 int i;
4385
4386 for (i = 0; i < ntparms; ++i)
4387 {
4388 tree parm = TREE_VEC_ELT (inner_parms, i);
4389
4390 if (parm == error_mark_node)
4391 continue;
4392
4393 if (TREE_PURPOSE (parm))
4394 seen_def_arg_p = 1;
4395 else if (seen_def_arg_p
4396 && !template_parameter_pack_p (TREE_VALUE (parm)))
4397 {
4398 error ("no default argument for %qD", TREE_VALUE (parm));
4399 /* For better subsequent error-recovery, we indicate that
4400 there should have been a default argument. */
4401 TREE_PURPOSE (parm) = error_mark_node;
4402 no_errors = false;
4403 }
4404 else if (!is_partial
4405 && !is_friend_decl
4406 /* Don't complain about an enclosing partial
4407 specialization. */
4408 && parm_level == parms
4409 && TREE_CODE (decl) == TYPE_DECL
4410 && i < ntparms - 1
4411 && template_parameter_pack_p (TREE_VALUE (parm)))
4412 {
4413 /* A primary class template can only have one
4414 parameter pack, at the end of the template
4415 parameter list. */
4416
4417 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4418 error ("parameter pack %qE must be at the end of the"
4419 " template parameter list", TREE_VALUE (parm));
4420 else
4421 error ("parameter pack %qT must be at the end of the"
4422 " template parameter list",
4423 TREE_TYPE (TREE_VALUE (parm)));
4424
4425 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4426 = error_mark_node;
4427 no_errors = false;
4428 }
4429 }
4430 }
4431 }
4432
4433 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4434 || is_partial
4435 || !is_primary
4436 || is_friend_decl)
4437 /* For an ordinary class template, default template arguments are
4438 allowed at the innermost level, e.g.:
4439 template <class T = int>
4440 struct S {};
4441 but, in a partial specialization, they're not allowed even
4442 there, as we have in [temp.class.spec]:
4443
4444 The template parameter list of a specialization shall not
4445 contain default template argument values.
4446
4447 So, for a partial specialization, or for a function template
4448 (in C++98/C++03), we look at all of them. */
4449 ;
4450 else
4451 /* But, for a primary class template that is not a partial
4452 specialization we look at all template parameters except the
4453 innermost ones. */
4454 parms = TREE_CHAIN (parms);
4455
4456 /* Figure out what error message to issue. */
4457 if (is_friend_decl == 2)
4458 msg = G_("default template arguments may not be used in function template "
4459 "friend re-declaration");
4460 else if (is_friend_decl)
4461 msg = G_("default template arguments may not be used in function template "
4462 "friend declarations");
4463 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4464 msg = G_("default template arguments may not be used in function templates "
4465 "without -std=c++11 or -std=gnu++11");
4466 else if (is_partial)
4467 msg = G_("default template arguments may not be used in "
4468 "partial specializations");
4469 else
4470 msg = G_("default argument for template parameter for class enclosing %qD");
4471
4472 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4473 /* If we're inside a class definition, there's no need to
4474 examine the parameters to the class itself. On the one
4475 hand, they will be checked when the class is defined, and,
4476 on the other, default arguments are valid in things like:
4477 template <class T = double>
4478 struct S { template <class U> void f(U); };
4479 Here the default argument for `S' has no bearing on the
4480 declaration of `f'. */
4481 last_level_to_check = template_class_depth (current_class_type) + 1;
4482 else
4483 /* Check everything. */
4484 last_level_to_check = 0;
4485
4486 for (parm_level = parms;
4487 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4488 parm_level = TREE_CHAIN (parm_level))
4489 {
4490 tree inner_parms = TREE_VALUE (parm_level);
4491 int i;
4492 int ntparms;
4493
4494 ntparms = TREE_VEC_LENGTH (inner_parms);
4495 for (i = 0; i < ntparms; ++i)
4496 {
4497 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4498 continue;
4499
4500 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4501 {
4502 if (msg)
4503 {
4504 no_errors = false;
4505 if (is_friend_decl == 2)
4506 return no_errors;
4507
4508 error (msg, decl);
4509 msg = 0;
4510 }
4511
4512 /* Clear out the default argument so that we are not
4513 confused later. */
4514 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4515 }
4516 }
4517
4518 /* At this point, if we're still interested in issuing messages,
4519 they must apply to classes surrounding the object declared. */
4520 if (msg)
4521 msg = G_("default argument for template parameter for class "
4522 "enclosing %qD");
4523 }
4524
4525 return no_errors;
4526 }
4527
4528 /* Worker for push_template_decl_real, called via
4529 for_each_template_parm. DATA is really an int, indicating the
4530 level of the parameters we are interested in. If T is a template
4531 parameter of that level, return nonzero. */
4532
4533 static int
4534 template_parm_this_level_p (tree t, void* data)
4535 {
4536 int this_level = *(int *)data;
4537 int level;
4538
4539 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4540 level = TEMPLATE_PARM_LEVEL (t);
4541 else
4542 level = TEMPLATE_TYPE_LEVEL (t);
4543 return level == this_level;
4544 }
4545
4546 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4547 parameters given by current_template_args, or reuses a
4548 previously existing one, if appropriate. Returns the DECL, or an
4549 equivalent one, if it is replaced via a call to duplicate_decls.
4550
4551 If IS_FRIEND is true, DECL is a friend declaration. */
4552
4553 tree
4554 push_template_decl_real (tree decl, bool is_friend)
4555 {
4556 tree tmpl;
4557 tree args;
4558 tree info;
4559 tree ctx;
4560 bool is_primary;
4561 bool is_partial;
4562 int new_template_p = 0;
4563 /* True if the template is a member template, in the sense of
4564 [temp.mem]. */
4565 bool member_template_p = false;
4566
4567 if (decl == error_mark_node || !current_template_parms)
4568 return error_mark_node;
4569
4570 /* See if this is a partial specialization. */
4571 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4572 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4573 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4574
4575 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4576 is_friend = true;
4577
4578 if (is_friend)
4579 /* For a friend, we want the context of the friend function, not
4580 the type of which it is a friend. */
4581 ctx = CP_DECL_CONTEXT (decl);
4582 else if (CP_DECL_CONTEXT (decl)
4583 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4584 /* In the case of a virtual function, we want the class in which
4585 it is defined. */
4586 ctx = CP_DECL_CONTEXT (decl);
4587 else
4588 /* Otherwise, if we're currently defining some class, the DECL
4589 is assumed to be a member of the class. */
4590 ctx = current_scope ();
4591
4592 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4593 ctx = NULL_TREE;
4594
4595 if (!DECL_CONTEXT (decl))
4596 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4597
4598 /* See if this is a primary template. */
4599 if (is_friend && ctx)
4600 /* A friend template that specifies a class context, i.e.
4601 template <typename T> friend void A<T>::f();
4602 is not primary. */
4603 is_primary = false;
4604 else
4605 is_primary = template_parm_scope_p ();
4606
4607 if (is_primary)
4608 {
4609 if (DECL_CLASS_SCOPE_P (decl))
4610 member_template_p = true;
4611 if (TREE_CODE (decl) == TYPE_DECL
4612 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4613 {
4614 error ("template class without a name");
4615 return error_mark_node;
4616 }
4617 else if (TREE_CODE (decl) == FUNCTION_DECL)
4618 {
4619 if (DECL_DESTRUCTOR_P (decl))
4620 {
4621 /* [temp.mem]
4622
4623 A destructor shall not be a member template. */
4624 error ("destructor %qD declared as member template", decl);
4625 return error_mark_node;
4626 }
4627 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4628 && (!prototype_p (TREE_TYPE (decl))
4629 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4630 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4631 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4632 == void_list_node)))
4633 {
4634 /* [basic.stc.dynamic.allocation]
4635
4636 An allocation function can be a function
4637 template. ... Template allocation functions shall
4638 have two or more parameters. */
4639 error ("invalid template declaration of %qD", decl);
4640 return error_mark_node;
4641 }
4642 }
4643 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4644 && CLASS_TYPE_P (TREE_TYPE (decl)))
4645 /* OK */;
4646 else if (TREE_CODE (decl) == TYPE_DECL
4647 && TYPE_DECL_ALIAS_P (decl))
4648 /* alias-declaration */
4649 gcc_assert (!DECL_ARTIFICIAL (decl));
4650 else
4651 {
4652 error ("template declaration of %q#D", decl);
4653 return error_mark_node;
4654 }
4655 }
4656
4657 /* Check to see that the rules regarding the use of default
4658 arguments are not being violated. */
4659 check_default_tmpl_args (decl, current_template_parms,
4660 is_primary, is_partial, /*is_friend_decl=*/0);
4661
4662 /* Ensure that there are no parameter packs in the type of this
4663 declaration that have not been expanded. */
4664 if (TREE_CODE (decl) == FUNCTION_DECL)
4665 {
4666 /* Check each of the arguments individually to see if there are
4667 any bare parameter packs. */
4668 tree type = TREE_TYPE (decl);
4669 tree arg = DECL_ARGUMENTS (decl);
4670 tree argtype = TYPE_ARG_TYPES (type);
4671
4672 while (arg && argtype)
4673 {
4674 if (!DECL_PACK_P (arg)
4675 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4676 {
4677 /* This is a PARM_DECL that contains unexpanded parameter
4678 packs. We have already complained about this in the
4679 check_for_bare_parameter_packs call, so just replace
4680 these types with ERROR_MARK_NODE. */
4681 TREE_TYPE (arg) = error_mark_node;
4682 TREE_VALUE (argtype) = error_mark_node;
4683 }
4684
4685 arg = DECL_CHAIN (arg);
4686 argtype = TREE_CHAIN (argtype);
4687 }
4688
4689 /* Check for bare parameter packs in the return type and the
4690 exception specifiers. */
4691 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4692 /* Errors were already issued, set return type to int
4693 as the frontend doesn't expect error_mark_node as
4694 the return type. */
4695 TREE_TYPE (type) = integer_type_node;
4696 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4697 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4698 }
4699 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4700 && TYPE_DECL_ALIAS_P (decl))
4701 ? DECL_ORIGINAL_TYPE (decl)
4702 : TREE_TYPE (decl)))
4703 {
4704 TREE_TYPE (decl) = error_mark_node;
4705 return error_mark_node;
4706 }
4707
4708 if (is_partial)
4709 return process_partial_specialization (decl);
4710
4711 args = current_template_args ();
4712
4713 if (!ctx
4714 || TREE_CODE (ctx) == FUNCTION_DECL
4715 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4716 || (TREE_CODE (decl) == TYPE_DECL
4717 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4718 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4719 {
4720 if (DECL_LANG_SPECIFIC (decl)
4721 && DECL_TEMPLATE_INFO (decl)
4722 && DECL_TI_TEMPLATE (decl))
4723 tmpl = DECL_TI_TEMPLATE (decl);
4724 /* If DECL is a TYPE_DECL for a class-template, then there won't
4725 be DECL_LANG_SPECIFIC. The information equivalent to
4726 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4727 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4728 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4729 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4730 {
4731 /* Since a template declaration already existed for this
4732 class-type, we must be redeclaring it here. Make sure
4733 that the redeclaration is valid. */
4734 redeclare_class_template (TREE_TYPE (decl),
4735 current_template_parms);
4736 /* We don't need to create a new TEMPLATE_DECL; just use the
4737 one we already had. */
4738 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4739 }
4740 else
4741 {
4742 tmpl = build_template_decl (decl, current_template_parms,
4743 member_template_p);
4744 new_template_p = 1;
4745
4746 if (DECL_LANG_SPECIFIC (decl)
4747 && DECL_TEMPLATE_SPECIALIZATION (decl))
4748 {
4749 /* A specialization of a member template of a template
4750 class. */
4751 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4752 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4753 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4754 }
4755 }
4756 }
4757 else
4758 {
4759 tree a, t, current, parms;
4760 int i;
4761 tree tinfo = get_template_info (decl);
4762
4763 if (!tinfo)
4764 {
4765 error ("template definition of non-template %q#D", decl);
4766 return error_mark_node;
4767 }
4768
4769 tmpl = TI_TEMPLATE (tinfo);
4770
4771 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4772 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4773 && DECL_TEMPLATE_SPECIALIZATION (decl)
4774 && DECL_MEMBER_TEMPLATE_P (tmpl))
4775 {
4776 tree new_tmpl;
4777
4778 /* The declaration is a specialization of a member
4779 template, declared outside the class. Therefore, the
4780 innermost template arguments will be NULL, so we
4781 replace them with the arguments determined by the
4782 earlier call to check_explicit_specialization. */
4783 args = DECL_TI_ARGS (decl);
4784
4785 new_tmpl
4786 = build_template_decl (decl, current_template_parms,
4787 member_template_p);
4788 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4789 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4790 DECL_TI_TEMPLATE (decl) = new_tmpl;
4791 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4792 DECL_TEMPLATE_INFO (new_tmpl)
4793 = build_template_info (tmpl, args);
4794
4795 register_specialization (new_tmpl,
4796 most_general_template (tmpl),
4797 args,
4798 is_friend, 0);
4799 return decl;
4800 }
4801
4802 /* Make sure the template headers we got make sense. */
4803
4804 parms = DECL_TEMPLATE_PARMS (tmpl);
4805 i = TMPL_PARMS_DEPTH (parms);
4806 if (TMPL_ARGS_DEPTH (args) != i)
4807 {
4808 error ("expected %d levels of template parms for %q#D, got %d",
4809 i, decl, TMPL_ARGS_DEPTH (args));
4810 }
4811 else
4812 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4813 {
4814 a = TMPL_ARGS_LEVEL (args, i);
4815 t = INNERMOST_TEMPLATE_PARMS (parms);
4816
4817 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4818 {
4819 if (current == decl)
4820 error ("got %d template parameters for %q#D",
4821 TREE_VEC_LENGTH (a), decl);
4822 else
4823 error ("got %d template parameters for %q#T",
4824 TREE_VEC_LENGTH (a), current);
4825 error (" but %d required", TREE_VEC_LENGTH (t));
4826 /* Avoid crash in import_export_decl. */
4827 DECL_INTERFACE_KNOWN (decl) = 1;
4828 return error_mark_node;
4829 }
4830
4831 if (current == decl)
4832 current = ctx;
4833 else if (current == NULL_TREE)
4834 /* Can happen in erroneous input. */
4835 break;
4836 else
4837 current = get_containing_scope (current);
4838 }
4839
4840 /* Check that the parms are used in the appropriate qualifying scopes
4841 in the declarator. */
4842 if (!comp_template_args
4843 (TI_ARGS (tinfo),
4844 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4845 {
4846 error ("\
4847 template arguments to %qD do not match original template %qD",
4848 decl, DECL_TEMPLATE_RESULT (tmpl));
4849 if (!uses_template_parms (TI_ARGS (tinfo)))
4850 inform (input_location, "use template<> for an explicit specialization");
4851 /* Avoid crash in import_export_decl. */
4852 DECL_INTERFACE_KNOWN (decl) = 1;
4853 return error_mark_node;
4854 }
4855 }
4856
4857 DECL_TEMPLATE_RESULT (tmpl) = decl;
4858 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4859
4860 /* Push template declarations for global functions and types. Note
4861 that we do not try to push a global template friend declared in a
4862 template class; such a thing may well depend on the template
4863 parameters of the class. */
4864 if (new_template_p && !ctx
4865 && !(is_friend && template_class_depth (current_class_type) > 0))
4866 {
4867 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4868 if (tmpl == error_mark_node)
4869 return error_mark_node;
4870
4871 /* Hide template friend classes that haven't been declared yet. */
4872 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4873 {
4874 DECL_ANTICIPATED (tmpl) = 1;
4875 DECL_FRIEND_P (tmpl) = 1;
4876 }
4877 }
4878
4879 if (is_primary)
4880 {
4881 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4882 int i;
4883
4884 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4885 if (DECL_CONV_FN_P (tmpl))
4886 {
4887 int depth = TMPL_PARMS_DEPTH (parms);
4888
4889 /* It is a conversion operator. See if the type converted to
4890 depends on innermost template operands. */
4891
4892 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4893 depth))
4894 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4895 }
4896
4897 /* Give template template parms a DECL_CONTEXT of the template
4898 for which they are a parameter. */
4899 parms = INNERMOST_TEMPLATE_PARMS (parms);
4900 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4901 {
4902 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4903 if (TREE_CODE (parm) == TEMPLATE_DECL)
4904 DECL_CONTEXT (parm) = tmpl;
4905 }
4906 }
4907
4908 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4909 back to its most general template. If TMPL is a specialization,
4910 ARGS may only have the innermost set of arguments. Add the missing
4911 argument levels if necessary. */
4912 if (DECL_TEMPLATE_INFO (tmpl))
4913 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4914
4915 info = build_template_info (tmpl, args);
4916
4917 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4918 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4919 else
4920 {
4921 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4922 retrofit_lang_decl (decl);
4923 if (DECL_LANG_SPECIFIC (decl))
4924 DECL_TEMPLATE_INFO (decl) = info;
4925 }
4926
4927 return DECL_TEMPLATE_RESULT (tmpl);
4928 }
4929
4930 tree
4931 push_template_decl (tree decl)
4932 {
4933 return push_template_decl_real (decl, false);
4934 }
4935
4936 /* FN is an inheriting constructor that inherits from the constructor
4937 template INHERITED; turn FN into a constructor template with a matching
4938 template header. */
4939
4940 tree
4941 add_inherited_template_parms (tree fn, tree inherited)
4942 {
4943 tree inner_parms
4944 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4945 inner_parms = copy_node (inner_parms);
4946 tree parms
4947 = tree_cons (size_int (processing_template_decl + 1),
4948 inner_parms, current_template_parms);
4949 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4950 tree args = template_parms_to_args (parms);
4951 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4952 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4953 DECL_TEMPLATE_RESULT (tmpl) = fn;
4954 DECL_ARTIFICIAL (tmpl) = true;
4955 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4956 return tmpl;
4957 }
4958
4959 /* Called when a class template TYPE is redeclared with the indicated
4960 template PARMS, e.g.:
4961
4962 template <class T> struct S;
4963 template <class T> struct S {}; */
4964
4965 bool
4966 redeclare_class_template (tree type, tree parms)
4967 {
4968 tree tmpl;
4969 tree tmpl_parms;
4970 int i;
4971
4972 if (!TYPE_TEMPLATE_INFO (type))
4973 {
4974 error ("%qT is not a template type", type);
4975 return false;
4976 }
4977
4978 tmpl = TYPE_TI_TEMPLATE (type);
4979 if (!PRIMARY_TEMPLATE_P (tmpl))
4980 /* The type is nested in some template class. Nothing to worry
4981 about here; there are no new template parameters for the nested
4982 type. */
4983 return true;
4984
4985 if (!parms)
4986 {
4987 error ("template specifiers not specified in declaration of %qD",
4988 tmpl);
4989 return false;
4990 }
4991
4992 parms = INNERMOST_TEMPLATE_PARMS (parms);
4993 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4994
4995 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4996 {
4997 error_n (input_location, TREE_VEC_LENGTH (parms),
4998 "redeclared with %d template parameter",
4999 "redeclared with %d template parameters",
5000 TREE_VEC_LENGTH (parms));
5001 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5002 "previous declaration %q+D used %d template parameter",
5003 "previous declaration %q+D used %d template parameters",
5004 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5005 return false;
5006 }
5007
5008 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5009 {
5010 tree tmpl_parm;
5011 tree parm;
5012 tree tmpl_default;
5013 tree parm_default;
5014
5015 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5016 || TREE_VEC_ELT (parms, i) == error_mark_node)
5017 continue;
5018
5019 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5020 if (tmpl_parm == error_mark_node)
5021 return false;
5022
5023 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5024 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5025 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5026
5027 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5028 TEMPLATE_DECL. */
5029 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5030 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5031 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5032 || (TREE_CODE (tmpl_parm) != PARM_DECL
5033 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5034 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5035 || (TREE_CODE (tmpl_parm) == PARM_DECL
5036 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5037 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5038 {
5039 error ("template parameter %q+#D", tmpl_parm);
5040 error ("redeclared here as %q#D", parm);
5041 return false;
5042 }
5043
5044 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5045 {
5046 /* We have in [temp.param]:
5047
5048 A template-parameter may not be given default arguments
5049 by two different declarations in the same scope. */
5050 error_at (input_location, "redefinition of default argument for %q#D", parm);
5051 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5052 "original definition appeared here");
5053 return false;
5054 }
5055
5056 if (parm_default != NULL_TREE)
5057 /* Update the previous template parameters (which are the ones
5058 that will really count) with the new default value. */
5059 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5060 else if (tmpl_default != NULL_TREE)
5061 /* Update the new parameters, too; they'll be used as the
5062 parameters for any members. */
5063 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5064 }
5065
5066 return true;
5067 }
5068
5069 /* Simplify EXPR if it is a non-dependent expression. Returns the
5070 (possibly simplified) expression. */
5071
5072 tree
5073 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5074 {
5075 if (expr == NULL_TREE)
5076 return NULL_TREE;
5077
5078 /* If we're in a template, but EXPR isn't value dependent, simplify
5079 it. We're supposed to treat:
5080
5081 template <typename T> void f(T[1 + 1]);
5082 template <typename T> void f(T[2]);
5083
5084 as two declarations of the same function, for example. */
5085 if (processing_template_decl
5086 && !instantiation_dependent_expression_p (expr)
5087 && potential_constant_expression (expr))
5088 {
5089 HOST_WIDE_INT saved_processing_template_decl;
5090
5091 saved_processing_template_decl = processing_template_decl;
5092 processing_template_decl = 0;
5093 expr = tsubst_copy_and_build (expr,
5094 /*args=*/NULL_TREE,
5095 complain,
5096 /*in_decl=*/NULL_TREE,
5097 /*function_p=*/false,
5098 /*integral_constant_expression_p=*/true);
5099 processing_template_decl = saved_processing_template_decl;
5100 }
5101 return expr;
5102 }
5103
5104 tree
5105 fold_non_dependent_expr (tree expr)
5106 {
5107 return fold_non_dependent_expr_sfinae (expr, tf_error);
5108 }
5109
5110 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5111 template declaration, or a TYPE_DECL for an alias declaration. */
5112
5113 bool
5114 alias_type_or_template_p (tree t)
5115 {
5116 if (t == NULL_TREE)
5117 return false;
5118 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5119 || (TYPE_P (t)
5120 && TYPE_NAME (t)
5121 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5122 || DECL_ALIAS_TEMPLATE_P (t));
5123 }
5124
5125 /* Return TRUE iff is a specialization of an alias template. */
5126
5127 bool
5128 alias_template_specialization_p (const_tree t)
5129 {
5130 if (t == NULL_TREE)
5131 return false;
5132
5133 return (TYPE_P (t)
5134 && TYPE_TEMPLATE_INFO (t)
5135 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5136 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5137 }
5138
5139 /* Return either TMPL or another template that it is equivalent to under DR
5140 1286: An alias that just changes the name of a template is equivalent to
5141 the other template. */
5142
5143 static tree
5144 get_underlying_template (tree tmpl)
5145 {
5146 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5147 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5148 {
5149 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5150 if (TYPE_TEMPLATE_INFO (result))
5151 {
5152 tree sub = TYPE_TI_TEMPLATE (result);
5153 if (PRIMARY_TEMPLATE_P (sub)
5154 && same_type_p (result, TREE_TYPE (sub)))
5155 {
5156 /* The alias type is equivalent to the pattern of the
5157 underlying template, so strip the alias. */
5158 tmpl = sub;
5159 continue;
5160 }
5161 }
5162 break;
5163 }
5164 return tmpl;
5165 }
5166
5167 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5168 must be a function or a pointer-to-function type, as specified
5169 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5170 and check that the resulting function has external linkage. */
5171
5172 static tree
5173 convert_nontype_argument_function (tree type, tree expr)
5174 {
5175 tree fns = expr;
5176 tree fn, fn_no_ptr;
5177 linkage_kind linkage;
5178
5179 fn = instantiate_type (type, fns, tf_none);
5180 if (fn == error_mark_node)
5181 return error_mark_node;
5182
5183 fn_no_ptr = fn;
5184 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5185 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5186 if (BASELINK_P (fn_no_ptr))
5187 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5188
5189 /* [temp.arg.nontype]/1
5190
5191 A template-argument for a non-type, non-template template-parameter
5192 shall be one of:
5193 [...]
5194 -- the address of an object or function with external [C++11: or
5195 internal] linkage. */
5196
5197 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5198 {
5199 error ("%qE is not a valid template argument for type %qT", expr, type);
5200 if (TYPE_PTR_P (type))
5201 error ("it must be the address of a function with external linkage");
5202 else
5203 error ("it must be the name of a function with external linkage");
5204 return NULL_TREE;
5205 }
5206
5207 linkage = decl_linkage (fn_no_ptr);
5208 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5209 {
5210 if (cxx_dialect >= cxx11)
5211 error ("%qE is not a valid template argument for type %qT "
5212 "because %qD has no linkage",
5213 expr, type, fn_no_ptr);
5214 else
5215 error ("%qE is not a valid template argument for type %qT "
5216 "because %qD does not have external linkage",
5217 expr, type, fn_no_ptr);
5218 return NULL_TREE;
5219 }
5220
5221 return fn;
5222 }
5223
5224 /* Subroutine of convert_nontype_argument.
5225 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5226 Emit an error otherwise. */
5227
5228 static bool
5229 check_valid_ptrmem_cst_expr (tree type, tree expr,
5230 tsubst_flags_t complain)
5231 {
5232 STRIP_NOPS (expr);
5233 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5234 return true;
5235 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5236 return true;
5237 if (complain & tf_error)
5238 {
5239 error ("%qE is not a valid template argument for type %qT",
5240 expr, type);
5241 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5242 }
5243 return false;
5244 }
5245
5246 /* Returns TRUE iff the address of OP is value-dependent.
5247
5248 14.6.2.4 [temp.dep.temp]:
5249 A non-integral non-type template-argument is dependent if its type is
5250 dependent or it has either of the following forms
5251 qualified-id
5252 & qualified-id
5253 and contains a nested-name-specifier which specifies a class-name that
5254 names a dependent type.
5255
5256 We generalize this to just say that the address of a member of a
5257 dependent class is value-dependent; the above doesn't cover the
5258 address of a static data member named with an unqualified-id. */
5259
5260 static bool
5261 has_value_dependent_address (tree op)
5262 {
5263 /* We could use get_inner_reference here, but there's no need;
5264 this is only relevant for template non-type arguments, which
5265 can only be expressed as &id-expression. */
5266 if (DECL_P (op))
5267 {
5268 tree ctx = CP_DECL_CONTEXT (op);
5269 if (TYPE_P (ctx) && dependent_type_p (ctx))
5270 return true;
5271 }
5272
5273 return false;
5274 }
5275
5276 /* The next set of functions are used for providing helpful explanatory
5277 diagnostics for failed overload resolution. Their messages should be
5278 indented by two spaces for consistency with the messages in
5279 call.c */
5280
5281 static int
5282 unify_success (bool /*explain_p*/)
5283 {
5284 return 0;
5285 }
5286
5287 static int
5288 unify_parameter_deduction_failure (bool explain_p, tree parm)
5289 {
5290 if (explain_p)
5291 inform (input_location,
5292 " couldn't deduce template parameter %qD", parm);
5293 return 1;
5294 }
5295
5296 static int
5297 unify_invalid (bool /*explain_p*/)
5298 {
5299 return 1;
5300 }
5301
5302 static int
5303 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5304 {
5305 if (explain_p)
5306 inform (input_location,
5307 " types %qT and %qT have incompatible cv-qualifiers",
5308 parm, arg);
5309 return 1;
5310 }
5311
5312 static int
5313 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5314 {
5315 if (explain_p)
5316 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5317 return 1;
5318 }
5319
5320 static int
5321 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5322 {
5323 if (explain_p)
5324 inform (input_location,
5325 " template parameter %qD is not a parameter pack, but "
5326 "argument %qD is",
5327 parm, arg);
5328 return 1;
5329 }
5330
5331 static int
5332 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5333 {
5334 if (explain_p)
5335 inform (input_location,
5336 " template argument %qE does not match "
5337 "pointer-to-member constant %qE",
5338 arg, parm);
5339 return 1;
5340 }
5341
5342 static int
5343 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5344 {
5345 if (explain_p)
5346 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5347 return 1;
5348 }
5349
5350 static int
5351 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5352 {
5353 if (explain_p)
5354 inform (input_location,
5355 " inconsistent parameter pack deduction with %qT and %qT",
5356 old_arg, new_arg);
5357 return 1;
5358 }
5359
5360 static int
5361 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5362 {
5363 if (explain_p)
5364 {
5365 if (TYPE_P (parm))
5366 inform (input_location,
5367 " deduced conflicting types for parameter %qT (%qT and %qT)",
5368 parm, first, second);
5369 else
5370 inform (input_location,
5371 " deduced conflicting values for non-type parameter "
5372 "%qE (%qE and %qE)", parm, first, second);
5373 }
5374 return 1;
5375 }
5376
5377 static int
5378 unify_vla_arg (bool explain_p, tree arg)
5379 {
5380 if (explain_p)
5381 inform (input_location,
5382 " variable-sized array type %qT is not "
5383 "a valid template argument",
5384 arg);
5385 return 1;
5386 }
5387
5388 static int
5389 unify_method_type_error (bool explain_p, tree arg)
5390 {
5391 if (explain_p)
5392 inform (input_location,
5393 " member function type %qT is not a valid template argument",
5394 arg);
5395 return 1;
5396 }
5397
5398 static int
5399 unify_arity (bool explain_p, int have, int wanted)
5400 {
5401 if (explain_p)
5402 inform_n (input_location, wanted,
5403 " candidate expects %d argument, %d provided",
5404 " candidate expects %d arguments, %d provided",
5405 wanted, have);
5406 return 1;
5407 }
5408
5409 static int
5410 unify_too_many_arguments (bool explain_p, int have, int wanted)
5411 {
5412 return unify_arity (explain_p, have, wanted);
5413 }
5414
5415 static int
5416 unify_too_few_arguments (bool explain_p, int have, int wanted)
5417 {
5418 return unify_arity (explain_p, have, wanted);
5419 }
5420
5421 static int
5422 unify_arg_conversion (bool explain_p, tree to_type,
5423 tree from_type, tree arg)
5424 {
5425 if (explain_p)
5426 inform (EXPR_LOC_OR_HERE (arg),
5427 " cannot convert %qE (type %qT) to type %qT",
5428 arg, from_type, to_type);
5429 return 1;
5430 }
5431
5432 static int
5433 unify_no_common_base (bool explain_p, enum template_base_result r,
5434 tree parm, tree arg)
5435 {
5436 if (explain_p)
5437 switch (r)
5438 {
5439 case tbr_ambiguous_baseclass:
5440 inform (input_location, " %qT is an ambiguous base class of %qT",
5441 parm, arg);
5442 break;
5443 default:
5444 inform (input_location, " %qT is not derived from %qT", arg, parm);
5445 break;
5446 }
5447 return 1;
5448 }
5449
5450 static int
5451 unify_inconsistent_template_template_parameters (bool explain_p)
5452 {
5453 if (explain_p)
5454 inform (input_location,
5455 " template parameters of a template template argument are "
5456 "inconsistent with other deduced template arguments");
5457 return 1;
5458 }
5459
5460 static int
5461 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5462 {
5463 if (explain_p)
5464 inform (input_location,
5465 " can't deduce a template for %qT from non-template type %qT",
5466 parm, arg);
5467 return 1;
5468 }
5469
5470 static int
5471 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5472 {
5473 if (explain_p)
5474 inform (input_location,
5475 " template argument %qE does not match %qD", arg, parm);
5476 return 1;
5477 }
5478
5479 static int
5480 unify_overload_resolution_failure (bool explain_p, tree arg)
5481 {
5482 if (explain_p)
5483 inform (input_location,
5484 " could not resolve address from overloaded function %qE",
5485 arg);
5486 return 1;
5487 }
5488
5489 /* Attempt to convert the non-type template parameter EXPR to the
5490 indicated TYPE. If the conversion is successful, return the
5491 converted value. If the conversion is unsuccessful, return
5492 NULL_TREE if we issued an error message, or error_mark_node if we
5493 did not. We issue error messages for out-and-out bad template
5494 parameters, but not simply because the conversion failed, since we
5495 might be just trying to do argument deduction. Both TYPE and EXPR
5496 must be non-dependent.
5497
5498 The conversion follows the special rules described in
5499 [temp.arg.nontype], and it is much more strict than an implicit
5500 conversion.
5501
5502 This function is called twice for each template argument (see
5503 lookup_template_class for a more accurate description of this
5504 problem). This means that we need to handle expressions which
5505 are not valid in a C++ source, but can be created from the
5506 first call (for instance, casts to perform conversions). These
5507 hacks can go away after we fix the double coercion problem. */
5508
5509 static tree
5510 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5511 {
5512 tree expr_type;
5513
5514 /* Detect immediately string literals as invalid non-type argument.
5515 This special-case is not needed for correctness (we would easily
5516 catch this later), but only to provide better diagnostic for this
5517 common user mistake. As suggested by DR 100, we do not mention
5518 linkage issues in the diagnostic as this is not the point. */
5519 /* FIXME we're making this OK. */
5520 if (TREE_CODE (expr) == STRING_CST)
5521 {
5522 if (complain & tf_error)
5523 error ("%qE is not a valid template argument for type %qT "
5524 "because string literals can never be used in this context",
5525 expr, type);
5526 return NULL_TREE;
5527 }
5528
5529 /* Add the ADDR_EXPR now for the benefit of
5530 value_dependent_expression_p. */
5531 if (TYPE_PTROBV_P (type)
5532 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5533 {
5534 expr = decay_conversion (expr, complain);
5535 if (expr == error_mark_node)
5536 return error_mark_node;
5537 }
5538
5539 /* If we are in a template, EXPR may be non-dependent, but still
5540 have a syntactic, rather than semantic, form. For example, EXPR
5541 might be a SCOPE_REF, rather than the VAR_DECL to which the
5542 SCOPE_REF refers. Preserving the qualifying scope is necessary
5543 so that access checking can be performed when the template is
5544 instantiated -- but here we need the resolved form so that we can
5545 convert the argument. */
5546 if (TYPE_REF_OBJ_P (type)
5547 && has_value_dependent_address (expr))
5548 /* If we want the address and it's value-dependent, don't fold. */;
5549 else if (!type_unknown_p (expr))
5550 expr = fold_non_dependent_expr_sfinae (expr, complain);
5551 if (error_operand_p (expr))
5552 return error_mark_node;
5553 expr_type = TREE_TYPE (expr);
5554 if (TREE_CODE (type) == REFERENCE_TYPE)
5555 expr = mark_lvalue_use (expr);
5556 else
5557 expr = mark_rvalue_use (expr);
5558
5559 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5560 to a non-type argument of "nullptr". */
5561 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5562 expr = convert (type, expr);
5563
5564 /* In C++11, integral or enumeration non-type template arguments can be
5565 arbitrary constant expressions. Pointer and pointer to
5566 member arguments can be general constant expressions that evaluate
5567 to a null value, but otherwise still need to be of a specific form. */
5568 if (cxx_dialect >= cxx11)
5569 {
5570 if (TREE_CODE (expr) == PTRMEM_CST)
5571 /* A PTRMEM_CST is already constant, and a valid template
5572 argument for a parameter of pointer to member type, we just want
5573 to leave it in that form rather than lower it to a
5574 CONSTRUCTOR. */;
5575 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5576 expr = maybe_constant_value (expr);
5577 else if (TYPE_PTR_OR_PTRMEM_P (type))
5578 {
5579 tree folded = maybe_constant_value (expr);
5580 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5581 : null_member_pointer_value_p (folded))
5582 expr = folded;
5583 }
5584 }
5585
5586 /* HACK: Due to double coercion, we can get a
5587 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5588 which is the tree that we built on the first call (see
5589 below when coercing to reference to object or to reference to
5590 function). We just strip everything and get to the arg.
5591 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5592 for examples. */
5593 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5594 {
5595 tree probe_type, probe = expr;
5596 if (REFERENCE_REF_P (probe))
5597 probe = TREE_OPERAND (probe, 0);
5598 probe_type = TREE_TYPE (probe);
5599 if (TREE_CODE (probe) == NOP_EXPR)
5600 {
5601 /* ??? Maybe we could use convert_from_reference here, but we
5602 would need to relax its constraints because the NOP_EXPR
5603 could actually change the type to something more cv-qualified,
5604 and this is not folded by convert_from_reference. */
5605 tree addr = TREE_OPERAND (probe, 0);
5606 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5607 && TREE_CODE (addr) == ADDR_EXPR
5608 && TYPE_PTR_P (TREE_TYPE (addr))
5609 && (same_type_ignoring_top_level_qualifiers_p
5610 (TREE_TYPE (probe_type),
5611 TREE_TYPE (TREE_TYPE (addr)))))
5612 {
5613 expr = TREE_OPERAND (addr, 0);
5614 expr_type = TREE_TYPE (probe_type);
5615 }
5616 }
5617 }
5618
5619 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5620 parameter is a pointer to object, through decay and
5621 qualification conversion. Let's strip everything. */
5622 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5623 {
5624 tree probe = expr;
5625 STRIP_NOPS (probe);
5626 if (TREE_CODE (probe) == ADDR_EXPR
5627 && TYPE_PTR_P (TREE_TYPE (probe)))
5628 {
5629 /* Skip the ADDR_EXPR only if it is part of the decay for
5630 an array. Otherwise, it is part of the original argument
5631 in the source code. */
5632 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5633 probe = TREE_OPERAND (probe, 0);
5634 expr = probe;
5635 expr_type = TREE_TYPE (expr);
5636 }
5637 }
5638
5639 /* [temp.arg.nontype]/5, bullet 1
5640
5641 For a non-type template-parameter of integral or enumeration type,
5642 integral promotions (_conv.prom_) and integral conversions
5643 (_conv.integral_) are applied. */
5644 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5645 {
5646 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5647 t = maybe_constant_value (t);
5648 if (t != error_mark_node)
5649 expr = t;
5650
5651 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5652 return error_mark_node;
5653
5654 /* Notice that there are constant expressions like '4 % 0' which
5655 do not fold into integer constants. */
5656 if (TREE_CODE (expr) != INTEGER_CST)
5657 {
5658 if (complain & tf_error)
5659 {
5660 int errs = errorcount, warns = warningcount + werrorcount;
5661 if (processing_template_decl
5662 && !require_potential_constant_expression (expr))
5663 return NULL_TREE;
5664 expr = cxx_constant_value (expr);
5665 if (errorcount > errs || warningcount + werrorcount > warns)
5666 inform (EXPR_LOC_OR_HERE (expr),
5667 "in template argument for type %qT ", type);
5668 if (expr == error_mark_node)
5669 return NULL_TREE;
5670 /* else cxx_constant_value complained but gave us
5671 a real constant, so go ahead. */
5672 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5673 }
5674 else
5675 return NULL_TREE;
5676 }
5677
5678 /* Avoid typedef problems. */
5679 if (TREE_TYPE (expr) != type)
5680 expr = fold_convert (type, expr);
5681 }
5682 /* [temp.arg.nontype]/5, bullet 2
5683
5684 For a non-type template-parameter of type pointer to object,
5685 qualification conversions (_conv.qual_) and the array-to-pointer
5686 conversion (_conv.array_) are applied. */
5687 else if (TYPE_PTROBV_P (type))
5688 {
5689 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5690
5691 A template-argument for a non-type, non-template template-parameter
5692 shall be one of: [...]
5693
5694 -- the name of a non-type template-parameter;
5695 -- the address of an object or function with external linkage, [...]
5696 expressed as "& id-expression" where the & is optional if the name
5697 refers to a function or array, or if the corresponding
5698 template-parameter is a reference.
5699
5700 Here, we do not care about functions, as they are invalid anyway
5701 for a parameter of type pointer-to-object. */
5702
5703 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5704 /* Non-type template parameters are OK. */
5705 ;
5706 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5707 /* Null pointer values are OK in C++11. */;
5708 else if (TREE_CODE (expr) != ADDR_EXPR
5709 && TREE_CODE (expr_type) != ARRAY_TYPE)
5710 {
5711 if (VAR_P (expr))
5712 {
5713 error ("%qD is not a valid template argument "
5714 "because %qD is a variable, not the address of "
5715 "a variable",
5716 expr, expr);
5717 return NULL_TREE;
5718 }
5719 if (POINTER_TYPE_P (expr_type))
5720 {
5721 error ("%qE is not a valid template argument for %qT "
5722 "because it is not the address of a variable",
5723 expr, type);
5724 return NULL_TREE;
5725 }
5726 /* Other values, like integer constants, might be valid
5727 non-type arguments of some other type. */
5728 return error_mark_node;
5729 }
5730 else
5731 {
5732 tree decl;
5733
5734 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5735 ? TREE_OPERAND (expr, 0) : expr);
5736 if (!VAR_P (decl))
5737 {
5738 error ("%qE is not a valid template argument of type %qT "
5739 "because %qE is not a variable",
5740 expr, type, decl);
5741 return NULL_TREE;
5742 }
5743 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5744 {
5745 error ("%qE is not a valid template argument of type %qT "
5746 "because %qD does not have external linkage",
5747 expr, type, decl);
5748 return NULL_TREE;
5749 }
5750 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5751 {
5752 error ("%qE is not a valid template argument of type %qT "
5753 "because %qD has no linkage",
5754 expr, type, decl);
5755 return NULL_TREE;
5756 }
5757 }
5758
5759 expr = decay_conversion (expr, complain);
5760 if (expr == error_mark_node)
5761 return error_mark_node;
5762
5763 expr = perform_qualification_conversions (type, expr);
5764 if (expr == error_mark_node)
5765 return error_mark_node;
5766 }
5767 /* [temp.arg.nontype]/5, bullet 3
5768
5769 For a non-type template-parameter of type reference to object, no
5770 conversions apply. The type referred to by the reference may be more
5771 cv-qualified than the (otherwise identical) type of the
5772 template-argument. The template-parameter is bound directly to the
5773 template-argument, which must be an lvalue. */
5774 else if (TYPE_REF_OBJ_P (type))
5775 {
5776 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5777 expr_type))
5778 return error_mark_node;
5779
5780 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5781 {
5782 error ("%qE is not a valid template argument for type %qT "
5783 "because of conflicts in cv-qualification", expr, type);
5784 return NULL_TREE;
5785 }
5786
5787 if (!real_lvalue_p (expr))
5788 {
5789 error ("%qE is not a valid template argument for type %qT "
5790 "because it is not an lvalue", expr, type);
5791 return NULL_TREE;
5792 }
5793
5794 /* [temp.arg.nontype]/1
5795
5796 A template-argument for a non-type, non-template template-parameter
5797 shall be one of: [...]
5798
5799 -- the address of an object or function with external linkage. */
5800 if (INDIRECT_REF_P (expr)
5801 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5802 {
5803 expr = TREE_OPERAND (expr, 0);
5804 if (DECL_P (expr))
5805 {
5806 error ("%q#D is not a valid template argument for type %qT "
5807 "because a reference variable does not have a constant "
5808 "address", expr, type);
5809 return NULL_TREE;
5810 }
5811 }
5812
5813 if (!DECL_P (expr))
5814 {
5815 error ("%qE is not a valid template argument for type %qT "
5816 "because it is not an object with external linkage",
5817 expr, type);
5818 return NULL_TREE;
5819 }
5820
5821 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5822 {
5823 error ("%qE is not a valid template argument for type %qT "
5824 "because object %qD has not external linkage",
5825 expr, type, expr);
5826 return NULL_TREE;
5827 }
5828
5829 expr = build_nop (type, build_address (expr));
5830 }
5831 /* [temp.arg.nontype]/5, bullet 4
5832
5833 For a non-type template-parameter of type pointer to function, only
5834 the function-to-pointer conversion (_conv.func_) is applied. If the
5835 template-argument represents a set of overloaded functions (or a
5836 pointer to such), the matching function is selected from the set
5837 (_over.over_). */
5838 else if (TYPE_PTRFN_P (type))
5839 {
5840 /* If the argument is a template-id, we might not have enough
5841 context information to decay the pointer. */
5842 if (!type_unknown_p (expr_type))
5843 {
5844 expr = decay_conversion (expr, complain);
5845 if (expr == error_mark_node)
5846 return error_mark_node;
5847 }
5848
5849 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5850 /* Null pointer values are OK in C++11. */
5851 return perform_qualification_conversions (type, expr);
5852
5853 expr = convert_nontype_argument_function (type, expr);
5854 if (!expr || expr == error_mark_node)
5855 return expr;
5856 }
5857 /* [temp.arg.nontype]/5, bullet 5
5858
5859 For a non-type template-parameter of type reference to function, no
5860 conversions apply. If the template-argument represents a set of
5861 overloaded functions, the matching function is selected from the set
5862 (_over.over_). */
5863 else if (TYPE_REFFN_P (type))
5864 {
5865 if (TREE_CODE (expr) == ADDR_EXPR)
5866 {
5867 error ("%qE is not a valid template argument for type %qT "
5868 "because it is a pointer", expr, type);
5869 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5870 return NULL_TREE;
5871 }
5872
5873 expr = convert_nontype_argument_function (type, expr);
5874 if (!expr || expr == error_mark_node)
5875 return expr;
5876
5877 expr = build_nop (type, build_address (expr));
5878 }
5879 /* [temp.arg.nontype]/5, bullet 6
5880
5881 For a non-type template-parameter of type pointer to member function,
5882 no conversions apply. If the template-argument represents a set of
5883 overloaded member functions, the matching member function is selected
5884 from the set (_over.over_). */
5885 else if (TYPE_PTRMEMFUNC_P (type))
5886 {
5887 expr = instantiate_type (type, expr, tf_none);
5888 if (expr == error_mark_node)
5889 return error_mark_node;
5890
5891 /* [temp.arg.nontype] bullet 1 says the pointer to member
5892 expression must be a pointer-to-member constant. */
5893 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5894 return error_mark_node;
5895
5896 /* There is no way to disable standard conversions in
5897 resolve_address_of_overloaded_function (called by
5898 instantiate_type). It is possible that the call succeeded by
5899 converting &B::I to &D::I (where B is a base of D), so we need
5900 to reject this conversion here.
5901
5902 Actually, even if there was a way to disable standard conversions,
5903 it would still be better to reject them here so that we can
5904 provide a superior diagnostic. */
5905 if (!same_type_p (TREE_TYPE (expr), type))
5906 {
5907 error ("%qE is not a valid template argument for type %qT "
5908 "because it is of type %qT", expr, type,
5909 TREE_TYPE (expr));
5910 /* If we are just one standard conversion off, explain. */
5911 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5912 inform (input_location,
5913 "standard conversions are not allowed in this context");
5914 return NULL_TREE;
5915 }
5916 }
5917 /* [temp.arg.nontype]/5, bullet 7
5918
5919 For a non-type template-parameter of type pointer to data member,
5920 qualification conversions (_conv.qual_) are applied. */
5921 else if (TYPE_PTRDATAMEM_P (type))
5922 {
5923 /* [temp.arg.nontype] bullet 1 says the pointer to member
5924 expression must be a pointer-to-member constant. */
5925 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5926 return error_mark_node;
5927
5928 expr = perform_qualification_conversions (type, expr);
5929 if (expr == error_mark_node)
5930 return expr;
5931 }
5932 else if (NULLPTR_TYPE_P (type))
5933 {
5934 if (expr != nullptr_node)
5935 {
5936 error ("%qE is not a valid template argument for type %qT "
5937 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5938 return NULL_TREE;
5939 }
5940 return expr;
5941 }
5942 /* A template non-type parameter must be one of the above. */
5943 else
5944 gcc_unreachable ();
5945
5946 /* Sanity check: did we actually convert the argument to the
5947 right type? */
5948 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5949 (type, TREE_TYPE (expr)));
5950 return expr;
5951 }
5952
5953 /* Subroutine of coerce_template_template_parms, which returns 1 if
5954 PARM_PARM and ARG_PARM match using the rule for the template
5955 parameters of template template parameters. Both PARM and ARG are
5956 template parameters; the rest of the arguments are the same as for
5957 coerce_template_template_parms.
5958 */
5959 static int
5960 coerce_template_template_parm (tree parm,
5961 tree arg,
5962 tsubst_flags_t complain,
5963 tree in_decl,
5964 tree outer_args)
5965 {
5966 if (arg == NULL_TREE || arg == error_mark_node
5967 || parm == NULL_TREE || parm == error_mark_node)
5968 return 0;
5969
5970 if (TREE_CODE (arg) != TREE_CODE (parm))
5971 return 0;
5972
5973 switch (TREE_CODE (parm))
5974 {
5975 case TEMPLATE_DECL:
5976 /* We encounter instantiations of templates like
5977 template <template <template <class> class> class TT>
5978 class C; */
5979 {
5980 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5981 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5982
5983 if (!coerce_template_template_parms
5984 (parmparm, argparm, complain, in_decl, outer_args))
5985 return 0;
5986 }
5987 /* Fall through. */
5988
5989 case TYPE_DECL:
5990 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5991 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5992 /* Argument is a parameter pack but parameter is not. */
5993 return 0;
5994 break;
5995
5996 case PARM_DECL:
5997 /* The tsubst call is used to handle cases such as
5998
5999 template <int> class C {};
6000 template <class T, template <T> class TT> class D {};
6001 D<int, C> d;
6002
6003 i.e. the parameter list of TT depends on earlier parameters. */
6004 if (!uses_template_parms (TREE_TYPE (arg))
6005 && !same_type_p
6006 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6007 TREE_TYPE (arg)))
6008 return 0;
6009
6010 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6011 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6012 /* Argument is a parameter pack but parameter is not. */
6013 return 0;
6014
6015 break;
6016
6017 default:
6018 gcc_unreachable ();
6019 }
6020
6021 return 1;
6022 }
6023
6024
6025 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6026 template template parameters. Both PARM_PARMS and ARG_PARMS are
6027 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6028 or PARM_DECL.
6029
6030 Consider the example:
6031 template <class T> class A;
6032 template<template <class U> class TT> class B;
6033
6034 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6035 the parameters to A, and OUTER_ARGS contains A. */
6036
6037 static int
6038 coerce_template_template_parms (tree parm_parms,
6039 tree arg_parms,
6040 tsubst_flags_t complain,
6041 tree in_decl,
6042 tree outer_args)
6043 {
6044 int nparms, nargs, i;
6045 tree parm, arg;
6046 int variadic_p = 0;
6047
6048 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6049 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6050
6051 nparms = TREE_VEC_LENGTH (parm_parms);
6052 nargs = TREE_VEC_LENGTH (arg_parms);
6053
6054 /* Determine whether we have a parameter pack at the end of the
6055 template template parameter's template parameter list. */
6056 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6057 {
6058 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6059
6060 if (parm == error_mark_node)
6061 return 0;
6062
6063 switch (TREE_CODE (parm))
6064 {
6065 case TEMPLATE_DECL:
6066 case TYPE_DECL:
6067 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6068 variadic_p = 1;
6069 break;
6070
6071 case PARM_DECL:
6072 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6073 variadic_p = 1;
6074 break;
6075
6076 default:
6077 gcc_unreachable ();
6078 }
6079 }
6080
6081 if (nargs != nparms
6082 && !(variadic_p && nargs >= nparms - 1))
6083 return 0;
6084
6085 /* Check all of the template parameters except the parameter pack at
6086 the end (if any). */
6087 for (i = 0; i < nparms - variadic_p; ++i)
6088 {
6089 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6090 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6091 continue;
6092
6093 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6094 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6095
6096 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6097 outer_args))
6098 return 0;
6099
6100 }
6101
6102 if (variadic_p)
6103 {
6104 /* Check each of the template parameters in the template
6105 argument against the template parameter pack at the end of
6106 the template template parameter. */
6107 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6108 return 0;
6109
6110 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6111
6112 for (; i < nargs; ++i)
6113 {
6114 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6115 continue;
6116
6117 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6118
6119 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6120 outer_args))
6121 return 0;
6122 }
6123 }
6124
6125 return 1;
6126 }
6127
6128 /* Verifies that the deduced template arguments (in TARGS) for the
6129 template template parameters (in TPARMS) represent valid bindings,
6130 by comparing the template parameter list of each template argument
6131 to the template parameter list of its corresponding template
6132 template parameter, in accordance with DR150. This
6133 routine can only be called after all template arguments have been
6134 deduced. It will return TRUE if all of the template template
6135 parameter bindings are okay, FALSE otherwise. */
6136 bool
6137 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6138 {
6139 int i, ntparms = TREE_VEC_LENGTH (tparms);
6140 bool ret = true;
6141
6142 /* We're dealing with template parms in this process. */
6143 ++processing_template_decl;
6144
6145 targs = INNERMOST_TEMPLATE_ARGS (targs);
6146
6147 for (i = 0; i < ntparms; ++i)
6148 {
6149 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6150 tree targ = TREE_VEC_ELT (targs, i);
6151
6152 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6153 {
6154 tree packed_args = NULL_TREE;
6155 int idx, len = 1;
6156
6157 if (ARGUMENT_PACK_P (targ))
6158 {
6159 /* Look inside the argument pack. */
6160 packed_args = ARGUMENT_PACK_ARGS (targ);
6161 len = TREE_VEC_LENGTH (packed_args);
6162 }
6163
6164 for (idx = 0; idx < len; ++idx)
6165 {
6166 tree targ_parms = NULL_TREE;
6167
6168 if (packed_args)
6169 /* Extract the next argument from the argument
6170 pack. */
6171 targ = TREE_VEC_ELT (packed_args, idx);
6172
6173 if (PACK_EXPANSION_P (targ))
6174 /* Look at the pattern of the pack expansion. */
6175 targ = PACK_EXPANSION_PATTERN (targ);
6176
6177 /* Extract the template parameters from the template
6178 argument. */
6179 if (TREE_CODE (targ) == TEMPLATE_DECL)
6180 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6181 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6182 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6183
6184 /* Verify that we can coerce the template template
6185 parameters from the template argument to the template
6186 parameter. This requires an exact match. */
6187 if (targ_parms
6188 && !coerce_template_template_parms
6189 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6190 targ_parms,
6191 tf_none,
6192 tparm,
6193 targs))
6194 {
6195 ret = false;
6196 goto out;
6197 }
6198 }
6199 }
6200 }
6201
6202 out:
6203
6204 --processing_template_decl;
6205 return ret;
6206 }
6207
6208 /* Since type attributes aren't mangled, we need to strip them from
6209 template type arguments. */
6210
6211 static tree
6212 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6213 {
6214 tree mv;
6215 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6216 return arg;
6217 mv = TYPE_MAIN_VARIANT (arg);
6218 arg = strip_typedefs (arg);
6219 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6220 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6221 {
6222 if (complain & tf_warning)
6223 warning (0, "ignoring attributes on template argument %qT", arg);
6224 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6225 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6226 }
6227 return arg;
6228 }
6229
6230 /* Convert the indicated template ARG as necessary to match the
6231 indicated template PARM. Returns the converted ARG, or
6232 error_mark_node if the conversion was unsuccessful. Error and
6233 warning messages are issued under control of COMPLAIN. This
6234 conversion is for the Ith parameter in the parameter list. ARGS is
6235 the full set of template arguments deduced so far. */
6236
6237 static tree
6238 convert_template_argument (tree parm,
6239 tree arg,
6240 tree args,
6241 tsubst_flags_t complain,
6242 int i,
6243 tree in_decl)
6244 {
6245 tree orig_arg;
6246 tree val;
6247 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6248
6249 if (TREE_CODE (arg) == TREE_LIST
6250 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6251 {
6252 /* The template argument was the name of some
6253 member function. That's usually
6254 invalid, but static members are OK. In any
6255 case, grab the underlying fields/functions
6256 and issue an error later if required. */
6257 orig_arg = TREE_VALUE (arg);
6258 TREE_TYPE (arg) = unknown_type_node;
6259 }
6260
6261 orig_arg = arg;
6262
6263 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6264 requires_type = (TREE_CODE (parm) == TYPE_DECL
6265 || requires_tmpl_type);
6266
6267 /* When determining whether an argument pack expansion is a template,
6268 look at the pattern. */
6269 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6270 arg = PACK_EXPANSION_PATTERN (arg);
6271
6272 /* Deal with an injected-class-name used as a template template arg. */
6273 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6274 {
6275 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6276 if (TREE_CODE (t) == TEMPLATE_DECL)
6277 {
6278 if (cxx_dialect >= cxx11)
6279 /* OK under DR 1004. */;
6280 else if (complain & tf_warning_or_error)
6281 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6282 " used as template template argument", TYPE_NAME (arg));
6283 else if (flag_pedantic_errors)
6284 t = arg;
6285
6286 arg = t;
6287 }
6288 }
6289
6290 is_tmpl_type =
6291 ((TREE_CODE (arg) == TEMPLATE_DECL
6292 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6293 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6294 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6295 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6296
6297 if (is_tmpl_type
6298 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6299 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6300 arg = TYPE_STUB_DECL (arg);
6301
6302 is_type = TYPE_P (arg) || is_tmpl_type;
6303
6304 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6305 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6306 {
6307 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6308 {
6309 if (complain & tf_error)
6310 error ("invalid use of destructor %qE as a type", orig_arg);
6311 return error_mark_node;
6312 }
6313
6314 permerror (input_location,
6315 "to refer to a type member of a template parameter, "
6316 "use %<typename %E%>", orig_arg);
6317
6318 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6319 TREE_OPERAND (arg, 1),
6320 typename_type,
6321 complain);
6322 arg = orig_arg;
6323 is_type = 1;
6324 }
6325 if (is_type != requires_type)
6326 {
6327 if (in_decl)
6328 {
6329 if (complain & tf_error)
6330 {
6331 error ("type/value mismatch at argument %d in template "
6332 "parameter list for %qD",
6333 i + 1, in_decl);
6334 if (is_type)
6335 error (" expected a constant of type %qT, got %qT",
6336 TREE_TYPE (parm),
6337 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6338 else if (requires_tmpl_type)
6339 error (" expected a class template, got %qE", orig_arg);
6340 else
6341 error (" expected a type, got %qE", orig_arg);
6342 }
6343 }
6344 return error_mark_node;
6345 }
6346 if (is_tmpl_type ^ requires_tmpl_type)
6347 {
6348 if (in_decl && (complain & tf_error))
6349 {
6350 error ("type/value mismatch at argument %d in template "
6351 "parameter list for %qD",
6352 i + 1, in_decl);
6353 if (is_tmpl_type)
6354 error (" expected a type, got %qT", DECL_NAME (arg));
6355 else
6356 error (" expected a class template, got %qT", orig_arg);
6357 }
6358 return error_mark_node;
6359 }
6360
6361 if (is_type)
6362 {
6363 if (requires_tmpl_type)
6364 {
6365 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6366 val = orig_arg;
6367 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6368 /* The number of argument required is not known yet.
6369 Just accept it for now. */
6370 val = TREE_TYPE (arg);
6371 else
6372 {
6373 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6374 tree argparm;
6375
6376 /* Strip alias templates that are equivalent to another
6377 template. */
6378 arg = get_underlying_template (arg);
6379 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6380
6381 if (coerce_template_template_parms (parmparm, argparm,
6382 complain, in_decl,
6383 args))
6384 {
6385 val = arg;
6386
6387 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6388 TEMPLATE_DECL. */
6389 if (val != error_mark_node)
6390 {
6391 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6392 val = TREE_TYPE (val);
6393 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6394 val = make_pack_expansion (val);
6395 }
6396 }
6397 else
6398 {
6399 if (in_decl && (complain & tf_error))
6400 {
6401 error ("type/value mismatch at argument %d in "
6402 "template parameter list for %qD",
6403 i + 1, in_decl);
6404 error (" expected a template of type %qD, got %qT",
6405 parm, orig_arg);
6406 }
6407
6408 val = error_mark_node;
6409 }
6410 }
6411 }
6412 else
6413 val = orig_arg;
6414 /* We only form one instance of each template specialization.
6415 Therefore, if we use a non-canonical variant (i.e., a
6416 typedef), any future messages referring to the type will use
6417 the typedef, which is confusing if those future uses do not
6418 themselves also use the typedef. */
6419 if (TYPE_P (val))
6420 val = canonicalize_type_argument (val, complain);
6421 }
6422 else
6423 {
6424 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6425
6426 if (invalid_nontype_parm_type_p (t, complain))
6427 return error_mark_node;
6428
6429 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6430 {
6431 if (same_type_p (t, TREE_TYPE (orig_arg)))
6432 val = orig_arg;
6433 else
6434 {
6435 /* Not sure if this is reachable, but it doesn't hurt
6436 to be robust. */
6437 error ("type mismatch in nontype parameter pack");
6438 val = error_mark_node;
6439 }
6440 }
6441 else if (!dependent_template_arg_p (orig_arg)
6442 && !uses_template_parms (t))
6443 /* We used to call digest_init here. However, digest_init
6444 will report errors, which we don't want when complain
6445 is zero. More importantly, digest_init will try too
6446 hard to convert things: for example, `0' should not be
6447 converted to pointer type at this point according to
6448 the standard. Accepting this is not merely an
6449 extension, since deciding whether or not these
6450 conversions can occur is part of determining which
6451 function template to call, or whether a given explicit
6452 argument specification is valid. */
6453 val = convert_nontype_argument (t, orig_arg, complain);
6454 else
6455 val = strip_typedefs_expr (orig_arg);
6456
6457 if (val == NULL_TREE)
6458 val = error_mark_node;
6459 else if (val == error_mark_node && (complain & tf_error))
6460 error ("could not convert template argument %qE to %qT", orig_arg, t);
6461
6462 if (TREE_CODE (val) == SCOPE_REF)
6463 {
6464 /* Strip typedefs from the SCOPE_REF. */
6465 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6466 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6467 complain);
6468 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6469 QUALIFIED_NAME_IS_TEMPLATE (val));
6470 }
6471 }
6472
6473 return val;
6474 }
6475
6476 /* Coerces the remaining template arguments in INNER_ARGS (from
6477 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6478 Returns the coerced argument pack. PARM_IDX is the position of this
6479 parameter in the template parameter list. ARGS is the original
6480 template argument list. */
6481 static tree
6482 coerce_template_parameter_pack (tree parms,
6483 int parm_idx,
6484 tree args,
6485 tree inner_args,
6486 int arg_idx,
6487 tree new_args,
6488 int* lost,
6489 tree in_decl,
6490 tsubst_flags_t complain)
6491 {
6492 tree parm = TREE_VEC_ELT (parms, parm_idx);
6493 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6494 tree packed_args;
6495 tree argument_pack;
6496 tree packed_types = NULL_TREE;
6497
6498 if (arg_idx > nargs)
6499 arg_idx = nargs;
6500
6501 packed_args = make_tree_vec (nargs - arg_idx);
6502
6503 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6504 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6505 {
6506 /* When the template parameter is a non-type template
6507 parameter pack whose type uses parameter packs, we need
6508 to look at each of the template arguments
6509 separately. Build a vector of the types for these
6510 non-type template parameters in PACKED_TYPES. */
6511 tree expansion
6512 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6513 packed_types = tsubst_pack_expansion (expansion, args,
6514 complain, in_decl);
6515
6516 if (packed_types == error_mark_node)
6517 return error_mark_node;
6518
6519 /* Check that we have the right number of arguments. */
6520 if (arg_idx < nargs
6521 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6522 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6523 {
6524 int needed_parms
6525 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6526 error ("wrong number of template arguments (%d, should be %d)",
6527 nargs, needed_parms);
6528 return error_mark_node;
6529 }
6530
6531 /* If we aren't able to check the actual arguments now
6532 (because they haven't been expanded yet), we can at least
6533 verify that all of the types used for the non-type
6534 template parameter pack are, in fact, valid for non-type
6535 template parameters. */
6536 if (arg_idx < nargs
6537 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6538 {
6539 int j, len = TREE_VEC_LENGTH (packed_types);
6540 for (j = 0; j < len; ++j)
6541 {
6542 tree t = TREE_VEC_ELT (packed_types, j);
6543 if (invalid_nontype_parm_type_p (t, complain))
6544 return error_mark_node;
6545 }
6546 }
6547 }
6548
6549 /* Convert the remaining arguments, which will be a part of the
6550 parameter pack "parm". */
6551 for (; arg_idx < nargs; ++arg_idx)
6552 {
6553 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6554 tree actual_parm = TREE_VALUE (parm);
6555
6556 if (packed_types && !PACK_EXPANSION_P (arg))
6557 {
6558 /* When we have a vector of types (corresponding to the
6559 non-type template parameter pack that uses parameter
6560 packs in its type, as mention above), and the
6561 argument is not an expansion (which expands to a
6562 currently unknown number of arguments), clone the
6563 parm and give it the next type in PACKED_TYPES. */
6564 actual_parm = copy_node (actual_parm);
6565 TREE_TYPE (actual_parm) =
6566 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6567 }
6568
6569 if (arg == error_mark_node)
6570 {
6571 if (complain & tf_error)
6572 error ("template argument %d is invalid", arg_idx + 1);
6573 }
6574 else
6575 arg = convert_template_argument (actual_parm,
6576 arg, new_args, complain, parm_idx,
6577 in_decl);
6578 if (arg == error_mark_node)
6579 (*lost)++;
6580 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6581 }
6582
6583 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6584 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6585 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6586 else
6587 {
6588 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6589 TREE_TYPE (argument_pack)
6590 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6591 TREE_CONSTANT (argument_pack) = 1;
6592 }
6593
6594 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6595 #ifdef ENABLE_CHECKING
6596 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6597 TREE_VEC_LENGTH (packed_args));
6598 #endif
6599 return argument_pack;
6600 }
6601
6602 /* Returns the number of pack expansions in the template argument vector
6603 ARGS. */
6604
6605 static int
6606 pack_expansion_args_count (tree args)
6607 {
6608 int i;
6609 int count = 0;
6610 if (args)
6611 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6612 {
6613 tree elt = TREE_VEC_ELT (args, i);
6614 if (elt && PACK_EXPANSION_P (elt))
6615 ++count;
6616 }
6617 return count;
6618 }
6619
6620 /* Convert all template arguments to their appropriate types, and
6621 return a vector containing the innermost resulting template
6622 arguments. If any error occurs, return error_mark_node. Error and
6623 warning messages are issued under control of COMPLAIN.
6624
6625 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6626 for arguments not specified in ARGS. Otherwise, if
6627 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6628 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6629 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6630 ARGS. */
6631
6632 static tree
6633 coerce_template_parms (tree parms,
6634 tree args,
6635 tree in_decl,
6636 tsubst_flags_t complain,
6637 bool require_all_args,
6638 bool use_default_args)
6639 {
6640 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6641 tree inner_args;
6642 tree new_args;
6643 tree new_inner_args;
6644 int saved_unevaluated_operand;
6645 int saved_inhibit_evaluation_warnings;
6646
6647 /* When used as a boolean value, indicates whether this is a
6648 variadic template parameter list. Since it's an int, we can also
6649 subtract it from nparms to get the number of non-variadic
6650 parameters. */
6651 int variadic_p = 0;
6652 int variadic_args_p = 0;
6653 int post_variadic_parms = 0;
6654
6655 if (args == error_mark_node)
6656 return error_mark_node;
6657
6658 nparms = TREE_VEC_LENGTH (parms);
6659
6660 /* Determine if there are any parameter packs. */
6661 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6662 {
6663 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6664 if (variadic_p)
6665 ++post_variadic_parms;
6666 if (template_parameter_pack_p (tparm))
6667 ++variadic_p;
6668 }
6669
6670 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6671 /* If there are no parameters that follow a parameter pack, we need to
6672 expand any argument packs so that we can deduce a parameter pack from
6673 some non-packed args followed by an argument pack, as in variadic85.C.
6674 If there are such parameters, we need to leave argument packs intact
6675 so the arguments are assigned properly. This can happen when dealing
6676 with a nested class inside a partial specialization of a class
6677 template, as in variadic92.C, or when deducing a template parameter pack
6678 from a sub-declarator, as in variadic114.C. */
6679 if (!post_variadic_parms)
6680 inner_args = expand_template_argument_pack (inner_args);
6681
6682 /* Count any pack expansion args. */
6683 variadic_args_p = pack_expansion_args_count (inner_args);
6684
6685 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6686 if ((nargs > nparms && !variadic_p)
6687 || (nargs < nparms - variadic_p
6688 && require_all_args
6689 && !variadic_args_p
6690 && (!use_default_args
6691 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6692 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6693 {
6694 if (complain & tf_error)
6695 {
6696 if (variadic_p)
6697 {
6698 nparms -= variadic_p;
6699 error ("wrong number of template arguments "
6700 "(%d, should be %d or more)", nargs, nparms);
6701 }
6702 else
6703 error ("wrong number of template arguments "
6704 "(%d, should be %d)", nargs, nparms);
6705
6706 if (in_decl)
6707 error ("provided for %q+D", in_decl);
6708 }
6709
6710 return error_mark_node;
6711 }
6712 /* We can't pass a pack expansion to a non-pack parameter of an alias
6713 template (DR 1430). */
6714 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6715 && variadic_args_p
6716 && nargs - variadic_args_p < nparms - variadic_p)
6717 {
6718 if (complain & tf_error)
6719 {
6720 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6721 {
6722 tree arg = TREE_VEC_ELT (inner_args, i);
6723 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6724
6725 if (PACK_EXPANSION_P (arg)
6726 && !template_parameter_pack_p (parm))
6727 {
6728 error ("pack expansion argument for non-pack parameter "
6729 "%qD of alias template %qD", parm, in_decl);
6730 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6731 goto found;
6732 }
6733 }
6734 gcc_unreachable ();
6735 found:;
6736 }
6737 return error_mark_node;
6738 }
6739
6740 /* We need to evaluate the template arguments, even though this
6741 template-id may be nested within a "sizeof". */
6742 saved_unevaluated_operand = cp_unevaluated_operand;
6743 cp_unevaluated_operand = 0;
6744 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6745 c_inhibit_evaluation_warnings = 0;
6746 new_inner_args = make_tree_vec (nparms);
6747 new_args = add_outermost_template_args (args, new_inner_args);
6748 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6749 {
6750 tree arg;
6751 tree parm;
6752
6753 /* Get the Ith template parameter. */
6754 parm = TREE_VEC_ELT (parms, parm_idx);
6755
6756 if (parm == error_mark_node)
6757 {
6758 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6759 continue;
6760 }
6761
6762 /* Calculate the next argument. */
6763 if (arg_idx < nargs)
6764 arg = TREE_VEC_ELT (inner_args, arg_idx);
6765 else
6766 arg = NULL_TREE;
6767
6768 if (template_parameter_pack_p (TREE_VALUE (parm))
6769 && !(arg && ARGUMENT_PACK_P (arg)))
6770 {
6771 /* All remaining arguments will be placed in the
6772 template parameter pack PARM. */
6773 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6774 inner_args, arg_idx,
6775 new_args, &lost,
6776 in_decl, complain);
6777
6778 /* Store this argument. */
6779 if (arg == error_mark_node)
6780 lost++;
6781 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6782
6783 /* We are done with all of the arguments. */
6784 arg_idx = nargs;
6785
6786 continue;
6787 }
6788 else if (arg)
6789 {
6790 if (PACK_EXPANSION_P (arg))
6791 {
6792 /* We don't know how many args we have yet, just
6793 use the unconverted ones for now. */
6794 new_inner_args = inner_args;
6795 break;
6796 }
6797 }
6798 else if (require_all_args)
6799 {
6800 /* There must be a default arg in this case. */
6801 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6802 complain, in_decl);
6803 /* The position of the first default template argument,
6804 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6805 Record that. */
6806 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6807 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6808 }
6809 else
6810 break;
6811
6812 if (arg == error_mark_node)
6813 {
6814 if (complain & tf_error)
6815 error ("template argument %d is invalid", arg_idx + 1);
6816 }
6817 else if (!arg)
6818 /* This only occurs if there was an error in the template
6819 parameter list itself (which we would already have
6820 reported) that we are trying to recover from, e.g., a class
6821 template with a parameter list such as
6822 template<typename..., typename>. */
6823 ++lost;
6824 else
6825 arg = convert_template_argument (TREE_VALUE (parm),
6826 arg, new_args, complain,
6827 parm_idx, in_decl);
6828
6829 if (arg == error_mark_node)
6830 lost++;
6831 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6832 }
6833 cp_unevaluated_operand = saved_unevaluated_operand;
6834 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6835
6836 if (lost)
6837 return error_mark_node;
6838
6839 #ifdef ENABLE_CHECKING
6840 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6842 TREE_VEC_LENGTH (new_inner_args));
6843 #endif
6844
6845 return new_inner_args;
6846 }
6847
6848 /* Like coerce_template_parms. If PARMS represents all template
6849 parameters levels, this function returns a vector of vectors
6850 representing all the resulting argument levels. Note that in this
6851 case, only the innermost arguments are coerced because the
6852 outermost ones are supposed to have been coerced already.
6853
6854 Otherwise, if PARMS represents only (the innermost) vector of
6855 parameters, this function returns a vector containing just the
6856 innermost resulting arguments. */
6857
6858 static tree
6859 coerce_innermost_template_parms (tree parms,
6860 tree args,
6861 tree in_decl,
6862 tsubst_flags_t complain,
6863 bool require_all_args,
6864 bool use_default_args)
6865 {
6866 int parms_depth = TMPL_PARMS_DEPTH (parms);
6867 int args_depth = TMPL_ARGS_DEPTH (args);
6868 tree coerced_args;
6869
6870 if (parms_depth > 1)
6871 {
6872 coerced_args = make_tree_vec (parms_depth);
6873 tree level;
6874 int cur_depth;
6875
6876 for (level = parms, cur_depth = parms_depth;
6877 parms_depth > 0 && level != NULL_TREE;
6878 level = TREE_CHAIN (level), --cur_depth)
6879 {
6880 tree l;
6881 if (cur_depth == args_depth)
6882 l = coerce_template_parms (TREE_VALUE (level),
6883 args, in_decl, complain,
6884 require_all_args,
6885 use_default_args);
6886 else
6887 l = TMPL_ARGS_LEVEL (args, cur_depth);
6888
6889 if (l == error_mark_node)
6890 return error_mark_node;
6891
6892 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6893 }
6894 }
6895 else
6896 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6897 args, in_decl, complain,
6898 require_all_args,
6899 use_default_args);
6900 return coerced_args;
6901 }
6902
6903 /* Returns 1 if template args OT and NT are equivalent. */
6904
6905 static int
6906 template_args_equal (tree ot, tree nt)
6907 {
6908 if (nt == ot)
6909 return 1;
6910 if (nt == NULL_TREE || ot == NULL_TREE)
6911 return false;
6912
6913 if (TREE_CODE (nt) == TREE_VEC)
6914 /* For member templates */
6915 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6916 else if (PACK_EXPANSION_P (ot))
6917 return (PACK_EXPANSION_P (nt)
6918 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6919 PACK_EXPANSION_PATTERN (nt))
6920 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6921 PACK_EXPANSION_EXTRA_ARGS (nt)));
6922 else if (ARGUMENT_PACK_P (ot))
6923 {
6924 int i, len;
6925 tree opack, npack;
6926
6927 if (!ARGUMENT_PACK_P (nt))
6928 return 0;
6929
6930 opack = ARGUMENT_PACK_ARGS (ot);
6931 npack = ARGUMENT_PACK_ARGS (nt);
6932 len = TREE_VEC_LENGTH (opack);
6933 if (TREE_VEC_LENGTH (npack) != len)
6934 return 0;
6935 for (i = 0; i < len; ++i)
6936 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6937 TREE_VEC_ELT (npack, i)))
6938 return 0;
6939 return 1;
6940 }
6941 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6942 {
6943 /* We get here probably because we are in the middle of substituting
6944 into the pattern of a pack expansion. In that case the
6945 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6946 interested in. So we want to use the initial pack argument for
6947 the comparison. */
6948 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6949 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6950 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6951 return template_args_equal (ot, nt);
6952 }
6953 else if (TYPE_P (nt))
6954 return TYPE_P (ot) && same_type_p (ot, nt);
6955 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6956 return 0;
6957 else
6958 return cp_tree_equal (ot, nt);
6959 }
6960
6961 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6962 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6963 NEWARG_PTR with the offending arguments if they are non-NULL. */
6964
6965 static int
6966 comp_template_args_with_info (tree oldargs, tree newargs,
6967 tree *oldarg_ptr, tree *newarg_ptr)
6968 {
6969 int i;
6970
6971 if (oldargs == newargs)
6972 return 1;
6973
6974 if (!oldargs || !newargs)
6975 return 0;
6976
6977 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6978 return 0;
6979
6980 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6981 {
6982 tree nt = TREE_VEC_ELT (newargs, i);
6983 tree ot = TREE_VEC_ELT (oldargs, i);
6984
6985 if (! template_args_equal (ot, nt))
6986 {
6987 if (oldarg_ptr != NULL)
6988 *oldarg_ptr = ot;
6989 if (newarg_ptr != NULL)
6990 *newarg_ptr = nt;
6991 return 0;
6992 }
6993 }
6994 return 1;
6995 }
6996
6997 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6998 of template arguments. Returns 0 otherwise. */
6999
7000 int
7001 comp_template_args (tree oldargs, tree newargs)
7002 {
7003 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7004 }
7005
7006 static void
7007 add_pending_template (tree d)
7008 {
7009 tree ti = (TYPE_P (d)
7010 ? CLASSTYPE_TEMPLATE_INFO (d)
7011 : DECL_TEMPLATE_INFO (d));
7012 struct pending_template *pt;
7013 int level;
7014
7015 if (TI_PENDING_TEMPLATE_FLAG (ti))
7016 return;
7017
7018 /* We are called both from instantiate_decl, where we've already had a
7019 tinst_level pushed, and instantiate_template, where we haven't.
7020 Compensate. */
7021 level = !current_tinst_level || current_tinst_level->decl != d;
7022
7023 if (level)
7024 push_tinst_level (d);
7025
7026 pt = ggc_alloc_pending_template ();
7027 pt->next = NULL;
7028 pt->tinst = current_tinst_level;
7029 if (last_pending_template)
7030 last_pending_template->next = pt;
7031 else
7032 pending_templates = pt;
7033
7034 last_pending_template = pt;
7035
7036 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7037
7038 if (level)
7039 pop_tinst_level ();
7040 }
7041
7042
7043 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7044 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7045 documentation for TEMPLATE_ID_EXPR. */
7046
7047 tree
7048 lookup_template_function (tree fns, tree arglist)
7049 {
7050 tree type;
7051
7052 if (fns == error_mark_node || arglist == error_mark_node)
7053 return error_mark_node;
7054
7055 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7056
7057 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7058 {
7059 error ("%q#D is not a function template", fns);
7060 return error_mark_node;
7061 }
7062
7063 if (BASELINK_P (fns))
7064 {
7065 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7066 unknown_type_node,
7067 BASELINK_FUNCTIONS (fns),
7068 arglist);
7069 return fns;
7070 }
7071
7072 type = TREE_TYPE (fns);
7073 if (TREE_CODE (fns) == OVERLOAD || !type)
7074 type = unknown_type_node;
7075
7076 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7077 }
7078
7079 /* Within the scope of a template class S<T>, the name S gets bound
7080 (in build_self_reference) to a TYPE_DECL for the class, not a
7081 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7082 or one of its enclosing classes, and that type is a template,
7083 return the associated TEMPLATE_DECL. Otherwise, the original
7084 DECL is returned.
7085
7086 Also handle the case when DECL is a TREE_LIST of ambiguous
7087 injected-class-names from different bases. */
7088
7089 tree
7090 maybe_get_template_decl_from_type_decl (tree decl)
7091 {
7092 if (decl == NULL_TREE)
7093 return decl;
7094
7095 /* DR 176: A lookup that finds an injected-class-name (10.2
7096 [class.member.lookup]) can result in an ambiguity in certain cases
7097 (for example, if it is found in more than one base class). If all of
7098 the injected-class-names that are found refer to specializations of
7099 the same class template, and if the name is followed by a
7100 template-argument-list, the reference refers to the class template
7101 itself and not a specialization thereof, and is not ambiguous. */
7102 if (TREE_CODE (decl) == TREE_LIST)
7103 {
7104 tree t, tmpl = NULL_TREE;
7105 for (t = decl; t; t = TREE_CHAIN (t))
7106 {
7107 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7108 if (!tmpl)
7109 tmpl = elt;
7110 else if (tmpl != elt)
7111 break;
7112 }
7113 if (tmpl && t == NULL_TREE)
7114 return tmpl;
7115 else
7116 return decl;
7117 }
7118
7119 return (decl != NULL_TREE
7120 && DECL_SELF_REFERENCE_P (decl)
7121 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7122 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7123 }
7124
7125 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7126 parameters, find the desired type.
7127
7128 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7129
7130 IN_DECL, if non-NULL, is the template declaration we are trying to
7131 instantiate.
7132
7133 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7134 the class we are looking up.
7135
7136 Issue error and warning messages under control of COMPLAIN.
7137
7138 If the template class is really a local class in a template
7139 function, then the FUNCTION_CONTEXT is the function in which it is
7140 being instantiated.
7141
7142 ??? Note that this function is currently called *twice* for each
7143 template-id: the first time from the parser, while creating the
7144 incomplete type (finish_template_type), and the second type during the
7145 real instantiation (instantiate_template_class). This is surely something
7146 that we want to avoid. It also causes some problems with argument
7147 coercion (see convert_nontype_argument for more information on this). */
7148
7149 static tree
7150 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7151 int entering_scope, tsubst_flags_t complain)
7152 {
7153 tree templ = NULL_TREE, parmlist;
7154 tree t;
7155 void **slot;
7156 spec_entry *entry;
7157 spec_entry elt;
7158 hashval_t hash;
7159
7160 if (identifier_p (d1))
7161 {
7162 tree value = innermost_non_namespace_value (d1);
7163 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7164 templ = value;
7165 else
7166 {
7167 if (context)
7168 push_decl_namespace (context);
7169 templ = lookup_name (d1);
7170 templ = maybe_get_template_decl_from_type_decl (templ);
7171 if (context)
7172 pop_decl_namespace ();
7173 }
7174 if (templ)
7175 context = DECL_CONTEXT (templ);
7176 }
7177 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7178 {
7179 tree type = TREE_TYPE (d1);
7180
7181 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7182 an implicit typename for the second A. Deal with it. */
7183 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7184 type = TREE_TYPE (type);
7185
7186 if (CLASSTYPE_TEMPLATE_INFO (type))
7187 {
7188 templ = CLASSTYPE_TI_TEMPLATE (type);
7189 d1 = DECL_NAME (templ);
7190 }
7191 }
7192 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7193 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7194 {
7195 templ = TYPE_TI_TEMPLATE (d1);
7196 d1 = DECL_NAME (templ);
7197 }
7198 else if (TREE_CODE (d1) == TEMPLATE_DECL
7199 && DECL_TEMPLATE_RESULT (d1)
7200 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7201 {
7202 templ = d1;
7203 d1 = DECL_NAME (templ);
7204 context = DECL_CONTEXT (templ);
7205 }
7206 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7207 {
7208 templ = d1;
7209 d1 = DECL_NAME (templ);
7210 }
7211
7212 /* Issue an error message if we didn't find a template. */
7213 if (! templ)
7214 {
7215 if (complain & tf_error)
7216 error ("%qT is not a template", d1);
7217 return error_mark_node;
7218 }
7219
7220 if (TREE_CODE (templ) != TEMPLATE_DECL
7221 /* Make sure it's a user visible template, if it was named by
7222 the user. */
7223 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7224 && !PRIMARY_TEMPLATE_P (templ)))
7225 {
7226 if (complain & tf_error)
7227 {
7228 error ("non-template type %qT used as a template", d1);
7229 if (in_decl)
7230 error ("for template declaration %q+D", in_decl);
7231 }
7232 return error_mark_node;
7233 }
7234
7235 complain &= ~tf_user;
7236
7237 /* An alias that just changes the name of a template is equivalent to the
7238 other template, so if any of the arguments are pack expansions, strip
7239 the alias to avoid problems with a pack expansion passed to a non-pack
7240 alias template parameter (DR 1430). */
7241 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7242 templ = get_underlying_template (templ);
7243
7244 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7245 {
7246 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7247 template arguments */
7248
7249 tree parm;
7250 tree arglist2;
7251 tree outer;
7252
7253 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7254
7255 /* Consider an example where a template template parameter declared as
7256
7257 template <class T, class U = std::allocator<T> > class TT
7258
7259 The template parameter level of T and U are one level larger than
7260 of TT. To proper process the default argument of U, say when an
7261 instantiation `TT<int>' is seen, we need to build the full
7262 arguments containing {int} as the innermost level. Outer levels,
7263 available when not appearing as default template argument, can be
7264 obtained from the arguments of the enclosing template.
7265
7266 Suppose that TT is later substituted with std::vector. The above
7267 instantiation is `TT<int, std::allocator<T> >' with TT at
7268 level 1, and T at level 2, while the template arguments at level 1
7269 becomes {std::vector} and the inner level 2 is {int}. */
7270
7271 outer = DECL_CONTEXT (templ);
7272 if (outer)
7273 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7274 else if (current_template_parms)
7275 /* This is an argument of the current template, so we haven't set
7276 DECL_CONTEXT yet. */
7277 outer = current_template_args ();
7278
7279 if (outer)
7280 arglist = add_to_template_args (outer, arglist);
7281
7282 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7283 complain,
7284 /*require_all_args=*/true,
7285 /*use_default_args=*/true);
7286 if (arglist2 == error_mark_node
7287 || (!uses_template_parms (arglist2)
7288 && check_instantiated_args (templ, arglist2, complain)))
7289 return error_mark_node;
7290
7291 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7292 return parm;
7293 }
7294 else
7295 {
7296 tree template_type = TREE_TYPE (templ);
7297 tree gen_tmpl;
7298 tree type_decl;
7299 tree found = NULL_TREE;
7300 int arg_depth;
7301 int parm_depth;
7302 int is_dependent_type;
7303 int use_partial_inst_tmpl = false;
7304
7305 if (template_type == error_mark_node)
7306 /* An error occurred while building the template TEMPL, and a
7307 diagnostic has most certainly been emitted for that
7308 already. Let's propagate that error. */
7309 return error_mark_node;
7310
7311 gen_tmpl = most_general_template (templ);
7312 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7313 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7314 arg_depth = TMPL_ARGS_DEPTH (arglist);
7315
7316 if (arg_depth == 1 && parm_depth > 1)
7317 {
7318 /* We've been given an incomplete set of template arguments.
7319 For example, given:
7320
7321 template <class T> struct S1 {
7322 template <class U> struct S2 {};
7323 template <class U> struct S2<U*> {};
7324 };
7325
7326 we will be called with an ARGLIST of `U*', but the
7327 TEMPLATE will be `template <class T> template
7328 <class U> struct S1<T>::S2'. We must fill in the missing
7329 arguments. */
7330 arglist
7331 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7332 arglist);
7333 arg_depth = TMPL_ARGS_DEPTH (arglist);
7334 }
7335
7336 /* Now we should have enough arguments. */
7337 gcc_assert (parm_depth == arg_depth);
7338
7339 /* From here on, we're only interested in the most general
7340 template. */
7341
7342 /* Calculate the BOUND_ARGS. These will be the args that are
7343 actually tsubst'd into the definition to create the
7344 instantiation. */
7345 if (parm_depth > 1)
7346 {
7347 /* We have multiple levels of arguments to coerce, at once. */
7348 int i;
7349 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7350
7351 tree bound_args = make_tree_vec (parm_depth);
7352
7353 for (i = saved_depth,
7354 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7355 i > 0 && t != NULL_TREE;
7356 --i, t = TREE_CHAIN (t))
7357 {
7358 tree a;
7359 if (i == saved_depth)
7360 a = coerce_template_parms (TREE_VALUE (t),
7361 arglist, gen_tmpl,
7362 complain,
7363 /*require_all_args=*/true,
7364 /*use_default_args=*/true);
7365 else
7366 /* Outer levels should have already been coerced. */
7367 a = TMPL_ARGS_LEVEL (arglist, i);
7368
7369 /* Don't process further if one of the levels fails. */
7370 if (a == error_mark_node)
7371 {
7372 /* Restore the ARGLIST to its full size. */
7373 TREE_VEC_LENGTH (arglist) = saved_depth;
7374 return error_mark_node;
7375 }
7376
7377 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7378
7379 /* We temporarily reduce the length of the ARGLIST so
7380 that coerce_template_parms will see only the arguments
7381 corresponding to the template parameters it is
7382 examining. */
7383 TREE_VEC_LENGTH (arglist)--;
7384 }
7385
7386 /* Restore the ARGLIST to its full size. */
7387 TREE_VEC_LENGTH (arglist) = saved_depth;
7388
7389 arglist = bound_args;
7390 }
7391 else
7392 arglist
7393 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7394 INNERMOST_TEMPLATE_ARGS (arglist),
7395 gen_tmpl,
7396 complain,
7397 /*require_all_args=*/true,
7398 /*use_default_args=*/true);
7399
7400 if (arglist == error_mark_node)
7401 /* We were unable to bind the arguments. */
7402 return error_mark_node;
7403
7404 /* In the scope of a template class, explicit references to the
7405 template class refer to the type of the template, not any
7406 instantiation of it. For example, in:
7407
7408 template <class T> class C { void f(C<T>); }
7409
7410 the `C<T>' is just the same as `C'. Outside of the
7411 class, however, such a reference is an instantiation. */
7412 if ((entering_scope
7413 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7414 || currently_open_class (template_type))
7415 /* comp_template_args is expensive, check it last. */
7416 && comp_template_args (TYPE_TI_ARGS (template_type),
7417 arglist))
7418 return template_type;
7419
7420 /* If we already have this specialization, return it. */
7421 elt.tmpl = gen_tmpl;
7422 elt.args = arglist;
7423 hash = hash_specialization (&elt);
7424 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7425 &elt, hash);
7426
7427 if (entry)
7428 return entry->spec;
7429
7430 is_dependent_type = uses_template_parms (arglist);
7431
7432 /* If the deduced arguments are invalid, then the binding
7433 failed. */
7434 if (!is_dependent_type
7435 && check_instantiated_args (gen_tmpl,
7436 INNERMOST_TEMPLATE_ARGS (arglist),
7437 complain))
7438 return error_mark_node;
7439
7440 if (!is_dependent_type
7441 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7442 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7443 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7444 {
7445 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7446 DECL_NAME (gen_tmpl),
7447 /*tag_scope=*/ts_global);
7448 return found;
7449 }
7450
7451 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7452 complain, in_decl);
7453 if (context == error_mark_node)
7454 return error_mark_node;
7455
7456 if (!context)
7457 context = global_namespace;
7458
7459 /* Create the type. */
7460 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7461 {
7462 if (!is_dependent_type)
7463 {
7464 set_current_access_from_decl (TYPE_NAME (template_type));
7465 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7466 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7467 arglist, complain, in_decl),
7468 SCOPED_ENUM_P (template_type), NULL);
7469 }
7470 else
7471 {
7472 /* We don't want to call start_enum for this type, since
7473 the values for the enumeration constants may involve
7474 template parameters. And, no one should be interested
7475 in the enumeration constants for such a type. */
7476 t = cxx_make_type (ENUMERAL_TYPE);
7477 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7478 }
7479 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7480 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7481 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7482 }
7483 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7484 {
7485 /* The user referred to a specialization of an alias
7486 template represented by GEN_TMPL.
7487
7488 [temp.alias]/2 says:
7489
7490 When a template-id refers to the specialization of an
7491 alias template, it is equivalent to the associated
7492 type obtained by substitution of its
7493 template-arguments for the template-parameters in the
7494 type-id of the alias template. */
7495
7496 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7497 /* Note that the call above (by indirectly calling
7498 register_specialization in tsubst_decl) registers the
7499 TYPE_DECL representing the specialization of the alias
7500 template. So next time someone substitutes ARGLIST for
7501 the template parms into the alias template (GEN_TMPL),
7502 she'll get that TYPE_DECL back. */
7503
7504 if (t == error_mark_node)
7505 return t;
7506 }
7507 else if (CLASS_TYPE_P (template_type))
7508 {
7509 t = make_class_type (TREE_CODE (template_type));
7510 CLASSTYPE_DECLARED_CLASS (t)
7511 = CLASSTYPE_DECLARED_CLASS (template_type);
7512 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7513 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7514
7515 /* A local class. Make sure the decl gets registered properly. */
7516 if (context == current_function_decl)
7517 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7518
7519 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7520 /* This instantiation is another name for the primary
7521 template type. Set the TYPE_CANONICAL field
7522 appropriately. */
7523 TYPE_CANONICAL (t) = template_type;
7524 else if (any_template_arguments_need_structural_equality_p (arglist))
7525 /* Some of the template arguments require structural
7526 equality testing, so this template class requires
7527 structural equality testing. */
7528 SET_TYPE_STRUCTURAL_EQUALITY (t);
7529 }
7530 else
7531 gcc_unreachable ();
7532
7533 /* If we called start_enum or pushtag above, this information
7534 will already be set up. */
7535 if (!TYPE_NAME (t))
7536 {
7537 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7538
7539 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7540 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7541 DECL_SOURCE_LOCATION (type_decl)
7542 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7543 }
7544 else
7545 type_decl = TYPE_NAME (t);
7546
7547 if (CLASS_TYPE_P (template_type))
7548 {
7549 TREE_PRIVATE (type_decl)
7550 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7551 TREE_PROTECTED (type_decl)
7552 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7553 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7554 {
7555 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7556 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7557 }
7558 }
7559
7560 /* Let's consider the explicit specialization of a member
7561 of a class template specialization that is implicitely instantiated,
7562 e.g.:
7563 template<class T>
7564 struct S
7565 {
7566 template<class U> struct M {}; //#0
7567 };
7568
7569 template<>
7570 template<>
7571 struct S<int>::M<char> //#1
7572 {
7573 int i;
7574 };
7575 [temp.expl.spec]/4 says this is valid.
7576
7577 In this case, when we write:
7578 S<int>::M<char> m;
7579
7580 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7581 the one of #0.
7582
7583 When we encounter #1, we want to store the partial instantiation
7584 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7585
7586 For all cases other than this "explicit specialization of member of a
7587 class template", we just want to store the most general template into
7588 the CLASSTYPE_TI_TEMPLATE of M.
7589
7590 This case of "explicit specialization of member of a class template"
7591 only happens when:
7592 1/ the enclosing class is an instantiation of, and therefore not
7593 the same as, the context of the most general template, and
7594 2/ we aren't looking at the partial instantiation itself, i.e.
7595 the innermost arguments are not the same as the innermost parms of
7596 the most general template.
7597
7598 So it's only when 1/ and 2/ happens that we want to use the partial
7599 instantiation of the member template in lieu of its most general
7600 template. */
7601
7602 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7603 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7604 /* the enclosing class must be an instantiation... */
7605 && CLASS_TYPE_P (context)
7606 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7607 {
7608 tree partial_inst_args;
7609 TREE_VEC_LENGTH (arglist)--;
7610 ++processing_template_decl;
7611 partial_inst_args =
7612 tsubst (INNERMOST_TEMPLATE_ARGS
7613 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7614 arglist, complain, NULL_TREE);
7615 --processing_template_decl;
7616 TREE_VEC_LENGTH (arglist)++;
7617 use_partial_inst_tmpl =
7618 /*...and we must not be looking at the partial instantiation
7619 itself. */
7620 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7621 partial_inst_args);
7622 }
7623
7624 if (!use_partial_inst_tmpl)
7625 /* This case is easy; there are no member templates involved. */
7626 found = gen_tmpl;
7627 else
7628 {
7629 /* This is a full instantiation of a member template. Find
7630 the partial instantiation of which this is an instance. */
7631
7632 /* Temporarily reduce by one the number of levels in the ARGLIST
7633 so as to avoid comparing the last set of arguments. */
7634 TREE_VEC_LENGTH (arglist)--;
7635 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7636 TREE_VEC_LENGTH (arglist)++;
7637 /* FOUND is either a proper class type, or an alias
7638 template specialization. In the later case, it's a
7639 TYPE_DECL, resulting from the substituting of arguments
7640 for parameters in the TYPE_DECL of the alias template
7641 done earlier. So be careful while getting the template
7642 of FOUND. */
7643 found = TREE_CODE (found) == TYPE_DECL
7644 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7645 : CLASSTYPE_TI_TEMPLATE (found);
7646 }
7647
7648 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7649
7650 elt.spec = t;
7651 slot = htab_find_slot_with_hash (type_specializations,
7652 &elt, hash, INSERT);
7653 entry = ggc_alloc_spec_entry ();
7654 *entry = elt;
7655 *slot = entry;
7656
7657 /* Note this use of the partial instantiation so we can check it
7658 later in maybe_process_partial_specialization. */
7659 DECL_TEMPLATE_INSTANTIATIONS (templ)
7660 = tree_cons (arglist, t,
7661 DECL_TEMPLATE_INSTANTIATIONS (templ));
7662
7663 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7664 /* Now that the type has been registered on the instantiations
7665 list, we set up the enumerators. Because the enumeration
7666 constants may involve the enumeration type itself, we make
7667 sure to register the type first, and then create the
7668 constants. That way, doing tsubst_expr for the enumeration
7669 constants won't result in recursive calls here; we'll find
7670 the instantiation and exit above. */
7671 tsubst_enum (template_type, t, arglist);
7672
7673 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7674 /* If the type makes use of template parameters, the
7675 code that generates debugging information will crash. */
7676 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7677
7678 /* Possibly limit visibility based on template args. */
7679 TREE_PUBLIC (type_decl) = 1;
7680 determine_visibility (type_decl);
7681
7682 return t;
7683 }
7684 }
7685
7686 /* Wrapper for lookup_template_class_1. */
7687
7688 tree
7689 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7690 int entering_scope, tsubst_flags_t complain)
7691 {
7692 tree ret;
7693 timevar_push (TV_TEMPLATE_INST);
7694 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7695 entering_scope, complain);
7696 timevar_pop (TV_TEMPLATE_INST);
7697 return ret;
7698 }
7699 \f
7700 struct pair_fn_data
7701 {
7702 tree_fn_t fn;
7703 void *data;
7704 /* True when we should also visit template parameters that occur in
7705 non-deduced contexts. */
7706 bool include_nondeduced_p;
7707 struct pointer_set_t *visited;
7708 };
7709
7710 /* Called from for_each_template_parm via walk_tree. */
7711
7712 static tree
7713 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7714 {
7715 tree t = *tp;
7716 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7717 tree_fn_t fn = pfd->fn;
7718 void *data = pfd->data;
7719
7720 if (TYPE_P (t)
7721 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7722 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7723 pfd->include_nondeduced_p))
7724 return error_mark_node;
7725
7726 switch (TREE_CODE (t))
7727 {
7728 case RECORD_TYPE:
7729 if (TYPE_PTRMEMFUNC_P (t))
7730 break;
7731 /* Fall through. */
7732
7733 case UNION_TYPE:
7734 case ENUMERAL_TYPE:
7735 if (!TYPE_TEMPLATE_INFO (t))
7736 *walk_subtrees = 0;
7737 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7738 fn, data, pfd->visited,
7739 pfd->include_nondeduced_p))
7740 return error_mark_node;
7741 break;
7742
7743 case INTEGER_TYPE:
7744 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7745 fn, data, pfd->visited,
7746 pfd->include_nondeduced_p)
7747 || for_each_template_parm (TYPE_MAX_VALUE (t),
7748 fn, data, pfd->visited,
7749 pfd->include_nondeduced_p))
7750 return error_mark_node;
7751 break;
7752
7753 case METHOD_TYPE:
7754 /* Since we're not going to walk subtrees, we have to do this
7755 explicitly here. */
7756 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7757 pfd->visited, pfd->include_nondeduced_p))
7758 return error_mark_node;
7759 /* Fall through. */
7760
7761 case FUNCTION_TYPE:
7762 /* Check the return type. */
7763 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7764 pfd->include_nondeduced_p))
7765 return error_mark_node;
7766
7767 /* Check the parameter types. Since default arguments are not
7768 instantiated until they are needed, the TYPE_ARG_TYPES may
7769 contain expressions that involve template parameters. But,
7770 no-one should be looking at them yet. And, once they're
7771 instantiated, they don't contain template parameters, so
7772 there's no point in looking at them then, either. */
7773 {
7774 tree parm;
7775
7776 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7777 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7778 pfd->visited, pfd->include_nondeduced_p))
7779 return error_mark_node;
7780
7781 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7782 want walk_tree walking into them itself. */
7783 *walk_subtrees = 0;
7784 }
7785 break;
7786
7787 case TYPEOF_TYPE:
7788 case UNDERLYING_TYPE:
7789 if (pfd->include_nondeduced_p
7790 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7791 pfd->visited,
7792 pfd->include_nondeduced_p))
7793 return error_mark_node;
7794 break;
7795
7796 case FUNCTION_DECL:
7797 case VAR_DECL:
7798 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7799 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7800 pfd->visited, pfd->include_nondeduced_p))
7801 return error_mark_node;
7802 /* Fall through. */
7803
7804 case PARM_DECL:
7805 case CONST_DECL:
7806 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7807 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7808 pfd->visited, pfd->include_nondeduced_p))
7809 return error_mark_node;
7810 if (DECL_CONTEXT (t)
7811 && pfd->include_nondeduced_p
7812 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7813 pfd->visited, pfd->include_nondeduced_p))
7814 return error_mark_node;
7815 break;
7816
7817 case BOUND_TEMPLATE_TEMPLATE_PARM:
7818 /* Record template parameters such as `T' inside `TT<T>'. */
7819 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7820 pfd->include_nondeduced_p))
7821 return error_mark_node;
7822 /* Fall through. */
7823
7824 case TEMPLATE_TEMPLATE_PARM:
7825 case TEMPLATE_TYPE_PARM:
7826 case TEMPLATE_PARM_INDEX:
7827 if (fn && (*fn)(t, data))
7828 return error_mark_node;
7829 else if (!fn)
7830 return error_mark_node;
7831 break;
7832
7833 case TEMPLATE_DECL:
7834 /* A template template parameter is encountered. */
7835 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7836 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7837 pfd->include_nondeduced_p))
7838 return error_mark_node;
7839
7840 /* Already substituted template template parameter */
7841 *walk_subtrees = 0;
7842 break;
7843
7844 case TYPENAME_TYPE:
7845 if (!fn
7846 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7847 data, pfd->visited,
7848 pfd->include_nondeduced_p))
7849 return error_mark_node;
7850 break;
7851
7852 case CONSTRUCTOR:
7853 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7854 && pfd->include_nondeduced_p
7855 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7856 (TREE_TYPE (t)), fn, data,
7857 pfd->visited, pfd->include_nondeduced_p))
7858 return error_mark_node;
7859 break;
7860
7861 case INDIRECT_REF:
7862 case COMPONENT_REF:
7863 /* If there's no type, then this thing must be some expression
7864 involving template parameters. */
7865 if (!fn && !TREE_TYPE (t))
7866 return error_mark_node;
7867 break;
7868
7869 case MODOP_EXPR:
7870 case CAST_EXPR:
7871 case IMPLICIT_CONV_EXPR:
7872 case REINTERPRET_CAST_EXPR:
7873 case CONST_CAST_EXPR:
7874 case STATIC_CAST_EXPR:
7875 case DYNAMIC_CAST_EXPR:
7876 case ARROW_EXPR:
7877 case DOTSTAR_EXPR:
7878 case TYPEID_EXPR:
7879 case PSEUDO_DTOR_EXPR:
7880 if (!fn)
7881 return error_mark_node;
7882 break;
7883
7884 default:
7885 break;
7886 }
7887
7888 /* We didn't find any template parameters we liked. */
7889 return NULL_TREE;
7890 }
7891
7892 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7893 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7894 call FN with the parameter and the DATA.
7895 If FN returns nonzero, the iteration is terminated, and
7896 for_each_template_parm returns 1. Otherwise, the iteration
7897 continues. If FN never returns a nonzero value, the value
7898 returned by for_each_template_parm is 0. If FN is NULL, it is
7899 considered to be the function which always returns 1.
7900
7901 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7902 parameters that occur in non-deduced contexts. When false, only
7903 visits those template parameters that can be deduced. */
7904
7905 static int
7906 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7907 struct pointer_set_t *visited,
7908 bool include_nondeduced_p)
7909 {
7910 struct pair_fn_data pfd;
7911 int result;
7912
7913 /* Set up. */
7914 pfd.fn = fn;
7915 pfd.data = data;
7916 pfd.include_nondeduced_p = include_nondeduced_p;
7917
7918 /* Walk the tree. (Conceptually, we would like to walk without
7919 duplicates, but for_each_template_parm_r recursively calls
7920 for_each_template_parm, so we would need to reorganize a fair
7921 bit to use walk_tree_without_duplicates, so we keep our own
7922 visited list.) */
7923 if (visited)
7924 pfd.visited = visited;
7925 else
7926 pfd.visited = pointer_set_create ();
7927 result = cp_walk_tree (&t,
7928 for_each_template_parm_r,
7929 &pfd,
7930 pfd.visited) != NULL_TREE;
7931
7932 /* Clean up. */
7933 if (!visited)
7934 {
7935 pointer_set_destroy (pfd.visited);
7936 pfd.visited = 0;
7937 }
7938
7939 return result;
7940 }
7941
7942 /* Returns true if T depends on any template parameter. */
7943
7944 int
7945 uses_template_parms (tree t)
7946 {
7947 bool dependent_p;
7948 int saved_processing_template_decl;
7949
7950 saved_processing_template_decl = processing_template_decl;
7951 if (!saved_processing_template_decl)
7952 processing_template_decl = 1;
7953 if (TYPE_P (t))
7954 dependent_p = dependent_type_p (t);
7955 else if (TREE_CODE (t) == TREE_VEC)
7956 dependent_p = any_dependent_template_arguments_p (t);
7957 else if (TREE_CODE (t) == TREE_LIST)
7958 dependent_p = (uses_template_parms (TREE_VALUE (t))
7959 || uses_template_parms (TREE_CHAIN (t)));
7960 else if (TREE_CODE (t) == TYPE_DECL)
7961 dependent_p = dependent_type_p (TREE_TYPE (t));
7962 else if (DECL_P (t)
7963 || EXPR_P (t)
7964 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7965 || TREE_CODE (t) == OVERLOAD
7966 || BASELINK_P (t)
7967 || identifier_p (t)
7968 || TREE_CODE (t) == TRAIT_EXPR
7969 || TREE_CODE (t) == CONSTRUCTOR
7970 || CONSTANT_CLASS_P (t))
7971 dependent_p = (type_dependent_expression_p (t)
7972 || value_dependent_expression_p (t));
7973 else
7974 {
7975 gcc_assert (t == error_mark_node);
7976 dependent_p = false;
7977 }
7978
7979 processing_template_decl = saved_processing_template_decl;
7980
7981 return dependent_p;
7982 }
7983
7984 /* Returns true iff current_function_decl is an incompletely instantiated
7985 template. Useful instead of processing_template_decl because the latter
7986 is set to 0 during fold_non_dependent_expr. */
7987
7988 bool
7989 in_template_function (void)
7990 {
7991 tree fn = current_function_decl;
7992 bool ret;
7993 ++processing_template_decl;
7994 ret = (fn && DECL_LANG_SPECIFIC (fn)
7995 && DECL_TEMPLATE_INFO (fn)
7996 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7997 --processing_template_decl;
7998 return ret;
7999 }
8000
8001 /* Returns true if T depends on any template parameter with level LEVEL. */
8002
8003 int
8004 uses_template_parms_level (tree t, int level)
8005 {
8006 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8007 /*include_nondeduced_p=*/true);
8008 }
8009
8010 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8011 ill-formed translation unit, i.e. a variable or function that isn't
8012 usable in a constant expression. */
8013
8014 static inline bool
8015 neglectable_inst_p (tree d)
8016 {
8017 return (DECL_P (d)
8018 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8019 : decl_maybe_constant_var_p (d)));
8020 }
8021
8022 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8023 neglectable and instantiated from within an erroneous instantiation. */
8024
8025 static bool
8026 limit_bad_template_recursion (tree decl)
8027 {
8028 struct tinst_level *lev = current_tinst_level;
8029 int errs = errorcount + sorrycount;
8030 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8031 return false;
8032
8033 for (; lev; lev = lev->next)
8034 if (neglectable_inst_p (lev->decl))
8035 break;
8036
8037 return (lev && errs > lev->errors);
8038 }
8039
8040 static int tinst_depth;
8041 extern int max_tinst_depth;
8042 int depth_reached;
8043
8044 static GTY(()) struct tinst_level *last_error_tinst_level;
8045
8046 /* We're starting to instantiate D; record the template instantiation context
8047 for diagnostics and to restore it later. */
8048
8049 int
8050 push_tinst_level (tree d)
8051 {
8052 struct tinst_level *new_level;
8053
8054 if (tinst_depth >= max_tinst_depth)
8055 {
8056 last_error_tinst_level = current_tinst_level;
8057 if (TREE_CODE (d) == TREE_LIST)
8058 error ("template instantiation depth exceeds maximum of %d (use "
8059 "-ftemplate-depth= to increase the maximum) substituting %qS",
8060 max_tinst_depth, d);
8061 else
8062 error ("template instantiation depth exceeds maximum of %d (use "
8063 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8064 max_tinst_depth, d);
8065
8066 print_instantiation_context ();
8067
8068 return 0;
8069 }
8070
8071 /* If the current instantiation caused problems, don't let it instantiate
8072 anything else. Do allow deduction substitution and decls usable in
8073 constant expressions. */
8074 if (limit_bad_template_recursion (d))
8075 return 0;
8076
8077 new_level = ggc_alloc_tinst_level ();
8078 new_level->decl = d;
8079 new_level->locus = input_location;
8080 new_level->errors = errorcount+sorrycount;
8081 new_level->in_system_header_p = in_system_header;
8082 new_level->next = current_tinst_level;
8083 current_tinst_level = new_level;
8084
8085 ++tinst_depth;
8086 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8087 depth_reached = tinst_depth;
8088
8089 return 1;
8090 }
8091
8092 /* We're done instantiating this template; return to the instantiation
8093 context. */
8094
8095 void
8096 pop_tinst_level (void)
8097 {
8098 /* Restore the filename and line number stashed away when we started
8099 this instantiation. */
8100 input_location = current_tinst_level->locus;
8101 current_tinst_level = current_tinst_level->next;
8102 --tinst_depth;
8103 }
8104
8105 /* We're instantiating a deferred template; restore the template
8106 instantiation context in which the instantiation was requested, which
8107 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8108
8109 static tree
8110 reopen_tinst_level (struct tinst_level *level)
8111 {
8112 struct tinst_level *t;
8113
8114 tinst_depth = 0;
8115 for (t = level; t; t = t->next)
8116 ++tinst_depth;
8117
8118 current_tinst_level = level;
8119 pop_tinst_level ();
8120 if (current_tinst_level)
8121 current_tinst_level->errors = errorcount+sorrycount;
8122 return level->decl;
8123 }
8124
8125 /* Returns the TINST_LEVEL which gives the original instantiation
8126 context. */
8127
8128 struct tinst_level *
8129 outermost_tinst_level (void)
8130 {
8131 struct tinst_level *level = current_tinst_level;
8132 if (level)
8133 while (level->next)
8134 level = level->next;
8135 return level;
8136 }
8137
8138 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8139 vector of template arguments, as for tsubst.
8140
8141 Returns an appropriate tsubst'd friend declaration. */
8142
8143 static tree
8144 tsubst_friend_function (tree decl, tree args)
8145 {
8146 tree new_friend;
8147
8148 if (TREE_CODE (decl) == FUNCTION_DECL
8149 && DECL_TEMPLATE_INSTANTIATION (decl)
8150 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8151 /* This was a friend declared with an explicit template
8152 argument list, e.g.:
8153
8154 friend void f<>(T);
8155
8156 to indicate that f was a template instantiation, not a new
8157 function declaration. Now, we have to figure out what
8158 instantiation of what template. */
8159 {
8160 tree template_id, arglist, fns;
8161 tree new_args;
8162 tree tmpl;
8163 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8164
8165 /* Friend functions are looked up in the containing namespace scope.
8166 We must enter that scope, to avoid finding member functions of the
8167 current class with same name. */
8168 push_nested_namespace (ns);
8169 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8170 tf_warning_or_error, NULL_TREE,
8171 /*integral_constant_expression_p=*/false);
8172 pop_nested_namespace (ns);
8173 arglist = tsubst (DECL_TI_ARGS (decl), args,
8174 tf_warning_or_error, NULL_TREE);
8175 template_id = lookup_template_function (fns, arglist);
8176
8177 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8178 tmpl = determine_specialization (template_id, new_friend,
8179 &new_args,
8180 /*need_member_template=*/0,
8181 TREE_VEC_LENGTH (args),
8182 tsk_none);
8183 return instantiate_template (tmpl, new_args, tf_error);
8184 }
8185
8186 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8187
8188 /* The NEW_FRIEND will look like an instantiation, to the
8189 compiler, but is not an instantiation from the point of view of
8190 the language. For example, we might have had:
8191
8192 template <class T> struct S {
8193 template <class U> friend void f(T, U);
8194 };
8195
8196 Then, in S<int>, template <class U> void f(int, U) is not an
8197 instantiation of anything. */
8198 if (new_friend == error_mark_node)
8199 return error_mark_node;
8200
8201 DECL_USE_TEMPLATE (new_friend) = 0;
8202 if (TREE_CODE (decl) == TEMPLATE_DECL)
8203 {
8204 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8205 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8206 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8207 }
8208
8209 /* The mangled name for the NEW_FRIEND is incorrect. The function
8210 is not a template instantiation and should not be mangled like
8211 one. Therefore, we forget the mangling here; we'll recompute it
8212 later if we need it. */
8213 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8214 {
8215 SET_DECL_RTL (new_friend, NULL);
8216 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8217 }
8218
8219 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8220 {
8221 tree old_decl;
8222 tree new_friend_template_info;
8223 tree new_friend_result_template_info;
8224 tree ns;
8225 int new_friend_is_defn;
8226
8227 /* We must save some information from NEW_FRIEND before calling
8228 duplicate decls since that function will free NEW_FRIEND if
8229 possible. */
8230 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8231 new_friend_is_defn =
8232 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8233 (template_for_substitution (new_friend)))
8234 != NULL_TREE);
8235 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8236 {
8237 /* This declaration is a `primary' template. */
8238 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8239
8240 new_friend_result_template_info
8241 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8242 }
8243 else
8244 new_friend_result_template_info = NULL_TREE;
8245
8246 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8247 if (new_friend_is_defn)
8248 DECL_INITIAL (new_friend) = error_mark_node;
8249
8250 /* Inside pushdecl_namespace_level, we will push into the
8251 current namespace. However, the friend function should go
8252 into the namespace of the template. */
8253 ns = decl_namespace_context (new_friend);
8254 push_nested_namespace (ns);
8255 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8256 pop_nested_namespace (ns);
8257
8258 if (old_decl == error_mark_node)
8259 return error_mark_node;
8260
8261 if (old_decl != new_friend)
8262 {
8263 /* This new friend declaration matched an existing
8264 declaration. For example, given:
8265
8266 template <class T> void f(T);
8267 template <class U> class C {
8268 template <class T> friend void f(T) {}
8269 };
8270
8271 the friend declaration actually provides the definition
8272 of `f', once C has been instantiated for some type. So,
8273 old_decl will be the out-of-class template declaration,
8274 while new_friend is the in-class definition.
8275
8276 But, if `f' was called before this point, the
8277 instantiation of `f' will have DECL_TI_ARGS corresponding
8278 to `T' but not to `U', references to which might appear
8279 in the definition of `f'. Previously, the most general
8280 template for an instantiation of `f' was the out-of-class
8281 version; now it is the in-class version. Therefore, we
8282 run through all specialization of `f', adding to their
8283 DECL_TI_ARGS appropriately. In particular, they need a
8284 new set of outer arguments, corresponding to the
8285 arguments for this class instantiation.
8286
8287 The same situation can arise with something like this:
8288
8289 friend void f(int);
8290 template <class T> class C {
8291 friend void f(T) {}
8292 };
8293
8294 when `C<int>' is instantiated. Now, `f(int)' is defined
8295 in the class. */
8296
8297 if (!new_friend_is_defn)
8298 /* On the other hand, if the in-class declaration does
8299 *not* provide a definition, then we don't want to alter
8300 existing definitions. We can just leave everything
8301 alone. */
8302 ;
8303 else
8304 {
8305 tree new_template = TI_TEMPLATE (new_friend_template_info);
8306 tree new_args = TI_ARGS (new_friend_template_info);
8307
8308 /* Overwrite whatever template info was there before, if
8309 any, with the new template information pertaining to
8310 the declaration. */
8311 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8312
8313 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8314 {
8315 /* We should have called reregister_specialization in
8316 duplicate_decls. */
8317 gcc_assert (retrieve_specialization (new_template,
8318 new_args, 0)
8319 == old_decl);
8320
8321 /* Instantiate it if the global has already been used. */
8322 if (DECL_ODR_USED (old_decl))
8323 instantiate_decl (old_decl, /*defer_ok=*/true,
8324 /*expl_inst_class_mem_p=*/false);
8325 }
8326 else
8327 {
8328 tree t;
8329
8330 /* Indicate that the old function template is a partial
8331 instantiation. */
8332 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8333 = new_friend_result_template_info;
8334
8335 gcc_assert (new_template
8336 == most_general_template (new_template));
8337 gcc_assert (new_template != old_decl);
8338
8339 /* Reassign any specializations already in the hash table
8340 to the new more general template, and add the
8341 additional template args. */
8342 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8343 t != NULL_TREE;
8344 t = TREE_CHAIN (t))
8345 {
8346 tree spec = TREE_VALUE (t);
8347 spec_entry elt;
8348
8349 elt.tmpl = old_decl;
8350 elt.args = DECL_TI_ARGS (spec);
8351 elt.spec = NULL_TREE;
8352
8353 htab_remove_elt (decl_specializations, &elt);
8354
8355 DECL_TI_ARGS (spec)
8356 = add_outermost_template_args (new_args,
8357 DECL_TI_ARGS (spec));
8358
8359 register_specialization
8360 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8361
8362 }
8363 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8364 }
8365 }
8366
8367 /* The information from NEW_FRIEND has been merged into OLD_DECL
8368 by duplicate_decls. */
8369 new_friend = old_decl;
8370 }
8371 }
8372 else
8373 {
8374 tree context = DECL_CONTEXT (new_friend);
8375 bool dependent_p;
8376
8377 /* In the code
8378 template <class T> class C {
8379 template <class U> friend void C1<U>::f (); // case 1
8380 friend void C2<T>::f (); // case 2
8381 };
8382 we only need to make sure CONTEXT is a complete type for
8383 case 2. To distinguish between the two cases, we note that
8384 CONTEXT of case 1 remains dependent type after tsubst while
8385 this isn't true for case 2. */
8386 ++processing_template_decl;
8387 dependent_p = dependent_type_p (context);
8388 --processing_template_decl;
8389
8390 if (!dependent_p
8391 && !complete_type_or_else (context, NULL_TREE))
8392 return error_mark_node;
8393
8394 if (COMPLETE_TYPE_P (context))
8395 {
8396 /* Check to see that the declaration is really present, and,
8397 possibly obtain an improved declaration. */
8398 tree fn = check_classfn (context,
8399 new_friend, NULL_TREE);
8400
8401 if (fn)
8402 new_friend = fn;
8403 }
8404 }
8405
8406 return new_friend;
8407 }
8408
8409 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8410 template arguments, as for tsubst.
8411
8412 Returns an appropriate tsubst'd friend type or error_mark_node on
8413 failure. */
8414
8415 static tree
8416 tsubst_friend_class (tree friend_tmpl, tree args)
8417 {
8418 tree friend_type;
8419 tree tmpl;
8420 tree context;
8421
8422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8423 {
8424 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8425 return TREE_TYPE (t);
8426 }
8427
8428 context = CP_DECL_CONTEXT (friend_tmpl);
8429
8430 if (context != global_namespace)
8431 {
8432 if (TREE_CODE (context) == NAMESPACE_DECL)
8433 push_nested_namespace (context);
8434 else
8435 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8436 }
8437
8438 /* Look for a class template declaration. We look for hidden names
8439 because two friend declarations of the same template are the
8440 same. For example, in:
8441
8442 struct A {
8443 template <typename> friend class F;
8444 };
8445 template <typename> struct B {
8446 template <typename> friend class F;
8447 };
8448
8449 both F templates are the same. */
8450 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8451 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8452
8453 /* But, if we don't find one, it might be because we're in a
8454 situation like this:
8455
8456 template <class T>
8457 struct S {
8458 template <class U>
8459 friend struct S;
8460 };
8461
8462 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8463 for `S<int>', not the TEMPLATE_DECL. */
8464 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8465 {
8466 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8467 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8468 }
8469
8470 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8471 {
8472 /* The friend template has already been declared. Just
8473 check to see that the declarations match, and install any new
8474 default parameters. We must tsubst the default parameters,
8475 of course. We only need the innermost template parameters
8476 because that is all that redeclare_class_template will look
8477 at. */
8478 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8479 > TMPL_ARGS_DEPTH (args))
8480 {
8481 tree parms;
8482 location_t saved_input_location;
8483 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8484 args, tf_warning_or_error);
8485
8486 saved_input_location = input_location;
8487 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8488 redeclare_class_template (TREE_TYPE (tmpl), parms);
8489 input_location = saved_input_location;
8490
8491 }
8492
8493 friend_type = TREE_TYPE (tmpl);
8494 }
8495 else
8496 {
8497 /* The friend template has not already been declared. In this
8498 case, the instantiation of the template class will cause the
8499 injection of this template into the global scope. */
8500 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8501 if (tmpl == error_mark_node)
8502 return error_mark_node;
8503
8504 /* The new TMPL is not an instantiation of anything, so we
8505 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8506 the new type because that is supposed to be the corresponding
8507 template decl, i.e., TMPL. */
8508 DECL_USE_TEMPLATE (tmpl) = 0;
8509 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8510 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8511 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8512 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8513
8514 /* Inject this template into the global scope. */
8515 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8516 }
8517
8518 if (context != global_namespace)
8519 {
8520 if (TREE_CODE (context) == NAMESPACE_DECL)
8521 pop_nested_namespace (context);
8522 else
8523 pop_nested_class ();
8524 }
8525
8526 return friend_type;
8527 }
8528
8529 /* Returns zero if TYPE cannot be completed later due to circularity.
8530 Otherwise returns one. */
8531
8532 static int
8533 can_complete_type_without_circularity (tree type)
8534 {
8535 if (type == NULL_TREE || type == error_mark_node)
8536 return 0;
8537 else if (COMPLETE_TYPE_P (type))
8538 return 1;
8539 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8540 return can_complete_type_without_circularity (TREE_TYPE (type));
8541 else if (CLASS_TYPE_P (type)
8542 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8543 return 0;
8544 else
8545 return 1;
8546 }
8547
8548 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8549
8550 /* Apply any attributes which had to be deferred until instantiation
8551 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8552 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8553
8554 static void
8555 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8556 tree args, tsubst_flags_t complain, tree in_decl)
8557 {
8558 tree last_dep = NULL_TREE;
8559 tree t;
8560 tree *p;
8561
8562 for (t = attributes; t; t = TREE_CHAIN (t))
8563 if (ATTR_IS_DEPENDENT (t))
8564 {
8565 last_dep = t;
8566 attributes = copy_list (attributes);
8567 break;
8568 }
8569
8570 if (DECL_P (*decl_p))
8571 {
8572 if (TREE_TYPE (*decl_p) == error_mark_node)
8573 return;
8574 p = &DECL_ATTRIBUTES (*decl_p);
8575 }
8576 else
8577 p = &TYPE_ATTRIBUTES (*decl_p);
8578
8579 if (last_dep)
8580 {
8581 tree late_attrs = NULL_TREE;
8582 tree *q = &late_attrs;
8583
8584 for (*p = attributes; *p; )
8585 {
8586 t = *p;
8587 if (ATTR_IS_DEPENDENT (t))
8588 {
8589 *p = TREE_CHAIN (t);
8590 TREE_CHAIN (t) = NULL_TREE;
8591 if (flag_openmp
8592 && is_attribute_p ("omp declare simd",
8593 get_attribute_name (t))
8594 && TREE_VALUE (t))
8595 {
8596 tree clauses = TREE_VALUE (TREE_VALUE (t));
8597 clauses = tsubst_omp_clauses (clauses, true, args,
8598 complain, in_decl);
8599 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8600 clauses = finish_omp_clauses (clauses);
8601 tree parms = DECL_ARGUMENTS (*decl_p);
8602 clauses
8603 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8604 if (clauses)
8605 TREE_VALUE (TREE_VALUE (t)) = clauses;
8606 else
8607 TREE_VALUE (t) = NULL_TREE;
8608 }
8609 /* If the first attribute argument is an identifier, don't
8610 pass it through tsubst. Attributes like mode, format,
8611 cleanup and several target specific attributes expect it
8612 unmodified. */
8613 else if (attribute_takes_identifier_p (TREE_PURPOSE (t)))
8614 {
8615 tree chain
8616 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8617 in_decl,
8618 /*integral_constant_expression_p=*/false);
8619 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8620 TREE_VALUE (t)
8621 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8622 chain);
8623 }
8624 else
8625 TREE_VALUE (t)
8626 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8627 /*integral_constant_expression_p=*/false);
8628 *q = t;
8629 q = &TREE_CHAIN (t);
8630 }
8631 else
8632 p = &TREE_CHAIN (t);
8633 }
8634
8635 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8636 }
8637 }
8638
8639 /* Perform (or defer) access check for typedefs that were referenced
8640 from within the template TMPL code.
8641 This is a subroutine of instantiate_decl and instantiate_class_template.
8642 TMPL is the template to consider and TARGS is the list of arguments of
8643 that template. */
8644
8645 static void
8646 perform_typedefs_access_check (tree tmpl, tree targs)
8647 {
8648 location_t saved_location;
8649 unsigned i;
8650 qualified_typedef_usage_t *iter;
8651
8652 if (!tmpl
8653 || (!CLASS_TYPE_P (tmpl)
8654 && TREE_CODE (tmpl) != FUNCTION_DECL))
8655 return;
8656
8657 saved_location = input_location;
8658 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8659 {
8660 tree type_decl = iter->typedef_decl;
8661 tree type_scope = iter->context;
8662
8663 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8664 continue;
8665
8666 if (uses_template_parms (type_decl))
8667 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8668 if (uses_template_parms (type_scope))
8669 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8670
8671 /* Make access check error messages point to the location
8672 of the use of the typedef. */
8673 input_location = iter->locus;
8674 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8675 type_decl, type_decl,
8676 tf_warning_or_error);
8677 }
8678 input_location = saved_location;
8679 }
8680
8681 static tree
8682 instantiate_class_template_1 (tree type)
8683 {
8684 tree templ, args, pattern, t, member;
8685 tree typedecl;
8686 tree pbinfo;
8687 tree base_list;
8688 unsigned int saved_maximum_field_alignment;
8689 tree fn_context;
8690
8691 if (type == error_mark_node)
8692 return error_mark_node;
8693
8694 if (COMPLETE_OR_OPEN_TYPE_P (type)
8695 || uses_template_parms (type))
8696 return type;
8697
8698 /* Figure out which template is being instantiated. */
8699 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8700 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8701
8702 /* Determine what specialization of the original template to
8703 instantiate. */
8704 t = most_specialized_class (type, templ, tf_warning_or_error);
8705 if (t == error_mark_node)
8706 {
8707 TYPE_BEING_DEFINED (type) = 1;
8708 return error_mark_node;
8709 }
8710 else if (t)
8711 {
8712 /* This TYPE is actually an instantiation of a partial
8713 specialization. We replace the innermost set of ARGS with
8714 the arguments appropriate for substitution. For example,
8715 given:
8716
8717 template <class T> struct S {};
8718 template <class T> struct S<T*> {};
8719
8720 and supposing that we are instantiating S<int*>, ARGS will
8721 presently be {int*} -- but we need {int}. */
8722 pattern = TREE_TYPE (t);
8723 args = TREE_PURPOSE (t);
8724 }
8725 else
8726 {
8727 pattern = TREE_TYPE (templ);
8728 args = CLASSTYPE_TI_ARGS (type);
8729 }
8730
8731 /* If the template we're instantiating is incomplete, then clearly
8732 there's nothing we can do. */
8733 if (!COMPLETE_TYPE_P (pattern))
8734 return type;
8735
8736 /* If we've recursively instantiated too many templates, stop. */
8737 if (! push_tinst_level (type))
8738 return type;
8739
8740 /* Now we're really doing the instantiation. Mark the type as in
8741 the process of being defined. */
8742 TYPE_BEING_DEFINED (type) = 1;
8743
8744 /* We may be in the middle of deferred access check. Disable
8745 it now. */
8746 push_deferring_access_checks (dk_no_deferred);
8747
8748 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8749 if (!fn_context)
8750 push_to_top_level ();
8751 /* Use #pragma pack from the template context. */
8752 saved_maximum_field_alignment = maximum_field_alignment;
8753 maximum_field_alignment = TYPE_PRECISION (pattern);
8754
8755 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8756
8757 /* Set the input location to the most specialized template definition.
8758 This is needed if tsubsting causes an error. */
8759 typedecl = TYPE_MAIN_DECL (pattern);
8760 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8761 DECL_SOURCE_LOCATION (typedecl);
8762
8763 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8764 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8765 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8766 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8767 if (ANON_AGGR_TYPE_P (pattern))
8768 SET_ANON_AGGR_TYPE_P (type);
8769 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8770 {
8771 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8772 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8773 /* Adjust visibility for template arguments. */
8774 determine_visibility (TYPE_MAIN_DECL (type));
8775 }
8776 if (CLASS_TYPE_P (type))
8777 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8778
8779 pbinfo = TYPE_BINFO (pattern);
8780
8781 /* We should never instantiate a nested class before its enclosing
8782 class; we need to look up the nested class by name before we can
8783 instantiate it, and that lookup should instantiate the enclosing
8784 class. */
8785 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8786 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8787
8788 base_list = NULL_TREE;
8789 if (BINFO_N_BASE_BINFOS (pbinfo))
8790 {
8791 tree pbase_binfo;
8792 tree pushed_scope;
8793 int i;
8794
8795 /* We must enter the scope containing the type, as that is where
8796 the accessibility of types named in dependent bases are
8797 looked up from. */
8798 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8799
8800 /* Substitute into each of the bases to determine the actual
8801 basetypes. */
8802 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8803 {
8804 tree base;
8805 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8806 tree expanded_bases = NULL_TREE;
8807 int idx, len = 1;
8808
8809 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8810 {
8811 expanded_bases =
8812 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8813 args, tf_error, NULL_TREE);
8814 if (expanded_bases == error_mark_node)
8815 continue;
8816
8817 len = TREE_VEC_LENGTH (expanded_bases);
8818 }
8819
8820 for (idx = 0; idx < len; idx++)
8821 {
8822 if (expanded_bases)
8823 /* Extract the already-expanded base class. */
8824 base = TREE_VEC_ELT (expanded_bases, idx);
8825 else
8826 /* Substitute to figure out the base class. */
8827 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8828 NULL_TREE);
8829
8830 if (base == error_mark_node)
8831 continue;
8832
8833 base_list = tree_cons (access, base, base_list);
8834 if (BINFO_VIRTUAL_P (pbase_binfo))
8835 TREE_TYPE (base_list) = integer_type_node;
8836 }
8837 }
8838
8839 /* The list is now in reverse order; correct that. */
8840 base_list = nreverse (base_list);
8841
8842 if (pushed_scope)
8843 pop_scope (pushed_scope);
8844 }
8845 /* Now call xref_basetypes to set up all the base-class
8846 information. */
8847 xref_basetypes (type, base_list);
8848
8849 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8850 (int) ATTR_FLAG_TYPE_IN_PLACE,
8851 args, tf_error, NULL_TREE);
8852 fixup_attribute_variants (type);
8853
8854 /* Now that our base classes are set up, enter the scope of the
8855 class, so that name lookups into base classes, etc. will work
8856 correctly. This is precisely analogous to what we do in
8857 begin_class_definition when defining an ordinary non-template
8858 class, except we also need to push the enclosing classes. */
8859 push_nested_class (type);
8860
8861 /* Now members are processed in the order of declaration. */
8862 for (member = CLASSTYPE_DECL_LIST (pattern);
8863 member; member = TREE_CHAIN (member))
8864 {
8865 tree t = TREE_VALUE (member);
8866
8867 if (TREE_PURPOSE (member))
8868 {
8869 if (TYPE_P (t))
8870 {
8871 /* Build new CLASSTYPE_NESTED_UTDS. */
8872
8873 tree newtag;
8874 bool class_template_p;
8875
8876 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8877 && TYPE_LANG_SPECIFIC (t)
8878 && CLASSTYPE_IS_TEMPLATE (t));
8879 /* If the member is a class template, then -- even after
8880 substitution -- there may be dependent types in the
8881 template argument list for the class. We increment
8882 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8883 that function will assume that no types are dependent
8884 when outside of a template. */
8885 if (class_template_p)
8886 ++processing_template_decl;
8887 newtag = tsubst (t, args, tf_error, NULL_TREE);
8888 if (class_template_p)
8889 --processing_template_decl;
8890 if (newtag == error_mark_node)
8891 continue;
8892
8893 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8894 {
8895 tree name = TYPE_IDENTIFIER (t);
8896
8897 if (class_template_p)
8898 /* Unfortunately, lookup_template_class sets
8899 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8900 instantiation (i.e., for the type of a member
8901 template class nested within a template class.)
8902 This behavior is required for
8903 maybe_process_partial_specialization to work
8904 correctly, but is not accurate in this case;
8905 the TAG is not an instantiation of anything.
8906 (The corresponding TEMPLATE_DECL is an
8907 instantiation, but the TYPE is not.) */
8908 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8909
8910 /* Now, we call pushtag to put this NEWTAG into the scope of
8911 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8912 pushtag calling push_template_decl. We don't have to do
8913 this for enums because it will already have been done in
8914 tsubst_enum. */
8915 if (name)
8916 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8917 pushtag (name, newtag, /*tag_scope=*/ts_current);
8918 }
8919 }
8920 else if (DECL_DECLARES_FUNCTION_P (t))
8921 {
8922 /* Build new TYPE_METHODS. */
8923 tree r;
8924
8925 if (TREE_CODE (t) == TEMPLATE_DECL)
8926 ++processing_template_decl;
8927 r = tsubst (t, args, tf_error, NULL_TREE);
8928 if (TREE_CODE (t) == TEMPLATE_DECL)
8929 --processing_template_decl;
8930 set_current_access_from_decl (r);
8931 finish_member_declaration (r);
8932 /* Instantiate members marked with attribute used. */
8933 if (r != error_mark_node && DECL_PRESERVE_P (r))
8934 mark_used (r);
8935 if (TREE_CODE (r) == FUNCTION_DECL
8936 && DECL_OMP_DECLARE_REDUCTION_P (r))
8937 cp_check_omp_declare_reduction (r);
8938 }
8939 else
8940 {
8941 /* Build new TYPE_FIELDS. */
8942 if (TREE_CODE (t) == STATIC_ASSERT)
8943 {
8944 tree condition;
8945
8946 ++c_inhibit_evaluation_warnings;
8947 condition =
8948 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8949 tf_warning_or_error, NULL_TREE,
8950 /*integral_constant_expression_p=*/true);
8951 --c_inhibit_evaluation_warnings;
8952
8953 finish_static_assert (condition,
8954 STATIC_ASSERT_MESSAGE (t),
8955 STATIC_ASSERT_SOURCE_LOCATION (t),
8956 /*member_p=*/true);
8957 }
8958 else if (TREE_CODE (t) != CONST_DECL)
8959 {
8960 tree r;
8961 tree vec = NULL_TREE;
8962 int len = 1;
8963
8964 /* The file and line for this declaration, to
8965 assist in error message reporting. Since we
8966 called push_tinst_level above, we don't need to
8967 restore these. */
8968 input_location = DECL_SOURCE_LOCATION (t);
8969
8970 if (TREE_CODE (t) == TEMPLATE_DECL)
8971 ++processing_template_decl;
8972 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8973 if (TREE_CODE (t) == TEMPLATE_DECL)
8974 --processing_template_decl;
8975
8976 if (TREE_CODE (r) == TREE_VEC)
8977 {
8978 /* A capture pack became multiple fields. */
8979 vec = r;
8980 len = TREE_VEC_LENGTH (vec);
8981 }
8982
8983 for (int i = 0; i < len; ++i)
8984 {
8985 if (vec)
8986 r = TREE_VEC_ELT (vec, i);
8987 if (VAR_P (r))
8988 {
8989 /* In [temp.inst]:
8990
8991 [t]he initialization (and any associated
8992 side-effects) of a static data member does
8993 not occur unless the static data member is
8994 itself used in a way that requires the
8995 definition of the static data member to
8996 exist.
8997
8998 Therefore, we do not substitute into the
8999 initialized for the static data member here. */
9000 finish_static_data_member_decl
9001 (r,
9002 /*init=*/NULL_TREE,
9003 /*init_const_expr_p=*/false,
9004 /*asmspec_tree=*/NULL_TREE,
9005 /*flags=*/0);
9006 /* Instantiate members marked with attribute used. */
9007 if (r != error_mark_node && DECL_PRESERVE_P (r))
9008 mark_used (r);
9009 }
9010 else if (TREE_CODE (r) == FIELD_DECL)
9011 {
9012 /* Determine whether R has a valid type and can be
9013 completed later. If R is invalid, then its type
9014 is replaced by error_mark_node. */
9015 tree rtype = TREE_TYPE (r);
9016 if (can_complete_type_without_circularity (rtype))
9017 complete_type (rtype);
9018
9019 if (!COMPLETE_TYPE_P (rtype))
9020 {
9021 cxx_incomplete_type_error (r, rtype);
9022 TREE_TYPE (r) = error_mark_node;
9023 }
9024 }
9025
9026 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9027 such a thing will already have been added to the field
9028 list by tsubst_enum in finish_member_declaration in the
9029 CLASSTYPE_NESTED_UTDS case above. */
9030 if (!(TREE_CODE (r) == TYPE_DECL
9031 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9032 && DECL_ARTIFICIAL (r)))
9033 {
9034 set_current_access_from_decl (r);
9035 finish_member_declaration (r);
9036 }
9037 }
9038 }
9039 }
9040 }
9041 else
9042 {
9043 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9044 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9045 {
9046 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9047
9048 tree friend_type = t;
9049 bool adjust_processing_template_decl = false;
9050
9051 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9052 {
9053 /* template <class T> friend class C; */
9054 friend_type = tsubst_friend_class (friend_type, args);
9055 adjust_processing_template_decl = true;
9056 }
9057 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9058 {
9059 /* template <class T> friend class C::D; */
9060 friend_type = tsubst (friend_type, args,
9061 tf_warning_or_error, NULL_TREE);
9062 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9063 friend_type = TREE_TYPE (friend_type);
9064 adjust_processing_template_decl = true;
9065 }
9066 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9067 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9068 {
9069 /* This could be either
9070
9071 friend class T::C;
9072
9073 when dependent_type_p is false or
9074
9075 template <class U> friend class T::C;
9076
9077 otherwise. */
9078 friend_type = tsubst (friend_type, args,
9079 tf_warning_or_error, NULL_TREE);
9080 /* Bump processing_template_decl for correct
9081 dependent_type_p calculation. */
9082 ++processing_template_decl;
9083 if (dependent_type_p (friend_type))
9084 adjust_processing_template_decl = true;
9085 --processing_template_decl;
9086 }
9087 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9088 && hidden_name_p (TYPE_NAME (friend_type)))
9089 {
9090 /* friend class C;
9091
9092 where C hasn't been declared yet. Let's lookup name
9093 from namespace scope directly, bypassing any name that
9094 come from dependent base class. */
9095 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9096
9097 /* The call to xref_tag_from_type does injection for friend
9098 classes. */
9099 push_nested_namespace (ns);
9100 friend_type =
9101 xref_tag_from_type (friend_type, NULL_TREE,
9102 /*tag_scope=*/ts_current);
9103 pop_nested_namespace (ns);
9104 }
9105 else if (uses_template_parms (friend_type))
9106 /* friend class C<T>; */
9107 friend_type = tsubst (friend_type, args,
9108 tf_warning_or_error, NULL_TREE);
9109 /* Otherwise it's
9110
9111 friend class C;
9112
9113 where C is already declared or
9114
9115 friend class C<int>;
9116
9117 We don't have to do anything in these cases. */
9118
9119 if (adjust_processing_template_decl)
9120 /* Trick make_friend_class into realizing that the friend
9121 we're adding is a template, not an ordinary class. It's
9122 important that we use make_friend_class since it will
9123 perform some error-checking and output cross-reference
9124 information. */
9125 ++processing_template_decl;
9126
9127 if (friend_type != error_mark_node)
9128 make_friend_class (type, friend_type, /*complain=*/false);
9129
9130 if (adjust_processing_template_decl)
9131 --processing_template_decl;
9132 }
9133 else
9134 {
9135 /* Build new DECL_FRIENDLIST. */
9136 tree r;
9137
9138 /* The file and line for this declaration, to
9139 assist in error message reporting. Since we
9140 called push_tinst_level above, we don't need to
9141 restore these. */
9142 input_location = DECL_SOURCE_LOCATION (t);
9143
9144 if (TREE_CODE (t) == TEMPLATE_DECL)
9145 {
9146 ++processing_template_decl;
9147 push_deferring_access_checks (dk_no_check);
9148 }
9149
9150 r = tsubst_friend_function (t, args);
9151 add_friend (type, r, /*complain=*/false);
9152 if (TREE_CODE (t) == TEMPLATE_DECL)
9153 {
9154 pop_deferring_access_checks ();
9155 --processing_template_decl;
9156 }
9157 }
9158 }
9159 }
9160
9161 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9162 {
9163 tree decl = lambda_function (type);
9164 if (decl)
9165 {
9166 if (!DECL_TEMPLATE_INFO (decl)
9167 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9168 instantiate_decl (decl, false, false);
9169
9170 /* We need to instantiate the capture list from the template
9171 after we've instantiated the closure members, but before we
9172 consider adding the conversion op. Also keep any captures
9173 that may have been added during instantiation of the op(). */
9174 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9175 tree tmpl_cap
9176 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9177 args, tf_warning_or_error, NULL_TREE,
9178 false, false);
9179
9180 LAMBDA_EXPR_CAPTURE_LIST (expr)
9181 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9182
9183 maybe_add_lambda_conv_op (type);
9184 }
9185 else
9186 gcc_assert (errorcount);
9187 }
9188
9189 /* Set the file and line number information to whatever is given for
9190 the class itself. This puts error messages involving generated
9191 implicit functions at a predictable point, and the same point
9192 that would be used for non-template classes. */
9193 input_location = DECL_SOURCE_LOCATION (typedecl);
9194
9195 unreverse_member_declarations (type);
9196 finish_struct_1 (type);
9197 TYPE_BEING_DEFINED (type) = 0;
9198
9199 /* We don't instantiate default arguments for member functions. 14.7.1:
9200
9201 The implicit instantiation of a class template specialization causes
9202 the implicit instantiation of the declarations, but not of the
9203 definitions or default arguments, of the class member functions,
9204 member classes, static data members and member templates.... */
9205
9206 /* Some typedefs referenced from within the template code need to be access
9207 checked at template instantiation time, i.e now. These types were
9208 added to the template at parsing time. Let's get those and perform
9209 the access checks then. */
9210 perform_typedefs_access_check (pattern, args);
9211 perform_deferred_access_checks (tf_warning_or_error);
9212 pop_nested_class ();
9213 maximum_field_alignment = saved_maximum_field_alignment;
9214 if (!fn_context)
9215 pop_from_top_level ();
9216 pop_deferring_access_checks ();
9217 pop_tinst_level ();
9218
9219 /* The vtable for a template class can be emitted in any translation
9220 unit in which the class is instantiated. When there is no key
9221 method, however, finish_struct_1 will already have added TYPE to
9222 the keyed_classes list. */
9223 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9224 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9225
9226 return type;
9227 }
9228
9229 /* Wrapper for instantiate_class_template_1. */
9230
9231 tree
9232 instantiate_class_template (tree type)
9233 {
9234 tree ret;
9235 timevar_push (TV_TEMPLATE_INST);
9236 ret = instantiate_class_template_1 (type);
9237 timevar_pop (TV_TEMPLATE_INST);
9238 return ret;
9239 }
9240
9241 static tree
9242 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9243 {
9244 tree r;
9245
9246 if (!t)
9247 r = t;
9248 else if (TYPE_P (t))
9249 r = tsubst (t, args, complain, in_decl);
9250 else
9251 {
9252 if (!(complain & tf_warning))
9253 ++c_inhibit_evaluation_warnings;
9254 r = tsubst_expr (t, args, complain, in_decl,
9255 /*integral_constant_expression_p=*/true);
9256 if (!(complain & tf_warning))
9257 --c_inhibit_evaluation_warnings;
9258 /* Preserve the raw-reference nature of T. */
9259 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9260 && REFERENCE_REF_P (r))
9261 r = TREE_OPERAND (r, 0);
9262 }
9263 return r;
9264 }
9265
9266 /* Given a function parameter pack TMPL_PARM and some function parameters
9267 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9268 and set *SPEC_P to point at the next point in the list. */
9269
9270 static tree
9271 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9272 {
9273 /* Collect all of the extra "packed" parameters into an
9274 argument pack. */
9275 tree parmvec;
9276 tree parmtypevec;
9277 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9278 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9279 tree spec_parm = *spec_p;
9280 int i, len;
9281
9282 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9283 if (tmpl_parm
9284 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9285 break;
9286
9287 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9288 parmvec = make_tree_vec (len);
9289 parmtypevec = make_tree_vec (len);
9290 spec_parm = *spec_p;
9291 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9292 {
9293 TREE_VEC_ELT (parmvec, i) = spec_parm;
9294 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9295 }
9296
9297 /* Build the argument packs. */
9298 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9299 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9300 TREE_TYPE (argpack) = argtypepack;
9301 *spec_p = spec_parm;
9302
9303 return argpack;
9304 }
9305
9306 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9307 NONTYPE_ARGUMENT_PACK. */
9308
9309 static tree
9310 make_fnparm_pack (tree spec_parm)
9311 {
9312 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9313 }
9314
9315 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9316 pack expansion. */
9317
9318 static bool
9319 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9320 {
9321 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9322 if (i >= TREE_VEC_LENGTH (vec))
9323 return false;
9324 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9325 }
9326
9327
9328 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9329
9330 static tree
9331 make_argument_pack_select (tree arg_pack, unsigned index)
9332 {
9333 tree aps = make_node (ARGUMENT_PACK_SELECT);
9334
9335 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9336 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9337
9338 return aps;
9339 }
9340
9341 /* This is a subroutine of tsubst_pack_expansion.
9342
9343 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9344 mechanism to store the (non complete list of) arguments of the
9345 substitution and return a non substituted pack expansion, in order
9346 to wait for when we have enough arguments to really perform the
9347 substitution. */
9348
9349 static bool
9350 use_pack_expansion_extra_args_p (tree parm_packs,
9351 int arg_pack_len,
9352 bool has_empty_arg)
9353 {
9354 /* If one pack has an expansion and another pack has a normal
9355 argument or if one pack has an empty argument and an another
9356 one hasn't then tsubst_pack_expansion cannot perform the
9357 substitution and need to fall back on the
9358 PACK_EXPANSION_EXTRA mechanism. */
9359 if (parm_packs == NULL_TREE)
9360 return false;
9361 else if (has_empty_arg)
9362 return true;
9363
9364 bool has_expansion_arg = false;
9365 for (int i = 0 ; i < arg_pack_len; ++i)
9366 {
9367 bool has_non_expansion_arg = false;
9368 for (tree parm_pack = parm_packs;
9369 parm_pack;
9370 parm_pack = TREE_CHAIN (parm_pack))
9371 {
9372 tree arg = TREE_VALUE (parm_pack);
9373
9374 if (argument_pack_element_is_expansion_p (arg, i))
9375 has_expansion_arg = true;
9376 else
9377 has_non_expansion_arg = true;
9378 }
9379
9380 if (has_expansion_arg && has_non_expansion_arg)
9381 return true;
9382 }
9383 return false;
9384 }
9385
9386 /* [temp.variadic]/6 says that:
9387
9388 The instantiation of a pack expansion [...]
9389 produces a list E1,E2, ..., En, where N is the number of elements
9390 in the pack expansion parameters.
9391
9392 This subroutine of tsubst_pack_expansion produces one of these Ei.
9393
9394 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9395 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9396 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9397 INDEX is the index 'i' of the element Ei to produce. ARGS,
9398 COMPLAIN, and IN_DECL are the same parameters as for the
9399 tsubst_pack_expansion function.
9400
9401 The function returns the resulting Ei upon successful completion,
9402 or error_mark_node.
9403
9404 Note that this function possibly modifies the ARGS parameter, so
9405 it's the responsibility of the caller to restore it. */
9406
9407 static tree
9408 gen_elem_of_pack_expansion_instantiation (tree pattern,
9409 tree parm_packs,
9410 unsigned index,
9411 tree args /* This parm gets
9412 modified. */,
9413 tsubst_flags_t complain,
9414 tree in_decl)
9415 {
9416 tree t;
9417 bool ith_elem_is_expansion = false;
9418
9419 /* For each parameter pack, change the substitution of the parameter
9420 pack to the ith argument in its argument pack, then expand the
9421 pattern. */
9422 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9423 {
9424 tree parm = TREE_PURPOSE (pack);
9425 tree arg_pack = TREE_VALUE (pack);
9426 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9427
9428 ith_elem_is_expansion |=
9429 argument_pack_element_is_expansion_p (arg_pack, index);
9430
9431 /* Select the Ith argument from the pack. */
9432 if (TREE_CODE (parm) == PARM_DECL
9433 || TREE_CODE (parm) == FIELD_DECL)
9434 {
9435 if (index == 0)
9436 {
9437 aps = make_argument_pack_select (arg_pack, index);
9438 mark_used (parm);
9439 register_local_specialization (aps, parm);
9440 }
9441 else
9442 aps = retrieve_local_specialization (parm);
9443 }
9444 else
9445 {
9446 int idx, level;
9447 template_parm_level_and_index (parm, &level, &idx);
9448
9449 if (index == 0)
9450 {
9451 aps = make_argument_pack_select (arg_pack, index);
9452 /* Update the corresponding argument. */
9453 TMPL_ARG (args, level, idx) = aps;
9454 }
9455 else
9456 /* Re-use the ARGUMENT_PACK_SELECT. */
9457 aps = TMPL_ARG (args, level, idx);
9458 }
9459 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9460 }
9461
9462 /* Substitute into the PATTERN with the (possibly altered)
9463 arguments. */
9464 if (!TYPE_P (pattern))
9465 t = tsubst_expr (pattern, args, complain, in_decl,
9466 /*integral_constant_expression_p=*/false);
9467 else
9468 t = tsubst (pattern, args, complain, in_decl);
9469
9470 /* If the Ith argument pack element is a pack expansion, then
9471 the Ith element resulting from the substituting is going to
9472 be a pack expansion as well. */
9473 if (ith_elem_is_expansion)
9474 t = make_pack_expansion (t);
9475
9476 return t;
9477 }
9478
9479 /* Substitute ARGS into T, which is an pack expansion
9480 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9481 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9482 (if only a partial substitution could be performed) or
9483 ERROR_MARK_NODE if there was an error. */
9484 tree
9485 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9486 tree in_decl)
9487 {
9488 tree pattern;
9489 tree pack, packs = NULL_TREE;
9490 bool unsubstituted_packs = false;
9491 int i, len = -1;
9492 tree result;
9493 struct pointer_map_t *saved_local_specializations = NULL;
9494 bool need_local_specializations = false;
9495 int levels;
9496
9497 gcc_assert (PACK_EXPANSION_P (t));
9498 pattern = PACK_EXPANSION_PATTERN (t);
9499
9500 /* Add in any args remembered from an earlier partial instantiation. */
9501 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9502
9503 levels = TMPL_ARGS_DEPTH (args);
9504
9505 /* Determine the argument packs that will instantiate the parameter
9506 packs used in the expansion expression. While we're at it,
9507 compute the number of arguments to be expanded and make sure it
9508 is consistent. */
9509 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9510 pack = TREE_CHAIN (pack))
9511 {
9512 tree parm_pack = TREE_VALUE (pack);
9513 tree arg_pack = NULL_TREE;
9514 tree orig_arg = NULL_TREE;
9515 int level = 0;
9516
9517 if (TREE_CODE (parm_pack) == BASES)
9518 {
9519 if (BASES_DIRECT (parm_pack))
9520 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9521 args, complain, in_decl, false));
9522 else
9523 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9524 args, complain, in_decl, false));
9525 }
9526 if (TREE_CODE (parm_pack) == PARM_DECL)
9527 {
9528 if (PACK_EXPANSION_LOCAL_P (t))
9529 arg_pack = retrieve_local_specialization (parm_pack);
9530 else
9531 {
9532 /* We can't rely on local_specializations for a parameter
9533 name used later in a function declaration (such as in a
9534 late-specified return type). Even if it exists, it might
9535 have the wrong value for a recursive call. Just make a
9536 dummy decl, since it's only used for its type. */
9537 arg_pack = tsubst_decl (parm_pack, args, complain);
9538 if (arg_pack && DECL_PACK_P (arg_pack))
9539 /* Partial instantiation of the parm_pack, we can't build
9540 up an argument pack yet. */
9541 arg_pack = NULL_TREE;
9542 else
9543 arg_pack = make_fnparm_pack (arg_pack);
9544 need_local_specializations = true;
9545 }
9546 }
9547 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9548 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9549 else
9550 {
9551 int idx;
9552 template_parm_level_and_index (parm_pack, &level, &idx);
9553
9554 if (level <= levels)
9555 arg_pack = TMPL_ARG (args, level, idx);
9556 }
9557
9558 orig_arg = arg_pack;
9559 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9560 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9561
9562 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9563 /* This can only happen if we forget to expand an argument
9564 pack somewhere else. Just return an error, silently. */
9565 {
9566 result = make_tree_vec (1);
9567 TREE_VEC_ELT (result, 0) = error_mark_node;
9568 return result;
9569 }
9570
9571 if (arg_pack)
9572 {
9573 int my_len =
9574 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9575
9576 /* Don't bother trying to do a partial substitution with
9577 incomplete packs; we'll try again after deduction. */
9578 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9579 return t;
9580
9581 if (len < 0)
9582 len = my_len;
9583 else if (len != my_len)
9584 {
9585 if (!(complain & tf_error))
9586 /* Fail quietly. */;
9587 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9588 error ("mismatched argument pack lengths while expanding "
9589 "%<%T%>",
9590 pattern);
9591 else
9592 error ("mismatched argument pack lengths while expanding "
9593 "%<%E%>",
9594 pattern);
9595 return error_mark_node;
9596 }
9597
9598 /* Keep track of the parameter packs and their corresponding
9599 argument packs. */
9600 packs = tree_cons (parm_pack, arg_pack, packs);
9601 TREE_TYPE (packs) = orig_arg;
9602 }
9603 else
9604 {
9605 /* We can't substitute for this parameter pack. We use a flag as
9606 well as the missing_level counter because function parameter
9607 packs don't have a level. */
9608 unsubstituted_packs = true;
9609 }
9610 }
9611
9612 /* We cannot expand this expansion expression, because we don't have
9613 all of the argument packs we need. */
9614 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9615 {
9616 /* We got some full packs, but we can't substitute them in until we
9617 have values for all the packs. So remember these until then. */
9618
9619 t = make_pack_expansion (pattern);
9620 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9621 return t;
9622 }
9623 else if (unsubstituted_packs)
9624 {
9625 /* There were no real arguments, we're just replacing a parameter
9626 pack with another version of itself. Substitute into the
9627 pattern and return a PACK_EXPANSION_*. The caller will need to
9628 deal with that. */
9629 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9630 t = tsubst_expr (pattern, args, complain, in_decl,
9631 /*integral_constant_expression_p=*/false);
9632 else
9633 t = tsubst (pattern, args, complain, in_decl);
9634 t = make_pack_expansion (t);
9635 return t;
9636 }
9637
9638 gcc_assert (len >= 0);
9639
9640 if (need_local_specializations)
9641 {
9642 /* We're in a late-specified return type, so create our own local
9643 specializations map; the current map is either NULL or (in the
9644 case of recursive unification) might have bindings that we don't
9645 want to use or alter. */
9646 saved_local_specializations = local_specializations;
9647 local_specializations = pointer_map_create ();
9648 }
9649
9650 /* For each argument in each argument pack, substitute into the
9651 pattern. */
9652 result = make_tree_vec (len);
9653 for (i = 0; i < len; ++i)
9654 {
9655 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9656 i,
9657 args, complain,
9658 in_decl);
9659 TREE_VEC_ELT (result, i) = t;
9660 if (t == error_mark_node)
9661 {
9662 result = error_mark_node;
9663 break;
9664 }
9665 }
9666
9667 /* Update ARGS to restore the substitution from parameter packs to
9668 their argument packs. */
9669 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9670 {
9671 tree parm = TREE_PURPOSE (pack);
9672
9673 if (TREE_CODE (parm) == PARM_DECL
9674 || TREE_CODE (parm) == FIELD_DECL)
9675 register_local_specialization (TREE_TYPE (pack), parm);
9676 else
9677 {
9678 int idx, level;
9679
9680 if (TREE_VALUE (pack) == NULL_TREE)
9681 continue;
9682
9683 template_parm_level_and_index (parm, &level, &idx);
9684
9685 /* Update the corresponding argument. */
9686 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9687 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9688 TREE_TYPE (pack);
9689 else
9690 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9691 }
9692 }
9693
9694 if (need_local_specializations)
9695 {
9696 pointer_map_destroy (local_specializations);
9697 local_specializations = saved_local_specializations;
9698 }
9699
9700 return result;
9701 }
9702
9703 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9704 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9705 parameter packs; all parms generated from a function parameter pack will
9706 have the same DECL_PARM_INDEX. */
9707
9708 tree
9709 get_pattern_parm (tree parm, tree tmpl)
9710 {
9711 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9712 tree patparm;
9713
9714 if (DECL_ARTIFICIAL (parm))
9715 {
9716 for (patparm = DECL_ARGUMENTS (pattern);
9717 patparm; patparm = DECL_CHAIN (patparm))
9718 if (DECL_ARTIFICIAL (patparm)
9719 && DECL_NAME (parm) == DECL_NAME (patparm))
9720 break;
9721 }
9722 else
9723 {
9724 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9725 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9726 gcc_assert (DECL_PARM_INDEX (patparm)
9727 == DECL_PARM_INDEX (parm));
9728 }
9729
9730 return patparm;
9731 }
9732
9733 /* Substitute ARGS into the vector or list of template arguments T. */
9734
9735 static tree
9736 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9737 {
9738 tree orig_t = t;
9739 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9740 tree *elts;
9741
9742 if (t == error_mark_node)
9743 return error_mark_node;
9744
9745 len = TREE_VEC_LENGTH (t);
9746 elts = XALLOCAVEC (tree, len);
9747
9748 for (i = 0; i < len; i++)
9749 {
9750 tree orig_arg = TREE_VEC_ELT (t, i);
9751 tree new_arg;
9752
9753 if (TREE_CODE (orig_arg) == TREE_VEC)
9754 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9755 else if (PACK_EXPANSION_P (orig_arg))
9756 {
9757 /* Substitute into an expansion expression. */
9758 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9759
9760 if (TREE_CODE (new_arg) == TREE_VEC)
9761 /* Add to the expanded length adjustment the number of
9762 expanded arguments. We subtract one from this
9763 measurement, because the argument pack expression
9764 itself is already counted as 1 in
9765 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9766 the argument pack is empty. */
9767 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9768 }
9769 else if (ARGUMENT_PACK_P (orig_arg))
9770 {
9771 /* Substitute into each of the arguments. */
9772 new_arg = TYPE_P (orig_arg)
9773 ? cxx_make_type (TREE_CODE (orig_arg))
9774 : make_node (TREE_CODE (orig_arg));
9775
9776 SET_ARGUMENT_PACK_ARGS (
9777 new_arg,
9778 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9779 args, complain, in_decl));
9780
9781 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9782 new_arg = error_mark_node;
9783
9784 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9785 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9786 complain, in_decl);
9787 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9788
9789 if (TREE_TYPE (new_arg) == error_mark_node)
9790 new_arg = error_mark_node;
9791 }
9792 }
9793 else
9794 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9795
9796 if (new_arg == error_mark_node)
9797 return error_mark_node;
9798
9799 elts[i] = new_arg;
9800 if (new_arg != orig_arg)
9801 need_new = 1;
9802 }
9803
9804 if (!need_new)
9805 return t;
9806
9807 /* Make space for the expanded arguments coming from template
9808 argument packs. */
9809 t = make_tree_vec (len + expanded_len_adjust);
9810 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9811 arguments for a member template.
9812 In that case each TREE_VEC in ORIG_T represents a level of template
9813 arguments, and ORIG_T won't carry any non defaulted argument count.
9814 It will rather be the nested TREE_VECs that will carry one.
9815 In other words, ORIG_T carries a non defaulted argument count only
9816 if it doesn't contain any nested TREE_VEC. */
9817 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9818 {
9819 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9820 count += expanded_len_adjust;
9821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9822 }
9823 for (i = 0, out = 0; i < len; i++)
9824 {
9825 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9826 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9827 && TREE_CODE (elts[i]) == TREE_VEC)
9828 {
9829 int idx;
9830
9831 /* Now expand the template argument pack "in place". */
9832 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9833 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9834 }
9835 else
9836 {
9837 TREE_VEC_ELT (t, out) = elts[i];
9838 out++;
9839 }
9840 }
9841
9842 return t;
9843 }
9844
9845 /* Return the result of substituting ARGS into the template parameters
9846 given by PARMS. If there are m levels of ARGS and m + n levels of
9847 PARMS, then the result will contain n levels of PARMS. For
9848 example, if PARMS is `template <class T> template <class U>
9849 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9850 result will be `template <int*, double, class V>'. */
9851
9852 static tree
9853 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9854 {
9855 tree r = NULL_TREE;
9856 tree* new_parms;
9857
9858 /* When substituting into a template, we must set
9859 PROCESSING_TEMPLATE_DECL as the template parameters may be
9860 dependent if they are based on one-another, and the dependency
9861 predicates are short-circuit outside of templates. */
9862 ++processing_template_decl;
9863
9864 for (new_parms = &r;
9865 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9866 new_parms = &(TREE_CHAIN (*new_parms)),
9867 parms = TREE_CHAIN (parms))
9868 {
9869 tree new_vec =
9870 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9871 int i;
9872
9873 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9874 {
9875 tree tuple;
9876
9877 if (parms == error_mark_node)
9878 continue;
9879
9880 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9881
9882 if (tuple == error_mark_node)
9883 continue;
9884
9885 TREE_VEC_ELT (new_vec, i) =
9886 tsubst_template_parm (tuple, args, complain);
9887 }
9888
9889 *new_parms =
9890 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9891 - TMPL_ARGS_DEPTH (args)),
9892 new_vec, NULL_TREE);
9893 }
9894
9895 --processing_template_decl;
9896
9897 return r;
9898 }
9899
9900 /* Return the result of substituting ARGS into one template parameter
9901 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9902 parameter and which TREE_PURPOSE is the default argument of the
9903 template parameter. */
9904
9905 static tree
9906 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9907 {
9908 tree default_value, parm_decl;
9909
9910 if (args == NULL_TREE
9911 || t == NULL_TREE
9912 || t == error_mark_node)
9913 return t;
9914
9915 gcc_assert (TREE_CODE (t) == TREE_LIST);
9916
9917 default_value = TREE_PURPOSE (t);
9918 parm_decl = TREE_VALUE (t);
9919
9920 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9921 if (TREE_CODE (parm_decl) == PARM_DECL
9922 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9923 parm_decl = error_mark_node;
9924 default_value = tsubst_template_arg (default_value, args,
9925 complain, NULL_TREE);
9926
9927 return build_tree_list (default_value, parm_decl);
9928 }
9929
9930 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9931 type T. If T is not an aggregate or enumeration type, it is
9932 handled as if by tsubst. IN_DECL is as for tsubst. If
9933 ENTERING_SCOPE is nonzero, T is the context for a template which
9934 we are presently tsubst'ing. Return the substituted value. */
9935
9936 static tree
9937 tsubst_aggr_type (tree t,
9938 tree args,
9939 tsubst_flags_t complain,
9940 tree in_decl,
9941 int entering_scope)
9942 {
9943 if (t == NULL_TREE)
9944 return NULL_TREE;
9945
9946 switch (TREE_CODE (t))
9947 {
9948 case RECORD_TYPE:
9949 if (TYPE_PTRMEMFUNC_P (t))
9950 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9951
9952 /* Else fall through. */
9953 case ENUMERAL_TYPE:
9954 case UNION_TYPE:
9955 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9956 {
9957 tree argvec;
9958 tree context;
9959 tree r;
9960 int saved_unevaluated_operand;
9961 int saved_inhibit_evaluation_warnings;
9962
9963 /* In "sizeof(X<I>)" we need to evaluate "I". */
9964 saved_unevaluated_operand = cp_unevaluated_operand;
9965 cp_unevaluated_operand = 0;
9966 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9967 c_inhibit_evaluation_warnings = 0;
9968
9969 /* First, determine the context for the type we are looking
9970 up. */
9971 context = TYPE_CONTEXT (t);
9972 if (context && TYPE_P (context))
9973 {
9974 context = tsubst_aggr_type (context, args, complain,
9975 in_decl, /*entering_scope=*/1);
9976 /* If context is a nested class inside a class template,
9977 it may still need to be instantiated (c++/33959). */
9978 context = complete_type (context);
9979 }
9980
9981 /* Then, figure out what arguments are appropriate for the
9982 type we are trying to find. For example, given:
9983
9984 template <class T> struct S;
9985 template <class T, class U> void f(T, U) { S<U> su; }
9986
9987 and supposing that we are instantiating f<int, double>,
9988 then our ARGS will be {int, double}, but, when looking up
9989 S we only want {double}. */
9990 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9991 complain, in_decl);
9992 if (argvec == error_mark_node)
9993 r = error_mark_node;
9994 else
9995 {
9996 r = lookup_template_class (t, argvec, in_decl, context,
9997 entering_scope, complain);
9998 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9999 }
10000
10001 cp_unevaluated_operand = saved_unevaluated_operand;
10002 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10003
10004 return r;
10005 }
10006 else
10007 /* This is not a template type, so there's nothing to do. */
10008 return t;
10009
10010 default:
10011 return tsubst (t, args, complain, in_decl);
10012 }
10013 }
10014
10015 /* Substitute into the default argument ARG (a default argument for
10016 FN), which has the indicated TYPE. */
10017
10018 tree
10019 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10020 {
10021 tree saved_class_ptr = NULL_TREE;
10022 tree saved_class_ref = NULL_TREE;
10023 int errs = errorcount + sorrycount;
10024
10025 /* This can happen in invalid code. */
10026 if (TREE_CODE (arg) == DEFAULT_ARG)
10027 return arg;
10028
10029 /* This default argument came from a template. Instantiate the
10030 default argument here, not in tsubst. In the case of
10031 something like:
10032
10033 template <class T>
10034 struct S {
10035 static T t();
10036 void f(T = t());
10037 };
10038
10039 we must be careful to do name lookup in the scope of S<T>,
10040 rather than in the current class. */
10041 push_access_scope (fn);
10042 /* The "this" pointer is not valid in a default argument. */
10043 if (cfun)
10044 {
10045 saved_class_ptr = current_class_ptr;
10046 cp_function_chain->x_current_class_ptr = NULL_TREE;
10047 saved_class_ref = current_class_ref;
10048 cp_function_chain->x_current_class_ref = NULL_TREE;
10049 }
10050
10051 push_deferring_access_checks(dk_no_deferred);
10052 /* The default argument expression may cause implicitly defined
10053 member functions to be synthesized, which will result in garbage
10054 collection. We must treat this situation as if we were within
10055 the body of function so as to avoid collecting live data on the
10056 stack. */
10057 ++function_depth;
10058 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10059 complain, NULL_TREE,
10060 /*integral_constant_expression_p=*/false);
10061 --function_depth;
10062 pop_deferring_access_checks();
10063
10064 /* Restore the "this" pointer. */
10065 if (cfun)
10066 {
10067 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10068 cp_function_chain->x_current_class_ref = saved_class_ref;
10069 }
10070
10071 if (errorcount+sorrycount > errs
10072 && (complain & tf_warning_or_error))
10073 inform (input_location,
10074 " when instantiating default argument for call to %D", fn);
10075
10076 /* Make sure the default argument is reasonable. */
10077 arg = check_default_argument (type, arg, complain);
10078
10079 pop_access_scope (fn);
10080
10081 return arg;
10082 }
10083
10084 /* Substitute into all the default arguments for FN. */
10085
10086 static void
10087 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10088 {
10089 tree arg;
10090 tree tmpl_args;
10091
10092 tmpl_args = DECL_TI_ARGS (fn);
10093
10094 /* If this function is not yet instantiated, we certainly don't need
10095 its default arguments. */
10096 if (uses_template_parms (tmpl_args))
10097 return;
10098 /* Don't do this again for clones. */
10099 if (DECL_CLONED_FUNCTION_P (fn))
10100 return;
10101
10102 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10103 arg;
10104 arg = TREE_CHAIN (arg))
10105 if (TREE_PURPOSE (arg))
10106 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10107 TREE_VALUE (arg),
10108 TREE_PURPOSE (arg),
10109 complain);
10110 }
10111
10112 /* Substitute the ARGS into the T, which is a _DECL. Return the
10113 result of the substitution. Issue error and warning messages under
10114 control of COMPLAIN. */
10115
10116 static tree
10117 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10118 {
10119 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10120 location_t saved_loc;
10121 tree r = NULL_TREE;
10122 tree in_decl = t;
10123 hashval_t hash = 0;
10124
10125 /* Set the filename and linenumber to improve error-reporting. */
10126 saved_loc = input_location;
10127 input_location = DECL_SOURCE_LOCATION (t);
10128
10129 switch (TREE_CODE (t))
10130 {
10131 case TEMPLATE_DECL:
10132 {
10133 /* We can get here when processing a member function template,
10134 member class template, or template template parameter. */
10135 tree decl = DECL_TEMPLATE_RESULT (t);
10136 tree spec;
10137 tree tmpl_args;
10138 tree full_args;
10139
10140 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10141 {
10142 /* Template template parameter is treated here. */
10143 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10144 if (new_type == error_mark_node)
10145 RETURN (error_mark_node);
10146 /* If we get a real template back, return it. This can happen in
10147 the context of most_specialized_class. */
10148 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10149 return new_type;
10150
10151 r = copy_decl (t);
10152 DECL_CHAIN (r) = NULL_TREE;
10153 TREE_TYPE (r) = new_type;
10154 DECL_TEMPLATE_RESULT (r)
10155 = build_decl (DECL_SOURCE_LOCATION (decl),
10156 TYPE_DECL, DECL_NAME (decl), new_type);
10157 DECL_TEMPLATE_PARMS (r)
10158 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10159 complain);
10160 TYPE_NAME (new_type) = r;
10161 break;
10162 }
10163
10164 /* We might already have an instance of this template.
10165 The ARGS are for the surrounding class type, so the
10166 full args contain the tsubst'd args for the context,
10167 plus the innermost args from the template decl. */
10168 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10169 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10170 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10171 /* Because this is a template, the arguments will still be
10172 dependent, even after substitution. If
10173 PROCESSING_TEMPLATE_DECL is not set, the dependency
10174 predicates will short-circuit. */
10175 ++processing_template_decl;
10176 full_args = tsubst_template_args (tmpl_args, args,
10177 complain, in_decl);
10178 --processing_template_decl;
10179 if (full_args == error_mark_node)
10180 RETURN (error_mark_node);
10181
10182 /* If this is a default template template argument,
10183 tsubst might not have changed anything. */
10184 if (full_args == tmpl_args)
10185 RETURN (t);
10186
10187 hash = hash_tmpl_and_args (t, full_args);
10188 spec = retrieve_specialization (t, full_args, hash);
10189 if (spec != NULL_TREE)
10190 {
10191 r = spec;
10192 break;
10193 }
10194
10195 /* Make a new template decl. It will be similar to the
10196 original, but will record the current template arguments.
10197 We also create a new function declaration, which is just
10198 like the old one, but points to this new template, rather
10199 than the old one. */
10200 r = copy_decl (t);
10201 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10202 DECL_CHAIN (r) = NULL_TREE;
10203
10204 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10205
10206 if (TREE_CODE (decl) == TYPE_DECL
10207 && !TYPE_DECL_ALIAS_P (decl))
10208 {
10209 tree new_type;
10210 ++processing_template_decl;
10211 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10212 --processing_template_decl;
10213 if (new_type == error_mark_node)
10214 RETURN (error_mark_node);
10215
10216 TREE_TYPE (r) = new_type;
10217 /* For a partial specialization, we need to keep pointing to
10218 the primary template. */
10219 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10220 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10221 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10222 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10223 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10224 }
10225 else
10226 {
10227 tree new_decl;
10228 ++processing_template_decl;
10229 new_decl = tsubst (decl, args, complain, in_decl);
10230 --processing_template_decl;
10231 if (new_decl == error_mark_node)
10232 RETURN (error_mark_node);
10233
10234 DECL_TEMPLATE_RESULT (r) = new_decl;
10235 DECL_TI_TEMPLATE (new_decl) = r;
10236 TREE_TYPE (r) = TREE_TYPE (new_decl);
10237 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10238 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10239 }
10240
10241 SET_DECL_IMPLICIT_INSTANTIATION (r);
10242 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10243 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10244
10245 /* The template parameters for this new template are all the
10246 template parameters for the old template, except the
10247 outermost level of parameters. */
10248 DECL_TEMPLATE_PARMS (r)
10249 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10250 complain);
10251
10252 if (PRIMARY_TEMPLATE_P (t))
10253 DECL_PRIMARY_TEMPLATE (r) = r;
10254
10255 if (TREE_CODE (decl) != TYPE_DECL)
10256 /* Record this non-type partial instantiation. */
10257 register_specialization (r, t,
10258 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10259 false, hash);
10260 }
10261 break;
10262
10263 case FUNCTION_DECL:
10264 {
10265 tree ctx;
10266 tree argvec = NULL_TREE;
10267 tree *friends;
10268 tree gen_tmpl;
10269 tree type;
10270 int member;
10271 int args_depth;
10272 int parms_depth;
10273
10274 /* Nobody should be tsubst'ing into non-template functions. */
10275 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10276
10277 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10278 {
10279 tree spec;
10280 bool dependent_p;
10281
10282 /* If T is not dependent, just return it. We have to
10283 increment PROCESSING_TEMPLATE_DECL because
10284 value_dependent_expression_p assumes that nothing is
10285 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10286 ++processing_template_decl;
10287 dependent_p = value_dependent_expression_p (t);
10288 --processing_template_decl;
10289 if (!dependent_p)
10290 RETURN (t);
10291
10292 /* Calculate the most general template of which R is a
10293 specialization, and the complete set of arguments used to
10294 specialize R. */
10295 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10296 argvec = tsubst_template_args (DECL_TI_ARGS
10297 (DECL_TEMPLATE_RESULT
10298 (DECL_TI_TEMPLATE (t))),
10299 args, complain, in_decl);
10300 if (argvec == error_mark_node)
10301 RETURN (error_mark_node);
10302
10303 /* Check to see if we already have this specialization. */
10304 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10305 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10306
10307 if (spec)
10308 {
10309 r = spec;
10310 break;
10311 }
10312
10313 /* We can see more levels of arguments than parameters if
10314 there was a specialization of a member template, like
10315 this:
10316
10317 template <class T> struct S { template <class U> void f(); }
10318 template <> template <class U> void S<int>::f(U);
10319
10320 Here, we'll be substituting into the specialization,
10321 because that's where we can find the code we actually
10322 want to generate, but we'll have enough arguments for
10323 the most general template.
10324
10325 We also deal with the peculiar case:
10326
10327 template <class T> struct S {
10328 template <class U> friend void f();
10329 };
10330 template <class U> void f() {}
10331 template S<int>;
10332 template void f<double>();
10333
10334 Here, the ARGS for the instantiation of will be {int,
10335 double}. But, we only need as many ARGS as there are
10336 levels of template parameters in CODE_PATTERN. We are
10337 careful not to get fooled into reducing the ARGS in
10338 situations like:
10339
10340 template <class T> struct S { template <class U> void f(U); }
10341 template <class T> template <> void S<T>::f(int) {}
10342
10343 which we can spot because the pattern will be a
10344 specialization in this case. */
10345 args_depth = TMPL_ARGS_DEPTH (args);
10346 parms_depth =
10347 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10348 if (args_depth > parms_depth
10349 && !DECL_TEMPLATE_SPECIALIZATION (t))
10350 args = get_innermost_template_args (args, parms_depth);
10351 }
10352 else
10353 {
10354 /* This special case arises when we have something like this:
10355
10356 template <class T> struct S {
10357 friend void f<int>(int, double);
10358 };
10359
10360 Here, the DECL_TI_TEMPLATE for the friend declaration
10361 will be an IDENTIFIER_NODE. We are being called from
10362 tsubst_friend_function, and we want only to create a
10363 new decl (R) with appropriate types so that we can call
10364 determine_specialization. */
10365 gen_tmpl = NULL_TREE;
10366 }
10367
10368 if (DECL_CLASS_SCOPE_P (t))
10369 {
10370 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10371 member = 2;
10372 else
10373 member = 1;
10374 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10375 complain, t, /*entering_scope=*/1);
10376 }
10377 else
10378 {
10379 member = 0;
10380 ctx = DECL_CONTEXT (t);
10381 }
10382 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10383 if (type == error_mark_node)
10384 RETURN (error_mark_node);
10385
10386 /* If we hit excessive deduction depth, the type is bogus even if
10387 it isn't error_mark_node, so don't build a decl. */
10388 if (excessive_deduction_depth)
10389 RETURN (error_mark_node);
10390
10391 /* We do NOT check for matching decls pushed separately at this
10392 point, as they may not represent instantiations of this
10393 template, and in any case are considered separate under the
10394 discrete model. */
10395 r = copy_decl (t);
10396 DECL_USE_TEMPLATE (r) = 0;
10397 TREE_TYPE (r) = type;
10398 /* Clear out the mangled name and RTL for the instantiation. */
10399 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10400 SET_DECL_RTL (r, NULL);
10401 /* Leave DECL_INITIAL set on deleted instantiations. */
10402 if (!DECL_DELETED_FN (r))
10403 DECL_INITIAL (r) = NULL_TREE;
10404 DECL_CONTEXT (r) = ctx;
10405
10406 /* OpenMP UDRs have the only argument a reference to the declared
10407 type. We want to diagnose if the declared type is a reference,
10408 which is invalid, but as references to references are usually
10409 quietly merged, diagnose it here. */
10410 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10411 {
10412 tree argtype
10413 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10414 argtype = tsubst (argtype, args, complain, in_decl);
10415 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10416 error_at (DECL_SOURCE_LOCATION (t),
10417 "reference type %qT in "
10418 "%<#pragma omp declare reduction%>", argtype);
10419 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10420 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10421 argtype);
10422 }
10423
10424 if (member && DECL_CONV_FN_P (r))
10425 /* Type-conversion operator. Reconstruct the name, in
10426 case it's the name of one of the template's parameters. */
10427 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10428
10429 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10430 complain, t);
10431 DECL_RESULT (r) = NULL_TREE;
10432
10433 TREE_STATIC (r) = 0;
10434 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10435 DECL_EXTERNAL (r) = 1;
10436 /* If this is an instantiation of a function with internal
10437 linkage, we already know what object file linkage will be
10438 assigned to the instantiation. */
10439 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10440 DECL_DEFER_OUTPUT (r) = 0;
10441 DECL_CHAIN (r) = NULL_TREE;
10442 DECL_PENDING_INLINE_INFO (r) = 0;
10443 DECL_PENDING_INLINE_P (r) = 0;
10444 DECL_SAVED_TREE (r) = NULL_TREE;
10445 DECL_STRUCT_FUNCTION (r) = NULL;
10446 TREE_USED (r) = 0;
10447 /* We'll re-clone as appropriate in instantiate_template. */
10448 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10449
10450 /* If we aren't complaining now, return on error before we register
10451 the specialization so that we'll complain eventually. */
10452 if ((complain & tf_error) == 0
10453 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10454 && !grok_op_properties (r, /*complain=*/false))
10455 RETURN (error_mark_node);
10456
10457 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10458 this in the special friend case mentioned above where
10459 GEN_TMPL is NULL. */
10460 if (gen_tmpl)
10461 {
10462 DECL_TEMPLATE_INFO (r)
10463 = build_template_info (gen_tmpl, argvec);
10464 SET_DECL_IMPLICIT_INSTANTIATION (r);
10465
10466 tree new_r
10467 = register_specialization (r, gen_tmpl, argvec, false, hash);
10468 if (new_r != r)
10469 /* We instantiated this while substituting into
10470 the type earlier (template/friend54.C). */
10471 RETURN (new_r);
10472
10473 /* We're not supposed to instantiate default arguments
10474 until they are called, for a template. But, for a
10475 declaration like:
10476
10477 template <class T> void f ()
10478 { extern void g(int i = T()); }
10479
10480 we should do the substitution when the template is
10481 instantiated. We handle the member function case in
10482 instantiate_class_template since the default arguments
10483 might refer to other members of the class. */
10484 if (!member
10485 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10486 && !uses_template_parms (argvec))
10487 tsubst_default_arguments (r, complain);
10488 }
10489 else
10490 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10491
10492 /* Copy the list of befriending classes. */
10493 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10494 *friends;
10495 friends = &TREE_CHAIN (*friends))
10496 {
10497 *friends = copy_node (*friends);
10498 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10499 args, complain,
10500 in_decl);
10501 }
10502
10503 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10504 {
10505 maybe_retrofit_in_chrg (r);
10506 if (DECL_CONSTRUCTOR_P (r))
10507 grok_ctor_properties (ctx, r);
10508 if (DECL_INHERITED_CTOR_BASE (r))
10509 deduce_inheriting_ctor (r);
10510 /* If this is an instantiation of a member template, clone it.
10511 If it isn't, that'll be handled by
10512 clone_constructors_and_destructors. */
10513 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10514 clone_function_decl (r, /*update_method_vec_p=*/0);
10515 }
10516 else if ((complain & tf_error) != 0
10517 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10518 && !grok_op_properties (r, /*complain=*/true))
10519 RETURN (error_mark_node);
10520
10521 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10522 SET_DECL_FRIEND_CONTEXT (r,
10523 tsubst (DECL_FRIEND_CONTEXT (t),
10524 args, complain, in_decl));
10525
10526 /* Possibly limit visibility based on template args. */
10527 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10528 if (DECL_VISIBILITY_SPECIFIED (t))
10529 {
10530 DECL_VISIBILITY_SPECIFIED (r) = 0;
10531 DECL_ATTRIBUTES (r)
10532 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10533 }
10534 determine_visibility (r);
10535 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10536 && !processing_template_decl)
10537 defaulted_late_check (r);
10538
10539 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10540 args, complain, in_decl);
10541 }
10542 break;
10543
10544 case PARM_DECL:
10545 {
10546 tree type = NULL_TREE;
10547 int i, len = 1;
10548 tree expanded_types = NULL_TREE;
10549 tree prev_r = NULL_TREE;
10550 tree first_r = NULL_TREE;
10551
10552 if (DECL_PACK_P (t))
10553 {
10554 /* If there is a local specialization that isn't a
10555 parameter pack, it means that we're doing a "simple"
10556 substitution from inside tsubst_pack_expansion. Just
10557 return the local specialization (which will be a single
10558 parm). */
10559 tree spec = retrieve_local_specialization (t);
10560 if (spec
10561 && TREE_CODE (spec) == PARM_DECL
10562 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10563 RETURN (spec);
10564
10565 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10566 the parameters in this function parameter pack. */
10567 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10568 complain, in_decl);
10569 if (TREE_CODE (expanded_types) == TREE_VEC)
10570 {
10571 len = TREE_VEC_LENGTH (expanded_types);
10572
10573 /* Zero-length parameter packs are boring. Just substitute
10574 into the chain. */
10575 if (len == 0)
10576 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10577 TREE_CHAIN (t)));
10578 }
10579 else
10580 {
10581 /* All we did was update the type. Make a note of that. */
10582 type = expanded_types;
10583 expanded_types = NULL_TREE;
10584 }
10585 }
10586
10587 /* Loop through all of the parameters we'll build. When T is
10588 a function parameter pack, LEN is the number of expanded
10589 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10590 r = NULL_TREE;
10591 for (i = 0; i < len; ++i)
10592 {
10593 prev_r = r;
10594 r = copy_node (t);
10595 if (DECL_TEMPLATE_PARM_P (t))
10596 SET_DECL_TEMPLATE_PARM_P (r);
10597
10598 if (expanded_types)
10599 /* We're on the Ith parameter of the function parameter
10600 pack. */
10601 {
10602 /* Get the Ith type. */
10603 type = TREE_VEC_ELT (expanded_types, i);
10604
10605 /* Rename the parameter to include the index. */
10606 DECL_NAME (r)
10607 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10608 }
10609 else if (!type)
10610 /* We're dealing with a normal parameter. */
10611 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10612
10613 type = type_decays_to (type);
10614 TREE_TYPE (r) = type;
10615 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10616
10617 if (DECL_INITIAL (r))
10618 {
10619 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10620 DECL_INITIAL (r) = TREE_TYPE (r);
10621 else
10622 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10623 complain, in_decl);
10624 }
10625
10626 DECL_CONTEXT (r) = NULL_TREE;
10627
10628 if (!DECL_TEMPLATE_PARM_P (r))
10629 DECL_ARG_TYPE (r) = type_passed_as (type);
10630
10631 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10632 args, complain, in_decl);
10633
10634 /* Keep track of the first new parameter we
10635 generate. That's what will be returned to the
10636 caller. */
10637 if (!first_r)
10638 first_r = r;
10639
10640 /* Build a proper chain of parameters when substituting
10641 into a function parameter pack. */
10642 if (prev_r)
10643 DECL_CHAIN (prev_r) = r;
10644 }
10645
10646 /* If cp_unevaluated_operand is set, we're just looking for a
10647 single dummy parameter, so don't keep going. */
10648 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10649 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10650 complain, DECL_CHAIN (t));
10651
10652 /* FIRST_R contains the start of the chain we've built. */
10653 r = first_r;
10654 }
10655 break;
10656
10657 case FIELD_DECL:
10658 {
10659 tree type = NULL_TREE;
10660 tree vec = NULL_TREE;
10661 tree expanded_types = NULL_TREE;
10662 int len = 1;
10663
10664 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10665 {
10666 /* This field is a lambda capture pack. Return a TREE_VEC of
10667 the expanded fields to instantiate_class_template_1 and
10668 store them in the specializations hash table as a
10669 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10670 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10671 complain, in_decl);
10672 if (TREE_CODE (expanded_types) == TREE_VEC)
10673 {
10674 len = TREE_VEC_LENGTH (expanded_types);
10675 vec = make_tree_vec (len);
10676 }
10677 else
10678 {
10679 /* All we did was update the type. Make a note of that. */
10680 type = expanded_types;
10681 expanded_types = NULL_TREE;
10682 }
10683 }
10684
10685 for (int i = 0; i < len; ++i)
10686 {
10687 r = copy_decl (t);
10688 if (expanded_types)
10689 {
10690 type = TREE_VEC_ELT (expanded_types, i);
10691 DECL_NAME (r)
10692 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10693 }
10694 else if (!type)
10695 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10696
10697 if (type == error_mark_node)
10698 RETURN (error_mark_node);
10699 TREE_TYPE (r) = type;
10700 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10701
10702 if (DECL_C_BIT_FIELD (r))
10703 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10704 non-bit-fields DECL_INITIAL is a non-static data member
10705 initializer, which gets deferred instantiation. */
10706 DECL_INITIAL (r)
10707 = tsubst_expr (DECL_INITIAL (t), args,
10708 complain, in_decl,
10709 /*integral_constant_expression_p=*/true);
10710 else if (DECL_INITIAL (t))
10711 {
10712 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10713 NSDMI in perform_member_init. Still set DECL_INITIAL
10714 so that we know there is one. */
10715 DECL_INITIAL (r) = void_zero_node;
10716 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10717 retrofit_lang_decl (r);
10718 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10719 }
10720 /* We don't have to set DECL_CONTEXT here; it is set by
10721 finish_member_declaration. */
10722 DECL_CHAIN (r) = NULL_TREE;
10723
10724 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10725 args, complain, in_decl);
10726
10727 if (vec)
10728 TREE_VEC_ELT (vec, i) = r;
10729 }
10730
10731 if (vec)
10732 {
10733 r = vec;
10734 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10735 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10736 SET_ARGUMENT_PACK_ARGS (pack, vec);
10737 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10738 TREE_TYPE (pack) = tpack;
10739 register_specialization (pack, t, args, false, 0);
10740 }
10741 }
10742 break;
10743
10744 case USING_DECL:
10745 /* We reach here only for member using decls. We also need to check
10746 uses_template_parms because DECL_DEPENDENT_P is not set for a
10747 using-declaration that designates a member of the current
10748 instantiation (c++/53549). */
10749 if (DECL_DEPENDENT_P (t)
10750 || uses_template_parms (USING_DECL_SCOPE (t)))
10751 {
10752 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10753 complain, in_decl);
10754 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10755 r = do_class_using_decl (inst_scope, name);
10756 if (!r)
10757 r = error_mark_node;
10758 else
10759 {
10760 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10761 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10762 }
10763 }
10764 else
10765 {
10766 r = copy_node (t);
10767 DECL_CHAIN (r) = NULL_TREE;
10768 }
10769 break;
10770
10771 case TYPE_DECL:
10772 case VAR_DECL:
10773 {
10774 tree argvec = NULL_TREE;
10775 tree gen_tmpl = NULL_TREE;
10776 tree spec;
10777 tree tmpl = NULL_TREE;
10778 tree ctx;
10779 tree type = NULL_TREE;
10780 bool local_p;
10781
10782 if (TREE_CODE (t) == TYPE_DECL
10783 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10784 {
10785 /* If this is the canonical decl, we don't have to
10786 mess with instantiations, and often we can't (for
10787 typename, template type parms and such). Note that
10788 TYPE_NAME is not correct for the above test if
10789 we've copied the type for a typedef. */
10790 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10791 if (type == error_mark_node)
10792 RETURN (error_mark_node);
10793 r = TYPE_NAME (type);
10794 break;
10795 }
10796
10797 /* Check to see if we already have the specialization we
10798 need. */
10799 spec = NULL_TREE;
10800 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10801 {
10802 /* T is a static data member or namespace-scope entity.
10803 We have to substitute into namespace-scope variables
10804 (even though such entities are never templates) because
10805 of cases like:
10806
10807 template <class T> void f() { extern T t; }
10808
10809 where the entity referenced is not known until
10810 instantiation time. */
10811 local_p = false;
10812 ctx = DECL_CONTEXT (t);
10813 if (DECL_CLASS_SCOPE_P (t))
10814 {
10815 ctx = tsubst_aggr_type (ctx, args,
10816 complain,
10817 in_decl, /*entering_scope=*/1);
10818 /* If CTX is unchanged, then T is in fact the
10819 specialization we want. That situation occurs when
10820 referencing a static data member within in its own
10821 class. We can use pointer equality, rather than
10822 same_type_p, because DECL_CONTEXT is always
10823 canonical... */
10824 if (ctx == DECL_CONTEXT (t)
10825 && (TREE_CODE (t) != TYPE_DECL
10826 /* ... unless T is a member template; in which
10827 case our caller can be willing to create a
10828 specialization of that template represented
10829 by T. */
10830 || !(DECL_TI_TEMPLATE (t)
10831 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10832 spec = t;
10833 }
10834
10835 if (!spec)
10836 {
10837 tmpl = DECL_TI_TEMPLATE (t);
10838 gen_tmpl = most_general_template (tmpl);
10839 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10840 if (argvec == error_mark_node)
10841 RETURN (error_mark_node);
10842 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10843 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10844 }
10845 }
10846 else
10847 {
10848 /* A local variable. */
10849 local_p = true;
10850 /* Subsequent calls to pushdecl will fill this in. */
10851 ctx = NULL_TREE;
10852 spec = retrieve_local_specialization (t);
10853 }
10854 /* If we already have the specialization we need, there is
10855 nothing more to do. */
10856 if (spec)
10857 {
10858 r = spec;
10859 break;
10860 }
10861
10862 /* Create a new node for the specialization we need. */
10863 r = copy_decl (t);
10864 if (type == NULL_TREE)
10865 {
10866 if (is_typedef_decl (t))
10867 type = DECL_ORIGINAL_TYPE (t);
10868 else
10869 type = TREE_TYPE (t);
10870 if (VAR_P (t)
10871 && VAR_HAD_UNKNOWN_BOUND (t)
10872 && type != error_mark_node)
10873 type = strip_array_domain (type);
10874 type = tsubst (type, args, complain, in_decl);
10875 }
10876 if (VAR_P (r))
10877 {
10878 /* Even if the original location is out of scope, the
10879 newly substituted one is not. */
10880 DECL_DEAD_FOR_LOCAL (r) = 0;
10881 DECL_INITIALIZED_P (r) = 0;
10882 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10883 if (type == error_mark_node)
10884 RETURN (error_mark_node);
10885 if (TREE_CODE (type) == FUNCTION_TYPE)
10886 {
10887 /* It may seem that this case cannot occur, since:
10888
10889 typedef void f();
10890 void g() { f x; }
10891
10892 declares a function, not a variable. However:
10893
10894 typedef void f();
10895 template <typename T> void g() { T t; }
10896 template void g<f>();
10897
10898 is an attempt to declare a variable with function
10899 type. */
10900 error ("variable %qD has function type",
10901 /* R is not yet sufficiently initialized, so we
10902 just use its name. */
10903 DECL_NAME (r));
10904 RETURN (error_mark_node);
10905 }
10906 type = complete_type (type);
10907 /* Wait until cp_finish_decl to set this again, to handle
10908 circular dependency (template/instantiate6.C). */
10909 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10910 type = check_var_type (DECL_NAME (r), type);
10911
10912 if (DECL_HAS_VALUE_EXPR_P (t))
10913 {
10914 tree ve = DECL_VALUE_EXPR (t);
10915 ve = tsubst_expr (ve, args, complain, in_decl,
10916 /*constant_expression_p=*/false);
10917 if (REFERENCE_REF_P (ve))
10918 {
10919 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10920 ve = TREE_OPERAND (ve, 0);
10921 }
10922 SET_DECL_VALUE_EXPR (r, ve);
10923 }
10924 }
10925 else if (DECL_SELF_REFERENCE_P (t))
10926 SET_DECL_SELF_REFERENCE_P (r);
10927 TREE_TYPE (r) = type;
10928 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10929 DECL_CONTEXT (r) = ctx;
10930 /* Clear out the mangled name and RTL for the instantiation. */
10931 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10932 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10933 SET_DECL_RTL (r, NULL);
10934 /* The initializer must not be expanded until it is required;
10935 see [temp.inst]. */
10936 DECL_INITIAL (r) = NULL_TREE;
10937 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10938 SET_DECL_RTL (r, NULL);
10939 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10940 if (VAR_P (r))
10941 {
10942 /* Possibly limit visibility based on template args. */
10943 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10944 if (DECL_VISIBILITY_SPECIFIED (t))
10945 {
10946 DECL_VISIBILITY_SPECIFIED (r) = 0;
10947 DECL_ATTRIBUTES (r)
10948 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10949 }
10950 determine_visibility (r);
10951 }
10952
10953 if (!local_p)
10954 {
10955 /* A static data member declaration is always marked
10956 external when it is declared in-class, even if an
10957 initializer is present. We mimic the non-template
10958 processing here. */
10959 DECL_EXTERNAL (r) = 1;
10960
10961 register_specialization (r, gen_tmpl, argvec, false, hash);
10962 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10963 SET_DECL_IMPLICIT_INSTANTIATION (r);
10964 }
10965 else if (cp_unevaluated_operand)
10966 gcc_unreachable ();
10967 else
10968 register_local_specialization (r, t);
10969
10970 DECL_CHAIN (r) = NULL_TREE;
10971
10972 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10973 /*flags=*/0,
10974 args, complain, in_decl);
10975
10976 /* Preserve a typedef that names a type. */
10977 if (is_typedef_decl (r))
10978 {
10979 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10980 set_underlying_type (r);
10981 }
10982
10983 layout_decl (r, 0);
10984 }
10985 break;
10986
10987 default:
10988 gcc_unreachable ();
10989 }
10990 #undef RETURN
10991
10992 out:
10993 /* Restore the file and line information. */
10994 input_location = saved_loc;
10995
10996 return r;
10997 }
10998
10999 /* Substitute into the ARG_TYPES of a function type.
11000 If END is a TREE_CHAIN, leave it and any following types
11001 un-substituted. */
11002
11003 static tree
11004 tsubst_arg_types (tree arg_types,
11005 tree args,
11006 tree end,
11007 tsubst_flags_t complain,
11008 tree in_decl)
11009 {
11010 tree remaining_arg_types;
11011 tree type = NULL_TREE;
11012 int i = 1;
11013 tree expanded_args = NULL_TREE;
11014 tree default_arg;
11015
11016 if (!arg_types || arg_types == void_list_node || arg_types == end)
11017 return arg_types;
11018
11019 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11020 args, end, complain, in_decl);
11021 if (remaining_arg_types == error_mark_node)
11022 return error_mark_node;
11023
11024 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11025 {
11026 /* For a pack expansion, perform substitution on the
11027 entire expression. Later on, we'll handle the arguments
11028 one-by-one. */
11029 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11030 args, complain, in_decl);
11031
11032 if (TREE_CODE (expanded_args) == TREE_VEC)
11033 /* So that we'll spin through the parameters, one by one. */
11034 i = TREE_VEC_LENGTH (expanded_args);
11035 else
11036 {
11037 /* We only partially substituted into the parameter
11038 pack. Our type is TYPE_PACK_EXPANSION. */
11039 type = expanded_args;
11040 expanded_args = NULL_TREE;
11041 }
11042 }
11043
11044 while (i > 0) {
11045 --i;
11046
11047 if (expanded_args)
11048 type = TREE_VEC_ELT (expanded_args, i);
11049 else if (!type)
11050 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11051
11052 if (type == error_mark_node)
11053 return error_mark_node;
11054 if (VOID_TYPE_P (type))
11055 {
11056 if (complain & tf_error)
11057 {
11058 error ("invalid parameter type %qT", type);
11059 if (in_decl)
11060 error ("in declaration %q+D", in_decl);
11061 }
11062 return error_mark_node;
11063 }
11064 /* DR 657. */
11065 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11066 return error_mark_node;
11067
11068 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11069 top-level qualifiers as required. */
11070 type = cv_unqualified (type_decays_to (type));
11071
11072 /* We do not substitute into default arguments here. The standard
11073 mandates that they be instantiated only when needed, which is
11074 done in build_over_call. */
11075 default_arg = TREE_PURPOSE (arg_types);
11076
11077 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11078 {
11079 /* We've instantiated a template before its default arguments
11080 have been parsed. This can happen for a nested template
11081 class, and is not an error unless we require the default
11082 argument in a call of this function. */
11083 remaining_arg_types =
11084 tree_cons (default_arg, type, remaining_arg_types);
11085 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11086 }
11087 else
11088 remaining_arg_types =
11089 hash_tree_cons (default_arg, type, remaining_arg_types);
11090 }
11091
11092 return remaining_arg_types;
11093 }
11094
11095 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11096 *not* handle the exception-specification for FNTYPE, because the
11097 initial substitution of explicitly provided template parameters
11098 during argument deduction forbids substitution into the
11099 exception-specification:
11100
11101 [temp.deduct]
11102
11103 All references in the function type of the function template to the
11104 corresponding template parameters are replaced by the specified tem-
11105 plate argument values. If a substitution in a template parameter or
11106 in the function type of the function template results in an invalid
11107 type, type deduction fails. [Note: The equivalent substitution in
11108 exception specifications is done only when the function is instanti-
11109 ated, at which point a program is ill-formed if the substitution
11110 results in an invalid type.] */
11111
11112 static tree
11113 tsubst_function_type (tree t,
11114 tree args,
11115 tsubst_flags_t complain,
11116 tree in_decl)
11117 {
11118 tree return_type;
11119 tree arg_types;
11120 tree fntype;
11121
11122 /* The TYPE_CONTEXT is not used for function/method types. */
11123 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11124
11125 /* Substitute the return type. */
11126 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11127 if (return_type == error_mark_node)
11128 return error_mark_node;
11129 /* DR 486 clarifies that creation of a function type with an
11130 invalid return type is a deduction failure. */
11131 if (TREE_CODE (return_type) == ARRAY_TYPE
11132 || TREE_CODE (return_type) == FUNCTION_TYPE)
11133 {
11134 if (complain & tf_error)
11135 {
11136 if (TREE_CODE (return_type) == ARRAY_TYPE)
11137 error ("function returning an array");
11138 else
11139 error ("function returning a function");
11140 }
11141 return error_mark_node;
11142 }
11143 /* And DR 657. */
11144 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11145 return error_mark_node;
11146
11147 /* Substitute the argument types. */
11148 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11149 complain, in_decl);
11150 if (arg_types == error_mark_node)
11151 return error_mark_node;
11152
11153 /* Construct a new type node and return it. */
11154 if (TREE_CODE (t) == FUNCTION_TYPE)
11155 {
11156 fntype = build_function_type (return_type, arg_types);
11157 fntype = apply_memfn_quals (fntype,
11158 type_memfn_quals (t),
11159 type_memfn_rqual (t));
11160 }
11161 else
11162 {
11163 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11164 if (! MAYBE_CLASS_TYPE_P (r))
11165 {
11166 /* [temp.deduct]
11167
11168 Type deduction may fail for any of the following
11169 reasons:
11170
11171 -- Attempting to create "pointer to member of T" when T
11172 is not a class type. */
11173 if (complain & tf_error)
11174 error ("creating pointer to member function of non-class type %qT",
11175 r);
11176 return error_mark_node;
11177 }
11178
11179 fntype = build_method_type_directly (r, return_type,
11180 TREE_CHAIN (arg_types));
11181 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11182 }
11183 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11184
11185 return fntype;
11186 }
11187
11188 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11189 ARGS into that specification, and return the substituted
11190 specification. If there is no specification, return NULL_TREE. */
11191
11192 static tree
11193 tsubst_exception_specification (tree fntype,
11194 tree args,
11195 tsubst_flags_t complain,
11196 tree in_decl,
11197 bool defer_ok)
11198 {
11199 tree specs;
11200 tree new_specs;
11201
11202 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11203 new_specs = NULL_TREE;
11204 if (specs && TREE_PURPOSE (specs))
11205 {
11206 /* A noexcept-specifier. */
11207 tree expr = TREE_PURPOSE (specs);
11208 if (TREE_CODE (expr) == INTEGER_CST)
11209 new_specs = expr;
11210 else if (defer_ok)
11211 {
11212 /* Defer instantiation of noexcept-specifiers to avoid
11213 excessive instantiations (c++/49107). */
11214 new_specs = make_node (DEFERRED_NOEXCEPT);
11215 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11216 {
11217 /* We already partially instantiated this member template,
11218 so combine the new args with the old. */
11219 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11220 = DEFERRED_NOEXCEPT_PATTERN (expr);
11221 DEFERRED_NOEXCEPT_ARGS (new_specs)
11222 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11223 }
11224 else
11225 {
11226 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11227 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11228 }
11229 }
11230 else
11231 new_specs = tsubst_copy_and_build
11232 (expr, args, complain, in_decl, /*function_p=*/false,
11233 /*integral_constant_expression_p=*/true);
11234 new_specs = build_noexcept_spec (new_specs, complain);
11235 }
11236 else if (specs)
11237 {
11238 if (! TREE_VALUE (specs))
11239 new_specs = specs;
11240 else
11241 while (specs)
11242 {
11243 tree spec;
11244 int i, len = 1;
11245 tree expanded_specs = NULL_TREE;
11246
11247 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11248 {
11249 /* Expand the pack expansion type. */
11250 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11251 args, complain,
11252 in_decl);
11253
11254 if (expanded_specs == error_mark_node)
11255 return error_mark_node;
11256 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11257 len = TREE_VEC_LENGTH (expanded_specs);
11258 else
11259 {
11260 /* We're substituting into a member template, so
11261 we got a TYPE_PACK_EXPANSION back. Add that
11262 expansion and move on. */
11263 gcc_assert (TREE_CODE (expanded_specs)
11264 == TYPE_PACK_EXPANSION);
11265 new_specs = add_exception_specifier (new_specs,
11266 expanded_specs,
11267 complain);
11268 specs = TREE_CHAIN (specs);
11269 continue;
11270 }
11271 }
11272
11273 for (i = 0; i < len; ++i)
11274 {
11275 if (expanded_specs)
11276 spec = TREE_VEC_ELT (expanded_specs, i);
11277 else
11278 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11279 if (spec == error_mark_node)
11280 return spec;
11281 new_specs = add_exception_specifier (new_specs, spec,
11282 complain);
11283 }
11284
11285 specs = TREE_CHAIN (specs);
11286 }
11287 }
11288 return new_specs;
11289 }
11290
11291 /* Take the tree structure T and replace template parameters used
11292 therein with the argument vector ARGS. IN_DECL is an associated
11293 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11294 Issue error and warning messages under control of COMPLAIN. Note
11295 that we must be relatively non-tolerant of extensions here, in
11296 order to preserve conformance; if we allow substitutions that
11297 should not be allowed, we may allow argument deductions that should
11298 not succeed, and therefore report ambiguous overload situations
11299 where there are none. In theory, we could allow the substitution,
11300 but indicate that it should have failed, and allow our caller to
11301 make sure that the right thing happens, but we don't try to do this
11302 yet.
11303
11304 This function is used for dealing with types, decls and the like;
11305 for expressions, use tsubst_expr or tsubst_copy. */
11306
11307 tree
11308 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11309 {
11310 enum tree_code code;
11311 tree type, r = NULL_TREE;
11312
11313 if (t == NULL_TREE || t == error_mark_node
11314 || t == integer_type_node
11315 || t == void_type_node
11316 || t == char_type_node
11317 || t == unknown_type_node
11318 || TREE_CODE (t) == NAMESPACE_DECL
11319 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11320 return t;
11321
11322 if (DECL_P (t))
11323 return tsubst_decl (t, args, complain);
11324
11325 if (args == NULL_TREE)
11326 return t;
11327
11328 code = TREE_CODE (t);
11329
11330 if (code == IDENTIFIER_NODE)
11331 type = IDENTIFIER_TYPE_VALUE (t);
11332 else
11333 type = TREE_TYPE (t);
11334
11335 gcc_assert (type != unknown_type_node);
11336
11337 /* Reuse typedefs. We need to do this to handle dependent attributes,
11338 such as attribute aligned. */
11339 if (TYPE_P (t)
11340 && typedef_variant_p (t))
11341 {
11342 tree decl = TYPE_NAME (t);
11343
11344 if (alias_template_specialization_p (t))
11345 {
11346 /* DECL represents an alias template and we want to
11347 instantiate it. */
11348 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11349 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11350 r = instantiate_alias_template (tmpl, gen_args, complain);
11351 }
11352 else if (DECL_CLASS_SCOPE_P (decl)
11353 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11354 && uses_template_parms (DECL_CONTEXT (decl)))
11355 {
11356 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11357 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11358 r = retrieve_specialization (tmpl, gen_args, 0);
11359 }
11360 else if (DECL_FUNCTION_SCOPE_P (decl)
11361 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11362 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11363 r = retrieve_local_specialization (decl);
11364 else
11365 /* The typedef is from a non-template context. */
11366 return t;
11367
11368 if (r)
11369 {
11370 r = TREE_TYPE (r);
11371 r = cp_build_qualified_type_real
11372 (r, cp_type_quals (t) | cp_type_quals (r),
11373 complain | tf_ignore_bad_quals);
11374 return r;
11375 }
11376 else
11377 {
11378 /* We don't have an instantiation yet, so drop the typedef. */
11379 int quals = cp_type_quals (t);
11380 t = DECL_ORIGINAL_TYPE (decl);
11381 t = cp_build_qualified_type_real (t, quals,
11382 complain | tf_ignore_bad_quals);
11383 }
11384 }
11385
11386 if (type
11387 && code != TYPENAME_TYPE
11388 && code != TEMPLATE_TYPE_PARM
11389 && code != IDENTIFIER_NODE
11390 && code != FUNCTION_TYPE
11391 && code != METHOD_TYPE)
11392 type = tsubst (type, args, complain, in_decl);
11393 if (type == error_mark_node)
11394 return error_mark_node;
11395
11396 switch (code)
11397 {
11398 case RECORD_TYPE:
11399 case UNION_TYPE:
11400 case ENUMERAL_TYPE:
11401 return tsubst_aggr_type (t, args, complain, in_decl,
11402 /*entering_scope=*/0);
11403
11404 case ERROR_MARK:
11405 case IDENTIFIER_NODE:
11406 case VOID_TYPE:
11407 case REAL_TYPE:
11408 case COMPLEX_TYPE:
11409 case VECTOR_TYPE:
11410 case BOOLEAN_TYPE:
11411 case NULLPTR_TYPE:
11412 case LANG_TYPE:
11413 return t;
11414
11415 case INTEGER_TYPE:
11416 if (t == integer_type_node)
11417 return t;
11418
11419 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11420 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11421 return t;
11422
11423 {
11424 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11425
11426 max = tsubst_expr (omax, args, complain, in_decl,
11427 /*integral_constant_expression_p=*/false);
11428
11429 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11430 needed. */
11431 if (TREE_CODE (max) == NOP_EXPR
11432 && TREE_SIDE_EFFECTS (omax)
11433 && !TREE_TYPE (max))
11434 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11435
11436 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11437 with TREE_SIDE_EFFECTS that indicates this is not an integral
11438 constant expression. */
11439 if (processing_template_decl
11440 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11441 {
11442 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11443 TREE_SIDE_EFFECTS (max) = 1;
11444 }
11445
11446 return compute_array_index_type (NULL_TREE, max, complain);
11447 }
11448
11449 case TEMPLATE_TYPE_PARM:
11450 case TEMPLATE_TEMPLATE_PARM:
11451 case BOUND_TEMPLATE_TEMPLATE_PARM:
11452 case TEMPLATE_PARM_INDEX:
11453 {
11454 int idx;
11455 int level;
11456 int levels;
11457 tree arg = NULL_TREE;
11458
11459 r = NULL_TREE;
11460
11461 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11462 template_parm_level_and_index (t, &level, &idx);
11463
11464 levels = TMPL_ARGS_DEPTH (args);
11465 if (level <= levels)
11466 {
11467 arg = TMPL_ARG (args, level, idx);
11468
11469 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11470 {
11471 /* See through ARGUMENT_PACK_SELECT arguments. */
11472 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11473 /* If the selected argument is an expansion E, that most
11474 likely means we were called from
11475 gen_elem_of_pack_expansion_instantiation during the
11476 substituting of pack an argument pack (which Ith
11477 element is a pack expansion, where I is
11478 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11479 In this case, the Ith element resulting from this
11480 substituting is going to be a pack expansion, which
11481 pattern is the pattern of E. Let's return the
11482 pattern of E, and
11483 gen_elem_of_pack_expansion_instantiation will
11484 build the resulting pack expansion from it. */
11485 if (PACK_EXPANSION_P (arg))
11486 arg = PACK_EXPANSION_PATTERN (arg);
11487 }
11488 }
11489
11490 if (arg == error_mark_node)
11491 return error_mark_node;
11492 else if (arg != NULL_TREE)
11493 {
11494 if (ARGUMENT_PACK_P (arg))
11495 /* If ARG is an argument pack, we don't actually want to
11496 perform a substitution here, because substitutions
11497 for argument packs are only done
11498 element-by-element. We can get to this point when
11499 substituting the type of a non-type template
11500 parameter pack, when that type actually contains
11501 template parameter packs from an outer template, e.g.,
11502
11503 template<typename... Types> struct A {
11504 template<Types... Values> struct B { };
11505 }; */
11506 return t;
11507
11508 if (code == TEMPLATE_TYPE_PARM)
11509 {
11510 int quals;
11511 gcc_assert (TYPE_P (arg));
11512
11513 quals = cp_type_quals (arg) | cp_type_quals (t);
11514
11515 return cp_build_qualified_type_real
11516 (arg, quals, complain | tf_ignore_bad_quals);
11517 }
11518 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11519 {
11520 /* We are processing a type constructed from a
11521 template template parameter. */
11522 tree argvec = tsubst (TYPE_TI_ARGS (t),
11523 args, complain, in_decl);
11524 if (argvec == error_mark_node)
11525 return error_mark_node;
11526
11527 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11528 || TREE_CODE (arg) == TEMPLATE_DECL
11529 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11530
11531 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11532 /* Consider this code:
11533
11534 template <template <class> class Template>
11535 struct Internal {
11536 template <class Arg> using Bind = Template<Arg>;
11537 };
11538
11539 template <template <class> class Template, class Arg>
11540 using Instantiate = Template<Arg>; //#0
11541
11542 template <template <class> class Template,
11543 class Argument>
11544 using Bind =
11545 Instantiate<Internal<Template>::template Bind,
11546 Argument>; //#1
11547
11548 When #1 is parsed, the
11549 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11550 parameter `Template' in #0 matches the
11551 UNBOUND_CLASS_TEMPLATE representing the argument
11552 `Internal<Template>::template Bind'; We then want
11553 to assemble the type `Bind<Argument>' that can't
11554 be fully created right now, because
11555 `Internal<Template>' not being complete, the Bind
11556 template cannot be looked up in that context. So
11557 we need to "store" `Bind<Argument>' for later
11558 when the context of Bind becomes complete. Let's
11559 store that in a TYPENAME_TYPE. */
11560 return make_typename_type (TYPE_CONTEXT (arg),
11561 build_nt (TEMPLATE_ID_EXPR,
11562 TYPE_IDENTIFIER (arg),
11563 argvec),
11564 typename_type,
11565 complain);
11566
11567 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11568 are resolving nested-types in the signature of a
11569 member function templates. Otherwise ARG is a
11570 TEMPLATE_DECL and is the real template to be
11571 instantiated. */
11572 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11573 arg = TYPE_NAME (arg);
11574
11575 r = lookup_template_class (arg,
11576 argvec, in_decl,
11577 DECL_CONTEXT (arg),
11578 /*entering_scope=*/0,
11579 complain);
11580 return cp_build_qualified_type_real
11581 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11582 }
11583 else
11584 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11585 return convert_from_reference (unshare_expr (arg));
11586 }
11587
11588 if (level == 1)
11589 /* This can happen during the attempted tsubst'ing in
11590 unify. This means that we don't yet have any information
11591 about the template parameter in question. */
11592 return t;
11593
11594 /* Early in template argument deduction substitution, we don't
11595 want to reduce the level of 'auto', or it will be confused
11596 with a normal template parm in subsequent deduction. */
11597 if (is_auto (t) && (complain & tf_partial))
11598 return t;
11599
11600 /* If we get here, we must have been looking at a parm for a
11601 more deeply nested template. Make a new version of this
11602 template parameter, but with a lower level. */
11603 switch (code)
11604 {
11605 case TEMPLATE_TYPE_PARM:
11606 case TEMPLATE_TEMPLATE_PARM:
11607 case BOUND_TEMPLATE_TEMPLATE_PARM:
11608 if (cp_type_quals (t))
11609 {
11610 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11611 r = cp_build_qualified_type_real
11612 (r, cp_type_quals (t),
11613 complain | (code == TEMPLATE_TYPE_PARM
11614 ? tf_ignore_bad_quals : 0));
11615 }
11616 else
11617 {
11618 r = copy_type (t);
11619 TEMPLATE_TYPE_PARM_INDEX (r)
11620 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11621 r, levels, args, complain);
11622 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11623 TYPE_MAIN_VARIANT (r) = r;
11624 TYPE_POINTER_TO (r) = NULL_TREE;
11625 TYPE_REFERENCE_TO (r) = NULL_TREE;
11626
11627 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11628 /* We have reduced the level of the template
11629 template parameter, but not the levels of its
11630 template parameters, so canonical_type_parameter
11631 will not be able to find the canonical template
11632 template parameter for this level. Thus, we
11633 require structural equality checking to compare
11634 TEMPLATE_TEMPLATE_PARMs. */
11635 SET_TYPE_STRUCTURAL_EQUALITY (r);
11636 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11637 SET_TYPE_STRUCTURAL_EQUALITY (r);
11638 else
11639 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11640
11641 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11642 {
11643 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11644 complain, in_decl);
11645 if (argvec == error_mark_node)
11646 return error_mark_node;
11647
11648 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11649 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11650 }
11651 }
11652 break;
11653
11654 case TEMPLATE_PARM_INDEX:
11655 r = reduce_template_parm_level (t, type, levels, args, complain);
11656 break;
11657
11658 default:
11659 gcc_unreachable ();
11660 }
11661
11662 return r;
11663 }
11664
11665 case TREE_LIST:
11666 {
11667 tree purpose, value, chain;
11668
11669 if (t == void_list_node)
11670 return t;
11671
11672 purpose = TREE_PURPOSE (t);
11673 if (purpose)
11674 {
11675 purpose = tsubst (purpose, args, complain, in_decl);
11676 if (purpose == error_mark_node)
11677 return error_mark_node;
11678 }
11679 value = TREE_VALUE (t);
11680 if (value)
11681 {
11682 value = tsubst (value, args, complain, in_decl);
11683 if (value == error_mark_node)
11684 return error_mark_node;
11685 }
11686 chain = TREE_CHAIN (t);
11687 if (chain && chain != void_type_node)
11688 {
11689 chain = tsubst (chain, args, complain, in_decl);
11690 if (chain == error_mark_node)
11691 return error_mark_node;
11692 }
11693 if (purpose == TREE_PURPOSE (t)
11694 && value == TREE_VALUE (t)
11695 && chain == TREE_CHAIN (t))
11696 return t;
11697 return hash_tree_cons (purpose, value, chain);
11698 }
11699
11700 case TREE_BINFO:
11701 /* We should never be tsubsting a binfo. */
11702 gcc_unreachable ();
11703
11704 case TREE_VEC:
11705 /* A vector of template arguments. */
11706 gcc_assert (!type);
11707 return tsubst_template_args (t, args, complain, in_decl);
11708
11709 case POINTER_TYPE:
11710 case REFERENCE_TYPE:
11711 {
11712 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11713 return t;
11714
11715 /* [temp.deduct]
11716
11717 Type deduction may fail for any of the following
11718 reasons:
11719
11720 -- Attempting to create a pointer to reference type.
11721 -- Attempting to create a reference to a reference type or
11722 a reference to void.
11723
11724 Core issue 106 says that creating a reference to a reference
11725 during instantiation is no longer a cause for failure. We
11726 only enforce this check in strict C++98 mode. */
11727 if ((TREE_CODE (type) == REFERENCE_TYPE
11728 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11729 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11730 {
11731 static location_t last_loc;
11732
11733 /* We keep track of the last time we issued this error
11734 message to avoid spewing a ton of messages during a
11735 single bad template instantiation. */
11736 if (complain & tf_error
11737 && last_loc != input_location)
11738 {
11739 if (VOID_TYPE_P (type))
11740 error ("forming reference to void");
11741 else if (code == POINTER_TYPE)
11742 error ("forming pointer to reference type %qT", type);
11743 else
11744 error ("forming reference to reference type %qT", type);
11745 last_loc = input_location;
11746 }
11747
11748 return error_mark_node;
11749 }
11750 else if (TREE_CODE (type) == FUNCTION_TYPE
11751 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11752 || type_memfn_rqual (type) != REF_QUAL_NONE))
11753 {
11754 if (complain & tf_error)
11755 {
11756 if (code == POINTER_TYPE)
11757 error ("forming pointer to qualified function type %qT",
11758 type);
11759 else
11760 error ("forming reference to qualified function type %qT",
11761 type);
11762 }
11763 return error_mark_node;
11764 }
11765 else if (code == POINTER_TYPE)
11766 {
11767 r = build_pointer_type (type);
11768 if (TREE_CODE (type) == METHOD_TYPE)
11769 r = build_ptrmemfunc_type (r);
11770 }
11771 else if (TREE_CODE (type) == REFERENCE_TYPE)
11772 /* In C++0x, during template argument substitution, when there is an
11773 attempt to create a reference to a reference type, reference
11774 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11775
11776 "If a template-argument for a template-parameter T names a type
11777 that is a reference to a type A, an attempt to create the type
11778 'lvalue reference to cv T' creates the type 'lvalue reference to
11779 A,' while an attempt to create the type type rvalue reference to
11780 cv T' creates the type T"
11781 */
11782 r = cp_build_reference_type
11783 (TREE_TYPE (type),
11784 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11785 else
11786 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11787 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11788
11789 if (cxx_dialect >= cxx1y
11790 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11791 && array_of_runtime_bound_p (type))
11792 {
11793 if (complain & tf_warning_or_error)
11794 pedwarn
11795 (input_location, OPT_Wvla,
11796 code == REFERENCE_TYPE
11797 ? G_("cannot declare reference to array of runtime bound")
11798 : G_("cannot declare pointer to array of runtime bound"));
11799 else
11800 r = error_mark_node;
11801 }
11802
11803 if (r != error_mark_node)
11804 /* Will this ever be needed for TYPE_..._TO values? */
11805 layout_type (r);
11806
11807 return r;
11808 }
11809 case OFFSET_TYPE:
11810 {
11811 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11812 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11813 {
11814 /* [temp.deduct]
11815
11816 Type deduction may fail for any of the following
11817 reasons:
11818
11819 -- Attempting to create "pointer to member of T" when T
11820 is not a class type. */
11821 if (complain & tf_error)
11822 error ("creating pointer to member of non-class type %qT", r);
11823 return error_mark_node;
11824 }
11825 if (TREE_CODE (type) == REFERENCE_TYPE)
11826 {
11827 if (complain & tf_error)
11828 error ("creating pointer to member reference type %qT", type);
11829 return error_mark_node;
11830 }
11831 if (VOID_TYPE_P (type))
11832 {
11833 if (complain & tf_error)
11834 error ("creating pointer to member of type void");
11835 return error_mark_node;
11836 }
11837 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11838 if (TREE_CODE (type) == FUNCTION_TYPE)
11839 {
11840 /* The type of the implicit object parameter gets its
11841 cv-qualifiers from the FUNCTION_TYPE. */
11842 tree memptr;
11843 tree method_type
11844 = build_memfn_type (type, r, type_memfn_quals (type),
11845 type_memfn_rqual (type));
11846 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11847 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11848 complain);
11849 }
11850 else
11851 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11852 cp_type_quals (t),
11853 complain);
11854 }
11855 case FUNCTION_TYPE:
11856 case METHOD_TYPE:
11857 {
11858 tree fntype;
11859 tree specs;
11860 fntype = tsubst_function_type (t, args, complain, in_decl);
11861 if (fntype == error_mark_node)
11862 return error_mark_node;
11863
11864 /* Substitute the exception specification. */
11865 specs = tsubst_exception_specification (t, args, complain,
11866 in_decl, /*defer_ok*/true);
11867 if (specs == error_mark_node)
11868 return error_mark_node;
11869 if (specs)
11870 fntype = build_exception_variant (fntype, specs);
11871 return fntype;
11872 }
11873 case ARRAY_TYPE:
11874 {
11875 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11876 if (domain == error_mark_node)
11877 return error_mark_node;
11878
11879 /* As an optimization, we avoid regenerating the array type if
11880 it will obviously be the same as T. */
11881 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11882 return t;
11883
11884 /* These checks should match the ones in grokdeclarator.
11885
11886 [temp.deduct]
11887
11888 The deduction may fail for any of the following reasons:
11889
11890 -- Attempting to create an array with an element type that
11891 is void, a function type, or a reference type, or [DR337]
11892 an abstract class type. */
11893 if (VOID_TYPE_P (type)
11894 || TREE_CODE (type) == FUNCTION_TYPE
11895 || TREE_CODE (type) == REFERENCE_TYPE)
11896 {
11897 if (complain & tf_error)
11898 error ("creating array of %qT", type);
11899 return error_mark_node;
11900 }
11901
11902 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11903 return error_mark_node;
11904
11905 r = build_cplus_array_type (type, domain);
11906
11907 if (TYPE_USER_ALIGN (t))
11908 {
11909 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11910 TYPE_USER_ALIGN (r) = 1;
11911 }
11912
11913 return r;
11914 }
11915
11916 case TYPENAME_TYPE:
11917 {
11918 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11919 in_decl, /*entering_scope=*/1);
11920 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11921 complain, in_decl);
11922
11923 if (ctx == error_mark_node || f == error_mark_node)
11924 return error_mark_node;
11925
11926 if (!MAYBE_CLASS_TYPE_P (ctx))
11927 {
11928 if (complain & tf_error)
11929 error ("%qT is not a class, struct, or union type", ctx);
11930 return error_mark_node;
11931 }
11932 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11933 {
11934 /* Normally, make_typename_type does not require that the CTX
11935 have complete type in order to allow things like:
11936
11937 template <class T> struct S { typename S<T>::X Y; };
11938
11939 But, such constructs have already been resolved by this
11940 point, so here CTX really should have complete type, unless
11941 it's a partial instantiation. */
11942 ctx = complete_type (ctx);
11943 if (!COMPLETE_TYPE_P (ctx))
11944 {
11945 if (complain & tf_error)
11946 cxx_incomplete_type_error (NULL_TREE, ctx);
11947 return error_mark_node;
11948 }
11949 }
11950
11951 f = make_typename_type (ctx, f, typename_type,
11952 complain | tf_keep_type_decl);
11953 if (f == error_mark_node)
11954 return f;
11955 if (TREE_CODE (f) == TYPE_DECL)
11956 {
11957 complain |= tf_ignore_bad_quals;
11958 f = TREE_TYPE (f);
11959 }
11960
11961 if (TREE_CODE (f) != TYPENAME_TYPE)
11962 {
11963 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11964 {
11965 if (complain & tf_error)
11966 error ("%qT resolves to %qT, which is not an enumeration type",
11967 t, f);
11968 else
11969 return error_mark_node;
11970 }
11971 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11972 {
11973 if (complain & tf_error)
11974 error ("%qT resolves to %qT, which is is not a class type",
11975 t, f);
11976 else
11977 return error_mark_node;
11978 }
11979 }
11980
11981 return cp_build_qualified_type_real
11982 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11983 }
11984
11985 case UNBOUND_CLASS_TEMPLATE:
11986 {
11987 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11988 in_decl, /*entering_scope=*/1);
11989 tree name = TYPE_IDENTIFIER (t);
11990 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11991
11992 if (ctx == error_mark_node || name == error_mark_node)
11993 return error_mark_node;
11994
11995 if (parm_list)
11996 parm_list = tsubst_template_parms (parm_list, args, complain);
11997 return make_unbound_class_template (ctx, name, parm_list, complain);
11998 }
11999
12000 case TYPEOF_TYPE:
12001 {
12002 tree type;
12003
12004 ++cp_unevaluated_operand;
12005 ++c_inhibit_evaluation_warnings;
12006
12007 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12008 complain, in_decl,
12009 /*integral_constant_expression_p=*/false);
12010
12011 --cp_unevaluated_operand;
12012 --c_inhibit_evaluation_warnings;
12013
12014 type = finish_typeof (type);
12015 return cp_build_qualified_type_real (type,
12016 cp_type_quals (t)
12017 | cp_type_quals (type),
12018 complain);
12019 }
12020
12021 case DECLTYPE_TYPE:
12022 {
12023 tree type;
12024
12025 ++cp_unevaluated_operand;
12026 ++c_inhibit_evaluation_warnings;
12027
12028 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12029 complain|tf_decltype, in_decl,
12030 /*function_p*/false,
12031 /*integral_constant_expression*/false);
12032
12033 --cp_unevaluated_operand;
12034 --c_inhibit_evaluation_warnings;
12035
12036 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12037 type = lambda_capture_field_type (type,
12038 DECLTYPE_FOR_INIT_CAPTURE (t));
12039 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12040 type = lambda_proxy_type (type);
12041 else
12042 {
12043 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12044 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12045 && EXPR_P (type))
12046 /* In a template ~id could be either a complement expression
12047 or an unqualified-id naming a destructor; if instantiating
12048 it produces an expression, it's not an id-expression or
12049 member access. */
12050 id = false;
12051 type = finish_decltype_type (type, id, complain);
12052 }
12053 return cp_build_qualified_type_real (type,
12054 cp_type_quals (t)
12055 | cp_type_quals (type),
12056 complain);
12057 }
12058
12059 case UNDERLYING_TYPE:
12060 {
12061 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12062 complain, in_decl);
12063 return finish_underlying_type (type);
12064 }
12065
12066 case TYPE_ARGUMENT_PACK:
12067 case NONTYPE_ARGUMENT_PACK:
12068 {
12069 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12070 tree packed_out =
12071 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12072 args,
12073 complain,
12074 in_decl);
12075 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12076
12077 /* For template nontype argument packs, also substitute into
12078 the type. */
12079 if (code == NONTYPE_ARGUMENT_PACK)
12080 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12081
12082 return r;
12083 }
12084 break;
12085
12086 case INTEGER_CST:
12087 case REAL_CST:
12088 case STRING_CST:
12089 case PLUS_EXPR:
12090 case MINUS_EXPR:
12091 case NEGATE_EXPR:
12092 case NOP_EXPR:
12093 case INDIRECT_REF:
12094 case ADDR_EXPR:
12095 case CALL_EXPR:
12096 case ARRAY_REF:
12097 case SCOPE_REF:
12098 /* We should use one of the expression tsubsts for these codes. */
12099 gcc_unreachable ();
12100
12101 default:
12102 sorry ("use of %qs in template", get_tree_code_name (code));
12103 return error_mark_node;
12104 }
12105 }
12106
12107 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12108 type of the expression on the left-hand side of the "." or "->"
12109 operator. */
12110
12111 static tree
12112 tsubst_baselink (tree baselink, tree object_type,
12113 tree args, tsubst_flags_t complain, tree in_decl)
12114 {
12115 tree name;
12116 tree qualifying_scope;
12117 tree fns;
12118 tree optype;
12119 tree template_args = 0;
12120 bool template_id_p = false;
12121 bool qualified = BASELINK_QUALIFIED_P (baselink);
12122
12123 /* A baselink indicates a function from a base class. Both the
12124 BASELINK_ACCESS_BINFO and the base class referenced may
12125 indicate bases of the template class, rather than the
12126 instantiated class. In addition, lookups that were not
12127 ambiguous before may be ambiguous now. Therefore, we perform
12128 the lookup again. */
12129 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12130 qualifying_scope = tsubst (qualifying_scope, args,
12131 complain, in_decl);
12132 fns = BASELINK_FUNCTIONS (baselink);
12133 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12134 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12135 {
12136 template_id_p = true;
12137 template_args = TREE_OPERAND (fns, 1);
12138 fns = TREE_OPERAND (fns, 0);
12139 if (template_args)
12140 template_args = tsubst_template_args (template_args, args,
12141 complain, in_decl);
12142 }
12143 name = DECL_NAME (get_first_fn (fns));
12144 if (IDENTIFIER_TYPENAME_P (name))
12145 name = mangle_conv_op_name_for_type (optype);
12146 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12147 if (!baselink)
12148 return error_mark_node;
12149
12150 /* If lookup found a single function, mark it as used at this
12151 point. (If it lookup found multiple functions the one selected
12152 later by overload resolution will be marked as used at that
12153 point.) */
12154 if (BASELINK_P (baselink))
12155 fns = BASELINK_FUNCTIONS (baselink);
12156 if (!template_id_p && !really_overloaded_fn (fns))
12157 mark_used (OVL_CURRENT (fns));
12158
12159 /* Add back the template arguments, if present. */
12160 if (BASELINK_P (baselink) && template_id_p)
12161 BASELINK_FUNCTIONS (baselink)
12162 = build_nt (TEMPLATE_ID_EXPR,
12163 BASELINK_FUNCTIONS (baselink),
12164 template_args);
12165 /* Update the conversion operator type. */
12166 BASELINK_OPTYPE (baselink) = optype;
12167
12168 if (!object_type)
12169 object_type = current_class_type;
12170
12171 if (qualified)
12172 baselink = adjust_result_of_qualified_name_lookup (baselink,
12173 qualifying_scope,
12174 object_type);
12175 return baselink;
12176 }
12177
12178 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12179 true if the qualified-id will be a postfix-expression in-and-of
12180 itself; false if more of the postfix-expression follows the
12181 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12182 of "&". */
12183
12184 static tree
12185 tsubst_qualified_id (tree qualified_id, tree args,
12186 tsubst_flags_t complain, tree in_decl,
12187 bool done, bool address_p)
12188 {
12189 tree expr;
12190 tree scope;
12191 tree name;
12192 bool is_template;
12193 tree template_args;
12194 location_t loc = UNKNOWN_LOCATION;
12195
12196 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12197
12198 /* Figure out what name to look up. */
12199 name = TREE_OPERAND (qualified_id, 1);
12200 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12201 {
12202 is_template = true;
12203 loc = EXPR_LOCATION (name);
12204 template_args = TREE_OPERAND (name, 1);
12205 if (template_args)
12206 template_args = tsubst_template_args (template_args, args,
12207 complain, in_decl);
12208 name = TREE_OPERAND (name, 0);
12209 }
12210 else
12211 {
12212 is_template = false;
12213 template_args = NULL_TREE;
12214 }
12215
12216 /* Substitute into the qualifying scope. When there are no ARGS, we
12217 are just trying to simplify a non-dependent expression. In that
12218 case the qualifying scope may be dependent, and, in any case,
12219 substituting will not help. */
12220 scope = TREE_OPERAND (qualified_id, 0);
12221 if (args)
12222 {
12223 scope = tsubst (scope, args, complain, in_decl);
12224 expr = tsubst_copy (name, args, complain, in_decl);
12225 }
12226 else
12227 expr = name;
12228
12229 if (dependent_scope_p (scope))
12230 {
12231 if (is_template)
12232 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12233 return build_qualified_name (NULL_TREE, scope, expr,
12234 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12235 }
12236
12237 if (!BASELINK_P (name) && !DECL_P (expr))
12238 {
12239 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12240 {
12241 /* A BIT_NOT_EXPR is used to represent a destructor. */
12242 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12243 {
12244 error ("qualifying type %qT does not match destructor name ~%qT",
12245 scope, TREE_OPERAND (expr, 0));
12246 expr = error_mark_node;
12247 }
12248 else
12249 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12250 /*is_type_p=*/0, false);
12251 }
12252 else
12253 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12254 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12255 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12256 {
12257 if (complain & tf_error)
12258 {
12259 error ("dependent-name %qE is parsed as a non-type, but "
12260 "instantiation yields a type", qualified_id);
12261 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12262 }
12263 return error_mark_node;
12264 }
12265 }
12266
12267 if (DECL_P (expr))
12268 {
12269 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12270 scope);
12271 /* Remember that there was a reference to this entity. */
12272 mark_used (expr);
12273 }
12274
12275 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12276 {
12277 if (complain & tf_error)
12278 qualified_name_lookup_error (scope,
12279 TREE_OPERAND (qualified_id, 1),
12280 expr, input_location);
12281 return error_mark_node;
12282 }
12283
12284 if (is_template)
12285 expr = lookup_template_function (expr, template_args);
12286
12287 if (expr == error_mark_node && complain & tf_error)
12288 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12289 expr, input_location);
12290 else if (TYPE_P (scope))
12291 {
12292 expr = (adjust_result_of_qualified_name_lookup
12293 (expr, scope, current_nonlambda_class_type ()));
12294 expr = (finish_qualified_id_expr
12295 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12296 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12297 /*template_arg_p=*/false, complain));
12298 }
12299
12300 /* Expressions do not generally have reference type. */
12301 if (TREE_CODE (expr) != SCOPE_REF
12302 /* However, if we're about to form a pointer-to-member, we just
12303 want the referenced member referenced. */
12304 && TREE_CODE (expr) != OFFSET_REF)
12305 expr = convert_from_reference (expr);
12306
12307 return expr;
12308 }
12309
12310 /* Like tsubst, but deals with expressions. This function just replaces
12311 template parms; to finish processing the resultant expression, use
12312 tsubst_copy_and_build or tsubst_expr. */
12313
12314 static tree
12315 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12316 {
12317 enum tree_code code;
12318 tree r;
12319
12320 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12321 return t;
12322
12323 code = TREE_CODE (t);
12324
12325 switch (code)
12326 {
12327 case PARM_DECL:
12328 r = retrieve_local_specialization (t);
12329
12330 if (r == NULL_TREE)
12331 {
12332 /* We get here for a use of 'this' in an NSDMI. */
12333 if (DECL_NAME (t) == this_identifier
12334 && at_function_scope_p ()
12335 && DECL_CONSTRUCTOR_P (current_function_decl))
12336 return current_class_ptr;
12337
12338 /* This can happen for a parameter name used later in a function
12339 declaration (such as in a late-specified return type). Just
12340 make a dummy decl, since it's only used for its type. */
12341 gcc_assert (cp_unevaluated_operand != 0);
12342 r = tsubst_decl (t, args, complain);
12343 /* Give it the template pattern as its context; its true context
12344 hasn't been instantiated yet and this is good enough for
12345 mangling. */
12346 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12347 }
12348
12349 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12350 r = ARGUMENT_PACK_SELECT_ARG (r);
12351 mark_used (r);
12352 return r;
12353
12354 case CONST_DECL:
12355 {
12356 tree enum_type;
12357 tree v;
12358
12359 if (DECL_TEMPLATE_PARM_P (t))
12360 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12361 /* There is no need to substitute into namespace-scope
12362 enumerators. */
12363 if (DECL_NAMESPACE_SCOPE_P (t))
12364 return t;
12365 /* If ARGS is NULL, then T is known to be non-dependent. */
12366 if (args == NULL_TREE)
12367 return integral_constant_value (t);
12368
12369 /* Unfortunately, we cannot just call lookup_name here.
12370 Consider:
12371
12372 template <int I> int f() {
12373 enum E { a = I };
12374 struct S { void g() { E e = a; } };
12375 };
12376
12377 When we instantiate f<7>::S::g(), say, lookup_name is not
12378 clever enough to find f<7>::a. */
12379 enum_type
12380 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12381 /*entering_scope=*/0);
12382
12383 for (v = TYPE_VALUES (enum_type);
12384 v != NULL_TREE;
12385 v = TREE_CHAIN (v))
12386 if (TREE_PURPOSE (v) == DECL_NAME (t))
12387 return TREE_VALUE (v);
12388
12389 /* We didn't find the name. That should never happen; if
12390 name-lookup found it during preliminary parsing, we
12391 should find it again here during instantiation. */
12392 gcc_unreachable ();
12393 }
12394 return t;
12395
12396 case FIELD_DECL:
12397 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12398 {
12399 /* Check for a local specialization set up by
12400 tsubst_pack_expansion. */
12401 tree r = retrieve_local_specialization (t);
12402 if (r)
12403 {
12404 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12405 r = ARGUMENT_PACK_SELECT_ARG (r);
12406 return r;
12407 }
12408
12409 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12410 tsubst_decl put in the hash table. */
12411 return retrieve_specialization (t, args, 0);
12412 }
12413
12414 if (DECL_CONTEXT (t))
12415 {
12416 tree ctx;
12417
12418 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12419 /*entering_scope=*/1);
12420 if (ctx != DECL_CONTEXT (t))
12421 {
12422 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12423 if (!r)
12424 {
12425 if (complain & tf_error)
12426 error ("using invalid field %qD", t);
12427 return error_mark_node;
12428 }
12429 return r;
12430 }
12431 }
12432
12433 return t;
12434
12435 case VAR_DECL:
12436 case FUNCTION_DECL:
12437 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12438 r = tsubst (t, args, complain, in_decl);
12439 else if (local_variable_p (t))
12440 {
12441 r = retrieve_local_specialization (t);
12442 if (r == NULL_TREE)
12443 {
12444 if (DECL_ANON_UNION_VAR_P (t))
12445 {
12446 /* Just use name lookup to find a member alias for an
12447 anonymous union, but then add it to the hash table. */
12448 r = lookup_name (DECL_NAME (t));
12449 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12450 register_local_specialization (r, t);
12451 }
12452 else
12453 {
12454 gcc_assert (errorcount || sorrycount);
12455 return error_mark_node;
12456 }
12457 }
12458 }
12459 else
12460 r = t;
12461 mark_used (r);
12462 return r;
12463
12464 case NAMESPACE_DECL:
12465 return t;
12466
12467 case OVERLOAD:
12468 /* An OVERLOAD will always be a non-dependent overload set; an
12469 overload set from function scope will just be represented with an
12470 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12471 gcc_assert (!uses_template_parms (t));
12472 return t;
12473
12474 case BASELINK:
12475 return tsubst_baselink (t, current_nonlambda_class_type (),
12476 args, complain, in_decl);
12477
12478 case TEMPLATE_DECL:
12479 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12480 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12481 args, complain, in_decl);
12482 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12483 return tsubst (t, args, complain, in_decl);
12484 else if (DECL_CLASS_SCOPE_P (t)
12485 && uses_template_parms (DECL_CONTEXT (t)))
12486 {
12487 /* Template template argument like the following example need
12488 special treatment:
12489
12490 template <template <class> class TT> struct C {};
12491 template <class T> struct D {
12492 template <class U> struct E {};
12493 C<E> c; // #1
12494 };
12495 D<int> d; // #2
12496
12497 We are processing the template argument `E' in #1 for
12498 the template instantiation #2. Originally, `E' is a
12499 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12500 have to substitute this with one having context `D<int>'. */
12501
12502 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12503 return lookup_field (context, DECL_NAME(t), 0, false);
12504 }
12505 else
12506 /* Ordinary template template argument. */
12507 return t;
12508
12509 case CAST_EXPR:
12510 case REINTERPRET_CAST_EXPR:
12511 case CONST_CAST_EXPR:
12512 case STATIC_CAST_EXPR:
12513 case DYNAMIC_CAST_EXPR:
12514 case IMPLICIT_CONV_EXPR:
12515 case CONVERT_EXPR:
12516 case NOP_EXPR:
12517 return build1
12518 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12519 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12520
12521 case SIZEOF_EXPR:
12522 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12523 {
12524
12525 tree expanded, op = TREE_OPERAND (t, 0);
12526 int len = 0;
12527
12528 if (SIZEOF_EXPR_TYPE_P (t))
12529 op = TREE_TYPE (op);
12530
12531 ++cp_unevaluated_operand;
12532 ++c_inhibit_evaluation_warnings;
12533 /* We only want to compute the number of arguments. */
12534 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12535 --cp_unevaluated_operand;
12536 --c_inhibit_evaluation_warnings;
12537
12538 if (TREE_CODE (expanded) == TREE_VEC)
12539 len = TREE_VEC_LENGTH (expanded);
12540
12541 if (expanded == error_mark_node)
12542 return error_mark_node;
12543 else if (PACK_EXPANSION_P (expanded)
12544 || (TREE_CODE (expanded) == TREE_VEC
12545 && len > 0
12546 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12547 {
12548 if (TREE_CODE (expanded) == TREE_VEC)
12549 expanded = TREE_VEC_ELT (expanded, len - 1);
12550
12551 if (TYPE_P (expanded))
12552 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12553 complain & tf_error);
12554 else
12555 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12556 complain & tf_error);
12557 }
12558 else
12559 return build_int_cst (size_type_node, len);
12560 }
12561 if (SIZEOF_EXPR_TYPE_P (t))
12562 {
12563 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12564 args, complain, in_decl);
12565 r = build1 (NOP_EXPR, r, error_mark_node);
12566 r = build1 (SIZEOF_EXPR,
12567 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12568 SIZEOF_EXPR_TYPE_P (r) = 1;
12569 return r;
12570 }
12571 /* Fall through */
12572
12573 case INDIRECT_REF:
12574 case NEGATE_EXPR:
12575 case TRUTH_NOT_EXPR:
12576 case BIT_NOT_EXPR:
12577 case ADDR_EXPR:
12578 case UNARY_PLUS_EXPR: /* Unary + */
12579 case ALIGNOF_EXPR:
12580 case AT_ENCODE_EXPR:
12581 case ARROW_EXPR:
12582 case THROW_EXPR:
12583 case TYPEID_EXPR:
12584 case REALPART_EXPR:
12585 case IMAGPART_EXPR:
12586 case PAREN_EXPR:
12587 return build1
12588 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12589 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12590
12591 case COMPONENT_REF:
12592 {
12593 tree object;
12594 tree name;
12595
12596 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12597 name = TREE_OPERAND (t, 1);
12598 if (TREE_CODE (name) == BIT_NOT_EXPR)
12599 {
12600 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12601 complain, in_decl);
12602 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12603 }
12604 else if (TREE_CODE (name) == SCOPE_REF
12605 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12606 {
12607 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12608 complain, in_decl);
12609 name = TREE_OPERAND (name, 1);
12610 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12611 complain, in_decl);
12612 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12613 name = build_qualified_name (/*type=*/NULL_TREE,
12614 base, name,
12615 /*template_p=*/false);
12616 }
12617 else if (BASELINK_P (name))
12618 name = tsubst_baselink (name,
12619 non_reference (TREE_TYPE (object)),
12620 args, complain,
12621 in_decl);
12622 else
12623 name = tsubst_copy (name, args, complain, in_decl);
12624 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12625 }
12626
12627 case PLUS_EXPR:
12628 case MINUS_EXPR:
12629 case MULT_EXPR:
12630 case TRUNC_DIV_EXPR:
12631 case CEIL_DIV_EXPR:
12632 case FLOOR_DIV_EXPR:
12633 case ROUND_DIV_EXPR:
12634 case EXACT_DIV_EXPR:
12635 case BIT_AND_EXPR:
12636 case BIT_IOR_EXPR:
12637 case BIT_XOR_EXPR:
12638 case TRUNC_MOD_EXPR:
12639 case FLOOR_MOD_EXPR:
12640 case TRUTH_ANDIF_EXPR:
12641 case TRUTH_ORIF_EXPR:
12642 case TRUTH_AND_EXPR:
12643 case TRUTH_OR_EXPR:
12644 case RSHIFT_EXPR:
12645 case LSHIFT_EXPR:
12646 case RROTATE_EXPR:
12647 case LROTATE_EXPR:
12648 case EQ_EXPR:
12649 case NE_EXPR:
12650 case MAX_EXPR:
12651 case MIN_EXPR:
12652 case LE_EXPR:
12653 case GE_EXPR:
12654 case LT_EXPR:
12655 case GT_EXPR:
12656 case COMPOUND_EXPR:
12657 case DOTSTAR_EXPR:
12658 case MEMBER_REF:
12659 case PREDECREMENT_EXPR:
12660 case PREINCREMENT_EXPR:
12661 case POSTDECREMENT_EXPR:
12662 case POSTINCREMENT_EXPR:
12663 return build_nt
12664 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12665 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12666
12667 case SCOPE_REF:
12668 return build_qualified_name (/*type=*/NULL_TREE,
12669 tsubst_copy (TREE_OPERAND (t, 0),
12670 args, complain, in_decl),
12671 tsubst_copy (TREE_OPERAND (t, 1),
12672 args, complain, in_decl),
12673 QUALIFIED_NAME_IS_TEMPLATE (t));
12674
12675 case ARRAY_REF:
12676 return build_nt
12677 (ARRAY_REF,
12678 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12679 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12680 NULL_TREE, NULL_TREE);
12681
12682 case CALL_EXPR:
12683 {
12684 int n = VL_EXP_OPERAND_LENGTH (t);
12685 tree result = build_vl_exp (CALL_EXPR, n);
12686 int i;
12687 for (i = 0; i < n; i++)
12688 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12689 complain, in_decl);
12690 return result;
12691 }
12692
12693 case COND_EXPR:
12694 case MODOP_EXPR:
12695 case PSEUDO_DTOR_EXPR:
12696 case VEC_PERM_EXPR:
12697 {
12698 r = build_nt
12699 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12700 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12701 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12702 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12703 return r;
12704 }
12705
12706 case NEW_EXPR:
12707 {
12708 r = build_nt
12709 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12710 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12711 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12712 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12713 return r;
12714 }
12715
12716 case DELETE_EXPR:
12717 {
12718 r = build_nt
12719 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12720 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12721 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12722 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12723 return r;
12724 }
12725
12726 case TEMPLATE_ID_EXPR:
12727 {
12728 /* Substituted template arguments */
12729 tree fn = TREE_OPERAND (t, 0);
12730 tree targs = TREE_OPERAND (t, 1);
12731
12732 fn = tsubst_copy (fn, args, complain, in_decl);
12733 if (targs)
12734 targs = tsubst_template_args (targs, args, complain, in_decl);
12735
12736 return lookup_template_function (fn, targs);
12737 }
12738
12739 case TREE_LIST:
12740 {
12741 tree purpose, value, chain;
12742
12743 if (t == void_list_node)
12744 return t;
12745
12746 purpose = TREE_PURPOSE (t);
12747 if (purpose)
12748 purpose = tsubst_copy (purpose, args, complain, in_decl);
12749 value = TREE_VALUE (t);
12750 if (value)
12751 value = tsubst_copy (value, args, complain, in_decl);
12752 chain = TREE_CHAIN (t);
12753 if (chain && chain != void_type_node)
12754 chain = tsubst_copy (chain, args, complain, in_decl);
12755 if (purpose == TREE_PURPOSE (t)
12756 && value == TREE_VALUE (t)
12757 && chain == TREE_CHAIN (t))
12758 return t;
12759 return tree_cons (purpose, value, chain);
12760 }
12761
12762 case RECORD_TYPE:
12763 case UNION_TYPE:
12764 case ENUMERAL_TYPE:
12765 case INTEGER_TYPE:
12766 case TEMPLATE_TYPE_PARM:
12767 case TEMPLATE_TEMPLATE_PARM:
12768 case BOUND_TEMPLATE_TEMPLATE_PARM:
12769 case TEMPLATE_PARM_INDEX:
12770 case POINTER_TYPE:
12771 case REFERENCE_TYPE:
12772 case OFFSET_TYPE:
12773 case FUNCTION_TYPE:
12774 case METHOD_TYPE:
12775 case ARRAY_TYPE:
12776 case TYPENAME_TYPE:
12777 case UNBOUND_CLASS_TEMPLATE:
12778 case TYPEOF_TYPE:
12779 case DECLTYPE_TYPE:
12780 case TYPE_DECL:
12781 return tsubst (t, args, complain, in_decl);
12782
12783 case USING_DECL:
12784 t = DECL_NAME (t);
12785 /* Fall through. */
12786 case IDENTIFIER_NODE:
12787 if (IDENTIFIER_TYPENAME_P (t))
12788 {
12789 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12790 return mangle_conv_op_name_for_type (new_type);
12791 }
12792 else
12793 return t;
12794
12795 case CONSTRUCTOR:
12796 /* This is handled by tsubst_copy_and_build. */
12797 gcc_unreachable ();
12798
12799 case VA_ARG_EXPR:
12800 return build_x_va_arg (EXPR_LOCATION (t),
12801 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12802 in_decl),
12803 tsubst (TREE_TYPE (t), args, complain, in_decl));
12804
12805 case CLEANUP_POINT_EXPR:
12806 /* We shouldn't have built any of these during initial template
12807 generation. Instead, they should be built during instantiation
12808 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12809 gcc_unreachable ();
12810
12811 case OFFSET_REF:
12812 r = build2
12813 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12814 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12815 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12816 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12817 mark_used (TREE_OPERAND (r, 1));
12818 return r;
12819
12820 case EXPR_PACK_EXPANSION:
12821 error ("invalid use of pack expansion expression");
12822 return error_mark_node;
12823
12824 case NONTYPE_ARGUMENT_PACK:
12825 error ("use %<...%> to expand argument pack");
12826 return error_mark_node;
12827
12828 case INTEGER_CST:
12829 case REAL_CST:
12830 case STRING_CST:
12831 case COMPLEX_CST:
12832 {
12833 /* Instantiate any typedefs in the type. */
12834 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12835 r = fold_convert (type, t);
12836 gcc_assert (TREE_CODE (r) == code);
12837 return r;
12838 }
12839
12840 case PTRMEM_CST:
12841 /* These can sometimes show up in a partial instantiation, but never
12842 involve template parms. */
12843 gcc_assert (!uses_template_parms (t));
12844 return t;
12845
12846 default:
12847 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12848 gcc_checking_assert (false);
12849 return t;
12850 }
12851 }
12852
12853 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12854
12855 static tree
12856 tsubst_omp_clauses (tree clauses, bool declare_simd,
12857 tree args, tsubst_flags_t complain, tree in_decl)
12858 {
12859 tree new_clauses = NULL, nc, oc;
12860
12861 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12862 {
12863 nc = copy_node (oc);
12864 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12865 new_clauses = nc;
12866
12867 switch (OMP_CLAUSE_CODE (nc))
12868 {
12869 case OMP_CLAUSE_LASTPRIVATE:
12870 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12871 {
12872 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12873 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12874 in_decl, /*integral_constant_expression_p=*/false);
12875 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12876 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12877 }
12878 /* FALLTHRU */
12879 case OMP_CLAUSE_PRIVATE:
12880 case OMP_CLAUSE_SHARED:
12881 case OMP_CLAUSE_FIRSTPRIVATE:
12882 case OMP_CLAUSE_COPYIN:
12883 case OMP_CLAUSE_COPYPRIVATE:
12884 case OMP_CLAUSE_IF:
12885 case OMP_CLAUSE_NUM_THREADS:
12886 case OMP_CLAUSE_SCHEDULE:
12887 case OMP_CLAUSE_COLLAPSE:
12888 case OMP_CLAUSE_FINAL:
12889 case OMP_CLAUSE_DEPEND:
12890 case OMP_CLAUSE_FROM:
12891 case OMP_CLAUSE_TO:
12892 case OMP_CLAUSE_UNIFORM:
12893 case OMP_CLAUSE_MAP:
12894 case OMP_CLAUSE_DEVICE:
12895 case OMP_CLAUSE_DIST_SCHEDULE:
12896 case OMP_CLAUSE_NUM_TEAMS:
12897 case OMP_CLAUSE_THREAD_LIMIT:
12898 case OMP_CLAUSE_SAFELEN:
12899 case OMP_CLAUSE_SIMDLEN:
12900 OMP_CLAUSE_OPERAND (nc, 0)
12901 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12902 in_decl, /*integral_constant_expression_p=*/false);
12903 break;
12904 case OMP_CLAUSE_REDUCTION:
12905 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12906 {
12907 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12908 if (TREE_CODE (placeholder) == SCOPE_REF)
12909 {
12910 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12911 complain, in_decl);
12912 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12913 = build_qualified_name (NULL_TREE, scope,
12914 TREE_OPERAND (placeholder, 1),
12915 false);
12916 }
12917 else
12918 gcc_assert (identifier_p (placeholder));
12919 }
12920 OMP_CLAUSE_OPERAND (nc, 0)
12921 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12922 in_decl, /*integral_constant_expression_p=*/false);
12923 break;
12924 case OMP_CLAUSE_LINEAR:
12925 case OMP_CLAUSE_ALIGNED:
12926 OMP_CLAUSE_OPERAND (nc, 0)
12927 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12928 in_decl, /*integral_constant_expression_p=*/false);
12929 OMP_CLAUSE_OPERAND (nc, 1)
12930 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12931 in_decl, /*integral_constant_expression_p=*/false);
12932 break;
12933
12934 case OMP_CLAUSE_NOWAIT:
12935 case OMP_CLAUSE_ORDERED:
12936 case OMP_CLAUSE_DEFAULT:
12937 case OMP_CLAUSE_UNTIED:
12938 case OMP_CLAUSE_MERGEABLE:
12939 case OMP_CLAUSE_INBRANCH:
12940 case OMP_CLAUSE_NOTINBRANCH:
12941 case OMP_CLAUSE_PROC_BIND:
12942 case OMP_CLAUSE_FOR:
12943 case OMP_CLAUSE_PARALLEL:
12944 case OMP_CLAUSE_SECTIONS:
12945 case OMP_CLAUSE_TASKGROUP:
12946 break;
12947 default:
12948 gcc_unreachable ();
12949 }
12950 }
12951
12952 new_clauses = nreverse (new_clauses);
12953 if (!declare_simd)
12954 new_clauses = finish_omp_clauses (new_clauses);
12955 return new_clauses;
12956 }
12957
12958 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12959
12960 static tree
12961 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12962 tree in_decl)
12963 {
12964 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12965
12966 tree purpose, value, chain;
12967
12968 if (t == NULL)
12969 return t;
12970
12971 if (TREE_CODE (t) != TREE_LIST)
12972 return tsubst_copy_and_build (t, args, complain, in_decl,
12973 /*function_p=*/false,
12974 /*integral_constant_expression_p=*/false);
12975
12976 if (t == void_list_node)
12977 return t;
12978
12979 purpose = TREE_PURPOSE (t);
12980 if (purpose)
12981 purpose = RECUR (purpose);
12982 value = TREE_VALUE (t);
12983 if (value)
12984 {
12985 if (TREE_CODE (value) != LABEL_DECL)
12986 value = RECUR (value);
12987 else
12988 {
12989 value = lookup_label (DECL_NAME (value));
12990 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12991 TREE_USED (value) = 1;
12992 }
12993 }
12994 chain = TREE_CHAIN (t);
12995 if (chain && chain != void_type_node)
12996 chain = RECUR (chain);
12997 return tree_cons (purpose, value, chain);
12998 #undef RECUR
12999 }
13000
13001 /* Substitute one OMP_FOR iterator. */
13002
13003 static void
13004 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13005 tree condv, tree incrv, tree *clauses,
13006 tree args, tsubst_flags_t complain, tree in_decl,
13007 bool integral_constant_expression_p)
13008 {
13009 #define RECUR(NODE) \
13010 tsubst_expr ((NODE), args, complain, in_decl, \
13011 integral_constant_expression_p)
13012 tree decl, init, cond, incr;
13013 bool init_decl;
13014
13015 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13016 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13017 decl = TREE_OPERAND (init, 0);
13018 init = TREE_OPERAND (init, 1);
13019 /* Do this before substituting into decl to handle 'auto'. */
13020 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13021 init = RECUR (init);
13022 decl = RECUR (decl);
13023 if (init_decl)
13024 {
13025 gcc_assert (!processing_template_decl);
13026 init = DECL_INITIAL (decl);
13027 DECL_INITIAL (decl) = NULL_TREE;
13028 }
13029
13030 gcc_assert (!type_dependent_expression_p (decl));
13031
13032 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13033 {
13034 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13035 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13036 if (TREE_CODE (incr) == MODIFY_EXPR)
13037 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13038 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13039 RECUR (TREE_OPERAND (incr, 1)),
13040 complain);
13041 else
13042 incr = RECUR (incr);
13043 TREE_VEC_ELT (declv, i) = decl;
13044 TREE_VEC_ELT (initv, i) = init;
13045 TREE_VEC_ELT (condv, i) = cond;
13046 TREE_VEC_ELT (incrv, i) = incr;
13047 return;
13048 }
13049
13050 if (init && !init_decl)
13051 {
13052 tree c;
13053 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13054 {
13055 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13056 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13057 && OMP_CLAUSE_DECL (c) == decl)
13058 break;
13059 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13060 && OMP_CLAUSE_DECL (c) == decl)
13061 error ("iteration variable %qD should not be firstprivate", decl);
13062 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13063 && OMP_CLAUSE_DECL (c) == decl)
13064 error ("iteration variable %qD should not be reduction", decl);
13065 }
13066 if (c == NULL)
13067 {
13068 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13069 OMP_CLAUSE_DECL (c) = decl;
13070 c = finish_omp_clauses (c);
13071 if (c)
13072 {
13073 OMP_CLAUSE_CHAIN (c) = *clauses;
13074 *clauses = c;
13075 }
13076 }
13077 }
13078 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13079 if (COMPARISON_CLASS_P (cond))
13080 cond = build2 (TREE_CODE (cond), boolean_type_node,
13081 RECUR (TREE_OPERAND (cond, 0)),
13082 RECUR (TREE_OPERAND (cond, 1)));
13083 else
13084 cond = RECUR (cond);
13085 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13086 switch (TREE_CODE (incr))
13087 {
13088 case PREINCREMENT_EXPR:
13089 case PREDECREMENT_EXPR:
13090 case POSTINCREMENT_EXPR:
13091 case POSTDECREMENT_EXPR:
13092 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13093 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13094 break;
13095 case MODIFY_EXPR:
13096 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13097 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13098 {
13099 tree rhs = TREE_OPERAND (incr, 1);
13100 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13101 RECUR (TREE_OPERAND (incr, 0)),
13102 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13103 RECUR (TREE_OPERAND (rhs, 0)),
13104 RECUR (TREE_OPERAND (rhs, 1))));
13105 }
13106 else
13107 incr = RECUR (incr);
13108 break;
13109 case MODOP_EXPR:
13110 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13111 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13112 {
13113 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13114 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13115 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13116 TREE_TYPE (decl), lhs,
13117 RECUR (TREE_OPERAND (incr, 2))));
13118 }
13119 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13120 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13121 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13122 {
13123 tree rhs = TREE_OPERAND (incr, 2);
13124 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13125 RECUR (TREE_OPERAND (incr, 0)),
13126 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13127 RECUR (TREE_OPERAND (rhs, 0)),
13128 RECUR (TREE_OPERAND (rhs, 1))));
13129 }
13130 else
13131 incr = RECUR (incr);
13132 break;
13133 default:
13134 incr = RECUR (incr);
13135 break;
13136 }
13137
13138 TREE_VEC_ELT (declv, i) = decl;
13139 TREE_VEC_ELT (initv, i) = init;
13140 TREE_VEC_ELT (condv, i) = cond;
13141 TREE_VEC_ELT (incrv, i) = incr;
13142 #undef RECUR
13143 }
13144
13145 /* Like tsubst_copy for expressions, etc. but also does semantic
13146 processing. */
13147
13148 static tree
13149 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13150 bool integral_constant_expression_p)
13151 {
13152 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13153 #define RECUR(NODE) \
13154 tsubst_expr ((NODE), args, complain, in_decl, \
13155 integral_constant_expression_p)
13156
13157 tree stmt, tmp;
13158 tree r;
13159 location_t loc;
13160
13161 if (t == NULL_TREE || t == error_mark_node)
13162 return t;
13163
13164 loc = input_location;
13165 if (EXPR_HAS_LOCATION (t))
13166 input_location = EXPR_LOCATION (t);
13167 if (STATEMENT_CODE_P (TREE_CODE (t)))
13168 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13169
13170 switch (TREE_CODE (t))
13171 {
13172 case STATEMENT_LIST:
13173 {
13174 tree_stmt_iterator i;
13175 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13176 RECUR (tsi_stmt (i));
13177 break;
13178 }
13179
13180 case CTOR_INITIALIZER:
13181 finish_mem_initializers (tsubst_initializer_list
13182 (TREE_OPERAND (t, 0), args));
13183 break;
13184
13185 case RETURN_EXPR:
13186 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13187 break;
13188
13189 case EXPR_STMT:
13190 tmp = RECUR (EXPR_STMT_EXPR (t));
13191 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13192 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13193 else
13194 finish_expr_stmt (tmp);
13195 break;
13196
13197 case USING_STMT:
13198 do_using_directive (USING_STMT_NAMESPACE (t));
13199 break;
13200
13201 case DECL_EXPR:
13202 {
13203 tree decl, pattern_decl;
13204 tree init;
13205
13206 pattern_decl = decl = DECL_EXPR_DECL (t);
13207 if (TREE_CODE (decl) == LABEL_DECL)
13208 finish_label_decl (DECL_NAME (decl));
13209 else if (TREE_CODE (decl) == USING_DECL)
13210 {
13211 tree scope = USING_DECL_SCOPE (decl);
13212 tree name = DECL_NAME (decl);
13213 tree decl;
13214
13215 scope = tsubst (scope, args, complain, in_decl);
13216 decl = lookup_qualified_name (scope, name,
13217 /*is_type_p=*/false,
13218 /*complain=*/false);
13219 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13220 qualified_name_lookup_error (scope, name, decl, input_location);
13221 else
13222 do_local_using_decl (decl, scope, name);
13223 }
13224 else if (DECL_PACK_P (decl))
13225 {
13226 /* Don't build up decls for a variadic capture proxy, we'll
13227 instantiate the elements directly as needed. */
13228 break;
13229 }
13230 else
13231 {
13232 init = DECL_INITIAL (decl);
13233 decl = tsubst (decl, args, complain, in_decl);
13234 if (decl != error_mark_node)
13235 {
13236 /* By marking the declaration as instantiated, we avoid
13237 trying to instantiate it. Since instantiate_decl can't
13238 handle local variables, and since we've already done
13239 all that needs to be done, that's the right thing to
13240 do. */
13241 if (VAR_P (decl))
13242 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13243 if (VAR_P (decl)
13244 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13245 /* Anonymous aggregates are a special case. */
13246 finish_anon_union (decl);
13247 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13248 {
13249 DECL_CONTEXT (decl) = current_function_decl;
13250 if (DECL_NAME (decl) == this_identifier)
13251 {
13252 tree lam = DECL_CONTEXT (current_function_decl);
13253 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13254 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13255 }
13256 insert_capture_proxy (decl);
13257 }
13258 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13259 /* We already did a pushtag. */;
13260 else if (TREE_CODE (decl) == FUNCTION_DECL
13261 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13262 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13263 {
13264 DECL_CONTEXT (decl) = NULL_TREE;
13265 pushdecl (decl);
13266 DECL_CONTEXT (decl) = current_function_decl;
13267 cp_check_omp_declare_reduction (decl);
13268 }
13269 else
13270 {
13271 int const_init = false;
13272 maybe_push_decl (decl);
13273 if (VAR_P (decl)
13274 && DECL_PRETTY_FUNCTION_P (decl))
13275 {
13276 /* For __PRETTY_FUNCTION__ we have to adjust the
13277 initializer. */
13278 const char *const name
13279 = cxx_printable_name (current_function_decl, 2);
13280 init = cp_fname_init (name, &TREE_TYPE (decl));
13281 }
13282 else
13283 {
13284 tree t = RECUR (init);
13285
13286 if (init && !t)
13287 {
13288 /* If we had an initializer but it
13289 instantiated to nothing,
13290 value-initialize the object. This will
13291 only occur when the initializer was a
13292 pack expansion where the parameter packs
13293 used in that expansion were of length
13294 zero. */
13295 init = build_value_init (TREE_TYPE (decl),
13296 complain);
13297 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13298 init = get_target_expr_sfinae (init, complain);
13299 }
13300 else
13301 init = t;
13302 }
13303
13304 if (VAR_P (decl))
13305 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13306 (pattern_decl));
13307 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13308 }
13309 }
13310 }
13311
13312 break;
13313 }
13314
13315 case FOR_STMT:
13316 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13317 RECUR (FOR_INIT_STMT (t));
13318 finish_for_init_stmt (stmt);
13319 tmp = RECUR (FOR_COND (t));
13320 finish_for_cond (tmp, stmt);
13321 tmp = RECUR (FOR_EXPR (t));
13322 finish_for_expr (tmp, stmt);
13323 RECUR (FOR_BODY (t));
13324 finish_for_stmt (stmt);
13325 break;
13326
13327 case RANGE_FOR_STMT:
13328 {
13329 tree decl, expr;
13330 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13331 decl = RANGE_FOR_DECL (t);
13332 decl = tsubst (decl, args, complain, in_decl);
13333 maybe_push_decl (decl);
13334 expr = RECUR (RANGE_FOR_EXPR (t));
13335 stmt = cp_convert_range_for (stmt, decl, expr);
13336 RECUR (RANGE_FOR_BODY (t));
13337 finish_for_stmt (stmt);
13338 }
13339 break;
13340
13341 case WHILE_STMT:
13342 stmt = begin_while_stmt ();
13343 tmp = RECUR (WHILE_COND (t));
13344 finish_while_stmt_cond (tmp, stmt);
13345 RECUR (WHILE_BODY (t));
13346 finish_while_stmt (stmt);
13347 break;
13348
13349 case DO_STMT:
13350 stmt = begin_do_stmt ();
13351 RECUR (DO_BODY (t));
13352 finish_do_body (stmt);
13353 tmp = RECUR (DO_COND (t));
13354 finish_do_stmt (tmp, stmt);
13355 break;
13356
13357 case IF_STMT:
13358 stmt = begin_if_stmt ();
13359 tmp = RECUR (IF_COND (t));
13360 finish_if_stmt_cond (tmp, stmt);
13361 RECUR (THEN_CLAUSE (t));
13362 finish_then_clause (stmt);
13363
13364 if (ELSE_CLAUSE (t))
13365 {
13366 begin_else_clause (stmt);
13367 RECUR (ELSE_CLAUSE (t));
13368 finish_else_clause (stmt);
13369 }
13370
13371 finish_if_stmt (stmt);
13372 break;
13373
13374 case BIND_EXPR:
13375 if (BIND_EXPR_BODY_BLOCK (t))
13376 stmt = begin_function_body ();
13377 else
13378 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13379 ? BCS_TRY_BLOCK : 0);
13380
13381 RECUR (BIND_EXPR_BODY (t));
13382
13383 if (BIND_EXPR_BODY_BLOCK (t))
13384 finish_function_body (stmt);
13385 else
13386 finish_compound_stmt (stmt);
13387 break;
13388
13389 case BREAK_STMT:
13390 finish_break_stmt ();
13391 break;
13392
13393 case CONTINUE_STMT:
13394 finish_continue_stmt ();
13395 break;
13396
13397 case SWITCH_STMT:
13398 stmt = begin_switch_stmt ();
13399 tmp = RECUR (SWITCH_STMT_COND (t));
13400 finish_switch_cond (tmp, stmt);
13401 RECUR (SWITCH_STMT_BODY (t));
13402 finish_switch_stmt (stmt);
13403 break;
13404
13405 case CASE_LABEL_EXPR:
13406 finish_case_label (EXPR_LOCATION (t),
13407 RECUR (CASE_LOW (t)),
13408 RECUR (CASE_HIGH (t)));
13409 break;
13410
13411 case LABEL_EXPR:
13412 {
13413 tree decl = LABEL_EXPR_LABEL (t);
13414 tree label;
13415
13416 label = finish_label_stmt (DECL_NAME (decl));
13417 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13418 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13419 }
13420 break;
13421
13422 case GOTO_EXPR:
13423 tmp = GOTO_DESTINATION (t);
13424 if (TREE_CODE (tmp) != LABEL_DECL)
13425 /* Computed goto's must be tsubst'd into. On the other hand,
13426 non-computed gotos must not be; the identifier in question
13427 will have no binding. */
13428 tmp = RECUR (tmp);
13429 else
13430 tmp = DECL_NAME (tmp);
13431 finish_goto_stmt (tmp);
13432 break;
13433
13434 case ASM_EXPR:
13435 tmp = finish_asm_stmt
13436 (ASM_VOLATILE_P (t),
13437 RECUR (ASM_STRING (t)),
13438 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13439 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13440 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13441 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13442 {
13443 tree asm_expr = tmp;
13444 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13445 asm_expr = TREE_OPERAND (asm_expr, 0);
13446 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13447 }
13448 break;
13449
13450 case TRY_BLOCK:
13451 if (CLEANUP_P (t))
13452 {
13453 stmt = begin_try_block ();
13454 RECUR (TRY_STMTS (t));
13455 finish_cleanup_try_block (stmt);
13456 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13457 }
13458 else
13459 {
13460 tree compound_stmt = NULL_TREE;
13461
13462 if (FN_TRY_BLOCK_P (t))
13463 stmt = begin_function_try_block (&compound_stmt);
13464 else
13465 stmt = begin_try_block ();
13466
13467 RECUR (TRY_STMTS (t));
13468
13469 if (FN_TRY_BLOCK_P (t))
13470 finish_function_try_block (stmt);
13471 else
13472 finish_try_block (stmt);
13473
13474 RECUR (TRY_HANDLERS (t));
13475 if (FN_TRY_BLOCK_P (t))
13476 finish_function_handler_sequence (stmt, compound_stmt);
13477 else
13478 finish_handler_sequence (stmt);
13479 }
13480 break;
13481
13482 case HANDLER:
13483 {
13484 tree decl = HANDLER_PARMS (t);
13485
13486 if (decl)
13487 {
13488 decl = tsubst (decl, args, complain, in_decl);
13489 /* Prevent instantiate_decl from trying to instantiate
13490 this variable. We've already done all that needs to be
13491 done. */
13492 if (decl != error_mark_node)
13493 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13494 }
13495 stmt = begin_handler ();
13496 finish_handler_parms (decl, stmt);
13497 RECUR (HANDLER_BODY (t));
13498 finish_handler (stmt);
13499 }
13500 break;
13501
13502 case TAG_DEFN:
13503 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13504 if (CLASS_TYPE_P (tmp))
13505 {
13506 /* Local classes are not independent templates; they are
13507 instantiated along with their containing function. And this
13508 way we don't have to deal with pushing out of one local class
13509 to instantiate a member of another local class. */
13510 tree fn;
13511 /* Closures are handled by the LAMBDA_EXPR. */
13512 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13513 complete_type (tmp);
13514 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13515 if (!DECL_ARTIFICIAL (fn))
13516 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13517 }
13518 break;
13519
13520 case STATIC_ASSERT:
13521 {
13522 tree condition;
13523
13524 ++c_inhibit_evaluation_warnings;
13525 condition =
13526 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13527 args,
13528 complain, in_decl,
13529 /*integral_constant_expression_p=*/true);
13530 --c_inhibit_evaluation_warnings;
13531
13532 finish_static_assert (condition,
13533 STATIC_ASSERT_MESSAGE (t),
13534 STATIC_ASSERT_SOURCE_LOCATION (t),
13535 /*member_p=*/false);
13536 }
13537 break;
13538
13539 case OMP_PARALLEL:
13540 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13541 args, complain, in_decl);
13542 stmt = begin_omp_parallel ();
13543 RECUR (OMP_PARALLEL_BODY (t));
13544 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13545 = OMP_PARALLEL_COMBINED (t);
13546 break;
13547
13548 case OMP_TASK:
13549 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13550 args, complain, in_decl);
13551 stmt = begin_omp_task ();
13552 RECUR (OMP_TASK_BODY (t));
13553 finish_omp_task (tmp, stmt);
13554 break;
13555
13556 case OMP_FOR:
13557 case OMP_SIMD:
13558 case OMP_DISTRIBUTE:
13559 {
13560 tree clauses, body, pre_body;
13561 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13562 tree incrv = NULL_TREE;
13563 int i;
13564
13565 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13566 args, complain, in_decl);
13567 if (OMP_FOR_INIT (t) != NULL_TREE)
13568 {
13569 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13570 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13571 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13572 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13573 }
13574
13575 stmt = begin_omp_structured_block ();
13576
13577 pre_body = push_stmt_list ();
13578 RECUR (OMP_FOR_PRE_BODY (t));
13579 pre_body = pop_stmt_list (pre_body);
13580
13581 if (OMP_FOR_INIT (t) != NULL_TREE)
13582 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13583 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13584 &clauses, args, complain, in_decl,
13585 integral_constant_expression_p);
13586
13587 body = push_stmt_list ();
13588 RECUR (OMP_FOR_BODY (t));
13589 body = pop_stmt_list (body);
13590
13591 if (OMP_FOR_INIT (t) != NULL_TREE)
13592 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13593 condv, incrv, body, pre_body, clauses);
13594 else
13595 {
13596 t = make_node (TREE_CODE (t));
13597 TREE_TYPE (t) = void_type_node;
13598 OMP_FOR_BODY (t) = body;
13599 OMP_FOR_PRE_BODY (t) = pre_body;
13600 OMP_FOR_CLAUSES (t) = clauses;
13601 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13602 add_stmt (t);
13603 }
13604
13605 add_stmt (finish_omp_structured_block (stmt));
13606 }
13607 break;
13608
13609 case OMP_SECTIONS:
13610 case OMP_SINGLE:
13611 case OMP_TEAMS:
13612 case OMP_TARGET_DATA:
13613 case OMP_TARGET:
13614 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13615 args, complain, in_decl);
13616 stmt = push_stmt_list ();
13617 RECUR (OMP_BODY (t));
13618 stmt = pop_stmt_list (stmt);
13619
13620 t = copy_node (t);
13621 OMP_BODY (t) = stmt;
13622 OMP_CLAUSES (t) = tmp;
13623 add_stmt (t);
13624 break;
13625
13626 case OMP_TARGET_UPDATE:
13627 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13628 args, complain, in_decl);
13629 t = copy_node (t);
13630 OMP_CLAUSES (t) = tmp;
13631 add_stmt (t);
13632 break;
13633
13634 case OMP_SECTION:
13635 case OMP_CRITICAL:
13636 case OMP_MASTER:
13637 case OMP_TASKGROUP:
13638 case OMP_ORDERED:
13639 stmt = push_stmt_list ();
13640 RECUR (OMP_BODY (t));
13641 stmt = pop_stmt_list (stmt);
13642
13643 t = copy_node (t);
13644 OMP_BODY (t) = stmt;
13645 add_stmt (t);
13646 break;
13647
13648 case OMP_ATOMIC:
13649 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13650 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13651 {
13652 tree op1 = TREE_OPERAND (t, 1);
13653 tree rhs1 = NULL_TREE;
13654 tree lhs, rhs;
13655 if (TREE_CODE (op1) == COMPOUND_EXPR)
13656 {
13657 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13658 op1 = TREE_OPERAND (op1, 1);
13659 }
13660 lhs = RECUR (TREE_OPERAND (op1, 0));
13661 rhs = RECUR (TREE_OPERAND (op1, 1));
13662 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13663 NULL_TREE, NULL_TREE, rhs1,
13664 OMP_ATOMIC_SEQ_CST (t));
13665 }
13666 else
13667 {
13668 tree op1 = TREE_OPERAND (t, 1);
13669 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13670 tree rhs1 = NULL_TREE;
13671 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13672 enum tree_code opcode = NOP_EXPR;
13673 if (code == OMP_ATOMIC_READ)
13674 {
13675 v = RECUR (TREE_OPERAND (op1, 0));
13676 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13677 }
13678 else if (code == OMP_ATOMIC_CAPTURE_OLD
13679 || code == OMP_ATOMIC_CAPTURE_NEW)
13680 {
13681 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13682 v = RECUR (TREE_OPERAND (op1, 0));
13683 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13684 if (TREE_CODE (op11) == COMPOUND_EXPR)
13685 {
13686 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13687 op11 = TREE_OPERAND (op11, 1);
13688 }
13689 lhs = RECUR (TREE_OPERAND (op11, 0));
13690 rhs = RECUR (TREE_OPERAND (op11, 1));
13691 opcode = TREE_CODE (op11);
13692 if (opcode == MODIFY_EXPR)
13693 opcode = NOP_EXPR;
13694 }
13695 else
13696 {
13697 code = OMP_ATOMIC;
13698 lhs = RECUR (TREE_OPERAND (op1, 0));
13699 rhs = RECUR (TREE_OPERAND (op1, 1));
13700 }
13701 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13702 OMP_ATOMIC_SEQ_CST (t));
13703 }
13704 break;
13705
13706 case TRANSACTION_EXPR:
13707 {
13708 int flags = 0;
13709 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13710 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13711
13712 if (TRANSACTION_EXPR_IS_STMT (t))
13713 {
13714 tree body = TRANSACTION_EXPR_BODY (t);
13715 tree noex = NULL_TREE;
13716 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13717 {
13718 noex = MUST_NOT_THROW_COND (body);
13719 if (noex == NULL_TREE)
13720 noex = boolean_true_node;
13721 body = TREE_OPERAND (body, 0);
13722 }
13723 stmt = begin_transaction_stmt (input_location, NULL, flags);
13724 RECUR (body);
13725 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13726 }
13727 else
13728 {
13729 stmt = build_transaction_expr (EXPR_LOCATION (t),
13730 RECUR (TRANSACTION_EXPR_BODY (t)),
13731 flags, NULL_TREE);
13732 RETURN (stmt);
13733 }
13734 }
13735 break;
13736
13737 case MUST_NOT_THROW_EXPR:
13738 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13739 RECUR (MUST_NOT_THROW_COND (t))));
13740
13741 case EXPR_PACK_EXPANSION:
13742 error ("invalid use of pack expansion expression");
13743 RETURN (error_mark_node);
13744
13745 case NONTYPE_ARGUMENT_PACK:
13746 error ("use %<...%> to expand argument pack");
13747 RETURN (error_mark_node);
13748
13749 case COMPOUND_EXPR:
13750 tmp = RECUR (TREE_OPERAND (t, 0));
13751 if (tmp == NULL_TREE)
13752 /* If the first operand was a statement, we're done with it. */
13753 RETURN (RECUR (TREE_OPERAND (t, 1)));
13754 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13755 RECUR (TREE_OPERAND (t, 1)),
13756 complain));
13757
13758 default:
13759 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13760
13761 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13762 /*function_p=*/false,
13763 integral_constant_expression_p));
13764 }
13765
13766 RETURN (NULL_TREE);
13767 out:
13768 input_location = loc;
13769 return r;
13770 #undef RECUR
13771 #undef RETURN
13772 }
13773
13774 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13775 function. For description of the body see comment above
13776 cp_parser_omp_declare_reduction_exprs. */
13777
13778 static void
13779 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13780 {
13781 if (t == NULL_TREE || t == error_mark_node)
13782 return;
13783
13784 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13785
13786 tree_stmt_iterator tsi;
13787 int i;
13788 tree stmts[7];
13789 memset (stmts, 0, sizeof stmts);
13790 for (i = 0, tsi = tsi_start (t);
13791 i < 7 && !tsi_end_p (tsi);
13792 i++, tsi_next (&tsi))
13793 stmts[i] = tsi_stmt (tsi);
13794 gcc_assert (tsi_end_p (tsi));
13795
13796 if (i >= 3)
13797 {
13798 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13799 && TREE_CODE (stmts[1]) == DECL_EXPR);
13800 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13801 args, complain, in_decl);
13802 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13803 args, complain, in_decl);
13804 DECL_CONTEXT (omp_out) = current_function_decl;
13805 DECL_CONTEXT (omp_in) = current_function_decl;
13806 keep_next_level (true);
13807 tree block = begin_omp_structured_block ();
13808 tsubst_expr (stmts[2], args, complain, in_decl, false);
13809 block = finish_omp_structured_block (block);
13810 block = maybe_cleanup_point_expr_void (block);
13811 add_decl_expr (omp_out);
13812 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13813 TREE_NO_WARNING (omp_out) = 1;
13814 add_decl_expr (omp_in);
13815 finish_expr_stmt (block);
13816 }
13817 if (i >= 6)
13818 {
13819 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13820 && TREE_CODE (stmts[4]) == DECL_EXPR);
13821 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13822 args, complain, in_decl);
13823 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13824 args, complain, in_decl);
13825 DECL_CONTEXT (omp_priv) = current_function_decl;
13826 DECL_CONTEXT (omp_orig) = current_function_decl;
13827 keep_next_level (true);
13828 tree block = begin_omp_structured_block ();
13829 tsubst_expr (stmts[5], args, complain, in_decl, false);
13830 block = finish_omp_structured_block (block);
13831 block = maybe_cleanup_point_expr_void (block);
13832 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13833 add_decl_expr (omp_priv);
13834 add_decl_expr (omp_orig);
13835 finish_expr_stmt (block);
13836 if (i == 7)
13837 add_decl_expr (omp_orig);
13838 }
13839 }
13840
13841 /* T is a postfix-expression that is not being used in a function
13842 call. Return the substituted version of T. */
13843
13844 static tree
13845 tsubst_non_call_postfix_expression (tree t, tree args,
13846 tsubst_flags_t complain,
13847 tree in_decl)
13848 {
13849 if (TREE_CODE (t) == SCOPE_REF)
13850 t = tsubst_qualified_id (t, args, complain, in_decl,
13851 /*done=*/false, /*address_p=*/false);
13852 else
13853 t = tsubst_copy_and_build (t, args, complain, in_decl,
13854 /*function_p=*/false,
13855 /*integral_constant_expression_p=*/false);
13856
13857 return t;
13858 }
13859
13860 /* Like tsubst but deals with expressions and performs semantic
13861 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13862
13863 tree
13864 tsubst_copy_and_build (tree t,
13865 tree args,
13866 tsubst_flags_t complain,
13867 tree in_decl,
13868 bool function_p,
13869 bool integral_constant_expression_p)
13870 {
13871 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13872 #define RECUR(NODE) \
13873 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13874 /*function_p=*/false, \
13875 integral_constant_expression_p)
13876
13877 tree retval, op1;
13878 location_t loc;
13879
13880 if (t == NULL_TREE || t == error_mark_node)
13881 return t;
13882
13883 loc = input_location;
13884 if (EXPR_HAS_LOCATION (t))
13885 input_location = EXPR_LOCATION (t);
13886
13887 /* N3276 decltype magic only applies to calls at the top level or on the
13888 right side of a comma. */
13889 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13890 complain &= ~tf_decltype;
13891
13892 switch (TREE_CODE (t))
13893 {
13894 case USING_DECL:
13895 t = DECL_NAME (t);
13896 /* Fall through. */
13897 case IDENTIFIER_NODE:
13898 {
13899 tree decl;
13900 cp_id_kind idk;
13901 bool non_integral_constant_expression_p;
13902 const char *error_msg;
13903
13904 if (IDENTIFIER_TYPENAME_P (t))
13905 {
13906 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13907 t = mangle_conv_op_name_for_type (new_type);
13908 }
13909
13910 /* Look up the name. */
13911 decl = lookup_name (t);
13912
13913 /* By convention, expressions use ERROR_MARK_NODE to indicate
13914 failure, not NULL_TREE. */
13915 if (decl == NULL_TREE)
13916 decl = error_mark_node;
13917
13918 decl = finish_id_expression (t, decl, NULL_TREE,
13919 &idk,
13920 integral_constant_expression_p,
13921 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13922 &non_integral_constant_expression_p,
13923 /*template_p=*/false,
13924 /*done=*/true,
13925 /*address_p=*/false,
13926 /*template_arg_p=*/false,
13927 &error_msg,
13928 input_location);
13929 if (error_msg)
13930 error (error_msg);
13931 if (!function_p && identifier_p (decl))
13932 {
13933 if (complain & tf_error)
13934 unqualified_name_lookup_error (decl);
13935 decl = error_mark_node;
13936 }
13937 RETURN (decl);
13938 }
13939
13940 case TEMPLATE_ID_EXPR:
13941 {
13942 tree object;
13943 tree templ = RECUR (TREE_OPERAND (t, 0));
13944 tree targs = TREE_OPERAND (t, 1);
13945
13946 if (targs)
13947 targs = tsubst_template_args (targs, args, complain, in_decl);
13948
13949 if (TREE_CODE (templ) == COMPONENT_REF)
13950 {
13951 object = TREE_OPERAND (templ, 0);
13952 templ = TREE_OPERAND (templ, 1);
13953 }
13954 else
13955 object = NULL_TREE;
13956 templ = lookup_template_function (templ, targs);
13957
13958 if (object)
13959 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13960 object, templ, NULL_TREE));
13961 else
13962 RETURN (baselink_for_fns (templ));
13963 }
13964
13965 case INDIRECT_REF:
13966 {
13967 tree r = RECUR (TREE_OPERAND (t, 0));
13968
13969 if (REFERENCE_REF_P (t))
13970 {
13971 /* A type conversion to reference type will be enclosed in
13972 such an indirect ref, but the substitution of the cast
13973 will have also added such an indirect ref. */
13974 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13975 r = convert_from_reference (r);
13976 }
13977 else
13978 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13979 complain|decltype_flag);
13980 RETURN (r);
13981 }
13982
13983 case NOP_EXPR:
13984 RETURN (build_nop
13985 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13986 RECUR (TREE_OPERAND (t, 0))));
13987
13988 case IMPLICIT_CONV_EXPR:
13989 {
13990 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13991 tree expr = RECUR (TREE_OPERAND (t, 0));
13992 int flags = LOOKUP_IMPLICIT;
13993 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13994 flags = LOOKUP_NORMAL;
13995 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13996 flags));
13997 }
13998
13999 case CONVERT_EXPR:
14000 RETURN (build1
14001 (CONVERT_EXPR,
14002 tsubst (TREE_TYPE (t), args, complain, in_decl),
14003 RECUR (TREE_OPERAND (t, 0))));
14004
14005 case CAST_EXPR:
14006 case REINTERPRET_CAST_EXPR:
14007 case CONST_CAST_EXPR:
14008 case DYNAMIC_CAST_EXPR:
14009 case STATIC_CAST_EXPR:
14010 {
14011 tree type;
14012 tree op, r = NULL_TREE;
14013
14014 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14015 if (integral_constant_expression_p
14016 && !cast_valid_in_integral_constant_expression_p (type))
14017 {
14018 if (complain & tf_error)
14019 error ("a cast to a type other than an integral or "
14020 "enumeration type cannot appear in a constant-expression");
14021 RETURN (error_mark_node);
14022 }
14023
14024 op = RECUR (TREE_OPERAND (t, 0));
14025
14026 ++c_inhibit_evaluation_warnings;
14027 switch (TREE_CODE (t))
14028 {
14029 case CAST_EXPR:
14030 r = build_functional_cast (type, op, complain);
14031 break;
14032 case REINTERPRET_CAST_EXPR:
14033 r = build_reinterpret_cast (type, op, complain);
14034 break;
14035 case CONST_CAST_EXPR:
14036 r = build_const_cast (type, op, complain);
14037 break;
14038 case DYNAMIC_CAST_EXPR:
14039 r = build_dynamic_cast (type, op, complain);
14040 break;
14041 case STATIC_CAST_EXPR:
14042 r = build_static_cast (type, op, complain);
14043 break;
14044 default:
14045 gcc_unreachable ();
14046 }
14047 --c_inhibit_evaluation_warnings;
14048
14049 RETURN (r);
14050 }
14051
14052 case POSTDECREMENT_EXPR:
14053 case POSTINCREMENT_EXPR:
14054 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14055 args, complain, in_decl);
14056 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14057 complain|decltype_flag));
14058
14059 case PREDECREMENT_EXPR:
14060 case PREINCREMENT_EXPR:
14061 case NEGATE_EXPR:
14062 case BIT_NOT_EXPR:
14063 case ABS_EXPR:
14064 case TRUTH_NOT_EXPR:
14065 case UNARY_PLUS_EXPR: /* Unary + */
14066 case REALPART_EXPR:
14067 case IMAGPART_EXPR:
14068 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14069 RECUR (TREE_OPERAND (t, 0)),
14070 complain|decltype_flag));
14071
14072 case FIX_TRUNC_EXPR:
14073 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14074 0, complain));
14075
14076 case ADDR_EXPR:
14077 op1 = TREE_OPERAND (t, 0);
14078 if (TREE_CODE (op1) == LABEL_DECL)
14079 RETURN (finish_label_address_expr (DECL_NAME (op1),
14080 EXPR_LOCATION (op1)));
14081 if (TREE_CODE (op1) == SCOPE_REF)
14082 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14083 /*done=*/true, /*address_p=*/true);
14084 else
14085 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14086 in_decl);
14087 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14088 complain|decltype_flag));
14089
14090 case PLUS_EXPR:
14091 case MINUS_EXPR:
14092 case MULT_EXPR:
14093 case TRUNC_DIV_EXPR:
14094 case CEIL_DIV_EXPR:
14095 case FLOOR_DIV_EXPR:
14096 case ROUND_DIV_EXPR:
14097 case EXACT_DIV_EXPR:
14098 case BIT_AND_EXPR:
14099 case BIT_IOR_EXPR:
14100 case BIT_XOR_EXPR:
14101 case TRUNC_MOD_EXPR:
14102 case FLOOR_MOD_EXPR:
14103 case TRUTH_ANDIF_EXPR:
14104 case TRUTH_ORIF_EXPR:
14105 case TRUTH_AND_EXPR:
14106 case TRUTH_OR_EXPR:
14107 case RSHIFT_EXPR:
14108 case LSHIFT_EXPR:
14109 case RROTATE_EXPR:
14110 case LROTATE_EXPR:
14111 case EQ_EXPR:
14112 case NE_EXPR:
14113 case MAX_EXPR:
14114 case MIN_EXPR:
14115 case LE_EXPR:
14116 case GE_EXPR:
14117 case LT_EXPR:
14118 case GT_EXPR:
14119 case MEMBER_REF:
14120 case DOTSTAR_EXPR:
14121 {
14122 tree r;
14123
14124 ++c_inhibit_evaluation_warnings;
14125
14126 r = build_x_binary_op
14127 (input_location, TREE_CODE (t),
14128 RECUR (TREE_OPERAND (t, 0)),
14129 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14130 ? ERROR_MARK
14131 : TREE_CODE (TREE_OPERAND (t, 0))),
14132 RECUR (TREE_OPERAND (t, 1)),
14133 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14134 ? ERROR_MARK
14135 : TREE_CODE (TREE_OPERAND (t, 1))),
14136 /*overload=*/NULL,
14137 complain|decltype_flag);
14138 if (EXPR_P (r) && TREE_NO_WARNING (t))
14139 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14140
14141 --c_inhibit_evaluation_warnings;
14142
14143 RETURN (r);
14144 }
14145
14146 case SCOPE_REF:
14147 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14148 /*address_p=*/false));
14149 case ARRAY_REF:
14150 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14151 args, complain, in_decl);
14152 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14153 RECUR (TREE_OPERAND (t, 1)),
14154 complain|decltype_flag));
14155
14156 case ARRAY_NOTATION_REF:
14157 {
14158 tree start_index, length, stride;
14159 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14160 args, complain, in_decl);
14161 start_index = RECUR (ARRAY_NOTATION_START (t));
14162 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14163 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14164 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14165 length, stride, TREE_TYPE (op1)));
14166 }
14167 case SIZEOF_EXPR:
14168 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14169 RETURN (tsubst_copy (t, args, complain, in_decl));
14170 /* Fall through */
14171
14172 case ALIGNOF_EXPR:
14173 {
14174 tree r;
14175
14176 op1 = TREE_OPERAND (t, 0);
14177 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14178 op1 = TREE_TYPE (op1);
14179 if (!args)
14180 {
14181 /* When there are no ARGS, we are trying to evaluate a
14182 non-dependent expression from the parser. Trying to do
14183 the substitutions may not work. */
14184 if (!TYPE_P (op1))
14185 op1 = TREE_TYPE (op1);
14186 }
14187 else
14188 {
14189 ++cp_unevaluated_operand;
14190 ++c_inhibit_evaluation_warnings;
14191 if (TYPE_P (op1))
14192 op1 = tsubst (op1, args, complain, in_decl);
14193 else
14194 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14195 /*function_p=*/false,
14196 /*integral_constant_expression_p=*/
14197 false);
14198 --cp_unevaluated_operand;
14199 --c_inhibit_evaluation_warnings;
14200 }
14201 if (TYPE_P (op1))
14202 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14203 complain & tf_error);
14204 else
14205 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14206 complain & tf_error);
14207 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14208 {
14209 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14210 {
14211 if (!processing_template_decl && TYPE_P (op1))
14212 {
14213 r = build_min (SIZEOF_EXPR, size_type_node,
14214 build1 (NOP_EXPR, op1, error_mark_node));
14215 SIZEOF_EXPR_TYPE_P (r) = 1;
14216 }
14217 else
14218 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14219 TREE_SIDE_EFFECTS (r) = 0;
14220 TREE_READONLY (r) = 1;
14221 }
14222 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14223 }
14224 RETURN (r);
14225 }
14226
14227 case AT_ENCODE_EXPR:
14228 {
14229 op1 = TREE_OPERAND (t, 0);
14230 ++cp_unevaluated_operand;
14231 ++c_inhibit_evaluation_warnings;
14232 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14233 /*function_p=*/false,
14234 /*integral_constant_expression_p=*/false);
14235 --cp_unevaluated_operand;
14236 --c_inhibit_evaluation_warnings;
14237 RETURN (objc_build_encode_expr (op1));
14238 }
14239
14240 case NOEXCEPT_EXPR:
14241 op1 = TREE_OPERAND (t, 0);
14242 ++cp_unevaluated_operand;
14243 ++c_inhibit_evaluation_warnings;
14244 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14245 /*function_p=*/false,
14246 /*integral_constant_expression_p=*/false);
14247 --cp_unevaluated_operand;
14248 --c_inhibit_evaluation_warnings;
14249 RETURN (finish_noexcept_expr (op1, complain));
14250
14251 case MODOP_EXPR:
14252 {
14253 tree r;
14254
14255 ++c_inhibit_evaluation_warnings;
14256
14257 r = build_x_modify_expr
14258 (EXPR_LOCATION (t),
14259 RECUR (TREE_OPERAND (t, 0)),
14260 TREE_CODE (TREE_OPERAND (t, 1)),
14261 RECUR (TREE_OPERAND (t, 2)),
14262 complain|decltype_flag);
14263 /* TREE_NO_WARNING must be set if either the expression was
14264 parenthesized or it uses an operator such as >>= rather
14265 than plain assignment. In the former case, it was already
14266 set and must be copied. In the latter case,
14267 build_x_modify_expr sets it and it must not be reset
14268 here. */
14269 if (TREE_NO_WARNING (t))
14270 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14271
14272 --c_inhibit_evaluation_warnings;
14273
14274 RETURN (r);
14275 }
14276
14277 case ARROW_EXPR:
14278 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14279 args, complain, in_decl);
14280 /* Remember that there was a reference to this entity. */
14281 if (DECL_P (op1))
14282 mark_used (op1);
14283 RETURN (build_x_arrow (input_location, op1, complain));
14284
14285 case NEW_EXPR:
14286 {
14287 tree placement = RECUR (TREE_OPERAND (t, 0));
14288 tree init = RECUR (TREE_OPERAND (t, 3));
14289 vec<tree, va_gc> *placement_vec;
14290 vec<tree, va_gc> *init_vec;
14291 tree ret;
14292
14293 if (placement == NULL_TREE)
14294 placement_vec = NULL;
14295 else
14296 {
14297 placement_vec = make_tree_vector ();
14298 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14299 vec_safe_push (placement_vec, TREE_VALUE (placement));
14300 }
14301
14302 /* If there was an initializer in the original tree, but it
14303 instantiated to an empty list, then we should pass a
14304 non-NULL empty vector to tell build_new that it was an
14305 empty initializer() rather than no initializer. This can
14306 only happen when the initializer is a pack expansion whose
14307 parameter packs are of length zero. */
14308 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14309 init_vec = NULL;
14310 else
14311 {
14312 init_vec = make_tree_vector ();
14313 if (init == void_zero_node)
14314 gcc_assert (init_vec != NULL);
14315 else
14316 {
14317 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14318 vec_safe_push (init_vec, TREE_VALUE (init));
14319 }
14320 }
14321
14322 ret = build_new (&placement_vec,
14323 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14324 RECUR (TREE_OPERAND (t, 2)),
14325 &init_vec,
14326 NEW_EXPR_USE_GLOBAL (t),
14327 complain);
14328
14329 if (placement_vec != NULL)
14330 release_tree_vector (placement_vec);
14331 if (init_vec != NULL)
14332 release_tree_vector (init_vec);
14333
14334 RETURN (ret);
14335 }
14336
14337 case DELETE_EXPR:
14338 RETURN (delete_sanity
14339 (RECUR (TREE_OPERAND (t, 0)),
14340 RECUR (TREE_OPERAND (t, 1)),
14341 DELETE_EXPR_USE_VEC (t),
14342 DELETE_EXPR_USE_GLOBAL (t),
14343 complain));
14344
14345 case COMPOUND_EXPR:
14346 {
14347 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14348 complain & ~tf_decltype, in_decl,
14349 /*function_p=*/false,
14350 integral_constant_expression_p);
14351 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14352 op0,
14353 RECUR (TREE_OPERAND (t, 1)),
14354 complain|decltype_flag));
14355 }
14356
14357 case CALL_EXPR:
14358 {
14359 tree function;
14360 vec<tree, va_gc> *call_args;
14361 unsigned int nargs, i;
14362 bool qualified_p;
14363 bool koenig_p;
14364 tree ret;
14365
14366 function = CALL_EXPR_FN (t);
14367 /* When we parsed the expression, we determined whether or
14368 not Koenig lookup should be performed. */
14369 koenig_p = KOENIG_LOOKUP_P (t);
14370 if (TREE_CODE (function) == SCOPE_REF)
14371 {
14372 qualified_p = true;
14373 function = tsubst_qualified_id (function, args, complain, in_decl,
14374 /*done=*/false,
14375 /*address_p=*/false);
14376 }
14377 else if (koenig_p && identifier_p (function))
14378 {
14379 /* Do nothing; calling tsubst_copy_and_build on an identifier
14380 would incorrectly perform unqualified lookup again.
14381
14382 Note that we can also have an IDENTIFIER_NODE if the earlier
14383 unqualified lookup found a member function; in that case
14384 koenig_p will be false and we do want to do the lookup
14385 again to find the instantiated member function.
14386
14387 FIXME but doing that causes c++/15272, so we need to stop
14388 using IDENTIFIER_NODE in that situation. */
14389 qualified_p = false;
14390 }
14391 else
14392 {
14393 if (TREE_CODE (function) == COMPONENT_REF)
14394 {
14395 tree op = TREE_OPERAND (function, 1);
14396
14397 qualified_p = (TREE_CODE (op) == SCOPE_REF
14398 || (BASELINK_P (op)
14399 && BASELINK_QUALIFIED_P (op)));
14400 }
14401 else
14402 qualified_p = false;
14403
14404 if (TREE_CODE (function) == ADDR_EXPR
14405 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14406 /* Avoid error about taking the address of a constructor. */
14407 function = TREE_OPERAND (function, 0);
14408
14409 function = tsubst_copy_and_build (function, args, complain,
14410 in_decl,
14411 !qualified_p,
14412 integral_constant_expression_p);
14413
14414 if (BASELINK_P (function))
14415 qualified_p = true;
14416 }
14417
14418 nargs = call_expr_nargs (t);
14419 call_args = make_tree_vector ();
14420 for (i = 0; i < nargs; ++i)
14421 {
14422 tree arg = CALL_EXPR_ARG (t, i);
14423
14424 if (!PACK_EXPANSION_P (arg))
14425 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14426 else
14427 {
14428 /* Expand the pack expansion and push each entry onto
14429 CALL_ARGS. */
14430 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14431 if (TREE_CODE (arg) == TREE_VEC)
14432 {
14433 unsigned int len, j;
14434
14435 len = TREE_VEC_LENGTH (arg);
14436 for (j = 0; j < len; ++j)
14437 {
14438 tree value = TREE_VEC_ELT (arg, j);
14439 if (value != NULL_TREE)
14440 value = convert_from_reference (value);
14441 vec_safe_push (call_args, value);
14442 }
14443 }
14444 else
14445 {
14446 /* A partial substitution. Add one entry. */
14447 vec_safe_push (call_args, arg);
14448 }
14449 }
14450 }
14451
14452 /* We do not perform argument-dependent lookup if normal
14453 lookup finds a non-function, in accordance with the
14454 expected resolution of DR 218. */
14455 if (koenig_p
14456 && ((is_overloaded_fn (function)
14457 /* If lookup found a member function, the Koenig lookup is
14458 not appropriate, even if an unqualified-name was used
14459 to denote the function. */
14460 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14461 || identifier_p (function))
14462 /* Only do this when substitution turns a dependent call
14463 into a non-dependent call. */
14464 && type_dependent_expression_p_push (t)
14465 && !any_type_dependent_arguments_p (call_args))
14466 function = perform_koenig_lookup (function, call_args, false,
14467 tf_none);
14468
14469 if (identifier_p (function)
14470 && !any_type_dependent_arguments_p (call_args))
14471 {
14472 if (koenig_p && (complain & tf_warning_or_error))
14473 {
14474 /* For backwards compatibility and good diagnostics, try
14475 the unqualified lookup again if we aren't in SFINAE
14476 context. */
14477 tree unq = (tsubst_copy_and_build
14478 (function, args, complain, in_decl, true,
14479 integral_constant_expression_p));
14480 if (unq == error_mark_node)
14481 RETURN (error_mark_node);
14482
14483 if (unq != function)
14484 {
14485 tree fn = unq;
14486 if (INDIRECT_REF_P (fn))
14487 fn = TREE_OPERAND (fn, 0);
14488 if (TREE_CODE (fn) == COMPONENT_REF)
14489 fn = TREE_OPERAND (fn, 1);
14490 if (is_overloaded_fn (fn))
14491 fn = get_first_fn (fn);
14492 if (permerror (EXPR_LOC_OR_HERE (t),
14493 "%qD was not declared in this scope, "
14494 "and no declarations were found by "
14495 "argument-dependent lookup at the point "
14496 "of instantiation", function))
14497 {
14498 if (!DECL_P (fn))
14499 /* Can't say anything more. */;
14500 else if (DECL_CLASS_SCOPE_P (fn))
14501 {
14502 inform (EXPR_LOC_OR_HERE (t),
14503 "declarations in dependent base %qT are "
14504 "not found by unqualified lookup",
14505 DECL_CLASS_CONTEXT (fn));
14506 if (current_class_ptr)
14507 inform (EXPR_LOC_OR_HERE (t),
14508 "use %<this->%D%> instead", function);
14509 else
14510 inform (EXPR_LOC_OR_HERE (t),
14511 "use %<%T::%D%> instead",
14512 current_class_name, function);
14513 }
14514 else
14515 inform (0, "%q+D declared here, later in the "
14516 "translation unit", fn);
14517 }
14518 function = unq;
14519 }
14520 }
14521 if (identifier_p (function))
14522 {
14523 if (complain & tf_error)
14524 unqualified_name_lookup_error (function);
14525 release_tree_vector (call_args);
14526 RETURN (error_mark_node);
14527 }
14528 }
14529
14530 /* Remember that there was a reference to this entity. */
14531 if (DECL_P (function))
14532 mark_used (function);
14533
14534 /* Put back tf_decltype for the actual call. */
14535 complain |= decltype_flag;
14536
14537 if (TREE_CODE (function) == OFFSET_REF)
14538 ret = build_offset_ref_call_from_tree (function, &call_args,
14539 complain);
14540 else if (TREE_CODE (function) == COMPONENT_REF)
14541 {
14542 tree instance = TREE_OPERAND (function, 0);
14543 tree fn = TREE_OPERAND (function, 1);
14544
14545 if (processing_template_decl
14546 && (type_dependent_expression_p (instance)
14547 || (!BASELINK_P (fn)
14548 && TREE_CODE (fn) != FIELD_DECL)
14549 || type_dependent_expression_p (fn)
14550 || any_type_dependent_arguments_p (call_args)))
14551 ret = build_nt_call_vec (function, call_args);
14552 else if (!BASELINK_P (fn))
14553 ret = finish_call_expr (function, &call_args,
14554 /*disallow_virtual=*/false,
14555 /*koenig_p=*/false,
14556 complain);
14557 else
14558 ret = (build_new_method_call
14559 (instance, fn,
14560 &call_args, NULL_TREE,
14561 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14562 /*fn_p=*/NULL,
14563 complain));
14564 }
14565 else
14566 ret = finish_call_expr (function, &call_args,
14567 /*disallow_virtual=*/qualified_p,
14568 koenig_p,
14569 complain);
14570
14571 release_tree_vector (call_args);
14572
14573 RETURN (ret);
14574 }
14575
14576 case COND_EXPR:
14577 {
14578 tree cond = RECUR (TREE_OPERAND (t, 0));
14579 tree exp1, exp2;
14580
14581 if (TREE_CODE (cond) == INTEGER_CST)
14582 {
14583 if (integer_zerop (cond))
14584 {
14585 ++c_inhibit_evaluation_warnings;
14586 exp1 = RECUR (TREE_OPERAND (t, 1));
14587 --c_inhibit_evaluation_warnings;
14588 exp2 = RECUR (TREE_OPERAND (t, 2));
14589 }
14590 else
14591 {
14592 exp1 = RECUR (TREE_OPERAND (t, 1));
14593 ++c_inhibit_evaluation_warnings;
14594 exp2 = RECUR (TREE_OPERAND (t, 2));
14595 --c_inhibit_evaluation_warnings;
14596 }
14597 }
14598 else
14599 {
14600 exp1 = RECUR (TREE_OPERAND (t, 1));
14601 exp2 = RECUR (TREE_OPERAND (t, 2));
14602 }
14603
14604 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14605 cond, exp1, exp2, complain));
14606 }
14607
14608 case PSEUDO_DTOR_EXPR:
14609 RETURN (finish_pseudo_destructor_expr
14610 (RECUR (TREE_OPERAND (t, 0)),
14611 RECUR (TREE_OPERAND (t, 1)),
14612 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14613 input_location));
14614
14615 case TREE_LIST:
14616 {
14617 tree purpose, value, chain;
14618
14619 if (t == void_list_node)
14620 RETURN (t);
14621
14622 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14623 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14624 {
14625 /* We have pack expansions, so expand those and
14626 create a new list out of it. */
14627 tree purposevec = NULL_TREE;
14628 tree valuevec = NULL_TREE;
14629 tree chain;
14630 int i, len = -1;
14631
14632 /* Expand the argument expressions. */
14633 if (TREE_PURPOSE (t))
14634 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14635 complain, in_decl);
14636 if (TREE_VALUE (t))
14637 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14638 complain, in_decl);
14639
14640 /* Build the rest of the list. */
14641 chain = TREE_CHAIN (t);
14642 if (chain && chain != void_type_node)
14643 chain = RECUR (chain);
14644
14645 /* Determine the number of arguments. */
14646 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14647 {
14648 len = TREE_VEC_LENGTH (purposevec);
14649 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14650 }
14651 else if (TREE_CODE (valuevec) == TREE_VEC)
14652 len = TREE_VEC_LENGTH (valuevec);
14653 else
14654 {
14655 /* Since we only performed a partial substitution into
14656 the argument pack, we only RETURN (a single list
14657 node. */
14658 if (purposevec == TREE_PURPOSE (t)
14659 && valuevec == TREE_VALUE (t)
14660 && chain == TREE_CHAIN (t))
14661 RETURN (t);
14662
14663 RETURN (tree_cons (purposevec, valuevec, chain));
14664 }
14665
14666 /* Convert the argument vectors into a TREE_LIST */
14667 i = len;
14668 while (i > 0)
14669 {
14670 /* Grab the Ith values. */
14671 i--;
14672 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14673 : NULL_TREE;
14674 value
14675 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14676 : NULL_TREE;
14677
14678 /* Build the list (backwards). */
14679 chain = tree_cons (purpose, value, chain);
14680 }
14681
14682 RETURN (chain);
14683 }
14684
14685 purpose = TREE_PURPOSE (t);
14686 if (purpose)
14687 purpose = RECUR (purpose);
14688 value = TREE_VALUE (t);
14689 if (value)
14690 value = RECUR (value);
14691 chain = TREE_CHAIN (t);
14692 if (chain && chain != void_type_node)
14693 chain = RECUR (chain);
14694 if (purpose == TREE_PURPOSE (t)
14695 && value == TREE_VALUE (t)
14696 && chain == TREE_CHAIN (t))
14697 RETURN (t);
14698 RETURN (tree_cons (purpose, value, chain));
14699 }
14700
14701 case COMPONENT_REF:
14702 {
14703 tree object;
14704 tree object_type;
14705 tree member;
14706
14707 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14708 args, complain, in_decl);
14709 /* Remember that there was a reference to this entity. */
14710 if (DECL_P (object))
14711 mark_used (object);
14712 object_type = TREE_TYPE (object);
14713
14714 member = TREE_OPERAND (t, 1);
14715 if (BASELINK_P (member))
14716 member = tsubst_baselink (member,
14717 non_reference (TREE_TYPE (object)),
14718 args, complain, in_decl);
14719 else
14720 member = tsubst_copy (member, args, complain, in_decl);
14721 if (member == error_mark_node)
14722 RETURN (error_mark_node);
14723
14724 if (type_dependent_expression_p (object))
14725 /* We can't do much here. */;
14726 else if (!CLASS_TYPE_P (object_type))
14727 {
14728 if (scalarish_type_p (object_type))
14729 {
14730 tree s = NULL_TREE;
14731 tree dtor = member;
14732
14733 if (TREE_CODE (dtor) == SCOPE_REF)
14734 {
14735 s = TREE_OPERAND (dtor, 0);
14736 dtor = TREE_OPERAND (dtor, 1);
14737 }
14738 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14739 {
14740 dtor = TREE_OPERAND (dtor, 0);
14741 if (TYPE_P (dtor))
14742 RETURN (finish_pseudo_destructor_expr
14743 (object, s, dtor, input_location));
14744 }
14745 }
14746 }
14747 else if (TREE_CODE (member) == SCOPE_REF
14748 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14749 {
14750 /* Lookup the template functions now that we know what the
14751 scope is. */
14752 tree scope = TREE_OPERAND (member, 0);
14753 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14754 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14755 member = lookup_qualified_name (scope, tmpl,
14756 /*is_type_p=*/false,
14757 /*complain=*/false);
14758 if (BASELINK_P (member))
14759 {
14760 BASELINK_FUNCTIONS (member)
14761 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14762 args);
14763 member = (adjust_result_of_qualified_name_lookup
14764 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14765 object_type));
14766 }
14767 else
14768 {
14769 qualified_name_lookup_error (scope, tmpl, member,
14770 input_location);
14771 RETURN (error_mark_node);
14772 }
14773 }
14774 else if (TREE_CODE (member) == SCOPE_REF
14775 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14776 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14777 {
14778 if (complain & tf_error)
14779 {
14780 if (TYPE_P (TREE_OPERAND (member, 0)))
14781 error ("%qT is not a class or namespace",
14782 TREE_OPERAND (member, 0));
14783 else
14784 error ("%qD is not a class or namespace",
14785 TREE_OPERAND (member, 0));
14786 }
14787 RETURN (error_mark_node);
14788 }
14789 else if (TREE_CODE (member) == FIELD_DECL)
14790 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14791
14792 RETURN (finish_class_member_access_expr (object, member,
14793 /*template_p=*/false,
14794 complain));
14795 }
14796
14797 case THROW_EXPR:
14798 RETURN (build_throw
14799 (RECUR (TREE_OPERAND (t, 0))));
14800
14801 case CONSTRUCTOR:
14802 {
14803 vec<constructor_elt, va_gc> *n;
14804 constructor_elt *ce;
14805 unsigned HOST_WIDE_INT idx;
14806 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14807 bool process_index_p;
14808 int newlen;
14809 bool need_copy_p = false;
14810 tree r;
14811
14812 if (type == error_mark_node)
14813 RETURN (error_mark_node);
14814
14815 /* digest_init will do the wrong thing if we let it. */
14816 if (type && TYPE_PTRMEMFUNC_P (type))
14817 RETURN (t);
14818
14819 /* We do not want to process the index of aggregate
14820 initializers as they are identifier nodes which will be
14821 looked up by digest_init. */
14822 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14823
14824 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14825 newlen = vec_safe_length (n);
14826 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14827 {
14828 if (ce->index && process_index_p
14829 /* An identifier index is looked up in the type
14830 being initialized, not the current scope. */
14831 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14832 ce->index = RECUR (ce->index);
14833
14834 if (PACK_EXPANSION_P (ce->value))
14835 {
14836 /* Substitute into the pack expansion. */
14837 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14838 in_decl);
14839
14840 if (ce->value == error_mark_node
14841 || PACK_EXPANSION_P (ce->value))
14842 ;
14843 else if (TREE_VEC_LENGTH (ce->value) == 1)
14844 /* Just move the argument into place. */
14845 ce->value = TREE_VEC_ELT (ce->value, 0);
14846 else
14847 {
14848 /* Update the length of the final CONSTRUCTOR
14849 arguments vector, and note that we will need to
14850 copy.*/
14851 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14852 need_copy_p = true;
14853 }
14854 }
14855 else
14856 ce->value = RECUR (ce->value);
14857 }
14858
14859 if (need_copy_p)
14860 {
14861 vec<constructor_elt, va_gc> *old_n = n;
14862
14863 vec_alloc (n, newlen);
14864 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14865 {
14866 if (TREE_CODE (ce->value) == TREE_VEC)
14867 {
14868 int i, len = TREE_VEC_LENGTH (ce->value);
14869 for (i = 0; i < len; ++i)
14870 CONSTRUCTOR_APPEND_ELT (n, 0,
14871 TREE_VEC_ELT (ce->value, i));
14872 }
14873 else
14874 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14875 }
14876 }
14877
14878 r = build_constructor (init_list_type_node, n);
14879 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14880
14881 if (TREE_HAS_CONSTRUCTOR (t))
14882 RETURN (finish_compound_literal (type, r, complain));
14883
14884 TREE_TYPE (r) = type;
14885 RETURN (r);
14886 }
14887
14888 case TYPEID_EXPR:
14889 {
14890 tree operand_0 = TREE_OPERAND (t, 0);
14891 if (TYPE_P (operand_0))
14892 {
14893 operand_0 = tsubst (operand_0, args, complain, in_decl);
14894 RETURN (get_typeid (operand_0, complain));
14895 }
14896 else
14897 {
14898 operand_0 = RECUR (operand_0);
14899 RETURN (build_typeid (operand_0, complain));
14900 }
14901 }
14902
14903 case VAR_DECL:
14904 if (!args)
14905 RETURN (t);
14906 else if (DECL_PACK_P (t))
14907 {
14908 /* We don't build decls for an instantiation of a
14909 variadic capture proxy, we instantiate the elements
14910 when needed. */
14911 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14912 return RECUR (DECL_VALUE_EXPR (t));
14913 }
14914 /* Fall through */
14915
14916 case PARM_DECL:
14917 {
14918 tree r = tsubst_copy (t, args, complain, in_decl);
14919
14920 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14921 /* If the original type was a reference, we'll be wrapped in
14922 the appropriate INDIRECT_REF. */
14923 r = convert_from_reference (r);
14924 RETURN (r);
14925 }
14926
14927 case VA_ARG_EXPR:
14928 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14929 RECUR (TREE_OPERAND (t, 0)),
14930 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14931
14932 case OFFSETOF_EXPR:
14933 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14934
14935 case TRAIT_EXPR:
14936 {
14937 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14938 complain, in_decl);
14939
14940 tree type2 = TRAIT_EXPR_TYPE2 (t);
14941 if (type2)
14942 type2 = tsubst_copy (type2, args, complain, in_decl);
14943
14944 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14945 }
14946
14947 case STMT_EXPR:
14948 {
14949 tree old_stmt_expr = cur_stmt_expr;
14950 tree stmt_expr = begin_stmt_expr ();
14951
14952 cur_stmt_expr = stmt_expr;
14953 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14954 integral_constant_expression_p);
14955 stmt_expr = finish_stmt_expr (stmt_expr, false);
14956 cur_stmt_expr = old_stmt_expr;
14957
14958 /* If the resulting list of expression statement is empty,
14959 fold it further into void_zero_node. */
14960 if (empty_expr_stmt_p (stmt_expr))
14961 stmt_expr = void_zero_node;
14962
14963 RETURN (stmt_expr);
14964 }
14965
14966 case LAMBDA_EXPR:
14967 {
14968 tree r = build_lambda_expr ();
14969
14970 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14971 LAMBDA_EXPR_CLOSURE (r) = type;
14972 CLASSTYPE_LAMBDA_EXPR (type) = r;
14973
14974 LAMBDA_EXPR_LOCATION (r)
14975 = LAMBDA_EXPR_LOCATION (t);
14976 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14977 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14978 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14979 LAMBDA_EXPR_DISCRIMINATOR (r)
14980 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14981 /* For a function scope, we want to use tsubst so that we don't
14982 complain about referring to an auto function before its return
14983 type has been deduced. Otherwise, we want to use tsubst_copy so
14984 that we look up the existing field/parameter/variable rather
14985 than build a new one. */
14986 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14987 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14988 scope = tsubst (scope, args, complain, in_decl);
14989 else if (scope && TREE_CODE (scope) == PARM_DECL)
14990 {
14991 /* Look up the parameter we want directly, as tsubst_copy
14992 doesn't do what we need. */
14993 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
14994 tree parm = FUNCTION_FIRST_USER_PARM (fn);
14995 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
14996 parm = DECL_CHAIN (parm);
14997 scope = parm;
14998 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14999 if (DECL_CONTEXT (scope) == NULL_TREE)
15000 DECL_CONTEXT (scope) = fn;
15001 }
15002 else
15003 scope = RECUR (scope);
15004 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15005 LAMBDA_EXPR_RETURN_TYPE (r)
15006 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15007
15008 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15009 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15010
15011 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15012 determine_visibility (TYPE_NAME (type));
15013 /* Now that we know visibility, instantiate the type so we have a
15014 declaration of the op() for later calls to lambda_function. */
15015 complete_type (type);
15016
15017 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15018
15019 RETURN (build_lambda_object (r));
15020 }
15021
15022 case TARGET_EXPR:
15023 /* We can get here for a constant initializer of non-dependent type.
15024 FIXME stop folding in cp_parser_initializer_clause. */
15025 {
15026 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15027 complain);
15028 RETURN (r);
15029 }
15030
15031 case TRANSACTION_EXPR:
15032 RETURN (tsubst_expr(t, args, complain, in_decl,
15033 integral_constant_expression_p));
15034
15035 case PAREN_EXPR:
15036 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15037
15038 case VEC_PERM_EXPR:
15039 RETURN (build_x_vec_perm_expr (input_location,
15040 RECUR (TREE_OPERAND (t, 0)),
15041 RECUR (TREE_OPERAND (t, 1)),
15042 RECUR (TREE_OPERAND (t, 2)),
15043 complain));
15044
15045 default:
15046 /* Handle Objective-C++ constructs, if appropriate. */
15047 {
15048 tree subst
15049 = objcp_tsubst_copy_and_build (t, args, complain,
15050 in_decl, /*function_p=*/false);
15051 if (subst)
15052 RETURN (subst);
15053 }
15054 RETURN (tsubst_copy (t, args, complain, in_decl));
15055 }
15056
15057 #undef RECUR
15058 #undef RETURN
15059 out:
15060 input_location = loc;
15061 return retval;
15062 }
15063
15064 /* Verify that the instantiated ARGS are valid. For type arguments,
15065 make sure that the type's linkage is ok. For non-type arguments,
15066 make sure they are constants if they are integral or enumerations.
15067 Emit an error under control of COMPLAIN, and return TRUE on error. */
15068
15069 static bool
15070 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15071 {
15072 if (dependent_template_arg_p (t))
15073 return false;
15074 if (ARGUMENT_PACK_P (t))
15075 {
15076 tree vec = ARGUMENT_PACK_ARGS (t);
15077 int len = TREE_VEC_LENGTH (vec);
15078 bool result = false;
15079 int i;
15080
15081 for (i = 0; i < len; ++i)
15082 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15083 result = true;
15084 return result;
15085 }
15086 else if (TYPE_P (t))
15087 {
15088 /* [basic.link]: A name with no linkage (notably, the name
15089 of a class or enumeration declared in a local scope)
15090 shall not be used to declare an entity with linkage.
15091 This implies that names with no linkage cannot be used as
15092 template arguments
15093
15094 DR 757 relaxes this restriction for C++0x. */
15095 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15096 : no_linkage_check (t, /*relaxed_p=*/false));
15097
15098 if (nt)
15099 {
15100 /* DR 488 makes use of a type with no linkage cause
15101 type deduction to fail. */
15102 if (complain & tf_error)
15103 {
15104 if (TYPE_ANONYMOUS_P (nt))
15105 error ("%qT is/uses anonymous type", t);
15106 else
15107 error ("template argument for %qD uses local type %qT",
15108 tmpl, t);
15109 }
15110 return true;
15111 }
15112 /* In order to avoid all sorts of complications, we do not
15113 allow variably-modified types as template arguments. */
15114 else if (variably_modified_type_p (t, NULL_TREE))
15115 {
15116 if (complain & tf_error)
15117 error ("%qT is a variably modified type", t);
15118 return true;
15119 }
15120 }
15121 /* Class template and alias template arguments should be OK. */
15122 else if (DECL_TYPE_TEMPLATE_P (t))
15123 ;
15124 /* A non-type argument of integral or enumerated type must be a
15125 constant. */
15126 else if (TREE_TYPE (t)
15127 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15128 && !TREE_CONSTANT (t))
15129 {
15130 if (complain & tf_error)
15131 error ("integral expression %qE is not constant", t);
15132 return true;
15133 }
15134 return false;
15135 }
15136
15137 static bool
15138 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15139 {
15140 int ix, len = DECL_NTPARMS (tmpl);
15141 bool result = false;
15142
15143 for (ix = 0; ix != len; ix++)
15144 {
15145 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15146 result = true;
15147 }
15148 if (result && (complain & tf_error))
15149 error (" trying to instantiate %qD", tmpl);
15150 return result;
15151 }
15152
15153 /* We're out of SFINAE context now, so generate diagnostics for the access
15154 errors we saw earlier when instantiating D from TMPL and ARGS. */
15155
15156 static void
15157 recheck_decl_substitution (tree d, tree tmpl, tree args)
15158 {
15159 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15160 tree type = TREE_TYPE (pattern);
15161 location_t loc = input_location;
15162
15163 push_access_scope (d);
15164 push_deferring_access_checks (dk_no_deferred);
15165 input_location = DECL_SOURCE_LOCATION (pattern);
15166 tsubst (type, args, tf_warning_or_error, d);
15167 input_location = loc;
15168 pop_deferring_access_checks ();
15169 pop_access_scope (d);
15170 }
15171
15172 /* Instantiate the indicated variable, function, or alias template TMPL with
15173 the template arguments in TARG_PTR. */
15174
15175 static tree
15176 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15177 {
15178 tree targ_ptr = orig_args;
15179 tree fndecl;
15180 tree gen_tmpl;
15181 tree spec;
15182 bool access_ok = true;
15183
15184 if (tmpl == error_mark_node)
15185 return error_mark_node;
15186
15187 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15188
15189 /* If this function is a clone, handle it specially. */
15190 if (DECL_CLONED_FUNCTION_P (tmpl))
15191 {
15192 tree spec;
15193 tree clone;
15194
15195 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15196 DECL_CLONED_FUNCTION. */
15197 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15198 targ_ptr, complain);
15199 if (spec == error_mark_node)
15200 return error_mark_node;
15201
15202 /* Look for the clone. */
15203 FOR_EACH_CLONE (clone, spec)
15204 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15205 return clone;
15206 /* We should always have found the clone by now. */
15207 gcc_unreachable ();
15208 return NULL_TREE;
15209 }
15210
15211 /* Check to see if we already have this specialization. */
15212 gen_tmpl = most_general_template (tmpl);
15213 if (tmpl != gen_tmpl)
15214 /* The TMPL is a partial instantiation. To get a full set of
15215 arguments we must add the arguments used to perform the
15216 partial instantiation. */
15217 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15218 targ_ptr);
15219
15220 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15221 but it doesn't seem to be on the hot path. */
15222 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15223
15224 gcc_assert (tmpl == gen_tmpl
15225 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15226 == spec)
15227 || fndecl == NULL_TREE);
15228
15229 if (spec != NULL_TREE)
15230 {
15231 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15232 {
15233 if (complain & tf_error)
15234 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15235 return error_mark_node;
15236 }
15237 return spec;
15238 }
15239
15240 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15241 complain))
15242 return error_mark_node;
15243
15244 /* We are building a FUNCTION_DECL, during which the access of its
15245 parameters and return types have to be checked. However this
15246 FUNCTION_DECL which is the desired context for access checking
15247 is not built yet. We solve this chicken-and-egg problem by
15248 deferring all checks until we have the FUNCTION_DECL. */
15249 push_deferring_access_checks (dk_deferred);
15250
15251 /* Instantiation of the function happens in the context of the function
15252 template, not the context of the overload resolution we're doing. */
15253 push_to_top_level ();
15254 /* If there are dependent arguments, e.g. because we're doing partial
15255 ordering, make sure processing_template_decl stays set. */
15256 if (uses_template_parms (targ_ptr))
15257 ++processing_template_decl;
15258 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15259 {
15260 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15261 complain, gen_tmpl);
15262 push_nested_class (ctx);
15263 }
15264 /* Substitute template parameters to obtain the specialization. */
15265 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15266 targ_ptr, complain, gen_tmpl);
15267 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15268 pop_nested_class ();
15269 pop_from_top_level ();
15270
15271 if (fndecl == error_mark_node)
15272 {
15273 pop_deferring_access_checks ();
15274 return error_mark_node;
15275 }
15276
15277 /* The DECL_TI_TEMPLATE should always be the immediate parent
15278 template, not the most general template. */
15279 DECL_TI_TEMPLATE (fndecl) = tmpl;
15280
15281 /* Now we know the specialization, compute access previously
15282 deferred. */
15283 push_access_scope (fndecl);
15284 if (!perform_deferred_access_checks (complain))
15285 access_ok = false;
15286 pop_access_scope (fndecl);
15287 pop_deferring_access_checks ();
15288
15289 /* If we've just instantiated the main entry point for a function,
15290 instantiate all the alternate entry points as well. We do this
15291 by cloning the instantiation of the main entry point, not by
15292 instantiating the template clones. */
15293 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15294 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15295
15296 if (!access_ok)
15297 {
15298 if (!(complain & tf_error))
15299 {
15300 /* Remember to reinstantiate when we're out of SFINAE so the user
15301 can see the errors. */
15302 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15303 }
15304 return error_mark_node;
15305 }
15306 return fndecl;
15307 }
15308
15309 /* Wrapper for instantiate_template_1. */
15310
15311 tree
15312 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15313 {
15314 tree ret;
15315 timevar_push (TV_TEMPLATE_INST);
15316 ret = instantiate_template_1 (tmpl, orig_args, complain);
15317 timevar_pop (TV_TEMPLATE_INST);
15318 return ret;
15319 }
15320
15321 /* Instantiate the alias template TMPL with ARGS. Also push a template
15322 instantiation level, which instantiate_template doesn't do because
15323 functions and variables have sufficient context established by the
15324 callers. */
15325
15326 static tree
15327 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15328 {
15329 struct pending_template *old_last_pend = last_pending_template;
15330 struct tinst_level *old_error_tinst = last_error_tinst_level;
15331 if (tmpl == error_mark_node || args == error_mark_node)
15332 return error_mark_node;
15333 tree tinst = build_tree_list (tmpl, args);
15334 if (!push_tinst_level (tinst))
15335 {
15336 ggc_free (tinst);
15337 return error_mark_node;
15338 }
15339
15340 args =
15341 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15342 args, tmpl, complain,
15343 /*require_all_args=*/true,
15344 /*use_default_args=*/true);
15345
15346 tree r = instantiate_template (tmpl, args, complain);
15347 pop_tinst_level ();
15348 /* We can't free this if a pending_template entry or last_error_tinst_level
15349 is pointing at it. */
15350 if (last_pending_template == old_last_pend
15351 && last_error_tinst_level == old_error_tinst)
15352 ggc_free (tinst);
15353
15354 return r;
15355 }
15356
15357 /* PARM is a template parameter pack for FN. Returns true iff
15358 PARM is used in a deducible way in the argument list of FN. */
15359
15360 static bool
15361 pack_deducible_p (tree parm, tree fn)
15362 {
15363 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15364 for (; t; t = TREE_CHAIN (t))
15365 {
15366 tree type = TREE_VALUE (t);
15367 tree packs;
15368 if (!PACK_EXPANSION_P (type))
15369 continue;
15370 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15371 packs; packs = TREE_CHAIN (packs))
15372 if (TREE_VALUE (packs) == parm)
15373 {
15374 /* The template parameter pack is used in a function parameter
15375 pack. If this is the end of the parameter list, the
15376 template parameter pack is deducible. */
15377 if (TREE_CHAIN (t) == void_list_node)
15378 return true;
15379 else
15380 /* Otherwise, not. Well, it could be deduced from
15381 a non-pack parameter, but doing so would end up with
15382 a deduction mismatch, so don't bother. */
15383 return false;
15384 }
15385 }
15386 /* The template parameter pack isn't used in any function parameter
15387 packs, but it might be used deeper, e.g. tuple<Args...>. */
15388 return true;
15389 }
15390
15391 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15392 NARGS elements of the arguments that are being used when calling
15393 it. TARGS is a vector into which the deduced template arguments
15394 are placed.
15395
15396 Return zero for success, 2 for an incomplete match that doesn't resolve
15397 all the types, and 1 for complete failure. An error message will be
15398 printed only for an incomplete match.
15399
15400 If FN is a conversion operator, or we are trying to produce a specific
15401 specialization, RETURN_TYPE is the return type desired.
15402
15403 The EXPLICIT_TARGS are explicit template arguments provided via a
15404 template-id.
15405
15406 The parameter STRICT is one of:
15407
15408 DEDUCE_CALL:
15409 We are deducing arguments for a function call, as in
15410 [temp.deduct.call].
15411
15412 DEDUCE_CONV:
15413 We are deducing arguments for a conversion function, as in
15414 [temp.deduct.conv].
15415
15416 DEDUCE_EXACT:
15417 We are deducing arguments when doing an explicit instantiation
15418 as in [temp.explicit], when determining an explicit specialization
15419 as in [temp.expl.spec], or when taking the address of a function
15420 template, as in [temp.deduct.funcaddr]. */
15421
15422 tree
15423 fn_type_unification (tree fn,
15424 tree explicit_targs,
15425 tree targs,
15426 const tree *args,
15427 unsigned int nargs,
15428 tree return_type,
15429 unification_kind_t strict,
15430 int flags,
15431 bool explain_p,
15432 bool decltype_p)
15433 {
15434 tree parms;
15435 tree fntype;
15436 tree decl = NULL_TREE;
15437 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15438 bool ok;
15439 static int deduction_depth;
15440 struct pending_template *old_last_pend = last_pending_template;
15441 struct tinst_level *old_error_tinst = last_error_tinst_level;
15442 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15443 tree tinst;
15444 tree r = error_mark_node;
15445
15446 if (decltype_p)
15447 complain |= tf_decltype;
15448
15449 /* In C++0x, it's possible to have a function template whose type depends
15450 on itself recursively. This is most obvious with decltype, but can also
15451 occur with enumeration scope (c++/48969). So we need to catch infinite
15452 recursion and reject the substitution at deduction time; this function
15453 will return error_mark_node for any repeated substitution.
15454
15455 This also catches excessive recursion such as when f<N> depends on
15456 f<N-1> across all integers, and returns error_mark_node for all the
15457 substitutions back up to the initial one.
15458
15459 This is, of course, not reentrant. */
15460 if (excessive_deduction_depth)
15461 return error_mark_node;
15462 tinst = build_tree_list (fn, NULL_TREE);
15463 ++deduction_depth;
15464
15465 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15466
15467 fntype = TREE_TYPE (fn);
15468 if (explicit_targs)
15469 {
15470 /* [temp.deduct]
15471
15472 The specified template arguments must match the template
15473 parameters in kind (i.e., type, nontype, template), and there
15474 must not be more arguments than there are parameters;
15475 otherwise type deduction fails.
15476
15477 Nontype arguments must match the types of the corresponding
15478 nontype template parameters, or must be convertible to the
15479 types of the corresponding nontype parameters as specified in
15480 _temp.arg.nontype_, otherwise type deduction fails.
15481
15482 All references in the function type of the function template
15483 to the corresponding template parameters are replaced by the
15484 specified template argument values. If a substitution in a
15485 template parameter or in the function type of the function
15486 template results in an invalid type, type deduction fails. */
15487 int i, len = TREE_VEC_LENGTH (tparms);
15488 location_t loc = input_location;
15489 bool incomplete = false;
15490
15491 /* Adjust any explicit template arguments before entering the
15492 substitution context. */
15493 explicit_targs
15494 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15495 complain,
15496 /*require_all_args=*/false,
15497 /*use_default_args=*/false));
15498 if (explicit_targs == error_mark_node)
15499 goto fail;
15500
15501 /* Substitute the explicit args into the function type. This is
15502 necessary so that, for instance, explicitly declared function
15503 arguments can match null pointed constants. If we were given
15504 an incomplete set of explicit args, we must not do semantic
15505 processing during substitution as we could create partial
15506 instantiations. */
15507 for (i = 0; i < len; i++)
15508 {
15509 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15510 bool parameter_pack = false;
15511 tree targ = TREE_VEC_ELT (explicit_targs, i);
15512
15513 /* Dig out the actual parm. */
15514 if (TREE_CODE (parm) == TYPE_DECL
15515 || TREE_CODE (parm) == TEMPLATE_DECL)
15516 {
15517 parm = TREE_TYPE (parm);
15518 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15519 }
15520 else if (TREE_CODE (parm) == PARM_DECL)
15521 {
15522 parm = DECL_INITIAL (parm);
15523 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15524 }
15525
15526 if (!parameter_pack && targ == NULL_TREE)
15527 /* No explicit argument for this template parameter. */
15528 incomplete = true;
15529
15530 if (parameter_pack && pack_deducible_p (parm, fn))
15531 {
15532 /* Mark the argument pack as "incomplete". We could
15533 still deduce more arguments during unification.
15534 We remove this mark in type_unification_real. */
15535 if (targ)
15536 {
15537 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15538 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15539 = ARGUMENT_PACK_ARGS (targ);
15540 }
15541
15542 /* We have some incomplete argument packs. */
15543 incomplete = true;
15544 }
15545 }
15546
15547 TREE_VALUE (tinst) = explicit_targs;
15548 if (!push_tinst_level (tinst))
15549 {
15550 excessive_deduction_depth = true;
15551 goto fail;
15552 }
15553 processing_template_decl += incomplete;
15554 input_location = DECL_SOURCE_LOCATION (fn);
15555 /* Ignore any access checks; we'll see them again in
15556 instantiate_template and they might have the wrong
15557 access path at this point. */
15558 push_deferring_access_checks (dk_deferred);
15559 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15560 complain | tf_partial, NULL_TREE);
15561 pop_deferring_access_checks ();
15562 input_location = loc;
15563 processing_template_decl -= incomplete;
15564 pop_tinst_level ();
15565
15566 if (fntype == error_mark_node)
15567 goto fail;
15568
15569 /* Place the explicitly specified arguments in TARGS. */
15570 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15571 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15572 }
15573
15574 /* Never do unification on the 'this' parameter. */
15575 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15576
15577 if (return_type)
15578 {
15579 tree *new_args;
15580
15581 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15582 new_args = XALLOCAVEC (tree, nargs + 1);
15583 new_args[0] = return_type;
15584 memcpy (new_args + 1, args, nargs * sizeof (tree));
15585 args = new_args;
15586 ++nargs;
15587 }
15588
15589 /* We allow incomplete unification without an error message here
15590 because the standard doesn't seem to explicitly prohibit it. Our
15591 callers must be ready to deal with unification failures in any
15592 event. */
15593
15594 TREE_VALUE (tinst) = targs;
15595 /* If we aren't explaining yet, push tinst context so we can see where
15596 any errors (e.g. from class instantiations triggered by instantiation
15597 of default template arguments) come from. If we are explaining, this
15598 context is redundant. */
15599 if (!explain_p && !push_tinst_level (tinst))
15600 {
15601 excessive_deduction_depth = true;
15602 goto fail;
15603 }
15604
15605 /* type_unification_real will pass back any access checks from default
15606 template argument substitution. */
15607 vec<deferred_access_check, va_gc> *checks;
15608 checks = NULL;
15609
15610 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15611 targs, parms, args, nargs, /*subr=*/0,
15612 strict, flags, &checks, explain_p);
15613 if (!explain_p)
15614 pop_tinst_level ();
15615 if (!ok)
15616 goto fail;
15617
15618 /* Now that we have bindings for all of the template arguments,
15619 ensure that the arguments deduced for the template template
15620 parameters have compatible template parameter lists. We cannot
15621 check this property before we have deduced all template
15622 arguments, because the template parameter types of a template
15623 template parameter might depend on prior template parameters
15624 deduced after the template template parameter. The following
15625 ill-formed example illustrates this issue:
15626
15627 template<typename T, template<T> class C> void f(C<5>, T);
15628
15629 template<int N> struct X {};
15630
15631 void g() {
15632 f(X<5>(), 5l); // error: template argument deduction fails
15633 }
15634
15635 The template parameter list of 'C' depends on the template type
15636 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15637 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15638 time that we deduce 'C'. */
15639 if (!template_template_parm_bindings_ok_p
15640 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15641 {
15642 unify_inconsistent_template_template_parameters (explain_p);
15643 goto fail;
15644 }
15645
15646 /* All is well so far. Now, check:
15647
15648 [temp.deduct]
15649
15650 When all template arguments have been deduced, all uses of
15651 template parameters in nondeduced contexts are replaced with
15652 the corresponding deduced argument values. If the
15653 substitution results in an invalid type, as described above,
15654 type deduction fails. */
15655 TREE_VALUE (tinst) = targs;
15656 if (!push_tinst_level (tinst))
15657 {
15658 excessive_deduction_depth = true;
15659 goto fail;
15660 }
15661
15662 /* Also collect access checks from the instantiation. */
15663 reopen_deferring_access_checks (checks);
15664
15665 decl = instantiate_template (fn, targs, complain);
15666
15667 checks = get_deferred_access_checks ();
15668 pop_deferring_access_checks ();
15669
15670 pop_tinst_level ();
15671
15672 if (decl == error_mark_node)
15673 goto fail;
15674
15675 /* Now perform any access checks encountered during substitution. */
15676 push_access_scope (decl);
15677 ok = perform_access_checks (checks, complain);
15678 pop_access_scope (decl);
15679 if (!ok)
15680 goto fail;
15681
15682 /* If we're looking for an exact match, check that what we got
15683 is indeed an exact match. It might not be if some template
15684 parameters are used in non-deduced contexts. */
15685 if (strict == DEDUCE_EXACT)
15686 {
15687 tree substed = TREE_TYPE (decl);
15688 unsigned int i;
15689
15690 tree sarg
15691 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15692 if (return_type)
15693 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15694 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15695 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15696 {
15697 unify_type_mismatch (explain_p, args[i],
15698 TREE_VALUE (sarg));
15699 goto fail;
15700 }
15701 }
15702
15703 r = decl;
15704
15705 fail:
15706 --deduction_depth;
15707 if (excessive_deduction_depth)
15708 {
15709 if (deduction_depth == 0)
15710 /* Reset once we're all the way out. */
15711 excessive_deduction_depth = false;
15712 }
15713
15714 /* We can't free this if a pending_template entry or last_error_tinst_level
15715 is pointing at it. */
15716 if (last_pending_template == old_last_pend
15717 && last_error_tinst_level == old_error_tinst)
15718 ggc_free (tinst);
15719
15720 return r;
15721 }
15722
15723 /* Adjust types before performing type deduction, as described in
15724 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15725 sections are symmetric. PARM is the type of a function parameter
15726 or the return type of the conversion function. ARG is the type of
15727 the argument passed to the call, or the type of the value
15728 initialized with the result of the conversion function.
15729 ARG_EXPR is the original argument expression, which may be null. */
15730
15731 static int
15732 maybe_adjust_types_for_deduction (unification_kind_t strict,
15733 tree* parm,
15734 tree* arg,
15735 tree arg_expr)
15736 {
15737 int result = 0;
15738
15739 switch (strict)
15740 {
15741 case DEDUCE_CALL:
15742 break;
15743
15744 case DEDUCE_CONV:
15745 {
15746 /* Swap PARM and ARG throughout the remainder of this
15747 function; the handling is precisely symmetric since PARM
15748 will initialize ARG rather than vice versa. */
15749 tree* temp = parm;
15750 parm = arg;
15751 arg = temp;
15752 break;
15753 }
15754
15755 case DEDUCE_EXACT:
15756 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15757 too, but here handle it by stripping the reference from PARM
15758 rather than by adding it to ARG. */
15759 if (TREE_CODE (*parm) == REFERENCE_TYPE
15760 && TYPE_REF_IS_RVALUE (*parm)
15761 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15762 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15763 && TREE_CODE (*arg) == REFERENCE_TYPE
15764 && !TYPE_REF_IS_RVALUE (*arg))
15765 *parm = TREE_TYPE (*parm);
15766 /* Nothing else to do in this case. */
15767 return 0;
15768
15769 default:
15770 gcc_unreachable ();
15771 }
15772
15773 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15774 {
15775 /* [temp.deduct.call]
15776
15777 If P is not a reference type:
15778
15779 --If A is an array type, the pointer type produced by the
15780 array-to-pointer standard conversion (_conv.array_) is
15781 used in place of A for type deduction; otherwise,
15782
15783 --If A is a function type, the pointer type produced by
15784 the function-to-pointer standard conversion
15785 (_conv.func_) is used in place of A for type deduction;
15786 otherwise,
15787
15788 --If A is a cv-qualified type, the top level
15789 cv-qualifiers of A's type are ignored for type
15790 deduction. */
15791 if (TREE_CODE (*arg) == ARRAY_TYPE)
15792 *arg = build_pointer_type (TREE_TYPE (*arg));
15793 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15794 *arg = build_pointer_type (*arg);
15795 else
15796 *arg = TYPE_MAIN_VARIANT (*arg);
15797 }
15798
15799 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15800 of the form T&&, where T is a template parameter, and the argument
15801 is an lvalue, T is deduced as A& */
15802 if (TREE_CODE (*parm) == REFERENCE_TYPE
15803 && TYPE_REF_IS_RVALUE (*parm)
15804 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15805 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15806 && (arg_expr ? real_lvalue_p (arg_expr)
15807 /* try_one_overload doesn't provide an arg_expr, but
15808 functions are always lvalues. */
15809 : TREE_CODE (*arg) == FUNCTION_TYPE))
15810 *arg = build_reference_type (*arg);
15811
15812 /* [temp.deduct.call]
15813
15814 If P is a cv-qualified type, the top level cv-qualifiers
15815 of P's type are ignored for type deduction. If P is a
15816 reference type, the type referred to by P is used for
15817 type deduction. */
15818 *parm = TYPE_MAIN_VARIANT (*parm);
15819 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15820 {
15821 *parm = TREE_TYPE (*parm);
15822 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15823 }
15824
15825 /* DR 322. For conversion deduction, remove a reference type on parm
15826 too (which has been swapped into ARG). */
15827 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15828 *arg = TREE_TYPE (*arg);
15829
15830 return result;
15831 }
15832
15833 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15834 template which does contain any deducible template parameters; check if
15835 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15836 unify_one_argument. */
15837
15838 static int
15839 check_non_deducible_conversion (tree parm, tree arg, int strict,
15840 int flags, bool explain_p)
15841 {
15842 tree type;
15843
15844 if (!TYPE_P (arg))
15845 type = TREE_TYPE (arg);
15846 else
15847 type = arg;
15848
15849 if (same_type_p (parm, type))
15850 return unify_success (explain_p);
15851
15852 if (strict == DEDUCE_CONV)
15853 {
15854 if (can_convert_arg (type, parm, NULL_TREE, flags,
15855 explain_p ? tf_warning_or_error : tf_none))
15856 return unify_success (explain_p);
15857 }
15858 else if (strict != DEDUCE_EXACT)
15859 {
15860 if (can_convert_arg (parm, type,
15861 TYPE_P (arg) ? NULL_TREE : arg,
15862 flags, explain_p ? tf_warning_or_error : tf_none))
15863 return unify_success (explain_p);
15864 }
15865
15866 if (strict == DEDUCE_EXACT)
15867 return unify_type_mismatch (explain_p, parm, arg);
15868 else
15869 return unify_arg_conversion (explain_p, parm, type, arg);
15870 }
15871
15872 static bool uses_deducible_template_parms (tree type);
15873
15874 /* Returns true iff the expression EXPR is one from which a template
15875 argument can be deduced. In other words, if it's an undecorated
15876 use of a template non-type parameter. */
15877
15878 static bool
15879 deducible_expression (tree expr)
15880 {
15881 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15882 }
15883
15884 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15885 deducible way; that is, if it has a max value of <PARM> - 1. */
15886
15887 static bool
15888 deducible_array_bound (tree domain)
15889 {
15890 if (domain == NULL_TREE)
15891 return false;
15892
15893 tree max = TYPE_MAX_VALUE (domain);
15894 if (TREE_CODE (max) != MINUS_EXPR)
15895 return false;
15896
15897 return deducible_expression (TREE_OPERAND (max, 0));
15898 }
15899
15900 /* Returns true iff the template arguments ARGS use a template parameter
15901 in a deducible way. */
15902
15903 static bool
15904 deducible_template_args (tree args)
15905 {
15906 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15907 {
15908 bool deducible;
15909 tree elt = TREE_VEC_ELT (args, i);
15910 if (ARGUMENT_PACK_P (elt))
15911 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15912 else
15913 {
15914 if (PACK_EXPANSION_P (elt))
15915 elt = PACK_EXPANSION_PATTERN (elt);
15916 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15917 deducible = true;
15918 else if (TYPE_P (elt))
15919 deducible = uses_deducible_template_parms (elt);
15920 else
15921 deducible = deducible_expression (elt);
15922 }
15923 if (deducible)
15924 return true;
15925 }
15926 return false;
15927 }
15928
15929 /* Returns true iff TYPE contains any deducible references to template
15930 parameters, as per 14.8.2.5. */
15931
15932 static bool
15933 uses_deducible_template_parms (tree type)
15934 {
15935 if (PACK_EXPANSION_P (type))
15936 type = PACK_EXPANSION_PATTERN (type);
15937
15938 /* T
15939 cv-list T
15940 TT<T>
15941 TT<i>
15942 TT<> */
15943 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15944 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15945 return true;
15946
15947 /* T*
15948 T&
15949 T&& */
15950 if (POINTER_TYPE_P (type))
15951 return uses_deducible_template_parms (TREE_TYPE (type));
15952
15953 /* T[integer-constant ]
15954 type [i] */
15955 if (TREE_CODE (type) == ARRAY_TYPE)
15956 return (uses_deducible_template_parms (TREE_TYPE (type))
15957 || deducible_array_bound (TYPE_DOMAIN (type)));
15958
15959 /* T type ::*
15960 type T::*
15961 T T::*
15962 T (type ::*)()
15963 type (T::*)()
15964 type (type ::*)(T)
15965 type (T::*)(T)
15966 T (type ::*)(T)
15967 T (T::*)()
15968 T (T::*)(T) */
15969 if (TYPE_PTRMEM_P (type))
15970 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15971 || (uses_deducible_template_parms
15972 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15973
15974 /* template-name <T> (where template-name refers to a class template)
15975 template-name <i> (where template-name refers to a class template) */
15976 if (CLASS_TYPE_P (type)
15977 && CLASSTYPE_TEMPLATE_INFO (type)
15978 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15979 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15980 (CLASSTYPE_TI_ARGS (type)));
15981
15982 /* type (T)
15983 T()
15984 T(T) */
15985 if (TREE_CODE (type) == FUNCTION_TYPE
15986 || TREE_CODE (type) == METHOD_TYPE)
15987 {
15988 if (uses_deducible_template_parms (TREE_TYPE (type)))
15989 return true;
15990 tree parm = TYPE_ARG_TYPES (type);
15991 if (TREE_CODE (type) == METHOD_TYPE)
15992 parm = TREE_CHAIN (parm);
15993 for (; parm; parm = TREE_CHAIN (parm))
15994 if (uses_deducible_template_parms (TREE_VALUE (parm)))
15995 return true;
15996 }
15997
15998 return false;
15999 }
16000
16001 /* Subroutine of type_unification_real and unify_pack_expansion to
16002 handle unification of a single P/A pair. Parameters are as
16003 for those functions. */
16004
16005 static int
16006 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16007 int subr, unification_kind_t strict, int flags,
16008 bool explain_p)
16009 {
16010 tree arg_expr = NULL_TREE;
16011 int arg_strict;
16012
16013 if (arg == error_mark_node || parm == error_mark_node)
16014 return unify_invalid (explain_p);
16015 if (arg == unknown_type_node)
16016 /* We can't deduce anything from this, but we might get all the
16017 template args from other function args. */
16018 return unify_success (explain_p);
16019
16020 /* Implicit conversions (Clause 4) will be performed on a function
16021 argument to convert it to the type of the corresponding function
16022 parameter if the parameter type contains no template-parameters that
16023 participate in template argument deduction. */
16024 if (TYPE_P (parm) && !uses_template_parms (parm))
16025 /* For function parameters that contain no template-parameters at all,
16026 we have historically checked for convertibility in order to shortcut
16027 consideration of this candidate. */
16028 return check_non_deducible_conversion (parm, arg, strict, flags,
16029 explain_p);
16030 else if (strict == DEDUCE_CALL
16031 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16032 /* For function parameters with only non-deducible template parameters,
16033 just return. */
16034 return unify_success (explain_p);
16035
16036 switch (strict)
16037 {
16038 case DEDUCE_CALL:
16039 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16040 | UNIFY_ALLOW_MORE_CV_QUAL
16041 | UNIFY_ALLOW_DERIVED);
16042 break;
16043
16044 case DEDUCE_CONV:
16045 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16046 break;
16047
16048 case DEDUCE_EXACT:
16049 arg_strict = UNIFY_ALLOW_NONE;
16050 break;
16051
16052 default:
16053 gcc_unreachable ();
16054 }
16055
16056 /* We only do these transformations if this is the top-level
16057 parameter_type_list in a call or declaration matching; in other
16058 situations (nested function declarators, template argument lists) we
16059 won't be comparing a type to an expression, and we don't do any type
16060 adjustments. */
16061 if (!subr)
16062 {
16063 if (!TYPE_P (arg))
16064 {
16065 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16066 if (type_unknown_p (arg))
16067 {
16068 /* [temp.deduct.type] A template-argument can be
16069 deduced from a pointer to function or pointer
16070 to member function argument if the set of
16071 overloaded functions does not contain function
16072 templates and at most one of a set of
16073 overloaded functions provides a unique
16074 match. */
16075
16076 if (resolve_overloaded_unification
16077 (tparms, targs, parm, arg, strict,
16078 arg_strict, explain_p))
16079 return unify_success (explain_p);
16080 return unify_overload_resolution_failure (explain_p, arg);
16081 }
16082
16083 arg_expr = arg;
16084 arg = unlowered_expr_type (arg);
16085 if (arg == error_mark_node)
16086 return unify_invalid (explain_p);
16087 }
16088
16089 arg_strict |=
16090 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16091 }
16092 else
16093 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16094 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16095
16096 /* For deduction from an init-list we need the actual list. */
16097 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16098 arg = arg_expr;
16099 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16100 }
16101
16102 /* Most parms like fn_type_unification.
16103
16104 If SUBR is 1, we're being called recursively (to unify the
16105 arguments of a function or method parameter of a function
16106 template).
16107
16108 CHECKS is a pointer to a vector of access checks encountered while
16109 substituting default template arguments. */
16110
16111 static int
16112 type_unification_real (tree tparms,
16113 tree targs,
16114 tree xparms,
16115 const tree *xargs,
16116 unsigned int xnargs,
16117 int subr,
16118 unification_kind_t strict,
16119 int flags,
16120 vec<deferred_access_check, va_gc> **checks,
16121 bool explain_p)
16122 {
16123 tree parm, arg;
16124 int i;
16125 int ntparms = TREE_VEC_LENGTH (tparms);
16126 int saw_undeduced = 0;
16127 tree parms;
16128 const tree *args;
16129 unsigned int nargs;
16130 unsigned int ia;
16131
16132 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16133 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16134 gcc_assert (ntparms > 0);
16135
16136 /* Reset the number of non-defaulted template arguments contained
16137 in TARGS. */
16138 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16139
16140 again:
16141 parms = xparms;
16142 args = xargs;
16143 nargs = xnargs;
16144
16145 ia = 0;
16146 while (parms && parms != void_list_node
16147 && ia < nargs)
16148 {
16149 parm = TREE_VALUE (parms);
16150
16151 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16152 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16153 /* For a function parameter pack that occurs at the end of the
16154 parameter-declaration-list, the type A of each remaining
16155 argument of the call is compared with the type P of the
16156 declarator-id of the function parameter pack. */
16157 break;
16158
16159 parms = TREE_CHAIN (parms);
16160
16161 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16162 /* For a function parameter pack that does not occur at the
16163 end of the parameter-declaration-list, the type of the
16164 parameter pack is a non-deduced context. */
16165 continue;
16166
16167 arg = args[ia];
16168 ++ia;
16169
16170 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16171 flags, explain_p))
16172 return 1;
16173 }
16174
16175 if (parms
16176 && parms != void_list_node
16177 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16178 {
16179 /* Unify the remaining arguments with the pack expansion type. */
16180 tree argvec;
16181 tree parmvec = make_tree_vec (1);
16182
16183 /* Allocate a TREE_VEC and copy in all of the arguments */
16184 argvec = make_tree_vec (nargs - ia);
16185 for (i = 0; ia < nargs; ++ia, ++i)
16186 TREE_VEC_ELT (argvec, i) = args[ia];
16187
16188 /* Copy the parameter into parmvec. */
16189 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16190 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16191 /*subr=*/subr, explain_p))
16192 return 1;
16193
16194 /* Advance to the end of the list of parameters. */
16195 parms = TREE_CHAIN (parms);
16196 }
16197
16198 /* Fail if we've reached the end of the parm list, and more args
16199 are present, and the parm list isn't variadic. */
16200 if (ia < nargs && parms == void_list_node)
16201 return unify_too_many_arguments (explain_p, nargs, ia);
16202 /* Fail if parms are left and they don't have default values. */
16203 if (parms && parms != void_list_node
16204 && TREE_PURPOSE (parms) == NULL_TREE)
16205 {
16206 unsigned int count = nargs;
16207 tree p = parms;
16208 while (p && p != void_list_node)
16209 {
16210 count++;
16211 p = TREE_CHAIN (p);
16212 }
16213 return unify_too_few_arguments (explain_p, ia, count);
16214 }
16215
16216 if (!subr)
16217 {
16218 tsubst_flags_t complain = (explain_p
16219 ? tf_warning_or_error
16220 : tf_none);
16221
16222 for (i = 0; i < ntparms; i++)
16223 {
16224 tree targ = TREE_VEC_ELT (targs, i);
16225 tree tparm = TREE_VEC_ELT (tparms, i);
16226
16227 /* Clear the "incomplete" flags on all argument packs now so that
16228 substituting them into later default arguments works. */
16229 if (targ && ARGUMENT_PACK_P (targ))
16230 {
16231 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16232 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16233 }
16234
16235 if (targ || tparm == error_mark_node)
16236 continue;
16237 tparm = TREE_VALUE (tparm);
16238
16239 /* If this is an undeduced nontype parameter that depends on
16240 a type parameter, try another pass; its type may have been
16241 deduced from a later argument than the one from which
16242 this parameter can be deduced. */
16243 if (TREE_CODE (tparm) == PARM_DECL
16244 && uses_template_parms (TREE_TYPE (tparm))
16245 && !saw_undeduced++)
16246 goto again;
16247
16248 /* Core issue #226 (C++0x) [temp.deduct]:
16249
16250 If a template argument has not been deduced, its
16251 default template argument, if any, is used.
16252
16253 When we are in C++98 mode, TREE_PURPOSE will either
16254 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16255 to explicitly check cxx_dialect here. */
16256 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16257 {
16258 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16259 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16260 reopen_deferring_access_checks (*checks);
16261 location_t save_loc = input_location;
16262 if (DECL_P (parm))
16263 input_location = DECL_SOURCE_LOCATION (parm);
16264 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16265 arg = convert_template_argument (parm, arg, targs, complain,
16266 i, NULL_TREE);
16267 input_location = save_loc;
16268 *checks = get_deferred_access_checks ();
16269 pop_deferring_access_checks ();
16270 if (arg == error_mark_node)
16271 return 1;
16272 else
16273 {
16274 TREE_VEC_ELT (targs, i) = arg;
16275 /* The position of the first default template argument,
16276 is also the number of non-defaulted arguments in TARGS.
16277 Record that. */
16278 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16279 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16280 continue;
16281 }
16282 }
16283
16284 /* If the type parameter is a parameter pack, then it will
16285 be deduced to an empty parameter pack. */
16286 if (template_parameter_pack_p (tparm))
16287 {
16288 tree arg;
16289
16290 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16291 {
16292 arg = make_node (NONTYPE_ARGUMENT_PACK);
16293 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16294 TREE_CONSTANT (arg) = 1;
16295 }
16296 else
16297 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16298
16299 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16300
16301 TREE_VEC_ELT (targs, i) = arg;
16302 continue;
16303 }
16304
16305 return unify_parameter_deduction_failure (explain_p, tparm);
16306 }
16307 }
16308 #ifdef ENABLE_CHECKING
16309 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16311 #endif
16312
16313 return unify_success (explain_p);
16314 }
16315
16316 /* Subroutine of type_unification_real. Args are like the variables
16317 at the call site. ARG is an overloaded function (or template-id);
16318 we try deducing template args from each of the overloads, and if
16319 only one succeeds, we go with that. Modifies TARGS and returns
16320 true on success. */
16321
16322 static bool
16323 resolve_overloaded_unification (tree tparms,
16324 tree targs,
16325 tree parm,
16326 tree arg,
16327 unification_kind_t strict,
16328 int sub_strict,
16329 bool explain_p)
16330 {
16331 tree tempargs = copy_node (targs);
16332 int good = 0;
16333 tree goodfn = NULL_TREE;
16334 bool addr_p;
16335
16336 if (TREE_CODE (arg) == ADDR_EXPR)
16337 {
16338 arg = TREE_OPERAND (arg, 0);
16339 addr_p = true;
16340 }
16341 else
16342 addr_p = false;
16343
16344 if (TREE_CODE (arg) == COMPONENT_REF)
16345 /* Handle `&x' where `x' is some static or non-static member
16346 function name. */
16347 arg = TREE_OPERAND (arg, 1);
16348
16349 if (TREE_CODE (arg) == OFFSET_REF)
16350 arg = TREE_OPERAND (arg, 1);
16351
16352 /* Strip baselink information. */
16353 if (BASELINK_P (arg))
16354 arg = BASELINK_FUNCTIONS (arg);
16355
16356 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16357 {
16358 /* If we got some explicit template args, we need to plug them into
16359 the affected templates before we try to unify, in case the
16360 explicit args will completely resolve the templates in question. */
16361
16362 int ok = 0;
16363 tree expl_subargs = TREE_OPERAND (arg, 1);
16364 arg = TREE_OPERAND (arg, 0);
16365
16366 for (; arg; arg = OVL_NEXT (arg))
16367 {
16368 tree fn = OVL_CURRENT (arg);
16369 tree subargs, elem;
16370
16371 if (TREE_CODE (fn) != TEMPLATE_DECL)
16372 continue;
16373
16374 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16375 expl_subargs, NULL_TREE, tf_none,
16376 /*require_all_args=*/true,
16377 /*use_default_args=*/true);
16378 if (subargs != error_mark_node
16379 && !any_dependent_template_arguments_p (subargs))
16380 {
16381 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16382 if (try_one_overload (tparms, targs, tempargs, parm,
16383 elem, strict, sub_strict, addr_p, explain_p)
16384 && (!goodfn || !same_type_p (goodfn, elem)))
16385 {
16386 goodfn = elem;
16387 ++good;
16388 }
16389 }
16390 else if (subargs)
16391 ++ok;
16392 }
16393 /* If no templates (or more than one) are fully resolved by the
16394 explicit arguments, this template-id is a non-deduced context; it
16395 could still be OK if we deduce all template arguments for the
16396 enclosing call through other arguments. */
16397 if (good != 1)
16398 good = ok;
16399 }
16400 else if (TREE_CODE (arg) != OVERLOAD
16401 && TREE_CODE (arg) != FUNCTION_DECL)
16402 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16403 -- but the deduction does not succeed because the expression is
16404 not just the function on its own. */
16405 return false;
16406 else
16407 for (; arg; arg = OVL_NEXT (arg))
16408 if (try_one_overload (tparms, targs, tempargs, parm,
16409 TREE_TYPE (OVL_CURRENT (arg)),
16410 strict, sub_strict, addr_p, explain_p)
16411 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16412 {
16413 goodfn = OVL_CURRENT (arg);
16414 ++good;
16415 }
16416
16417 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16418 to function or pointer to member function argument if the set of
16419 overloaded functions does not contain function templates and at most
16420 one of a set of overloaded functions provides a unique match.
16421
16422 So if we found multiple possibilities, we return success but don't
16423 deduce anything. */
16424
16425 if (good == 1)
16426 {
16427 int i = TREE_VEC_LENGTH (targs);
16428 for (; i--; )
16429 if (TREE_VEC_ELT (tempargs, i))
16430 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16431 }
16432 if (good)
16433 return true;
16434
16435 return false;
16436 }
16437
16438 /* Core DR 115: In contexts where deduction is done and fails, or in
16439 contexts where deduction is not done, if a template argument list is
16440 specified and it, along with any default template arguments, identifies
16441 a single function template specialization, then the template-id is an
16442 lvalue for the function template specialization. */
16443
16444 tree
16445 resolve_nondeduced_context (tree orig_expr)
16446 {
16447 tree expr, offset, baselink;
16448 bool addr;
16449
16450 if (!type_unknown_p (orig_expr))
16451 return orig_expr;
16452
16453 expr = orig_expr;
16454 addr = false;
16455 offset = NULL_TREE;
16456 baselink = NULL_TREE;
16457
16458 if (TREE_CODE (expr) == ADDR_EXPR)
16459 {
16460 expr = TREE_OPERAND (expr, 0);
16461 addr = true;
16462 }
16463 if (TREE_CODE (expr) == OFFSET_REF)
16464 {
16465 offset = expr;
16466 expr = TREE_OPERAND (expr, 1);
16467 }
16468 if (BASELINK_P (expr))
16469 {
16470 baselink = expr;
16471 expr = BASELINK_FUNCTIONS (expr);
16472 }
16473
16474 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16475 {
16476 int good = 0;
16477 tree goodfn = NULL_TREE;
16478
16479 /* If we got some explicit template args, we need to plug them into
16480 the affected templates before we try to unify, in case the
16481 explicit args will completely resolve the templates in question. */
16482
16483 tree expl_subargs = TREE_OPERAND (expr, 1);
16484 tree arg = TREE_OPERAND (expr, 0);
16485 tree badfn = NULL_TREE;
16486 tree badargs = NULL_TREE;
16487
16488 for (; arg; arg = OVL_NEXT (arg))
16489 {
16490 tree fn = OVL_CURRENT (arg);
16491 tree subargs, elem;
16492
16493 if (TREE_CODE (fn) != TEMPLATE_DECL)
16494 continue;
16495
16496 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16497 expl_subargs, NULL_TREE, tf_none,
16498 /*require_all_args=*/true,
16499 /*use_default_args=*/true);
16500 if (subargs != error_mark_node
16501 && !any_dependent_template_arguments_p (subargs))
16502 {
16503 elem = instantiate_template (fn, subargs, tf_none);
16504 if (elem == error_mark_node)
16505 {
16506 badfn = fn;
16507 badargs = subargs;
16508 }
16509 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16510 {
16511 goodfn = elem;
16512 ++good;
16513 }
16514 }
16515 }
16516 if (good == 1)
16517 {
16518 mark_used (goodfn);
16519 expr = goodfn;
16520 if (baselink)
16521 expr = build_baselink (BASELINK_BINFO (baselink),
16522 BASELINK_ACCESS_BINFO (baselink),
16523 expr, BASELINK_OPTYPE (baselink));
16524 if (offset)
16525 {
16526 tree base
16527 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16528 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16529 }
16530 if (addr)
16531 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16532 return expr;
16533 }
16534 else if (good == 0 && badargs)
16535 /* There were no good options and at least one bad one, so let the
16536 user know what the problem is. */
16537 instantiate_template (badfn, badargs, tf_warning_or_error);
16538 }
16539 return orig_expr;
16540 }
16541
16542 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16543 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16544 different overloads deduce different arguments for a given parm.
16545 ADDR_P is true if the expression for which deduction is being
16546 performed was of the form "& fn" rather than simply "fn".
16547
16548 Returns 1 on success. */
16549
16550 static int
16551 try_one_overload (tree tparms,
16552 tree orig_targs,
16553 tree targs,
16554 tree parm,
16555 tree arg,
16556 unification_kind_t strict,
16557 int sub_strict,
16558 bool addr_p,
16559 bool explain_p)
16560 {
16561 int nargs;
16562 tree tempargs;
16563 int i;
16564
16565 if (arg == error_mark_node)
16566 return 0;
16567
16568 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16569 to function or pointer to member function argument if the set of
16570 overloaded functions does not contain function templates and at most
16571 one of a set of overloaded functions provides a unique match.
16572
16573 So if this is a template, just return success. */
16574
16575 if (uses_template_parms (arg))
16576 return 1;
16577
16578 if (TREE_CODE (arg) == METHOD_TYPE)
16579 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16580 else if (addr_p)
16581 arg = build_pointer_type (arg);
16582
16583 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16584
16585 /* We don't copy orig_targs for this because if we have already deduced
16586 some template args from previous args, unify would complain when we
16587 try to deduce a template parameter for the same argument, even though
16588 there isn't really a conflict. */
16589 nargs = TREE_VEC_LENGTH (targs);
16590 tempargs = make_tree_vec (nargs);
16591
16592 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16593 return 0;
16594
16595 /* First make sure we didn't deduce anything that conflicts with
16596 explicitly specified args. */
16597 for (i = nargs; i--; )
16598 {
16599 tree elt = TREE_VEC_ELT (tempargs, i);
16600 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16601
16602 if (!elt)
16603 /*NOP*/;
16604 else if (uses_template_parms (elt))
16605 /* Since we're unifying against ourselves, we will fill in
16606 template args used in the function parm list with our own
16607 template parms. Discard them. */
16608 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16609 else if (oldelt && !template_args_equal (oldelt, elt))
16610 return 0;
16611 }
16612
16613 for (i = nargs; i--; )
16614 {
16615 tree elt = TREE_VEC_ELT (tempargs, i);
16616
16617 if (elt)
16618 TREE_VEC_ELT (targs, i) = elt;
16619 }
16620
16621 return 1;
16622 }
16623
16624 /* PARM is a template class (perhaps with unbound template
16625 parameters). ARG is a fully instantiated type. If ARG can be
16626 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16627 TARGS are as for unify. */
16628
16629 static tree
16630 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16631 bool explain_p)
16632 {
16633 tree copy_of_targs;
16634
16635 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16636 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16637 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16638 return NULL_TREE;
16639
16640 /* We need to make a new template argument vector for the call to
16641 unify. If we used TARGS, we'd clutter it up with the result of
16642 the attempted unification, even if this class didn't work out.
16643 We also don't want to commit ourselves to all the unifications
16644 we've already done, since unification is supposed to be done on
16645 an argument-by-argument basis. In other words, consider the
16646 following pathological case:
16647
16648 template <int I, int J, int K>
16649 struct S {};
16650
16651 template <int I, int J>
16652 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16653
16654 template <int I, int J, int K>
16655 void f(S<I, J, K>, S<I, I, I>);
16656
16657 void g() {
16658 S<0, 0, 0> s0;
16659 S<0, 1, 2> s2;
16660
16661 f(s0, s2);
16662 }
16663
16664 Now, by the time we consider the unification involving `s2', we
16665 already know that we must have `f<0, 0, 0>'. But, even though
16666 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16667 because there are two ways to unify base classes of S<0, 1, 2>
16668 with S<I, I, I>. If we kept the already deduced knowledge, we
16669 would reject the possibility I=1. */
16670 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16671
16672 /* If unification failed, we're done. */
16673 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16674 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16675 return NULL_TREE;
16676
16677 return arg;
16678 }
16679
16680 /* Given a template type PARM and a class type ARG, find the unique
16681 base type in ARG that is an instance of PARM. We do not examine
16682 ARG itself; only its base-classes. If there is not exactly one
16683 appropriate base class, return NULL_TREE. PARM may be the type of
16684 a partial specialization, as well as a plain template type. Used
16685 by unify. */
16686
16687 static enum template_base_result
16688 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16689 bool explain_p, tree *result)
16690 {
16691 tree rval = NULL_TREE;
16692 tree binfo;
16693
16694 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16695
16696 binfo = TYPE_BINFO (complete_type (arg));
16697 if (!binfo)
16698 {
16699 /* The type could not be completed. */
16700 *result = NULL_TREE;
16701 return tbr_incomplete_type;
16702 }
16703
16704 /* Walk in inheritance graph order. The search order is not
16705 important, and this avoids multiple walks of virtual bases. */
16706 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16707 {
16708 tree r = try_class_unification (tparms, targs, parm,
16709 BINFO_TYPE (binfo), explain_p);
16710
16711 if (r)
16712 {
16713 /* If there is more than one satisfactory baseclass, then:
16714
16715 [temp.deduct.call]
16716
16717 If they yield more than one possible deduced A, the type
16718 deduction fails.
16719
16720 applies. */
16721 if (rval && !same_type_p (r, rval))
16722 {
16723 *result = NULL_TREE;
16724 return tbr_ambiguous_baseclass;
16725 }
16726
16727 rval = r;
16728 }
16729 }
16730
16731 *result = rval;
16732 return tbr_success;
16733 }
16734
16735 /* Returns the level of DECL, which declares a template parameter. */
16736
16737 static int
16738 template_decl_level (tree decl)
16739 {
16740 switch (TREE_CODE (decl))
16741 {
16742 case TYPE_DECL:
16743 case TEMPLATE_DECL:
16744 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16745
16746 case PARM_DECL:
16747 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16748
16749 default:
16750 gcc_unreachable ();
16751 }
16752 return 0;
16753 }
16754
16755 /* Decide whether ARG can be unified with PARM, considering only the
16756 cv-qualifiers of each type, given STRICT as documented for unify.
16757 Returns nonzero iff the unification is OK on that basis. */
16758
16759 static int
16760 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16761 {
16762 int arg_quals = cp_type_quals (arg);
16763 int parm_quals = cp_type_quals (parm);
16764
16765 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16766 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16767 {
16768 /* Although a CVR qualifier is ignored when being applied to a
16769 substituted template parameter ([8.3.2]/1 for example), that
16770 does not allow us to unify "const T" with "int&" because both
16771 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16772 It is ok when we're allowing additional CV qualifiers
16773 at the outer level [14.8.2.1]/3,1st bullet. */
16774 if ((TREE_CODE (arg) == REFERENCE_TYPE
16775 || TREE_CODE (arg) == FUNCTION_TYPE
16776 || TREE_CODE (arg) == METHOD_TYPE)
16777 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16778 return 0;
16779
16780 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16781 && (parm_quals & TYPE_QUAL_RESTRICT))
16782 return 0;
16783 }
16784
16785 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16786 && (arg_quals & parm_quals) != parm_quals)
16787 return 0;
16788
16789 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16790 && (parm_quals & arg_quals) != arg_quals)
16791 return 0;
16792
16793 return 1;
16794 }
16795
16796 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16797 void
16798 template_parm_level_and_index (tree parm, int* level, int* index)
16799 {
16800 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16801 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16802 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16803 {
16804 *index = TEMPLATE_TYPE_IDX (parm);
16805 *level = TEMPLATE_TYPE_LEVEL (parm);
16806 }
16807 else
16808 {
16809 *index = TEMPLATE_PARM_IDX (parm);
16810 *level = TEMPLATE_PARM_LEVEL (parm);
16811 }
16812 }
16813
16814 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16815 do { \
16816 if (unify (TP, TA, P, A, S, EP)) \
16817 return 1; \
16818 } while (0);
16819
16820 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16821 expansion at the end of PACKED_PARMS. Returns 0 if the type
16822 deduction succeeds, 1 otherwise. STRICT is the same as in
16823 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16824 call argument list. We'll need to adjust the arguments to make them
16825 types. SUBR tells us if this is from a recursive call to
16826 type_unification_real, or for comparing two template argument
16827 lists. */
16828
16829 static int
16830 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16831 tree packed_args, unification_kind_t strict,
16832 bool subr, bool explain_p)
16833 {
16834 tree parm
16835 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16836 tree pattern = PACK_EXPANSION_PATTERN (parm);
16837 tree pack, packs = NULL_TREE;
16838 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16839 int len = TREE_VEC_LENGTH (packed_args);
16840
16841 /* Determine the parameter packs we will be deducing from the
16842 pattern, and record their current deductions. */
16843 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16844 pack; pack = TREE_CHAIN (pack))
16845 {
16846 tree parm_pack = TREE_VALUE (pack);
16847 int idx, level;
16848
16849 /* Determine the index and level of this parameter pack. */
16850 template_parm_level_and_index (parm_pack, &level, &idx);
16851
16852 /* Keep track of the parameter packs and their corresponding
16853 argument packs. */
16854 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16855 TREE_TYPE (packs) = make_tree_vec (len - start);
16856 }
16857
16858 /* Loop through all of the arguments that have not yet been
16859 unified and unify each with the pattern. */
16860 for (i = start; i < len; i++)
16861 {
16862 tree parm;
16863 bool any_explicit = false;
16864 tree arg = TREE_VEC_ELT (packed_args, i);
16865
16866 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16867 or the element of its argument pack at the current index if
16868 this argument was explicitly specified. */
16869 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16870 {
16871 int idx, level;
16872 tree arg, pargs;
16873 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16874
16875 arg = NULL_TREE;
16876 if (TREE_VALUE (pack)
16877 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16878 && (i - start < TREE_VEC_LENGTH (pargs)))
16879 {
16880 any_explicit = true;
16881 arg = TREE_VEC_ELT (pargs, i - start);
16882 }
16883 TMPL_ARG (targs, level, idx) = arg;
16884 }
16885
16886 /* If we had explicit template arguments, substitute them into the
16887 pattern before deduction. */
16888 if (any_explicit)
16889 {
16890 /* Some arguments might still be unspecified or dependent. */
16891 bool dependent;
16892 ++processing_template_decl;
16893 dependent = any_dependent_template_arguments_p (targs);
16894 if (!dependent)
16895 --processing_template_decl;
16896 parm = tsubst (pattern, targs,
16897 explain_p ? tf_warning_or_error : tf_none,
16898 NULL_TREE);
16899 if (dependent)
16900 --processing_template_decl;
16901 if (parm == error_mark_node)
16902 return 1;
16903 }
16904 else
16905 parm = pattern;
16906
16907 /* Unify the pattern with the current argument. */
16908 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16909 LOOKUP_IMPLICIT, explain_p))
16910 return 1;
16911
16912 /* For each parameter pack, collect the deduced value. */
16913 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16914 {
16915 int idx, level;
16916 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16917
16918 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16919 TMPL_ARG (targs, level, idx);
16920 }
16921 }
16922
16923 /* Verify that the results of unification with the parameter packs
16924 produce results consistent with what we've seen before, and make
16925 the deduced argument packs available. */
16926 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16927 {
16928 tree old_pack = TREE_VALUE (pack);
16929 tree new_args = TREE_TYPE (pack);
16930 int i, len = TREE_VEC_LENGTH (new_args);
16931 int idx, level;
16932 bool nondeduced_p = false;
16933
16934 /* By default keep the original deduced argument pack.
16935 If necessary, more specific code is going to update the
16936 resulting deduced argument later down in this function. */
16937 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16938 TMPL_ARG (targs, level, idx) = old_pack;
16939
16940 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16941 actually deduce anything. */
16942 for (i = 0; i < len && !nondeduced_p; ++i)
16943 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16944 nondeduced_p = true;
16945 if (nondeduced_p)
16946 continue;
16947
16948 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16949 {
16950 /* If we had fewer function args than explicit template args,
16951 just use the explicits. */
16952 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16953 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16954 if (len < explicit_len)
16955 new_args = explicit_args;
16956 }
16957
16958 if (!old_pack)
16959 {
16960 tree result;
16961 /* Build the deduced *_ARGUMENT_PACK. */
16962 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16963 {
16964 result = make_node (NONTYPE_ARGUMENT_PACK);
16965 TREE_TYPE (result) =
16966 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16967 TREE_CONSTANT (result) = 1;
16968 }
16969 else
16970 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16971
16972 SET_ARGUMENT_PACK_ARGS (result, new_args);
16973
16974 /* Note the deduced argument packs for this parameter
16975 pack. */
16976 TMPL_ARG (targs, level, idx) = result;
16977 }
16978 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16979 && (ARGUMENT_PACK_ARGS (old_pack)
16980 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16981 {
16982 /* We only had the explicitly-provided arguments before, but
16983 now we have a complete set of arguments. */
16984 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16985
16986 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16987 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16988 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16989 }
16990 else
16991 {
16992 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16993 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16994
16995 if (!comp_template_args_with_info (old_args, new_args,
16996 &bad_old_arg, &bad_new_arg))
16997 /* Inconsistent unification of this parameter pack. */
16998 return unify_parameter_pack_inconsistent (explain_p,
16999 bad_old_arg,
17000 bad_new_arg);
17001 }
17002 }
17003
17004 return unify_success (explain_p);
17005 }
17006
17007 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17008 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17009 parameters and return value are as for unify. */
17010
17011 static int
17012 unify_array_domain (tree tparms, tree targs,
17013 tree parm_dom, tree arg_dom,
17014 bool explain_p)
17015 {
17016 tree parm_max;
17017 tree arg_max;
17018 bool parm_cst;
17019 bool arg_cst;
17020
17021 /* Our representation of array types uses "N - 1" as the
17022 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17023 not an integer constant. We cannot unify arbitrarily
17024 complex expressions, so we eliminate the MINUS_EXPRs
17025 here. */
17026 parm_max = TYPE_MAX_VALUE (parm_dom);
17027 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17028 if (!parm_cst)
17029 {
17030 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17031 parm_max = TREE_OPERAND (parm_max, 0);
17032 }
17033 arg_max = TYPE_MAX_VALUE (arg_dom);
17034 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17035 if (!arg_cst)
17036 {
17037 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17038 trying to unify the type of a variable with the type
17039 of a template parameter. For example:
17040
17041 template <unsigned int N>
17042 void f (char (&) [N]);
17043 int g();
17044 void h(int i) {
17045 char a[g(i)];
17046 f(a);
17047 }
17048
17049 Here, the type of the ARG will be "int [g(i)]", and
17050 may be a SAVE_EXPR, etc. */
17051 if (TREE_CODE (arg_max) != MINUS_EXPR)
17052 return unify_vla_arg (explain_p, arg_dom);
17053 arg_max = TREE_OPERAND (arg_max, 0);
17054 }
17055
17056 /* If only one of the bounds used a MINUS_EXPR, compensate
17057 by adding one to the other bound. */
17058 if (parm_cst && !arg_cst)
17059 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17060 integer_type_node,
17061 parm_max,
17062 integer_one_node);
17063 else if (arg_cst && !parm_cst)
17064 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17065 integer_type_node,
17066 arg_max,
17067 integer_one_node);
17068
17069 return unify (tparms, targs, parm_max, arg_max,
17070 UNIFY_ALLOW_INTEGER, explain_p);
17071 }
17072
17073 /* Deduce the value of template parameters. TPARMS is the (innermost)
17074 set of template parameters to a template. TARGS is the bindings
17075 for those template parameters, as determined thus far; TARGS may
17076 include template arguments for outer levels of template parameters
17077 as well. PARM is a parameter to a template function, or a
17078 subcomponent of that parameter; ARG is the corresponding argument.
17079 This function attempts to match PARM with ARG in a manner
17080 consistent with the existing assignments in TARGS. If more values
17081 are deduced, then TARGS is updated.
17082
17083 Returns 0 if the type deduction succeeds, 1 otherwise. The
17084 parameter STRICT is a bitwise or of the following flags:
17085
17086 UNIFY_ALLOW_NONE:
17087 Require an exact match between PARM and ARG.
17088 UNIFY_ALLOW_MORE_CV_QUAL:
17089 Allow the deduced ARG to be more cv-qualified (by qualification
17090 conversion) than ARG.
17091 UNIFY_ALLOW_LESS_CV_QUAL:
17092 Allow the deduced ARG to be less cv-qualified than ARG.
17093 UNIFY_ALLOW_DERIVED:
17094 Allow the deduced ARG to be a template base class of ARG,
17095 or a pointer to a template base class of the type pointed to by
17096 ARG.
17097 UNIFY_ALLOW_INTEGER:
17098 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17099 case for more information.
17100 UNIFY_ALLOW_OUTER_LEVEL:
17101 This is the outermost level of a deduction. Used to determine validity
17102 of qualification conversions. A valid qualification conversion must
17103 have const qualified pointers leading up to the inner type which
17104 requires additional CV quals, except at the outer level, where const
17105 is not required [conv.qual]. It would be normal to set this flag in
17106 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17107 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17108 This is the outermost level of a deduction, and PARM can be more CV
17109 qualified at this point.
17110 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17111 This is the outermost level of a deduction, and PARM can be less CV
17112 qualified at this point. */
17113
17114 static int
17115 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17116 bool explain_p)
17117 {
17118 int idx;
17119 tree targ;
17120 tree tparm;
17121 int strict_in = strict;
17122
17123 /* I don't think this will do the right thing with respect to types.
17124 But the only case I've seen it in so far has been array bounds, where
17125 signedness is the only information lost, and I think that will be
17126 okay. */
17127 while (TREE_CODE (parm) == NOP_EXPR)
17128 parm = TREE_OPERAND (parm, 0);
17129
17130 if (arg == error_mark_node)
17131 return unify_invalid (explain_p);
17132 if (arg == unknown_type_node
17133 || arg == init_list_type_node)
17134 /* We can't deduce anything from this, but we might get all the
17135 template args from other function args. */
17136 return unify_success (explain_p);
17137
17138 /* If PARM uses template parameters, then we can't bail out here,
17139 even if ARG == PARM, since we won't record unifications for the
17140 template parameters. We might need them if we're trying to
17141 figure out which of two things is more specialized. */
17142 if (arg == parm && !uses_template_parms (parm))
17143 return unify_success (explain_p);
17144
17145 /* Handle init lists early, so the rest of the function can assume
17146 we're dealing with a type. */
17147 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17148 {
17149 tree elt, elttype;
17150 unsigned i;
17151 tree orig_parm = parm;
17152
17153 /* Replace T with std::initializer_list<T> for deduction. */
17154 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17155 && flag_deduce_init_list)
17156 parm = listify (parm);
17157
17158 if (!is_std_init_list (parm)
17159 && TREE_CODE (parm) != ARRAY_TYPE)
17160 /* We can only deduce from an initializer list argument if the
17161 parameter is std::initializer_list or an array; otherwise this
17162 is a non-deduced context. */
17163 return unify_success (explain_p);
17164
17165 if (TREE_CODE (parm) == ARRAY_TYPE)
17166 elttype = TREE_TYPE (parm);
17167 else
17168 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17169
17170 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17171 {
17172 int elt_strict = strict;
17173
17174 if (elt == error_mark_node)
17175 return unify_invalid (explain_p);
17176
17177 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17178 {
17179 tree type = TREE_TYPE (elt);
17180 /* It should only be possible to get here for a call. */
17181 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17182 elt_strict |= maybe_adjust_types_for_deduction
17183 (DEDUCE_CALL, &elttype, &type, elt);
17184 elt = type;
17185 }
17186
17187 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17188 explain_p);
17189 }
17190
17191 if (TREE_CODE (parm) == ARRAY_TYPE)
17192 {
17193 /* Also deduce from the length of the initializer list. */
17194 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17195 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17196 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17197 idx, explain_p);
17198 }
17199
17200 /* If the std::initializer_list<T> deduction worked, replace the
17201 deduced A with std::initializer_list<A>. */
17202 if (orig_parm != parm)
17203 {
17204 idx = TEMPLATE_TYPE_IDX (orig_parm);
17205 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17206 targ = listify (targ);
17207 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17208 }
17209 return unify_success (explain_p);
17210 }
17211
17212 /* Immediately reject some pairs that won't unify because of
17213 cv-qualification mismatches. */
17214 if (TREE_CODE (arg) == TREE_CODE (parm)
17215 && TYPE_P (arg)
17216 /* It is the elements of the array which hold the cv quals of an array
17217 type, and the elements might be template type parms. We'll check
17218 when we recurse. */
17219 && TREE_CODE (arg) != ARRAY_TYPE
17220 /* We check the cv-qualifiers when unifying with template type
17221 parameters below. We want to allow ARG `const T' to unify with
17222 PARM `T' for example, when computing which of two templates
17223 is more specialized, for example. */
17224 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17225 && !check_cv_quals_for_unify (strict_in, arg, parm))
17226 return unify_cv_qual_mismatch (explain_p, parm, arg);
17227
17228 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17229 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17230 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17231 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17232 strict &= ~UNIFY_ALLOW_DERIVED;
17233 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17234 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17235
17236 switch (TREE_CODE (parm))
17237 {
17238 case TYPENAME_TYPE:
17239 case SCOPE_REF:
17240 case UNBOUND_CLASS_TEMPLATE:
17241 /* In a type which contains a nested-name-specifier, template
17242 argument values cannot be deduced for template parameters used
17243 within the nested-name-specifier. */
17244 return unify_success (explain_p);
17245
17246 case TEMPLATE_TYPE_PARM:
17247 case TEMPLATE_TEMPLATE_PARM:
17248 case BOUND_TEMPLATE_TEMPLATE_PARM:
17249 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17250 if (tparm == error_mark_node)
17251 return unify_invalid (explain_p);
17252
17253 if (TEMPLATE_TYPE_LEVEL (parm)
17254 != template_decl_level (tparm))
17255 /* The PARM is not one we're trying to unify. Just check
17256 to see if it matches ARG. */
17257 {
17258 if (TREE_CODE (arg) == TREE_CODE (parm)
17259 && (is_auto (parm) ? is_auto (arg)
17260 : same_type_p (parm, arg)))
17261 return unify_success (explain_p);
17262 else
17263 return unify_type_mismatch (explain_p, parm, arg);
17264 }
17265 idx = TEMPLATE_TYPE_IDX (parm);
17266 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17267 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17268 if (tparm == error_mark_node)
17269 return unify_invalid (explain_p);
17270
17271 /* Check for mixed types and values. */
17272 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17273 && TREE_CODE (tparm) != TYPE_DECL)
17274 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17275 && TREE_CODE (tparm) != TEMPLATE_DECL))
17276 gcc_unreachable ();
17277
17278 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17279 {
17280 /* ARG must be constructed from a template class or a template
17281 template parameter. */
17282 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17283 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17284 return unify_template_deduction_failure (explain_p, parm, arg);
17285
17286 {
17287 tree parmvec = TYPE_TI_ARGS (parm);
17288 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17289 tree full_argvec = add_to_template_args (targs, argvec);
17290 tree parm_parms
17291 = DECL_INNERMOST_TEMPLATE_PARMS
17292 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17293 int i, len;
17294 int parm_variadic_p = 0;
17295
17296 /* The resolution to DR150 makes clear that default
17297 arguments for an N-argument may not be used to bind T
17298 to a template template parameter with fewer than N
17299 parameters. It is not safe to permit the binding of
17300 default arguments as an extension, as that may change
17301 the meaning of a conforming program. Consider:
17302
17303 struct Dense { static const unsigned int dim = 1; };
17304
17305 template <template <typename> class View,
17306 typename Block>
17307 void operator+(float, View<Block> const&);
17308
17309 template <typename Block,
17310 unsigned int Dim = Block::dim>
17311 struct Lvalue_proxy { operator float() const; };
17312
17313 void
17314 test_1d (void) {
17315 Lvalue_proxy<Dense> p;
17316 float b;
17317 b + p;
17318 }
17319
17320 Here, if Lvalue_proxy is permitted to bind to View, then
17321 the global operator+ will be used; if they are not, the
17322 Lvalue_proxy will be converted to float. */
17323 if (coerce_template_parms (parm_parms,
17324 full_argvec,
17325 TYPE_TI_TEMPLATE (parm),
17326 (explain_p
17327 ? tf_warning_or_error
17328 : tf_none),
17329 /*require_all_args=*/true,
17330 /*use_default_args=*/false)
17331 == error_mark_node)
17332 return 1;
17333
17334 /* Deduce arguments T, i from TT<T> or TT<i>.
17335 We check each element of PARMVEC and ARGVEC individually
17336 rather than the whole TREE_VEC since they can have
17337 different number of elements. */
17338
17339 parmvec = expand_template_argument_pack (parmvec);
17340 argvec = expand_template_argument_pack (argvec);
17341
17342 len = TREE_VEC_LENGTH (parmvec);
17343
17344 /* Check if the parameters end in a pack, making them
17345 variadic. */
17346 if (len > 0
17347 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17348 parm_variadic_p = 1;
17349
17350 for (i = 0; i < len - parm_variadic_p; ++i)
17351 /* If the template argument list of P contains a pack
17352 expansion that is not the last template argument, the
17353 entire template argument list is a non-deduced
17354 context. */
17355 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17356 return unify_success (explain_p);
17357
17358 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17359 return unify_too_few_arguments (explain_p,
17360 TREE_VEC_LENGTH (argvec), len);
17361
17362 for (i = 0; i < len - parm_variadic_p; ++i)
17363 {
17364 RECUR_AND_CHECK_FAILURE (tparms, targs,
17365 TREE_VEC_ELT (parmvec, i),
17366 TREE_VEC_ELT (argvec, i),
17367 UNIFY_ALLOW_NONE, explain_p);
17368 }
17369
17370 if (parm_variadic_p
17371 && unify_pack_expansion (tparms, targs,
17372 parmvec, argvec,
17373 DEDUCE_EXACT,
17374 /*subr=*/true, explain_p))
17375 return 1;
17376 }
17377 arg = TYPE_TI_TEMPLATE (arg);
17378
17379 /* Fall through to deduce template name. */
17380 }
17381
17382 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17383 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17384 {
17385 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17386
17387 /* Simple cases: Value already set, does match or doesn't. */
17388 if (targ != NULL_TREE && template_args_equal (targ, arg))
17389 return unify_success (explain_p);
17390 else if (targ)
17391 return unify_inconsistency (explain_p, parm, targ, arg);
17392 }
17393 else
17394 {
17395 /* If PARM is `const T' and ARG is only `int', we don't have
17396 a match unless we are allowing additional qualification.
17397 If ARG is `const int' and PARM is just `T' that's OK;
17398 that binds `const int' to `T'. */
17399 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17400 arg, parm))
17401 return unify_cv_qual_mismatch (explain_p, parm, arg);
17402
17403 /* Consider the case where ARG is `const volatile int' and
17404 PARM is `const T'. Then, T should be `volatile int'. */
17405 arg = cp_build_qualified_type_real
17406 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17407 if (arg == error_mark_node)
17408 return unify_invalid (explain_p);
17409
17410 /* Simple cases: Value already set, does match or doesn't. */
17411 if (targ != NULL_TREE && same_type_p (targ, arg))
17412 return unify_success (explain_p);
17413 else if (targ)
17414 return unify_inconsistency (explain_p, parm, targ, arg);
17415
17416 /* Make sure that ARG is not a variable-sized array. (Note
17417 that were talking about variable-sized arrays (like
17418 `int[n]'), rather than arrays of unknown size (like
17419 `int[]').) We'll get very confused by such a type since
17420 the bound of the array is not constant, and therefore
17421 not mangleable. Besides, such types are not allowed in
17422 ISO C++, so we can do as we please here. We do allow
17423 them for 'auto' deduction, since that isn't ABI-exposed. */
17424 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17425 return unify_vla_arg (explain_p, arg);
17426
17427 /* Strip typedefs as in convert_template_argument. */
17428 arg = canonicalize_type_argument (arg, tf_none);
17429 }
17430
17431 /* If ARG is a parameter pack or an expansion, we cannot unify
17432 against it unless PARM is also a parameter pack. */
17433 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17434 && !template_parameter_pack_p (parm))
17435 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17436
17437 /* If the argument deduction results is a METHOD_TYPE,
17438 then there is a problem.
17439 METHOD_TYPE doesn't map to any real C++ type the result of
17440 the deduction can not be of that type. */
17441 if (TREE_CODE (arg) == METHOD_TYPE)
17442 return unify_method_type_error (explain_p, arg);
17443
17444 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17445 return unify_success (explain_p);
17446
17447 case TEMPLATE_PARM_INDEX:
17448 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17449 if (tparm == error_mark_node)
17450 return unify_invalid (explain_p);
17451
17452 if (TEMPLATE_PARM_LEVEL (parm)
17453 != template_decl_level (tparm))
17454 {
17455 /* The PARM is not one we're trying to unify. Just check
17456 to see if it matches ARG. */
17457 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17458 && cp_tree_equal (parm, arg));
17459 if (result)
17460 unify_expression_unequal (explain_p, parm, arg);
17461 return result;
17462 }
17463
17464 idx = TEMPLATE_PARM_IDX (parm);
17465 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17466
17467 if (targ)
17468 {
17469 int x = !cp_tree_equal (targ, arg);
17470 if (x)
17471 unify_inconsistency (explain_p, parm, targ, arg);
17472 return x;
17473 }
17474
17475 /* [temp.deduct.type] If, in the declaration of a function template
17476 with a non-type template-parameter, the non-type
17477 template-parameter is used in an expression in the function
17478 parameter-list and, if the corresponding template-argument is
17479 deduced, the template-argument type shall match the type of the
17480 template-parameter exactly, except that a template-argument
17481 deduced from an array bound may be of any integral type.
17482 The non-type parameter might use already deduced type parameters. */
17483 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17484 if (!TREE_TYPE (arg))
17485 /* Template-parameter dependent expression. Just accept it for now.
17486 It will later be processed in convert_template_argument. */
17487 ;
17488 else if (same_type_p (TREE_TYPE (arg), tparm))
17489 /* OK */;
17490 else if ((strict & UNIFY_ALLOW_INTEGER)
17491 && CP_INTEGRAL_TYPE_P (tparm))
17492 /* Convert the ARG to the type of PARM; the deduced non-type
17493 template argument must exactly match the types of the
17494 corresponding parameter. */
17495 arg = fold (build_nop (tparm, arg));
17496 else if (uses_template_parms (tparm))
17497 /* We haven't deduced the type of this parameter yet. Try again
17498 later. */
17499 return unify_success (explain_p);
17500 else
17501 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17502
17503 /* If ARG is a parameter pack or an expansion, we cannot unify
17504 against it unless PARM is also a parameter pack. */
17505 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17506 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17507 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17508
17509 arg = strip_typedefs_expr (arg);
17510 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17511 return unify_success (explain_p);
17512
17513 case PTRMEM_CST:
17514 {
17515 /* A pointer-to-member constant can be unified only with
17516 another constant. */
17517 if (TREE_CODE (arg) != PTRMEM_CST)
17518 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17519
17520 /* Just unify the class member. It would be useless (and possibly
17521 wrong, depending on the strict flags) to unify also
17522 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17523 arg refer to the same variable, even if through different
17524 classes. For instance:
17525
17526 struct A { int x; };
17527 struct B : A { };
17528
17529 Unification of &A::x and &B::x must succeed. */
17530 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17531 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17532 }
17533
17534 case POINTER_TYPE:
17535 {
17536 if (!TYPE_PTR_P (arg))
17537 return unify_type_mismatch (explain_p, parm, arg);
17538
17539 /* [temp.deduct.call]
17540
17541 A can be another pointer or pointer to member type that can
17542 be converted to the deduced A via a qualification
17543 conversion (_conv.qual_).
17544
17545 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17546 This will allow for additional cv-qualification of the
17547 pointed-to types if appropriate. */
17548
17549 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17550 /* The derived-to-base conversion only persists through one
17551 level of pointers. */
17552 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17553
17554 return unify (tparms, targs, TREE_TYPE (parm),
17555 TREE_TYPE (arg), strict, explain_p);
17556 }
17557
17558 case REFERENCE_TYPE:
17559 if (TREE_CODE (arg) != REFERENCE_TYPE)
17560 return unify_type_mismatch (explain_p, parm, arg);
17561 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17562 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17563
17564 case ARRAY_TYPE:
17565 if (TREE_CODE (arg) != ARRAY_TYPE)
17566 return unify_type_mismatch (explain_p, parm, arg);
17567 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17568 != (TYPE_DOMAIN (arg) == NULL_TREE))
17569 return unify_type_mismatch (explain_p, parm, arg);
17570 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17571 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17572 if (TYPE_DOMAIN (parm) != NULL_TREE)
17573 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17574 TYPE_DOMAIN (arg), explain_p);
17575 return unify_success (explain_p);
17576
17577 case REAL_TYPE:
17578 case COMPLEX_TYPE:
17579 case VECTOR_TYPE:
17580 case INTEGER_TYPE:
17581 case BOOLEAN_TYPE:
17582 case ENUMERAL_TYPE:
17583 case VOID_TYPE:
17584 case NULLPTR_TYPE:
17585 if (TREE_CODE (arg) != TREE_CODE (parm))
17586 return unify_type_mismatch (explain_p, parm, arg);
17587
17588 /* We have already checked cv-qualification at the top of the
17589 function. */
17590 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17591 return unify_type_mismatch (explain_p, parm, arg);
17592
17593 /* As far as unification is concerned, this wins. Later checks
17594 will invalidate it if necessary. */
17595 return unify_success (explain_p);
17596
17597 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17598 /* Type INTEGER_CST can come from ordinary constant template args. */
17599 case INTEGER_CST:
17600 while (TREE_CODE (arg) == NOP_EXPR)
17601 arg = TREE_OPERAND (arg, 0);
17602
17603 if (TREE_CODE (arg) != INTEGER_CST)
17604 return unify_template_argument_mismatch (explain_p, parm, arg);
17605 return (tree_int_cst_equal (parm, arg)
17606 ? unify_success (explain_p)
17607 : unify_template_argument_mismatch (explain_p, parm, arg));
17608
17609 case TREE_VEC:
17610 {
17611 int i, len, argslen;
17612 int parm_variadic_p = 0;
17613
17614 if (TREE_CODE (arg) != TREE_VEC)
17615 return unify_template_argument_mismatch (explain_p, parm, arg);
17616
17617 len = TREE_VEC_LENGTH (parm);
17618 argslen = TREE_VEC_LENGTH (arg);
17619
17620 /* Check for pack expansions in the parameters. */
17621 for (i = 0; i < len; ++i)
17622 {
17623 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17624 {
17625 if (i == len - 1)
17626 /* We can unify against something with a trailing
17627 parameter pack. */
17628 parm_variadic_p = 1;
17629 else
17630 /* [temp.deduct.type]/9: If the template argument list of
17631 P contains a pack expansion that is not the last
17632 template argument, the entire template argument list
17633 is a non-deduced context. */
17634 return unify_success (explain_p);
17635 }
17636 }
17637
17638 /* If we don't have enough arguments to satisfy the parameters
17639 (not counting the pack expression at the end), or we have
17640 too many arguments for a parameter list that doesn't end in
17641 a pack expression, we can't unify. */
17642 if (parm_variadic_p
17643 ? argslen < len - parm_variadic_p
17644 : argslen != len)
17645 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17646
17647 /* Unify all of the parameters that precede the (optional)
17648 pack expression. */
17649 for (i = 0; i < len - parm_variadic_p; ++i)
17650 {
17651 RECUR_AND_CHECK_FAILURE (tparms, targs,
17652 TREE_VEC_ELT (parm, i),
17653 TREE_VEC_ELT (arg, i),
17654 UNIFY_ALLOW_NONE, explain_p);
17655 }
17656 if (parm_variadic_p)
17657 return unify_pack_expansion (tparms, targs, parm, arg,
17658 DEDUCE_EXACT,
17659 /*subr=*/true, explain_p);
17660 return unify_success (explain_p);
17661 }
17662
17663 case RECORD_TYPE:
17664 case UNION_TYPE:
17665 if (TREE_CODE (arg) != TREE_CODE (parm))
17666 return unify_type_mismatch (explain_p, parm, arg);
17667
17668 if (TYPE_PTRMEMFUNC_P (parm))
17669 {
17670 if (!TYPE_PTRMEMFUNC_P (arg))
17671 return unify_type_mismatch (explain_p, parm, arg);
17672
17673 return unify (tparms, targs,
17674 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17675 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17676 strict, explain_p);
17677 }
17678
17679 if (CLASSTYPE_TEMPLATE_INFO (parm))
17680 {
17681 tree t = NULL_TREE;
17682
17683 if (strict_in & UNIFY_ALLOW_DERIVED)
17684 {
17685 /* First, we try to unify the PARM and ARG directly. */
17686 t = try_class_unification (tparms, targs,
17687 parm, arg, explain_p);
17688
17689 if (!t)
17690 {
17691 /* Fallback to the special case allowed in
17692 [temp.deduct.call]:
17693
17694 If P is a class, and P has the form
17695 template-id, then A can be a derived class of
17696 the deduced A. Likewise, if P is a pointer to
17697 a class of the form template-id, A can be a
17698 pointer to a derived class pointed to by the
17699 deduced A. */
17700 enum template_base_result r;
17701 r = get_template_base (tparms, targs, parm, arg,
17702 explain_p, &t);
17703
17704 if (!t)
17705 return unify_no_common_base (explain_p, r, parm, arg);
17706 }
17707 }
17708 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17709 && (CLASSTYPE_TI_TEMPLATE (parm)
17710 == CLASSTYPE_TI_TEMPLATE (arg)))
17711 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17712 Then, we should unify `int' and `U'. */
17713 t = arg;
17714 else
17715 /* There's no chance of unification succeeding. */
17716 return unify_type_mismatch (explain_p, parm, arg);
17717
17718 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17719 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17720 }
17721 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17722 return unify_type_mismatch (explain_p, parm, arg);
17723 return unify_success (explain_p);
17724
17725 case METHOD_TYPE:
17726 case FUNCTION_TYPE:
17727 {
17728 unsigned int nargs;
17729 tree *args;
17730 tree a;
17731 unsigned int i;
17732
17733 if (TREE_CODE (arg) != TREE_CODE (parm))
17734 return unify_type_mismatch (explain_p, parm, arg);
17735
17736 /* CV qualifications for methods can never be deduced, they must
17737 match exactly. We need to check them explicitly here,
17738 because type_unification_real treats them as any other
17739 cv-qualified parameter. */
17740 if (TREE_CODE (parm) == METHOD_TYPE
17741 && (!check_cv_quals_for_unify
17742 (UNIFY_ALLOW_NONE,
17743 class_of_this_parm (arg),
17744 class_of_this_parm (parm))))
17745 return unify_cv_qual_mismatch (explain_p, parm, arg);
17746
17747 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17748 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17749
17750 nargs = list_length (TYPE_ARG_TYPES (arg));
17751 args = XALLOCAVEC (tree, nargs);
17752 for (a = TYPE_ARG_TYPES (arg), i = 0;
17753 a != NULL_TREE && a != void_list_node;
17754 a = TREE_CHAIN (a), ++i)
17755 args[i] = TREE_VALUE (a);
17756 nargs = i;
17757
17758 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17759 args, nargs, 1, DEDUCE_EXACT,
17760 LOOKUP_NORMAL, NULL, explain_p);
17761 }
17762
17763 case OFFSET_TYPE:
17764 /* Unify a pointer to member with a pointer to member function, which
17765 deduces the type of the member as a function type. */
17766 if (TYPE_PTRMEMFUNC_P (arg))
17767 {
17768 /* Check top-level cv qualifiers */
17769 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17770 return unify_cv_qual_mismatch (explain_p, parm, arg);
17771
17772 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17773 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17774 UNIFY_ALLOW_NONE, explain_p);
17775
17776 /* Determine the type of the function we are unifying against. */
17777 tree fntype = static_fn_type (arg);
17778
17779 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17780 }
17781
17782 if (TREE_CODE (arg) != OFFSET_TYPE)
17783 return unify_type_mismatch (explain_p, parm, arg);
17784 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17785 TYPE_OFFSET_BASETYPE (arg),
17786 UNIFY_ALLOW_NONE, explain_p);
17787 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17788 strict, explain_p);
17789
17790 case CONST_DECL:
17791 if (DECL_TEMPLATE_PARM_P (parm))
17792 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17793 if (arg != integral_constant_value (parm))
17794 return unify_template_argument_mismatch (explain_p, parm, arg);
17795 return unify_success (explain_p);
17796
17797 case FIELD_DECL:
17798 case TEMPLATE_DECL:
17799 /* Matched cases are handled by the ARG == PARM test above. */
17800 return unify_template_argument_mismatch (explain_p, parm, arg);
17801
17802 case VAR_DECL:
17803 /* A non-type template parameter that is a variable should be a
17804 an integral constant, in which case, it whould have been
17805 folded into its (constant) value. So we should not be getting
17806 a variable here. */
17807 gcc_unreachable ();
17808
17809 case TYPE_ARGUMENT_PACK:
17810 case NONTYPE_ARGUMENT_PACK:
17811 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17812 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17813
17814 case TYPEOF_TYPE:
17815 case DECLTYPE_TYPE:
17816 case UNDERLYING_TYPE:
17817 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17818 or UNDERLYING_TYPE nodes. */
17819 return unify_success (explain_p);
17820
17821 case ERROR_MARK:
17822 /* Unification fails if we hit an error node. */
17823 return unify_invalid (explain_p);
17824
17825 default:
17826 /* An unresolved overload is a nondeduced context. */
17827 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17828 return unify_success (explain_p);
17829 gcc_assert (EXPR_P (parm));
17830
17831 /* We must be looking at an expression. This can happen with
17832 something like:
17833
17834 template <int I>
17835 void foo(S<I>, S<I + 2>);
17836
17837 This is a "nondeduced context":
17838
17839 [deduct.type]
17840
17841 The nondeduced contexts are:
17842
17843 --A type that is a template-id in which one or more of
17844 the template-arguments is an expression that references
17845 a template-parameter.
17846
17847 In these cases, we assume deduction succeeded, but don't
17848 actually infer any unifications. */
17849
17850 if (!uses_template_parms (parm)
17851 && !template_args_equal (parm, arg))
17852 return unify_expression_unequal (explain_p, parm, arg);
17853 else
17854 return unify_success (explain_p);
17855 }
17856 }
17857 #undef RECUR_AND_CHECK_FAILURE
17858 \f
17859 /* Note that DECL can be defined in this translation unit, if
17860 required. */
17861
17862 static void
17863 mark_definable (tree decl)
17864 {
17865 tree clone;
17866 DECL_NOT_REALLY_EXTERN (decl) = 1;
17867 FOR_EACH_CLONE (clone, decl)
17868 DECL_NOT_REALLY_EXTERN (clone) = 1;
17869 }
17870
17871 /* Called if RESULT is explicitly instantiated, or is a member of an
17872 explicitly instantiated class. */
17873
17874 void
17875 mark_decl_instantiated (tree result, int extern_p)
17876 {
17877 SET_DECL_EXPLICIT_INSTANTIATION (result);
17878
17879 /* If this entity has already been written out, it's too late to
17880 make any modifications. */
17881 if (TREE_ASM_WRITTEN (result))
17882 return;
17883
17884 /* For anonymous namespace we don't need to do anything. */
17885 if (decl_anon_ns_mem_p (result))
17886 {
17887 gcc_assert (!TREE_PUBLIC (result));
17888 return;
17889 }
17890
17891 if (TREE_CODE (result) != FUNCTION_DECL)
17892 /* The TREE_PUBLIC flag for function declarations will have been
17893 set correctly by tsubst. */
17894 TREE_PUBLIC (result) = 1;
17895
17896 /* This might have been set by an earlier implicit instantiation. */
17897 DECL_COMDAT (result) = 0;
17898
17899 if (extern_p)
17900 DECL_NOT_REALLY_EXTERN (result) = 0;
17901 else
17902 {
17903 mark_definable (result);
17904 /* Always make artificials weak. */
17905 if (DECL_ARTIFICIAL (result) && flag_weak)
17906 comdat_linkage (result);
17907 /* For WIN32 we also want to put explicit instantiations in
17908 linkonce sections. */
17909 else if (TREE_PUBLIC (result))
17910 maybe_make_one_only (result);
17911 }
17912
17913 /* If EXTERN_P, then this function will not be emitted -- unless
17914 followed by an explicit instantiation, at which point its linkage
17915 will be adjusted. If !EXTERN_P, then this function will be
17916 emitted here. In neither circumstance do we want
17917 import_export_decl to adjust the linkage. */
17918 DECL_INTERFACE_KNOWN (result) = 1;
17919 }
17920
17921 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17922 important template arguments. If any are missing, we check whether
17923 they're important by using error_mark_node for substituting into any
17924 args that were used for partial ordering (the ones between ARGS and END)
17925 and seeing if it bubbles up. */
17926
17927 static bool
17928 check_undeduced_parms (tree targs, tree args, tree end)
17929 {
17930 bool found = false;
17931 int i;
17932 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17933 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17934 {
17935 found = true;
17936 TREE_VEC_ELT (targs, i) = error_mark_node;
17937 }
17938 if (found)
17939 {
17940 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17941 if (substed == error_mark_node)
17942 return true;
17943 }
17944 return false;
17945 }
17946
17947 /* Given two function templates PAT1 and PAT2, return:
17948
17949 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17950 -1 if PAT2 is more specialized than PAT1.
17951 0 if neither is more specialized.
17952
17953 LEN indicates the number of parameters we should consider
17954 (defaulted parameters should not be considered).
17955
17956 The 1998 std underspecified function template partial ordering, and
17957 DR214 addresses the issue. We take pairs of arguments, one from
17958 each of the templates, and deduce them against each other. One of
17959 the templates will be more specialized if all the *other*
17960 template's arguments deduce against its arguments and at least one
17961 of its arguments *does* *not* deduce against the other template's
17962 corresponding argument. Deduction is done as for class templates.
17963 The arguments used in deduction have reference and top level cv
17964 qualifiers removed. Iff both arguments were originally reference
17965 types *and* deduction succeeds in both directions, an lvalue reference
17966 wins against an rvalue reference and otherwise the template
17967 with the more cv-qualified argument wins for that pairing (if
17968 neither is more cv-qualified, they both are equal). Unlike regular
17969 deduction, after all the arguments have been deduced in this way,
17970 we do *not* verify the deduced template argument values can be
17971 substituted into non-deduced contexts.
17972
17973 The logic can be a bit confusing here, because we look at deduce1 and
17974 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17975 can find template arguments for pat1 to make arg1 look like arg2, that
17976 means that arg2 is at least as specialized as arg1. */
17977
17978 int
17979 more_specialized_fn (tree pat1, tree pat2, int len)
17980 {
17981 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17982 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17983 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17984 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17985 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17986 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17987 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17988 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17989 tree origs1, origs2;
17990 bool lose1 = false;
17991 bool lose2 = false;
17992
17993 /* Remove the this parameter from non-static member functions. If
17994 one is a non-static member function and the other is not a static
17995 member function, remove the first parameter from that function
17996 also. This situation occurs for operator functions where we
17997 locate both a member function (with this pointer) and non-member
17998 operator (with explicit first operand). */
17999 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18000 {
18001 len--; /* LEN is the number of significant arguments for DECL1 */
18002 args1 = TREE_CHAIN (args1);
18003 if (!DECL_STATIC_FUNCTION_P (decl2))
18004 args2 = TREE_CHAIN (args2);
18005 }
18006 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18007 {
18008 args2 = TREE_CHAIN (args2);
18009 if (!DECL_STATIC_FUNCTION_P (decl1))
18010 {
18011 len--;
18012 args1 = TREE_CHAIN (args1);
18013 }
18014 }
18015
18016 /* If only one is a conversion operator, they are unordered. */
18017 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18018 return 0;
18019
18020 /* Consider the return type for a conversion function */
18021 if (DECL_CONV_FN_P (decl1))
18022 {
18023 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18024 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18025 len++;
18026 }
18027
18028 processing_template_decl++;
18029
18030 origs1 = args1;
18031 origs2 = args2;
18032
18033 while (len--
18034 /* Stop when an ellipsis is seen. */
18035 && args1 != NULL_TREE && args2 != NULL_TREE)
18036 {
18037 tree arg1 = TREE_VALUE (args1);
18038 tree arg2 = TREE_VALUE (args2);
18039 int deduce1, deduce2;
18040 int quals1 = -1;
18041 int quals2 = -1;
18042 int ref1 = 0;
18043 int ref2 = 0;
18044
18045 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18046 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18047 {
18048 /* When both arguments are pack expansions, we need only
18049 unify the patterns themselves. */
18050 arg1 = PACK_EXPANSION_PATTERN (arg1);
18051 arg2 = PACK_EXPANSION_PATTERN (arg2);
18052
18053 /* This is the last comparison we need to do. */
18054 len = 0;
18055 }
18056
18057 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18058 {
18059 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18060 arg1 = TREE_TYPE (arg1);
18061 quals1 = cp_type_quals (arg1);
18062 }
18063
18064 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18065 {
18066 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18067 arg2 = TREE_TYPE (arg2);
18068 quals2 = cp_type_quals (arg2);
18069 }
18070
18071 arg1 = TYPE_MAIN_VARIANT (arg1);
18072 arg2 = TYPE_MAIN_VARIANT (arg2);
18073
18074 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18075 {
18076 int i, len2 = list_length (args2);
18077 tree parmvec = make_tree_vec (1);
18078 tree argvec = make_tree_vec (len2);
18079 tree ta = args2;
18080
18081 /* Setup the parameter vector, which contains only ARG1. */
18082 TREE_VEC_ELT (parmvec, 0) = arg1;
18083
18084 /* Setup the argument vector, which contains the remaining
18085 arguments. */
18086 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18087 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18088
18089 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18090 argvec, DEDUCE_EXACT,
18091 /*subr=*/true, /*explain_p=*/false)
18092 == 0);
18093
18094 /* We cannot deduce in the other direction, because ARG1 is
18095 a pack expansion but ARG2 is not. */
18096 deduce2 = 0;
18097 }
18098 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18099 {
18100 int i, len1 = list_length (args1);
18101 tree parmvec = make_tree_vec (1);
18102 tree argvec = make_tree_vec (len1);
18103 tree ta = args1;
18104
18105 /* Setup the parameter vector, which contains only ARG1. */
18106 TREE_VEC_ELT (parmvec, 0) = arg2;
18107
18108 /* Setup the argument vector, which contains the remaining
18109 arguments. */
18110 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18111 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18112
18113 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18114 argvec, DEDUCE_EXACT,
18115 /*subr=*/true, /*explain_p=*/false)
18116 == 0);
18117
18118 /* We cannot deduce in the other direction, because ARG2 is
18119 a pack expansion but ARG1 is not.*/
18120 deduce1 = 0;
18121 }
18122
18123 else
18124 {
18125 /* The normal case, where neither argument is a pack
18126 expansion. */
18127 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18128 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18129 == 0);
18130 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18131 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18132 == 0);
18133 }
18134
18135 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18136 arg2, then arg2 is not as specialized as arg1. */
18137 if (!deduce1)
18138 lose2 = true;
18139 if (!deduce2)
18140 lose1 = true;
18141
18142 /* "If, for a given type, deduction succeeds in both directions
18143 (i.e., the types are identical after the transformations above)
18144 and both P and A were reference types (before being replaced with
18145 the type referred to above):
18146 - if the type from the argument template was an lvalue reference and
18147 the type from the parameter template was not, the argument type is
18148 considered to be more specialized than the other; otherwise,
18149 - if the type from the argument template is more cv-qualified
18150 than the type from the parameter template (as described above),
18151 the argument type is considered to be more specialized than the other;
18152 otherwise,
18153 - neither type is more specialized than the other." */
18154
18155 if (deduce1 && deduce2)
18156 {
18157 if (ref1 && ref2 && ref1 != ref2)
18158 {
18159 if (ref1 > ref2)
18160 lose1 = true;
18161 else
18162 lose2 = true;
18163 }
18164 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18165 {
18166 if ((quals1 & quals2) == quals2)
18167 lose2 = true;
18168 if ((quals1 & quals2) == quals1)
18169 lose1 = true;
18170 }
18171 }
18172
18173 if (lose1 && lose2)
18174 /* We've failed to deduce something in either direction.
18175 These must be unordered. */
18176 break;
18177
18178 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18179 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18180 /* We have already processed all of the arguments in our
18181 handing of the pack expansion type. */
18182 len = 0;
18183
18184 args1 = TREE_CHAIN (args1);
18185 args2 = TREE_CHAIN (args2);
18186 }
18187
18188 /* "In most cases, all template parameters must have values in order for
18189 deduction to succeed, but for partial ordering purposes a template
18190 parameter may remain without a value provided it is not used in the
18191 types being used for partial ordering."
18192
18193 Thus, if we are missing any of the targs1 we need to substitute into
18194 origs1, then pat2 is not as specialized as pat1. This can happen when
18195 there is a nondeduced context. */
18196 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18197 lose2 = true;
18198 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18199 lose1 = true;
18200
18201 processing_template_decl--;
18202
18203 /* All things being equal, if the next argument is a pack expansion
18204 for one function but not for the other, prefer the
18205 non-variadic function. FIXME this is bogus; see c++/41958. */
18206 if (lose1 == lose2
18207 && args1 && TREE_VALUE (args1)
18208 && args2 && TREE_VALUE (args2))
18209 {
18210 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18211 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18212 }
18213
18214 if (lose1 == lose2)
18215 return 0;
18216 else if (!lose1)
18217 return 1;
18218 else
18219 return -1;
18220 }
18221
18222 /* Determine which of two partial specializations of MAIN_TMPL is more
18223 specialized.
18224
18225 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18226 to the first partial specialization. The TREE_VALUE is the
18227 innermost set of template parameters for the partial
18228 specialization. PAT2 is similar, but for the second template.
18229
18230 Return 1 if the first partial specialization is more specialized;
18231 -1 if the second is more specialized; 0 if neither is more
18232 specialized.
18233
18234 See [temp.class.order] for information about determining which of
18235 two templates is more specialized. */
18236
18237 static int
18238 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18239 {
18240 tree targs;
18241 tree tmpl1, tmpl2;
18242 int winner = 0;
18243 bool any_deductions = false;
18244
18245 tmpl1 = TREE_TYPE (pat1);
18246 tmpl2 = TREE_TYPE (pat2);
18247
18248 /* Just like what happens for functions, if we are ordering between
18249 different class template specializations, we may encounter dependent
18250 types in the arguments, and we need our dependency check functions
18251 to behave correctly. */
18252 ++processing_template_decl;
18253 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18254 CLASSTYPE_TI_ARGS (tmpl1),
18255 CLASSTYPE_TI_ARGS (tmpl2));
18256 if (targs)
18257 {
18258 --winner;
18259 any_deductions = true;
18260 }
18261
18262 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18263 CLASSTYPE_TI_ARGS (tmpl2),
18264 CLASSTYPE_TI_ARGS (tmpl1));
18265 if (targs)
18266 {
18267 ++winner;
18268 any_deductions = true;
18269 }
18270 --processing_template_decl;
18271
18272 /* In the case of a tie where at least one of the class templates
18273 has a parameter pack at the end, the template with the most
18274 non-packed parameters wins. */
18275 if (winner == 0
18276 && any_deductions
18277 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18278 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18279 {
18280 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18281 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18282 int len1 = TREE_VEC_LENGTH (args1);
18283 int len2 = TREE_VEC_LENGTH (args2);
18284
18285 /* We don't count the pack expansion at the end. */
18286 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18287 --len1;
18288 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18289 --len2;
18290
18291 if (len1 > len2)
18292 return 1;
18293 else if (len1 < len2)
18294 return -1;
18295 }
18296
18297 return winner;
18298 }
18299
18300 /* Return the template arguments that will produce the function signature
18301 DECL from the function template FN, with the explicit template
18302 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18303 also match. Return NULL_TREE if no satisfactory arguments could be
18304 found. */
18305
18306 static tree
18307 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18308 {
18309 int ntparms = DECL_NTPARMS (fn);
18310 tree targs = make_tree_vec (ntparms);
18311 tree decl_type = TREE_TYPE (decl);
18312 tree decl_arg_types;
18313 tree *args;
18314 unsigned int nargs, ix;
18315 tree arg;
18316
18317 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18318
18319 /* Never do unification on the 'this' parameter. */
18320 decl_arg_types = skip_artificial_parms_for (decl,
18321 TYPE_ARG_TYPES (decl_type));
18322
18323 nargs = list_length (decl_arg_types);
18324 args = XALLOCAVEC (tree, nargs);
18325 for (arg = decl_arg_types, ix = 0;
18326 arg != NULL_TREE && arg != void_list_node;
18327 arg = TREE_CHAIN (arg), ++ix)
18328 args[ix] = TREE_VALUE (arg);
18329
18330 if (fn_type_unification (fn, explicit_args, targs,
18331 args, ix,
18332 (check_rettype || DECL_CONV_FN_P (fn)
18333 ? TREE_TYPE (decl_type) : NULL_TREE),
18334 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18335 /*decltype*/false)
18336 == error_mark_node)
18337 return NULL_TREE;
18338
18339 return targs;
18340 }
18341
18342 /* Return the innermost template arguments that, when applied to a partial
18343 specialization of MAIN_TMPL whose innermost template parameters are
18344 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18345 ARGS.
18346
18347 For example, suppose we have:
18348
18349 template <class T, class U> struct S {};
18350 template <class T> struct S<T*, int> {};
18351
18352 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18353 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18354 int}. The resulting vector will be {double}, indicating that `T'
18355 is bound to `double'. */
18356
18357 static tree
18358 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18359 {
18360 int i, ntparms = TREE_VEC_LENGTH (tparms);
18361 tree deduced_args;
18362 tree innermost_deduced_args;
18363
18364 innermost_deduced_args = make_tree_vec (ntparms);
18365 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18366 {
18367 deduced_args = copy_node (args);
18368 SET_TMPL_ARGS_LEVEL (deduced_args,
18369 TMPL_ARGS_DEPTH (deduced_args),
18370 innermost_deduced_args);
18371 }
18372 else
18373 deduced_args = innermost_deduced_args;
18374
18375 if (unify (tparms, deduced_args,
18376 INNERMOST_TEMPLATE_ARGS (spec_args),
18377 INNERMOST_TEMPLATE_ARGS (args),
18378 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18379 return NULL_TREE;
18380
18381 for (i = 0; i < ntparms; ++i)
18382 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18383 return NULL_TREE;
18384
18385 /* Verify that nondeduced template arguments agree with the type
18386 obtained from argument deduction.
18387
18388 For example:
18389
18390 struct A { typedef int X; };
18391 template <class T, class U> struct C {};
18392 template <class T> struct C<T, typename T::X> {};
18393
18394 Then with the instantiation `C<A, int>', we can deduce that
18395 `T' is `A' but unify () does not check whether `typename T::X'
18396 is `int'. */
18397 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18398 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18399 spec_args, main_tmpl,
18400 tf_none, false, false);
18401 if (spec_args == error_mark_node
18402 /* We only need to check the innermost arguments; the other
18403 arguments will always agree. */
18404 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18405 INNERMOST_TEMPLATE_ARGS (args)))
18406 return NULL_TREE;
18407
18408 /* Now that we have bindings for all of the template arguments,
18409 ensure that the arguments deduced for the template template
18410 parameters have compatible template parameter lists. See the use
18411 of template_template_parm_bindings_ok_p in fn_type_unification
18412 for more information. */
18413 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18414 return NULL_TREE;
18415
18416 return deduced_args;
18417 }
18418
18419 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18420 Return the TREE_LIST node with the most specialized template, if
18421 any. If there is no most specialized template, the error_mark_node
18422 is returned.
18423
18424 Note that this function does not look at, or modify, the
18425 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18426 returned is one of the elements of INSTANTIATIONS, callers may
18427 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18428 and retrieve it from the value returned. */
18429
18430 tree
18431 most_specialized_instantiation (tree templates)
18432 {
18433 tree fn, champ;
18434
18435 ++processing_template_decl;
18436
18437 champ = templates;
18438 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18439 {
18440 int fate = 0;
18441
18442 if (get_bindings (TREE_VALUE (champ),
18443 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18444 NULL_TREE, /*check_ret=*/true))
18445 fate--;
18446
18447 if (get_bindings (TREE_VALUE (fn),
18448 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18449 NULL_TREE, /*check_ret=*/true))
18450 fate++;
18451
18452 if (fate == -1)
18453 champ = fn;
18454 else if (!fate)
18455 {
18456 /* Equally specialized, move to next function. If there
18457 is no next function, nothing's most specialized. */
18458 fn = TREE_CHAIN (fn);
18459 champ = fn;
18460 if (!fn)
18461 break;
18462 }
18463 }
18464
18465 if (champ)
18466 /* Now verify that champ is better than everything earlier in the
18467 instantiation list. */
18468 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18469 if (get_bindings (TREE_VALUE (champ),
18470 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18471 NULL_TREE, /*check_ret=*/true)
18472 || !get_bindings (TREE_VALUE (fn),
18473 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18474 NULL_TREE, /*check_ret=*/true))
18475 {
18476 champ = NULL_TREE;
18477 break;
18478 }
18479
18480 processing_template_decl--;
18481
18482 if (!champ)
18483 return error_mark_node;
18484
18485 return champ;
18486 }
18487
18488 /* If DECL is a specialization of some template, return the most
18489 general such template. Otherwise, returns NULL_TREE.
18490
18491 For example, given:
18492
18493 template <class T> struct S { template <class U> void f(U); };
18494
18495 if TMPL is `template <class U> void S<int>::f(U)' this will return
18496 the full template. This function will not trace past partial
18497 specializations, however. For example, given in addition:
18498
18499 template <class T> struct S<T*> { template <class U> void f(U); };
18500
18501 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18502 `template <class T> template <class U> S<T*>::f(U)'. */
18503
18504 tree
18505 most_general_template (tree decl)
18506 {
18507 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18508 an immediate specialization. */
18509 if (TREE_CODE (decl) == FUNCTION_DECL)
18510 {
18511 if (DECL_TEMPLATE_INFO (decl)) {
18512 decl = DECL_TI_TEMPLATE (decl);
18513
18514 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18515 template friend. */
18516 if (TREE_CODE (decl) != TEMPLATE_DECL)
18517 return NULL_TREE;
18518 } else
18519 return NULL_TREE;
18520 }
18521
18522 /* Look for more and more general templates. */
18523 while (DECL_TEMPLATE_INFO (decl))
18524 {
18525 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18526 (See cp-tree.h for details.) */
18527 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18528 break;
18529
18530 if (CLASS_TYPE_P (TREE_TYPE (decl))
18531 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18532 break;
18533
18534 /* Stop if we run into an explicitly specialized class template. */
18535 if (!DECL_NAMESPACE_SCOPE_P (decl)
18536 && DECL_CONTEXT (decl)
18537 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18538 break;
18539
18540 decl = DECL_TI_TEMPLATE (decl);
18541 }
18542
18543 return decl;
18544 }
18545
18546 /* Return the most specialized of the class template partial
18547 specializations of TMPL which can produce TYPE, a specialization of
18548 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18549 a _TYPE node corresponding to the partial specialization, while the
18550 TREE_PURPOSE is the set of template arguments that must be
18551 substituted into the TREE_TYPE in order to generate TYPE.
18552
18553 If the choice of partial specialization is ambiguous, a diagnostic
18554 is issued, and the error_mark_node is returned. If there are no
18555 partial specializations of TMPL matching TYPE, then NULL_TREE is
18556 returned. */
18557
18558 static tree
18559 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18560 {
18561 tree list = NULL_TREE;
18562 tree t;
18563 tree champ;
18564 int fate;
18565 bool ambiguous_p;
18566 tree args;
18567 tree outer_args = NULL_TREE;
18568
18569 tmpl = most_general_template (tmpl);
18570 args = CLASSTYPE_TI_ARGS (type);
18571
18572 /* For determining which partial specialization to use, only the
18573 innermost args are interesting. */
18574 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18575 {
18576 outer_args = strip_innermost_template_args (args, 1);
18577 args = INNERMOST_TEMPLATE_ARGS (args);
18578 }
18579
18580 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18581 {
18582 tree partial_spec_args;
18583 tree spec_args;
18584 tree spec_tmpl = TREE_VALUE (t);
18585 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18586
18587 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18588
18589 ++processing_template_decl;
18590
18591 if (outer_args)
18592 {
18593 /* Discard the outer levels of args, and then substitute in the
18594 template args from the enclosing class. */
18595 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18596 partial_spec_args = tsubst_template_args
18597 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18598
18599 /* And the same for the partial specialization TEMPLATE_DECL. */
18600 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18601 }
18602
18603 partial_spec_args =
18604 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18605 add_to_template_args (outer_args,
18606 partial_spec_args),
18607 tmpl, tf_none,
18608 /*require_all_args=*/true,
18609 /*use_default_args=*/true);
18610
18611 --processing_template_decl;
18612
18613 if (partial_spec_args == error_mark_node)
18614 return error_mark_node;
18615 if (spec_tmpl == error_mark_node)
18616 return error_mark_node;
18617
18618 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18619 spec_args = get_class_bindings (tmpl, parms,
18620 partial_spec_args,
18621 args);
18622 if (spec_args)
18623 {
18624 if (outer_args)
18625 spec_args = add_to_template_args (outer_args, spec_args);
18626 list = tree_cons (spec_args, orig_parms, list);
18627 TREE_TYPE (list) = TREE_TYPE (t);
18628 }
18629 }
18630
18631 if (! list)
18632 return NULL_TREE;
18633
18634 ambiguous_p = false;
18635 t = list;
18636 champ = t;
18637 t = TREE_CHAIN (t);
18638 for (; t; t = TREE_CHAIN (t))
18639 {
18640 fate = more_specialized_class (tmpl, champ, t);
18641 if (fate == 1)
18642 ;
18643 else
18644 {
18645 if (fate == 0)
18646 {
18647 t = TREE_CHAIN (t);
18648 if (! t)
18649 {
18650 ambiguous_p = true;
18651 break;
18652 }
18653 }
18654 champ = t;
18655 }
18656 }
18657
18658 if (!ambiguous_p)
18659 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18660 {
18661 fate = more_specialized_class (tmpl, champ, t);
18662 if (fate != 1)
18663 {
18664 ambiguous_p = true;
18665 break;
18666 }
18667 }
18668
18669 if (ambiguous_p)
18670 {
18671 const char *str;
18672 char *spaces = NULL;
18673 if (!(complain & tf_error))
18674 return error_mark_node;
18675 error ("ambiguous class template instantiation for %q#T", type);
18676 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18677 for (t = list; t; t = TREE_CHAIN (t))
18678 {
18679 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18680 spaces = spaces ? spaces : get_spaces (str);
18681 }
18682 free (spaces);
18683 return error_mark_node;
18684 }
18685
18686 return champ;
18687 }
18688
18689 /* Explicitly instantiate DECL. */
18690
18691 void
18692 do_decl_instantiation (tree decl, tree storage)
18693 {
18694 tree result = NULL_TREE;
18695 int extern_p = 0;
18696
18697 if (!decl || decl == error_mark_node)
18698 /* An error occurred, for which grokdeclarator has already issued
18699 an appropriate message. */
18700 return;
18701 else if (! DECL_LANG_SPECIFIC (decl))
18702 {
18703 error ("explicit instantiation of non-template %q#D", decl);
18704 return;
18705 }
18706 else if (VAR_P (decl))
18707 {
18708 /* There is an asymmetry here in the way VAR_DECLs and
18709 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18710 the latter, the DECL we get back will be marked as a
18711 template instantiation, and the appropriate
18712 DECL_TEMPLATE_INFO will be set up. This does not happen for
18713 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18714 should handle VAR_DECLs as it currently handles
18715 FUNCTION_DECLs. */
18716 if (!DECL_CLASS_SCOPE_P (decl))
18717 {
18718 error ("%qD is not a static data member of a class template", decl);
18719 return;
18720 }
18721 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18722 if (!result || !VAR_P (result))
18723 {
18724 error ("no matching template for %qD found", decl);
18725 return;
18726 }
18727 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18728 {
18729 error ("type %qT for explicit instantiation %qD does not match "
18730 "declared type %qT", TREE_TYPE (result), decl,
18731 TREE_TYPE (decl));
18732 return;
18733 }
18734 }
18735 else if (TREE_CODE (decl) != FUNCTION_DECL)
18736 {
18737 error ("explicit instantiation of %q#D", decl);
18738 return;
18739 }
18740 else
18741 result = decl;
18742
18743 /* Check for various error cases. Note that if the explicit
18744 instantiation is valid the RESULT will currently be marked as an
18745 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18746 until we get here. */
18747
18748 if (DECL_TEMPLATE_SPECIALIZATION (result))
18749 {
18750 /* DR 259 [temp.spec].
18751
18752 Both an explicit instantiation and a declaration of an explicit
18753 specialization shall not appear in a program unless the explicit
18754 instantiation follows a declaration of the explicit specialization.
18755
18756 For a given set of template parameters, if an explicit
18757 instantiation of a template appears after a declaration of an
18758 explicit specialization for that template, the explicit
18759 instantiation has no effect. */
18760 return;
18761 }
18762 else if (DECL_EXPLICIT_INSTANTIATION (result))
18763 {
18764 /* [temp.spec]
18765
18766 No program shall explicitly instantiate any template more
18767 than once.
18768
18769 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18770 the first instantiation was `extern' and the second is not,
18771 and EXTERN_P for the opposite case. */
18772 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18773 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18774 /* If an "extern" explicit instantiation follows an ordinary
18775 explicit instantiation, the template is instantiated. */
18776 if (extern_p)
18777 return;
18778 }
18779 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18780 {
18781 error ("no matching template for %qD found", result);
18782 return;
18783 }
18784 else if (!DECL_TEMPLATE_INFO (result))
18785 {
18786 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18787 return;
18788 }
18789
18790 if (storage == NULL_TREE)
18791 ;
18792 else if (storage == ridpointers[(int) RID_EXTERN])
18793 {
18794 if (!in_system_header && (cxx_dialect == cxx98))
18795 pedwarn (input_location, OPT_Wpedantic,
18796 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18797 "instantiations");
18798 extern_p = 1;
18799 }
18800 else
18801 error ("storage class %qD applied to template instantiation", storage);
18802
18803 check_explicit_instantiation_namespace (result);
18804 mark_decl_instantiated (result, extern_p);
18805 if (! extern_p)
18806 instantiate_decl (result, /*defer_ok=*/1,
18807 /*expl_inst_class_mem_p=*/false);
18808 }
18809
18810 static void
18811 mark_class_instantiated (tree t, int extern_p)
18812 {
18813 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18814 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18815 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18816 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18817 if (! extern_p)
18818 {
18819 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18820 rest_of_type_compilation (t, 1);
18821 }
18822 }
18823
18824 /* Called from do_type_instantiation through binding_table_foreach to
18825 do recursive instantiation for the type bound in ENTRY. */
18826 static void
18827 bt_instantiate_type_proc (binding_entry entry, void *data)
18828 {
18829 tree storage = *(tree *) data;
18830
18831 if (MAYBE_CLASS_TYPE_P (entry->type)
18832 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18833 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18834 }
18835
18836 /* Called from do_type_instantiation to instantiate a member
18837 (a member function or a static member variable) of an
18838 explicitly instantiated class template. */
18839 static void
18840 instantiate_class_member (tree decl, int extern_p)
18841 {
18842 mark_decl_instantiated (decl, extern_p);
18843 if (! extern_p)
18844 instantiate_decl (decl, /*defer_ok=*/1,
18845 /*expl_inst_class_mem_p=*/true);
18846 }
18847
18848 /* Perform an explicit instantiation of template class T. STORAGE, if
18849 non-null, is the RID for extern, inline or static. COMPLAIN is
18850 nonzero if this is called from the parser, zero if called recursively,
18851 since the standard is unclear (as detailed below). */
18852
18853 void
18854 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18855 {
18856 int extern_p = 0;
18857 int nomem_p = 0;
18858 int static_p = 0;
18859 int previous_instantiation_extern_p = 0;
18860
18861 if (TREE_CODE (t) == TYPE_DECL)
18862 t = TREE_TYPE (t);
18863
18864 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18865 {
18866 tree tmpl =
18867 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18868 if (tmpl)
18869 error ("explicit instantiation of non-class template %qD", tmpl);
18870 else
18871 error ("explicit instantiation of non-template type %qT", t);
18872 return;
18873 }
18874
18875 complete_type (t);
18876
18877 if (!COMPLETE_TYPE_P (t))
18878 {
18879 if (complain & tf_error)
18880 error ("explicit instantiation of %q#T before definition of template",
18881 t);
18882 return;
18883 }
18884
18885 if (storage != NULL_TREE)
18886 {
18887 if (!in_system_header)
18888 {
18889 if (storage == ridpointers[(int) RID_EXTERN])
18890 {
18891 if (cxx_dialect == cxx98)
18892 pedwarn (input_location, OPT_Wpedantic,
18893 "ISO C++ 1998 forbids the use of %<extern%> on "
18894 "explicit instantiations");
18895 }
18896 else
18897 pedwarn (input_location, OPT_Wpedantic,
18898 "ISO C++ forbids the use of %qE"
18899 " on explicit instantiations", storage);
18900 }
18901
18902 if (storage == ridpointers[(int) RID_INLINE])
18903 nomem_p = 1;
18904 else if (storage == ridpointers[(int) RID_EXTERN])
18905 extern_p = 1;
18906 else if (storage == ridpointers[(int) RID_STATIC])
18907 static_p = 1;
18908 else
18909 {
18910 error ("storage class %qD applied to template instantiation",
18911 storage);
18912 extern_p = 0;
18913 }
18914 }
18915
18916 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18917 {
18918 /* DR 259 [temp.spec].
18919
18920 Both an explicit instantiation and a declaration of an explicit
18921 specialization shall not appear in a program unless the explicit
18922 instantiation follows a declaration of the explicit specialization.
18923
18924 For a given set of template parameters, if an explicit
18925 instantiation of a template appears after a declaration of an
18926 explicit specialization for that template, the explicit
18927 instantiation has no effect. */
18928 return;
18929 }
18930 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18931 {
18932 /* [temp.spec]
18933
18934 No program shall explicitly instantiate any template more
18935 than once.
18936
18937 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18938 instantiation was `extern'. If EXTERN_P then the second is.
18939 These cases are OK. */
18940 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18941
18942 if (!previous_instantiation_extern_p && !extern_p
18943 && (complain & tf_error))
18944 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18945
18946 /* If we've already instantiated the template, just return now. */
18947 if (!CLASSTYPE_INTERFACE_ONLY (t))
18948 return;
18949 }
18950
18951 check_explicit_instantiation_namespace (TYPE_NAME (t));
18952 mark_class_instantiated (t, extern_p);
18953
18954 if (nomem_p)
18955 return;
18956
18957 {
18958 tree tmp;
18959
18960 /* In contrast to implicit instantiation, where only the
18961 declarations, and not the definitions, of members are
18962 instantiated, we have here:
18963
18964 [temp.explicit]
18965
18966 The explicit instantiation of a class template specialization
18967 implies the instantiation of all of its members not
18968 previously explicitly specialized in the translation unit
18969 containing the explicit instantiation.
18970
18971 Of course, we can't instantiate member template classes, since
18972 we don't have any arguments for them. Note that the standard
18973 is unclear on whether the instantiation of the members are
18974 *explicit* instantiations or not. However, the most natural
18975 interpretation is that it should be an explicit instantiation. */
18976
18977 if (! static_p)
18978 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18979 if (TREE_CODE (tmp) == FUNCTION_DECL
18980 && DECL_TEMPLATE_INSTANTIATION (tmp))
18981 instantiate_class_member (tmp, extern_p);
18982
18983 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18984 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
18985 instantiate_class_member (tmp, extern_p);
18986
18987 if (CLASSTYPE_NESTED_UTDS (t))
18988 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18989 bt_instantiate_type_proc, &storage);
18990 }
18991 }
18992
18993 /* Given a function DECL, which is a specialization of TMPL, modify
18994 DECL to be a re-instantiation of TMPL with the same template
18995 arguments. TMPL should be the template into which tsubst'ing
18996 should occur for DECL, not the most general template.
18997
18998 One reason for doing this is a scenario like this:
18999
19000 template <class T>
19001 void f(const T&, int i);
19002
19003 void g() { f(3, 7); }
19004
19005 template <class T>
19006 void f(const T& t, const int i) { }
19007
19008 Note that when the template is first instantiated, with
19009 instantiate_template, the resulting DECL will have no name for the
19010 first parameter, and the wrong type for the second. So, when we go
19011 to instantiate the DECL, we regenerate it. */
19012
19013 static void
19014 regenerate_decl_from_template (tree decl, tree tmpl)
19015 {
19016 /* The arguments used to instantiate DECL, from the most general
19017 template. */
19018 tree args;
19019 tree code_pattern;
19020
19021 args = DECL_TI_ARGS (decl);
19022 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19023
19024 /* Make sure that we can see identifiers, and compute access
19025 correctly. */
19026 push_access_scope (decl);
19027
19028 if (TREE_CODE (decl) == FUNCTION_DECL)
19029 {
19030 tree decl_parm;
19031 tree pattern_parm;
19032 tree specs;
19033 int args_depth;
19034 int parms_depth;
19035
19036 args_depth = TMPL_ARGS_DEPTH (args);
19037 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19038 if (args_depth > parms_depth)
19039 args = get_innermost_template_args (args, parms_depth);
19040
19041 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19042 args, tf_error, NULL_TREE,
19043 /*defer_ok*/false);
19044 if (specs && specs != error_mark_node)
19045 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19046 specs);
19047
19048 /* Merge parameter declarations. */
19049 decl_parm = skip_artificial_parms_for (decl,
19050 DECL_ARGUMENTS (decl));
19051 pattern_parm
19052 = skip_artificial_parms_for (code_pattern,
19053 DECL_ARGUMENTS (code_pattern));
19054 while (decl_parm && !DECL_PACK_P (pattern_parm))
19055 {
19056 tree parm_type;
19057 tree attributes;
19058
19059 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19060 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19061 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19062 NULL_TREE);
19063 parm_type = type_decays_to (parm_type);
19064 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19065 TREE_TYPE (decl_parm) = parm_type;
19066 attributes = DECL_ATTRIBUTES (pattern_parm);
19067 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19068 {
19069 DECL_ATTRIBUTES (decl_parm) = attributes;
19070 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19071 }
19072 decl_parm = DECL_CHAIN (decl_parm);
19073 pattern_parm = DECL_CHAIN (pattern_parm);
19074 }
19075 /* Merge any parameters that match with the function parameter
19076 pack. */
19077 if (pattern_parm && DECL_PACK_P (pattern_parm))
19078 {
19079 int i, len;
19080 tree expanded_types;
19081 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19082 the parameters in this function parameter pack. */
19083 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19084 args, tf_error, NULL_TREE);
19085 len = TREE_VEC_LENGTH (expanded_types);
19086 for (i = 0; i < len; i++)
19087 {
19088 tree parm_type;
19089 tree attributes;
19090
19091 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19092 /* Rename the parameter to include the index. */
19093 DECL_NAME (decl_parm) =
19094 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19095 parm_type = TREE_VEC_ELT (expanded_types, i);
19096 parm_type = type_decays_to (parm_type);
19097 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19098 TREE_TYPE (decl_parm) = parm_type;
19099 attributes = DECL_ATTRIBUTES (pattern_parm);
19100 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19101 {
19102 DECL_ATTRIBUTES (decl_parm) = attributes;
19103 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19104 }
19105 decl_parm = DECL_CHAIN (decl_parm);
19106 }
19107 }
19108 /* Merge additional specifiers from the CODE_PATTERN. */
19109 if (DECL_DECLARED_INLINE_P (code_pattern)
19110 && !DECL_DECLARED_INLINE_P (decl))
19111 DECL_DECLARED_INLINE_P (decl) = 1;
19112 }
19113 else if (VAR_P (decl))
19114 {
19115 DECL_INITIAL (decl) =
19116 tsubst_expr (DECL_INITIAL (code_pattern), args,
19117 tf_error, DECL_TI_TEMPLATE (decl),
19118 /*integral_constant_expression_p=*/false);
19119 if (VAR_HAD_UNKNOWN_BOUND (decl))
19120 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19121 tf_error, DECL_TI_TEMPLATE (decl));
19122 }
19123 else
19124 gcc_unreachable ();
19125
19126 pop_access_scope (decl);
19127 }
19128
19129 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19130 substituted to get DECL. */
19131
19132 tree
19133 template_for_substitution (tree decl)
19134 {
19135 tree tmpl = DECL_TI_TEMPLATE (decl);
19136
19137 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19138 for the instantiation. This is not always the most general
19139 template. Consider, for example:
19140
19141 template <class T>
19142 struct S { template <class U> void f();
19143 template <> void f<int>(); };
19144
19145 and an instantiation of S<double>::f<int>. We want TD to be the
19146 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19147 while (/* An instantiation cannot have a definition, so we need a
19148 more general template. */
19149 DECL_TEMPLATE_INSTANTIATION (tmpl)
19150 /* We must also deal with friend templates. Given:
19151
19152 template <class T> struct S {
19153 template <class U> friend void f() {};
19154 };
19155
19156 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19157 so far as the language is concerned, but that's still
19158 where we get the pattern for the instantiation from. On
19159 other hand, if the definition comes outside the class, say:
19160
19161 template <class T> struct S {
19162 template <class U> friend void f();
19163 };
19164 template <class U> friend void f() {}
19165
19166 we don't need to look any further. That's what the check for
19167 DECL_INITIAL is for. */
19168 || (TREE_CODE (decl) == FUNCTION_DECL
19169 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19170 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19171 {
19172 /* The present template, TD, should not be a definition. If it
19173 were a definition, we should be using it! Note that we
19174 cannot restructure the loop to just keep going until we find
19175 a template with a definition, since that might go too far if
19176 a specialization was declared, but not defined. */
19177 gcc_assert (!VAR_P (decl)
19178 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19179
19180 /* Fetch the more general template. */
19181 tmpl = DECL_TI_TEMPLATE (tmpl);
19182 }
19183
19184 return tmpl;
19185 }
19186
19187 /* Returns true if we need to instantiate this template instance even if we
19188 know we aren't going to emit it.. */
19189
19190 bool
19191 always_instantiate_p (tree decl)
19192 {
19193 /* We always instantiate inline functions so that we can inline them. An
19194 explicit instantiation declaration prohibits implicit instantiation of
19195 non-inline functions. With high levels of optimization, we would
19196 normally inline non-inline functions -- but we're not allowed to do
19197 that for "extern template" functions. Therefore, we check
19198 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19199 return ((TREE_CODE (decl) == FUNCTION_DECL
19200 && (DECL_DECLARED_INLINE_P (decl)
19201 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19202 /* And we need to instantiate static data members so that
19203 their initializers are available in integral constant
19204 expressions. */
19205 || (VAR_P (decl)
19206 && decl_maybe_constant_var_p (decl)));
19207 }
19208
19209 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19210 instantiate it now, modifying TREE_TYPE (fn). */
19211
19212 void
19213 maybe_instantiate_noexcept (tree fn)
19214 {
19215 tree fntype, spec, noex, clone;
19216
19217 if (DECL_CLONED_FUNCTION_P (fn))
19218 fn = DECL_CLONED_FUNCTION (fn);
19219 fntype = TREE_TYPE (fn);
19220 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19221
19222 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19223 return;
19224
19225 noex = TREE_PURPOSE (spec);
19226
19227 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19228 {
19229 if (push_tinst_level (fn))
19230 {
19231 push_access_scope (fn);
19232 push_deferring_access_checks (dk_no_deferred);
19233 input_location = DECL_SOURCE_LOCATION (fn);
19234 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19235 DEFERRED_NOEXCEPT_ARGS (noex),
19236 tf_warning_or_error, fn,
19237 /*function_p=*/false,
19238 /*integral_constant_expression_p=*/true);
19239 pop_deferring_access_checks ();
19240 pop_access_scope (fn);
19241 pop_tinst_level ();
19242 spec = build_noexcept_spec (noex, tf_warning_or_error);
19243 if (spec == error_mark_node)
19244 spec = noexcept_false_spec;
19245 }
19246 else
19247 spec = noexcept_false_spec;
19248 }
19249 else
19250 {
19251 /* This is an implicitly declared function, so NOEX is a list of
19252 other functions to evaluate and merge. */
19253 tree elt;
19254 spec = noexcept_true_spec;
19255 for (elt = noex; elt; elt = OVL_NEXT (elt))
19256 {
19257 tree fn = OVL_CURRENT (elt);
19258 tree subspec;
19259 maybe_instantiate_noexcept (fn);
19260 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19261 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19262 }
19263 }
19264
19265 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19266
19267 FOR_EACH_CLONE (clone, fn)
19268 {
19269 if (TREE_TYPE (clone) == fntype)
19270 TREE_TYPE (clone) = TREE_TYPE (fn);
19271 else
19272 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19273 }
19274 }
19275
19276 /* Produce the definition of D, a _DECL generated from a template. If
19277 DEFER_OK is nonzero, then we don't have to actually do the
19278 instantiation now; we just have to do it sometime. Normally it is
19279 an error if this is an explicit instantiation but D is undefined.
19280 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19281 explicitly instantiated class template. */
19282
19283 tree
19284 instantiate_decl (tree d, int defer_ok,
19285 bool expl_inst_class_mem_p)
19286 {
19287 tree tmpl = DECL_TI_TEMPLATE (d);
19288 tree gen_args;
19289 tree args;
19290 tree td;
19291 tree code_pattern;
19292 tree spec;
19293 tree gen_tmpl;
19294 bool pattern_defined;
19295 location_t saved_loc = input_location;
19296 int saved_unevaluated_operand = cp_unevaluated_operand;
19297 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19298 bool external_p;
19299 tree fn_context;
19300 bool nested;
19301
19302 /* This function should only be used to instantiate templates for
19303 functions and static member variables. */
19304 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19305
19306 /* Variables are never deferred; if instantiation is required, they
19307 are instantiated right away. That allows for better code in the
19308 case that an expression refers to the value of the variable --
19309 if the variable has a constant value the referring expression can
19310 take advantage of that fact. */
19311 if (VAR_P (d)
19312 || DECL_DECLARED_CONSTEXPR_P (d))
19313 defer_ok = 0;
19314
19315 /* Don't instantiate cloned functions. Instead, instantiate the
19316 functions they cloned. */
19317 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19318 d = DECL_CLONED_FUNCTION (d);
19319
19320 if (DECL_TEMPLATE_INSTANTIATED (d)
19321 || (TREE_CODE (d) == FUNCTION_DECL
19322 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19323 || DECL_TEMPLATE_SPECIALIZATION (d))
19324 /* D has already been instantiated or explicitly specialized, so
19325 there's nothing for us to do here.
19326
19327 It might seem reasonable to check whether or not D is an explicit
19328 instantiation, and, if so, stop here. But when an explicit
19329 instantiation is deferred until the end of the compilation,
19330 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19331 the instantiation. */
19332 return d;
19333
19334 /* Check to see whether we know that this template will be
19335 instantiated in some other file, as with "extern template"
19336 extension. */
19337 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19338
19339 /* In general, we do not instantiate such templates. */
19340 if (external_p && !always_instantiate_p (d))
19341 return d;
19342
19343 gen_tmpl = most_general_template (tmpl);
19344 gen_args = DECL_TI_ARGS (d);
19345
19346 if (tmpl != gen_tmpl)
19347 /* We should already have the extra args. */
19348 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19349 == TMPL_ARGS_DEPTH (gen_args));
19350 /* And what's in the hash table should match D. */
19351 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19352 || spec == NULL_TREE);
19353
19354 /* This needs to happen before any tsubsting. */
19355 if (! push_tinst_level (d))
19356 return d;
19357
19358 timevar_push (TV_TEMPLATE_INST);
19359
19360 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19361 for the instantiation. */
19362 td = template_for_substitution (d);
19363 code_pattern = DECL_TEMPLATE_RESULT (td);
19364
19365 /* We should never be trying to instantiate a member of a class
19366 template or partial specialization. */
19367 gcc_assert (d != code_pattern);
19368
19369 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19370 || DECL_TEMPLATE_SPECIALIZATION (td))
19371 /* In the case of a friend template whose definition is provided
19372 outside the class, we may have too many arguments. Drop the
19373 ones we don't need. The same is true for specializations. */
19374 args = get_innermost_template_args
19375 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19376 else
19377 args = gen_args;
19378
19379 if (TREE_CODE (d) == FUNCTION_DECL)
19380 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19381 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19382 else
19383 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19384
19385 /* We may be in the middle of deferred access check. Disable it now. */
19386 push_deferring_access_checks (dk_no_deferred);
19387
19388 /* Unless an explicit instantiation directive has already determined
19389 the linkage of D, remember that a definition is available for
19390 this entity. */
19391 if (pattern_defined
19392 && !DECL_INTERFACE_KNOWN (d)
19393 && !DECL_NOT_REALLY_EXTERN (d))
19394 mark_definable (d);
19395
19396 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19397 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19398 input_location = DECL_SOURCE_LOCATION (d);
19399
19400 /* If D is a member of an explicitly instantiated class template,
19401 and no definition is available, treat it like an implicit
19402 instantiation. */
19403 if (!pattern_defined && expl_inst_class_mem_p
19404 && DECL_EXPLICIT_INSTANTIATION (d))
19405 {
19406 /* Leave linkage flags alone on instantiations with anonymous
19407 visibility. */
19408 if (TREE_PUBLIC (d))
19409 {
19410 DECL_NOT_REALLY_EXTERN (d) = 0;
19411 DECL_INTERFACE_KNOWN (d) = 0;
19412 }
19413 SET_DECL_IMPLICIT_INSTANTIATION (d);
19414 }
19415
19416 if (TREE_CODE (d) == FUNCTION_DECL)
19417 maybe_instantiate_noexcept (d);
19418
19419 /* Defer all other templates, unless we have been explicitly
19420 forbidden from doing so. */
19421 if (/* If there is no definition, we cannot instantiate the
19422 template. */
19423 ! pattern_defined
19424 /* If it's OK to postpone instantiation, do so. */
19425 || defer_ok
19426 /* If this is a static data member that will be defined
19427 elsewhere, we don't want to instantiate the entire data
19428 member, but we do want to instantiate the initializer so that
19429 we can substitute that elsewhere. */
19430 || (external_p && VAR_P (d)))
19431 {
19432 /* The definition of the static data member is now required so
19433 we must substitute the initializer. */
19434 if (VAR_P (d)
19435 && !DECL_INITIAL (d)
19436 && DECL_INITIAL (code_pattern))
19437 {
19438 tree ns;
19439 tree init;
19440 bool const_init = false;
19441
19442 ns = decl_namespace_context (d);
19443 push_nested_namespace (ns);
19444 push_nested_class (DECL_CONTEXT (d));
19445 init = tsubst_expr (DECL_INITIAL (code_pattern),
19446 args,
19447 tf_warning_or_error, NULL_TREE,
19448 /*integral_constant_expression_p=*/false);
19449 /* Make sure the initializer is still constant, in case of
19450 circular dependency (template/instantiate6.C). */
19451 const_init
19452 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19453 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19454 /*asmspec_tree=*/NULL_TREE,
19455 LOOKUP_ONLYCONVERTING);
19456 pop_nested_class ();
19457 pop_nested_namespace (ns);
19458 }
19459
19460 /* We restore the source position here because it's used by
19461 add_pending_template. */
19462 input_location = saved_loc;
19463
19464 if (at_eof && !pattern_defined
19465 && DECL_EXPLICIT_INSTANTIATION (d)
19466 && DECL_NOT_REALLY_EXTERN (d))
19467 /* [temp.explicit]
19468
19469 The definition of a non-exported function template, a
19470 non-exported member function template, or a non-exported
19471 member function or static data member of a class template
19472 shall be present in every translation unit in which it is
19473 explicitly instantiated. */
19474 permerror (input_location, "explicit instantiation of %qD "
19475 "but no definition available", d);
19476
19477 /* If we're in unevaluated context, we just wanted to get the
19478 constant value; this isn't an odr use, so don't queue
19479 a full instantiation. */
19480 if (cp_unevaluated_operand != 0)
19481 goto out;
19482 /* ??? Historically, we have instantiated inline functions, even
19483 when marked as "extern template". */
19484 if (!(external_p && VAR_P (d)))
19485 add_pending_template (d);
19486 goto out;
19487 }
19488 /* Tell the repository that D is available in this translation unit
19489 -- and see if it is supposed to be instantiated here. */
19490 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19491 {
19492 /* In a PCH file, despite the fact that the repository hasn't
19493 requested instantiation in the PCH it is still possible that
19494 an instantiation will be required in a file that includes the
19495 PCH. */
19496 if (pch_file)
19497 add_pending_template (d);
19498 /* Instantiate inline functions so that the inliner can do its
19499 job, even though we'll not be emitting a copy of this
19500 function. */
19501 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19502 goto out;
19503 }
19504
19505 fn_context = decl_function_context (d);
19506 nested = (current_function_decl != NULL_TREE);
19507 if (!fn_context)
19508 push_to_top_level ();
19509 else
19510 {
19511 if (nested)
19512 push_function_context ();
19513 cp_unevaluated_operand = 0;
19514 c_inhibit_evaluation_warnings = 0;
19515 }
19516
19517 /* Mark D as instantiated so that recursive calls to
19518 instantiate_decl do not try to instantiate it again. */
19519 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19520
19521 /* Regenerate the declaration in case the template has been modified
19522 by a subsequent redeclaration. */
19523 regenerate_decl_from_template (d, td);
19524
19525 /* We already set the file and line above. Reset them now in case
19526 they changed as a result of calling regenerate_decl_from_template. */
19527 input_location = DECL_SOURCE_LOCATION (d);
19528
19529 if (VAR_P (d))
19530 {
19531 tree init;
19532 bool const_init = false;
19533
19534 /* Clear out DECL_RTL; whatever was there before may not be right
19535 since we've reset the type of the declaration. */
19536 SET_DECL_RTL (d, NULL);
19537 DECL_IN_AGGR_P (d) = 0;
19538
19539 /* The initializer is placed in DECL_INITIAL by
19540 regenerate_decl_from_template so we don't need to
19541 push/pop_access_scope again here. Pull it out so that
19542 cp_finish_decl can process it. */
19543 init = DECL_INITIAL (d);
19544 DECL_INITIAL (d) = NULL_TREE;
19545 DECL_INITIALIZED_P (d) = 0;
19546
19547 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19548 initializer. That function will defer actual emission until
19549 we have a chance to determine linkage. */
19550 DECL_EXTERNAL (d) = 0;
19551
19552 /* Enter the scope of D so that access-checking works correctly. */
19553 push_nested_class (DECL_CONTEXT (d));
19554 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19555 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19556 pop_nested_class ();
19557 }
19558 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19559 synthesize_method (d);
19560 else if (TREE_CODE (d) == FUNCTION_DECL)
19561 {
19562 struct pointer_map_t *saved_local_specializations;
19563 tree subst_decl;
19564 tree tmpl_parm;
19565 tree spec_parm;
19566 tree block = NULL_TREE;
19567
19568 /* Save away the current list, in case we are instantiating one
19569 template from within the body of another. */
19570 saved_local_specializations = local_specializations;
19571
19572 /* Set up the list of local specializations. */
19573 local_specializations = pointer_map_create ();
19574
19575 /* Set up context. */
19576 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19577 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19578 block = push_stmt_list ();
19579 else
19580 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19581
19582 /* Some typedefs referenced from within the template code need to be
19583 access checked at template instantiation time, i.e now. These
19584 types were added to the template at parsing time. Let's get those
19585 and perform the access checks then. */
19586 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19587 gen_args);
19588
19589 /* Create substitution entries for the parameters. */
19590 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19591 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19592 spec_parm = DECL_ARGUMENTS (d);
19593 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19594 {
19595 register_local_specialization (spec_parm, tmpl_parm);
19596 spec_parm = skip_artificial_parms_for (d, spec_parm);
19597 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19598 }
19599 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19600 {
19601 if (!DECL_PACK_P (tmpl_parm))
19602 {
19603 register_local_specialization (spec_parm, tmpl_parm);
19604 spec_parm = DECL_CHAIN (spec_parm);
19605 }
19606 else
19607 {
19608 /* Register the (value) argument pack as a specialization of
19609 TMPL_PARM, then move on. */
19610 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19611 register_local_specialization (argpack, tmpl_parm);
19612 }
19613 }
19614 gcc_assert (!spec_parm);
19615
19616 /* Substitute into the body of the function. */
19617 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19618 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19619 tf_warning_or_error, tmpl);
19620 else
19621 {
19622 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19623 tf_warning_or_error, tmpl,
19624 /*integral_constant_expression_p=*/false);
19625
19626 /* Set the current input_location to the end of the function
19627 so that finish_function knows where we are. */
19628 input_location
19629 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19630 }
19631
19632 /* We don't need the local specializations any more. */
19633 pointer_map_destroy (local_specializations);
19634 local_specializations = saved_local_specializations;
19635
19636 /* Finish the function. */
19637 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19638 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19639 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19640 else
19641 {
19642 d = finish_function (0);
19643 expand_or_defer_fn (d);
19644 }
19645
19646 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19647 cp_check_omp_declare_reduction (d);
19648 }
19649
19650 /* We're not deferring instantiation any more. */
19651 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19652
19653 if (!fn_context)
19654 pop_from_top_level ();
19655 else if (nested)
19656 pop_function_context ();
19657
19658 out:
19659 input_location = saved_loc;
19660 cp_unevaluated_operand = saved_unevaluated_operand;
19661 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19662 pop_deferring_access_checks ();
19663 pop_tinst_level ();
19664
19665 timevar_pop (TV_TEMPLATE_INST);
19666
19667 return d;
19668 }
19669
19670 /* Run through the list of templates that we wish we could
19671 instantiate, and instantiate any we can. RETRIES is the
19672 number of times we retry pending template instantiation. */
19673
19674 void
19675 instantiate_pending_templates (int retries)
19676 {
19677 int reconsider;
19678 location_t saved_loc = input_location;
19679
19680 /* Instantiating templates may trigger vtable generation. This in turn
19681 may require further template instantiations. We place a limit here
19682 to avoid infinite loop. */
19683 if (pending_templates && retries >= max_tinst_depth)
19684 {
19685 tree decl = pending_templates->tinst->decl;
19686
19687 error ("template instantiation depth exceeds maximum of %d"
19688 " instantiating %q+D, possibly from virtual table generation"
19689 " (use -ftemplate-depth= to increase the maximum)",
19690 max_tinst_depth, decl);
19691 if (TREE_CODE (decl) == FUNCTION_DECL)
19692 /* Pretend that we defined it. */
19693 DECL_INITIAL (decl) = error_mark_node;
19694 return;
19695 }
19696
19697 do
19698 {
19699 struct pending_template **t = &pending_templates;
19700 struct pending_template *last = NULL;
19701 reconsider = 0;
19702 while (*t)
19703 {
19704 tree instantiation = reopen_tinst_level ((*t)->tinst);
19705 bool complete = false;
19706
19707 if (TYPE_P (instantiation))
19708 {
19709 tree fn;
19710
19711 if (!COMPLETE_TYPE_P (instantiation))
19712 {
19713 instantiate_class_template (instantiation);
19714 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19715 for (fn = TYPE_METHODS (instantiation);
19716 fn;
19717 fn = TREE_CHAIN (fn))
19718 if (! DECL_ARTIFICIAL (fn))
19719 instantiate_decl (fn,
19720 /*defer_ok=*/0,
19721 /*expl_inst_class_mem_p=*/false);
19722 if (COMPLETE_TYPE_P (instantiation))
19723 reconsider = 1;
19724 }
19725
19726 complete = COMPLETE_TYPE_P (instantiation);
19727 }
19728 else
19729 {
19730 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19731 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19732 {
19733 instantiation
19734 = instantiate_decl (instantiation,
19735 /*defer_ok=*/0,
19736 /*expl_inst_class_mem_p=*/false);
19737 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19738 reconsider = 1;
19739 }
19740
19741 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19742 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19743 }
19744
19745 if (complete)
19746 /* If INSTANTIATION has been instantiated, then we don't
19747 need to consider it again in the future. */
19748 *t = (*t)->next;
19749 else
19750 {
19751 last = *t;
19752 t = &(*t)->next;
19753 }
19754 tinst_depth = 0;
19755 current_tinst_level = NULL;
19756 }
19757 last_pending_template = last;
19758 }
19759 while (reconsider);
19760
19761 input_location = saved_loc;
19762 }
19763
19764 /* Substitute ARGVEC into T, which is a list of initializers for
19765 either base class or a non-static data member. The TREE_PURPOSEs
19766 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19767 instantiate_decl. */
19768
19769 static tree
19770 tsubst_initializer_list (tree t, tree argvec)
19771 {
19772 tree inits = NULL_TREE;
19773
19774 for (; t; t = TREE_CHAIN (t))
19775 {
19776 tree decl;
19777 tree init;
19778 tree expanded_bases = NULL_TREE;
19779 tree expanded_arguments = NULL_TREE;
19780 int i, len = 1;
19781
19782 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19783 {
19784 tree expr;
19785 tree arg;
19786
19787 /* Expand the base class expansion type into separate base
19788 classes. */
19789 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19790 tf_warning_or_error,
19791 NULL_TREE);
19792 if (expanded_bases == error_mark_node)
19793 continue;
19794
19795 /* We'll be building separate TREE_LISTs of arguments for
19796 each base. */
19797 len = TREE_VEC_LENGTH (expanded_bases);
19798 expanded_arguments = make_tree_vec (len);
19799 for (i = 0; i < len; i++)
19800 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19801
19802 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19803 expand each argument in the TREE_VALUE of t. */
19804 expr = make_node (EXPR_PACK_EXPANSION);
19805 PACK_EXPANSION_LOCAL_P (expr) = true;
19806 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19807 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19808
19809 if (TREE_VALUE (t) == void_type_node)
19810 /* VOID_TYPE_NODE is used to indicate
19811 value-initialization. */
19812 {
19813 for (i = 0; i < len; i++)
19814 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19815 }
19816 else
19817 {
19818 /* Substitute parameter packs into each argument in the
19819 TREE_LIST. */
19820 in_base_initializer = 1;
19821 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19822 {
19823 tree expanded_exprs;
19824
19825 /* Expand the argument. */
19826 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19827 expanded_exprs
19828 = tsubst_pack_expansion (expr, argvec,
19829 tf_warning_or_error,
19830 NULL_TREE);
19831 if (expanded_exprs == error_mark_node)
19832 continue;
19833
19834 /* Prepend each of the expanded expressions to the
19835 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19836 for (i = 0; i < len; i++)
19837 {
19838 TREE_VEC_ELT (expanded_arguments, i) =
19839 tree_cons (NULL_TREE,
19840 TREE_VEC_ELT (expanded_exprs, i),
19841 TREE_VEC_ELT (expanded_arguments, i));
19842 }
19843 }
19844 in_base_initializer = 0;
19845
19846 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19847 since we built them backwards. */
19848 for (i = 0; i < len; i++)
19849 {
19850 TREE_VEC_ELT (expanded_arguments, i) =
19851 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19852 }
19853 }
19854 }
19855
19856 for (i = 0; i < len; ++i)
19857 {
19858 if (expanded_bases)
19859 {
19860 decl = TREE_VEC_ELT (expanded_bases, i);
19861 decl = expand_member_init (decl);
19862 init = TREE_VEC_ELT (expanded_arguments, i);
19863 }
19864 else
19865 {
19866 tree tmp;
19867 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19868 tf_warning_or_error, NULL_TREE);
19869
19870 decl = expand_member_init (decl);
19871 if (decl && !DECL_P (decl))
19872 in_base_initializer = 1;
19873
19874 init = TREE_VALUE (t);
19875 tmp = init;
19876 if (init != void_type_node)
19877 init = tsubst_expr (init, argvec,
19878 tf_warning_or_error, NULL_TREE,
19879 /*integral_constant_expression_p=*/false);
19880 if (init == NULL_TREE && tmp != NULL_TREE)
19881 /* If we had an initializer but it instantiated to nothing,
19882 value-initialize the object. This will only occur when
19883 the initializer was a pack expansion where the parameter
19884 packs used in that expansion were of length zero. */
19885 init = void_type_node;
19886 in_base_initializer = 0;
19887 }
19888
19889 if (decl)
19890 {
19891 init = build_tree_list (decl, init);
19892 TREE_CHAIN (init) = inits;
19893 inits = init;
19894 }
19895 }
19896 }
19897 return inits;
19898 }
19899
19900 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19901
19902 static void
19903 set_current_access_from_decl (tree decl)
19904 {
19905 if (TREE_PRIVATE (decl))
19906 current_access_specifier = access_private_node;
19907 else if (TREE_PROTECTED (decl))
19908 current_access_specifier = access_protected_node;
19909 else
19910 current_access_specifier = access_public_node;
19911 }
19912
19913 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19914 is the instantiation (which should have been created with
19915 start_enum) and ARGS are the template arguments to use. */
19916
19917 static void
19918 tsubst_enum (tree tag, tree newtag, tree args)
19919 {
19920 tree e;
19921
19922 if (SCOPED_ENUM_P (newtag))
19923 begin_scope (sk_scoped_enum, newtag);
19924
19925 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19926 {
19927 tree value;
19928 tree decl;
19929
19930 decl = TREE_VALUE (e);
19931 /* Note that in a template enum, the TREE_VALUE is the
19932 CONST_DECL, not the corresponding INTEGER_CST. */
19933 value = tsubst_expr (DECL_INITIAL (decl),
19934 args, tf_warning_or_error, NULL_TREE,
19935 /*integral_constant_expression_p=*/true);
19936
19937 /* Give this enumeration constant the correct access. */
19938 set_current_access_from_decl (decl);
19939
19940 /* Actually build the enumerator itself. */
19941 build_enumerator
19942 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19943 }
19944
19945 if (SCOPED_ENUM_P (newtag))
19946 finish_scope ();
19947
19948 finish_enum_value_list (newtag);
19949 finish_enum (newtag);
19950
19951 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19952 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19953 }
19954
19955 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19956 its type -- but without substituting the innermost set of template
19957 arguments. So, innermost set of template parameters will appear in
19958 the type. */
19959
19960 tree
19961 get_mostly_instantiated_function_type (tree decl)
19962 {
19963 tree fn_type;
19964 tree tmpl;
19965 tree targs;
19966 tree tparms;
19967 int parm_depth;
19968
19969 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19970 targs = DECL_TI_ARGS (decl);
19971 tparms = DECL_TEMPLATE_PARMS (tmpl);
19972 parm_depth = TMPL_PARMS_DEPTH (tparms);
19973
19974 /* There should be as many levels of arguments as there are levels
19975 of parameters. */
19976 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19977
19978 fn_type = TREE_TYPE (tmpl);
19979
19980 if (parm_depth == 1)
19981 /* No substitution is necessary. */
19982 ;
19983 else
19984 {
19985 int i;
19986 tree partial_args;
19987
19988 /* Replace the innermost level of the TARGS with NULL_TREEs to
19989 let tsubst know not to substitute for those parameters. */
19990 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19991 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19992 SET_TMPL_ARGS_LEVEL (partial_args, i,
19993 TMPL_ARGS_LEVEL (targs, i));
19994 SET_TMPL_ARGS_LEVEL (partial_args,
19995 TMPL_ARGS_DEPTH (targs),
19996 make_tree_vec (DECL_NTPARMS (tmpl)));
19997
19998 /* Make sure that we can see identifiers, and compute access
19999 correctly. */
20000 push_access_scope (decl);
20001
20002 ++processing_template_decl;
20003 /* Now, do the (partial) substitution to figure out the
20004 appropriate function type. */
20005 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20006 --processing_template_decl;
20007
20008 /* Substitute into the template parameters to obtain the real
20009 innermost set of parameters. This step is important if the
20010 innermost set of template parameters contains value
20011 parameters whose types depend on outer template parameters. */
20012 TREE_VEC_LENGTH (partial_args)--;
20013 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20014
20015 pop_access_scope (decl);
20016 }
20017
20018 return fn_type;
20019 }
20020
20021 /* Return truthvalue if we're processing a template different from
20022 the last one involved in diagnostics. */
20023 int
20024 problematic_instantiation_changed (void)
20025 {
20026 return current_tinst_level != last_error_tinst_level;
20027 }
20028
20029 /* Remember current template involved in diagnostics. */
20030 void
20031 record_last_problematic_instantiation (void)
20032 {
20033 last_error_tinst_level = current_tinst_level;
20034 }
20035
20036 struct tinst_level *
20037 current_instantiation (void)
20038 {
20039 return current_tinst_level;
20040 }
20041
20042 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20043 type. Return zero for ok, nonzero for disallowed. Issue error and
20044 warning messages under control of COMPLAIN. */
20045
20046 static int
20047 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20048 {
20049 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20050 return 0;
20051 else if (POINTER_TYPE_P (type))
20052 return 0;
20053 else if (TYPE_PTRMEM_P (type))
20054 return 0;
20055 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20056 return 0;
20057 else if (TREE_CODE (type) == TYPENAME_TYPE)
20058 return 0;
20059 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20060 return 0;
20061 else if (TREE_CODE (type) == NULLPTR_TYPE)
20062 return 0;
20063
20064 if (complain & tf_error)
20065 {
20066 if (type == error_mark_node)
20067 inform (input_location, "invalid template non-type parameter");
20068 else
20069 error ("%q#T is not a valid type for a template non-type parameter",
20070 type);
20071 }
20072 return 1;
20073 }
20074
20075 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20076 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20077
20078 static bool
20079 dependent_type_p_r (tree type)
20080 {
20081 tree scope;
20082
20083 /* [temp.dep.type]
20084
20085 A type is dependent if it is:
20086
20087 -- a template parameter. Template template parameters are types
20088 for us (since TYPE_P holds true for them) so we handle
20089 them here. */
20090 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20091 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20092 return true;
20093 /* -- a qualified-id with a nested-name-specifier which contains a
20094 class-name that names a dependent type or whose unqualified-id
20095 names a dependent type. */
20096 if (TREE_CODE (type) == TYPENAME_TYPE)
20097 return true;
20098 /* -- a cv-qualified type where the cv-unqualified type is
20099 dependent. */
20100 type = TYPE_MAIN_VARIANT (type);
20101 /* -- a compound type constructed from any dependent type. */
20102 if (TYPE_PTRMEM_P (type))
20103 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20104 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20105 (type)));
20106 else if (TYPE_PTR_P (type)
20107 || TREE_CODE (type) == REFERENCE_TYPE)
20108 return dependent_type_p (TREE_TYPE (type));
20109 else if (TREE_CODE (type) == FUNCTION_TYPE
20110 || TREE_CODE (type) == METHOD_TYPE)
20111 {
20112 tree arg_type;
20113
20114 if (dependent_type_p (TREE_TYPE (type)))
20115 return true;
20116 for (arg_type = TYPE_ARG_TYPES (type);
20117 arg_type;
20118 arg_type = TREE_CHAIN (arg_type))
20119 if (dependent_type_p (TREE_VALUE (arg_type)))
20120 return true;
20121 return false;
20122 }
20123 /* -- an array type constructed from any dependent type or whose
20124 size is specified by a constant expression that is
20125 value-dependent.
20126
20127 We checked for type- and value-dependence of the bounds in
20128 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20129 if (TREE_CODE (type) == ARRAY_TYPE)
20130 {
20131 if (TYPE_DOMAIN (type)
20132 && dependent_type_p (TYPE_DOMAIN (type)))
20133 return true;
20134 return dependent_type_p (TREE_TYPE (type));
20135 }
20136
20137 /* -- a template-id in which either the template name is a template
20138 parameter ... */
20139 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20140 return true;
20141 /* ... or any of the template arguments is a dependent type or
20142 an expression that is type-dependent or value-dependent. */
20143 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20144 && (any_dependent_template_arguments_p
20145 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20146 return true;
20147
20148 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20149 dependent; if the argument of the `typeof' expression is not
20150 type-dependent, then it should already been have resolved. */
20151 if (TREE_CODE (type) == TYPEOF_TYPE
20152 || TREE_CODE (type) == DECLTYPE_TYPE
20153 || TREE_CODE (type) == UNDERLYING_TYPE)
20154 return true;
20155
20156 /* A template argument pack is dependent if any of its packed
20157 arguments are. */
20158 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20159 {
20160 tree args = ARGUMENT_PACK_ARGS (type);
20161 int i, len = TREE_VEC_LENGTH (args);
20162 for (i = 0; i < len; ++i)
20163 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20164 return true;
20165 }
20166
20167 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20168 be template parameters. */
20169 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20170 return true;
20171
20172 /* The standard does not specifically mention types that are local
20173 to template functions or local classes, but they should be
20174 considered dependent too. For example:
20175
20176 template <int I> void f() {
20177 enum E { a = I };
20178 S<sizeof (E)> s;
20179 }
20180
20181 The size of `E' cannot be known until the value of `I' has been
20182 determined. Therefore, `E' must be considered dependent. */
20183 scope = TYPE_CONTEXT (type);
20184 if (scope && TYPE_P (scope))
20185 return dependent_type_p (scope);
20186 /* Don't use type_dependent_expression_p here, as it can lead
20187 to infinite recursion trying to determine whether a lambda
20188 nested in a lambda is dependent (c++/47687). */
20189 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20190 && DECL_LANG_SPECIFIC (scope)
20191 && DECL_TEMPLATE_INFO (scope)
20192 && (any_dependent_template_arguments_p
20193 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20194 return true;
20195
20196 /* Other types are non-dependent. */
20197 return false;
20198 }
20199
20200 /* Returns TRUE if TYPE is dependent, in the sense of
20201 [temp.dep.type]. Note that a NULL type is considered dependent. */
20202
20203 bool
20204 dependent_type_p (tree type)
20205 {
20206 /* If there are no template parameters in scope, then there can't be
20207 any dependent types. */
20208 if (!processing_template_decl)
20209 {
20210 /* If we are not processing a template, then nobody should be
20211 providing us with a dependent type. */
20212 gcc_assert (type);
20213 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20214 return false;
20215 }
20216
20217 /* If the type is NULL, we have not computed a type for the entity
20218 in question; in that case, the type is dependent. */
20219 if (!type)
20220 return true;
20221
20222 /* Erroneous types can be considered non-dependent. */
20223 if (type == error_mark_node)
20224 return false;
20225
20226 /* If we have not already computed the appropriate value for TYPE,
20227 do so now. */
20228 if (!TYPE_DEPENDENT_P_VALID (type))
20229 {
20230 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20231 TYPE_DEPENDENT_P_VALID (type) = 1;
20232 }
20233
20234 return TYPE_DEPENDENT_P (type);
20235 }
20236
20237 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20238 lookup. In other words, a dependent type that is not the current
20239 instantiation. */
20240
20241 bool
20242 dependent_scope_p (tree scope)
20243 {
20244 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20245 && !currently_open_class (scope));
20246 }
20247
20248 /* T is a SCOPE_REF; return whether we need to consider it
20249 instantiation-dependent so that we can check access at instantiation
20250 time even though we know which member it resolves to. */
20251
20252 static bool
20253 instantiation_dependent_scope_ref_p (tree t)
20254 {
20255 if (DECL_P (TREE_OPERAND (t, 1))
20256 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20257 && accessible_in_template_p (TREE_OPERAND (t, 0),
20258 TREE_OPERAND (t, 1)))
20259 return false;
20260 else
20261 return true;
20262 }
20263
20264 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20265 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20266 expression. */
20267
20268 /* Note that this predicate is not appropriate for general expressions;
20269 only constant expressions (that satisfy potential_constant_expression)
20270 can be tested for value dependence. */
20271
20272 bool
20273 value_dependent_expression_p (tree expression)
20274 {
20275 if (!processing_template_decl)
20276 return false;
20277
20278 /* A name declared with a dependent type. */
20279 if (DECL_P (expression) && type_dependent_expression_p (expression))
20280 return true;
20281
20282 switch (TREE_CODE (expression))
20283 {
20284 case IDENTIFIER_NODE:
20285 /* A name that has not been looked up -- must be dependent. */
20286 return true;
20287
20288 case TEMPLATE_PARM_INDEX:
20289 /* A non-type template parm. */
20290 return true;
20291
20292 case CONST_DECL:
20293 /* A non-type template parm. */
20294 if (DECL_TEMPLATE_PARM_P (expression))
20295 return true;
20296 return value_dependent_expression_p (DECL_INITIAL (expression));
20297
20298 case VAR_DECL:
20299 /* A constant with literal type and is initialized
20300 with an expression that is value-dependent.
20301
20302 Note that a non-dependent parenthesized initializer will have
20303 already been replaced with its constant value, so if we see
20304 a TREE_LIST it must be dependent. */
20305 if (DECL_INITIAL (expression)
20306 && decl_constant_var_p (expression)
20307 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20308 || value_dependent_expression_p (DECL_INITIAL (expression))))
20309 return true;
20310 return false;
20311
20312 case DYNAMIC_CAST_EXPR:
20313 case STATIC_CAST_EXPR:
20314 case CONST_CAST_EXPR:
20315 case REINTERPRET_CAST_EXPR:
20316 case CAST_EXPR:
20317 /* These expressions are value-dependent if the type to which
20318 the cast occurs is dependent or the expression being casted
20319 is value-dependent. */
20320 {
20321 tree type = TREE_TYPE (expression);
20322
20323 if (dependent_type_p (type))
20324 return true;
20325
20326 /* A functional cast has a list of operands. */
20327 expression = TREE_OPERAND (expression, 0);
20328 if (!expression)
20329 {
20330 /* If there are no operands, it must be an expression such
20331 as "int()". This should not happen for aggregate types
20332 because it would form non-constant expressions. */
20333 gcc_assert (cxx_dialect >= cxx11
20334 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20335
20336 return false;
20337 }
20338
20339 if (TREE_CODE (expression) == TREE_LIST)
20340 return any_value_dependent_elements_p (expression);
20341
20342 return value_dependent_expression_p (expression);
20343 }
20344
20345 case SIZEOF_EXPR:
20346 if (SIZEOF_EXPR_TYPE_P (expression))
20347 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20348 /* FALLTHRU */
20349 case ALIGNOF_EXPR:
20350 case TYPEID_EXPR:
20351 /* A `sizeof' expression is value-dependent if the operand is
20352 type-dependent or is a pack expansion. */
20353 expression = TREE_OPERAND (expression, 0);
20354 if (PACK_EXPANSION_P (expression))
20355 return true;
20356 else if (TYPE_P (expression))
20357 return dependent_type_p (expression);
20358 return instantiation_dependent_expression_p (expression);
20359
20360 case AT_ENCODE_EXPR:
20361 /* An 'encode' expression is value-dependent if the operand is
20362 type-dependent. */
20363 expression = TREE_OPERAND (expression, 0);
20364 return dependent_type_p (expression);
20365
20366 case NOEXCEPT_EXPR:
20367 expression = TREE_OPERAND (expression, 0);
20368 return instantiation_dependent_expression_p (expression);
20369
20370 case SCOPE_REF:
20371 /* All instantiation-dependent expressions should also be considered
20372 value-dependent. */
20373 return instantiation_dependent_scope_ref_p (expression);
20374
20375 case COMPONENT_REF:
20376 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20377 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20378
20379 case NONTYPE_ARGUMENT_PACK:
20380 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20381 is value-dependent. */
20382 {
20383 tree values = ARGUMENT_PACK_ARGS (expression);
20384 int i, len = TREE_VEC_LENGTH (values);
20385
20386 for (i = 0; i < len; ++i)
20387 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20388 return true;
20389
20390 return false;
20391 }
20392
20393 case TRAIT_EXPR:
20394 {
20395 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20396 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20397 || (type2 ? dependent_type_p (type2) : false));
20398 }
20399
20400 case MODOP_EXPR:
20401 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20402 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20403
20404 case ARRAY_REF:
20405 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20406 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20407
20408 case ADDR_EXPR:
20409 {
20410 tree op = TREE_OPERAND (expression, 0);
20411 return (value_dependent_expression_p (op)
20412 || has_value_dependent_address (op));
20413 }
20414
20415 case CALL_EXPR:
20416 {
20417 tree fn = get_callee_fndecl (expression);
20418 int i, nargs;
20419 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20420 return true;
20421 nargs = call_expr_nargs (expression);
20422 for (i = 0; i < nargs; ++i)
20423 {
20424 tree op = CALL_EXPR_ARG (expression, i);
20425 /* In a call to a constexpr member function, look through the
20426 implicit ADDR_EXPR on the object argument so that it doesn't
20427 cause the call to be considered value-dependent. We also
20428 look through it in potential_constant_expression. */
20429 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20430 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20431 && TREE_CODE (op) == ADDR_EXPR)
20432 op = TREE_OPERAND (op, 0);
20433 if (value_dependent_expression_p (op))
20434 return true;
20435 }
20436 return false;
20437 }
20438
20439 case TEMPLATE_ID_EXPR:
20440 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20441 type-dependent. */
20442 return type_dependent_expression_p (expression);
20443
20444 case CONSTRUCTOR:
20445 {
20446 unsigned ix;
20447 tree val;
20448 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20449 if (value_dependent_expression_p (val))
20450 return true;
20451 return false;
20452 }
20453
20454 case STMT_EXPR:
20455 /* Treat a GNU statement expression as dependent to avoid crashing
20456 under fold_non_dependent_expr; it can't be constant. */
20457 return true;
20458
20459 default:
20460 /* A constant expression is value-dependent if any subexpression is
20461 value-dependent. */
20462 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20463 {
20464 case tcc_reference:
20465 case tcc_unary:
20466 case tcc_comparison:
20467 case tcc_binary:
20468 case tcc_expression:
20469 case tcc_vl_exp:
20470 {
20471 int i, len = cp_tree_operand_length (expression);
20472
20473 for (i = 0; i < len; i++)
20474 {
20475 tree t = TREE_OPERAND (expression, i);
20476
20477 /* In some cases, some of the operands may be missing.l
20478 (For example, in the case of PREDECREMENT_EXPR, the
20479 amount to increment by may be missing.) That doesn't
20480 make the expression dependent. */
20481 if (t && value_dependent_expression_p (t))
20482 return true;
20483 }
20484 }
20485 break;
20486 default:
20487 break;
20488 }
20489 break;
20490 }
20491
20492 /* The expression is not value-dependent. */
20493 return false;
20494 }
20495
20496 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20497 [temp.dep.expr]. Note that an expression with no type is
20498 considered dependent. Other parts of the compiler arrange for an
20499 expression with type-dependent subexpressions to have no type, so
20500 this function doesn't have to be fully recursive. */
20501
20502 bool
20503 type_dependent_expression_p (tree expression)
20504 {
20505 if (!processing_template_decl)
20506 return false;
20507
20508 if (expression == error_mark_node)
20509 return false;
20510
20511 /* An unresolved name is always dependent. */
20512 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20513 return true;
20514
20515 /* Some expression forms are never type-dependent. */
20516 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20517 || TREE_CODE (expression) == SIZEOF_EXPR
20518 || TREE_CODE (expression) == ALIGNOF_EXPR
20519 || TREE_CODE (expression) == AT_ENCODE_EXPR
20520 || TREE_CODE (expression) == NOEXCEPT_EXPR
20521 || TREE_CODE (expression) == TRAIT_EXPR
20522 || TREE_CODE (expression) == TYPEID_EXPR
20523 || TREE_CODE (expression) == DELETE_EXPR
20524 || TREE_CODE (expression) == VEC_DELETE_EXPR
20525 || TREE_CODE (expression) == THROW_EXPR)
20526 return false;
20527
20528 /* The types of these expressions depends only on the type to which
20529 the cast occurs. */
20530 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20531 || TREE_CODE (expression) == STATIC_CAST_EXPR
20532 || TREE_CODE (expression) == CONST_CAST_EXPR
20533 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20534 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20535 || TREE_CODE (expression) == CAST_EXPR)
20536 return dependent_type_p (TREE_TYPE (expression));
20537
20538 /* The types of these expressions depends only on the type created
20539 by the expression. */
20540 if (TREE_CODE (expression) == NEW_EXPR
20541 || TREE_CODE (expression) == VEC_NEW_EXPR)
20542 {
20543 /* For NEW_EXPR tree nodes created inside a template, either
20544 the object type itself or a TREE_LIST may appear as the
20545 operand 1. */
20546 tree type = TREE_OPERAND (expression, 1);
20547 if (TREE_CODE (type) == TREE_LIST)
20548 /* This is an array type. We need to check array dimensions
20549 as well. */
20550 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20551 || value_dependent_expression_p
20552 (TREE_OPERAND (TREE_VALUE (type), 1));
20553 else
20554 return dependent_type_p (type);
20555 }
20556
20557 if (TREE_CODE (expression) == SCOPE_REF)
20558 {
20559 tree scope = TREE_OPERAND (expression, 0);
20560 tree name = TREE_OPERAND (expression, 1);
20561
20562 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20563 contains an identifier associated by name lookup with one or more
20564 declarations declared with a dependent type, or...a
20565 nested-name-specifier or qualified-id that names a member of an
20566 unknown specialization. */
20567 return (type_dependent_expression_p (name)
20568 || dependent_scope_p (scope));
20569 }
20570
20571 if (TREE_CODE (expression) == FUNCTION_DECL
20572 && DECL_LANG_SPECIFIC (expression)
20573 && DECL_TEMPLATE_INFO (expression)
20574 && (any_dependent_template_arguments_p
20575 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20576 return true;
20577
20578 if (TREE_CODE (expression) == TEMPLATE_DECL
20579 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20580 return false;
20581
20582 if (TREE_CODE (expression) == STMT_EXPR)
20583 expression = stmt_expr_value_expr (expression);
20584
20585 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20586 {
20587 tree elt;
20588 unsigned i;
20589
20590 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20591 {
20592 if (type_dependent_expression_p (elt))
20593 return true;
20594 }
20595 return false;
20596 }
20597
20598 /* A static data member of the current instantiation with incomplete
20599 array type is type-dependent, as the definition and specializations
20600 can have different bounds. */
20601 if (VAR_P (expression)
20602 && DECL_CLASS_SCOPE_P (expression)
20603 && dependent_type_p (DECL_CONTEXT (expression))
20604 && VAR_HAD_UNKNOWN_BOUND (expression))
20605 return true;
20606
20607 /* An array of unknown bound depending on a variadic parameter, eg:
20608
20609 template<typename... Args>
20610 void foo (Args... args)
20611 {
20612 int arr[] = { args... };
20613 }
20614
20615 template<int... vals>
20616 void bar ()
20617 {
20618 int arr[] = { vals... };
20619 }
20620
20621 If the array has no length and has an initializer, it must be that
20622 we couldn't determine its length in cp_complete_array_type because
20623 it is dependent. */
20624 if (VAR_P (expression)
20625 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20626 && !TYPE_DOMAIN (TREE_TYPE (expression))
20627 && DECL_INITIAL (expression))
20628 return true;
20629
20630 if (TREE_TYPE (expression) == unknown_type_node)
20631 {
20632 if (TREE_CODE (expression) == ADDR_EXPR)
20633 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20634 if (TREE_CODE (expression) == COMPONENT_REF
20635 || TREE_CODE (expression) == OFFSET_REF)
20636 {
20637 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20638 return true;
20639 expression = TREE_OPERAND (expression, 1);
20640 if (identifier_p (expression))
20641 return false;
20642 }
20643 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20644 if (TREE_CODE (expression) == SCOPE_REF)
20645 return false;
20646
20647 /* Always dependent, on the number of arguments if nothing else. */
20648 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20649 return true;
20650
20651 if (BASELINK_P (expression))
20652 expression = BASELINK_FUNCTIONS (expression);
20653
20654 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20655 {
20656 if (any_dependent_template_arguments_p
20657 (TREE_OPERAND (expression, 1)))
20658 return true;
20659 expression = TREE_OPERAND (expression, 0);
20660 }
20661 gcc_assert (TREE_CODE (expression) == OVERLOAD
20662 || TREE_CODE (expression) == FUNCTION_DECL);
20663
20664 while (expression)
20665 {
20666 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20667 return true;
20668 expression = OVL_NEXT (expression);
20669 }
20670 return false;
20671 }
20672
20673 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20674
20675 return (dependent_type_p (TREE_TYPE (expression)));
20676 }
20677
20678 /* walk_tree callback function for instantiation_dependent_expression_p,
20679 below. Returns non-zero if a dependent subexpression is found. */
20680
20681 static tree
20682 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20683 void * /*data*/)
20684 {
20685 if (TYPE_P (*tp))
20686 {
20687 /* We don't have to worry about decltype currently because decltype
20688 of an instantiation-dependent expr is a dependent type. This
20689 might change depending on the resolution of DR 1172. */
20690 *walk_subtrees = false;
20691 return NULL_TREE;
20692 }
20693 enum tree_code code = TREE_CODE (*tp);
20694 switch (code)
20695 {
20696 /* Don't treat an argument list as dependent just because it has no
20697 TREE_TYPE. */
20698 case TREE_LIST:
20699 case TREE_VEC:
20700 return NULL_TREE;
20701
20702 case VAR_DECL:
20703 case CONST_DECL:
20704 /* A constant with a dependent initializer is dependent. */
20705 if (value_dependent_expression_p (*tp))
20706 return *tp;
20707 break;
20708
20709 case TEMPLATE_PARM_INDEX:
20710 return *tp;
20711
20712 /* Handle expressions with type operands. */
20713 case SIZEOF_EXPR:
20714 case ALIGNOF_EXPR:
20715 case TYPEID_EXPR:
20716 case AT_ENCODE_EXPR:
20717 {
20718 tree op = TREE_OPERAND (*tp, 0);
20719 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20720 op = TREE_TYPE (op);
20721 if (TYPE_P (op))
20722 {
20723 if (dependent_type_p (op))
20724 return *tp;
20725 else
20726 {
20727 *walk_subtrees = false;
20728 return NULL_TREE;
20729 }
20730 }
20731 break;
20732 }
20733
20734 case TRAIT_EXPR:
20735 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20736 || (TRAIT_EXPR_TYPE2 (*tp)
20737 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20738 return *tp;
20739 *walk_subtrees = false;
20740 return NULL_TREE;
20741
20742 case COMPONENT_REF:
20743 if (identifier_p (TREE_OPERAND (*tp, 1)))
20744 /* In a template, finish_class_member_access_expr creates a
20745 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20746 type-dependent, so that we can check access control at
20747 instantiation time (PR 42277). See also Core issue 1273. */
20748 return *tp;
20749 break;
20750
20751 case SCOPE_REF:
20752 if (instantiation_dependent_scope_ref_p (*tp))
20753 return *tp;
20754 else
20755 break;
20756
20757 /* Treat statement-expressions as dependent. */
20758 case BIND_EXPR:
20759 return *tp;
20760
20761 default:
20762 break;
20763 }
20764
20765 if (type_dependent_expression_p (*tp))
20766 return *tp;
20767 else
20768 return NULL_TREE;
20769 }
20770
20771 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20772 sense defined by the ABI:
20773
20774 "An expression is instantiation-dependent if it is type-dependent
20775 or value-dependent, or it has a subexpression that is type-dependent
20776 or value-dependent." */
20777
20778 bool
20779 instantiation_dependent_expression_p (tree expression)
20780 {
20781 tree result;
20782
20783 if (!processing_template_decl)
20784 return false;
20785
20786 if (expression == error_mark_node)
20787 return false;
20788
20789 result = cp_walk_tree_without_duplicates (&expression,
20790 instantiation_dependent_r, NULL);
20791 return result != NULL_TREE;
20792 }
20793
20794 /* Like type_dependent_expression_p, but it also works while not processing
20795 a template definition, i.e. during substitution or mangling. */
20796
20797 bool
20798 type_dependent_expression_p_push (tree expr)
20799 {
20800 bool b;
20801 ++processing_template_decl;
20802 b = type_dependent_expression_p (expr);
20803 --processing_template_decl;
20804 return b;
20805 }
20806
20807 /* Returns TRUE if ARGS contains a type-dependent expression. */
20808
20809 bool
20810 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20811 {
20812 unsigned int i;
20813 tree arg;
20814
20815 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20816 {
20817 if (type_dependent_expression_p (arg))
20818 return true;
20819 }
20820 return false;
20821 }
20822
20823 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20824 expressions) contains any type-dependent expressions. */
20825
20826 bool
20827 any_type_dependent_elements_p (const_tree list)
20828 {
20829 for (; list; list = TREE_CHAIN (list))
20830 if (type_dependent_expression_p (TREE_VALUE (list)))
20831 return true;
20832
20833 return false;
20834 }
20835
20836 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20837 expressions) contains any value-dependent expressions. */
20838
20839 bool
20840 any_value_dependent_elements_p (const_tree list)
20841 {
20842 for (; list; list = TREE_CHAIN (list))
20843 if (value_dependent_expression_p (TREE_VALUE (list)))
20844 return true;
20845
20846 return false;
20847 }
20848
20849 /* Returns TRUE if the ARG (a template argument) is dependent. */
20850
20851 bool
20852 dependent_template_arg_p (tree arg)
20853 {
20854 if (!processing_template_decl)
20855 return false;
20856
20857 /* Assume a template argument that was wrongly written by the user
20858 is dependent. This is consistent with what
20859 any_dependent_template_arguments_p [that calls this function]
20860 does. */
20861 if (!arg || arg == error_mark_node)
20862 return true;
20863
20864 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20865 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20866
20867 if (TREE_CODE (arg) == TEMPLATE_DECL
20868 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20869 return dependent_template_p (arg);
20870 else if (ARGUMENT_PACK_P (arg))
20871 {
20872 tree args = ARGUMENT_PACK_ARGS (arg);
20873 int i, len = TREE_VEC_LENGTH (args);
20874 for (i = 0; i < len; ++i)
20875 {
20876 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20877 return true;
20878 }
20879
20880 return false;
20881 }
20882 else if (TYPE_P (arg))
20883 return dependent_type_p (arg);
20884 else
20885 return (type_dependent_expression_p (arg)
20886 || value_dependent_expression_p (arg));
20887 }
20888
20889 /* Returns true if ARGS (a collection of template arguments) contains
20890 any types that require structural equality testing. */
20891
20892 bool
20893 any_template_arguments_need_structural_equality_p (tree args)
20894 {
20895 int i;
20896 int j;
20897
20898 if (!args)
20899 return false;
20900 if (args == error_mark_node)
20901 return true;
20902
20903 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20904 {
20905 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20906 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20907 {
20908 tree arg = TREE_VEC_ELT (level, j);
20909 tree packed_args = NULL_TREE;
20910 int k, len = 1;
20911
20912 if (ARGUMENT_PACK_P (arg))
20913 {
20914 /* Look inside the argument pack. */
20915 packed_args = ARGUMENT_PACK_ARGS (arg);
20916 len = TREE_VEC_LENGTH (packed_args);
20917 }
20918
20919 for (k = 0; k < len; ++k)
20920 {
20921 if (packed_args)
20922 arg = TREE_VEC_ELT (packed_args, k);
20923
20924 if (error_operand_p (arg))
20925 return true;
20926 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20927 continue;
20928 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20929 return true;
20930 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20931 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20932 return true;
20933 }
20934 }
20935 }
20936
20937 return false;
20938 }
20939
20940 /* Returns true if ARGS (a collection of template arguments) contains
20941 any dependent arguments. */
20942
20943 bool
20944 any_dependent_template_arguments_p (const_tree args)
20945 {
20946 int i;
20947 int j;
20948
20949 if (!args)
20950 return false;
20951 if (args == error_mark_node)
20952 return true;
20953
20954 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20955 {
20956 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20957 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20958 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20959 return true;
20960 }
20961
20962 return false;
20963 }
20964
20965 /* Returns TRUE if the template TMPL is dependent. */
20966
20967 bool
20968 dependent_template_p (tree tmpl)
20969 {
20970 if (TREE_CODE (tmpl) == OVERLOAD)
20971 {
20972 while (tmpl)
20973 {
20974 if (dependent_template_p (OVL_CURRENT (tmpl)))
20975 return true;
20976 tmpl = OVL_NEXT (tmpl);
20977 }
20978 return false;
20979 }
20980
20981 /* Template template parameters are dependent. */
20982 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20983 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20984 return true;
20985 /* So are names that have not been looked up. */
20986 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
20987 return true;
20988 /* So are member templates of dependent classes. */
20989 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
20990 return dependent_type_p (DECL_CONTEXT (tmpl));
20991 return false;
20992 }
20993
20994 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20995
20996 bool
20997 dependent_template_id_p (tree tmpl, tree args)
20998 {
20999 return (dependent_template_p (tmpl)
21000 || any_dependent_template_arguments_p (args));
21001 }
21002
21003 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21004 is dependent. */
21005
21006 bool
21007 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21008 {
21009 int i;
21010
21011 if (!processing_template_decl)
21012 return false;
21013
21014 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21015 {
21016 tree decl = TREE_VEC_ELT (declv, i);
21017 tree init = TREE_VEC_ELT (initv, i);
21018 tree cond = TREE_VEC_ELT (condv, i);
21019 tree incr = TREE_VEC_ELT (incrv, i);
21020
21021 if (type_dependent_expression_p (decl))
21022 return true;
21023
21024 if (init && type_dependent_expression_p (init))
21025 return true;
21026
21027 if (type_dependent_expression_p (cond))
21028 return true;
21029
21030 if (COMPARISON_CLASS_P (cond)
21031 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21032 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21033 return true;
21034
21035 if (TREE_CODE (incr) == MODOP_EXPR)
21036 {
21037 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21038 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21039 return true;
21040 }
21041 else if (type_dependent_expression_p (incr))
21042 return true;
21043 else if (TREE_CODE (incr) == MODIFY_EXPR)
21044 {
21045 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21046 return true;
21047 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21048 {
21049 tree t = TREE_OPERAND (incr, 1);
21050 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21051 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21052 return true;
21053 }
21054 }
21055 }
21056
21057 return false;
21058 }
21059
21060 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21061 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21062 no such TYPE can be found. Note that this function peers inside
21063 uninstantiated templates and therefore should be used only in
21064 extremely limited situations. ONLY_CURRENT_P restricts this
21065 peering to the currently open classes hierarchy (which is required
21066 when comparing types). */
21067
21068 tree
21069 resolve_typename_type (tree type, bool only_current_p)
21070 {
21071 tree scope;
21072 tree name;
21073 tree decl;
21074 int quals;
21075 tree pushed_scope;
21076 tree result;
21077
21078 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21079
21080 scope = TYPE_CONTEXT (type);
21081 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21082 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21083 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21084 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21085 identifier of the TYPENAME_TYPE anymore.
21086 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21087 TYPENAME_TYPE instead, we avoid messing up with a possible
21088 typedef variant case. */
21089 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21090
21091 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21092 it first before we can figure out what NAME refers to. */
21093 if (TREE_CODE (scope) == TYPENAME_TYPE)
21094 {
21095 if (TYPENAME_IS_RESOLVING_P (scope))
21096 /* Given a class template A with a dependent base with nested type C,
21097 typedef typename A::C::C C will land us here, as trying to resolve
21098 the initial A::C leads to the local C typedef, which leads back to
21099 A::C::C. So we break the recursion now. */
21100 return type;
21101 else
21102 scope = resolve_typename_type (scope, only_current_p);
21103 }
21104 /* If we don't know what SCOPE refers to, then we cannot resolve the
21105 TYPENAME_TYPE. */
21106 if (TREE_CODE (scope) == TYPENAME_TYPE)
21107 return type;
21108 /* If the SCOPE is a template type parameter, we have no way of
21109 resolving the name. */
21110 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21111 return type;
21112 /* If the SCOPE is not the current instantiation, there's no reason
21113 to look inside it. */
21114 if (only_current_p && !currently_open_class (scope))
21115 return type;
21116 /* If this is a typedef, we don't want to look inside (c++/11987). */
21117 if (typedef_variant_p (type))
21118 return type;
21119 /* If SCOPE isn't the template itself, it will not have a valid
21120 TYPE_FIELDS list. */
21121 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21122 /* scope is either the template itself or a compatible instantiation
21123 like X<T>, so look up the name in the original template. */
21124 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21125 else
21126 /* scope is a partial instantiation, so we can't do the lookup or we
21127 will lose the template arguments. */
21128 return type;
21129 /* Enter the SCOPE so that name lookup will be resolved as if we
21130 were in the class definition. In particular, SCOPE will no
21131 longer be considered a dependent type. */
21132 pushed_scope = push_scope (scope);
21133 /* Look up the declaration. */
21134 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21135 tf_warning_or_error);
21136
21137 result = NULL_TREE;
21138
21139 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21140 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21141 if (!decl)
21142 /*nop*/;
21143 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21144 && TREE_CODE (decl) == TYPE_DECL)
21145 {
21146 result = TREE_TYPE (decl);
21147 if (result == error_mark_node)
21148 result = NULL_TREE;
21149 }
21150 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21151 && DECL_CLASS_TEMPLATE_P (decl))
21152 {
21153 tree tmpl;
21154 tree args;
21155 /* Obtain the template and the arguments. */
21156 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21157 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21158 /* Instantiate the template. */
21159 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21160 /*entering_scope=*/0,
21161 tf_error | tf_user);
21162 if (result == error_mark_node)
21163 result = NULL_TREE;
21164 }
21165
21166 /* Leave the SCOPE. */
21167 if (pushed_scope)
21168 pop_scope (pushed_scope);
21169
21170 /* If we failed to resolve it, return the original typename. */
21171 if (!result)
21172 return type;
21173
21174 /* If lookup found a typename type, resolve that too. */
21175 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21176 {
21177 /* Ill-formed programs can cause infinite recursion here, so we
21178 must catch that. */
21179 TYPENAME_IS_RESOLVING_P (type) = 1;
21180 result = resolve_typename_type (result, only_current_p);
21181 TYPENAME_IS_RESOLVING_P (type) = 0;
21182 }
21183
21184 /* Qualify the resulting type. */
21185 quals = cp_type_quals (type);
21186 if (quals)
21187 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21188
21189 return result;
21190 }
21191
21192 /* EXPR is an expression which is not type-dependent. Return a proxy
21193 for EXPR that can be used to compute the types of larger
21194 expressions containing EXPR. */
21195
21196 tree
21197 build_non_dependent_expr (tree expr)
21198 {
21199 tree inner_expr;
21200
21201 #ifdef ENABLE_CHECKING
21202 /* Try to get a constant value for all non-dependent expressions in
21203 order to expose bugs in *_dependent_expression_p and constexpr. */
21204 if (cxx_dialect >= cxx11)
21205 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21206 #endif
21207
21208 /* Preserve OVERLOADs; the functions must be available to resolve
21209 types. */
21210 inner_expr = expr;
21211 if (TREE_CODE (inner_expr) == STMT_EXPR)
21212 inner_expr = stmt_expr_value_expr (inner_expr);
21213 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21214 inner_expr = TREE_OPERAND (inner_expr, 0);
21215 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21216 inner_expr = TREE_OPERAND (inner_expr, 1);
21217 if (is_overloaded_fn (inner_expr)
21218 || TREE_CODE (inner_expr) == OFFSET_REF)
21219 return expr;
21220 /* There is no need to return a proxy for a variable. */
21221 if (VAR_P (expr))
21222 return expr;
21223 /* Preserve string constants; conversions from string constants to
21224 "char *" are allowed, even though normally a "const char *"
21225 cannot be used to initialize a "char *". */
21226 if (TREE_CODE (expr) == STRING_CST)
21227 return expr;
21228 /* Preserve arithmetic constants, as an optimization -- there is no
21229 reason to create a new node. */
21230 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21231 return expr;
21232 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21233 There is at least one place where we want to know that a
21234 particular expression is a throw-expression: when checking a ?:
21235 expression, there are special rules if the second or third
21236 argument is a throw-expression. */
21237 if (TREE_CODE (expr) == THROW_EXPR)
21238 return expr;
21239
21240 /* Don't wrap an initializer list, we need to be able to look inside. */
21241 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21242 return expr;
21243
21244 /* Don't wrap a dummy object, we need to be able to test for it. */
21245 if (is_dummy_object (expr))
21246 return expr;
21247
21248 if (TREE_CODE (expr) == COND_EXPR)
21249 return build3 (COND_EXPR,
21250 TREE_TYPE (expr),
21251 TREE_OPERAND (expr, 0),
21252 (TREE_OPERAND (expr, 1)
21253 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21254 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21255 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21256 if (TREE_CODE (expr) == COMPOUND_EXPR
21257 && !COMPOUND_EXPR_OVERLOADED (expr))
21258 return build2 (COMPOUND_EXPR,
21259 TREE_TYPE (expr),
21260 TREE_OPERAND (expr, 0),
21261 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21262
21263 /* If the type is unknown, it can't really be non-dependent */
21264 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21265
21266 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21267 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21268 }
21269
21270 /* ARGS is a vector of expressions as arguments to a function call.
21271 Replace the arguments with equivalent non-dependent expressions.
21272 This modifies ARGS in place. */
21273
21274 void
21275 make_args_non_dependent (vec<tree, va_gc> *args)
21276 {
21277 unsigned int ix;
21278 tree arg;
21279
21280 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21281 {
21282 tree newarg = build_non_dependent_expr (arg);
21283 if (newarg != arg)
21284 (*args)[ix] = newarg;
21285 }
21286 }
21287
21288 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21289 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21290 parms. */
21291
21292 static tree
21293 make_auto_1 (tree name)
21294 {
21295 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21296 TYPE_NAME (au) = build_decl (input_location,
21297 TYPE_DECL, name, au);
21298 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21299 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21300 (0, processing_template_decl + 1, processing_template_decl + 1,
21301 TYPE_NAME (au), NULL_TREE);
21302 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21303 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21304 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21305
21306 return au;
21307 }
21308
21309 tree
21310 make_decltype_auto (void)
21311 {
21312 return make_auto_1 (get_identifier ("decltype(auto)"));
21313 }
21314
21315 tree
21316 make_auto (void)
21317 {
21318 return make_auto_1 (get_identifier ("auto"));
21319 }
21320
21321 /* Given type ARG, return std::initializer_list<ARG>. */
21322
21323 static tree
21324 listify (tree arg)
21325 {
21326 tree std_init_list = namespace_binding
21327 (get_identifier ("initializer_list"), std_node);
21328 tree argvec;
21329 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21330 {
21331 error ("deducing from brace-enclosed initializer list requires "
21332 "#include <initializer_list>");
21333 return error_mark_node;
21334 }
21335 argvec = make_tree_vec (1);
21336 TREE_VEC_ELT (argvec, 0) = arg;
21337 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21338 NULL_TREE, 0, tf_warning_or_error);
21339 }
21340
21341 /* Replace auto in TYPE with std::initializer_list<auto>. */
21342
21343 static tree
21344 listify_autos (tree type, tree auto_node)
21345 {
21346 tree init_auto = listify (auto_node);
21347 tree argvec = make_tree_vec (1);
21348 TREE_VEC_ELT (argvec, 0) = init_auto;
21349 if (processing_template_decl)
21350 argvec = add_to_template_args (current_template_args (), argvec);
21351 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21352 }
21353
21354 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21355 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21356
21357 tree
21358 do_auto_deduction (tree type, tree init, tree auto_node)
21359 {
21360 tree targs;
21361
21362 if (init == error_mark_node)
21363 return error_mark_node;
21364
21365 if (type_dependent_expression_p (init))
21366 /* Defining a subset of type-dependent expressions that we can deduce
21367 from ahead of time isn't worth the trouble. */
21368 return type;
21369
21370 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21371 with either a new invented type template parameter U or, if the
21372 initializer is a braced-init-list (8.5.4), with
21373 std::initializer_list<U>. */
21374 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21375 type = listify_autos (type, auto_node);
21376
21377 init = resolve_nondeduced_context (init);
21378
21379 targs = make_tree_vec (1);
21380 if (AUTO_IS_DECLTYPE (auto_node))
21381 {
21382 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21383 TREE_VEC_ELT (targs, 0)
21384 = finish_decltype_type (init, id, tf_warning_or_error);
21385 if (type != auto_node)
21386 {
21387 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21388 return error_mark_node;
21389 }
21390 }
21391 else
21392 {
21393 tree parms = build_tree_list (NULL_TREE, type);
21394 tree tparms = make_tree_vec (1);
21395 int val;
21396
21397 TREE_VEC_ELT (tparms, 0)
21398 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21399 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21400 DEDUCE_CALL, LOOKUP_NORMAL,
21401 NULL, /*explain_p=*/false);
21402 if (val > 0)
21403 {
21404 if (processing_template_decl)
21405 /* Try again at instantiation time. */
21406 return type;
21407 if (type && type != error_mark_node)
21408 /* If type is error_mark_node a diagnostic must have been
21409 emitted by now. Also, having a mention to '<type error>'
21410 in the diagnostic is not really useful to the user. */
21411 {
21412 if (cfun && auto_node == current_function_auto_return_pattern
21413 && LAMBDA_FUNCTION_P (current_function_decl))
21414 error ("unable to deduce lambda return type from %qE", init);
21415 else
21416 error ("unable to deduce %qT from %qE", type, init);
21417 }
21418 return error_mark_node;
21419 }
21420 }
21421
21422 /* If the list of declarators contains more than one declarator, the type
21423 of each declared variable is determined as described above. If the
21424 type deduced for the template parameter U is not the same in each
21425 deduction, the program is ill-formed. */
21426 if (TREE_TYPE (auto_node)
21427 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21428 {
21429 if (cfun && auto_node == current_function_auto_return_pattern
21430 && LAMBDA_FUNCTION_P (current_function_decl))
21431 error ("inconsistent types %qT and %qT deduced for "
21432 "lambda return type", TREE_TYPE (auto_node),
21433 TREE_VEC_ELT (targs, 0));
21434 else
21435 error ("inconsistent deduction for %qT: %qT and then %qT",
21436 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21437 return error_mark_node;
21438 }
21439 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21440
21441 if (processing_template_decl)
21442 targs = add_to_template_args (current_template_args (), targs);
21443 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21444 }
21445
21446 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21447 result. */
21448
21449 tree
21450 splice_late_return_type (tree type, tree late_return_type)
21451 {
21452 tree argvec;
21453
21454 if (late_return_type == NULL_TREE)
21455 return type;
21456 argvec = make_tree_vec (1);
21457 TREE_VEC_ELT (argvec, 0) = late_return_type;
21458 if (processing_template_parmlist)
21459 /* For a late-specified return type in a template type-parameter, we
21460 need to add a dummy argument level for its parmlist. */
21461 argvec = add_to_template_args
21462 (make_tree_vec (processing_template_parmlist), argvec);
21463 if (current_template_parms)
21464 argvec = add_to_template_args (current_template_args (), argvec);
21465 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21466 }
21467
21468 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21469 'decltype(auto)'. */
21470
21471 bool
21472 is_auto (const_tree type)
21473 {
21474 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21475 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21476 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21477 return true;
21478 else
21479 return false;
21480 }
21481
21482 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21483 a use of `auto'. Returns NULL_TREE otherwise. */
21484
21485 tree
21486 type_uses_auto (tree type)
21487 {
21488 return find_type_usage (type, is_auto);
21489 }
21490
21491 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21492 'decltype(auto)' or a concept. */
21493
21494 bool
21495 is_auto_or_concept (const_tree type)
21496 {
21497 return is_auto (type); // or concept
21498 }
21499
21500 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21501 a concept identifier) iff TYPE contains a use of a generic type. Returns
21502 NULL_TREE otherwise. */
21503
21504 tree
21505 type_uses_auto_or_concept (tree type)
21506 {
21507 return find_type_usage (type, is_auto_or_concept);
21508 }
21509
21510
21511 /* For a given template T, return the vector of typedefs referenced
21512 in T for which access check is needed at T instantiation time.
21513 T is either a FUNCTION_DECL or a RECORD_TYPE.
21514 Those typedefs were added to T by the function
21515 append_type_to_template_for_access_check. */
21516
21517 vec<qualified_typedef_usage_t, va_gc> *
21518 get_types_needing_access_check (tree t)
21519 {
21520 tree ti;
21521 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21522
21523 if (!t || t == error_mark_node)
21524 return NULL;
21525
21526 if (!(ti = get_template_info (t)))
21527 return NULL;
21528
21529 if (CLASS_TYPE_P (t)
21530 || TREE_CODE (t) == FUNCTION_DECL)
21531 {
21532 if (!TI_TEMPLATE (ti))
21533 return NULL;
21534
21535 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21536 }
21537
21538 return result;
21539 }
21540
21541 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21542 tied to T. That list of typedefs will be access checked at
21543 T instantiation time.
21544 T is either a FUNCTION_DECL or a RECORD_TYPE.
21545 TYPE_DECL is a TYPE_DECL node representing a typedef.
21546 SCOPE is the scope through which TYPE_DECL is accessed.
21547 LOCATION is the location of the usage point of TYPE_DECL.
21548
21549 This function is a subroutine of
21550 append_type_to_template_for_access_check. */
21551
21552 static void
21553 append_type_to_template_for_access_check_1 (tree t,
21554 tree type_decl,
21555 tree scope,
21556 location_t location)
21557 {
21558 qualified_typedef_usage_t typedef_usage;
21559 tree ti;
21560
21561 if (!t || t == error_mark_node)
21562 return;
21563
21564 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21565 || CLASS_TYPE_P (t))
21566 && type_decl
21567 && TREE_CODE (type_decl) == TYPE_DECL
21568 && scope);
21569
21570 if (!(ti = get_template_info (t)))
21571 return;
21572
21573 gcc_assert (TI_TEMPLATE (ti));
21574
21575 typedef_usage.typedef_decl = type_decl;
21576 typedef_usage.context = scope;
21577 typedef_usage.locus = location;
21578
21579 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21580 }
21581
21582 /* Append TYPE_DECL to the template TEMPL.
21583 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21584 At TEMPL instanciation time, TYPE_DECL will be checked to see
21585 if it can be accessed through SCOPE.
21586 LOCATION is the location of the usage point of TYPE_DECL.
21587
21588 e.g. consider the following code snippet:
21589
21590 class C
21591 {
21592 typedef int myint;
21593 };
21594
21595 template<class U> struct S
21596 {
21597 C::myint mi; // <-- usage point of the typedef C::myint
21598 };
21599
21600 S<char> s;
21601
21602 At S<char> instantiation time, we need to check the access of C::myint
21603 In other words, we need to check the access of the myint typedef through
21604 the C scope. For that purpose, this function will add the myint typedef
21605 and the scope C through which its being accessed to a list of typedefs
21606 tied to the template S. That list will be walked at template instantiation
21607 time and access check performed on each typedefs it contains.
21608 Note that this particular code snippet should yield an error because
21609 myint is private to C. */
21610
21611 void
21612 append_type_to_template_for_access_check (tree templ,
21613 tree type_decl,
21614 tree scope,
21615 location_t location)
21616 {
21617 qualified_typedef_usage_t *iter;
21618 unsigned i;
21619
21620 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21621
21622 /* Make sure we don't append the type to the template twice. */
21623 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21624 if (iter->typedef_decl == type_decl && scope == iter->context)
21625 return;
21626
21627 append_type_to_template_for_access_check_1 (templ, type_decl,
21628 scope, location);
21629 }
21630
21631 /* Set up the hash tables for template instantiations. */
21632
21633 void
21634 init_template_processing (void)
21635 {
21636 decl_specializations = htab_create_ggc (37,
21637 hash_specialization,
21638 eq_specializations,
21639 ggc_free);
21640 type_specializations = htab_create_ggc (37,
21641 hash_specialization,
21642 eq_specializations,
21643 ggc_free);
21644 }
21645
21646 /* Print stats about the template hash tables for -fstats. */
21647
21648 void
21649 print_template_statistics (void)
21650 {
21651 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21652 "%f collisions\n", (long) htab_size (decl_specializations),
21653 (long) htab_elements (decl_specializations),
21654 htab_collisions (decl_specializations));
21655 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21656 "%f collisions\n", (long) htab_size (type_specializations),
21657 (long) htab_elements (type_specializations),
21658 htab_collisions (type_specializations));
21659 }
21660
21661 #include "gt-cp-pt.h"